18
18
*/
19
19
20
20
#include " cgroup_setter.hpp"
21
- #include < iostream>
22
- #include < fstream>
23
- #include < sstream>
24
- #include < cstring>
25
- #include < unistd.h>
21
+
26
22
#include < boost/process.hpp>
23
+
24
+ #include < unistd.h>
27
25
#include < yaml-cpp/yaml.h>
28
26
27
+ #include < cstring>
28
+ #include < fstream>
29
+ #include < iostream>
30
+ #include < sstream>
31
+
29
32
namespace bp = boost::process;
30
33
31
34
CgroupSetter::CgroupSetter (const rclcpp::NodeOptions & options)
32
- : Node(" cgroup_setter_node" , options),
33
- updater_(this )
35
+ : Node(" cgroup_setter_node" , options), updater_(this )
34
36
{
35
37
try {
36
38
std::string yaml_path = this ->declare_parameter <std::string>(" cgroup_setting_config_path" );
37
39
YAML::Node config = YAML::LoadFile (yaml_path);
38
40
if (config[" base_path" ]) {
39
- base_path_ = config[" base_path" ].as <std::string>();
41
+ base_path_ = config[" base_path" ].as <std::string>();
40
42
} else {
41
- RCLCPP_ERROR (this ->get_logger (), " base_path is not set in the config file." );
42
- return ;
43
+ RCLCPP_ERROR (this ->get_logger (), " base_path is not set in the config file." );
44
+ return ;
43
45
}
44
46
45
47
if (!config[" settings" ]) {
46
- RCLCPP_ERROR (this ->get_logger (), " settings is not set in the config file." );
47
- return ;
48
+ RCLCPP_ERROR (this ->get_logger (), " settings is not set in the config file." );
49
+ return ;
48
50
}
49
-
51
+
50
52
for (auto setting : config[" settings" ]) {
51
- if (!setting[" directory" ] || !setting[" search_word" ]) {
53
+ if (!setting[" directory" ] || !setting[" search_word" ]) {
52
54
RCLCPP_ERROR (this ->get_logger (), " directory or search_word is not set in the config file." );
53
55
return ;
54
- }
56
+ }
55
57
56
- for (auto word : setting[" search_word" ]) {
57
- std::pair<std::string, std::string> tmp_pair = std::make_pair (setting[" directory" ].as <std::string>(), word.as <std::string>());
58
+ for (auto word : setting[" search_word" ]) {
59
+ std::pair<std::string, std::string> tmp_pair =
60
+ std::make_pair (setting[" directory" ].as <std::string>(), word.as <std::string>());
58
61
cgroup_map_[tmp_pair] = false ;
59
- }
62
+ }
60
63
}
61
- } catch (const std::exception & e) {
64
+ } catch (const std::exception & e) {
62
65
RCLCPP_ERROR (this ->get_logger (), " Failed to load the config file." );
63
66
return ;
64
67
}
@@ -73,58 +76,69 @@ CgroupSetter::CgroupSetter(const rclcpp::NodeOptions & options)
73
76
this , this ->get_clock (), 1s, std::bind (&CgroupSetter::checkProcessAndAddToCgroup, this ));
74
77
}
75
78
76
- void CgroupSetter::checkCgroup (diagnostic_updater::DiagnosticStatusWrapper & stat) {
77
- bool allOK = true ;
78
- for (auto & entry : cgroup_map_) {
79
- if (entry.second ) {
80
- stat.add (entry.first .first + " " + entry.first .second , " OK" );
81
- } else {
82
- allOK = false ;
83
- stat.add (entry.first .first + " " + entry.first .second , " NG" );
84
- }
85
- }
86
- if (allOK) {
87
- timer_->cancel ();
88
- stat.summary (diagnostic_msgs::msg::DiagnosticStatus::OK, " All processes are added to cgroup." );
79
+ void CgroupSetter::checkCgroup (diagnostic_updater::DiagnosticStatusWrapper & stat)
80
+ {
81
+ bool allOK = true ;
82
+ for (auto & entry : cgroup_map_) {
83
+ if (entry.second ) {
84
+ stat.add (entry.first .first + " " + entry.first .second , " OK" );
89
85
} else {
90
- stat.summary (diagnostic_msgs::msg::DiagnosticStatus::WARN, " Some processes are not added to cgroup." );
86
+ allOK = false ;
87
+ stat.add (entry.first .first + " " + entry.first .second , " NG" );
91
88
}
89
+ }
90
+ if (allOK) {
91
+ timer_->cancel ();
92
+ stat.summary (diagnostic_msgs::msg::DiagnosticStatus::OK, " All processes are added to cgroup." );
93
+ } else {
94
+ stat.summary (
95
+ diagnostic_msgs::msg::DiagnosticStatus::WARN, " Some processes are not added to cgroup." );
96
+ }
92
97
}
93
98
94
- void CgroupSetter::checkProcessAndAddToCgroup () {
95
- for (auto & entry : cgroup_map_) {
96
- if (entry.second ) {
97
- continue ;
98
- }
99
- std::string word = entry.first .second ;
100
- std::string result = executeCommand (word);
101
- if (!result.empty ()) {
102
- std::istringstream iss (result);
103
- std::string pid;
104
- bool allAdded = true ;
105
- while (std::getline (iss, pid, ' \n ' )) {
106
- if (!pid.empty () && addToCgroup (entry.first .first , pid)) {
107
- if (checkPIDExists (base_path_ + " /" + entry.first .first + " /cgroup.procs" , pid)) {
108
- RCLCPP_INFO (this ->get_logger (), " Added all PIDs to cgroup. %s %s" , entry.first .second .c_str (), pid.c_str ());
109
- } else {
110
- allAdded = false ;
111
- RCLCPP_ERROR (this ->get_logger (), " Failed to add PID %s to cgroup. %s %s" , pid.c_str (), entry.first .second .c_str (), result.c_str ());
112
- }
113
- } else {
114
- allAdded = false ;
115
- RCLCPP_ERROR (this ->get_logger (), " Failed to add PID %s to cgroup. %s %s" , pid.c_str (), entry.first .second .c_str (), result.c_str ());
116
- }
117
- }
118
- if (allAdded) {
119
- entry.second = true ;
120
- }
99
+ void CgroupSetter::checkProcessAndAddToCgroup ()
100
+ {
101
+ for (auto & entry : cgroup_map_) {
102
+ if (entry.second ) {
103
+ continue ;
104
+ }
105
+ std::string word = entry.first .second ;
106
+ std::string result = executeCommand (word);
107
+ if (!result.empty ()) {
108
+ std::istringstream iss (result);
109
+ std::string pid;
110
+ bool allAdded = true ;
111
+ while (std::getline (iss, pid, ' \n ' )) {
112
+ if (!pid.empty () && addToCgroup (entry.first .first , pid)) {
113
+ if (checkPIDExists (base_path_ + " /" + entry.first .first + " /cgroup.procs" , pid)) {
114
+ RCLCPP_INFO (
115
+ this ->get_logger (), " Added all PIDs to cgroup. %s %s" , entry.first .second .c_str (),
116
+ pid.c_str ());
117
+ } else {
118
+ allAdded = false ;
119
+ RCLCPP_ERROR (
120
+ this ->get_logger (), " Failed to add PID %s to cgroup. %s %s" , pid.c_str (),
121
+ entry.first .second .c_str (), result.c_str ());
122
+ }
121
123
} else {
122
- RCLCPP_ERROR (this ->get_logger (), " Failed to get PID. %s %s" , entry.first .second .c_str (), result.c_str ());
124
+ allAdded = false ;
125
+ RCLCPP_ERROR (
126
+ this ->get_logger (), " Failed to add PID %s to cgroup. %s %s" , pid.c_str (),
127
+ entry.first .second .c_str (), result.c_str ());
123
128
}
129
+ }
130
+ if (allAdded) {
131
+ entry.second = true ;
132
+ }
133
+ } else {
134
+ RCLCPP_ERROR (
135
+ this ->get_logger (), " Failed to get PID. %s %s" , entry.first .second .c_str (), result.c_str ());
124
136
}
137
+ }
125
138
}
126
139
127
- std::string CgroupSetter::executeCommand (const std::string& search_word) {
140
+ std::string CgroupSetter::executeCommand (const std::string & search_word)
141
+ {
128
142
int out_fd[2 ];
129
143
if (pipe2 (out_fd, O_CLOEXEC) != 0 ) {
130
144
RCLCPP_ERROR (this ->get_logger (), " pipe2 error" );
@@ -140,11 +154,11 @@ std::string CgroupSetter::executeCommand(const std::string& search_word) {
140
154
}
141
155
bp::pipe err_pipe{err_fd[0 ], err_fd[1 ]};
142
156
bp::ipstream is_err{std::move (err_pipe)};
143
- auto cmd= bp::search_path (" pgrep" );
157
+ auto cmd = bp::search_path (" pgrep" );
144
158
std::vector<std::string> args;
145
159
args.push_back (" -f" );
146
160
args.push_back (search_word);
147
- bp::child c (cmd, bp::args= args, bp::std_out > is_out, bp::std_err > is_err);
161
+ bp::child c (cmd, bp::args = args, bp::std_out > is_out, bp::std_err > is_err);
148
162
c.wait ();
149
163
if (c.exit_code () != 0 ) {
150
164
std::ostringstream os;
@@ -159,47 +173,50 @@ std::string CgroupSetter::executeCommand(const std::string& search_word) {
159
173
}
160
174
}
161
175
162
- bool CgroupSetter::checkPIDExists (const std::string& filePath, const std::string & pid) {
163
- std::ifstream file (filePath);
164
- if (!file.is_open ()) {
165
- RCLCPP_ERROR (this ->get_logger (), " Failed to open %s" , filePath.c_str ());
166
- return false ;
167
- }
176
+ bool CgroupSetter::checkPIDExists (const std::string & filePath, const std::string & pid)
177
+ {
178
+ std::ifstream file (filePath);
179
+ if (!file.is_open ()) {
180
+ RCLCPP_ERROR (this ->get_logger (), " Failed to open %s" , filePath.c_str ());
181
+ return false ;
182
+ }
168
183
169
- std::string line;
170
- while (std::getline (file, line)) {
171
- if (line == pid) {
172
- return true ;
173
- }
184
+ std::string line;
185
+ while (std::getline (file, line)) {
186
+ if (line == pid) {
187
+ return true ;
174
188
}
175
- return false ;
189
+ }
190
+ return false ;
176
191
}
177
192
178
- bool CgroupSetter::addToCgroup (const std::string& cgroupPath, const std::string& pid) {
179
- std::string cgroupProcFile = base_path_ + " /" + cgroupPath + " /cgroup.procs" ;
180
- std::ofstream ofs (cgroupProcFile, std::ofstream::app);
181
- if (!ofs) {
182
- std::cerr << " Failed to open " << cgroupProcFile << std::endl;
183
- ofs.close ();
184
- return false ;
185
- }
186
- ofs << pid;
187
- if (!ofs) {
188
- std::cerr << " Failed to write to " << cgroupProcFile << std::endl;
189
- ofs.close ();
190
- return false ;
191
- }
193
+ bool CgroupSetter::addToCgroup (const std::string & cgroupPath, const std::string & pid)
194
+ {
195
+ std::string cgroupProcFile = base_path_ + " /" + cgroupPath + " /cgroup.procs" ;
196
+ std::ofstream ofs (cgroupProcFile, std::ofstream::app);
197
+ if (!ofs) {
198
+ std::cerr << " Failed to open " << cgroupProcFile << std::endl;
199
+ ofs.close ();
200
+ return false ;
201
+ }
202
+ ofs << pid;
203
+ if (!ofs) {
204
+ std::cerr << " Failed to write to " << cgroupProcFile << std::endl;
192
205
ofs.close ();
193
- return true ;
206
+ return false ;
207
+ }
208
+ ofs.close ();
209
+ return true ;
194
210
}
195
211
196
- int main (int argc, char ** argv) {
197
- rclcpp::init (argc, argv);
198
- rclcpp::executors::SingleThreadedExecutor executor;
199
- auto options = rclcpp::NodeOptions ();
200
- auto node = std::make_shared<CgroupSetter>(options);
201
- executor.add_node (node);
202
- executor.spin ();
203
- executor.remove_node (node);
204
- rclcpp::shutdown ();
205
- }
212
+ int main (int argc, char ** argv)
213
+ {
214
+ rclcpp::init (argc, argv);
215
+ rclcpp::executors::SingleThreadedExecutor executor;
216
+ auto options = rclcpp::NodeOptions ();
217
+ auto node = std::make_shared<CgroupSetter>(options);
218
+ executor.add_node (node);
219
+ executor.spin ();
220
+ executor.remove_node (node);
221
+ rclcpp::shutdown ();
222
+ }
0 commit comments