1 /* 2 * Copyright (C) 2019 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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_FUCHSIA_FUCHSIA_TRACE_TOKENIZER_H_ 18 #define SRC_TRACE_PROCESSOR_IMPORTERS_FUCHSIA_FUCHSIA_TRACE_TOKENIZER_H_ 19 20 #include <cstdint> 21 #include <memory> 22 #include <optional> 23 #include <string> 24 #include <unordered_map> 25 #include <vector> 26 27 #include "perfetto/base/status.h" 28 #include "src/trace_processor/importers/common/chunked_trace_reader.h" 29 #include "src/trace_processor/importers/fuchsia/fuchsia_record.h" 30 #include "src/trace_processor/importers/proto/proto_trace_reader.h" 31 #include "src/trace_processor/storage/trace_storage.h" 32 #include "src/trace_processor/tables/sched_tables_py.h" 33 #include "src/trace_processor/util/trace_type.h" 34 35 namespace perfetto::trace_processor { 36 37 class TraceProcessorContext; 38 39 // The Fuchsia trace format is documented at 40 // https://fuchsia.googlesource.com/fuchsia/+/HEAD/docs/development/tracing/trace-format/README.md 41 class FuchsiaTraceTokenizer : public ChunkedTraceReader { 42 public: 43 static constexpr TraceType kTraceType = TraceType::kFuchsiaTraceType; 44 explicit FuchsiaTraceTokenizer(TraceProcessorContext*); 45 ~FuchsiaTraceTokenizer() override; 46 47 // ChunkedTraceReader implementation 48 base::Status Parse(TraceBlobView) override; 49 base::Status NotifyEndOfFile() override; 50 51 private: 52 struct ProviderInfo { 53 std::string name; 54 55 std::unordered_map<uint64_t, StringId> string_table; 56 std::unordered_map<uint64_t, FuchsiaThreadInfo> thread_table; 57 58 // Returns a StringId for the given FXT string ref id. GetStringProviderInfo59 StringId GetString(uint64_t string_ref) { 60 auto search = string_table.find(string_ref); 61 if (search != string_table.end()) { 62 return search->second; 63 } 64 return kNullStringId; 65 } 66 67 // Returns a FuchsiaThreadInfo for the given FXT thread ref id. GetThreadProviderInfo68 FuchsiaThreadInfo GetThread(uint64_t thread_ref) { 69 auto search = thread_table.find(thread_ref); 70 if (search != thread_table.end()) { 71 return search->second; 72 } 73 return {0, 0}; 74 } 75 76 uint64_t ticks_per_second = 1000000000; 77 }; 78 79 void ParseRecord(TraceBlobView); 80 void RegisterProvider(uint32_t, std::string); 81 82 TraceProcessorContext* const context_; 83 std::vector<uint8_t> leftover_bytes_; 84 85 // Proto reader creates state that the blobs it emits reference, so the 86 // proto_reader needs to live for as long as the tokenizer. 87 ProtoTraceReader proto_reader_; 88 std::vector<uint8_t> proto_trace_data_; 89 90 std::unordered_map<uint32_t, std::unique_ptr<ProviderInfo>> providers_; 91 ProviderInfo* current_provider_; 92 93 // Interned string ids for record arguments. 94 StringId process_id_; 95 }; 96 97 } // namespace perfetto::trace_processor 98 99 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_FUCHSIA_FUCHSIA_TRACE_TOKENIZER_H_ 100