sched_policy.c revision 3cd5b66ba05cffe38bfa4e2da9b93292ba0b073a
1b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley/* libs/cutils/sched_policy.c
2b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley**
3b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** Copyright 2007, The Android Open Source Project
4b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley**
5b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** Licensed under the Apache License, Version 2.0 (the "License");
6b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** you may not use this file except in compliance with the License.
7b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** You may obtain a copy of the License at
8b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley**
9b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley**     http://www.apache.org/licenses/LICENSE-2.0
10b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley**
11b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** Unless required by applicable law or agreed to in writing, software
12b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** distributed under the License is distributed on an "AS IS" BASIS,
13b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** See the License for the specific language governing permissions and
15b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley** limitations under the License.
16b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley*/
17b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley
18b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#include <stdio.h>
19b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#include <stdlib.h>
20b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#include <unistd.h>
21b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#include <string.h>
22b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#include <errno.h>
23b4eca4b24af9c80ebb2a7fa2ba539a48096b7576Stephen Smalley#include <fcntl.h>
24#include <sched.h>
25
26#include <cutils/sched_policy.h>
27
28#ifndef SCHED_NORMAL
29  #define SCHED_NORMAL 0
30#endif
31
32#ifndef SCHED_BATCH
33  #define SCHED_BATCH 3
34#endif
35
36static int add_tid_to_cgroup(int tid, const char *grp_name)
37{
38    int fd;
39    char path[255];
40    char text[64];
41
42    sprintf(path, "/dev/cpuctl/%s/tasks", grp_name);
43
44    if ((fd = open(path, O_WRONLY)) < 0)
45        return -1;
46
47    sprintf(text, "%d", tid);
48    if (write(fd, text, strlen(text)) < 0) {
49        close(fd);
50        return -1;
51    }
52
53    close(fd);
54    return 0;
55}
56
57int set_sched_policy(int tid, SchedPolicy policy)
58{
59    static int __sys_supports_schedgroups = -1;
60
61    if (__sys_supports_schedgroups < 0) {
62        if (!access("/dev/cpuctl/tasks", F_OK)) {
63            __sys_supports_schedgroups = 1;
64        } else {
65            __sys_supports_schedgroups = 0;
66        }
67    }
68
69    if (__sys_supports_schedgroups) {
70        const char *grp = NULL;
71
72        if (policy == SP_BACKGROUND) {
73            grp = "bg_non_interactive";
74        }
75
76        if (add_tid_to_cgroup(tid, grp)) {
77            if (errno != ESRCH && errno != ENOENT)
78                return -errno;
79        }
80    } else {
81        struct sched_param param;
82
83        param.sched_priority = 0;
84        sched_setscheduler(tid,
85                           (policy == SP_BACKGROUND) ?
86                            SCHED_BATCH : SCHED_NORMAL,
87                           &param);
88    }
89
90    return 0;
91}
92