Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 24 | #include <corkscrew/backtrace.h> |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 25 | #include <cutils/sched_policy.h> |
| 26 | #include <utils/threads.h> |
| 27 | |
| 28 | #include "macros.h" |
| 29 | |
| 30 | namespace 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 | */ |
| 38 | static 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 | |
| 51 | void 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 | |
| 71 | int 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"; |
| 76 | return Thread::kNormPriority; |
| 77 | } |
| 78 | |
| 79 | int managed_priority = Thread::kMinPriority; |
| 80 | for (size_t i = 0; i < arraysize(kNiceValues); i++) { |
| 81 | if (native_priority >= kNiceValues[i]) { |
| 82 | break; |
| 83 | } |
| 84 | managed_priority++; |
| 85 | } |
| 86 | if (managed_priority > Thread::kMaxPriority) { |
| 87 | managed_priority = Thread::kMaxPriority; |
| 88 | } |
| 89 | return managed_priority; |
| 90 | } |
| 91 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 92 | void 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) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | UniquePtr<backtrace_symbol_t[]> backtrace_symbols(new backtrace_symbol_t[frame_count]); |
| 104 | get_backtrace_symbols(backtrace.get(), frame_count, backtrace_symbols.get()); |
| 105 | |
| 106 | for (size_t i = 0; i < static_cast<size_t>(frame_count); ++i) { |
| 107 | char line[MAX_BACKTRACE_LINE_LENGTH]; |
| 108 | format_backtrace_line(i, &backtrace[i], &backtrace_symbols[i], |
| 109 | line, MAX_BACKTRACE_LINE_LENGTH); |
| 110 | os << " " << line << "\n"; |
| 111 | } |
| 112 | |
| 113 | free_backtrace_symbols(backtrace_symbols.get(), frame_count); |
| 114 | } |
| 115 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 116 | } // namespace art |