blob: 4d982b1f7f5b6af889644754c52f697fb31fb430 [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
Elliott Hughesffb465f2012-03-01 18:46:05 -080024#include <corkscrew/backtrace.h>
Elliott Hughes8daa0922011-09-11 13:46:25 -070025#include <cutils/sched_policy.h>
26#include <utils/threads.h>
27
28#include "macros.h"
29
30namespace art {
31
32/*
33 * Conversion map for "nice" values.
34 *
35 * We use Android thread priority constants to be consistent with the rest
36 * of the system. In some cases adjacent entries may overlap.
37 */
38static const int kNiceValues[10] = {
39 ANDROID_PRIORITY_LOWEST, /* 1 (MIN_PRIORITY) */
40 ANDROID_PRIORITY_BACKGROUND + 6,
41 ANDROID_PRIORITY_BACKGROUND + 3,
42 ANDROID_PRIORITY_BACKGROUND,
43 ANDROID_PRIORITY_NORMAL, /* 5 (NORM_PRIORITY) */
44 ANDROID_PRIORITY_NORMAL - 2,
45 ANDROID_PRIORITY_NORMAL - 4,
46 ANDROID_PRIORITY_URGENT_DISPLAY + 3,
47 ANDROID_PRIORITY_URGENT_DISPLAY + 2,
48 ANDROID_PRIORITY_URGENT_DISPLAY /* 10 (MAX_PRIORITY) */
49};
50
51void Thread::SetNativePriority(int newPriority) {
52 if (newPriority < 1 || newPriority > 10) {
53 LOG(WARNING) << "bad priority " << newPriority;
54 newPriority = 5;
55 }
56
57 int newNice = kNiceValues[newPriority-1];
58 pid_t tid = GetTid();
59
60 if (newNice >= ANDROID_PRIORITY_BACKGROUND) {
61 set_sched_policy(tid, SP_BACKGROUND);
62 } else if (getpriority(PRIO_PROCESS, tid) >= ANDROID_PRIORITY_BACKGROUND) {
63 set_sched_policy(tid, SP_FOREGROUND);
64 }
65
66 if (setpriority(PRIO_PROCESS, tid, newNice) != 0) {
67 PLOG(INFO) << *this << " setPriority(PRIO_PROCESS, " << tid << ", " << newNice << ") failed";
68 }
69}
70
71int Thread::GetNativePriority() {
72 errno = 0;
73 int native_priority = getpriority(PRIO_PROCESS, 0);
74 if (native_priority == -1 && errno != 0) {
75 PLOG(WARNING) << "getpriority failed";
Elliott Hughes34e06962012-04-09 13:55:55 -070076 return kNormThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070077 }
78
Elliott Hughes34e06962012-04-09 13:55:55 -070079 int managed_priority = kMinThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070080 for (size_t i = 0; i < arraysize(kNiceValues); i++) {
81 if (native_priority >= kNiceValues[i]) {
82 break;
83 }
84 managed_priority++;
85 }
Elliott Hughes34e06962012-04-09 13:55:55 -070086 if (managed_priority > kMaxThreadPriority) {
87 managed_priority = kMaxThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070088 }
89 return managed_priority;
90}
91
Elliott Hughesffb465f2012-03-01 18:46:05 -080092void Thread::DumpNativeStack(std::ostream& os) const {
93 const size_t MAX_DEPTH = 32;
94 UniquePtr<backtrace_frame_t[]> backtrace(new backtrace_frame_t[MAX_DEPTH]);
95 ssize_t frame_count = unwind_backtrace_thread(GetTid(), backtrace.get(), 0, MAX_DEPTH);
96 if (frame_count == -1) {
97 os << " (unwind_backtrace_thread failed for thread " << GetTid() << ".)";
98 return;
99 } else if (frame_count == 0) {
Elliott Hughese85d2e92012-05-01 14:02:10 -0700100 os << " (no native stack frames)";
Elliott Hughesffb465f2012-03-01 18:46:05 -0800101 return;
102 }
103
104 UniquePtr<backtrace_symbol_t[]> backtrace_symbols(new backtrace_symbol_t[frame_count]);
105 get_backtrace_symbols(backtrace.get(), frame_count, backtrace_symbols.get());
106
107 for (size_t i = 0; i < static_cast<size_t>(frame_count); ++i) {
108 char line[MAX_BACKTRACE_LINE_LENGTH];
109 format_backtrace_line(i, &backtrace[i], &backtrace_symbols[i],
110 line, MAX_BACKTRACE_LINE_LENGTH);
111 os << " " << line << "\n";
112 }
113
114 free_backtrace_symbols(backtrace_symbols.get(), frame_count);
115}
116
Elliott Hughes8daa0922011-09-11 13:46:25 -0700117} // namespace art