26void ParseCpuset(
const std::string& str, std::vector<int>* cpuset) {
27 std::vector<std::string> lines;
28 std::stringstream ss(str);
30 while (getline(ss, l,
',')) {
33 for (
auto line : lines) {
34 std::stringstream ss(line);
35 std::vector<std::string> range;
36 while (getline(ss, l,
'-')) {
39 if (range.size() == 1) {
40 cpuset->push_back(std::stoi(range[0]));
41 }
else if (range.size() == 2) {
42 for (
int i = std::stoi(range[0]), e = std::stoi(range[1]); i <= e; i++) {
46 ADEBUG <<
"Parsing cpuset format error.";
53 const std::string& affinity,
int cpu_id) {
58 if (!affinity.compare(
"range")) {
59 for (
const auto cpu : cpus) {
62 pthread_setaffinity_np(thread->native_handle(),
sizeof(set), &set);
63 AINFO <<
"thread " << thread->get_id() <<
" set range affinity";
64 }
else if (!affinity.compare(
"1to1")) {
65 if (cpu_id == -1 || (uint32_t)cpu_id >= cpus.size()) {
68 CPU_SET(cpus[cpu_id], &set);
69 pthread_setaffinity_np(thread->native_handle(),
sizeof(set), &set);
70 AINFO <<
"thread " << thread->get_id() <<
" set 1to1 affinity";
76 int sched_priority, pid_t tid) {
77 struct sched_param sp;
80 memset(
reinterpret_cast<void*
>(&sp), 0,
sizeof(sp));
81 sp.sched_priority = sched_priority;
83 if (!spolicy.compare(
"SCHED_FIFO")) {
85 pthread_setschedparam(thread->native_handle(), policy, &sp);
86 AINFO <<
"thread " << tid <<
" set sched_policy: " << spolicy;
87 }
else if (!spolicy.compare(
"SCHED_RR")) {
89 pthread_setschedparam(thread->native_handle(), policy, &sp);
90 AINFO <<
"thread " << tid <<
" set sched_policy: " << spolicy;
91 }
else if (!spolicy.compare(
"SCHED_OTHER")) {
92 setpriority(PRIO_PROCESS, tid, sched_priority);
93 AINFO <<
"thread " << tid <<
" set sched_policy: " << spolicy;