Apollo 11.0
自动驾驶开放平台
cpu_resource_statistic.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
17
18#include <algorithm>
19#include <regex>
20#include <string>
21#include <vector>
22
23#include <boost/algorithm/string.hpp>
24#include <boost/filesystem.hpp>
25
26#include "absl/strings/str_split.h"
27
28#include "cyber/cyber.h"
30
31namespace apollo {
32namespace resource_statistic {
33
34// cpu
36 if (!GetUsage(cpu_metric)) {
37 return false;
38 }
39 if (!GetTemperature(cpu_metric)) {
40 return false;
41 }
42 return true;
43}
44
45bool CpuResourceStatistic::GetUsage(CpuMetric* cpu_metric) {
46 static uint64_t prev_jiffies = 0, prev_work_jiffies = 0;
47 static const std::string system_cpu_stat_file = "/proc/stat";
48 const int users = 1, system = 3, total = 7;
49 static constexpr int kSystemCpuInfo = 0;
50 const auto stat_lines = GetStatsLines(system_cpu_stat_file, kSystemCpuInfo + 1);
51 if (stat_lines.size() <= kSystemCpuInfo) {
52 AERROR << "failed to load contents from " << system_cpu_stat_file;
53 return false;
54 }
55 const std::vector<std::string> jiffies_stats
56 = absl::StrSplit(stat_lines[kSystemCpuInfo], ' ', absl::SkipWhitespace());
57 if (jiffies_stats.size() <= total) {
58 AERROR << "failed to get system CPU info from " << system_cpu_stat_file;
59 return false;
60 }
61 uint64_t jiffies = 0, work_jiffies = 0;
62 for (int cur_stat = users; cur_stat <= total; ++cur_stat) {
63 const auto cur_stat_value = std::stoll(jiffies_stats[cur_stat]);
64 jiffies += cur_stat_value;
65 if (cur_stat <= system) {
66 work_jiffies += cur_stat_value;
67 }
68 }
69 const uint64_t tmp_prev_jiffies = prev_jiffies;
70 const uint64_t tmp_prev_work_jiffies = prev_work_jiffies;
71 prev_jiffies = jiffies;
72 prev_work_jiffies = work_jiffies;
73 if (tmp_prev_jiffies == 0) {
74 cpu_metric->set_usage(0.f);
75 } else {
76 cpu_metric->set_usage(
77 100.f * (static_cast<float>(work_jiffies - tmp_prev_work_jiffies) / (jiffies - tmp_prev_jiffies)));
78 }
79 return true;
80}
81
82bool CpuResourceStatistic::GetTemperature(CpuMetric* cpu_metric) {
83 static const std::string cpu_temp_files_path = "/sys/class/hwmon/hwmon*/temp*_input";
84 std::vector<std::string> temp_files = cyber::common::Glob(cpu_temp_files_path);
85 if (temp_files.empty()) {
86 AERROR << "temperature files not found!";
87 return true;
88 }
89 float temperature = 0;
90 for (auto& temp_input : temp_files) {
91 std::string temp;
92 cyber::common::GetContent(temp_input, &temp);
93 temperature = std::max(std::stof(temp), temperature);
94 }
95 temperature /= 1000;
96 cpu_metric->set_temperature(temperature);
97 return true;
98}
99
100} // namespace resource_statistic
101} // namespace apollo
bool GetCpuMetric(CpuMetric *cpu_metric)
Get CPU metric and write it into cpu_metric.
#define AERROR
Definition log.h:44
std::vector< std::string > Glob(const std::string &pattern)
Expand path pattern to matched paths.
Definition file.cc:212
bool GetContent(const std::string &file_name, std::string *content)
Get file content as string.
Definition file.cc:167
std::vector< std::string > GetStatsLines(const std::string &stat_file, const int line_count)
Get stats lines from file
Definition util.cc:24
class register implement
Definition arena_queue.h:37