brpc icon indicating copy to clipboard operation
brpc copied to clipboard

Why switch bthread cost so much time? (2us), both libgo and golang coroutine switch cost ~200ns?

Open jiangdongzi opened this issue 2 years ago • 1 comments

They also support work stealing.

jiangdongzi avatar Jul 26 '22 09:07 jiangdongzi

The work stealing in libgo is implemented by a global scheduler: https://github.com/yyzybb537/libgo/blob/master/libgo/scheduler/scheduler.cpp#L340 It runs work stealing at an interval of 1000us by default. When a task is ready to run but current worker is busy, it needs at most 1000us to be stolen by an idle worker.

In bthread, when a task is ready to run, futex_wake is called to wakeup an idle worker at once. This avoids long-tail latency. But the cost is that most task switch need futex. It needs about 2us from one thread calls futex_wake and another thread resumes from futex_wait.

wwbmmm avatar Aug 01 '22 11:08 wwbmmm