Skip to content

Commit

Permalink
fix(system_monitor): fix program command line reading (backport autow…
Browse files Browse the repository at this point in the history
…arefoundation#5191, autowarefoundation#5430) (#995)

* perf(system_monitor): fix program command line reading (autowarefoundation#5191)

* Fix program command line reading

Signed-off-by: Owen-Liuyuxuan <[email protected]>

* style(pre-commit): autofix

* fix spelling commandline->command_line

Signed-off-by: Owen-Liuyuxuan <[email protected]>

---------

Signed-off-by: Owen-Liuyuxuan <[email protected]>
Co-authored-by: Owen-Liuyuxuan <[email protected]>
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 <[email protected]>

* style(pre-commit): autofix

---------

Signed-off-by: takeshi.iwanari <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

---------

Signed-off-by: Owen-Liuyuxuan <[email protected]>
Signed-off-by: takeshi.iwanari <[email protected]>
Co-authored-by: Yuxuan Liu <[email protected]>
Co-authored-by: Owen-Liuyuxuan <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: takeshi-iwanari <[email protected]>
Co-authored-by: Akihisa Nagata <[email protected]>
  • Loading branch information
6 people committed Aug 14, 2024
1 parent ff7c1e5 commit 69ee630
Show file tree
Hide file tree
Showing 2 changed files with 38 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
32 changes: 30 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,27 @@ 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, std::ios::in | std::ios::binary);

if (commandFile.is_open()) {
std::vector<uint8_t> buffer;
std::copy(
std::istream_iterator<uint8_t>(commandFile), std::istream_iterator<uint8_t>(),
std::back_inserter(buffer));
commandFile.close();
std::replace(
buffer.begin(), buffer.end(), '\0',
' '); // 0x00 is used as delimiter in /cmdline instead of 0x20 (space)
command = std::string(buffer.begin(), buffer.end());
return (buffer.size() > 0) ? true : false; // cmdline is empty if it is kernel process
} else {
return false;
}
}

void ProcessMonitor::getTopratedProcesses(
std::vector<std::shared_ptr<DiagTask>> * tasks, bp::pipe * p)
{
Expand Down Expand Up @@ -462,7 +483,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 +543,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 69ee630

Please sign in to comment.