/src/brpc/src/butil/threading/platform_thread_linux.cc
Line | Count | Source (jump to first uncovered line) |
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 | | |
5 | | #include "butil/threading/platform_thread.h" |
6 | | |
7 | | #include <errno.h> |
8 | | #include <sched.h> |
9 | | |
10 | | #include "butil/lazy_instance.h" |
11 | | #include "butil/logging.h" |
12 | | #include "butil/memory/scoped_ptr.h" |
13 | | #include "butil/safe_strerror_posix.h" |
14 | | #include "butil/threading/thread_id_name_manager.h" |
15 | | #include "butil/threading/thread_restrictions.h" |
16 | | |
17 | | #if !defined(OS_NACL) |
18 | | #include <sys/prctl.h> |
19 | | #include <sys/resource.h> |
20 | | #include <sys/syscall.h> |
21 | | #include <sys/time.h> |
22 | | #include <unistd.h> |
23 | | #endif |
24 | | |
25 | | namespace butil { |
26 | | |
27 | | namespace { |
28 | | |
29 | 0 | int ThreadNiceValue(ThreadPriority priority) { |
30 | 0 | switch (priority) { |
31 | 0 | case kThreadPriority_RealtimeAudio: |
32 | 0 | return -10; |
33 | 0 | case kThreadPriority_Background: |
34 | 0 | return 10; |
35 | 0 | case kThreadPriority_Normal: |
36 | 0 | return 0; |
37 | 0 | case kThreadPriority_Display: |
38 | 0 | return -6; |
39 | 0 | default: |
40 | 0 | NOTREACHED() << "Unknown priority."; |
41 | 0 | return 0; |
42 | 0 | } |
43 | 0 | } |
44 | | |
45 | | } // namespace |
46 | | |
47 | | // NOTE(gejun): PR_SET_NAME was added in 2.6.9, should be working on most of |
48 | | // our machines, but missing from our linux headers. |
49 | | #if !defined(PR_SET_NAME) |
50 | | #define PR_SET_NAME 15 |
51 | | #endif |
52 | | |
53 | | // static |
54 | 20 | void PlatformThread::SetName(const char* name) { |
55 | 20 | ThreadIdNameManager::GetInstance()->SetName(CurrentId(), name); |
56 | | |
57 | 20 | #if !defined(OS_NACL) |
58 | | // On linux we can get the thread names to show up in the debugger by setting |
59 | | // the process name for the LWP. We don't want to do this for the main |
60 | | // thread because that would rename the process, causing tools like killall |
61 | | // to stop working. |
62 | 20 | if (PlatformThread::CurrentId() == getpid()) |
63 | 0 | return; |
64 | | |
65 | | // http://0pointer.de/blog/projects/name-your-threads.html |
66 | | // Set the name for the LWP (which gets truncated to 15 characters). |
67 | | // Note that glibc also has a 'pthread_setname_np' api, but it may not be |
68 | | // available everywhere and it's only benefit over using prctl directly is |
69 | | // that it can set the name of threads other than the current thread. |
70 | 20 | int err = prctl(PR_SET_NAME, name); |
71 | | // We expect EPERM failures in sandboxed processes, just ignore those. |
72 | 20 | if (err < 0 && errno != EPERM) |
73 | 0 | DPLOG(ERROR) << "prctl(PR_SET_NAME)"; |
74 | 20 | #endif // !defined(OS_NACL) |
75 | 20 | } |
76 | | |
77 | | // static |
78 | | void PlatformThread::SetThreadPriority(PlatformThreadHandle handle, |
79 | 0 | ThreadPriority priority) { |
80 | 0 | #if !defined(OS_NACL) |
81 | 0 | if (priority == kThreadPriority_RealtimeAudio) { |
82 | 0 | const struct sched_param kRealTimePrio = { 8 }; |
83 | 0 | if (pthread_setschedparam(pthread_self(), SCHED_RR, &kRealTimePrio) == 0) { |
84 | | // Got real time priority, no need to set nice level. |
85 | 0 | return; |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | // setpriority(2) will set a thread's priority if it is passed a tid as |
90 | | // the 'process identifier', not affecting the rest of the threads in the |
91 | | // process. Setting this priority will only succeed if the user has been |
92 | | // granted permission to adjust nice values on the system. |
93 | 0 | DCHECK_NE(handle.id_, kInvalidThreadId); |
94 | 0 | const int kNiceSetting = ThreadNiceValue(priority); |
95 | 0 | if (setpriority(PRIO_PROCESS, handle.id_, kNiceSetting)) { |
96 | 0 | DVPLOG(1) << "Failed to set nice value of thread (" |
97 | 0 | << handle.id_ << ") to " << kNiceSetting; |
98 | 0 | } |
99 | 0 | #endif // !defined(OS_NACL) |
100 | 0 | } |
101 | | |
102 | 0 | void InitThreading() {} |
103 | | |
104 | 0 | void InitOnThread() {} |
105 | | |
106 | 0 | void TerminateOnThread() {} |
107 | | |
108 | 0 | size_t GetDefaultThreadStackSize(const pthread_attr_t& attributes) { |
109 | 0 | #if !defined(THREAD_SANITIZER) && !defined(MEMORY_SANITIZER) |
110 | 0 | return 0; |
111 | | #else |
112 | | // ThreadSanitizer bloats the stack heavily. Evidence has been that the |
113 | | // default stack size isn't enough for some browser tests. |
114 | | // MemorySanitizer needs this as a temporary fix for http://crbug.com/353687 |
115 | | return 2 * (1 << 23); // 2 times 8192K (the default stack size on Linux). |
116 | | #endif |
117 | 0 | } |
118 | | |
119 | | } // namespace butil |