From 9db7f2cc23023ac99da41b292bf1431ad9fb0d57 Mon Sep 17 00:00:00 2001 From: Yang Liming Date: Thu, 21 Dec 2023 10:40:43 +0800 Subject: [PATCH] modify bthread attribute with tag --- src/brpc/acceptor.cpp | 5 +++-- src/brpc/details/http_message.h | 2 +- src/brpc/server.cpp | 5 ++++- src/bthread/bthread.cpp | 20 +++++++++++++------- src/bthread/task_group.cpp | 2 +- src/bthread/types.h | 13 +++++++------ 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/brpc/acceptor.cpp b/src/brpc/acceptor.cpp index 68d77082b7..d265725849 100644 --- a/src/brpc/acceptor.cpp +++ b/src/brpc/acceptor.cpp @@ -76,8 +76,9 @@ int Acceptor::StartAccept(int listened_fd, int idle_timeout_sec, return -1; } if (idle_timeout_sec > 0) { - if (bthread_start_background(&_close_idle_tid, NULL, - CloseIdleConnections, this) != 0) { + bthread_attr_t tmp = BTHREAD_ATTR_NORMAL; + tmp.tag = _bthread_tag; + if (bthread_start_background(&_close_idle_tid, &tmp, CloseIdleConnections, this) != 0) { LOG(FATAL) << "Fail to start bthread"; return -1; } diff --git a/src/brpc/details/http_message.h b/src/brpc/details/http_message.h index 97b3713214..1b2ae26c01 100644 --- a/src/brpc/details/http_message.h +++ b/src/brpc/details/http_message.h @@ -19,7 +19,7 @@ #ifndef BRPC_HTTP_MESSAGE_H #define BRPC_HTTP_MESSAGE_H -#include +#include // std::unique_ptr #include // std::string #include "butil/macros.h" #include "butil/iobuf.h" // butil::IOBuf diff --git a/src/brpc/server.cpp b/src/brpc/server.cpp index ac8f29c9f3..6145e7d444 100644 --- a/src/brpc/server.cpp +++ b/src/brpc/server.cpp @@ -906,6 +906,7 @@ int Server::StartInternal(const butil::EndPoint& endpoint, init_args[i].done = false; init_args[i].stop = false; bthread_attr_t tmp = BTHREAD_ATTR_NORMAL; + tmp.tag = _options.bthread_tag; tmp.keytable_pool = _keytable_pool; if (bthread_start_background( &init_args[i].th, &tmp, BthreadInitEntry, &init_args[i]) != 0) { @@ -1144,7 +1145,9 @@ int Server::StartInternal(const butil::EndPoint& endpoint, // Launch _derivative_thread. CHECK_EQ(INVALID_BTHREAD, _derivative_thread); - if (bthread_start_background(&_derivative_thread, NULL, + bthread_attr_t tmp = BTHREAD_ATTR_NORMAL; + tmp.tag = _options.bthread_tag; + if (bthread_start_background(&_derivative_thread, &tmp, UpdateDerivedVars, this) != 0) { LOG(ERROR) << "Fail to create _derivative_thread"; return -1; diff --git a/src/bthread/bthread.cpp b/src/bthread/bthread.cpp index 201a674592..316c53f798 100644 --- a/src/bthread/bthread.cpp +++ b/src/bthread/bthread.cpp @@ -174,26 +174,32 @@ start_from_non_worker(bthread_t* __restrict tid, if (NULL == c) { return ENOMEM; } - TaskGroup* g = NULL; + auto tag = BTHREAD_TAG_DEFAULT; + if (attr != NULL && attr->tag != BTHREAD_TAG_INVALID) { + tag = attr->tag; + } if (attr != NULL && (attr->flags & BTHREAD_NOSIGNAL)) { // Remember the TaskGroup to insert NOSIGNAL tasks for 2 reasons: // 1. NOSIGNAL is often for creating many bthreads in batch, // inserting into the same TaskGroup maximizes the batch. // 2. bthread_flush() needs to know which TaskGroup to flush. - g = tls_task_group_nosignal; + auto g = tls_task_group_nosignal; if (NULL == g) { - g = c->choose_one_group(attr->tag); + g = c->choose_one_group(tag); tls_task_group_nosignal = g; } return g->start_background(tid, attr, fn, arg); } - g = c->choose_one_group(attr ? attr->tag : BTHREAD_TAG_DEFAULT); - return g->start_background(tid, attr, fn, arg); + return c->choose_one_group(tag)->start_background(tid, attr, fn, arg); } -// if tag is default or equal to thread local use thread local task group +// Meet one of the three conditions, can run in thread local +// attr is nullptr +// tag equal to thread local +// tag equal to BTHREAD_TAG_INVALID BUTIL_FORCE_INLINE bool can_run_thread_local(const bthread_attr_t* __restrict attr) { - return attr == nullptr || attr->tag == bthread::tls_task_group->tag(); + return attr == nullptr || attr->tag == bthread::tls_task_group->tag() || + attr->tag == BTHREAD_TAG_INVALID; } struct TidTraits { diff --git a/src/bthread/task_group.cpp b/src/bthread/task_group.cpp index 1c2fd5221a..381243f9bb 100644 --- a/src/bthread/task_group.cpp +++ b/src/bthread/task_group.cpp @@ -40,7 +40,7 @@ namespace bthread { static const bthread_attr_t BTHREAD_ATTR_TASKGROUP = { - BTHREAD_STACKTYPE_UNKNOWN, 0, NULL, BTHREAD_TAG_DEFAULT }; + BTHREAD_STACKTYPE_UNKNOWN, 0, NULL, BTHREAD_TAG_INVALID }; static bool pass_bool(const char*, bool) { return true; } diff --git a/src/bthread/types.h b/src/bthread/types.h index e3fdaa8f96..cb39ae3c9d 100644 --- a/src/bthread/types.h +++ b/src/bthread/types.h @@ -34,6 +34,7 @@ static const bthread_t INVALID_BTHREAD = 0; // bthread tag default is 0 typedef int bthread_tag_t; +static const bthread_tag_t BTHREAD_TAG_INVALID = -1; static const bthread_tag_t BTHREAD_TAG_DEFAULT = 0; struct sockaddr; @@ -104,7 +105,7 @@ typedef struct bthread_attr_t { stack_type = (stacktype_and_flags & 7); flags = (stacktype_and_flags & ~(unsigned)7u); keytable_pool = NULL; - tag = BTHREAD_TAG_DEFAULT; + tag = BTHREAD_TAG_INVALID; } bthread_attr_t operator|(unsigned other_flags) const { CHECK(!(other_flags & 7)) << "flags=" << other_flags; @@ -122,22 +123,22 @@ typedef struct bthread_attr_t { // obvious drawback is that you need more worker pthreads when you have a lot // of such bthreads. static const bthread_attr_t BTHREAD_ATTR_PTHREAD = -{ BTHREAD_STACKTYPE_PTHREAD, 0, NULL, BTHREAD_TAG_DEFAULT }; +{ BTHREAD_STACKTYPE_PTHREAD, 0, NULL, BTHREAD_TAG_INVALID }; // bthreads created with following attributes will have different size of // stacks. Default is BTHREAD_ATTR_NORMAL. static const bthread_attr_t BTHREAD_ATTR_SMALL = {BTHREAD_STACKTYPE_SMALL, 0, NULL, - BTHREAD_TAG_DEFAULT}; + BTHREAD_TAG_INVALID}; static const bthread_attr_t BTHREAD_ATTR_NORMAL = {BTHREAD_STACKTYPE_NORMAL, 0, NULL, - BTHREAD_TAG_DEFAULT}; + BTHREAD_TAG_INVALID}; static const bthread_attr_t BTHREAD_ATTR_LARGE = {BTHREAD_STACKTYPE_LARGE, 0, NULL, - BTHREAD_TAG_DEFAULT}; + BTHREAD_TAG_INVALID}; // bthreads created with this attribute will print log when it's started, // context-switched, finished. static const bthread_attr_t BTHREAD_ATTR_DEBUG = { BTHREAD_STACKTYPE_NORMAL, BTHREAD_LOG_START_AND_FINISH | BTHREAD_LOG_CONTEXT_SWITCH, NULL, - BTHREAD_TAG_DEFAULT}; + BTHREAD_TAG_INVALID}; static const size_t BTHREAD_EPOLL_THREAD_NUM = 1; static const bthread_t BTHREAD_ATOMIC_INIT = 0;