Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many threads in Ceph (and the MDS) which do not use this class Thread. So the "name" will wrongly be empty every time this is called.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to make those threads use Thread class then.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the idea behind helpers like make_named_thread() was the opposite -- slowly move off of Thread and just use std::thread.

}
};

// 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