提交 0d718044 编写于 作者: A Aaron Xiao 提交者: GitHub

Monitor: Match process by keywords instead of binary name, to support GPS and velodyne. (#1053)

上级 b8a3d6bb
......@@ -19,11 +19,24 @@
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <fstream>
namespace apollo {
namespace common {
namespace util {
bool GetContent(const std::string &file_name, std::string *content) {
std::ifstream fin(file_name);
if (!fin) {
return false;
}
std::stringstream str_stream;
str_stream << fin.rdbuf();
*content = str_stream.str();
return true;
}
bool PathExists(const std::string &path) {
struct stat info;
return stat(path.c_str(), &info) == 0;
......
......@@ -170,6 +170,14 @@ bool GetProtoFromFile(const std::string &file_name, MessageType *message) {
return true;
}
/**
* @brief Get file content as string.
* @param file_name The name of the file to read content.
* @param content The file content.
* @return If the action is successful.
*/
bool GetContent(const std::string &file_name, std::string *content);
/**
* @brief Check if the path exists.
* @return If the path exists.
......
......@@ -142,19 +142,11 @@ hardware {
key: "GPS"
value: {
display_name: "GPS"
supported_commands {
key: "health_check"
value: "scripts/hw_check.sh gps"
}
}
}
hardware {
key: "ESD_CAN-0"
key: "CAN"
value: {
display_name: "CAN"
supported_commands {
key: "health_check"
value: "scripts/hw_check.sh can"
}
}
}
modules {
name: "canbus"
binary_path: "modules/canbus/canbus"
process_cmd_keywords: "modules/canbus/canbus"
}
modules {
name: "GPS"
process_cmd_keywords: "gnss_nodelet_manager"
}
modules {
name: "velodyne"
process_cmd_keywords: "velodyne_nodelet_manager"
}
modules {
name: "localization"
binary_path: "modules/localization/localization"
process_cmd_keywords: "modules/localization/localization"
}
modules {
name: "perception"
binary_path: "modules/perception/perception"
process_cmd_keywords: "modules/perception/perception"
}
modules {
name: "prediction"
binary_path: "modules/prediction/prediction"
process_cmd_keywords: "modules/prediction/prediction"
}
modules {
name: "routing"
binary_path: "modules/routing/routing"
process_cmd_keywords: "modules/routing/routing"
}
modules {
name: "planning"
binary_path: "modules/planning/planning"
process_cmd_keywords: "modules/planning/planning"
}
modules {
name: "control"
binary_path: "modules/control/control"
process_cmd_keywords: "modules/control/control"
}
......@@ -9,7 +9,7 @@ message ModuleMonitorConf {
optional string name = 1;
// For process monitor.
optional string binary_path = 2;
repeated string process_cmd_keywords = 2;
// TODO(xiaoxq): For message monitor, which hasn't been implemented.
optional apollo.common.adapter.AdapterConfig.MessageType msg_type = 3;
......
......@@ -24,7 +24,7 @@
DEFINE_string(process_monitor_name, "ProcessMonitor",
"Name of the process monitor.");
DEFINE_double(process_monitor_interval, 3,
DEFINE_double(process_monitor_interval, 1.5,
"Process status checking interval (s).");
DEFINE_string(module_monitor_conf_path,
......@@ -33,23 +33,9 @@ DEFINE_string(module_monitor_conf_path,
namespace apollo {
namespace monitor {
namespace {
using apollo::common::util::GetProtoFromFile;
std::string TranslateSymbolicLink(const std::string &origin) {
constexpr int BUF_SIZE = 512;
char buf[BUF_SIZE];
int count = readlink(origin.c_str(), buf, BUF_SIZE);
if (count < 0) {
count = 0;
}
buf[count] = '\0';
return buf;
}
} // namespace
ProcessMonitor::ProcessMonitor(SystemStatus *system_status)
: RecurrentRunner(FLAGS_process_monitor_name,
FLAGS_process_monitor_interval)
......@@ -59,7 +45,7 @@ ProcessMonitor::ProcessMonitor(SystemStatus *system_status)
// Init module status if it has set binary to check.
for (const auto &module_conf : config_.modules()) {
if (module_conf.has_binary_path()) {
if (!module_conf.process_cmd_keywords().empty()) {
status_->insert({module_conf.name(), {}});
}
}
......@@ -68,22 +54,30 @@ ProcessMonitor::ProcessMonitor(SystemStatus *system_status)
void ProcessMonitor::RunOnce(const double current_time) {
// Set all processes as not-running by default.
for (const auto &module_conf : config_.modules()) {
if (module_conf.has_binary_path()) {
if (!module_conf.process_cmd_keywords().empty()) {
(*status_)[module_conf.name()].set_process_running(false);
}
}
const auto procs = common::util::ListSubDirectories("/proc");
for (const auto &proc : procs) {
const auto binary_link = common::util::StrCat("/proc/", proc, "/exe");
const auto binary_path = TranslateSymbolicLink(binary_link);
if (binary_path.empty()) {
// Get process command string.
std::string cmd_string;
const auto cmd_file = common::util::StrCat("/proc/", proc, "/cmdline");
if (!common::util::GetContent(cmd_file, &cmd_string)) {
continue;
}
for (const auto &module_conf : config_.modules()) {
// Find the bounded module.
if (module_conf.has_binary_path() &&
common::util::EndWith(binary_path, module_conf.binary_path())) {
const auto &keywords = module_conf.process_cmd_keywords();
// Check if the command string contains all keywords of this module.
const bool keywords_matched = !keywords.empty() && std::find_if(
keywords.begin(), keywords.end(),
[cmd_string](const std::string &keyword)->bool {
return cmd_string.find(keyword) == std::string::npos;
}) == keywords.end();
if (keywords_matched) {
(*status_)[module_conf.name()].set_process_running(true);
ADEBUG << "Found " << module_conf.name() << " module "
"is running on process " << proc;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册