Skip to content

Commit 89404e6

Browse files
Owen-LiuyuxuanOwen-Liuyuxuanpre-commit-ci[bot]
authored
perf(system_monitor): fix program command line reading (#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>
1 parent 93b6118 commit 89404e6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-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

+22-2
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,19 @@ 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);
421+
if (commandFile.is_open()) {
422+
std::getline(commandFile, *command);
423+
commandFile.close();
424+
return true;
425+
} else {
426+
return false;
427+
}
428+
}
429+
417430
void ProcessMonitor::getTopratedProcesses(
418431
std::vector<std::shared_ptr<DiagTask>> * tasks, bp::pipe * p)
419432
{
@@ -462,7 +475,14 @@ void ProcessMonitor::getTopratedProcesses(
462475
info.virtualImage >> info.residentSize >> info.sharedMemSize >> info.processStatus >>
463476
info.cpuUsage >> info.memoryUsage >> info.cpuTime;
464477

465-
std::getline(stream, info.commandName);
478+
std::string program_name;
479+
std::getline(stream, program_name);
480+
481+
bool flag_find_command_line = getCommandLineFromPiD(info.processId, &info.commandName);
482+
483+
if (!flag_find_command_line) {
484+
info.commandName = program_name; // if command line is not found, use program name instead
485+
}
466486

467487
tasks->at(index)->setDiagnosticsStatus(DiagStatus::OK, "OK");
468488
tasks->at(index)->setProcessInformation(info);
@@ -515,7 +535,7 @@ void ProcessMonitor::onTimer()
515535
std::ostringstream os;
516536

517537
// Get processes
518-
bp::child c("top -bcn1 -o %CPU -w 256", bp::std_out > is_out, bp::std_err > is_err);
538+
bp::child c("top -bn1 -o %CPU -w 128", bp::std_out > is_out, bp::std_err > is_err);
519539
c.wait();
520540

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

0 commit comments

Comments
 (0)