Add pthread CPU affinity support#3122
Conversation
| DEFINE_bool(task_group_set_worker_name, true, | ||
| "Whether to set the name of the worker thread"); | ||
| DEFINE_bool(thread_affinity, false, "Whether to Bind Cores"); | ||
| DEFINE_string(cpu_set , "", |
There was a problem hiding this comment.
Maybe you can combine these two flags into one flag. If cpu_set is an empty string, then the thread affinity feature is off.
There was a problem hiding this comment.
Pull Request Overview
This PR adds pthread CPU affinity support to enable binding worker threads to specific CPU cores for better performance control. The implementation allows users to control thread-to-CPU binding through configuration flags.
Key Changes:
- Added two gflags:
thread_affinityto enable/disable CPU binding andcpu_setto specify target CPUs - Implemented CPU set parsing with support for range notation (e.g., "0-3,5,6-7")
- Modified worker thread creation to bind threads to CPUs in round-robin fashion when affinity is enabled
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/bthread/task_control.h | Added CPU affinity helper functions and static _cpus vector to store parsed CPU set |
| src/bthread/task_control.cpp | Implemented CPU set parsing, thread binding logic, and integrated affinity support into worker thread initialization |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
这个过程能放到startfn这些函数里面吗? |
@yanglimingcn |
|
动态增加线程会怎么设置呢? |
@yanglimingcn 无论如何增加,它都是存在_workers里的,所以只要保证_workers[i]对应_cpus[i % _cpus.size()] |
|
收到 |
3bd580f to
2169ba1
Compare
|
|
||
| static int parse_cpuset(std::string value, std::vector<unsigned>& cpus); | ||
|
|
||
| static inline void bind_thread(pthread_t pthread, unsigned cpuId) { |
|
有没有测试过绑核和不绑核的数据? |
不绑核 connection_type=single
[Latency] [Latency] [Latency] 绑核 connection_type=single
不绑核 connection_type=pooled
[Latency] [Latency] 绑核 connection_type=pooled
[Latency] [Latency] |
|
在single模式下收益没有pooled下面明显。 |
|
修复一下冲突吧 |
|
LGTM |
1 similar comment
|
LGTM |
What problem does this PR solve?
Issue Number:#1140
Problem Summary:
What is changed and the side effects?
Changed:
添加gflag
cpu_set:用户需要绑定的cpu集合,默认为空串,表示不开启绑核
当开启绑核时,会解析cpu集合到_cpus数组中
每次调用pthread_create(&_workers[i], NULL, worker_thread, arg);后,会绑定worker_id对应的_cpus[worker_id % _cpus.size()]
Side effects:
Performance effects:
Breaking backward compatibility:
Check List: