Skip to content

Commit

Permalink
change thread name
Browse files Browse the repository at this point in the history
  • Loading branch information
brother-jin committed Jun 12, 2024
1 parent d6cb38d commit 58df896
Show file tree
Hide file tree
Showing 15 changed files with 25 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/net/include/backend_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class BackendThread : public Thread {
*/
int StartThread() override;
int StopThread() override;
void set_thread_name(const std::string& name) override { Thread::set_thread_name(name); }
pstd::Status Write(int fd, const std::string& msg);
pstd::Status Close(int fd);
// Try to connect fd noblock, if return EINPROGRESS or EAGAIN or EWOULDBLOCK
Expand Down
1 change: 1 addition & 0 deletions src/net/include/client_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class ClientThread : public Thread {
*/
int StartThread() override;
int StopThread() override;
void set_thread_name(const std::string& name) override { Thread::set_thread_name(name); }
pstd::Status Write(const std::string& ip, int port, const std::string& msg);
pstd::Status Close(const std::string& ip, int port);

Expand Down
2 changes: 1 addition & 1 deletion src/net/include/net_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Thread : public pstd::noncopyable {

std::string thread_name() const { return thread_name_; }

void set_thread_name(const std::string& name) { thread_name_ = name; }
virtual void set_thread_name(const std::string& name) { thread_name_ = name; }

protected:
std::atomic_bool should_stop_;
Expand Down
2 changes: 2 additions & 0 deletions src/net/include/server_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class ServerThread : public Thread {
// Move into server thread
virtual void MoveConnIn(std::shared_ptr<NetConn> conn, const NotifyType& type) = 0;

void set_thread_name(const std::string& name) override { Thread::set_thread_name(name); }

virtual void KillAllConns() = 0;
virtual bool KillConn(const std::string& ip_port) = 0;

Expand Down
2 changes: 2 additions & 0 deletions src/net/src/backend_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ int BackendThread::StartThread() {
if (res) {
return res;
}
set_thread_name("BackendThread");

return Thread::StartThread();
}

Expand Down
2 changes: 2 additions & 0 deletions src/net/src/client_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ int ClientThread::StartThread() {
if (res) {
return res;
}
set_thread_name("ClientThread");

return Thread::StartThread();
}

Expand Down
2 changes: 1 addition & 1 deletion src/net/src/net_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Thread::~Thread() = default;
void* Thread::RunThread(void* arg) {
auto thread = reinterpret_cast<Thread*>(arg);
if (!(thread->thread_name().empty())) {
SetThreadName(pthread_self(), "pika");
SetThreadName(pthread_self(), thread->thread_name());
}
thread->ThreadMain();
return nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/net/src/net_thread_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ inline bool SetThreadName(pthread_t id, const std::string& name) {
#else
inline bool SetThreadName(pthread_t id, const std::string& name) {
// printf ("no pthread_setname\n");
return false;
pthread_setname_np(name.c_str());
}
#endif
} // namespace net
Expand Down
1 change: 1 addition & 0 deletions src/net/src/net_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int TimerTaskThread::StartThread() {
// if there is no timer task registered, no need of start the thread
return -1;
}
set_thread_name("TimerTaskThread");
LOG(INFO) << "TimerTaskThread Starting...";
return Thread::StartThread();
}
Expand Down
1 change: 1 addition & 0 deletions src/net/src/net_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class TimerTaskThread : public Thread {
~TimerTaskThread() override;
int StartThread() override;
int StopThread() override;
void set_thread_name(const std::string& name) override { Thread::set_thread_name(name); }

uint32_t AddTimerTask(const std::string& task_name, int interval_ms, bool repeat_exec, const std::function<void()> &task){
return timer_task_manager_.AddTimerTask(task_name, interval_ms, repeat_exec, task);
Expand Down
1 change: 1 addition & 0 deletions src/net/src/server_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ int ServerThread::StartThread() {
if (ret != kSuccess) {
return ret;
}
set_thread_name("ServerThread");
return Thread::StartThread();
}

Expand Down
4 changes: 3 additions & 1 deletion src/net/src/thread_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <sys/time.h>

#include <string>
#include <utility>

namespace net {
Expand All @@ -24,7 +25,8 @@ int ThreadPool::Worker::start() {
return -1;
} else {
start_.store(true);
SetThreadName(thread_id_, thread_pool_->thread_pool_name() + "Worker");
std::string thread_id_str = std::to_string(reinterpret_cast<unsigned long>(thread_id_));
SetThreadName(thread_id_, thread_pool_->thread_pool_name() + "_Worker_" + thread_id_str);
}
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/pika_repl_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extern PikaServer* g_pika_server;
extern std::unique_ptr<PikaReplicaManager> g_pika_rm;

PikaReplServer::PikaReplServer(const std::set<std::string>& ips, int port, int cron_interval) {
server_tp_ = std::make_unique<net::ThreadPool>(PIKA_REPL_SERVER_TP_SIZE, 100000);
server_tp_ = std::make_unique<net::ThreadPool>(PIKA_REPL_SERVER_TP_SIZE, 100000, "PikaReplServer");
pika_repl_server_thread_ = std::make_unique<PikaReplServerThread>(ips, port, cron_interval);
pika_repl_server_thread_->set_thread_name("PikaReplServer");
}
Expand Down
5 changes: 5 additions & 0 deletions src/pika_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,13 @@ size_t PikaServer::SlowCmdThreadPoolMaxQueueSize() {
}

void PikaServer::BGSaveTaskSchedule(net::TaskFunc func, void* arg) {
bgsave_thread_.set_thread_name("BGSaveTaskThread");
bgsave_thread_.StartThread();
bgsave_thread_.Schedule(func, arg);
}

void PikaServer::PurgelogsTaskSchedule(net::TaskFunc func, void* arg) {
purge_thread_.set_thread_name("PurgelogsTaskThread");
purge_thread_.StartThread();
purge_thread_.Schedule(func, arg);
}
Expand All @@ -774,6 +776,7 @@ void PikaServer::PurgeDir(const std::string& path) {
}

void PikaServer::PurgeDirTaskSchedule(void (*function)(void*), void* arg) {
purge_thread_.set_thread_name("PurgeDirTaskThread");
purge_thread_.StartThread();
purge_thread_.Schedule(function, arg);
}
Expand Down Expand Up @@ -824,6 +827,7 @@ void PikaServer::TryDBSync(const std::string& ip, int port, const std::string& d
}

void PikaServer::KeyScanTaskSchedule(net::TaskFunc func, void* arg) {
key_scan_thread_.set_thread_name("KeyScanTaskThread");
key_scan_thread_.StartThread();
key_scan_thread_.Schedule(func, arg);
}
Expand Down Expand Up @@ -1463,6 +1467,7 @@ void PikaServer::Bgslotsreload(const std::shared_ptr<DB>& db) {
LOG(INFO) << "Start slot reloading";

// Start new thread if needed
bgsave_thread_.set_thread_name("Bgslotsreload");
bgsave_thread_.StartThread();
bgsave_thread_.Schedule(&DoBgslotsreload, static_cast<void*>(this));
}
Expand Down
4 changes: 2 additions & 2 deletions src/rsync_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void RsyncWriteResp(RsyncService::RsyncResponse& response, std::shared_ptr<net::
}

RsyncServer::RsyncServer(const std::set<std::string>& ips, const int port) {
work_thread_ = std::make_unique<net::ThreadPool>(2, 100000);
work_thread_ = std::make_unique<net::ThreadPool>(2, 100000, "RsyncServerThreadPool");
rsync_server_thread_ = std::make_unique<RsyncServerThread>(ips, port, 1 * 1000, this);
rsync_server_thread_->set_thread_name("RsyncServer");
rsync_server_thread_->set_thread_name("RsyncServerThread");
}

RsyncServer::~RsyncServer() {
Expand Down

0 comments on commit 58df896

Please sign in to comment.