24namespace process_statistic {
29 if (!ComponentBase::GetProtoConfig(&conf)) {
30 AERROR <<
"Unable to load process statistic conf file: " << ComponentBase::ConfigFilePath();
33 AINFO <<
"read conf success.";
41 if (conf.has_general_process()) {
42 for (
auto& regex_str : conf.general_process().regex()) {
44 general_process_regex_list_.emplace_back(regex_str);
45 }
catch (std::regex_error& e) {
46 AERROR <<
"error general process conf: " << regex_str <<
", regex_error caught: " << e.what();
53 auto metrics = std::make_shared<ProcessMetrics>();
54 apollo::common::util::FillHeader(
node_->Name(), metrics.get());
58 std::string cmd_string;
62 std::replace(cmd_string.begin(), cmd_string.end(),
'\0',
' ');
64 if (ParseFromCache(cmd_string, metrics)) {
67 ParseCmd(cmd_string, metrics, cache_.at(cmd_string));
71 metric_writer_->Write(metrics);
81bool ProcessStatisticComponent::ParseFromCache(
const std::string& cmd_string, std::shared_ptr<ProcessMetrics> metrics) {
83 if (cache_.find(cmd_string) == cache_.end()) {
84 std::shared_ptr<CacheMetric> cache_metric = std::make_shared<CacheMetric>();
85 cache_metric->timestamp = now;
86 cache_[cmd_string] = std::move(cache_metric);
89 auto& cache_metric = cache_[cmd_string];
90 cache_metric->timestamp = now;
91 for (
const auto& dag : cache_metric->mainboard_dags) {
92 metrics->mutable_mainboard_dag()->add_name(dag);
94 if (!cache_metric->mainboard_process.empty()) {
95 metrics->mutable_mainboard_process_group()->add_name(cache_metric->mainboard_process);
97 if (!cache_metric->general_process.empty()) {
98 metrics->mutable_general_process()->add_name(cache_metric->general_process);
103void ProcessStatisticComponent::ParseCmd(
104 const std::string& cmd_string,
105 std::shared_ptr<ProcessMetrics> metrics,
106 std::shared_ptr<CacheMetric> cache_metric) {
108 if (cmd_string.find(
"mainboard") != cmd_string.npos) {
109 auto end = std::sregex_iterator();
111 auto dag_begin = std::sregex_iterator(cmd_string.begin(), cmd_string.end(),
mainboard_dag_regex);
113 for (
auto it = dag_begin; it != end; ++it) {
114 std::string name = (*it).str(1);
115 metrics->mutable_mainboard_dag()->add_name(name);
116 cache_metric->mainboard_dags.push_back(std::move(name));
120 for (
auto it = process_begin; it != end; ++it) {
121 std::string name = (*it).str(1);
122 metrics->mutable_mainboard_process_group()->add_name(name);
123 cache_metric->mainboard_process = std::move(name);
127 for (
auto& re : general_process_regex_list_) {
128 if (std::regex_match(cmd_string, re)) {
129 metrics->mutable_general_process()->add_name(cmd_string);
130 cache_metric->general_process = cmd_string;
136void ProcessStatisticComponent::CleanCache() {
137 for (
auto it = cache_.begin(); it != cache_.end();) {
139 auto threshold = cyber::Time().Now().ToNanosecond() - 1e+10;
140 if (it->second->timestamp < threshold) {
141 it = cache_.erase(it);