/tmp/bitcoin/src/util/threadnames.cpp
Line | Count | Source |
1 | | // Copyright (c) 2018-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #include <util/threadnames.h> |
6 | | #include <util/check.h> |
7 | | |
8 | | #include <algorithm> |
9 | | #include <cstring> |
10 | | #include <string> |
11 | | |
12 | | #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) |
13 | | #include <pthread.h> |
14 | | #include <pthread_np.h> |
15 | | #endif |
16 | | |
17 | | #if __has_include(<sys/prctl.h>) |
18 | | #include <sys/prctl.h> |
19 | | #endif |
20 | | |
21 | | #ifdef WIN32 |
22 | | #include <windows.h> |
23 | | #endif |
24 | | |
25 | | //! Set the thread's name at the process level. Does not affect the |
26 | | //! internal name. |
27 | | static void SetThreadName(const char* name) |
28 | 15.1k | { |
29 | 15.1k | #if defined(PR_SET_NAME) |
30 | | // Only the first 15 characters are used (16 - NUL terminator) |
31 | 15.1k | ::prctl(PR_SET_NAME, name, 0, 0, 0); |
32 | | #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) |
33 | | pthread_set_name_np(pthread_self(), name); |
34 | | #elif defined(__APPLE__) |
35 | | pthread_setname_np(name); |
36 | | #elif defined(WIN32) |
37 | | // Thread names are ASCII-only, so widening each character is sufficient as |
38 | | // a conversion to UTF-16. |
39 | | const std::wstring wname{name, name + std::strlen(name)}; |
40 | | ::SetThreadDescription(::GetCurrentThread(), wname.c_str()); |
41 | | #else |
42 | | // Prevent warnings for unused parameters... |
43 | | (void)name; |
44 | | #endif |
45 | 15.1k | } |
46 | | |
47 | | /** |
48 | | * The name of the thread. We use char array instead of std::string to avoid |
49 | | * complications with running a destructor when the thread exits. Avoid adding |
50 | | * other thread_local variables. |
51 | | * @see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278701 |
52 | | */ |
53 | | static thread_local char g_thread_name[128]{'\0'}; |
54 | 82.3M | std::string util::ThreadGetInternalName() { return g_thread_name; } |
55 | | //! Set the in-memory internal name for this thread. Does not affect the process |
56 | | //! name. |
57 | | static void SetInternalName(const std::string& name) |
58 | 16.4k | { |
59 | 16.4k | const size_t copy_bytes{std::min(sizeof(g_thread_name) - 1, name.length())}; |
60 | 16.4k | std::memcpy(g_thread_name, name.data(), copy_bytes); |
61 | 16.4k | g_thread_name[copy_bytes] = '\0'; |
62 | 16.4k | } |
63 | | |
64 | | void util::ThreadRename(const std::string& name) |
65 | 15.1k | { |
66 | 15.1k | Assume(name.size() <= 13); // Linux keeps 15 bytes |
67 | 15.1k | SetThreadName(("b-" + name).c_str()); |
68 | 15.1k | SetInternalName(name); |
69 | 15.1k | } |
70 | | |
71 | | void util::ThreadSetInternalName(const std::string& name) |
72 | 1.29k | { |
73 | 1.29k | SetInternalName(name); |
74 | 1.29k | } |