void os_changeThreadPriority(Thread* thread, int newPriority) { FASTIVA_LATE_IMPL(); #if 0 if (newPriority < 1 || newPriority > 10) { ALOGW("bad priority %d", newPriority); newPriority = 5; } int newNice = kNiceValues[newPriority-1]; pid_t pid = thread->systemTid; if (newNice >= ANDROID_PRIORITY_BACKGROUND) { set_sched_policy(dvmGetSysThreadId(), SP_BACKGROUND); } else if (getpriority(PRIO_PROCESS, pid) >= ANDROID_PRIORITY_BACKGROUND) { set_sched_policy(dvmGetSysThreadId(), SP_FOREGROUND); } if (setpriority(PRIO_PROCESS, pid, newNice) != 0) { std::string threadName(dvmGetThreadName(thread)); ALOGI("setPriority(%d) '%s' to prio=%d(n=%d) failed: %s", pid, threadName.c_str(), newPriority, newNice, strerror(errno)); } else { ALOGV("setPriority(%d) to prio=%d(n=%d)", pid, newPriority, newNice); } #endif }
int os_raiseThreadPriority() { /* Get the priority (the "nice" value) of the current thread. The * getpriority() call can legitimately return -1, so we have to * explicitly test errno. */ errno = 0; int oldThreadPriority = getpriority(PRIO_PROCESS, 0); if (errno != 0) { ALOGI("getpriority(self) failed: %s", strerror(errno)); } else if (oldThreadPriority > ANDROID_PRIORITY_NORMAL) { /* Current value is numerically greater than "normal", which * in backward UNIX terms means lower priority. */ if (oldThreadPriority >= ANDROID_PRIORITY_BACKGROUND) { set_sched_policy(dvmGetSysThreadId(), SP_FOREGROUND); } if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_NORMAL) != 0) { ALOGI("Unable to elevate priority from %d to %d", oldThreadPriority, ANDROID_PRIORITY_NORMAL); } else { /* * The priority has been elevated. Return the old value * so the caller can restore it later. */ ALOGV("Elevating priority from %d to %d", oldThreadPriority, ANDROID_PRIORITY_NORMAL); return oldThreadPriority; } } return INT_MAX; }
/* * 参数 : void* arg I 线程参数 * 返回 : void * 描述 : 报告线程 * 历史 : * 备注 : */ static void* report_thread(void *arg) { uint64_t event_type; event_payload_u_type event_data; set_sched_policy(gettid(), SP_FOREGROUND); while (1) { pthread_mutex_lock(&inter_context.data_lock); if (inter_context.report_thread_status == REPORT_STATUS_IDEL) { inter_context.release_wakelock_cb(); pthread_cond_wait(&inter_context.data_cond, &inter_context.data_lock); inter_context.acquire_wakelock_cb(); } if (inter_context.report_thread_status == REPORT_STATUS_QUIT) { pthread_mutex_unlock(&inter_context.data_lock); break; } inter_context.report_thread_status = REPORT_STATUS_IDEL; event_type = inter_context.event_type; //lzg todo memcpy(&event_data, &inter_context.event_payload, sizeof(event_data)); pthread_mutex_unlock(&inter_context.data_lock); //GPS_LOGD("report_hread report..."); iwise_interface_process_loc_event(event_type, &event_data); } return 0; }
// static void *TimedEventQueue::ThreadWrapper(void *me) { #ifdef ANDROID_SIMULATOR // The simulator runs everything as one process, so any // Binder calls happen on this thread instead of a thread // in another process. We therefore need to make sure that // this thread can do calls into interpreted code. // On the device this is not an issue because the remote // thread will already be set up correctly for this. JavaVM *vm; int numvms; JNI_GetCreatedJavaVMs(&vm, 1, &numvms); JNIEnv *env; vm->AttachCurrentThread(&env, NULL); #endif #ifndef TARGET_OMAP4 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_FOREGROUND); #else setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_URGENT_AUDIO); #endif set_sched_policy(androidGetTid(), SP_FOREGROUND); static_cast<TimedEventQueue *>(me)->threadEntry(); #ifdef ANDROID_SIMULATOR vm->DetachCurrentThread(); #endif return NULL; }
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; switch (policy) { case SP_BACKGROUND: fd = bg_cpuset_fd; break; case SP_FOREGROUND: case SP_AUDIO_APP: case SP_AUDIO_SYS: fd = fg_cpuset_fd; break; default: fd = -1; break; } if (add_tid_to_cgroup(tid, fd) != 0) { if (errno != ESRCH && errno != ENOENT) return -errno; } return 0; #endif }
static void *reinit_thread_start(void * /*obj*/) { prctl(PR_SET_NAME, "logd.daemon"); set_sched_policy(0, SP_BACKGROUND); setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND); setgid(AID_SYSTEM); setuid(AID_SYSTEM); while (reinit_running && !sem_wait(&reinit) && reinit_running) { // uidToName Privileged Worker if (uid) { name = NULL; FILE *fp = fopen("/data/system/packages.list", "r"); if (fp) { // This simple parser is sensitive to format changes in // frameworks/base/services/core/java/com/android/server/pm/Settings.java // A dependency note has been added to that file to correct // this parser. char *buffer = NULL; size_t len; while (getline(&buffer, &len, fp) > 0) { char *userId = strchr(buffer, ' '); if (!userId) { continue; } *userId = '\0'; unsigned long value = strtoul(userId + 1, NULL, 10); if (value != uid) { continue; } name = strdup(buffer); break; } free(buffer); fclose(fp); } uid = 0; sem_post(&uidName); continue; } if (fdDmesg >= 0) { static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO), 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':', ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' }; write(fdDmesg, reinit_message, sizeof(reinit_message)); } // Anything that reads persist.<property> if (logBuf) { logBuf->init(); } } return NULL; }
static int drop_privs() { struct sched_param param; memset(¶m, 0, sizeof(param)); if (set_sched_policy(0, SP_BACKGROUND) < 0) { return -1; } if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { return -1; } if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { return -1; } if (prctl(PR_SET_KEEPCAPS, 1) < 0) { return -1; } gid_t groups[] = { AID_READPROC }; if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) == -1) { return -1; } if (setgid(AID_LOGD) != 0) { return -1; } if (setuid(AID_LOGD) != 0) { return -1; } struct __user_cap_header_struct capheader; struct __user_cap_data_struct capdata[2]; memset(&capheader, 0, sizeof(capheader)); memset(&capdata, 0, sizeof(capdata)); capheader.version = _LINUX_CAPABILITY_VERSION_3; capheader.pid = 0; capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG); capdata[CAP_TO_INDEX(CAP_AUDIT_CONTROL)].permitted |= CAP_TO_MASK(CAP_AUDIT_CONTROL); capdata[0].effective = capdata[0].permitted; capdata[1].effective = capdata[1].permitted; capdata[0].inheritable = 0; capdata[1].inheritable = 0; if (capset(&capheader, &capdata[0]) < 0) { return -1; } return 0; }
void os_lowerThreadPriority(int oldThreadPriority) { if (setpriority(PRIO_PROCESS, 0, oldThreadPriority) != 0) { ALOGW("Unable to reset priority to %d: %s", oldThreadPriority, strerror(errno)); } else { ALOGV("Reset priority to %d", oldThreadPriority); } if (oldThreadPriority >= ANDROID_PRIORITY_BACKGROUND) { set_sched_policy(dvmGetSysThreadId(), SP_BACKGROUND); } }
main(void) { set_sched_policy(1); //int i = 0; //for (i = 0; i<100000; ++i); int ret_fork; ret_fork = -1; ret_fork = fork(); //write(1, "\n1: ", 4); //write_int(ret_fork); if (ret_fork == 0){ write(1, "fork1!", 6); WL1(); } ret_fork = -1; ret_fork = fork(); //write(1, "\n2: ", 4); //write_int(ret_fork); if (ret_fork == 0){ write(1, "fork2!", 6); WL2(); } ret_fork = -1; ret_fork = fork(); //write(1, "\n3: ", 4); //write_int(ret_fork); if (ret_fork == 0){ write(1, "fork3!", 6); WL3(); } //char *inutilbuf; //read(0, &inutilbuf, 1000); //print_stats(getpid()); while (1); }
static void setupOutput() { if (g_outputFileName == NULL) { g_outFD = STDOUT_FILENO; } else { if (set_sched_policy(0, SP_BACKGROUND) < 0) { fprintf(stderr, "failed to set background scheduling policy\n"); } struct sched_param param; memset(¶m, 0, sizeof(param)); if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { fprintf(stderr, "failed to set to batch scheduler\n"); } if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { fprintf(stderr, "failed set to priority\n"); } g_outFD = openLogFile (g_outputFileName); if (g_outFD < 0) { logcat_panic(false, "couldn't open output file"); } struct stat statbuf; if (fstat(g_outFD, &statbuf) == -1) { close(g_outFD); logcat_panic(false, "couldn't get output file stat\n"); } if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) { close(g_outFD); logcat_panic(false, "invalid output file stat\n"); } g_outByteCount = statbuf.st_size; } }
static void *reinit_thread_start(void * /*obj*/) { prctl(PR_SET_NAME, "logd.daemon"); set_sched_policy(0, SP_BACKGROUND); setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND); setgid(AID_SYSTEM); setuid(AID_SYSTEM); while (reinit_running && !sem_wait(&reinit) && reinit_running) { // uidToName Privileged Worker if (uid) { name = NULL; packagelist_parse(package_list_parser_cb, NULL); uid = 0; sem_post(&uidName); continue; } if (fdDmesg >= 0) { static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO), 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':', ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' }; write(fdDmesg, reinit_message, sizeof(reinit_message)); } // Anything that reads persist.<property> if (logBuf) { logBuf->init(); logBuf->initPrune(NULL); } if (logAudit) { logAudit->allowSafeMode(); } } return NULL; }
void AudioRecord::stop() { AutoMutex lock(mLock); if (!mActive) { return; } mActive = false; mProxy->interrupt(); mAudioRecord->stop(); // the record head position will reset to 0, so if a marker is set, we need // to activate it again mMarkerReached = false; sp<AudioRecordThread> t = mAudioRecordThread; if (t != 0) { t->pause(); } else { setpriority(PRIO_PROCESS, 0, mPreviousPriority); set_sched_policy(0, mPreviousSchedulingGroup); } }
/***************************************************************************** ** ** Function raise_priority_a2dp ** ** Description Raise task priority for A2DP streaming ** ** Returns void ** *******************************************************************************/ void raise_priority_a2dp(tHIGH_PRIORITY_TASK high_task) { int rc = 0; int tid = gettid(); pthread_mutex_lock(&gIdxLock); g_TaskIdx = high_task; pthread_once(&g_DoSchedulingGroupOnce[g_TaskIdx], check_do_scheduling_group); if (g_DoSchedulingGroup[g_TaskIdx]) { // set_sched_policy does not support tid == 0 rc = set_sched_policy(tid, SP_FOREGROUND); } pthread_mutex_unlock(&gIdxLock); if (rc) { ALOGW("failed to change sched policy, tid %d, err: %d", tid, errno); } if (setpriority(PRIO_PROCESS, tid, ANDROID_PRIORITY_AUDIO) < 0) { ALOGW("failed to change priority tid: %d to %d", tid, ANDROID_PRIORITY_AUDIO); } }
void AudioRecord::stop() { AutoMutex lock(mLock); if (!mActive) { return; } mActive = false; mProxy->interrupt(); mAudioRecord->stop(); // Note: legacy handling - stop does not clear record marker and // periodic update position; we update those on start(). sp<AudioRecordThread> t = mAudioRecordThread; if (t != 0) { t->pause(); } else { setpriority(PRIO_PROCESS, 0, mPreviousPriority); set_sched_policy(0, mPreviousSchedulingGroup); } }
int GonkSchedulePolicyService::requestPriority(int32_t pid, int32_t tid, int32_t prio) { // See SchedulingPolicyService.java #define PRIORITY_MIN 1 #define PRIORITY_MAX 3 IPCThreadState* ipcState = IPCThreadState::self(); if (ipcState->getCallingUid() != AID_MEDIA || prio < PRIORITY_MIN || prio > PRIORITY_MAX || !tidBelongsToPid(tid, pid)) return -1; /* PackageManager.PERMISSION_DENIED */ set_sched_policy(tid, ipcState->getCallingPid() == pid ? SP_AUDIO_SYS : SP_AUDIO_APP); struct sched_param param; param.sched_priority = prio; int rc = sched_setscheduler(tid, SCHED_FIFO, ¶m); if (rc) return -1; return 0; /* PackageManger.PERMISSION_GRANTED */ }
static int drop_privs(bool klogd, bool auditd) { // Tricky, if ro.build.type is "eng" then this is true because of the // side effect that ro.debuggable == 1 as well, else it is false. bool eng = __android_logger_property_get_bool("ro.build.type", BOOL_DEFAULT_FALSE); struct sched_param param; memset(¶m, 0, sizeof(param)); if (set_sched_policy(0, SP_BACKGROUND) < 0) { android::prdebug("failed to set background scheduling policy"); if (!eng) return -1; } if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) { android::prdebug("failed to set batch scheduler"); if (!eng) return -1; } if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { android::prdebug("failed to set background cgroup"); if (!eng) return -1; } if (!eng && (prctl(PR_SET_DUMPABLE, 0) < 0)) { android::prdebug("failed to clear PR_SET_DUMPABLE"); return -1; } if (prctl(PR_SET_KEEPCAPS, 1) < 0) { android::prdebug("failed to set PR_SET_KEEPCAPS"); if (!eng) return -1; } std::unique_ptr<struct _cap_struct, int (*)(void*)> caps(cap_init(), cap_free); if (cap_clear(caps.get()) < 0) return -1; cap_value_t cap_value[] = { CAP_SETGID, // must be first for below klogd ? CAP_SYSLOG : CAP_SETGID, auditd ? CAP_AUDIT_CONTROL : CAP_SETGID }; if (cap_set_flag(caps.get(), CAP_PERMITTED, arraysize(cap_value), cap_value, CAP_SET) < 0) { return -1; } if (cap_set_flag(caps.get(), CAP_EFFECTIVE, arraysize(cap_value), cap_value, CAP_SET) < 0) { return -1; } if (cap_set_proc(caps.get()) < 0) { android::prdebug( "failed to set CAP_SETGID, CAP_SYSLOG or CAP_AUDIT_CONTROL (%d)", errno); if (!eng) return -1; } gid_t groups[] = { AID_READPROC }; if (setgroups(arraysize(groups), groups) == -1) { android::prdebug("failed to set AID_READPROC groups"); if (!eng) return -1; } if (setgid(AID_LOGD) != 0) { android::prdebug("failed to set AID_LOGD gid"); if (!eng) return -1; } if (setuid(AID_LOGD) != 0) { android::prdebug("failed to set AID_LOGD uid"); if (!eng) return -1; } if (cap_set_flag(caps.get(), CAP_PERMITTED, 1, cap_value, CAP_CLEAR) < 0) { return -1; } if (cap_set_flag(caps.get(), CAP_EFFECTIVE, 1, cap_value, CAP_CLEAR) < 0) { return -1; } if (cap_set_proc(caps.get()) < 0) { android::prdebug("failed to clear CAP_SETGID (%d)", errno); if (!eng) return -1; } return 0; }
static void* reinit_thread_start(void* /*obj*/) { prctl(PR_SET_NAME, "logd.daemon"); set_sched_policy(0, SP_BACKGROUND); setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND); // We should drop to AID_LOGD, if we are anything else, we have // even lesser privileges and accept our fate. gid_t groups[] = { AID_SYSTEM, // search access to /data/system path AID_PACKAGE_INFO, // readonly access to /data/system/packages.list }; if (setgroups(arraysize(groups), groups) == -1) { android::prdebug( "logd.daemon: failed to set AID_SYSTEM AID_PACKAGE_INFO groups"); } if (setgid(AID_LOGD) != 0) { android::prdebug("logd.daemon: failed to set AID_LOGD gid"); } if (setuid(AID_LOGD) != 0) { android::prdebug("logd.daemon: failed to set AID_LOGD uid"); } cap_t caps = cap_init(); (void)cap_clear(caps); (void)cap_set_proc(caps); (void)cap_free(caps); while (reinit_running && !sem_wait(&reinit) && reinit_running) { // uidToName Privileged Worker if (uid) { name = nullptr; // if we got the perms wrong above, this would spam if we reported // problems with acquisition of an uid name from the packages. (void)packagelist_parse(package_list_parser_cb, nullptr); uid = 0; sem_post(&uidName); continue; } if (fdDmesg >= 0) { static const char reinit_message[] = { KMSG_PRIORITY(LOG_INFO), 'l', 'o', 'g', 'd', '.', 'd', 'a', 'e', 'm', 'o', 'n', ':', ' ', 'r', 'e', 'i', 'n', 'i', 't', '\n' }; write(fdDmesg, reinit_message, sizeof(reinit_message)); } // Anything that reads persist.<property> if (logBuf) { logBuf->init(); logBuf->initPrune(nullptr); } android::ReReadEventLogTags(); } return nullptr; }
static void setSchedPolicy(const Parcel& data) { SchedPolicy policy = (SchedPolicy) data.readInt32(); set_sched_policy(gettid(), policy); }
static void restoreSchedPolicy() { set_sched_policy(gettid(), SP_FOREGROUND); }