Skip to content

Commit 7de5af5

Browse files
committed
common: cache pthread names
This provides common ceph entrypoints for the pthread_[gs]name functions which will also cache a thread_local copy. This also removes the pthread_t parameter which precipitated the bug i50743. Obviously, the overall goal here is to avoid system calls. See-also: https://tracker.ceph.com/issues/50743 Fixes: 0be8d01 Fixes: https://tracker.ceph.com/issues/68691 Signed-off-by: Patrick Donnelly <[email protected]>
1 parent 1f40d0c commit 7de5af5

25 files changed

+102
-134
lines changed

src/ceph_mds.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void handle_mds_signal(int signum)
8181

8282
int main(int argc, const char **argv)
8383
{
84-
ceph_pthread_setname(pthread_self(), "ceph-mds");
84+
ceph_pthread_setname("ceph-mds");
8585

8686
auto args = argv_to_vec(argc, argv);
8787
if (args.empty()) {

src/ceph_mgr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static void usage()
4141
*/
4242
int main(int argc, const char **argv)
4343
{
44-
ceph_pthread_setname(pthread_self(), "ceph-mgr");
44+
ceph_pthread_setname("ceph-mgr");
4545

4646
auto args = argv_to_vec(argc, argv);
4747
if (args.empty()) {

src/ceph_mon.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ int main(int argc, const char **argv)
250250
{
251251
// reset our process name, in case we did a respawn, so that it's not
252252
// left as "exe".
253-
ceph_pthread_setname(pthread_self(), "ceph-mon");
253+
ceph_pthread_setname("ceph-mon");
254254

255255
int err;
256256

src/ceph_nvmeof_monitor_client.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void usage()
4545
*/
4646
int main(int argc, const char **argv)
4747
{
48-
ceph_pthread_setname(pthread_self(), "ceph-nvmeof-monitor-client");
48+
ceph_pthread_setname("ceph-nvmeof-monitor-client");
4949

5050
auto args = argv_to_vec(argc, argv);
5151
if (args.empty()) {

src/client/SyntheticClient.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ SyntheticClient::SyntheticClient(StandaloneClient *client, int w)
290290

291291
void *synthetic_client_thread_entry(void *ptr)
292292
{
293+
ceph_pthread_setname("client");
293294
SyntheticClient *sc = static_cast<SyntheticClient*>(ptr);
294295
//int r =
295296
sc->run();
@@ -945,7 +946,6 @@ int SyntheticClient::start_thread()
945946

946947
pthread_create(&thread_id, NULL, synthetic_client_thread_entry, this);
947948
ceph_assert(thread_id);
948-
ceph_pthread_setname(thread_id, "client");
949949
return 0;
950950
}
951951

src/common/Thread.cc

Lines changed: 2 additions & 20 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::thread_name.c_str());
86+
ceph_pthread_setname(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::thread_name = name;
157+
thread_name = name;
158158

159159
int ret = try_create(stacksize);
160160
if (ret != 0) {
@@ -203,24 +203,6 @@ int Thread::set_affinity(int id)
203203
// Functions for std::thread
204204
// =========================
205205

206-
void set_thread_name(std::thread& t, const std::string& s) {
207-
int r = ceph_pthread_setname(t.native_handle(), s.c_str());
208-
if (r != 0) {
209-
throw std::system_error(r, std::generic_category());
210-
}
211-
}
212-
std::string get_thread_name(const std::thread& t) {
213-
std::string s(256, '\0');
214-
215-
int r = ceph_pthread_getname(const_cast<std::thread&>(t).native_handle(),
216-
s.data(), s.length());
217-
if (r != 0) {
218-
throw std::system_error(r, std::generic_category());
219-
}
220-
s.resize(std::strlen(s.data()));
221-
return s;
222-
}
223-
224206
void kill(std::thread& t, int signal)
225207
{
226208
auto r = ceph_pthread_kill(t.native_handle(), signal);

src/common/Thread.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
#include "include/ceph_assert.h"
2929
#include "include/compat.h"
30-
#include "include/spinlock.h"
3130

3231
extern pid_t ceph_gettid();
3332

@@ -36,7 +35,7 @@ class Thread {
3635
pthread_t thread_id;
3736
pid_t pid;
3837
int cpuid;
39-
static inline thread_local std::string thread_name;
38+
std::string thread_name;
4039

4140
void *entry_wrapper();
4241

@@ -64,15 +63,10 @@ class Thread {
6463
int join(void **prval = 0);
6564
int detach();
6665
int set_affinity(int cpuid);
67-
static const std::string get_thread_name() {
68-
return Thread::thread_name;
69-
}
7066
};
7167

7268
// Functions for with std::thread
7369

74-
void set_thread_name(std::thread& t, const std::string& s);
75-
std::string get_thread_name(const std::thread& t);
7670
void kill(std::thread& t, int signal);
7771

7872
template<typename Fun, typename... Args>
@@ -81,7 +75,7 @@ std::thread make_named_thread(std::string_view n,
8175
Args&& ...args) {
8276

8377
return std::thread([n = std::string(n)](auto&& fun, auto&& ...args) {
84-
ceph_pthread_setname(pthread_self(), n.data());
78+
ceph_pthread_setname(n.data());
8579
std::invoke(std::forward<Fun>(fun),
8680
std::forward<Args>(args)...);
8781
}, std::forward<Fun>(fun), std::forward<Args>(args)...);

src/common/assert.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ namespace ceph {
4444
g_assert_line = line;
4545
g_assert_func = func;
4646
g_assert_thread = (unsigned long long)pthread_self();
47-
ceph_pthread_getname(pthread_self(), g_assert_thread_name,
48-
sizeof(g_assert_thread_name));
47+
ceph_pthread_getname(g_assert_thread_name, sizeof(g_assert_thread_name));
4948

5049
ostringstream tss;
5150
tss << ceph_clock_now();
@@ -122,8 +121,7 @@ namespace ceph {
122121
g_assert_line = line;
123122
g_assert_func = func;
124123
g_assert_thread = (unsigned long long)pthread_self();
125-
ceph_pthread_getname(pthread_self(), g_assert_thread_name,
126-
sizeof(g_assert_thread_name));
124+
ceph_pthread_getname(g_assert_thread_name, sizeof(g_assert_thread_name));
127125

128126
BufAppender ba(g_assert_msg, sizeof(g_assert_msg));
129127
BackTrace *bt = new ClibBackTrace(1);
@@ -168,8 +166,7 @@ namespace ceph {
168166
g_assert_line = line;
169167
g_assert_func = func;
170168
g_assert_thread = (unsigned long long)pthread_self();
171-
ceph_pthread_getname(pthread_self(), g_assert_thread_name,
172-
sizeof(g_assert_thread_name));
169+
ceph_pthread_getname(g_assert_thread_name, sizeof(g_assert_thread_name));
173170

174171
BackTrace *bt = new ClibBackTrace(1);
175172
snprintf(g_assert_msg, sizeof(g_assert_msg),
@@ -210,8 +207,7 @@ namespace ceph {
210207
g_assert_line = line;
211208
g_assert_func = func;
212209
g_assert_thread = (unsigned long long)pthread_self();
213-
ceph_pthread_getname(pthread_self(), g_assert_thread_name,
214-
sizeof(g_assert_thread_name));
210+
ceph_pthread_getname(g_assert_thread_name, sizeof(g_assert_thread_name));
215211

216212
BufAppender ba(g_assert_msg, sizeof(g_assert_msg));
217213
BackTrace *bt = new ClibBackTrace(1);

src/common/ceph_timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class timer {
9898
std::thread thread;
9999

100100
void timer_thread() {
101+
ceph_pthread_setname("ceph_timer");
101102
std::unique_lock l(lock);
102103
while (!suspended) {
103104
auto now = TC::now();
@@ -155,7 +156,6 @@ class timer {
155156
public:
156157
timer() : suspended(false) {
157158
thread = std::thread(&timer::timer_thread, this);
158-
set_thread_name(thread, "ceph_timer");
159159
}
160160

161161
// Create a suspended timer, jobs will be executed in order when

src/common/code_environment.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
* Foundation. See file COPYING.
1212
*
1313
*/
14+
#include "include/compat.h"
1415

1516
#include "common/code_environment.h"
1617

1718
#include <iostream>
1819

1920
#include "acconfig.h"
2021

21-
#ifdef HAVE_PTHREAD_GETNAME_NP
22-
#include <pthread.h>
23-
#endif
24-
2522
#include <string.h>
2623

2724
code_environment_t g_code_env = CODE_ENVIRONMENT_UTILITY;
@@ -57,7 +54,7 @@ int get_process_name(char *buf, int len)
5754
}
5855
// FIPS zeroization audit 20191115: this memset is not security related.
5956
memset(buf, 0, len);
60-
return pthread_getname_np(pthread_self(), buf, len);
57+
return ceph_pthread_getname(buf, len);
6158
}
6259

6360
#elif defined(HAVE_GETPROGNAME)

0 commit comments

Comments
 (0)