Skip to content

Commit

Permalink
set worker thread name
Browse files Browse the repository at this point in the history
  • Loading branch information
lkpworkspace committed Jan 3, 2025
1 parent ede4505 commit 43cccf4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
8 changes: 4 additions & 4 deletions myframe/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ std::shared_ptr<WorkerTimer> App::GetTimerWorker() {
LOG(ERROR) << "worker context manager is nullptr";
return nullptr;
}
std::string worker_timer_name = "worker.timer.#1";
std::string worker_timer_name = "worker.T.1";
auto w = ev_mgr_->Get<WorkerContext>(worker_timer_name);
if (w == nullptr) {
LOG(ERROR)
Expand Down Expand Up @@ -440,7 +440,7 @@ bool App::StartCommonWorker(int worker_count) {
for (int i = 0; i < worker_count; ++i) {
auto worker = std::make_shared<WorkerCommon>();
worker->SetModName("class");
worker->SetTypeName("WorkerCommon");
worker->SetTypeName("C");
if (!AddWorker(std::to_string(i), worker)) {
LOG(ERROR) << "start common worker " << i << " failed";
continue;
Expand All @@ -454,8 +454,8 @@ bool App::StartCommonWorker(int worker_count) {
bool App::StartTimerWorker() {
auto worker = std::make_shared<WorkerTimer>();
worker->SetModName("class");
worker->SetTypeName("timer");
if (!AddWorker("#1", worker)) {
worker->SetTypeName("T");
if (!AddWorker("1", worker)) {
LOG(ERROR) << "start timer worker failed";
return false;
}
Expand Down
32 changes: 32 additions & 0 deletions myframe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,36 @@ int Common::SetSelfThreadAffinity(int cpu_core) {
#endif
}

int Common::SetThreadName(std::thread* t, const std::string& name) {
#if defined(MYFRAME_OS_WINDOWS)
auto handle = t->native_handle();
auto res = SetThreadDescription(handle, name.c_str());
if (res != 0) {
return -1;
}
return 0;
#elif defined(MYFRAME_OS_MACOSX)
// unsupport
return -1;
#else
auto handle = t->native_handle();
return pthread_setname_np(handle, name.c_str());
#endif
}

int Common::SetSelfThreadName(const std::string& name) {
#if defined(MYFRAME_OS_WINDOWS)
auto handle = GetCurrentThread();
auto res = SetThreadDescription(handle, name.c_str());
if (res != 0) {
return -1;
}
return 0;
#elif defined(MYFRAME_OS_MACOSX)
return pthread_setname_np(name.c_str());
#else
return pthread_setname_np(pthread_self(), name.c_str());
#endif
}

} // namespace myframe
2 changes: 2 additions & 0 deletions myframe/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class MYFRAME_EXPORT Common final {

static int SetThreadAffinity(std::thread* t, int cpu_core);
static int SetSelfThreadAffinity(int cpu_core);
static int SetThreadName(std::thread* t, const std::string& name);
static int SetSelfThreadName(const std::string& name);
};

} // namespace myframe
5 changes: 5 additions & 0 deletions myframe/worker_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ bool WorkerContext::SetThreadAffinity(int cpu_core) {

void WorkerContext::Initialize() {
mailbox_.SetAddr(worker_->GetWorkerName());
std::string th_name = mailbox_.Addr();
th_name = th_name.size() >= 16 ? th_name.substr(0, 15) : th_name;
if (Common::SetSelfThreadName(th_name)) {
LOG(WARNING) << "set thread name " << th_name << " failed";
}
worker_->Init();
}

Expand Down

0 comments on commit 43cccf4

Please sign in to comment.