aboutsummaryrefslogtreecommitdiff
path: root/src/tools/ftrace_proto_gen/ftrace_proto_gen.cc
blob: f692373139fc53f2ad2ecf502ce7a87df5137a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "src/tools/ftrace_proto_gen/ftrace_proto_gen.h"

#include <algorithm>
#include <fstream>
#include <regex>

#include "perfetto/base/logging.h"
#include "perfetto/ext/base/file_utils.h"
#include "perfetto/ext/base/pipe.h"
#include "perfetto/ext/base/string_splitter.h"
#include "perfetto/ext/base/string_utils.h"

namespace perfetto {

namespace {

const char kCopyrightHeader[] = R"(/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

)";

}  // namespace

using base::Contains;
using base::StartsWith;

std::string EventNameToProtoFieldName(const std::string& group,
                                      const std::string& name) {
  std::string event_name = (name == "0") ? "zero" : name;
  // These groups have events where the name alone conflicts with an existing
  // proto:
  if (group == "sde" || group == "g2d" || group == "dpu" || group == "mali" ||
      group == "lwis") {
    event_name = group + "_" + event_name;
  }
  return event_name;
}

std::string EventNameToProtoName(const std::string& group,
                                 const std::string& name) {
  return ToCamelCase(EventNameToProtoFieldName(group, name)) + "FtraceEvent";
}

std::vector<FtraceEventName> ReadAllowList(const std::string& filename) {
  std::string line;
  std::vector<FtraceEventName> lines;
  std::ifstream fin(filename, std::ios::in);
  if (!fin) {
    fprintf(stderr, "failed to open event list %s\n", filename.c_str());
    return lines;
  }
  while (std::getline(fin, line)) {
    if (!StartsWith(line, "#"))
      lines.emplace_back(FtraceEventName(line));
  }
  return lines;
}

std::vector<Proto::Field> ToProtoFields(const FtraceEvent& format) {
  std::vector<Proto::Field> ret;
  for (size_t i = 0; i < format.fields.size(); i++) {
    const FtraceEvent::Field& field = format.fields[i];
    std::string name = GetNameFromTypeAndName(field.type_and_name);
    // Skip tracepoint fields whose names cannot be used as an identifier in the
    // generated C++ code due to stdlib header conflicts.
    if (name == "" || name == "sa_handler" || name == "errno")
      continue;
    ProtoType type = InferProtoType(field);
    if (type.type == ProtoType::INVALID)
      continue;
    ret.push_back({type, std::move(name), static_cast<uint32_t>(i)});
  }
  return ret;
}

void GenerateFtraceEventProto(const std::vector<FtraceEventName>& raw_eventlist,
                              const std::set<std::string>& groups,
                              std::ostream* fout) {
  *fout << kCopyrightHeader;
  *fout << "// Autogenerated by:\n";
  *fout << std::string("// ") + __FILE__ + "\n";
  *fout << "// Do not edit.\n\n";
  *fout << R"(syntax = "proto2";)"
        << "\n\n";

  for (const std::string& group : groups) {
    *fout << R"(import "protos/perfetto/trace/ftrace/)" << group
          << R"(.proto";)"
          << "\n";
  }
  *fout << "import \"protos/perfetto/trace/ftrace/generic.proto\";\n";
  *fout << "\n";
  *fout << "package perfetto.protos;\n\n";
  *fout << R"(message FtraceEvent {
  // Timestamp in nanoseconds using .../tracing/trace_clock.
  optional uint64 timestamp = 1;

  // Kernel pid (do not confuse with userspace pid aka tgid).
  optional uint32 pid = 2;

  // Not populated in actual traces. Wire format might change.
  // Placeholder declaration so that the ftrace parsing code accepts the
  // existence of this common field. If this becomes needed for all events:
  // consider merging with common_preempt_count to avoid extra proto tags.
  optional uint32 common_flags = 5;

  oneof event {
)";

  int i = 3;
  for (const FtraceEventName& event : raw_eventlist) {
    if (!event.valid()) {
      *fout << "    // removed field with id " << i << ";\n";
      ++i;
      continue;
    }

    std::string field_name =
        EventNameToProtoFieldName(event.group(), event.name());
    std::string type_name = EventNameToProtoName(event.group(), event.name());

    // "    " (indent) + TypeName + " " + field_name + " = " + 123 + ";"
    if (4 + type_name.size() + 1 + field_name.size() + 3 + 3 + 1 <= 80) {
      // Everything fits in one line:
      *fout << "    " << type_name << " " << field_name << " = " << i << ";\n";
    } else if (4 + type_name.size() + 1 + field_name.size() + 2 <= 80) {
      // Everything fits except the field id:
      *fout << "    " << type_name << " " << field_name << " =\n        " << i
            << ";\n";
    } else {
      // Nothing fits:
      *fout << "    " << type_name << "\n        " << field_name << " = " << i
            << ";\n";
    }
    ++i;
    // We cannot depend on the proto file to get this number because
    // it would cause a dependency cycle between this generator and the
    // generated code.
    if (i == 327) {
      *fout << "    GenericFtraceEvent generic = " << i << ";\n";
      ++i;
    }
  }
  *fout << "  }\n";
  *fout << "}\n";
}

