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/tools/ftrace_proto_gen/ftrace_proto_gen.h"
18
19 #include <algorithm>
20 #include <fstream>
21 #include <regex>
22
23 #include "perfetto/base/logging.h"
24 #include "perfetto/ext/base/file_utils.h"
25 #include "perfetto/ext/base/pipe.h"
26 #include "perfetto/ext/base/string_splitter.h"
27 #include "perfetto/ext/base/string_utils.h"
28
29 namespace perfetto {
30
31 namespace {
32
33 const char kCopyrightHeader[] = R"(/*
34 * Copyright (C) 2017 The Android Open Source Project
35 *
36 * Licensed under the Apache License, Version 2.0 (the "License");
37 * you may not use this file except in compliance with the License.
38 * You may obtain a copy of the License at
39 *
40 * http://www.apache.org/licenses/LICENSE-2.0
41 *
42 * Unless required by applicable law or agreed to in writing, software
43 * distributed under the License is distributed on an "AS IS" BASIS,
44 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45 * See the License for the specific language governing permissions and
46 * limitations under the License.
47 */
48
49 )";
50
51 } // namespace
52
53 using base::Contains;
54 using base::StartsWith;
55
EventNameToProtoFieldName(const std::string & group,const std::string & name)56 std::string EventNameToProtoFieldName(const std::string& group,
57 const std::string& name) {
58 std::string event_name = (name == "0") ? "zero" : name;
59 // These groups have events where the name alone conflicts with an existing
60 // proto:
61 if (group == "sde" || group == "g2d" || group == "dpu" || group == "mali" ||
62 group == "lwis" || group == "samsung" ||
63 (group == "kgsl" && name == "gpu_frequency")) {
64 event_name = group + "_" + event_name;
65 }
66 return event_name;
67 }
68
EventNameToProtoName(const std::string & group,const std::string & name)69 std::string EventNameToProtoName(const std::string& group,
70 const std::string& name) {
71 return ToCamelCase(EventNameToProtoFieldName(group, name)) + "FtraceEvent";
72 }
73
ReadAllowList(const std::string & filename)74 std::vector<FtraceEventName> ReadAllowList(const std::string& filename) {
75 std::string line;
76 std::vector<FtraceEventName> lines;
77 std::ifstream fin(filename, std::ios::in);
78 if (!fin) {
79 fprintf(stderr, "failed to open event list %s\n", filename.c_str());
80 return lines;
81 }
82 while (std::getline(fin, line)) {
83 if (!StartsWith(line, "#"))
84 lines.emplace_back(FtraceEventName(line));
85 }
86 return lines;
87 }
88
ToProtoFields(const FtraceEvent & format)89 std::vector<Proto::Field> ToProtoFields(const FtraceEvent& format) {
90 std::vector<Proto::Field> ret;
91 for (size_t i = 0; i < format.fields.size(); i++) {
92 const FtraceEvent::Field& field = format.fields[i];
93 std::string name = GetNameFromTypeAndName(field.type_and_name);
94 // Skip tracepoint fields whose names cannot be used as an identifier in the
95 // generated C++ code due to stdlib header conflicts.
96 if (name == "" || name == "sa_handler" || name == "errno")
97 continue;
98 ProtoType type = InferProtoType(field);
99 if (type.type == ProtoType::INVALID)
100 continue;
101 ret.push_back({type, std::move(name), static_cast<uint32_t>(i)});
102 }
103 return ret;
104 }
105
GenerateFtraceEventProto(const std::vector<FtraceEventName> & raw_eventlist,const std::set<std::string> & groups,std::ostream * fout)106 void GenerateFtraceEventProto(const std::vector<FtraceEventName>& raw_eventlist,
107 const std::set<std::string>& groups,
108 std::ostream* fout) {
109 *fout << kCopyrightHeader;
110 *fout << "// Autogenerated by:\n";
111 *fout << std::string("// ") + __FILE__ + "\n";
112 *fout << "// Do not edit.\n\n";
113 *fout << R"(syntax = "proto2";)"
114 << "\n\n";
115
116 for (const std::string& group : groups) {
117 *fout << R"(import "protos/perfetto/trace/ftrace/)" << group
118 << R"(.proto";)"
119 << "\n";
120 }
121 *fout << "import \"protos/perfetto/trace/ftrace/generic.proto\";\n";
122 *fout << "\n";
123 *fout << "package perfetto.protos;\n\n";
124 *fout << R"(message FtraceEvent {
125 // Timestamp in nanoseconds using .../tracing/trace_clock.
126 optional uint64 timestamp = 1;
127
128 // Kernel pid (do not confuse with userspace pid aka tgid).
129 optional uint32 pid = 2;
130
131 // Not populated in actual traces. Wire format might change.
132 // Placeholder declaration so that the ftrace parsing code accepts the
133 // existence of this common field. If this becomes needed for all events:
134 // consider merging with common_preempt_count to avoid extra proto tags.
135 optional uint32 common_flags = 5;
136
137 oneof event {
138 )";
139
140 int i = 3;
141 for (const FtraceEventName& event : raw_eventlist) {
142 if (!event.valid()) {
143 *fout << " // removed field with id " << i << ";\n";
144 ++i;
145 continue;
146 }
147
148 std::string field_name =
149 EventNameToProtoFieldName(event.group(), event.name());
150 std::string type_name = EventNameToProtoName(event.group(), event.name());
151
152 // " " (indent) + TypeName + " " + field_name + " = " + 123 + ";"
153 if (4 + type_name.size() + 1 + field_name.size() + 3 + 3 + 1 <= 80) {
154 // Everything fits in one line:
155 *fout << " " << type_name << " " << field_name << " = " << i << ";\n";
156 } else if (4 + type_name.size() + 1 + field_name.size() + 2 <= 80) {
157 // Everything fits except the field id:
158 *fout << " " << type_name << " " << field_name << " =\n " << i
159 << ";\n";
160 } else {
161 // Nothing fits:
162 *fout << " " << type_name << "\n " << field_name << " = " << i
163 << ";\n";
164 }
165 ++i;
166 // We cannot depend on the proto file to get this number because
167 // it would cause a dependency cycle between this generator and the
168 // generated code.
169 if (i == 327) {
170 *fout << " GenericFtraceEvent generic = " << i << ";\n";
171 ++i;
172 }
173 }
174 *fout << " }\n";
175 *fout << "}\n";
176 }
177
178 // Generates section of event_info.cc for a single event.
SingleEventInfo(perfetto::Proto proto,const std::string & group,const uint32_t proto_field_id)179 std::string SingleEventInfo(perfetto::Proto proto,
180 const std::string& group,
181 const uint32_t proto_field_id) {
182 std::string s = "{";
183 s += "\"" + proto.event_name + "\", ";
184 s += "\"" + group + "\", ";
185
186 // Vector of fields.
187 s += "{ ";
188 for (const auto& field : proto.SortedFields()) {
189 // Ignore the "ip" field from print events. This field has not proven
190 // particularly useful and takes up a large amount of space (30% of total
191 // PrintFtraceEvent size and up to 12% of the entire trace in some
192 // configurations)
193 if (group == "ftrace" && proto.event_name == "print" && field->name == "ip")
194 continue;
195 // Ignore the "nid" field. On new kernels, this field has a type that we
196 // don't know how to parse. See b/281660544
197 if (group == "f2fs" && proto.event_name == "f2fs_truncate_partial_nodes" &&
198 field->name == "nid")
199 continue;
200 s += "{";
201 s += "kUnsetOffset, ";
202 s += "kUnsetSize, ";
203 s += "FtraceFieldType::kInvalidFtraceFieldType, ";
204 s += "\"" + field->name + "\", ";
205 s += std::to_string(field->number) + ", ";
206 s += "ProtoSchemaType::k" + ToCamelCase(field->type.ToString()) + ", ";
207 s += "TranslationStrategy::kInvalidTranslationStrategy";
208 s += "}, ";
209 }
210 s += "}, ";
211
212 s += "kUnsetFtraceId, ";
213 s += std::to_string(proto_field_id) + ", ";
214 s += "kUnsetSize";
215 s += "}";
216 return s;
217 }
218
219 // This will generate the event_info.cc file for the listed protos.
GenerateEventInfo(const std::vector<std::string> & events_info,std::ostream * fout)220 void GenerateEventInfo(const std::vector<std::string>& events_info,
221 std::ostream* fout) {
222 std::string s = kCopyrightHeader;
223 s += "// Autogenerated by:\n";
224 s += std::string("// ") + __FILE__ + "\n";
225 s += "// Do not edit.\n";
226 s += R"(
227 #include "src/traced/probes/ftrace/event_info.h"
228
229 #include "perfetto/protozero/proto_utils.h"
230
231 namespace perfetto {
232
233 using protozero::proto_utils::ProtoSchemaType;
234
235 std::vector<Event> GetStaticEventInfo() {
236 static constexpr uint16_t kUnsetOffset = 0;
237 static constexpr uint16_t kUnsetSize = 0;
238 static constexpr uint16_t kUnsetFtraceId = 0;
239 return
240 )";
241
242 s += " {";
243 for (const auto& event : events_info) {
244 s += event;
245 s += ",\n";
246 }
247 s += "};";
248
249 s += R"(
250 }
251
252 } // namespace perfetto
253 )";
254
255 *fout << s;
256 }
257
ProtoHeader()258 std::string ProtoHeader() {
259 std::string s = "// Autogenerated by:\n";
260 s += std::string("// ") + __FILE__ + "\n";
261 s += "// Do not edit.\n";
262
263 s += R"(
264 syntax = "proto2";
265 package perfetto.protos;
266
267 )";
268 return s;
269 }
270
271 } // namespace perfetto
272