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/ftrace/cpu_stats_parser.h"
18
19 #include "perfetto/base/string_splitter.h"
20 #include "perfetto/base/string_utils.h"
21 #include "src/traced/probes/ftrace/ftrace_controller.h"
22 #include "src/traced/probes/ftrace/ftrace_procfs.h"
23 #include "src/traced/probes/ftrace/ftrace_stats.h"
24
25 namespace perfetto {
26 namespace {
27
ExtractInt(const char * s)28 uint32_t ExtractInt(const char* s) {
29 for (; *s != '\0'; s++) {
30 if (*s == ':') {
31 return static_cast<uint32_t>(atoi(s + 1));
32 }
33 }
34 return 0;
35 }
36
ExtractDouble(const char * s)37 double ExtractDouble(const char* s) {
38 for (; *s != '\0'; s++) {
39 if (*s == ':') {
40 return strtod(s + 1, nullptr);
41 }
42 }
43 return 0;
44 }
45
46 } // namespace
47
DumpCpuStats(std::string text,FtraceCpuStats * stats)48 bool DumpCpuStats(std::string text, FtraceCpuStats* stats) {
49 if (text.empty())
50 return false;
51
52 base::StringSplitter splitter(std::move(text), '\n');
53 while (splitter.Next()) {
54 if (base::StartsWith(splitter.cur_token(), "entries")) {
55 stats->entries = ExtractInt(splitter.cur_token());
56 } else if (base::StartsWith(splitter.cur_token(), "overrun")) {
57 stats->overrun = ExtractInt(splitter.cur_token());
58 } else if (base::StartsWith(splitter.cur_token(), "commit overrun")) {
59 stats->commit_overrun = ExtractInt(splitter.cur_token());
60 } else if (base::StartsWith(splitter.cur_token(), "bytes")) {
61 stats->bytes_read = ExtractInt(splitter.cur_token());
62 } else if (base::StartsWith(splitter.cur_token(), "oldest event ts")) {
63 stats->oldest_event_ts = ExtractDouble(splitter.cur_token());
64 } else if (base::StartsWith(splitter.cur_token(), "now ts")) {
65 stats->now_ts = ExtractDouble(splitter.cur_token());
66 } else if (base::StartsWith(splitter.cur_token(), "dropped events")) {
67 stats->dropped_events = ExtractInt(splitter.cur_token());
68 } else if (base::StartsWith(splitter.cur_token(), "read events")) {
69 stats->read_events = ExtractInt(splitter.cur_token());
70 }
71 }
72
73 return true;
74 }
75
DumpAllCpuStats(FtraceProcfs * ftrace,FtraceStats * stats)76 bool DumpAllCpuStats(FtraceProcfs* ftrace, FtraceStats* stats) {
77 stats->cpu_stats.resize(ftrace->NumberOfCpus(), {});
78 for (size_t cpu = 0; cpu < ftrace->NumberOfCpus(); cpu++) {
79 stats->cpu_stats[cpu].cpu = cpu;
80 if (!DumpCpuStats(ftrace->ReadCpuStats(cpu), &stats->cpu_stats[cpu]))
81 return false;
82 }
83 return true;
84 }
85
86 } // namespace perfetto
87