Skip to content

Commit

Permalink
Merge pull request ceph#60058 from mchangir/log-save-thread-name-in-l…
Browse files Browse the repository at this point in the history
…og-entries

log: save/fetch thread name infra

Reviewed-by: Radoslaw Zarzynski <[email protected]>
  • Loading branch information
yuriw authored Oct 16, 2024
2 parents ce84e76 + 0be8d01 commit 6e72881
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/common/Thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void *Thread::entry_wrapper()
if (pid && cpuid >= 0)
_set_affinity(cpuid);

ceph_pthread_setname(pthread_self(), thread_name.c_str());
ceph_pthread_setname(pthread_self(), Thread::thread_name.c_str());
return entry();
}

Expand Down Expand Up @@ -154,7 +154,7 @@ int Thread::try_create(size_t stacksize)
void Thread::create(const char *name, size_t stacksize)
{
ceph_assert(strlen(name) < 16);
thread_name = name;
Thread::thread_name = name;

int ret = try_create(stacksize);
if (ret != 0) {
Expand Down
8 changes: 7 additions & 1 deletion src/common/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
#include <string_view>
#include <system_error>
#include <thread>
#include <cstring>

#include <pthread.h>
#include <sys/types.h>

#include "include/ceph_assert.h"
#include "include/compat.h"
#include "include/spinlock.h"

extern pid_t ceph_gettid();

Expand All @@ -33,7 +36,7 @@ class Thread {
pthread_t thread_id;
pid_t pid;
int cpuid;
std::string thread_name;
static inline thread_local std::string thread_name;

void *entry_wrapper();

Expand Down Expand Up @@ -61,6 +64,9 @@ class Thread {
int join(void **prval = 0);
int detach();
int set_affinity(int cpuid);
static const std::string get_thread_name() {
return Thread::thread_name;
}
};

// Functions for with std::thread
Expand Down
10 changes: 9 additions & 1 deletion src/log/Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
#ifndef __CEPH_LOG_ENTRY_H
#define __CEPH_LOG_ENTRY_H

#include "include/compat.h"

#include "log/LogClock.h"

#include "common/StackStringStream.h"
#include "common/Thread.h"

#include "boost/container/small_vector.hpp"

#include <pthread.h>

#include <string_view>


namespace ceph {
namespace logging {

Expand All @@ -27,7 +31,10 @@ class Entry {
m_thread(pthread_self()),
m_prio(pr),
m_subsys(sub)
{}
{
strncpy(m_thread_name, Thread::get_thread_name().data(), 16);
m_thread_name[15] = '\0';
}
Entry(const Entry &) = default;
Entry& operator=(const Entry &) = default;
Entry(Entry &&e) = default;
Expand All @@ -40,6 +47,7 @@ class Entry {
time m_stamp;
pthread_t m_thread;
short m_prio, m_subsys;
char m_thread_name[16];

static log_clock& clock() {
static log_clock clock;
Expand Down
11 changes: 4 additions & 7 deletions src/log/Log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,13 @@ void Log::dump_recent()
_flush(m_flush, false);

_log_message("--- begin dump of recent events ---", true);
std::set<pthread_t> recent_pthread_ids;
std::set<std::pair<pthread_t, const char *>> recent_pthread_ids;
{
EntryVector t;
t.insert(t.end(), std::make_move_iterator(m_recent.begin()), std::make_move_iterator(m_recent.end()));
m_recent.clear();
for (const auto& e : t) {
recent_pthread_ids.emplace(e.m_thread);
recent_pthread_ids.emplace(std::make_pair(e.m_thread, e.m_thread_name));
}
_flush(t, true);
}
Expand All @@ -515,14 +515,11 @@ void Log::dump_recent()
m_stderr_log, m_stderr_crash), true);

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

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

0 comments on commit 6e72881

Please sign in to comment.