Skip to content

Commit 7134cac

Browse files
author
m30070657
committed
combine thread_affinity and cpu_set into one flag
1 parent 5f591c3 commit 7134cac

2 files changed

Lines changed: 10 additions & 32 deletions

File tree

src/bthread/task_control.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ DEFINE_int32(task_group_runqueue_capacity, 4096,
4242
DEFINE_int32(task_group_ntags, 1, "TaskGroup will be grouped by number ntags");
4343
DEFINE_bool(task_group_set_worker_name, true,
4444
"Whether to set the name of the worker thread");
45-
DEFINE_bool(thread_affinity, false, "Whether to Bind Cores");
4645
DEFINE_string(cpu_set, "",
4746
"Set of CPUs to which cores are bound, for example, 0-3,5,6-7; default: all");
4847

@@ -79,13 +78,6 @@ void run_tagged_worker_startfn(bthread_tag_t tag) {
7978
struct WorkerThreadArgs {
8079
WorkerThreadArgs(TaskControl* _c, bthread_tag_t _t) : c(_c), tag(_t) {}
8180

82-
WorkerThreadArgs* set_cpuId(unsigned _cpuId) {
83-
if (FLAGS_thread_affinity) {
84-
cpuId = _cpuId;
85-
}
86-
return this;
87-
}
88-
8981
TaskControl* c;
9082
bthread_tag_t tag;
9183
unsigned cpuId;
@@ -100,7 +92,7 @@ void* TaskControl::worker_thread(void* arg) {
10092
auto dummy = static_cast<WorkerThreadArgs*>(arg);
10193
auto c = dummy->c;
10294
auto tag = dummy->tag;
103-
if (FLAGS_thread_affinity) {
95+
if (!_cpus.empty()) {
10496
bind_thread(pthread_self(), _cpus[dummy->cpuId]);
10597
}
10698
delete dummy;
@@ -225,8 +217,8 @@ int TaskControl::init(int concurrency) {
225217
}
226218
_concurrency = concurrency;
227219

228-
if (FLAGS_thread_affinity) {
229-
if (parse_cpuset(FLAGS_cpu_set, _cpus) == -1 || _cpus.empty()) {
220+
if (!FLAGS_cpu_set.empty()) {
221+
if (parse_cpuset(FLAGS_cpu_set, _cpus) == -1) {
230222
LOG(ERROR) << "invalid cpuset=" << FLAGS_cpu_set;
231223
return -1;
232224
}
@@ -264,7 +256,9 @@ int TaskControl::init(int concurrency) {
264256
_workers.resize(_concurrency);
265257
for (int i = 0; i < _concurrency; ++i) {
266258
auto arg = new WorkerThreadArgs(this, i % FLAGS_task_group_ntags);
267-
arg->set_cpuId(i % _cpus.size());
259+
if (!_cpus.empty()) {
260+
arg->cpuId = i % _cpus.size();
261+
}
268262
const int rc = pthread_create(&_workers[i], NULL, worker_thread, arg);
269263
if (rc) {
270264
delete arg;
@@ -308,7 +302,9 @@ int TaskControl::add_workers(int num, bthread_tag_t tag) {
308302
// _concurrency before create a worker.
309303
_concurrency.fetch_add(1);
310304
auto arg = new WorkerThreadArgs(this, tag);
311-
arg->set_cpuId((i + old_concurency) % _cpus.size());
305+
if (!_cpus.empty()) {
306+
arg->cpuId = (i + old_concurency) % _cpus.size();
307+
}
312308
const int rc = pthread_create(
313309
&_workers[i + old_concurency], NULL, worker_thread, arg);
314310
if (rc) {
@@ -339,8 +335,7 @@ int TaskControl::parse_cpuset(std::string value, std::vector<unsigned>& cpus) {
339335
std::smatch match;
340336
std::set<unsigned> cpuset;
341337
if (value.empty()) {
342-
cpus = get_current_cpus();
343-
return 0;
338+
return -1;
344339
}
345340
if (std::regex_match(value, match, r)) {
346341
for (butil::StringSplitter split(value.data(), ','); split; ++split) {

src/bthread/task_control.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,23 +106,6 @@ friend bthread_t init_for_pthread_stack_trace();
106106
(void)r;
107107
}
108108

109-
static inline std::vector<unsigned> get_current_cpus() {
110-
cpu_set_t cs;
111-
auto r = pthread_getaffinity_np(pthread_self(), sizeof(cs), &cs);
112-
if (r != 0) {
113-
LOG(ERROR) << "get thread affinity failed";
114-
exit(1);
115-
}
116-
std::vector<unsigned> cpus;
117-
unsigned nr = CPU_COUNT(&cs);
118-
for (int cpu = 0; cpu < CPU_SETSIZE && cpus.size() < nr; cpu++) {
119-
if (CPU_ISSET(cpu, &cs)) {
120-
cpus.push_back(cpu);
121-
}
122-
}
123-
return cpus;
124-
}
125-
126109
#ifdef BRPC_BTHREAD_TRACER
127110
// A stacktrace of bthread can be helpful in debugging.
128111
void stack_trace(std::ostream& os, bthread_t tid);

0 commit comments

Comments
 (0)