Skip to content

Commit afe783f

Browse files
committed
Support naming bthread to help debug
The bthread name is shown when checking bthread status by curl ip:port/bthreads/xxx, which helps to debug when bthread trace is not enabled.
1 parent a54b607 commit afe783f

14 files changed

+58
-23
lines changed

src/brpc/acceptor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ int Acceptor::StartAccept(int listened_fd, int idle_timeout_sec,
7878
if (idle_timeout_sec > 0) {
7979
bthread_attr_t tmp = BTHREAD_ATTR_NORMAL;
8080
tmp.tag = _bthread_tag;
81+
tmp.set_name("CloseIdleConnections");
8182
if (bthread_start_background(&_close_idle_tid, &tmp, CloseIdleConnections, this) != 0) {
8283
LOG(FATAL) << "Fail to start bthread";
8384
return -1;

src/brpc/controller.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ void Controller::OnVersionedRPCReturned(const CompletionInfo& info,
743743
bthread_t bt;
744744
bthread_attr_t attr = (FLAGS_usercode_in_pthread ?
745745
BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
746+
attr.set_name("RunEndRPC");
746747
_tmp_completion_info = info;
747748
if (bthread_start_background(&bt, &attr, RunEndRPC, this) != 0) {
748749
LOG(FATAL) << "Fail to start bthread";

src/brpc/event_dispatcher_epoll.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ int EventDispatcher::Start(const bthread_attr_t* thread_attr) {
7878
// Only event dispatcher thread has flag BTHREAD_GLOBAL_PRIORITY.
7979
bthread_attr_t epoll_thread_attr =
8080
_thread_attr | BTHREAD_NEVER_QUIT | BTHREAD_GLOBAL_PRIORITY;
81+
epoll_thread_attr.set_name("EventDispatcher::RunThis");
8182

8283
// Polling thread uses the same attr for consumer threads (NORMAL right
8384
// now). Previously, we used small stack (32KB) which may be overflowed

src/brpc/event_dispatcher_kqueue.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ int EventDispatcher::Start(const bthread_attr_t* thread_attr) {
7878
// Only event dispatcher thread has flag BTHREAD_GLOBAL_PRIORITY.
7979
bthread_attr_t kqueue_thread_attr =
8080
_thread_attr | BTHREAD_NEVER_QUIT | BTHREAD_GLOBAL_PRIORITY;
81+
kqueue_thread_attr.set_name("EventDispatcher::RunThis");
8182

8283
// Polling thread uses the same attr for consumer threads (NORMAL right
8384
// now). Previously, we used small stack (32KB) which may be overflowed

src/brpc/global.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,9 @@ static void GlobalInitializeOrDieImpl() {
635635

636636
// We never join GlobalUpdate, let it quit with the process.
637637
bthread_t th;
638-
CHECK(bthread_start_background(&th, NULL, GlobalUpdate, NULL) == 0)
638+
bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
639+
attr.set_name("GlobalUpdate");
640+
CHECK(bthread_start_background(&th, &attr, GlobalUpdate, NULL) == 0)
639641
<< "Fail to start GlobalUpdate";
640642
}
641643

src/brpc/input_messenger.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ static void QueueMessage(InputMessageBase* to_run_msg,
197197
if (!to_run_msg) {
198198
return;
199199
}
200+
201+
#if BRPC_WITH_RDMA
202+
if (rdma::FLAGS_rdma_disable_bthread) {
203+
ProcessInputMessage(to_run_msg);
204+
return;
205+
}
206+
#endif
200207
// Create bthread for last_msg. The bthread is not scheduled
201208
// until bthread_flush() is called (in the worse case).
202209

@@ -207,14 +214,8 @@ static void QueueMessage(InputMessageBase* to_run_msg,
207214
BTHREAD_ATTR_NORMAL) | BTHREAD_NOSIGNAL;
208215
tmp.keytable_pool = keytable_pool;
209216
tmp.tag = bthread_self_tag();
210-
211-
#if BRPC_WITH_RDMA
212-
if (rdma::FLAGS_rdma_disable_bthread) {
213-
ProcessInputMessage(to_run_msg);
214-
return;
215-
}
216-
#endif
217-
217+
tmp.set_name("ProcessInputMessage");
218+
218219
if (!FLAGS_usercode_in_coroutine && bthread_start_background(
219220
&th, &tmp, ProcessInputMessage, to_run_msg) == 0) {
220221
++*num_bthread_created;

src/brpc/periodic_task.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ static void* PeriodicTaskThread(void* arg) {
3838

3939
static void RunPeriodicTaskThread(void* arg) {
4040
bthread_t th = 0;
41+
bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
42+
attr.set_name("PeriodicTaskThread");
4143
int rc = bthread_start_background(
42-
&th, &BTHREAD_ATTR_NORMAL, PeriodicTaskThread, arg);
44+
&th, &attr, PeriodicTaskThread, arg);
4345
if (rc != 0) {
4446
LOG(ERROR) << "Fail to start PeriodicTaskThread";
4547
static_cast<PeriodicTask*>(arg)->OnDestroyingTask();

src/brpc/rdma/rdma_endpoint.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ void RdmaConnect::StartConnect(const Socket* socket,
250250
_done = done;
251251
_data = data;
252252
bthread_t tid;
253-
if (bthread_start_background(&tid, &BTHREAD_ATTR_NORMAL,
253+
bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
254+
attr.set_name("RdmaProcessHandshakeAtClient");
255+
if (bthread_start_background(&tid, &attr,
254256
RdmaEndpoint::ProcessHandshakeAtClient, socket->_rdma_ep) < 0) {
255257
LOG(FATAL) << "Fail to start handshake bthread";
256258
} else {
@@ -309,7 +311,9 @@ void RdmaEndpoint::OnNewDataFromTcp(Socket* m) {
309311
ep->_state = S_HELLO_WAIT;
310312
SocketUniquePtr s;
311313
m->ReAddress(&s);
312-
if (bthread_start_background(&tid, &BTHREAD_ATTR_NORMAL,
314+
bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
315+
attr.set_name("RdmaProcessHandshakeAtServer");
316+
if (bthread_start_background(&tid, &attr,
313317
ProcessHandshakeAtServer, ep) < 0) {
314318
ep->_state = UNINIT;
315319
LOG(FATAL) << "Fail to start handshake bthread";
@@ -1616,6 +1620,7 @@ int RdmaEndpoint::PollingModeInitialize(bthread_tag_t tag,
16161620
auto attr = FLAGS_rdma_disable_bthread ? BTHREAD_ATTR_PTHREAD
16171621
: BTHREAD_ATTR_NORMAL;
16181622
attr.tag = tag;
1623+
attr.set_name("RdmaPolling");
16191624
pollers[i].callback = callback;
16201625
pollers[i].init_fn = init_fn;
16211626
pollers[i].release_fn = release_fn;

src/brpc/server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ int Server::StartInternal(const butil::EndPoint& endpoint,
12361236
CHECK_EQ(INVALID_BTHREAD, _derivative_thread);
12371237
bthread_attr_t tmp = BTHREAD_ATTR_NORMAL;
12381238
tmp.tag = _options.bthread_tag;
1239+
tmp.set_name("UpdateDerivedVars");
12391240
if (bthread_start_background(&_derivative_thread, &tmp,
12401241
UpdateDerivedVars, this) != 0) {
12411242
LOG(ERROR) << "Fail to create _derivative_thread";

src/brpc/socket.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,8 +1491,10 @@ void Socket::AfterAppConnected(int err, void* data) {
14911491
// requests are not setup yet. check the comment on Setup() in Write()
14921492
req->Setup(s);
14931493
bthread_t th;
1494+
bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
1495+
attr.set_name("KeepWrite");
14941496
if (bthread_start_background(
1495-
&th, &BTHREAD_ATTR_NORMAL, KeepWrite, req) != 0) {
1497+
&th, &attr, KeepWrite, req) != 0) {
14961498
PLOG(WARNING) << "Fail to start KeepWrite";
14971499
KeepWrite(req);
14981500
}
@@ -1532,7 +1534,9 @@ int Socket::KeepWriteIfConnected(int fd, int err, void* data) {
15321534
bthread_t th;
15331535
std::unique_ptr<google::protobuf::Closure> thrd_func(brpc::NewCallback(
15341536
Socket::CheckConnectedAndKeepWrite, fd, err, data));
1535-
if ((err = bthread_start_background(&th, &BTHREAD_ATTR_NORMAL,
1537+
bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
1538+
attr.set_name("CheckConnectedAndKeepWrite");
1539+
if ((err = bthread_start_background(&th, &attr,
15361540
RunClosure, thrd_func.get())) == 0) {
15371541
thrd_func.release();
15381542
return 0;
@@ -1705,6 +1709,8 @@ int Socket::StartWrite(WriteRequest* req, const WriteOptions& opt) {
17051709

17061710
int saved_errno = 0;
17071711
bthread_t th;
1712+
bthread_attr_t attr = BTHREAD_ATTR_NORMAL;
1713+
attr.set_name("KeepWrite");
17081714
SocketUniquePtr ptr_for_keep_write;
17091715
ssize_t nw = 0;
17101716
int ret = 0;
@@ -1779,7 +1785,7 @@ int Socket::StartWrite(WriteRequest* req, const WriteOptions& opt) {
17791785
KEEPWRITE_IN_BACKGROUND:
17801786
ReAddress(&ptr_for_keep_write);
17811787
req->set_socket(ptr_for_keep_write.release());
1782-
if (bthread_start_background(&th, &BTHREAD_ATTR_NORMAL,
1788+
if (bthread_start_background(&th, &attr,
17831789
KeepWrite, req) != 0) {
17841790
LOG(FATAL) << "Fail to start KeepWrite";
17851791
KeepWrite(req);
@@ -2266,6 +2272,7 @@ int Socket::OnInputEvent(void* user_data, uint32_t events,
22662272
bthread_attr_t attr = thread_attr;
22672273
attr.keytable_pool = p->_keytable_pool;
22682274
attr.tag = bthread_self_tag();
2275+
attr.set_name("ProcessEvent");
22692276
if (FLAGS_usercode_in_coroutine) {
22702277
ProcessEvent(p);
22712278
#if BRPC_WITH_RDMA

0 commit comments

Comments
 (0)