Hector Dearman | 72a4905 | 2017-11-08 14:12:55 +0000 | [diff] [blame] | 1 | /* |
| 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 Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 17 | #ifndef FTRACE_PROTO_TRANSLATION_TABLE_H_ |
| 18 | #define FTRACE_PROTO_TRANSLATION_TABLE_H_ |
Hector Dearman | 72a4905 | 2017-11-08 14:12:55 +0000 | [diff] [blame] | 19 | |
| 20 | #include <stdint.h> |
| 21 | |
Hector Dearman | 13a9930 | 2017-11-08 14:15:44 +0000 | [diff] [blame] | 22 | #include <map> |
| 23 | #include <memory> |
Hector Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 24 | #include <set> |
Hector Dearman | 13a9930 | 2017-11-08 14:15:44 +0000 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 28 | #include "perfetto_base/scoped_file.h" |
Primiano Tucci | 2ee254a | 2017-11-15 00:38:48 +0000 | [diff] [blame] | 29 | |
Hector Dearman | 72a4905 | 2017-11-08 14:12:55 +0000 | [diff] [blame] | 30 | namespace perfetto { |
| 31 | |
Hector Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 32 | class FtraceProcfs; |
| 33 | |
Primiano Tucci | 5f48d7b | 2017-11-27 16:57:13 +0000 | [diff] [blame] | 34 | namespace protos { |
| 35 | namespace pbzero { |
| 36 | class FtraceEventBundle; |
| 37 | } // namespace pbzero |
| 38 | } // namespace protos |
| 39 | |
Hector Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 40 | class ProtoTranslationTable { |
Hector Dearman | 72a4905 | 2017-11-08 14:12:55 +0000 | [diff] [blame] | 41 | public: |
Hector Dearman | 13a9930 | 2017-11-08 14:15:44 +0000 | [diff] [blame] | 42 | 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 Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 66 | static std::unique_ptr<ProtoTranslationTable> Create( |
Hector Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 67 | const FtraceProcfs* ftrace_procfs); |
| 68 | ~ProtoTranslationTable(); |
Hector Dearman | 72a4905 | 2017-11-08 14:12:55 +0000 | [diff] [blame] | 69 | |
Hector Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 70 | ProtoTranslationTable(const std::vector<Event>& events, |
| 71 | std::vector<Field> common_fields); |
Hector Dearman | a4cb564 | 2017-11-15 16:35:56 +0000 | [diff] [blame] | 72 | |
| 73 | size_t largest_id() const { return largest_id_; } |
| 74 | |
Hector Dearman | 7d8bbfa | 2017-11-09 11:36:04 +0000 | [diff] [blame] | 75 | const std::vector<Field>& common_fields() const { return common_fields_; } |
| 76 | |
Hector Dearman | a4cb564 | 2017-11-15 16:35:56 +0000 | [diff] [blame] | 77 | 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 Dearman | 72a4905 | 2017-11-08 14:12:55 +0000 | [diff] [blame] | 97 | private: |
Hector Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 98 | ProtoTranslationTable(const ProtoTranslationTable&) = delete; |
| 99 | ProtoTranslationTable& operator=(const ProtoTranslationTable&) = delete; |
Hector Dearman | 13a9930 | 2017-11-08 14:15:44 +0000 | [diff] [blame] | 100 | |
Hector Dearman | a4cb564 | 2017-11-15 16:35:56 +0000 | [diff] [blame] | 101 | const std::vector<Event> events_; |
| 102 | size_t largest_id_; |
| 103 | std::map<std::string, const Event*> name_to_event_; |
Hector Dearman | 13a9930 | 2017-11-08 14:15:44 +0000 | [diff] [blame] | 104 | std::vector<Field> common_fields_; |
Hector Dearman | 72a4905 | 2017-11-08 14:12:55 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace perfetto |
| 108 | |
Hector Dearman | 0d30033 | 2017-11-22 11:05:34 +0000 | [diff] [blame] | 109 | #endif // FTRACE_PROTO_TRANSLATION_TABLE_H_ |