Apollo 10.0
自动驾驶开放平台
pin_thread.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <sched.h>
20#include <sys/resource.h>
21
22namespace apollo {
23namespace cyber {
24namespace scheduler {
25
26void ParseCpuset(const std::string& str, std::vector<int>* cpuset) {
27 std::vector<std::string> lines;
28 std::stringstream ss(str);
29 std::string l;
30 while (getline(ss, l, ',')) {
31 lines.push_back(l);
32 }
33 for (auto line : lines) {
34 std::stringstream ss(line);
35 std::vector<std::string> range;
36 while (getline(ss, l, '-')) {
37 range.push_back(l);
38 }
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++) {
43 cpuset->push_back(i);
44 }
45 } else {
46 ADEBUG << "Parsing cpuset format error.";
47 exit(0);
48 }
49 }
50}
51
52void SetSchedAffinity(std::thread* thread, const std::vector<int>& cpus,
53 const std::string& affinity, int cpu_id) {
54 cpu_set_t set;
55 CPU_ZERO(&set);
56
57 if (cpus.size()) {
58 if (!affinity.compare("range")) {
59 for (const auto cpu : cpus) {
60 CPU_SET(cpu, &set);
61 }
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()) {
66 return;
67 }
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";
71 }
72 }
73}
74
75void SetSchedPolicy(std::thread* thread, std::string spolicy,
76 int sched_priority, pid_t tid) {
77 struct sched_param sp;
78 int policy;
79
80 memset(reinterpret_cast<void*>(&sp), 0, sizeof(sp));
81 sp.sched_priority = sched_priority;
82
83 if (!spolicy.compare("SCHED_FIFO")) {
84 policy = 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")) {
88 policy = 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;
94 }
95}
96
97} // namespace scheduler
98} // namespace cyber
99} // namespace apollo
#define ADEBUG
Definition log.h:41
#define AINFO
Definition log.h:42
void SetSchedAffinity(std::thread *thread, const std::vector< int > &cpus, const std::string &affinity, int cpu_id)
Definition pin_thread.cc:52
void SetSchedPolicy(std::thread *thread, std::string spolicy, int sched_priority, pid_t tid)
Definition pin_thread.cc:75
void ParseCpuset(const std::string &str, std::vector< int > *cpuset)
Definition pin_thread.cc:26
class register implement
Definition arena_queue.h:37