190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/threading/platform_thread.h"
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <errno.h>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <sys/prctl.h>
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <sys/resource.h>
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/android/jni_android.h"
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/android/thread_utils.h"
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/lazy_instance.h"
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/logging.h"
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/threading/thread_id_name_manager.h"
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/tracked_objects.h"
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "jni/ThreadUtils_jni.h"
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace base {
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace {
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)int ThreadNiceValue(ThreadPriority priority) {
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // These nice values are taken from Android, which uses nice
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // values like linux, but defines some preset nice values.
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_AUDIO = -16
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_BACKGROUND = 10
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_DEFAULT = 0;
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_DISPLAY = -4;
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_FOREGROUND = -2;
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_LESS_FAVORABLE = 1;
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_LOWEST = 19;
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_MORE_FAVORABLE = -1;
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_URGENT_AUDIO = -19;
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //   Process.THREAD_PRIORITY_URGENT_DISPLAY = -8;
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // We use -6 for display, but we may want to split this
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // into urgent (-8) and non-urgent (-4).
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static const int threadPriorityAudio = -16;
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static const int threadPriorityBackground = 10;
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static const int threadPriorityDefault = 0;
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  static const int threadPriorityDisplay = -6;
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  switch (priority) {
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case kThreadPriority_RealtimeAudio:
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return threadPriorityAudio;
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case kThreadPriority_Background:
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return threadPriorityBackground;
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case kThreadPriority_Normal:
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return threadPriorityDefault;
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    case kThreadPriority_Display:
4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return threadPriorityDisplay;
5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    default:
5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      NOTREACHED() << "Unknown priority.";
5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      return 0;
5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace
5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//static
5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void PlatformThread::SetThreadPriority(PlatformThreadHandle handle,
5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                       ThreadPriority priority) {
6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // On Android, we set the Audio priority through JNI as Audio priority
6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // will also allow the process to run while it is backgrounded.
6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (priority == kThreadPriority_RealtimeAudio) {
6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    JNIEnv* env = base::android::AttachCurrentThread();
6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    Java_ThreadUtils_setThreadPriorityAudio(env, PlatformThread::CurrentId());
6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    return;
6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // setpriority(2) should change the whole thread group's (i.e. process)
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // priority. however, on linux it will only change the target thread's
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // priority. see the bugs section in
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // http://man7.org/linux/man-pages/man2/getpriority.2.html.
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // we prefer using 0 rather than the current thread id since they are
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // equivalent but it makes sandboxing easier (https://crbug.com/399473).
7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DCHECK_NE(handle.id_, kInvalidThreadId);
7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  int kNiceSetting = ThreadNiceValue(priority);
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const PlatformThreadId current_id = PlatformThread::CurrentId();
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (setpriority(PRIO_PROCESS,
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  handle.id_ == current_id ? 0 : handle.id_,
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                  kNiceSetting)) {
8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)    LOG(ERROR) << "Failed to set nice value of thread to " << kNiceSetting;
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void PlatformThread::SetName(const char* name) {
8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name);
8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  tracked_objects::ThreadData::InitializeThreadContext(name);
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Like linux, on android we can get the thread names to show up in the
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // debugger by setting the process name for the LWP.
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // We don't want to do this for the main thread because that would rename
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // the process, causing tools like killall to stop working.
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (PlatformThread::CurrentId() == getpid())
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Set the name for the LWP (which gets truncated to 15 characters).
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int err = prctl(PR_SET_NAME, name);
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (err < 0 && errno != EPERM)
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DPLOG(ERROR) << "prctl(PR_SET_NAME)";
9990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
10090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void InitThreading() {
10390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
10490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
10590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void InitOnThread() {
10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Threads on linux/android may inherit their priority from the thread
10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // where they were created. This sets all new threads to the default.
10890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  PlatformThread::SetThreadPriority(PlatformThread::CurrentHandle(),
10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)                                    kThreadPriority_Normal);
11090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
11190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)void TerminateOnThread() {
11390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  base::android::DetachFromVM();
11490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
11590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
11690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) {
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#if !defined(ADDRESS_SANITIZER)
11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return 0;
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#else
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // AddressSanitizer bloats the stack approximately 2x. Default stack size of
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // 1Mb is not enough for some tests (see http://crbug.com/263749 for example).
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return 2 * (1 << 20);  // 2Mb
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif
12490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
12590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
12690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)bool RegisterThreadUtils(JNIEnv* env) {
12790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  return RegisterNativesImpl(env);
12890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}
12990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
13090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace base
131