Permalink
Browse files

Optimize set_sched_policy(), which gets called in every binder call.

- use static /dev/cpuctl filenames, rather than sprintf() on the fly
- use a custom formatter instead of sprintf() for the tid number

This is a simplified version of Dan's original
Ifc9c81f74fe65f0695ead53b30194bc6adf00da0 to be less risky for Froyo.

Bug: 2660235
Change-Id: I744bbc2ec01fd2569612c814a497df40a933b622
  • Loading branch information...
1 parent 458f063 commit 253e27acb6c9e3e0b03c59f25cdf9ecbd64bcdad @bradfitz bradfitz committed May 6, 2010
Showing with 22 additions and 16 deletions.
  1. +22 −16 libcutils/sched_policy.c
View
@@ -44,31 +44,36 @@
static int __sys_supports_schedgroups = -1;
-static int add_tid_to_cgroup(int tid, const char *grp_name)
+/* Add tid to the group defined by dev_path ("/dev/cpuctl/.../tasks") */
+static int add_tid_to_cgroup(int tid, const char *dev_path)
{
int fd;
- char path[255];
- char text[64];
-
- sprintf(path, "/dev/cpuctl/%s/tasks", grp_name);
-
- if ((fd = open(path, O_WRONLY)) < 0) {
- SLOGE("add_tid_to_cgroup failed to open '%s' (%s)\n", path,
+ if ((fd = open(dev_path, O_WRONLY)) < 0) {
+ SLOGE("add_tid_to_cgroup failed to open '%s' (%s)\n", dev_path,
strerror(errno));
return -1;
}
- sprintf(text, "%d", tid);
- if (write(fd, text, strlen(text)) < 0) {
+ // specialized itoa -- works for tid > 0
+ char text[22];
+ char *end = text + sizeof(text) - 1;
+ char *ptr = end;
+ *ptr = '\0';
+ while (tid > 0) {
+ *--ptr = '0' + (tid % 10);
+ tid = tid / 10;
+ }
+
+ if (write(fd, ptr, end - ptr) < 0) {
close(fd);
/*
* If the thread is in the process of exiting,
* don't flag an error
*/
if (errno == ESRCH)
return 0;
- SLOGW("add_tid_to_cgroup failed to write '%s' (%s)\n", path,
- strerror(errno));
+ SLOGW("add_tid_to_cgroup failed to write '%s' to '%s' (%s)\n",
+ ptr, dev_path, strerror(errno));
return -1;
}
@@ -228,13 +233,14 @@ int set_sched_policy(int tid, SchedPolicy policy)
#endif
if (__sys_supports_schedgroups) {
- const char *grp = "";
-
+ const char *dev_path;
if (policy == SP_BACKGROUND) {
- grp = "bg_non_interactive";
+ dev_path = "/dev/cpuctl/bg_non_interactive/tasks";
+ } else {
+ dev_path = "/dev/cpuctl/tasks";
}
- if (add_tid_to_cgroup(tid, grp)) {
+ if (add_tid_to_cgroup(tid, dev_path)) {
if (errno != ESRCH && errno != ENOENT)
return -errno;
}

0 comments on commit 253e27a

Please sign in to comment.