Home | History | Annotate | Download | only in ftrace_proto_gen
      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_descriptor_gen.h"
     18 
     19 namespace perfetto {
     20 
     21 void GenerateFtraceDescriptors(
     22     const google::protobuf::DescriptorPool& descriptor_pool,
     23     std::ostream* fout) {
     24   const google::protobuf::Descriptor* ftrace_event =
     25       descriptor_pool.FindMessageTypeByName("perfetto.protos.FtraceEvent");
     26   const google::protobuf::OneofDescriptor* one_of_event =
     27       ftrace_event->FindOneofByName("event");
     28 
     29   // Find max id for any ftrace event.
     30   int max_id = 0;
     31   for (int i = 0; i < one_of_event->field_count(); i++)
     32     max_id = std::max(max_id, one_of_event->field(i)->number());
     33 
     34   *fout << "// Autogenerated by:\n";
     35   *fout << std::string("// ") + __FILE__ + "\n";
     36   *fout << "// Do not edit.\n";
     37   *fout << R"(
     38   #include "src/trace_processor/ftrace_descriptors.h"
     39 
     40   namespace perfetto {
     41   namespace trace_processor {
     42   namespace {
     43 
     44   std::array<MessageDescriptor,
     45   )";
     46   *fout << std::to_string(max_id + 1) + "> descriptors{{";
     47 
     48   for (int i = 0; i <= max_id; i++) {
     49     const google::protobuf::FieldDescriptor* event =
     50         ftrace_event->FindFieldByNumber(i);
     51     // Skip events that don't exist or are not messages. (Proxy for events)
     52     if (!event ||
     53         event->type() != google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
     54       *fout << "{nullptr, 0, {}},";
     55       continue;
     56     }
     57 
     58     const auto* event_descriptor = event->message_type();
     59 
     60     // Find the max field id in the event.
     61     int max_field_id = 0;
     62     for (int j = 0; j < event_descriptor->field_count(); j++)
     63       max_field_id =
     64           std::max(max_field_id, event_descriptor->field(j)->number());
     65 
     66     *fout << "{\"" + event->name() + "\", " << max_field_id << ", "
     67           << "{";
     68 
     69     for (int j = 0; j <= max_field_id; j++) {
     70       const auto* field = event_descriptor->FindFieldByNumber(j);
     71       // Skip fields that don't exist or are nested messages.
     72       if (!field ||
     73           field->type() == google::protobuf::FieldDescriptor::TYPE_MESSAGE) {
     74         *fout << "{},";
     75         continue;
     76       }
     77       ProtoType type = ProtoType::FromDescriptor(field->type());
     78       *fout << "{\"" + field->name() + "\", ProtoSchemaType::k" +
     79                    ToCamelCase(type.ToString()) + "},";
     80     }
     81     *fout << "},\n},";
     82   }
     83   *fout << "}};\n";
     84   *fout << R"(
     85   } // namespace
     86 
     87   MessageDescriptor* GetMessageDescriptorForId(size_t id) {
     88     PERFETTO_CHECK(id < descriptors.size());
     89     return &descriptors[id];
     90   }
     91 
     92   size_t GetDescriptorsSize() {
     93     return descriptors.size();
     94   }
     95   )";
     96   *fout << "} // namespace trace_processor\n} // namespace perfetto\n";
     97 }
     98 
     99 }  // namespace perfetto
    100