Skip to content

Commit

Permalink
Merge pull request #901 from tier4/chore/cherry-pick-5191
Browse files Browse the repository at this point in the history
perf(system_monitor): fix program command line reading (autowarefoundation#5191)
  • Loading branch information
0x126 authored Oct 2, 2023
2 parents 8a9262c + 4a7cda7 commit 10a2376
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ class ProcessMonitor : public rclcpp::Node
*/
void getHighMemoryProcesses(const std::string & output);

/**
* @brief get command line from process id
* @param [in] pid process id
* @param [out] command output command line
* @return true if success to get command line name
*/
bool getCommandLineFromPiD(const std::string & pid, std::string * command);

/**
* @brief get top-rated processes
* @param [in] tasks list of diagnostics tasks for high load procs
Expand Down
24 changes: 22 additions & 2 deletions system/system_monitor/src/process_monitor/process_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,19 @@ void ProcessMonitor::getHighMemoryProcesses(const std::string & output)
getTopratedProcesses(&memory_tasks_, &p2);
}

bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string * command)
{
std::string commandLineFilePath = "/proc/" + pid + "/cmdline";
std::ifstream commandFile(commandLineFilePath);
if (commandFile.is_open()) {
std::getline(commandFile, *command);
commandFile.close();
return true;
} else {
return false;
}
}

void ProcessMonitor::getTopratedProcesses(
std::vector<std::shared_ptr<DiagTask>> * tasks, bp::pipe * p)
{
Expand Down Expand Up @@ -462,7 +475,14 @@ void ProcessMonitor::getTopratedProcesses(
info.virtualImage >> info.residentSize >> info.sharedMemSize >> info.processStatus >>
info.cpuUsage >> info.memoryUsage >> info.cpuTime;

std::getline(stream, info.commandName);
std::string program_name;
std::getline(stream, program_name);

bool flag_find_command_line = getCommandLineFromPiD(info.processId, &info.commandName);

if (!flag_find_command_line) {
info.commandName = program_name; // if command line is not found, use program name instead
}

tasks->at(index)->setDiagnosticsStatus(DiagStatus::OK, "OK");
tasks->at(index)->setProcessInformation(info);
Expand Down Expand Up @@ -515,7 +535,7 @@ void ProcessMonitor::onTimer()
std::ostringstream os;

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

if (c.exit_code() != 0) {
Expand Down

0 comments on commit 10a2376

Please sign in to comment.