1 /*
2 * Copyright (C) 2020 The Android Open Source Project
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 */
16
17 #include "src/traced/probes/system_info/system_info_data_source.h"
18
19 #include <optional>
20
21 #include "perfetto/base/logging.h"
22 #include "perfetto/base/time.h"
23 #include "perfetto/ext/base/file_utils.h"
24 #include "perfetto/ext/base/string_splitter.h"
25 #include "perfetto/ext/base/string_utils.h"
26
27 #include "protos/perfetto/trace/system_info/cpu_info.pbzero.h"
28 #include "protos/perfetto/trace/trace_packet.pbzero.h"
29
30 namespace perfetto {
31
32 namespace {
33
34 // Key for default processor string in /proc/cpuinfo as seen on arm. Note the
35 // uppercase P.
36 const char kDefaultProcessor[] = "Processor";
37
38 // Key for processor entry in /proc/cpuinfo. Used to determine whether a group
39 // of lines describes a CPU.
40 const char kProcessor[] = "processor";
41
42 // Key for CPU implementer in /proc/cpuinfo. Arm only.
43 const char kImplementer[] = "CPU implementer";
44
45 // Key for CPU architecture in /proc/cpuinfo. Arm only.
46 const char kArchitecture[] = "CPU architecture";
47
48 // Key for CPU variant in /proc/cpuinfo. Arm only.
49 const char kVariant[] = "CPU variant";
50
51 // Key for CPU part in /proc/cpuinfo. Arm only.
52 const char kPart[] = "CPU part";
53
54 // Key for CPU revision in /proc/cpuinfo. Arm only.
55 const char kRevision[] = "CPU revision";
56
57 } // namespace
58
59 // static
60 const ProbesDataSource::Descriptor SystemInfoDataSource::descriptor = {
61 /* name */ "linux.system_info",
62 /* flags */ Descriptor::kFlagsNone,
63 /* fill_descriptor_func */ nullptr,
64 };
65
SystemInfoDataSource(TracingSessionID session_id,std::unique_ptr<TraceWriter> writer,std::unique_ptr<CpuFreqInfo> cpu_freq_info)66 SystemInfoDataSource::SystemInfoDataSource(
67 TracingSessionID session_id,
68 std::unique_ptr<TraceWriter> writer,
69 std::unique_ptr<CpuFreqInfo> cpu_freq_info)
70 : ProbesDataSource(session_id, &descriptor),
71 writer_(std::move(writer)),
72 cpu_freq_info_(std::move(cpu_freq_info)) {}
73
Start()74 void SystemInfoDataSource::Start() {
75 auto packet = writer_->NewTracePacket();
76 packet->set_timestamp(static_cast<uint64_t>(base::GetBootTimeNs().count()));
77 auto* cpu_info = packet->set_cpu_info();
78
79 // Parse /proc/cpuinfo which contains groups of "key\t: value" lines separated
80 // by an empty line. Each group represents a CPU. See the full example in the
81 // unittest.
82 std::string proc_cpu_info = ReadFile("/proc/cpuinfo");
83 std::string::iterator line_start = proc_cpu_info.begin();
84 std::string::iterator line_end = proc_cpu_info.end();
85 std::string default_processor = "unknown";
86 std::string cpu_index = "";
87
88 std::optional<uint32_t> implementer;
89 std::optional<uint32_t> architecture;
90 std::optional<uint32_t> variant;
91 std::optional<uint32_t> part;
92 std::optional<uint32_t> revision;
93
94 uint32_t next_cpu_index = 0;
95 while (line_start != proc_cpu_info.end()) {
96 line_end = find(line_start, proc_cpu_info.end(), '\n');
97 if (line_end == proc_cpu_info.end())
98 break;
99 std::string line = std::string(line_start, line_end);
100 line_start = line_end + 1;
101 if (line.empty() && !cpu_index.empty()) {
102 PERFETTO_DCHECK(cpu_index == std::to_string(next_cpu_index));
103
104 auto* cpu = cpu_info->add_cpus();
105 cpu->set_processor(default_processor);
106
107 std::optional<uint32_t> cpu_capacity = base::StringToUInt32(
108 base::StripSuffix(ReadFile("/sys/devices/system/cpu/cpu" + cpu_index +
109 "/cpu_capacity"),
110 "\n"));
111
112 if (cpu_capacity.has_value()) {
113 cpu->set_capacity(cpu_capacity.value());
114 }
115
116 auto freqs_range = cpu_freq_info_->GetFreqs(next_cpu_index);
117 for (auto it = freqs_range.first; it != freqs_range.second; it++) {
118 cpu->add_frequencies(*it);
119 }
120 cpu_index = "";
121
122 // Set Arm CPU identifier if available
123 if (implementer || architecture || part || variant || revision) {
124 if (implementer && architecture && part && variant && revision) {
125 auto* identifier = cpu->set_arm_identifier();
126 identifier->set_implementer(implementer.value());
127 identifier->set_architecture(architecture.value());
128 identifier->set_variant(variant.value());
129 identifier->set_part(part.value());
130 identifier->set_revision(revision.value());
131 } else {
132 PERFETTO_ILOG(
133 "Failed to parse Arm specific fields from /proc/cpuinfo");
134 }
135 }
136
137 implementer = std::nullopt;
138 architecture = std::nullopt;
139 variant = std::nullopt;
140 part = std::nullopt;
141 revision = std::nullopt;
142
143 next_cpu_index++;
144 continue;
145 }
146 auto splits = base::SplitString(line, ":");
147 if (splits.size() != 2)
148 continue;
149 std::string key =
150 base::StripSuffix(base::StripChars(splits[0], "\t", ' '), " ");
151 std::string value = base::StripPrefix(splits[1], " ");
152 if (key == kDefaultProcessor)
153 default_processor = value;
154 else if (key == kProcessor)
155 cpu_index = value;
156 else if (key == kImplementer)
157 implementer = base::CStringToUInt32(value.data(), 16);
158 else if (key == kArchitecture)
159 architecture = base::CStringToUInt32(value.data(), 10);
160 else if (key == kVariant)
161 variant = base::CStringToUInt32(value.data(), 16);
162 else if (key == kPart)
163 part = base::CStringToUInt32(value.data(), 16);
164 else if (key == kRevision)
165 revision = base::CStringToUInt32(value.data(), 10);
166 }
167
168 packet->Finalize();
169 writer_->Flush();
170 }
171
Flush(FlushRequestID,std::function<void ()> callback)172 void SystemInfoDataSource::Flush(FlushRequestID,
173 std::function<void()> callback) {
174 writer_->Flush(callback);
175 }
176
ReadFile(std::string path)177 std::string SystemInfoDataSource::ReadFile(std::string path) {
178 std::string contents;
179 if (!base::ReadFile(path, &contents))
180 return "";
181 return contents;
182 }
183
184 } // namespace perfetto
185