Skip to content

Commit

Permalink
fix(system_monitor): output command line (autowarefoundation#5430)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and tkimura4 committed Nov 1, 2023
1 parent d06733f commit 4354856
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class ProcessMonitor : public rclcpp::Node
* @param [out] command output command line
* @return true if success to get command line name
*/
bool getCommandLineFromPiD(const std::string & pid, std::string * command);
bool getCommandLineFromPiD(const std::string & pid, std::string & command);

/**
* @brief get top-rated processes
Expand Down
18 changes: 13 additions & 5 deletions system/system_monitor/src/process_monitor/process_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,22 @@ void ProcessMonitor::getHighMemoryProcesses(const std::string & output)
getTopratedProcesses(&memory_tasks_, &p2);
}

bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string * command)
bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string & command)
{
std::string commandLineFilePath = "/proc/" + pid + "/cmdline";
std::ifstream commandFile(commandLineFilePath);
std::ifstream commandFile(commandLineFilePath, std::ios::in | std::ios::binary);

if (commandFile.is_open()) {
std::getline(commandFile, *command);
std::vector<uint8_t> buffer;
std::copy(
std::istream_iterator<uint8_t>(commandFile), std::istream_iterator<uint8_t>(),
std::back_inserter(buffer));
commandFile.close();
return true;
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;
}
Expand Down Expand Up @@ -478,7 +486,7 @@ void ProcessMonitor::getTopratedProcesses(
std::string program_name;
std::getline(stream, program_name);

bool flag_find_command_line = getCommandLineFromPiD(info.processId, &info.commandName);
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
Expand Down

0 comments on commit 4354856

Please sign in to comment.