xref: /aosp_15_r20/external/perfetto/src/traced/probes/ftrace/vendor_tracepoints.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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/ftrace/vendor_tracepoints.h"
18 
19 #include <errno.h>
20 #include <string.h>
21 
22 #include <map>
23 #include <string>
24 #include <vector>
25 
26 #include "perfetto/base/status.h"
27 #include "perfetto/ext/base/file_utils.h"
28 #include "perfetto/ext/base/string_splitter.h"
29 #include "src/traced/probes/ftrace/atrace_hal_wrapper.h"
30 #include "src/traced/probes/ftrace/ftrace_procfs.h"
31 #include "src/traced/probes/ftrace/proto_translation_table.h"
32 
33 namespace perfetto {
34 namespace vendor_tracepoints {
35 namespace {
36 
37 using EmptyTokenMode = ::perfetto::base::StringSplitter::EmptyTokenMode;
38 
DiscoverTracepoints(AtraceHalWrapper * hal,FtraceProcfs * ftrace,const std::string & category)39 std::vector<GroupAndName> DiscoverTracepoints(AtraceHalWrapper* hal,
40                                               FtraceProcfs* ftrace,
41                                               const std::string& category) {
42   ftrace->DisableAllEvents();
43   hal->EnableCategories({category});
44 
45   std::vector<GroupAndName> events;
46   for (const std::string& group_name : ftrace->ReadEnabledEvents()) {
47     size_t pos = group_name.find('/');
48     PERFETTO_CHECK(pos != std::string::npos);
49     events.push_back(
50         GroupAndName(group_name.substr(0, pos), group_name.substr(pos + 1)));
51   }
52 
53   hal->DisableAllCategories();
54   ftrace->DisableAllEvents();
55   return events;
56 }
57 
ParseEventLine(base::StringView line,std::vector<GroupAndName> * category)58 base::Status ParseEventLine(base::StringView line,
59                             std::vector<GroupAndName>* category) {
60   // `line` is a line in the vendor file that starts with one or more whitespace
61   // and is expected to contain the path to an ftrace event like:
62   // ```
63   //  cma/cma_alloc_start
64   // ```
65   while (!line.empty() && (line.at(0) == ' ' || line.at(0) == '\t')) {
66     line = line.substr(1);
67   }
68   if (line.empty()) {
69     return base::OkStatus();
70   }
71   size_t pos = line.find('/');
72   if (pos == line.npos) {
73     return base::ErrStatus("Ftrace event path not in group/event format");
74   }
75   base::StringView group = line.substr(0, pos);
76   if (group.empty()) {
77     return base::ErrStatus("Ftrace event path group is empty");
78   }
79   base::StringView name = line.substr(pos + 1);
80   if (name.find('/') != name.npos) {
81     return base::ErrStatus("Ftrace event path has extra / in event name");
82   }
83   if (name.empty()) {
84     return base::ErrStatus("Ftrace event name empty");
85   }
86   category->push_back(GroupAndName(group.ToStdString(), name.ToStdString()));
87   return base::OkStatus();
88 }
89 
90 }  // namespace
91 
92 std::map<std::string, std::vector<GroupAndName>>
DiscoverVendorTracepointsWithHal(AtraceHalWrapper * hal,FtraceProcfs * ftrace)93 DiscoverVendorTracepointsWithHal(AtraceHalWrapper* hal, FtraceProcfs* ftrace) {
94   std::map<std::string, std::vector<GroupAndName>> results;
95   for (const auto& category : hal->ListCategories()) {
96     results.emplace(category, DiscoverTracepoints(hal, ftrace, category));
97   }
98   return results;
99 }
100 
DiscoverVendorTracepointsWithFile(const std::string & vendor_atrace_categories_path,std::map<std::string,std::vector<GroupAndName>> * categories_map)101 base::Status DiscoverVendorTracepointsWithFile(
102     const std::string& vendor_atrace_categories_path,
103     std::map<std::string, std::vector<GroupAndName>>* categories_map) {
104   std::string content;
105   if (!base::ReadFile(vendor_atrace_categories_path, &content)) {
106     return base::ErrStatus("Cannot read vendor atrace file: %s (errno: %d, %s)",
107                            vendor_atrace_categories_path.c_str(), errno,
108                            strerror(errno));
109   }
110   // The file should contain a list of categories (one per line) and, for each
111   // category, a list of ftrace events (one per line, nested):
112   // ```
113   // gfx
114   //  mali/gpu_power_state
115   //  mali/mali_pm_status
116   // thermal_tj
117   //  thermal_exynos/thermal_cpu_pressure
118   //  thermal_exynos/thermal_exynos_arm_update
119   // ```
120   std::vector<GroupAndName>* category = nullptr;
121   for (base::StringSplitter lines(std::move(content), '\n',
122                                   EmptyTokenMode::DISALLOW_EMPTY_TOKENS);
123        lines.Next();) {
124     base::StringView line(lines.cur_token());
125     if (line.empty()) {
126       continue;
127     }
128     char firstchar = line.at(0);
129     if (firstchar == '\t' || firstchar == ' ') {
130       // The line begins with a whitespace. It should contain an ftrace event
131       // path, part of a previously defined category.
132       if (category == nullptr) {
133         return base::ErrStatus(
134             "Ftrace event path before category. Malformed vendor atrace file");
135       }
136       base::Status status = ParseEventLine(line, category);
137       if (!status.ok()) {
138         return status;
139       }
140     } else {
141       // The line doesn't begin with a whitespace. Start a new category.
142       category = &(*categories_map)[line.ToStdString()];
143     }
144   }
145   return base::OkStatus();
146 }
147 
DiscoverAccessibleVendorTracepointsWithFile(const std::string & vendor_atrace_categories_path,std::map<std::string,std::vector<GroupAndName>> * categories_map,FtraceProcfs * ftrace)148 base::Status DiscoverAccessibleVendorTracepointsWithFile(
149     const std::string& vendor_atrace_categories_path,
150     std::map<std::string, std::vector<GroupAndName>>* categories_map,
151     FtraceProcfs* ftrace) {
152   categories_map->clear();
153   base::Status status = DiscoverVendorTracepointsWithFile(
154       vendor_atrace_categories_path, categories_map);
155   if (!status.ok()) {
156     return status;
157   }
158 
159   for (auto& it : *categories_map) {
160     std::vector<GroupAndName>& events = it.second;
161     events.erase(std::remove_if(events.begin(), events.end(),
162                                 [ftrace](const GroupAndName& event) {
163                                   return !ftrace->IsEventAccessible(
164                                       event.group(), event.name());
165                                 }),
166                  events.end());
167   }
168 
169   return base::OkStatus();
170 }
171 
172 }  // namespace vendor_tracepoints
173 }  // namespace perfetto
174