Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(system_monitor): output command line #5430

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,4 @@
// Copyright 2020 Autoware Foundation

Check notice on line 1 in system/system_monitor/src/process_monitor/process_monitor.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 5.70 to 5.80, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -414,14 +414,22 @@
getTopratedProcesses(&memory_tasks_, &p2);
}

bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string * command)
bool ProcessMonitor::getCommandLineFromPiD(const std::string & pid, std::string & command)

Check warning on line 417 in system/system_monitor/src/process_monitor/process_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/process_monitor/process_monitor.cpp#L417

Added line #L417 was not covered by tests
{
std::string commandLineFilePath = "/proc/" + pid + "/cmdline";
std::ifstream commandFile(commandLineFilePath);
std::ifstream commandFile(commandLineFilePath, std::ios::in | std::ios::binary);

Check warning on line 420 in system/system_monitor/src/process_monitor/process_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/process_monitor/process_monitor.cpp#L420

Added line #L420 was not covered by tests

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>(),

Check warning on line 425 in system/system_monitor/src/process_monitor/process_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/process_monitor/process_monitor.cpp#L424-L425

Added lines #L424 - L425 were not covered by tests
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

Check warning on line 432 in system/system_monitor/src/process_monitor/process_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/process_monitor/process_monitor.cpp#L431-L432

Added lines #L431 - L432 were not covered by tests
} else {
return false;
}
Expand Down Expand Up @@ -478,7 +486,7 @@
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);

Check warning on line 489 in system/system_monitor/src/process_monitor/process_monitor.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/src/process_monitor/process_monitor.cpp#L489

Added line #L489 was not covered by tests

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