forked from Bitcoin-ABC/bitcoin-abc
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
threads: introduce util/threadnames, refactor thread naming
This work is prerequisite to attaching thread names to log lines and deadlock debug utilities. This code allows setting of an "internal" threadname per thread on platforms where thread_local is available. This commit also moves RenameThread() out of a more general module and adds a numeric suffix to disambiguate between threads with the same name. It explicitly names a few main threads using the new util::ThreadRename().
- Loading branch information
Showing
14 changed files
with
108 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2018 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#if defined(HAVE_CONFIG_H) | ||
#include <config/bitcoin-config.h> | ||
#endif | ||
|
||
#include <atomic> | ||
#include <thread> | ||
|
||
#include <util/threadnames.h> | ||
|
||
#ifdef HAVE_SYS_PRCTL_H | ||
#include <sys/prctl.h> // For prctl, PR_SET_NAME, PR_GET_NAME | ||
#endif | ||
|
||
//! Set the thread's name at the process level. Does not affect the | ||
//! internal name. | ||
static void SetThreadName(const char* name) | ||
{ | ||
#if defined(PR_SET_NAME) | ||
// Only the first 15 characters are used (16 - NUL terminator) | ||
::prctl(PR_SET_NAME, name, 0, 0, 0); | ||
#elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) | ||
pthread_set_name_np(pthread_self(), name); | ||
#elif defined(MAC_OSX) | ||
pthread_setname_np(name); | ||
#else | ||
// Prevent warnings for unused parameters... | ||
(void)name; | ||
#endif | ||
} | ||
|
||
static thread_local std::string g_thread_name; | ||
const std::string& util::ThreadGetInternalName() { return g_thread_name; } | ||
//! Set the in-memory internal name for this thread. Does not affect the process | ||
//! name. | ||
static void SetInternalName(std::string name) { g_thread_name = std::move(name); } | ||
|
||
void util::ThreadRename(std::string&& name) | ||
{ | ||
SetThreadName(("bitcoin-" + name).c_str()); | ||
SetInternalName(std::move(name)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) 2018 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_UTIL_THREADNAMES_H | ||
#define BITCOIN_UTIL_THREADNAMES_H | ||
|
||
#include <string> | ||
|
||
namespace util { | ||
//! Rename a thread both in terms of an internal (in-memory) name as well | ||
//! as its system thread name. | ||
void ThreadRename(std::string&&); | ||
|
||
//! Get the thread's internal (in-memory) name; used e.g. for identification in | ||
//! logging. | ||
const std::string& ThreadGetInternalName(); | ||
|
||
} // namespace util | ||
|
||
#endif // BITCOIN_UTIL_THREADNAMES_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters