blob: f86d0f461dd82964d4e3afc8415b5f262e093206 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "thread.h"
18
19#include <sys/time.h>
20#include <sys/resource.h>
21#include <limits.h>
22#include <errno.h>
23
24#include <cutils/sched_policy.h>
25#include <utils/threads.h>
26
27#include "macros.h"
28
29namespace art {
30
Elliott Hughes00e446e2012-05-22 15:10:45 -070031// Conversion map for "nice" values.
32//
33// We use Android thread priority constants to be consistent with the rest
34// of the system. In some cases adjacent entries may overlap.
35//
Elliott Hughes8daa0922011-09-11 13:46:25 -070036static const int kNiceValues[10] = {
Elliott Hughes00e446e2012-05-22 15:10:45 -070037 ANDROID_PRIORITY_LOWEST, // 1 (MIN_PRIORITY)
Elliott Hughes8daa0922011-09-11 13:46:25 -070038 ANDROID_PRIORITY_BACKGROUND + 6,
39 ANDROID_PRIORITY_BACKGROUND + 3,
40 ANDROID_PRIORITY_BACKGROUND,
Elliott Hughes00e446e2012-05-22 15:10:45 -070041 ANDROID_PRIORITY_NORMAL, // 5 (NORM_PRIORITY)
Elliott Hughes8daa0922011-09-11 13:46:25 -070042 ANDROID_PRIORITY_NORMAL - 2,
43 ANDROID_PRIORITY_NORMAL - 4,
44 ANDROID_PRIORITY_URGENT_DISPLAY + 3,
45 ANDROID_PRIORITY_URGENT_DISPLAY + 2,
Elliott Hughes00e446e2012-05-22 15:10:45 -070046 ANDROID_PRIORITY_URGENT_DISPLAY // 10 (MAX_PRIORITY)
Elliott Hughes8daa0922011-09-11 13:46:25 -070047};
48
49void Thread::SetNativePriority(int newPriority) {
50 if (newPriority < 1 || newPriority > 10) {
51 LOG(WARNING) << "bad priority " << newPriority;
52 newPriority = 5;
53 }
54
55 int newNice = kNiceValues[newPriority-1];
56 pid_t tid = GetTid();
57
58 if (newNice >= ANDROID_PRIORITY_BACKGROUND) {
59 set_sched_policy(tid, SP_BACKGROUND);
60 } else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) {
61 set_sched_policy(tid, SP_FOREGROUND);
62 }
63
64 if (setpriority(PRIO_PROCESS, tid, newNice) != 0) {
65 PLOG(INFO) << *this << " setPriority(PRIO_PROCESS, " << tid << ", " << newNice << ") failed";
66 }
67}
68
69int Thread::GetNativePriority() {
70 errno = 0;
71 int native_priority = getpriority(PRIO_PROCESS, 0);
72 if (native_priority == -1 && errno != 0) {
73 PLOG(WARNING) << "getpriority failed";
Elliott Hughes34e06962012-04-09 13:55:55 -070074 return kNormThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070075 }
76
Elliott Hughes34e06962012-04-09 13:55:55 -070077 int managed_priority = kMinThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070078 for (size_t i = 0; i < arraysize(kNiceValues); i++) {
79 if (native_priority >= kNiceValues[i]) {
80 break;
81 }
82 managed_priority++;
83 }
Elliott Hughes34e06962012-04-09 13:55:55 -070084 if (managed_priority > kMaxThreadPriority) {
85 managed_priority = kMaxThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070086 }
87 return managed_priority;
88}
89
Elliott Hughes8daa0922011-09-11 13:46:25 -070090} // namespace art