Skip to content

Commit 43cccf4

Browse files
committed
set worker thread name
1 parent ede4505 commit 43cccf4

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

myframe/app.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ std::shared_ptr<WorkerTimer> App::GetTimerWorker() {
3434
LOG(ERROR) << "worker context manager is nullptr";
3535
return nullptr;
3636
}
37-
std::string worker_timer_name = "worker.timer.#1";
37+
std::string worker_timer_name = "worker.T.1";
3838
auto w = ev_mgr_->Get<WorkerContext>(worker_timer_name);
3939
if (w == nullptr) {
4040
LOG(ERROR)
@@ -440,7 +440,7 @@ bool App::StartCommonWorker(int worker_count) {
440440
for (int i = 0; i < worker_count; ++i) {
441441
auto worker = std::make_shared<WorkerCommon>();
442442
worker->SetModName("class");
443-
worker->SetTypeName("WorkerCommon");
443+
worker->SetTypeName("C");
444444
if (!AddWorker(std::to_string(i), worker)) {
445445
LOG(ERROR) << "start common worker " << i << " failed";
446446
continue;
@@ -454,8 +454,8 @@ bool App::StartCommonWorker(int worker_count) {
454454
bool App::StartTimerWorker() {
455455
auto worker = std::make_shared<WorkerTimer>();
456456
worker->SetModName("class");
457-
worker->SetTypeName("timer");
458-
if (!AddWorker("#1", worker)) {
457+
worker->SetTypeName("T");
458+
if (!AddWorker("1", worker)) {
459459
LOG(ERROR) << "start timer worker failed";
460460
return false;
461461
}

myframe/common.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,36 @@ int Common::SetSelfThreadAffinity(int cpu_core) {
192192
#endif
193193
}
194194

195+
int Common::SetThreadName(std::thread* t, const std::string& name) {
196+
#if defined(MYFRAME_OS_WINDOWS)
197+
auto handle = t->native_handle();
198+
auto res = SetThreadDescription(handle, name.c_str());
199+
if (res != 0) {
200+
return -1;
201+
}
202+
return 0;
203+
#elif defined(MYFRAME_OS_MACOSX)
204+
// unsupport
205+
return -1;
206+
#else
207+
auto handle = t->native_handle();
208+
return pthread_setname_np(handle, name.c_str());
209+
#endif
210+
}
211+
212+
int Common::SetSelfThreadName(const std::string& name) {
213+
#if defined(MYFRAME_OS_WINDOWS)
214+
auto handle = GetCurrentThread();
215+
auto res = SetThreadDescription(handle, name.c_str());
216+
if (res != 0) {
217+
return -1;
218+
}
219+
return 0;
220+
#elif defined(MYFRAME_OS_MACOSX)
221+
return pthread_setname_np(name.c_str());
222+
#else
223+
return pthread_setname_np(pthread_self(), name.c_str());
224+
#endif
225+
}
226+
195227
} // namespace myframe

myframe/common.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class MYFRAME_EXPORT Common final {
3737

3838
static int SetThreadAffinity(std::thread* t, int cpu_core);
3939
static int SetSelfThreadAffinity(int cpu_core);
40+
static int SetThreadName(std::thread* t, const std::string& name);
41+
static int SetSelfThreadName(const std::string& name);
4042
};
4143

4244
} // namespace myframe

myframe/worker_context.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ bool WorkerContext::SetThreadAffinity(int cpu_core) {
7575

7676
void WorkerContext::Initialize() {
7777
mailbox_.SetAddr(worker_->GetWorkerName());
78+
std::string th_name = mailbox_.Addr();
79+
th_name = th_name.size() >= 16 ? th_name.substr(0, 15) : th_name;
80+
if (Common::SetSelfThreadName(th_name)) {
81+
LOG(WARNING) << "set thread name " << th_name << " failed";
82+
}
7883
worker_->Init();
7984
}
8085

0 commit comments

Comments
 (0)