• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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/sys_stats/sys_stats_data_source.h"
18 
19 #include <stdlib.h>
20 #include <unistd.h>
21 
22 #include <algorithm>
23 #include <array>
24 #include <bitset>
25 #include <limits>
26 #include <utility>
27 
28 #include "perfetto/base/task_runner.h"
29 #include "perfetto/base/time.h"
30 #include "perfetto/ext/base/file_utils.h"
31 #include "perfetto/ext/base/metatrace.h"
32 #include "perfetto/ext/base/scoped_file.h"
33 #include "perfetto/ext/base/string_splitter.h"
34 #include "perfetto/ext/base/utils.h"
35 #include "perfetto/ext/traced/sys_stats_counters.h"
36 
37 #include "protos/perfetto/common/sys_stats_counters.pbzero.h"
38 #include "protos/perfetto/config/sys_stats/sys_stats_config.pbzero.h"
39 #include "protos/perfetto/trace/sys_stats/sys_stats.pbzero.h"
40 #include "protos/perfetto/trace/trace_packet.pbzero.h"
41 
42 namespace perfetto {
43 
44 using protos::pbzero::SysStatsConfig;
45 
46 namespace {
47 constexpr size_t kReadBufSize = 1024 * 16;
48 
OpenReadOnly(const char * path)49 base::ScopedFile OpenReadOnly(const char* path) {
50   base::ScopedFile fd(base::OpenFile(path, O_RDONLY));
51   if (!fd)
52     PERFETTO_PLOG("Failed opening %s", path);
53   return fd;
54 }
55 
ClampTo10Ms(uint32_t period_ms,const char * counter_name)56 uint32_t ClampTo10Ms(uint32_t period_ms, const char* counter_name) {
57   if (period_ms > 0 && period_ms < 10) {
58     PERFETTO_ILOG("%s %" PRIu32
59                   " is less than minimum of 10ms. Increasing to 10ms.",
60                   counter_name, period_ms);
61     return 10;
62   }
63   return period_ms;
64 }
65 
66 }  // namespace
67 
68 // static
69 const ProbesDataSource::Descriptor SysStatsDataSource::descriptor = {
70     /*name*/ "linux.sys_stats",
71     /*flags*/ Descriptor::kFlagsNone,
72 };
73 
SysStatsDataSource(base::TaskRunner * task_runner,TracingSessionID session_id,std::unique_ptr<TraceWriter> writer,const DataSourceConfig & ds_config,OpenFunction open_fn)74 SysStatsDataSource::SysStatsDataSource(base::TaskRunner* task_runner,
75                                        TracingSessionID session_id,
76                                        std::unique_ptr<TraceWriter> writer,
77                                        const DataSourceConfig& ds_config,
78                                        OpenFunction open_fn)
79     : ProbesDataSource(session_id, &descriptor),
80       task_runner_(task_runner),
81       writer_(std::move(writer)),
82       weak_factory_(this) {
83   ns_per_user_hz_ = 1000000000ull / static_cast<uint64_t>(sysconf(_SC_CLK_TCK));
84 
85   open_fn = open_fn ? open_fn : OpenReadOnly;
86   meminfo_fd_ = open_fn("/proc/meminfo");
87   vmstat_fd_ = open_fn("/proc/vmstat");
88   stat_fd_ = open_fn("/proc/stat");
89 
90   read_buf_ = base::PagedMemory::Allocate(kReadBufSize);
91 
92   // Build a lookup map that allows to quickly translate strings like "MemTotal"
93   // into the corresponding enum value, only for the counters enabled in the
94   // config.
95 
96   using protos::pbzero::SysStatsConfig;
97   SysStatsConfig::Decoder cfg(ds_config.sys_stats_config_raw());
98 
99   constexpr size_t kMaxMeminfoEnum = protos::pbzero::MeminfoCounters_MAX;
100   std::bitset<kMaxMeminfoEnum + 1> meminfo_counters_enabled{};
101   if (!cfg.has_meminfo_counters())
102     meminfo_counters_enabled.set();
103   for (auto it = cfg.meminfo_counters(); it; ++it) {
104     uint32_t counter = static_cast<uint32_t>(*it);
105     if (counter > 0 && counter <= kMaxMeminfoEnum) {
106       meminfo_counters_enabled.set(counter);
107     } else {
108       PERFETTO_DFATAL("Meminfo counter out of bounds %u", counter);
109     }
110   }
111   for (size_t i = 0; i < base::ArraySize(kMeminfoKeys); i++) {
112     const auto& k = kMeminfoKeys[i];
113     if (meminfo_counters_enabled[static_cast<size_t>(k.id)])
114       meminfo_counters_.emplace(k.str, k.id);
115   }
116 
117   constexpr size_t kMaxVmstatEnum = protos::pbzero::VmstatCounters_MAX;
118   std::bitset<kMaxVmstatEnum + 1> vmstat_counters_enabled{};
119   if (!cfg.has_vmstat_counters())
120     vmstat_counters_enabled.set();
121   for (auto it = cfg.vmstat_counters(); it; ++it) {
122     uint32_t counter = static_cast<uint32_t>(*it);
123     if (counter > 0 && counter <= kMaxVmstatEnum) {
124       vmstat_counters_enabled.set(counter);
125     } else {
126       PERFETTO_DFATAL("Vmstat counter out of bounds %u", counter);
127     }
128   }
129   for (size_t i = 0; i < base::ArraySize(kVmstatKeys); i++) {
130     const auto& k = kVmstatKeys[i];
131     if (vmstat_counters_enabled[static_cast<size_t>(k.id)])
132       vmstat_counters_.emplace(k.str, k.id);
133   }
134 
135   if (!cfg.has_stat_counters())
136     stat_enabled_fields_ = ~0u;
137   for (auto counter = cfg.stat_counters(); counter; ++counter) {
138     stat_enabled_fields_ |= 1ul << static_cast<uint32_t>(*counter);
139   }
140 
141   std::array<uint32_t, 4> periods_ms{};
142   std::array<uint32_t, 4> ticks{};
143   static_assert(periods_ms.size() == ticks.size(), "must have same size");
144 
145   periods_ms[0] = ClampTo10Ms(cfg.meminfo_period_ms(), "meminfo_period_ms");
146   periods_ms[1] = ClampTo10Ms(cfg.vmstat_period_ms(), "vmstat_period_ms");
147   periods_ms[2] = ClampTo10Ms(cfg.stat_period_ms(), "stat_period_ms");
148   periods_ms[3] = ClampTo10Ms(cfg.devfreq_period_ms(), "devfreq_period_ms");
149 
150   tick_period_ms_ = 0;
151   for (uint32_t ms : periods_ms) {
152     if (ms && (ms < tick_period_ms_ || tick_period_ms_ == 0))
153       tick_period_ms_ = ms;
154   }
155 
156   if (tick_period_ms_ == 0)
157     return;  // No polling configured.
158 
159   for (size_t i = 0; i < periods_ms.size(); i++) {
160     auto ms = periods_ms[i];
161     if (ms && ms % tick_period_ms_ != 0) {
162       PERFETTO_ELOG("SysStat periods are not integer multiples of each other");
163       return;
164     }
165     ticks[i] = ms / tick_period_ms_;
166   }
167   meminfo_ticks_ = ticks[0];
168   vmstat_ticks_ = ticks[1];
169   stat_ticks_ = ticks[2];
170   devfreq_ticks_ = ticks[3];
171 }
172 
Start()173 void SysStatsDataSource::Start() {
174   auto weak_this = GetWeakPtr();
175   task_runner_->PostTask(std::bind(&SysStatsDataSource::Tick, weak_this));
176 }
177 
178 // static
Tick(base::WeakPtr<SysStatsDataSource> weak_this)179 void SysStatsDataSource::Tick(base::WeakPtr<SysStatsDataSource> weak_this) {
180   if (!weak_this)
181     return;
182   SysStatsDataSource& thiz = *weak_this;
183 
184   uint32_t period_ms = thiz.tick_period_ms_;
185   uint32_t delay_ms =
186       period_ms -
187       static_cast<uint32_t>(base::GetWallTimeMs().count() % period_ms);
188   thiz.task_runner_->PostDelayedTask(
189       std::bind(&SysStatsDataSource::Tick, weak_this), delay_ms);
190   thiz.ReadSysStats();
191 }
192 
193 SysStatsDataSource::~SysStatsDataSource() = default;
194 
ReadSysStats()195 void SysStatsDataSource::ReadSysStats() {
196   PERFETTO_METATRACE_SCOPED(TAG_PROC_POLLERS, READ_SYS_STATS);
197   auto packet = writer_->NewTracePacket();
198 
199   packet->set_timestamp(static_cast<uint64_t>(base::GetBootTimeNs().count()));
200   auto* sys_stats = packet->set_sys_stats();
201 
202   if (meminfo_ticks_ && tick_ % meminfo_ticks_ == 0)
203     ReadMeminfo(sys_stats);
204 
205   if (vmstat_ticks_ && tick_ % vmstat_ticks_ == 0)
206     ReadVmstat(sys_stats);
207 
208   if (stat_ticks_ && tick_ % stat_ticks_ == 0)
209     ReadStat(sys_stats);
210 
211   if (devfreq_ticks_ && tick_ % devfreq_ticks_ == 0)
212     ReadDevfreq(sys_stats);
213 
214   sys_stats->set_collection_end_timestamp(
215       static_cast<uint64_t>(base::GetBootTimeNs().count()));
216 
217   tick_++;
218 }
219 
ReadDevfreq(protos::pbzero::SysStats * sys_stats)220 void SysStatsDataSource::ReadDevfreq(protos::pbzero::SysStats* sys_stats) {
221   base::ScopedDir devfreq_dir = OpenDevfreqDir();
222   if (devfreq_dir) {
223     while (struct dirent* dir_ent = readdir(*devfreq_dir)) {
224       // Entries in /sys/class/devfreq are symlinks to /devices/platform
225       if (dir_ent->d_type != DT_LNK)
226         continue;
227       const char* name = dir_ent->d_name;
228       const char* file_content = ReadDevfreqCurFreq(name);
229       auto value = static_cast<uint64_t>(strtoll(file_content, nullptr, 10));
230       auto* devfreq = sys_stats->add_devfreq();
231       devfreq->set_key(name);
232       devfreq->set_value(value);
233     }
234   }
235 }
236 
OpenDevfreqDir()237 base::ScopedDir SysStatsDataSource::OpenDevfreqDir() {
238   const char* base_dir = "/sys/class/devfreq/";
239   base::ScopedDir devfreq_dir(opendir(base_dir));
240   if (!devfreq_dir && !devfreq_error_logged_) {
241     devfreq_error_logged_ = true;
242     PERFETTO_PLOG("failed to opendir(/sys/class/devfreq)");
243   }
244   return devfreq_dir;
245 }
246 
ReadDevfreqCurFreq(const std::string & deviceName)247 const char* SysStatsDataSource::ReadDevfreqCurFreq(
248     const std::string& deviceName) {
249   const char* devfreq_base_path = "/sys/class/devfreq";
250   const char* freq_file_name = "cur_freq";
251   char cur_freq_path[256];
252   snprintf(cur_freq_path, sizeof(cur_freq_path), "%s/%s/%s", devfreq_base_path,
253            deviceName.c_str(), freq_file_name);
254   base::ScopedFile fd = OpenReadOnly(cur_freq_path);
255   if (!fd && !devfreq_error_logged_) {
256     devfreq_error_logged_ = true;
257     PERFETTO_PLOG("Failed to open %s", cur_freq_path);
258     return "";
259   }
260   size_t rsize = ReadFile(&fd, cur_freq_path);
261   if (!rsize)
262     return "";
263   return static_cast<char*>(read_buf_.Get());
264 }
265 
ReadMeminfo(protos::pbzero::SysStats * sys_stats)266 void SysStatsDataSource::ReadMeminfo(protos::pbzero::SysStats* sys_stats) {
267   size_t rsize = ReadFile(&meminfo_fd_, "/proc/meminfo");
268   if (!rsize)
269     return;
270   char* buf = static_cast<char*>(read_buf_.Get());
271   for (base::StringSplitter lines(buf, rsize, '\n'); lines.Next();) {
272     base::StringSplitter words(&lines, ' ');
273     if (!words.Next())
274       continue;
275     // Extract the meminfo key, dropping trailing ':' (e.g., "MemTotal: NN KB").
276     words.cur_token()[words.cur_token_size() - 1] = '\0';
277     auto it = meminfo_counters_.find(words.cur_token());
278     if (it == meminfo_counters_.end())
279       continue;
280     int counter_id = it->second;
281     if (!words.Next())
282       continue;
283     auto value = static_cast<uint64_t>(strtoll(words.cur_token(), nullptr, 10));
284     auto* meminfo = sys_stats->add_meminfo();
285     meminfo->set_key(static_cast<protos::pbzero::MeminfoCounters>(counter_id));
286     meminfo->set_value(value);
287   }
288 }
289 
ReadVmstat(protos::pbzero::SysStats * sys_stats)290 void SysStatsDataSource::ReadVmstat(protos::pbzero::SysStats* sys_stats) {
291   size_t rsize = ReadFile(&vmstat_fd_, "/proc/vmstat");
292   if (!rsize)
293     return;
294   char* buf = static_cast<char*>(read_buf_.Get());
295   for (base::StringSplitter lines(buf, rsize, '\n'); lines.Next();) {
296     base::StringSplitter words(&lines, ' ');
297     if (!words.Next())
298       continue;
299     auto it = vmstat_counters_.find(words.cur_token());
300     if (it == vmstat_counters_.end())
301       continue;
302     int counter_id = it->second;
303     if (!words.Next())
304       continue;
305     auto value = static_cast<uint64_t>(strtoll(words.cur_token(), nullptr, 10));
306     auto* vmstat = sys_stats->add_vmstat();
307     vmstat->set_key(static_cast<protos::pbzero::VmstatCounters>(counter_id));
308     vmstat->set_value(value);
309   }
310 }
311 
ReadStat(protos::pbzero::SysStats * sys_stats)312 void SysStatsDataSource::ReadStat(protos::pbzero::SysStats* sys_stats) {
313   size_t rsize = ReadFile(&stat_fd_, "/proc/stat");
314   if (!rsize)
315     return;
316   char* buf = static_cast<char*>(read_buf_.Get());
317   for (base::StringSplitter lines(buf, rsize, '\n'); lines.Next();) {
318     base::StringSplitter words(&lines, ' ');
319     if (!words.Next())
320       continue;
321 
322     // Per-CPU stats.
323     if ((stat_enabled_fields_ & (1 << SysStatsConfig::STAT_CPU_TIMES)) &&
324         words.cur_token_size() > 3 && !strncmp(words.cur_token(), "cpu", 3)) {
325       long cpu_id = strtol(words.cur_token() + 3, nullptr, 10);
326       std::array<uint64_t, 7> cpu_times{};
327       for (size_t i = 0; i < cpu_times.size() && words.Next(); i++) {
328         cpu_times[i] =
329             static_cast<uint64_t>(strtoll(words.cur_token(), nullptr, 10));
330       }
331       auto* cpu_stat = sys_stats->add_cpu_stat();
332       cpu_stat->set_cpu_id(static_cast<uint32_t>(cpu_id));
333       cpu_stat->set_user_ns(cpu_times[0] * ns_per_user_hz_);
334       cpu_stat->set_user_ice_ns(cpu_times[1] * ns_per_user_hz_);
335       cpu_stat->set_system_mode_ns(cpu_times[2] * ns_per_user_hz_);
336       cpu_stat->set_idle_ns(cpu_times[3] * ns_per_user_hz_);
337       cpu_stat->set_io_wait_ns(cpu_times[4] * ns_per_user_hz_);
338       cpu_stat->set_irq_ns(cpu_times[5] * ns_per_user_hz_);
339       cpu_stat->set_softirq_ns(cpu_times[6] * ns_per_user_hz_);
340     }
341     // IRQ counters
342     else if ((stat_enabled_fields_ & (1 << SysStatsConfig::STAT_IRQ_COUNTS)) &&
343              !strcmp(words.cur_token(), "intr")) {
344       for (size_t i = 0; words.Next(); i++) {
345         auto v = static_cast<uint64_t>(strtoll(words.cur_token(), nullptr, 10));
346         if (i == 0) {
347           sys_stats->set_num_irq_total(v);
348         } else if (v > 0) {
349           auto* irq_stat = sys_stats->add_num_irq();
350           irq_stat->set_irq(static_cast<int32_t>(i - 1));
351           irq_stat->set_count(v);
352         }
353       }
354     }
355     // Softirq counters.
356     else if ((stat_enabled_fields_ &
357               (1 << SysStatsConfig::STAT_SOFTIRQ_COUNTS)) &&
358              !strcmp(words.cur_token(), "softirq")) {
359       for (size_t i = 0; words.Next(); i++) {
360         auto v = static_cast<uint64_t>(strtoll(words.cur_token(), nullptr, 10));
361         if (i == 0) {
362           sys_stats->set_num_softirq_total(v);
363         } else {
364           auto* softirq_stat = sys_stats->add_num_softirq();
365           softirq_stat->set_irq(static_cast<int32_t>(i - 1));
366           softirq_stat->set_count(v);
367         }
368       }
369     }
370     // Number of forked processes since boot.
371     else if ((stat_enabled_fields_ & (1 << SysStatsConfig::STAT_FORK_COUNT)) &&
372              !strcmp(words.cur_token(), "processes")) {
373       if (words.Next()) {
374         sys_stats->set_num_forks(
375             static_cast<uint64_t>(strtoll(words.cur_token(), nullptr, 10)));
376       }
377     }
378 
379   }  // for (line)
380 }
381 
GetWeakPtr() const382 base::WeakPtr<SysStatsDataSource> SysStatsDataSource::GetWeakPtr() const {
383   return weak_factory_.GetWeakPtr();
384 }
385 
Flush(FlushRequestID,std::function<void ()> callback)386 void SysStatsDataSource::Flush(FlushRequestID, std::function<void()> callback) {
387   writer_->Flush(callback);
388 }
389 
ReadFile(base::ScopedFile * fd,const char * path)390 size_t SysStatsDataSource::ReadFile(base::ScopedFile* fd, const char* path) {
391   if (!*fd)
392     return 0;
393   ssize_t res = pread(**fd, read_buf_.Get(), kReadBufSize - 1, 0);
394   if (res <= 0) {
395     PERFETTO_PLOG("Failed reading %s", path);
396     fd->reset();
397     return 0;
398   }
399   size_t rsize = static_cast<size_t>(res);
400   static_cast<char*>(read_buf_.Get())[rsize] = '\0';
401   return rsize + 1;  // Include null terminator in the count.
402 }
403 
404 }  // namespace perfetto
405