San Mehat | 493dad9 | 2009-09-12 10:06:57 -0700 | [diff] [blame] | 1 | /* libs/cutils/sched_policy.c |
| 2 | ** |
| 3 | ** Copyright 2007, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <sched.h> |
San Mehat | 493dad9 | 2009-09-12 10:06:57 -0700 | [diff] [blame] | 25 | |
| 26 | #include <cutils/sched_policy.h> |
| 27 | |
San Mehat | 3cd5b66 | 2009-09-14 16:05:24 -0700 | [diff] [blame^] | 28 | #ifndef SCHED_NORMAL |
| 29 | #define SCHED_NORMAL 0 |
| 30 | #endif |
| 31 | |
| 32 | #ifndef SCHED_BATCH |
| 33 | #define SCHED_BATCH 3 |
| 34 | #endif |
| 35 | |
San Mehat | 493dad9 | 2009-09-12 10:06:57 -0700 | [diff] [blame] | 36 | static 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 | |
| 57 | int 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 | ¶m); |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |