int set_cpuset_policy(int tid, SchedPolicy policy) { // in the absence of cpusets, use the old sched policy #ifndef USE_CPUSETS return set_sched_policy(tid, policy); #else if (tid == 0) { tid = gettid(); } policy = _policy(policy); pthread_once(&the_once, __initialize); int fd = -1; int boost_fd = -1; switch (policy) { case SP_BACKGROUND: fd = bg_cpuset_fd; boost_fd = bg_schedboost_fd; break; case SP_FOREGROUND: case SP_AUDIO_APP: case SP_AUDIO_SYS: fd = fg_cpuset_fd; boost_fd = fg_schedboost_fd; break; case SP_TOP_APP : fd = ta_cpuset_fd; boost_fd = fg_schedboost_fd; break; case SP_SYSTEM: fd = system_bg_cpuset_fd; boost_fd = bg_schedboost_fd; break; default: boost_fd = fd = -1; break; } if (add_tid_to_cgroup(tid, fd) != 0) { if (errno != ESRCH && errno != ENOENT) return -errno; } if (boost_fd > 0 && add_tid_to_cgroup(tid, boost_fd) != 0) { if (errno != ESRCH && errno != ENOENT) return -errno; } return 0; #endif }
int set_sched_policy(int tid, SchedPolicy policy) { initialize(); #if POLICY_DEBUG char statfile[64]; char statline[1024]; char thread_name[255]; int fd; sprintf(statfile, "/proc/%d/stat", tid); memset(thread_name, 0, sizeof(thread_name)); fd = open(statfile, O_RDONLY); if (fd >= 0) { int rc = read(fd, statline, 1023); close(fd); statline[rc] = 0; char *p = statline; char *q; for (p = statline; *p != '('; p++); p++; for (q = p; *q != ')'; q++); strncpy(thread_name, p, (q-p)); } if (policy == SP_BACKGROUND) { LOGD("vvv tid %d (%s)", tid, thread_name); } else if (policy == SP_FOREGROUND) { LOGD("^^^ tid %d (%s)", tid, thread_name); } else { LOGD("??? tid %d (%s)", tid, thread_name); } #endif if (__sys_supports_schedgroups) { const char *grp = ""; if (policy == SP_BACKGROUND) { grp = "bg_non_interactive"; } if (add_tid_to_cgroup(tid, grp)) { if (errno != ESRCH && errno != ENOENT) return -errno; } } else { struct sched_param param; param.sched_priority = 0; sched_setscheduler(tid, (policy == SP_BACKGROUND) ? SCHED_BATCH : SCHED_NORMAL, ¶m); } return 0; }
int set_sched_policy(int tid, SchedPolicy policy) { if (tid == 0) { tid = gettid(); } policy = _policy(policy); pthread_once(&the_once, __initialize); #if POLICY_DEBUG char statfile[64]; char statline[1024]; char thread_name[255]; int fd; sprintf(statfile, "/proc/%d/stat", tid); memset(thread_name, 0, sizeof(thread_name)); fd = open(statfile, O_RDONLY); if (fd >= 0) { int rc = read(fd, statline, 1023); close(fd); statline[rc] = 0; char *p = statline; char *q; for (p = statline; *p != '('; p++); p++; for (q = p; *q != ')'; q++); strncpy(thread_name, p, (q-p)); } switch (policy) { case SP_BACKGROUND: SLOGD("vvv tid %d (%s)", tid, thread_name); break; case SP_FOREGROUND: case SP_AUDIO_APP: case SP_AUDIO_SYS: case SP_TOP_APP: SLOGD("^^^ tid %d (%s)", tid, thread_name); break; case SP_SYSTEM: SLOGD("/// tid %d (%s)", tid, thread_name); break; default: SLOGD("??? tid %d (%s)", tid, thread_name); break; } #endif if (__sys_supports_schedgroups) { int fd; switch (policy) { case SP_BACKGROUND: fd = bg_cgroup_fd; break; case SP_FOREGROUND: case SP_AUDIO_APP: case SP_AUDIO_SYS: case SP_TOP_APP: fd = fg_cgroup_fd; break; default: fd = -1; break; } if (add_tid_to_cgroup(tid, fd) != 0) { if (errno != ESRCH && errno != ENOENT) return -errno; } } else { struct sched_param param; param.sched_priority = 0; sched_setscheduler(tid, (policy == SP_BACKGROUND) ? SCHED_BATCH : SCHED_NORMAL, ¶m); } prctl(PR_SET_TIMERSLACK_PID, policy == SP_BACKGROUND ? TIMER_SLACK_BG : TIMER_SLACK_FG, tid); return 0; }