blob: 0058527cf223f8e7372fbcfb428587dadacb149a [file] [log] [blame]
Hector Dearman72a49052017-11-08 14:12:55 +00001/*
2 * Copyright (C) 2017 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
Hector Dearman0d300332017-11-22 11:05:34 +000017#ifndef FTRACE_PROTO_TRANSLATION_TABLE_H_
18#define FTRACE_PROTO_TRANSLATION_TABLE_H_
Hector Dearman72a49052017-11-08 14:12:55 +000019
20#include <stdint.h>
21
Hector Dearman13a99302017-11-08 14:15:44 +000022#include <map>
23#include <memory>
Hector Dearman0d300332017-11-22 11:05:34 +000024#include <set>
Hector Dearman13a99302017-11-08 14:15:44 +000025#include <string>
26#include <vector>
27
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080028#include "perfetto_base/scoped_file.h"
Primiano Tucci2ee254a2017-11-15 00:38:48 +000029
Hector Dearman72a49052017-11-08 14:12:55 +000030namespace perfetto {
31
Hector Dearman0d300332017-11-22 11:05:34 +000032class FtraceProcfs;
33
Primiano Tucci5f48d7b2017-11-27 16:57:13 +000034namespace protos {
35namespace pbzero {
36class FtraceEventBundle;
37} // namespace pbzero
38} // namespace protos
39
Hector Dearman0d300332017-11-22 11:05:34 +000040class ProtoTranslationTable {
Hector Dearman72a49052017-11-08 14:12:55 +000041 public:
Hector Dearman13a99302017-11-08 14:15:44 +000042 enum FtraceFieldType {
43 kFtraceNumber = 0,
44 };
45
46 enum ProtoFieldType {
47 kProtoNumber = 0,
48 };
49
50 struct Field {
51 size_t ftrace_offset;
52 size_t ftrace_size;
53 FtraceFieldType ftrace_type;
54 size_t proto_field_id;
55 ProtoFieldType proto_field_type;
56 };
57
58 struct Event {
59 std::string name;
60 std::string group;
61 std::vector<Field> fields;
62 size_t ftrace_event_id;
63 size_t proto_field_id;
64 };
65
Hector Dearman0d300332017-11-22 11:05:34 +000066 static std::unique_ptr<ProtoTranslationTable> Create(
Hector Dearman0d300332017-11-22 11:05:34 +000067 const FtraceProcfs* ftrace_procfs);
68 ~ProtoTranslationTable();
Hector Dearman72a49052017-11-08 14:12:55 +000069
Hector Dearman0d300332017-11-22 11:05:34 +000070 ProtoTranslationTable(const std::vector<Event>& events,
71 std::vector<Field> common_fields);
Hector Dearmana4cb5642017-11-15 16:35:56 +000072
73 size_t largest_id() const { return largest_id_; }
74
Hector Dearman7d8bbfa2017-11-09 11:36:04 +000075 const std::vector<Field>& common_fields() const { return common_fields_; }
76
Hector Dearmana4cb5642017-11-15 16:35:56 +000077 const Event* GetEventByName(const std::string& name) const {
78 if (!name_to_event_.count(name))
79 return nullptr;
80 return name_to_event_.at(name);
81 }
82
83 const Event* GetEventById(size_t id) const {
84 if (id == 0 || id > largest_id_)
85 return nullptr;
86 if (!events_.at(id).ftrace_event_id)
87 return nullptr;
88 return &events_.at(id);
89 }
90
91 size_t EventNameToFtraceId(const std::string& name) const {
92 if (!name_to_event_.count(name))
93 return 0;
94 return name_to_event_.at(name)->ftrace_event_id;
95 }
96
Hector Dearman72a49052017-11-08 14:12:55 +000097 private:
Hector Dearman0d300332017-11-22 11:05:34 +000098 ProtoTranslationTable(const ProtoTranslationTable&) = delete;
99 ProtoTranslationTable& operator=(const ProtoTranslationTable&) = delete;
Hector Dearman13a99302017-11-08 14:15:44 +0000100
Hector Dearmana4cb5642017-11-15 16:35:56 +0000101 const std::vector<Event> events_;
102 size_t largest_id_;
103 std::map<std::string, const Event*> name_to_event_;
Hector Dearman13a99302017-11-08 14:15:44 +0000104 std::vector<Field> common_fields_;
Hector Dearman72a49052017-11-08 14:12:55 +0000105};
106
107} // namespace perfetto
108
Hector Dearman0d300332017-11-22 11:05:34 +0000109#endif // FTRACE_PROTO_TRANSLATION_TABLE_H_