• 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 "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     /* fill_descriptor_func */ nullptr,
46 };
47 
SystemInfoDataSource(TracingSessionID session_id,std::unique_ptr<TraceWriter> writer,std::unique_ptr<CpuFreqInfo> cpu_freq_info)48 SystemInfoDataSource::SystemInfoDataSource(
49     TracingSessionID session_id,
50     std::unique_ptr<TraceWriter> writer,
51     std::unique_ptr<CpuFreqInfo> cpu_freq_info)
52     : ProbesDataSource(session_id, &descriptor),
53       writer_(std::move(writer)),
54       cpu_freq_info_(std::move(cpu_freq_info)) {}
55 
Start()56 void SystemInfoDataSource::Start() {
57   auto packet = writer_->NewTracePacket();
58   packet->set_timestamp(static_cast<uint64_t>(base::GetBootTimeNs().count()));
59   auto* cpu_info = packet->set_cpu_info();
60 
61   // Parse /proc/cpuinfo which contains groups of "key\t: value" lines separated
62   // by an empty line. Each group represents a CPU. See the full example in the
63   // unittest.
64   std::string proc_cpu_info = ReadFile("/proc/cpuinfo");
65   std::string::iterator line_start = proc_cpu_info.begin();
66   std::string::iterator line_end = proc_cpu_info.end();
67   std::string default_processor = "unknown";
68   std::string cpu_index = "";
69   uint32_t next_cpu_index = 0;
70   while (line_start != proc_cpu_info.end()) {
71     line_end = find(line_start, proc_cpu_info.end(), '\n');
72     if (line_end == proc_cpu_info.end())
73       break;
74     std::string line = std::string(line_start, line_end);
75     line_start = line_end + 1;
76     if (line.empty() && !cpu_index.empty()) {
77       PERFETTO_DCHECK(cpu_index == std::to_string(next_cpu_index));
78       auto* cpu = cpu_info->add_cpus();
79       cpu->set_processor(default_processor);
80       auto freqs_range = cpu_freq_info_->GetFreqs(next_cpu_index);
81       for (auto it = freqs_range.first; it != freqs_range.second; it++) {
82         cpu->add_frequencies(*it);
83       }
84       cpu_index = "";
85       next_cpu_index++;
86       continue;
87     }
88     auto splits = base::SplitString(line, ":");
89     if (splits.size() != 2)
90       continue;
91     std::string key =
92         base::StripSuffix(base::StripChars(splits[0], "\t", ' '), " ");
93     std::string value = base::StripPrefix(splits[1], " ");
94     if (key == kDefaultProcessor)
95       default_processor = value;
96     else if (key == kProcessor)
97       cpu_index = value;
98   }
99 
100   packet->Finalize();
101   writer_->Flush();
102 }
103 
Flush(FlushRequestID,std::function<void ()> callback)104 void SystemInfoDataSource::Flush(FlushRequestID,
105                                  std::function<void()> callback) {
106   writer_->Flush(callback);
107 }
108 
ReadFile(std::string path)109 std::string SystemInfoDataSource::ReadFile(std::string path) {
110   std::string contents;
111   if (!base::ReadFile(path, &contents))
112     return "";
113   return contents;
114 }
115 
116 }  // namespace perfetto
117