Give threads a recognisable name to aid in debugging

NOTE: These thread names are visible in gdb when using 'info threads'.
      Additionally both 'top' and 'ps' show these names *unless* told to
      display the command-line instead of task name.

Signed-off-by: Giel van Schijndel <me@mortis.eu>
This commit is contained in:
Giel van Schijndel 2012-06-24 17:03:57 +02:00
parent 1c009d622d
commit 96931d6f78
8 changed files with 75 additions and 3 deletions

View file

@ -1569,11 +1569,17 @@ Value keypoolrefill(const Array& params, bool fHelp)
void ThreadTopUpKeyPool(void* parg) void ThreadTopUpKeyPool(void* parg)
{ {
// Make this thread recognisable as the key-topping-up thread
RenameThread("bitcoin-key-top");
pwalletMain->TopUpKeyPool(); pwalletMain->TopUpKeyPool();
} }
void ThreadCleanWalletPassphrase(void* parg) void ThreadCleanWalletPassphrase(void* parg)
{ {
// Make this thread recognisable as the wallet unlocking thread
RenameThread("bitcoin-unlo-wa");
int64 nMyWakeTime = GetTimeMillis() + *((int64*)parg) * 1000; int64 nMyWakeTime = GetTimeMillis() + *((int64*)parg) * 1000;
ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime); ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime);
@ -2479,6 +2485,10 @@ private:
void ThreadRPCServer(void* parg) void ThreadRPCServer(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadRPCServer(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadRPCServer(parg));
// Make this thread recognisable as the RPC listener
RenameThread("bitcoin-rpclist");
try try
{ {
vnThreadsRunning[THREAD_RPCLISTENER]++; vnThreadsRunning[THREAD_RPCLISTENER]++;
@ -2762,6 +2772,10 @@ static CCriticalSection cs_THREAD_RPCHANDLER;
void ThreadRPCServer3(void* parg) void ThreadRPCServer3(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadRPCServer3(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadRPCServer3(parg));
// Make this thread recognisable as the RPC handler
RenameThread("bitcoin-rpchand");
{ {
LOCK(cs_THREAD_RPCHANDLER); LOCK(cs_THREAD_RPCHANDLER);
vnThreadsRunning[THREAD_RPCHANDLER]++; vnThreadsRunning[THREAD_RPCHANDLER]++;

View file

@ -53,6 +53,10 @@ void Shutdown(void* parg)
{ {
static CCriticalSection cs_Shutdown; static CCriticalSection cs_Shutdown;
static bool fTaken; static bool fTaken;
// Make this thread recognisable as the shutdown thread
RenameThread("bitcoin-shutoff");
bool fFirstThread = false; bool fFirstThread = false;
{ {
TRY_LOCK(cs_Shutdown, lockShutdown); TRY_LOCK(cs_Shutdown, lockShutdown);

View file

@ -189,6 +189,10 @@ bool GetIPFromIRC(SOCKET hSocket, string strMyName, CNetAddr& ipRet)
void ThreadIRCSeed(void* parg) void ThreadIRCSeed(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadIRCSeed(parg));
// Make this thread recognisable as the IRC seeding thread
RenameThread("bitcoin-ircseed");
try try
{ {
ThreadIRCSeed2(parg); ThreadIRCSeed2(parg);

View file

@ -3594,6 +3594,9 @@ void static BitcoinMiner(CWallet *pwallet)
printf("BitcoinMiner started\n"); printf("BitcoinMiner started\n");
SetThreadPriority(THREAD_PRIORITY_LOWEST); SetThreadPriority(THREAD_PRIORITY_LOWEST);
// Make this thread recognisable as the mining thread
RenameThread("bitcoin [miner]");
// Each thread has its own key and counter // Each thread has its own key and counter
CReserveKey reservekey(pwallet); CReserveKey reservekey(pwallet);
unsigned int nExtraNonce = 0; unsigned int nExtraNonce = 0;

View file

@ -407,6 +407,9 @@ bool GetMyExternalIP(CNetAddr& ipRet)
void ThreadGetMyExternalIP(void* parg) void ThreadGetMyExternalIP(void* parg)
{ {
// Make this thread recognisable as the message handling thread
RenameThread("bitcoin-ext-ip");
CNetAddr addrLocalHost; CNetAddr addrLocalHost;
if (GetMyExternalIP(addrLocalHost)) if (GetMyExternalIP(addrLocalHost))
{ {
@ -636,6 +639,10 @@ void CNode::copyStats(CNodeStats &stats)
void ThreadSocketHandler(void* parg) void ThreadSocketHandler(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadSocketHandler(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadSocketHandler(parg));
// Make this thread recognisable as the networking thread
RenameThread("bitcoind [net]");
try try
{ {
vnThreadsRunning[THREAD_SOCKETHANDLER]++; vnThreadsRunning[THREAD_SOCKETHANDLER]++;
@ -988,6 +995,10 @@ void ThreadSocketHandler2(void* parg)
void ThreadMapPort(void* parg) void ThreadMapPort(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadMapPort(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadMapPort(parg));
// Make this thread recognisable as the UPnP thread
RenameThread("bitcoind [UPnP]");
try try
{ {
vnThreadsRunning[THREAD_UPNP]++; vnThreadsRunning[THREAD_UPNP]++;
@ -1146,6 +1157,10 @@ static const char *strDNSSeed[][2] = {
void ThreadDNSAddressSeed(void* parg) void ThreadDNSAddressSeed(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadDNSAddressSeed(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadDNSAddressSeed(parg));
// Make this thread recognisable as the DNS seeding thread
RenameThread("bitcoin-dnsseed");
try try
{ {
vnThreadsRunning[THREAD_DNSSEED]++; vnThreadsRunning[THREAD_DNSSEED]++;
@ -1315,6 +1330,10 @@ void ThreadDumpAddress2(void* parg)
void ThreadDumpAddress(void* parg) void ThreadDumpAddress(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadDumpAddress(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadDumpAddress(parg));
// Make this thread recognisable as the address dumping thread
RenameThread("bitcoin-adrdump");
try try
{ {
ThreadDumpAddress2(parg); ThreadDumpAddress2(parg);
@ -1328,6 +1347,10 @@ void ThreadDumpAddress(void* parg)
void ThreadOpenConnections(void* parg) void ThreadOpenConnections(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadOpenConnections(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadOpenConnections(parg));
// Make this thread recognisable as the connection opening thread
RenameThread("bitcoin-opencon");
try try
{ {
vnThreadsRunning[THREAD_OPENCONNECTIONS]++; vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
@ -1481,6 +1504,10 @@ void ThreadOpenConnections2(void* parg)
void ThreadOpenAddedConnections(void* parg) void ThreadOpenAddedConnections(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadOpenAddedConnections(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadOpenAddedConnections(parg));
// Make this thread recognisable as the connection opening thread
RenameThread("bitcoin-opencon");
try try
{ {
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++; vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++;
@ -1610,6 +1637,10 @@ bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOu
void ThreadMessageHandler(void* parg) void ThreadMessageHandler(void* parg)
{ {
IMPLEMENT_RANDOMIZE_STACK(ThreadMessageHandler(parg)); IMPLEMENT_RANDOMIZE_STACK(ThreadMessageHandler(parg));
// Make this thread recognisable as the message handling thread
RenameThread("bitcoin-msghand");
try try
{ {
vnThreadsRunning[THREAD_MESSAGEHANDLER]++; vnThreadsRunning[THREAD_MESSAGEHANDLER]++;
@ -1852,6 +1883,9 @@ void static Discover()
void StartNode(void* parg) void StartNode(void* parg)
{ {
// Make this thread recognisable as the startup thread
RenameThread("bitcoin [start]");
if (semOutbound == NULL) { if (semOutbound == NULL) {
// initialize semaphore // initialize semaphore
int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125)); int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125));

View file

@ -51,6 +51,8 @@ namespace boost {
#endif #endif
#include <io.h> /* for _commit */ #include <io.h> /* for _commit */
#include "shlobj.h" #include "shlobj.h"
#elif defined(__linux__)
# include <sys/prctl.h>
#endif #endif
using namespace std; using namespace std;
@ -1275,3 +1277,13 @@ void runCommand(std::string strCommand)
printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr); printf("runCommand error: system(%s) returned %d\n", strCommand.c_str(), nErr);
} }
void RenameThread(const char* name)
{
#if defined(__linux__) && defined(PR_SET_NAME)
// Only the first 15 characters are used (16 - NUL terminator)
::prctl(PR_SET_NAME, name, 0, 0, 0);
#else
// Prevent warnings for unused parameters...
(void)name;
#endif
}

View file

@ -621,9 +621,7 @@ inline void ExitThread(size_t nExitCode)
} }
#endif #endif
void RenameThread(const char* name);
inline uint32_t ByteReverse(uint32_t value) inline uint32_t ByteReverse(uint32_t value)
{ {

View file

@ -323,6 +323,9 @@ int CWalletDB::LoadWallet(CWallet* pwallet)
void ThreadFlushWalletDB(void* parg) void ThreadFlushWalletDB(void* parg)
{ {
// Make this thread recognisable as the wallet flushing thread
RenameThread("bitcoin-wallet");
const string& strFile = ((const string*)parg)[0]; const string& strFile = ((const string*)parg)[0];
static bool fOneThread; static bool fOneThread;
if (fOneThread) if (fOneThread)