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