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 "perfetto/base/time.h"
20 #include "perfetto/ext/base/file_utils.h"
21 #include "perfetto/ext/base/string_splitter.h"
22 #include "perfetto/ext/base/string_utils.h"
23 
24 #include "protos/perfetto/trace/system_info/cpu_info.pbzero.h"
25 #include "protos/perfetto/trace/trace_packet.pbzero.h"
26 
27 namespace perfetto {
28 
29 namespace {
30 
31 // Key for default processor string in /proc/cpuinfo as seen on arm. Note the
32 // uppercase P.
33 const char kDefaultProcessor[] = "Processor";
34 
35 // Key for processor entry in /proc/cpuinfo. Used to determine whether a group
36 // of lines describes a CPU.
37 const char kProcessor[] = "processor";
38 
39 }  // namespace
40 
41 // static
42 const ProbesDataSource::Descriptor SystemInfoDataSource::descriptor = {
43     /* name */ "linux.system_info",
44     /* flags */ Descriptor::kFlagsNone,
45 };
46 
SystemInfoDataSource(TracingSessionID session_id,std::unique_ptr<TraceWriter> writer,std::unique_ptr<CpuFreqInfo> cpu_freq_info)47 SystemInfoDataSource::SystemInfoDataSource(
48     TracingSessionID session_id,
49     std::unique_ptr<TraceWriter> writer,
50     std::unique_ptr<CpuFreqInfo> cpu_freq_info)
51     : ProbesDataSource(session_id, &descriptor),
52       writer_(std::move(writer)),
53       cpu_freq_info_(std::move(cpu_freq_info)) {}
54 
Start()55 void SystemInfoDataSource::Start() {
56   auto packet = writer_->NewTracePacket();
57   packet->set_timestamp(static_cast<uint64_t>(base::GetBootTimeNs().count()));
58   auto* cpu_info = packet->set_cpu_info();
59 
60   // Parse /proc/cpuinfo which contains groups of "key\t: value" lines separated
61   // by an empty line. Each group represents a CPU. See the full example in the
62   // unittest.
63   std::string proc_cpu_info = ReadFile("/proc/cpuinfo");
64   std::string::iterator line_start = proc_cpu_info.begin();
65   std::string::iterator line_end = proc_cpu_info.end();
66   std::string default_processor = "unknown";
67   std::string cpu_index = "";
68   uint32_t next_cpu_index = 0;
69   while (line_start != proc_cpu_info.end()) {
70     line_end = find(line_start, proc_cpu_info.end(), '\n');
71     if (line_end == proc_cpu_info.end())
72       break;
73     std::string line = std::string(line_start, line_end);
74     line_start = line_end + 1;
75     if (line.empty() && !cpu_index.empty()) {
76       PERFETTO_DCHECK(cpu_index == std::to_string(next_cpu_index));
77       auto* cpu = cpu_info->add_cpus();
78       cpu->set_processor(default_processor);
79       auto freqs_range = cpu_freq_info_->GetFreqs(next_cpu_index);
80       for (auto it = freqs_range.first; it != freqs_range.second; it++) {
81         cpu->add_frequencies(*it);
82       }
83       cpu_index = "";
84       next_cpu_index++;
85       continue;
86     }
87     auto splits = base::SplitString(line, ":");
88     if (splits.size() != 2)
89       continue;
90     std::string key =
91         base::StripSuffix(base::StripChars(splits[0], "\t", ' '), " ");
92     std::string value = base::StripPrefix(splits[1], " ");
93     if (key == kDefaultProcessor)
94       default_processor = value;
95     else if (key == kProcessor)
96       cpu_index = value;
97   }
98 
99   packet->Finalize();
100   writer_->Flush();
101 }
102 
Flush(FlushRequestID,std::function<void ()> callback)103 void SystemInfoDataSource::Flush(FlushRequestID,
104                                  std::function<void()> callback) {
105   writer_->Flush(callback);
106 }
107 
ReadFile(std::string path)108 std::string SystemInfoDataSource::ReadFile(std::string path) {
109   std::string contents;
110   if (!base::ReadFile(path, &contents))
111     return "";
112   return contents;
113 }
114 
115 }  // namespace perfetto
116