Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

epoll bthread deal first #2819

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/brpc/event_dispatcher_epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ EventDispatcher::EventDispatcher()
: _event_dispatcher_fd(-1)
, _stop(false)
, _tid(0)
, _thread_attr(BTHREAD_ATTR_NORMAL) {
, _thread_attr(BTHREAD_ATTR_EPOLL) {
_event_dispatcher_fd = epoll_create(1024 * 1024);
if (_event_dispatcher_fd < 0) {
PLOG(FATAL) << "Fail to create epoll";
Expand Down Expand Up @@ -70,7 +70,8 @@ int EventDispatcher::Start(const bthread_attr_t* consumer_thread_attr) {
// Set _thread_attr before creating epoll thread to make sure
// everyting seems sane to the thread.
_thread_attr = consumer_thread_attr ?
*consumer_thread_attr : BTHREAD_ATTR_NORMAL;
*consumer_thread_attr : _thread_attr;
_thread_attr = _thread_attr | BTHREAD_GLOBAL_PRIORITY;

//_thread_attr is used in StartInputEvent(), assign flag NEVER_QUIT to it will cause new bthread
// that created by epoll_wait() never to quit.
Expand Down
1 change: 1 addition & 0 deletions src/bthread/parking_lot.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class BAIDU_CACHELINE_ALIGNMENT ParkingLot {
_pending_signal.fetch_or(1);
futex_wake_private(&_pending_signal, 10000);
}

private:
// higher 31 bits for signalling, LSB for stopping.
butil::atomic<int> _pending_signal;
Expand Down
10 changes: 10 additions & 0 deletions src/bthread/task_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ TaskControl::TaskControl()
, _signal_per_second(&_cumulated_signal_count)
, _status(print_rq_sizes_in_the_tc, this)
, _nbthreads("bthread_count")
, _priority_qs(FLAGS_task_group_ntags)
, _pl(FLAGS_task_group_ntags)
{}

Expand All @@ -207,6 +208,10 @@ int TaskControl::init(int concurrency) {
_tagged_worker_usage_second.push_back(new bvar::PerSecond<bvar::PassiveStatus<double>>(
"bthread_worker_usage", tag_str, _tagged_cumulated_worker_time[i], 1));
_tagged_nbthreads.push_back(new bvar::Adder<int64_t>("bthread_count", tag_str));
if (_priority_qs[i].init(BTHREAD_MAX_CONCURRENCY) != 0) {
LOG(FATAL) << "Fail to init _priority_q";
return -1;
}
}

// Make sure TimerThread is ready.
Expand Down Expand Up @@ -430,6 +435,11 @@ int TaskControl::_destroy_group(TaskGroup* g) {

bool TaskControl::steal_task(bthread_t* tid, size_t* seed, size_t offset) {
auto tag = tls_task_group->tag();

if (_priority_qs[tag].steal(tid)) {
return true;
}

// 1: Acquiring fence is paired with releasing fence in _add_group to
// avoid accessing uninitialized slot of _groups.
const size_t ngroup = tag_ngroup(tag).load(butil::memory_order_acquire/*1*/);
Expand Down
5 changes: 5 additions & 0 deletions src/bthread/task_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ friend bthread_t init_for_pthread_stack_trace();
std::string stack_trace(bthread_t tid);
#endif // BRPC_BTHREAD_TRACER

void push_priority_q(bthread_tag_t tag, bthread_t tid) {
_priority_qs[tag].push(tid);
}

private:
typedef std::array<TaskGroup*, BTHREAD_MAX_CONCURRENCY> TaggedGroups;
static const int PARKING_LOT_NUM = 4;
Expand Down Expand Up @@ -153,6 +157,7 @@ friend bthread_t init_for_pthread_stack_trace();
std::vector<bvar::PassiveStatus<double>*> _tagged_cumulated_worker_time;
std::vector<bvar::PerSecond<bvar::PassiveStatus<double>>*> _tagged_worker_usage_second;
std::vector<bvar::Adder<int64_t>*> _tagged_nbthreads;
std::vector<WorkStealingQueue<bthread_t>> _priority_qs;

std::vector<TaggedParkingLot> _pl;

Expand Down
18 changes: 15 additions & 3 deletions src/bthread/task_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,12 +429,19 @@ int TaskGroup::start_foreground(TaskGroup** pg,
} else {
// NOSIGNAL affects current task, not the new task.
RemainedFn fn = NULL;
if (g->current_task()->about_to_quit) {
auto& cur_attr = g->get_current_attr();
if (cur_attr.flags & BTHREAD_GLOBAL_PRIORITY) {
fn = priority_to_run;
} else if (g->current_task()->about_to_quit) {
fn = ready_to_run_in_worker_ignoresignal;
} else {
fn = ready_to_run_in_worker;
}
ReadyToRunArgs args = { g->_cur_meta, (bool)(using_attr.flags & BTHREAD_NOSIGNAL) };
ReadyToRunArgs args = {
g->tag(),
g->_cur_meta,
(bool)(using_attr.flags & BTHREAD_NOSIGNAL)
};
g->set_remained(fn, &args);
TaskGroup::sched_to(pg, m->tid);
}
Expand Down Expand Up @@ -798,6 +805,11 @@ void TaskGroup::ready_to_run_in_worker_ignoresignal(void* args_in) {
return tls_task_group->push_rq(args->meta->tid);
}

void TaskGroup::priority_to_run(void* args_in) {
ReadyToRunArgs* args = static_cast<ReadyToRunArgs*>(args_in);
return tls_task_group->control()->push_priority_q(args->tag, args->meta->tid);
}

struct SleepArgs {
uint64_t timeout_us;
bthread_t tid;
Expand Down Expand Up @@ -972,7 +984,7 @@ int TaskGroup::interrupt(bthread_t tid, TaskControl* c, bthread_tag_t tag) {

void TaskGroup::yield(TaskGroup** pg) {
TaskGroup* g = *pg;
ReadyToRunArgs args = { g->_cur_meta, false };
ReadyToRunArgs args = { g->tag(), g->_cur_meta, false };
g->set_remained(ready_to_run_in_worker, &args);
sched(pg);
}
Expand Down
4 changes: 4 additions & 0 deletions src/bthread/task_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class TaskGroup {
return total_ns;
}

const bthread_attr_t& get_current_attr() { return _cur_meta->attr; }

private:
friend class TaskControl;

Expand All @@ -218,11 +220,13 @@ friend class TaskControl;
static void _release_last_context(void*);
static void _add_sleep_event(void*);
struct ReadyToRunArgs {
bthread_tag_t tag;
TaskMeta* meta;
bool nosignal;
};
static void ready_to_run_in_worker(void*);
static void ready_to_run_in_worker_ignoresignal(void*);
static void priority_to_run(void*);

// Wait for a task to run.
// Returns true on success, false is treated as permanent error and the
Expand Down
2 changes: 1 addition & 1 deletion src/bthread/task_group_inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ inline void TaskGroup::exchange(TaskGroup** pg, TaskMeta* next_meta) {
if (g->is_current_pthread_task()) {
return g->ready_to_run(next_meta);
}
ReadyToRunArgs args = { g->_cur_meta, false };
ReadyToRunArgs args = { g->tag(), g->_cur_meta, false };
g->set_remained((g->current_task()->about_to_quit
? ready_to_run_in_worker_ignoresignal
: ready_to_run_in_worker),
Expand Down
5 changes: 5 additions & 0 deletions src/bthread/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static const bthread_attrflags_t BTHREAD_LOG_CONTEXT_SWITCH = 16;
static const bthread_attrflags_t BTHREAD_NOSIGNAL = 32;
static const bthread_attrflags_t BTHREAD_NEVER_QUIT = 64;
static const bthread_attrflags_t BTHREAD_INHERIT_SPAN = 128;
static const bthread_attrflags_t BTHREAD_GLOBAL_PRIORITY = 256;

// Key of thread-local data, created by bthread_key_create.
typedef struct {
Expand Down Expand Up @@ -137,6 +138,10 @@ static const bthread_attr_t BTHREAD_ATTR_NORMAL = {BTHREAD_STACKTYPE_NORMAL, 0,
static const bthread_attr_t BTHREAD_ATTR_LARGE = {BTHREAD_STACKTYPE_LARGE, 0, NULL,
BTHREAD_TAG_INVALID};

// epoll bthread
static const bthread_attr_t BTHREAD_ATTR_EPOLL = {
BTHREAD_STACKTYPE_NORMAL, BTHREAD_GLOBAL_PRIORITY, NULL, 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 = {
Expand Down
1 change: 0 additions & 1 deletion test/bthread_setconcurrency_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ int concurrency_by_tag(int num) {

TEST(BthreadTest, concurrency_by_tag) {
ASSERT_EQ(concurrency_by_tag(1), false);
auto tag_con = bthread_getconcurrency_by_tag(0);
auto con = bthread_getconcurrency();
ASSERT_EQ(concurrency_by_tag(con), true);
ASSERT_EQ(concurrency_by_tag(con + 1), true);
Expand Down
Loading