Skip to content

Commit 6e72881

Browse files
authored
Merge pull request ceph#60058 from mchangir/log-save-thread-name-in-log-entries
log: save/fetch thread name infra Reviewed-by: Radoslaw Zarzynski <[email protected]>
2 parents ce84e76 + 0be8d01 commit 6e72881

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

src/common/Thread.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void *Thread::entry_wrapper()
8383
if (pid && cpuid >= 0)
8484
_set_affinity(cpuid);
8585

86-
ceph_pthread_setname(pthread_self(), thread_name.c_str());
86+
ceph_pthread_setname(pthread_self(), Thread::thread_name.c_str());
8787
return entry();
8888
}
8989

@@ -154,7 +154,7 @@ int Thread::try_create(size_t stacksize)
154154
void Thread::create(const char *name, size_t stacksize)
155155
{
156156
ceph_assert(strlen(name) < 16);
157-
thread_name = name;
157+
Thread::thread_name = name;
158158

159159
int ret = try_create(stacksize);
160160
if (ret != 0) {

src/common/Thread.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
#include <string_view>
2121
#include <system_error>
2222
#include <thread>
23+
#include <cstring>
2324

2425
#include <pthread.h>
2526
#include <sys/types.h>
2627

28+
#include "include/ceph_assert.h"
2729
#include "include/compat.h"
30+
#include "include/spinlock.h"
2831

2932
extern pid_t ceph_gettid();
3033

@@ -33,7 +36,7 @@ class Thread {
3336
pthread_t thread_id;
3437
pid_t pid;
3538
int cpuid;
36-
std::string thread_name;
39+
static inline thread_local std::string thread_name;
3740

3841
void *entry_wrapper();
3942

@@ -61,6 +64,9 @@ class Thread {
6164
int join(void **prval = 0);
6265
int detach();
6366
int set_affinity(int cpuid);
67+
static const std::string get_thread_name() {
68+
return Thread::thread_name;
69+
}
6470
};
6571

6672
// Functions for with std::thread

src/log/Entry.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44
#ifndef __CEPH_LOG_ENTRY_H
55
#define __CEPH_LOG_ENTRY_H
66

7+
#include "include/compat.h"
8+
79
#include "log/LogClock.h"
810

911
#include "common/StackStringStream.h"
12+
#include "common/Thread.h"
1013

1114
#include "boost/container/small_vector.hpp"
1215

1316
#include <pthread.h>
1417

1518
#include <string_view>
1619

20+
1721
namespace ceph {
1822
namespace logging {
1923

@@ -27,7 +31,10 @@ class Entry {
2731
m_thread(pthread_self()),
2832
m_prio(pr),
2933
m_subsys(sub)
30-
{}
34+
{
35+
strncpy(m_thread_name, Thread::get_thread_name().data(), 16);
36+
m_thread_name[15] = '\0';
37+
}
3138
Entry(const Entry &) = default;
3239
Entry& operator=(const Entry &) = default;
3340
Entry(Entry &&e) = default;
@@ -40,6 +47,7 @@ class Entry {
4047
time m_stamp;
4148
pthread_t m_thread;
4249
short m_prio, m_subsys;
50+
char m_thread_name[16];
4351

4452
static log_clock& clock() {
4553
static log_clock clock;

src/log/Log.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,13 @@ void Log::dump_recent()
493493
_flush(m_flush, false);
494494

495495
_log_message("--- begin dump of recent events ---", true);
496-
std::set<pthread_t> recent_pthread_ids;
496+
std::set<std::pair<pthread_t, const char *>> recent_pthread_ids;
497497
{
498498
EntryVector t;
499499
t.insert(t.end(), std::make_move_iterator(m_recent.begin()), std::make_move_iterator(m_recent.end()));
500500
m_recent.clear();
501501
for (const auto& e : t) {
502-
recent_pthread_ids.emplace(e.m_thread);
502+
recent_pthread_ids.emplace(std::make_pair(e.m_thread, e.m_thread_name));
503503
}
504504
_flush(t, true);
505505
}
@@ -515,14 +515,11 @@ void Log::dump_recent()
515515
m_stderr_log, m_stderr_crash), true);
516516

517517
_log_message("--- pthread ID / name mapping for recent threads ---", true);
518-
for (const auto pthread_id : recent_pthread_ids)
518+
for (auto& [pthread_id, pthread_name] : recent_pthread_ids)
519519
{
520-
char pthread_name[16] = {0}; //limited by 16B include terminating null byte.
521-
ceph_pthread_getname(pthread_id, pthread_name, sizeof(pthread_name));
522520
// we want the ID to be printed in the same format as we use for a log entry.
523521
// The reason is easier grepping.
524-
_log_message(fmt::format(" {:x} / {}",
525-
tid_to_int(pthread_id), pthread_name), true);
522+
_log_message(fmt::format(" {:x} / {}", tid_to_int(pthread_id), pthread_name), true);
526523
}
527524

528525
_log_message(fmt::format(" max_recent {:9}", m_recent.capacity()), true);

0 commit comments

Comments
 (0)