platform_thread_android.cc 4.1 KB

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