summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSan Mehat2010-03-02 19:09:56 -0600
committerSan Mehat2010-03-02 19:09:56 -0600
commit503df2075991cd03ddf43d14e05768a2138b9028 (patch)
treef5dfd88121a7024652a2cf6d0555f5f8cb2250cd /libcutils/sched_policy.c
parent026b017a9b922604e9dcd1dbcce88df153250ae7 (diff)
downloadplatform-system-core-503df2075991cd03ddf43d14e05768a2138b9028.tar.gz
platform-system-core-503df2075991cd03ddf43d14e05768a2138b9028.tar.xz
platform-system-core-503df2075991cd03ddf43d14e05768a2138b9028.zip
cutils: sched_policy: Make getSchedulerGroup() play nicely with multiple control groups
Signed-off-by: San Mehat <san@google.com>
Diffstat (limited to 'libcutils/sched_policy.c')
-rw-r--r--libcutils/sched_policy.c64
1 files changed, 44 insertions, 20 deletions
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c
index 8134aa13a..2333762ff 100644
--- a/libcutils/sched_policy.c
+++ b/libcutils/sched_policy.c
@@ -90,8 +90,9 @@ static inline void initialize()
90/* 90/*
91 * Try to get the scheduler group. 91 * Try to get the scheduler group.
92 * 92 *
93 * The data from /proc/<pid>/cgroup looks like: 93 * The data from /proc/<pid>/cgroup looks (something) like:
94 * 2:cpu:/bg_non_interactive 94 * 2:cpu:/bg_non_interactive
95 * 1:cpuacct:/
95 * 96 *
96 * We return the part after the "/", which will be an empty string for 97 * We return the part after the "/", which will be an empty string for
97 * the default cgroup. If the string is longer than "bufLen", the string 98 * the default cgroup. If the string is longer than "bufLen", the string
@@ -101,34 +102,57 @@ static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
101{ 102{
102#ifdef HAVE_ANDROID_OS 103#ifdef HAVE_ANDROID_OS
103 char pathBuf[32]; 104 char pathBuf[32];
104 char readBuf[256]; 105 char lineBuf[256];
105 ssize_t count; 106 FILE *fp;
106 int fd;
107 107
108 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid); 108 snprintf(pathBuf, sizeof(pathBuf), "/proc/%d/cgroup", tid);
109 if ((fd = open(pathBuf, O_RDONLY)) < 0) { 109 if (!(fp = fopen(pathBuf, "r"))) {
110 return -1; 110 return -1;
111 } 111 }
112 112
113 count = read(fd, readBuf, sizeof(readBuf)); 113 while(fgets(lineBuf, sizeof(lineBuf) -1, fp)) {
114 if (count <= 0) { 114 char *next = lineBuf;
115 close(fd); 115 char *subsys;
116 errno = ENODATA; 116 char *grp;
117 return -1; 117 size_t len;
118 }
119 close(fd);
120 118
121 readBuf[--count] = '\0'; /* remove the '\n', now count==strlen */ 119 /* Junk the first field */
120 if (!strsep(&next, ":")) {
121 goto out_bad_data;
122 }
122 123
123 char* cp = strchr(readBuf, '/'); 124 if (!(subsys = strsep(&next, ":"))) {
124 if (cp == NULL) { 125 goto out_bad_data;
125 readBuf[sizeof(readBuf)-1] = '\0'; 126 }
126 errno = ENODATA; 127
127 return -1; 128 if (strcmp(subsys, "cpu")) {
129 /* Not the subsys we're looking for */
130 continue;
131 }
132
133 if (!(grp = strsep(&next, ":"))) {
134 goto out_bad_data;
135 }
136 grp++; /* Drop the leading '/' */
137 len = strlen(grp);
138 grp[len-1] = '\0'; /* Drop the trailing '\n' */
139
140 if (bufLen <= len) {
141 len = bufLen - 1;
142 }
143 strncpy(buf, grp, len);
144 buf[len] = '\0';
145 fclose(fp);
146 return 0;
128 } 147 }
129 148
130 memcpy(buf, cp+1, count); /* count-1 for cp+1, count+1 for NUL */ 149 LOGE("Failed to find cpu subsys");
131 return 0; 150 fclose(fp);
151 return -1;
152 out_bad_data:
153 LOGE("Bad cgroup data {%s}", lineBuf);
154 fclose(fp);
155 return -1;
132#else 156#else
133 errno = ENOSYS; 157 errno = ENOSYS;
134 return -1; 158 return -1;