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