Skip to content

Commit 861c01e

Browse files
h-ohtaOwen-LiuyuxuanOwen-Liuyuxuanpre-commit-ci[bot]takeshi-iwanari
authored
fix(system_monitor): fix program command line reading (backport autowarefoundation#5191, autowarefoundation#5430) (#995)
* perf(system_monitor): fix program command line reading (autowarefoundation#5191) * Fix program command line reading Signed-off-by: Owen-Liuyuxuan <uken.ryu@tier4.jp> * style(pre-commit): autofix * fix spelling commandline->command_line Signed-off-by: Owen-Liuyuxuan <uken.ryu@tier4.jp> --------- Signed-off-by: Owen-Liuyuxuan <uken.ryu@tier4.jp> Co-authored-by: Owen-Liuyuxuan <uken.ryu@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * fix(system_monitor): output command line (autowarefoundation#5430) * fix(system_monitor): output command line Signed-off-by: takeshi.iwanari <takeshi.iwanari@tier4.jp> * style(pre-commit): autofix --------- Signed-off-by: takeshi.iwanari <takeshi.iwanari@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --------- Signed-off-by: Owen-Liuyuxuan <uken.ryu@tier4.jp> Signed-off-by: takeshi.iwanari <takeshi.iwanari@tier4.jp> Co-authored-by: Yuxuan Liu <619684051@qq.com> Co-authored-by: Owen-Liuyuxuan <uken.ryu@tier4.jp> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: takeshi-iwanari <takeshi.iwanari@tier4.jp> Co-authored-by: Akihisa Nagata <54956813+asa-naki@users.noreply.github.com>
1 parent a2cbf8c commit 861c01e

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

system/system_monitor/include/system_monitor/process_monitor/process_monitor.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ class ProcessMonitor : public rclcpp::Node
8888
*/
8989
void getHighMemoryProcesses(const std::string & output);
9090

91+
/**
92+
* @brief get command line from process id
93+
* @param [in] pid process id
94+
* @param [out] command output command line
95+
* @return true if success to get command line name
96+
*/
97+
bool getCommandLineFromPiD(const std::string & pid, std::string & command);
98+
9199
/**
92100
* @brief get top-rated processes
93101
* @param [in] tasks list of diagnostics tasks for high load procs

system/system_monitor/src/process_monitor/process_monitor.cpp

+30-2
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,27 @@ void ProcessMonitor::getHighMemoryProcesses(const std::string & output)
414414
getTopratedProcesses(&memory_tasks_, &p2);
415415
}
416416

417+
bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string & command)
418+
{
419+
std::string commandLineFilePath = "/proc/" + pid + "/cmdline";
420+
std::ifstream commandFile(commandLineFilePath, std::ios::in | std::ios::binary);
421+
422+
if (commandFile.is_open()) {
423+
std::vector<uint8_t> buffer;
424+
std::copy(
425+
std::istream_iterator<uint8_t>(commandFile), std::istream_iterator<uint8_t>(),
426+
std::back_inserter(buffer));
427+
commandFile.close();
428+
std::replace(
429+
buffer.begin(), buffer.end(), '\0',
430+
' '); // 0x00 is used as delimiter in /cmdline instead of 0x20 (space)
431+
command = std::string(buffer.begin(), buffer.end());
432+
return (buffer.size() > 0) ? true : false; // cmdline is empty if it is kernel process
433+
} else {
434+
return false;
435+
}
436+
}
437+
417438
void ProcessMonitor::getTopratedProcesses(
418439
std::vector<std::shared_ptr<DiagTask>> * tasks, bp::pipe * p)
419440
{
@@ -462,7 +483,14 @@ void ProcessMonitor::getTopratedProcesses(
462483
info.virtualImage >> info.residentSize >> info.sharedMemSize >> info.processStatus >>
463484
info.cpuUsage >> info.memoryUsage >> info.cpuTime;
464485

465-
std::getline(stream, info.commandName);
486+
std::string program_name;
487+
std::getline(stream, program_name);
488+
489+
bool flag_find_command_line = getCommandLineFromPiD(info.processId, info.commandName);
490+
491+
if (!flag_find_command_line) {
492+
info.commandName = program_name; // if command line is not found, use program name instead
493+
}
466494

467495
tasks->at(index)->setDiagnosticsStatus(DiagStatus::OK, "OK");
468496
tasks->at(index)->setProcessInformation(info);
@@ -515,7 +543,7 @@ void ProcessMonitor::onTimer()
515543
std::ostringstream os;
516544

517545
// Get processes
518-
bp::child c("top -bcn1 -o %CPU -w 256", bp::std_out > is_out, bp::std_err > is_err);
546+
bp::child c("top -bn1 -o %CPU -w 128", bp::std_out > is_out, bp::std_err > is_err);
519547
c.wait();
520548

521549
if (c.exit_code() != 0) {

0 commit comments

Comments
 (0)