// Generates section of event_info.cc for a single event.
std::string SingleEventInfo(perfetto::Proto proto,
                            const std::string& group,
                            const uint32_t proto_field_id) {
  std::string s = "{";
  s += "\"" + proto.event_name + "\", ";
  s += "\"" + group + "\", ";

  // Vector of fields.
  s += "{ ";
  for (const auto& field : proto.SortedFields()) {
    // Ignore the "ip" field from print events. This field has not proven
    // particularly useful and takes up a large amount of space (30% of total
    // PrintFtraceEvent size and up to 12% of the entire trace in some
    // configurations)
    if (group == "ftrace" && proto.event_name == "print" && field->name == "ip")
      continue;
    s += "{";
    s += "kUnsetOffset, ";
    s += "kUnsetSize, ";
    s += "FtraceFieldType::kInvalidFtraceFieldType, ";
    s += "\"" + field->name + "\", ";
    s += std::to_string(field->number) + ", ";
    s += "ProtoSchemaType::k" + ToCamelCase(field->type.ToString()) + ", ";
    s += "TranslationStrategy::kInvalidTranslationStrategy";
    s += "}, ";
  }
  s += "}, ";

  s += "kUnsetFtraceId, ";
  s += std::to_string(proto_field_id) + ", ";
  s += "kUnsetSize";
  s += "}";
  return s;
}

// This will generate the event_info.cc file for the listed protos.
void GenerateEventInfo(const std::vector<std::string>& events_info,
                       std::ostream* fout) {
  std::string s = kCopyrightHeader;
  s += "// Autogenerated by:\n";
  s += std::string("// ") + __FILE__ + "\n";
  s += "// Do not edit.\n";
  s += R"(
#include "perfetto/protozero/proto_utils.h"
#include "src/traced/probes/ftrace/event_info.h"

namespace perfetto {

using protozero::proto_utils::ProtoSchemaType;

std::vector<Event> GetStaticEventInfo() {
  static constexpr uint16_t kUnsetOffset = 0;
  static constexpr uint16_t kUnsetSize = 0;
  static constexpr uint16_t kUnsetFtraceId = 0;
  return
)";

  s += " {";
  for (const auto& event : events_info) {
    s += event;
    s += ",\n";
  }
  s += "};";

  s += R"(
}

}  // namespace perfetto
)";

  *fout << s;
}

std::string ProtoHeader() {
  std::string s = "// Autogenerated by:\n";
  s += std::string("// ") + __FILE__ + "\n";
  s += "// Do not edit.\n";

  s += R"(
syntax = "proto2";
package perfetto.protos;

)";
  return s;
}

}  // namespace perfetto