| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <sys/prctl.h> | 8 #include <sys/prctl.h> |
| 9 #include <sys/resource.h> | 9 #include <sys/resource.h> |
| 10 #include <sys/types.h> | 10 #include <sys/types.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include "base/android/jni_android.h" | 13 #include "base/android/jni_android.h" |
| 14 #include "base/android/thread_utils.h" | 14 #include "base/android/thread_utils.h" |
| 15 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/threading/platform_thread_internal_posix.h" | 17 #include "base/threading/platform_thread_internal_posix.h" |
| 18 #include "base/threading/thread_id_name_manager.h" | 18 #include "base/threading/thread_id_name_manager.h" |
| 19 #include "base/tracked_objects.h" | 19 #include "base/tracked_objects.h" |
| 20 #include "jni/ThreadUtils_jni.h" | 20 #include "jni/ThreadUtils_jni.h" |
| 21 | 21 |
| 22 namespace base { | 22 namespace base { |
| 23 | 23 |
| 24 namespace internal { | 24 namespace internal { |
| 25 | 25 |
| 26 // These nice values are taken from Android, which uses nice values like linux, | 26 // - BACKGROUND is 9 due to it being the nicest value we can use that's still |
| 27 // but defines some preset nice values. | 27 // above an Android system threshold that enables heavy throttling starting at |
| 28 // Process.THREAD_PRIORITY_AUDIO = -16 | 28 // 10; we want to be lower-priority than Chrome's other threads without |
| 29 // Process.THREAD_PRIORITY_BACKGROUND = 10 | 29 // incurring this behavior. |
| 30 // Process.THREAD_PRIORITY_DEFAULT = 0; | 30 // - DISPLAY is -6 due to being midway between Android's DISPLAY (-4) and |
| 31 // Process.THREAD_PRIORITY_DISPLAY = -4; | 31 // URGENT_DISPLAY (-8). |
| 32 // Process.THREAD_PRIORITY_FOREGROUND = -2; | 32 // - REALTIME_AUDIO corresponds to Android's THREAD_PRIORITY_AUDIO = -16 value. |
| 33 // Process.THREAD_PRIORITY_LESS_FAVORABLE = 1; | |
| 34 // Process.THREAD_PRIORITY_LOWEST = 19; | |
| 35 // Process.THREAD_PRIORITY_MORE_FAVORABLE = -1; | |
| 36 // Process.THREAD_PRIORITY_URGENT_AUDIO = -19; | |
| 37 // Process.THREAD_PRIORITY_URGENT_DISPLAY = -8; | |
| 38 // We use -6 for display, but we may want to split this into urgent (-8) and | |
| 39 // non-urgent (-4). | |
| 40 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = { | 33 const ThreadPriorityToNiceValuePair kThreadPriorityToNiceValueMap[4] = { |
| 41 {ThreadPriority::BACKGROUND, 10}, | 34 {ThreadPriority::BACKGROUND, 9}, |
| 42 {ThreadPriority::NORMAL, 0}, | 35 {ThreadPriority::NORMAL, 0}, |
| 43 {ThreadPriority::DISPLAY, -6}, | 36 {ThreadPriority::DISPLAY, -6}, |
| 44 {ThreadPriority::REALTIME_AUDIO, -16}, | 37 {ThreadPriority::REALTIME_AUDIO, -16}, |
| 45 }; | 38 }; |
| 46 | 39 |
| 47 bool SetThreadPriorityForPlatform(PlatformThreadHandle handle, | 40 bool SetCurrentThreadPriorityForPlatform(ThreadPriority priority) { |
| 48 ThreadPriority priority) { | |
| 49 // On Android, we set the Audio priority through JNI as Audio priority | 41 // On Android, we set the Audio priority through JNI as Audio priority |
| 50 // will also allow the process to run while it is backgrounded. | 42 // will also allow the process to run while it is backgrounded. |
| 51 if (priority == ThreadPriority::REALTIME_AUDIO) { | 43 if (priority == ThreadPriority::REALTIME_AUDIO) { |
| 52 JNIEnv* env = base::android::AttachCurrentThread(); | 44 JNIEnv* env = base::android::AttachCurrentThread(); |
| 53 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); | 45 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId()); |
| 54 return true; | 46 return true; |
| 55 } | 47 } |
| 56 return false; | 48 return false; |
| 57 } | 49 } |
| 58 | 50 |
| 59 bool GetThreadPriorityForPlatform(PlatformThreadHandle handle, | 51 bool GetCurrentThreadPriorityForPlatform(ThreadPriority* priority) { |
| 60 ThreadPriority* priority) { | 52 // See http://crbug.com/505474. |
| 61 NOTIMPLEMENTED(); | 53 NOTIMPLEMENTED(); |
| 62 return false; | 54 return false; |
| 63 } | 55 } |
| 64 | 56 |
| 65 } // namespace internal | 57 } // namespace internal |
| 66 | 58 |
| 67 void PlatformThread::SetName(const std::string& name) { | 59 void PlatformThread::SetName(const std::string& name) { |
| 68 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); | 60 ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
| 69 tracked_objects::ThreadData::InitializeThreadContext(name); | 61 tracked_objects::ThreadData::InitializeThreadContext(name); |
| 70 | 62 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 81 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; | 73 DPLOG(ERROR) << "prctl(PR_SET_NAME)"; |
| 82 } | 74 } |
| 83 | 75 |
| 84 | 76 |
| 85 void InitThreading() { | 77 void InitThreading() { |
| 86 } | 78 } |
| 87 | 79 |
| 88 void InitOnThread() { | 80 void InitOnThread() { |
| 89 // Threads on linux/android may inherit their priority from the thread | 81 // Threads on linux/android may inherit their priority from the thread |
| 90 // where they were created. This sets all new threads to the default. | 82 // where they were created. This sets all new threads to the default. |
| 91 PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(), | 83 PlatformThread::SetCurrentThreadPriority(ThreadPriority::NORMAL); |
| 92 ThreadPriority::NORMAL); | |
| 93 } | 84 } |
| 94 | 85 |
| 95 void TerminateOnThread() { | 86 void TerminateOnThread() { |
| 96 base::android::DetachFromVM(); | 87 base::android::DetachFromVM(); |
| 97 } | 88 } |
| 98 | 89 |
| 99 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { | 90 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { |
| 100 #if !defined(ADDRESS_SANITIZER) | 91 #if !defined(ADDRESS_SANITIZER) |
| 101 return 0; | 92 return 0; |
| 102 #else | 93 #else |
| 103 // AddressSanitizer bloats the stack approximately 2x. Default stack size of | 94 // AddressSanitizer bloats the stack approximately 2x. Default stack size of |
| 104 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example). | 95 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example). |
| 105 return 2 * (1 << 20); // 2Mb | 96 return 2 * (1 << 20); // 2Mb |
| 106 #endif | 97 #endif |
| 107 } | 98 } |
| 108 | 99 |
| 109 bool RegisterThreadUtils(JNIEnv* env) { | 100 bool RegisterThreadUtils(JNIEnv* env) { |
| 110 return RegisterNativesImpl(env); | 101 return RegisterNativesImpl(env); |
| 111 } | 102 } |
| 112 | 103 |
| 113 } // namespace base | 104 } // namespace base |
| OLD | NEW |