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