LCOV - code coverage report
Current view: top level - src/util - threadnames.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 16 16 100.0 %
Date: 2026-06-25 07:23:43 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2018-2019 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             : #if defined(HAVE_CONFIG_H)
       6             : #include <config/bitcoin-config.h>
       7             : #endif
       8             : 
       9             : #include <cstring>
      10             : #include <string>
      11             : #include <thread>
      12             : #include <utility>
      13             : 
      14             : #if (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__))
      15             : #include <pthread.h>
      16             : #include <pthread_np.h>
      17             : #endif
      18             : 
      19             : #include <util/threadnames.h>
      20             : 
      21             : #ifdef HAVE_SYS_PRCTL_H
      22             : #include <sys/prctl.h>
      23             : #endif
      24             : 
      25             : //! Set the thread's name at the process level. Does not affect the
      26             : //! internal name.
      27      163492 : static void SetThreadName(const char* name)
      28             : {
      29             : #if defined(PR_SET_NAME)
      30             :     // Only the first 15 characters are used (16 - NUL terminator)
      31             :     ::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(MAC_OSX)
      35      163492 :     pthread_setname_np(name);
      36             : #else
      37             :     // Prevent warnings for unused parameters...
      38             :     (void)name;
      39             : #endif
      40      163492 : }
      41             : 
      42             : /**
      43             :  * The name of the thread. We use char array instead of std::string to avoid
      44             :  * complications with running a destructor when the thread exits. Avoid adding
      45             :  * other thread_local variables.
      46             :  * @see https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278701
      47             :  */
      48             : static thread_local char g_thread_name[128]{'\0'};
      49    26341603 : std::string util::ThreadGetInternalName() { return g_thread_name; }
      50             : //! Set the in-memory internal name for this thread. Does not affect the process
      51             : //! name.
      52      166902 : static void SetInternalName(const std::string& name)
      53             : {
      54      166902 :     const size_t copy_bytes{std::min(sizeof(g_thread_name) - 1, name.length())};
      55      166902 :     std::memcpy(g_thread_name, name.data(), copy_bytes);
      56      166902 :     g_thread_name[copy_bytes] = '\0';
      57      166902 : }
      58             : 
      59      163019 : void util::ThreadRename(const std::string& name)
      60             : {
      61      163019 :     SetThreadName(("d-" + name).c_str());
      62      163019 :     SetInternalName(name);
      63      163019 : }
      64             : 
      65        3162 : void util::ThreadSetInternalName(const std::string& name)
      66             : {
      67        3162 :     SetInternalName(name);
      68        3162 : }

Generated by: LCOV version 1.16