1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/threading/platform_thread.h"
6
7 #include <errno.h>
8 #include <stddef.h>
9 #include <sys/prctl.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 #include <optional>
14
15 #include "base/android/jni_android.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/threading/platform_thread_internal_posix.h"
19 #include "base/threading/thread_id_name_manager.h"
20
21 // Must come after all headers that specialize FromJniType() / ToJniType().
22 #include "base/tasks_jni/ThreadUtils_jni.h"
23
24 namespace base {
25
26 namespace internal {
27
28 // - kRealtimeAudio corresponds to Android's PRIORITY_AUDIO = -16 value.
29 // - kDisplay corresponds to Android's PRIORITY_DISPLAY = -4 value.
30 // - kBackground corresponds to Android's PRIORITY_BACKGROUND = 10 value and can
31 // result in heavy throttling and force the thread onto a little core on
32 // big.LITTLE devices.
33 const ThreadPriorityToNiceValuePairForTest
34 kThreadPriorityToNiceValueMapForTest[7] = {
35 {ThreadPriorityForTest::kRealtimeAudio, -16},
36 {ThreadPriorityForTest::kDisplay, -4},
37 {ThreadPriorityForTest::kNormal, 0},
38 {ThreadPriorityForTest::kResourceEfficient, 0},
39 {ThreadPriorityForTest::kUtility, 1},
40 {ThreadPriorityForTest::kBackground, 10},
41 };
42
43 // - kBackground corresponds to Android's PRIORITY_BACKGROUND = 10 value and can
44 // result in heavy throttling and force the thread onto a little core on
45 // big.LITTLE devices.
46 // - kUtility corresponds to Android's THREAD_PRIORITY_LESS_FAVORABLE = 1 value.
47 // - kDisplayCritical corresponds to Android's PRIORITY_DISPLAY = -4 value.
48 // - kRealtimeAudio corresponds to Android's PRIORITY_AUDIO = -16 value.
49 const ThreadTypeToNiceValuePair kThreadTypeToNiceValueMap[7] = {
50 {ThreadType::kBackground, 10}, {ThreadType::kUtility, 1},
51 {ThreadType::kResourceEfficient, 0}, {ThreadType::kDefault, 0},
52 {ThreadType::kDisplayCritical, -4}, {ThreadType::kRealtimeAudio, -16},
53 };
54
CanSetThreadTypeToRealtimeAudio()55 bool CanSetThreadTypeToRealtimeAudio() {
56 return true;
57 }
58
SetCurrentThreadTypeForPlatform(ThreadType thread_type,MessagePumpType pump_type_hint)59 bool SetCurrentThreadTypeForPlatform(ThreadType thread_type,
60 MessagePumpType pump_type_hint) {
61 // On Android, we set the Audio priority through JNI as Audio priority
62 // will also allow the process to run while it is backgrounded.
63 if (thread_type == ThreadType::kRealtimeAudio) {
64 JNIEnv* env = base::android::AttachCurrentThread();
65 Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
66 return true;
67 }
68 // Recent versions of Android (O+) up the priority of the UI thread
69 // automatically.
70 if (thread_type == ThreadType::kDisplayCritical &&
71 pump_type_hint == MessagePumpType::UI &&
72 GetCurrentThreadNiceValue() <=
73 ThreadTypeToNiceValue(ThreadType::kDisplayCritical)) {
74 return true;
75 }
76 return false;
77 }
78
79 std::optional<ThreadPriorityForTest>
GetCurrentThreadPriorityForPlatformForTest()80 GetCurrentThreadPriorityForPlatformForTest() {
81 JNIEnv* env = base::android::AttachCurrentThread();
82 if (Java_ThreadUtils_isThreadPriorityAudio(
83 env, PlatformThread::CurrentId())) {
84 return std::make_optional(ThreadPriorityForTest::kRealtimeAudio);
85 }
86 return std::nullopt;
87 }
88
89 } // namespace internal
90
SetName(const std::string & name)91 void PlatformThread::SetName(const std::string& name) {
92 SetNameCommon(name);
93
94 // Like linux, on android we can get the thread names to show up in the
95 // debugger by setting the process name for the LWP.
96 // We don't want to do this for the main thread because that would rename
97 // the process, causing tools like killall to stop working.
98 if (PlatformThread::CurrentId() == getpid())
99 return;
100
101 // Set the name for the LWP (which gets truncated to 15 characters).
102 int err = prctl(PR_SET_NAME, name.c_str());
103 if (err < 0 && errno != EPERM)
104 DPLOG(ERROR) << "prctl(PR_SET_NAME)";
105 }
106
107
InitThreading()108 void InitThreading() {
109 }
110
TerminateOnThread()111 void TerminateOnThread() {
112 base::android::DetachFromVM();
113 }
114
GetDefaultThreadStackSize(const pthread_attr_t & attributes)115 size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
116 #if !defined(ADDRESS_SANITIZER)
117 return 0;
118 #else
119 // AddressSanitizer bloats the stack approximately 2x. Default stack size of
120 // 1Mb is not enough for some tests (see http://crbug.com/263749 for example).
121 return 2 * (1 << 20); // 2Mb
122 #endif
123 }
124
125 } // namespace base
126