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") {
63 event_name = group + "_" + event_name;
64 }
65 return event_name;
66 }
67
EventNameToProtoName(const std::string & group,const std::string & name)68 std::string EventNameToProtoName(const std::string& group,
69 const std::string& name) {
70 return ToCamelCase(EventNameToProtoFieldName(group, name)) + "FtraceEvent";
71 }
72
ReadAllowList(const std::string & filename)73 std::vector<FtraceEventName> ReadAllowList(const std::string& filename) {
74 std::string line;
75 std::vector<FtraceEventName> lines;
76 std::ifstream fin(filename, std::ios::in);
77 if (!fin) {
78 fprintf(stderr, "failed to open event list %s\n", filename.c_str());
79 return lines;
80 }
81 while (std::getline(fin, line)) {
82 if (!StartsWith(line, "#"))
83 lines.emplace_back(FtraceEventName(line));
84 }
85 return lines;
86 }
87
ToProtoFields(const FtraceEvent & format)88 std::vector<Proto::Field> ToProtoFields(const FtraceEvent& format) {
89 std::vector<Proto::Field> ret;
90 for (size_t i = 0; i < format.fields.size(); i++) {
91 const FtraceEvent::Field& field = format.fields[i];
92 std::string name = GetNameFromTypeAndName(field.type_and_name);
93 // Skip tracepoint fields whose names cannot be used as an identifier in the
94 // generated C++ code due to stdlib header conflicts.
95 if (name == "" || name == "sa_handler" || name == "errno")
96 continue;
97 ProtoType type = InferProtoType(field);
98 if (type.type == ProtoType::INVALID)
99 continue;
100 ret.push_back({type, std::move(name), static_cast<uint32_t>(i)});
101 }
102 return ret;
103 }
104
GenerateFtraceEventProto(const std::vector<FtraceEventName> & raw_eventlist,const std::set<std::string> & groups,std::ostream * fout)105 void GenerateFtraceEventProto(const std::vector<FtraceEventName>& raw_eventlist,
106 const std::set<std::string>& groups,
107 std::ostream* fout) {
108 *fout << kCopyrightHeader;
109 *fout << "// Autogenerated by:\n";
110 *fout << std::string("// ") + __FILE__ + "\n";
111 *fout << "// Do not edit.\n\n";
112 *fout << R"(syntax = "proto2";)"
113 << "\n\n";
114
115 for (const std::string& group : groups) {
116 *fout << R"(import "protos/perfetto/trace/ftrace/)" << group
117 << R"(.proto";)"
118 << "\n";
119 }
120 *fout << "import \"protos/perfetto/trace/ftrace/generic.proto\";\n";
121 *fout << "\n";
122 *fout << "package perfetto.protos;\n\n";
123 *fout << R"(message FtraceEvent {
124 // Timestamp in nanoseconds using .../tracing/trace_clock.
125 optional uint64 timestamp = 1;
126
127 // Kernel pid (do not confuse with userspace pid aka tgid).
128 optional uint32 pid = 2;
129
130 // Not populated in actual traces. Wire format might change.
131 // Placeholder declaration so that the ftrace parsing code accepts the
132 // existence of this common field. If this becomes needed for all events:
133 // consider merging with common_preempt_count to avoid extra proto tags.
134 optional uint32 common_flags = 5;
135
136 oneof event {
137 )";
138
139 int i = 3;
140 for (const FtraceEventName& event : raw_eventlist) {
141 if (!event.valid()) {
142 *fout << " // removed field with id " << i << ";\n";
143 ++i;
144 continue;
145 }
146
147 std::string field_name =
148 EventNameToProtoFieldName(event.group(), event.name());
149 std::string type_name = EventNameToProtoName(event.group(), event.name());
150
151 // " " (indent) + TypeName + " " + field_name + " = " + 123 + ";"
152 if (4 + type_name.size() + 1 + field_name.size() + 3 + 3 + 1 <= 80) {
153 // Everything fits in one line:
154 *fout << " " << type_name << " " << field_name << " = " << i << ";\n";
155 } else if (4 + type_name.size() + 1 + field_name.size() + 2 <= 80) {
156 // Everything fits except the field id:
157 *fout << " " << type_name << " " << field_name << " =\n " << i
158 << ";\n";
159 } else {
160 // Nothing fits:
161 *fout << " " << type_name << "\n " << field_name << " = " << i
162 << ";\n";
163 }
164 ++i;
165 // We cannot depend on the proto file to get this number because
166 // it would cause a dependency cycle between this generator and the
167 // generated code.
168 if (i == 327) {
169 *fout << " GenericFtraceEvent generic = " << i << ";\n";
170 ++i;
171 }
172 }
173 *fout << " }\n";
174 *fout << "}\n";
175 }
176
177 // Generates section of event_info.cc for a single event.
SingleEventInfo(perfetto::Proto proto,const std::string & group,const uint32_t proto_field_id)178 std::string SingleEventInfo(perfetto::Proto proto,
179 const std::string& group,
180 const uint32_t proto_field_id) {
181 std::string s = "{";
182 s += "\"" + proto.event_name + "\", ";
183 s += "\"" + group + "\", ";
184
185 // Vector of fields.
186 s += "{ ";
187 for (const auto& field : proto.SortedFields()) {
188 // Ignore the "ip" field from print events. This field has not proven
189 // particularly useful and takes up a large amount of space (30% of total
190 // PrintFtraceEvent size and up to 12% of the entire trace in some
191 // configurations)
192 if (group == "ftrace" && proto.event_name == "print" && field->name == "ip")
193 continue;
194 // Ignore the "nid" field. On new kernels, this field has a type that we
195 // don't know how to parse. See b/281660544
196 if (group == "f2fs" && proto.event_name == "f2fs_truncate_partial_nodes" &&
197 field->name == "nid")
198 continue;
199 s += "{";
200 s += "kUnsetOffset, ";
201 s += "kUnsetSize, ";
202 s += "FtraceFieldType::kInvalidFtraceFieldType, ";
203 s += "\"" + field->name + "\", ";
204 s += std::to_string(field->number) + ", ";
205 s += "ProtoSchemaType::k" + ToCamelCase(field->type.ToString()) + ", ";
206 s += "TranslationStrategy::kInvalidTranslationStrategy";
207 s += "}, ";
208 }
209 s += "}, ";
210
211 s += "kUnsetFtraceId, ";
212 s += std::to_string(proto_field_id) + ", ";
213 s += "kUnsetSize";
214 s += "}";
215 return s;
216 }
217
218 // This will generate the event_info.cc file for the listed protos.
GenerateEventInfo(const std::vector<std::string> & events_info,std::ostream * fout)219 void GenerateEventInfo(const std::vector<std::string>& events_info,
220 std::ostream* fout) {
221 std::string s = kCopyrightHeader;
222 s += "// Autogenerated by:\n";
223 s += std::string("// ") + __FILE__ + "\n";
224 s += "// Do not edit.\n";
225 s += R"(
226 #include "perfetto/protozero/proto_utils.h"
227 #include "src/traced/probes/ftrace/event_info.h"
228
229 namespace perfetto {
230
231 using protozero::proto_utils::ProtoSchemaType;
232
233 std::vector<Event> GetStaticEventInfo() {
234 static constexpr uint16_t kUnsetOffset = 0;
235 static constexpr uint16_t kUnsetSize = 0;
236 static constexpr uint16_t kUnsetFtraceId = 0;
237 return
238 )";
239
240 s += " {";
241 for (const auto& event : events_info) {
242 s += event;
243 s += ",\n";
244 }
245 s += "};";
246
247 s += R"(
248 }
249
250 } // namespace perfetto
251 )";
252
253 *fout << s;
254 }
255
ProtoHeader()256 std::string ProtoHeader() {
257 std::string s = "// Autogenerated by:\n";
258 s += std::string("// ") + __FILE__ + "\n";
259 s += "// Do not edit.\n";
260
261 s += R"(
262 syntax = "proto2";
263 package perfetto.protos;
264
265 )";
266 return s;
267 }
268
269 } // namespace perfetto
270