LCOV - code coverage report
Current view: top level - src/util - threadnames.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 81.2 % 16 13
Test Date: 2024-08-28 04:44:32 Functions: 80.0 % 5 4
Branches: 50.0 % 2 1

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

Generated by: LCOV version 2.0-1