summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@android.com>2010-05-10 09:49:46 -0700
committerAndroid Git Automerger <android-git-automerger@android.com>2010-05-10 09:49:46 -0700
commitc279d3c542ff266670ae8aef8d57932cda2c249a (patch)
tree15924ef8746458978e2bcb788bea4b4ba26b72d6
parent7f2ad58a6d9cb70da934825a7648a5d3f8cbc498 (diff)
parent218faa30d547e549c1efbf9bacf23cc56c8bf481 (diff)
downloadsystem_core-c279d3c542ff266670ae8aef8d57932cda2c249a.tar.gz
system_core-c279d3c542ff266670ae8aef8d57932cda2c249a.tar.bz2
system_core-c279d3c542ff266670ae8aef8d57932cda2c249a.zip
am 218faa30: am e43c2483: More fixes to Binder perf regression from Eclair
Merge commit '218faa30d547e549c1efbf9bacf23cc56c8bf481' into kraken * commit '218faa30d547e549c1efbf9bacf23cc56c8bf481': More fixes to Binder perf regression from Eclair
-rw-r--r--libcutils/sched_policy.c79
1 files changed, 47 insertions, 32 deletions
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c
index 6d2727ba4..78e094376 100644
--- a/libcutils/sched_policy.c
+++ b/libcutils/sched_policy.c
@@ -22,6 +22,7 @@
#include <string.h>
#include <errno.h>
#include <fcntl.h>
+#include <pthread.h>
#define LOG_TAG "SchedPolicy"
#include "cutils/log.h"
@@ -42,15 +43,28 @@
#define POLICY_DEBUG 0
+static pthread_once_t the_once = PTHREAD_ONCE_INIT;
+
static int __sys_supports_schedgroups = -1;
-/* Add tid to the group defined by dev_path ("/dev/cpuctl/.../tasks") */
-static int add_tid_to_cgroup(int tid, const char *dev_path)
+// File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error.
+static int normal_cgroup_fd = -1;
+static int bg_cgroup_fd = -1;
+
+/* Add tid to the scheduling group defined by the policy */
+static int add_tid_to_cgroup(int tid, SchedPolicy policy)
{
int fd;
- if ((fd = open(dev_path, O_WRONLY)) < 0) {
- SLOGE("add_tid_to_cgroup failed to open '%s' (%s)\n", dev_path,
- strerror(errno));
+
+ if (policy == SP_BACKGROUND) {
+ fd = bg_cgroup_fd;
+ } else {
+ fd = normal_cgroup_fd;
+ }
+
+ if (fd < 0) {
+ SLOGE("add_tid_to_cgroup failed; background=%d\n",
+ policy == SP_BACKGROUND ? 1 : 0);
return -1;
}
@@ -65,30 +79,38 @@ static int add_tid_to_cgroup(int tid, const char *dev_path)
}
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' to '%s' (%s)\n",
- ptr, dev_path, strerror(errno));
+ /*
+ * 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); background=%d\n",
+ ptr, strerror(errno), policy == SP_BACKGROUND ? 1 : 0);
return -1;
}
- close(fd);
return 0;
}
-static inline void initialize()
-{
- if (__sys_supports_schedgroups < 0) {
- if (!access("/dev/cpuctl/tasks", F_OK)) {
- __sys_supports_schedgroups = 1;
- } else {
- __sys_supports_schedgroups = 0;
+static void __initialize(void) {
+ char* filename;
+ if (!access("/dev/cpuctl/tasks", F_OK)) {
+ __sys_supports_schedgroups = 1;
+
+ filename = "/dev/cpuctl/tasks";
+ normal_cgroup_fd = open(filename, O_WRONLY);
+ if (normal_cgroup_fd < 0) {
+ SLOGE("open of %s failed: %s\n", filename, strerror(errno));
+ }
+
+ filename = "/dev/cpuctl/bg_non_interactive/tasks";
+ bg_cgroup_fd = open(filename, O_WRONLY);
+ if (bg_cgroup_fd < 0) {
+ SLOGE("open of %s failed: %s\n", filename, strerror(errno));
}
+ } else {
+ __sys_supports_schedgroups = 0;
}
}
@@ -166,7 +188,7 @@ static int getSchedulerGroup(int tid, char* buf, size_t bufLen)
int get_sched_policy(int tid, SchedPolicy *policy)
{
- initialize();
+ pthread_once(&the_once, __initialize);
if (__sys_supports_schedgroups) {
char grpBuf[32];
@@ -198,7 +220,7 @@ int get_sched_policy(int tid, SchedPolicy *policy)
int set_sched_policy(int tid, SchedPolicy policy)
{
- initialize();
+ pthread_once(&the_once, __initialize);
#if POLICY_DEBUG
char statfile[64];
@@ -233,14 +255,7 @@ int set_sched_policy(int tid, SchedPolicy policy)
#endif
if (__sys_supports_schedgroups) {
- const char *dev_path;
- if (policy == SP_BACKGROUND) {
- dev_path = "/dev/cpuctl/bg_non_interactive/tasks";
- } else {
- dev_path = "/dev/cpuctl/tasks";
- }
-
- if (add_tid_to_cgroup(tid, dev_path)) {
+ if (add_tid_to_cgroup(tid, policy)) {
if (errno != ESRCH && errno != ENOENT)
return -errno;
}