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 "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 event_name = group + "_" + event_name;
63 }
64 return event_name;
65 }
66
EventNameToProtoName(const std::string & group,const std::string & name)67 std::string EventNameToProtoName(const std::string& group,
68 const std::string& name) {
69 return ToCamelCase(EventNameToProtoFieldName(group, name)) + "FtraceEvent";
70 }
71
ReadAllowList(const std::string & filename)72 std::vector<FtraceEventName> ReadAllowList(const std::string& filename) {
73 std::string line;
74 std::vector<FtraceEventName> lines;
75 std::ifstream fin(filename, std::ios::in);
76 if (!fin) {
77 fprintf(stderr, "failed to open event list %s\n", filename.c_str());
78 return lines;
79 }
80 while (std::getline(fin, line)) {
81 if (!StartsWith(line, "#"))
82 lines.emplace_back(FtraceEventName(line));
83 }
84 return lines;
85 }
86
GenerateProto(const std::string & group,const FtraceEvent & format,Proto * proto_out)87 bool GenerateProto(const std::string& group,
88 const FtraceEvent& format,
89 Proto* proto_out) {
90 proto_out->name = EventNameToProtoName(group, format.name);
91 proto_out->event_name = format.name;
92 std::set<std::string> seen;
93 // TODO(hjd): We should be cleverer about id assignment.
94 uint32_t i = 1;
95 for (const FtraceEvent::Field& field : format.fields) {
96 std::string name = GetNameFromTypeAndName(field.type_and_name);
97 // TODO(hjd): Handle dup names.
98 // sa_handler is problematic because glib headers redefine it at the
99 // preprocessor level. It's impossible to have a variable or a function
100 // called sa_handler. On the good side, we realistically don't care about
101 // this field, it's just easier to skip it.
102 if (name == "" || seen.count(name) || name == "sa_handler" ||
103 name == "errno")
104 continue;
105 seen.insert(name);
106 ProtoType type = InferProtoType(field);
107 // Check we managed to infer a type.
108 if (type.type == ProtoType::INVALID)
109 continue;
110 Proto::Field protofield{std::move(type), name, i};
111 proto_out->AddField(std::move(protofield));
112 i++;
113 }
114
115 return true;
116 }
117
GenerateFtraceEventProto(const std::vector<FtraceEventName> & raw_eventlist,const std::set<std::string> & groups,std::ostream * fout)118 void GenerateFtraceEventProto(const std::vector<FtraceEventName>& raw_eventlist,
119 const std::set<std::string>& groups,
120 std::ostream* fout) {
121 *fout << kCopyrightHeader;
122 *fout << "// Autogenerated by:\n";
123 *fout << std::string("// ") + __FILE__ + "\n";
124 *fout << "// Do not edit.\n\n";
125 *fout << R"(syntax = "proto2";)"
126 << "\n\n";
127
128 for (const std::string& group : groups) {
129 *fout << R"(import "protos/perfetto/trace/ftrace/)" << group
130 << R"(.proto";)"
131 << "\n";
132 }
133 *fout << "import \"protos/perfetto/trace/ftrace/generic.proto\";\n";
134 *fout << "\n";
135 *fout << "package perfetto.protos;\n\n";
136 *fout << R"(message FtraceEvent {
137 // Nanoseconds since an epoch.
138 // Epoch is configurable by writing into trace_clock.
139 // By default this timestamp is CPU local.
140 // TODO: Figure out a story for reconciling the various clocks.
141 optional uint64 timestamp = 1;
142
143 // Kernel pid (do not confuse with userspace pid aka tgid)
144 optional uint32 pid = 2;
145
146 oneof event {
147 )";
148
149 int i = 3;
150 for (const FtraceEventName& event : raw_eventlist) {
151 if (!event.valid()) {
152 *fout << " // removed field with id " << i << ";\n";
153 ++i;
154 continue;
155 }
156
157 std::string field_name =
158 EventNameToProtoFieldName(event.group(), event.name());
159 std::string type_name = EventNameToProtoName(event.group(), event.name());
160
161 // " " (indent) + TypeName + " " + field_name + " = " + 123 + ";"
162 if (4 + type_name.size() + 1 + field_name.size() + 3 + 3 + 1 <= 80) {
163 // Everything fits in one line:
164 *fout << " " << type_name << " " << field_name << " = " << i << ";\n";
165 } else if (4 + type_name.size() + 1 + field_name.size() + 2 <= 80) {
166 // Everything fits except the field id:
167 *fout << " " << type_name << " " << field_name << " =\n " << i
168 << ";\n";
169 } else {
170 // Nothing fits:
171 *fout << " " << type_name << "\n " << field_name << " = " << i
172 << ";\n";
173 }
174 ++i;
175 // We cannot depend on the proto file to get this number because
176 // it would cause a dependency cycle between this generator and the
177 // generated code.
178 if (i == 327) {
179 *fout << " GenericFtraceEvent generic = " << i << ";\n";
180 ++i;
181 }
182 }
183 *fout << " }\n";
184 *fout << "}\n";
185 }
186
187 // Generates section of event_info.cc for a single event.
SingleEventInfo(perfetto::Proto proto,const std::string & group,const uint32_t proto_field_id)188 std::string SingleEventInfo(perfetto::Proto proto,
189 const std::string& group,
190 const uint32_t proto_field_id) {
191 std::string s = "{";
192 s += "\"" + proto.event_name + "\", ";
193 s += "\"" + group + "\", ";
194
195 // Vector of fields.
196 s += "{ ";
197 for (const auto& field : proto.SortedFields()) {
198 // Ignore the "ip" field from print events. This field has not proven
199 // particularly useful and takes up a large amount of space (30% of total
200 // PrintFtraceEvent size and up to 12% of the entire trace in some
201 // configurations)
202 if (group == "ftrace" && proto.event_name == "print" && field->name == "ip")
203 continue;
204 s += "{";
205 s += "kUnsetOffset, ";
206 s += "kUnsetSize, ";
207 s += "FtraceFieldType::kInvalidFtraceFieldType, ";
208 s += "\"" + field->name + "\", ";
209 s += std::to_string(field->number) + ", ";
210 s += "ProtoSchemaType::k" + ToCamelCase(field->type.ToString()) + ", ";
211 s += "TranslationStrategy::kInvalidTranslationStrategy";
212 s += "}, ";
213 }
214 s += "}, ";
215
216 s += "kUnsetFtraceId, ";
217 s += std::to_string(proto_field_id) + ", ";
218 s += "kUnsetSize";
219 s += "}";
220 return s;
221 }
222
223 // This will generate the event_info.cc file for the listed protos.
GenerateEventInfo(const std::vector<std::string> & events_info,std::ostream * fout)224 void GenerateEventInfo(const std::vector<std::string>& events_info,
225 std::ostream* fout) {
226 std::string s = kCopyrightHeader;
227 s += "// Autogenerated by:\n";
228 s += std::string("// ") + __FILE__ + "\n";
229 s += "// Do not edit.\n";
230 s += R"(
231 #include "perfetto/protozero/proto_utils.h"
232 #include "src/traced/probes/ftrace/event_info.h"
233
234 namespace perfetto {
235
236 using protozero::proto_utils::ProtoSchemaType;
237
238 std::vector<Event> GetStaticEventInfo() {
239 static constexpr uint16_t kUnsetOffset = 0;
240 static constexpr uint16_t kUnsetSize = 0;
241 static constexpr uint16_t kUnsetFtraceId = 0;
242 return
243 )";
244
245 s += " {";
246 for (const auto& event : events_info) {
247 s += event;
248 s += ",\n";
249 }
250 s += "};";
251
252 s += R"(
253 }
254
255 } // namespace perfetto
256 )";
257
258 *fout << s;
259 }
260
ProtoHeader()261 std::string ProtoHeader() {
262 std::string s = "// Autogenerated by:\n";
263 s += std::string("// ") + __FILE__ + "\n";
264 s += "// Do not edit.\n";
265
266 s += R"(
267 syntax = "proto2";
268 package perfetto.protos;
269
270 )";
271 return s;
272 }
273
274 } // namespace perfetto
275