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 // Tracks the state for updating sched slice and thread state tables. 80 struct Thread { ThreadThread81 explicit Thread(uint64_t tid) : info{0, tid} {} 82 83 FuchsiaThreadInfo info; 84 int64_t last_ts{0}; 85 std::optional<tables::SchedSliceTable::RowNumber> last_slice_row; 86 std::optional<tables::ThreadStateTable::RowNumber> last_state_row; 87 }; 88 89 void SwitchFrom(Thread* thread, 90 int64_t ts, 91 uint32_t cpu, 92 uint32_t thread_state); 93 void SwitchTo(Thread* thread, int64_t ts, uint32_t cpu, int32_t weight); 94 void Wake(Thread* thread, int64_t ts, uint32_t cpu); 95 96 // Allocates or returns an existing Thread instance for the given tid. GetThread(uint64_t tid)97 Thread& GetThread(uint64_t tid) { 98 auto search = threads_.find(tid); 99 if (search != threads_.end()) { 100 return search->second; 101 } 102 auto result = threads_.emplace(tid, tid); 103 return result.first->second; 104 } 105 106 void ParseRecord(TraceBlobView); 107 void RegisterProvider(uint32_t, std::string); 108 StringId IdForOutgoingThreadState(uint32_t state); 109 110 TraceProcessorContext* const context_; 111 std::vector<uint8_t> leftover_bytes_; 112 113 // Proto reader creates state that the blobs it emits reference, so the 114 // proto_reader needs to live for as long as the tokenizer. 115 ProtoTraceReader proto_reader_; 116 std::vector<uint8_t> proto_trace_data_; 117 118 std::unordered_map<uint32_t, std::unique_ptr<ProviderInfo>> providers_; 119 ProviderInfo* current_provider_; 120 121 // Interned string ids for the relevant thread states. 122 StringId running_string_id_; 123 StringId runnable_string_id_; 124 StringId preempted_string_id_; 125 StringId waking_string_id_; 126 StringId blocked_string_id_; 127 StringId suspended_string_id_; 128 StringId exit_dying_string_id_; 129 StringId exit_dead_string_id_; 130 131 // Interned string ids for record arguments. 132 StringId incoming_weight_id_; 133 StringId outgoing_weight_id_; 134 StringId weight_id_; 135 StringId process_id_; 136 137 // Map from tid to Thread. 138 std::unordered_map<uint64_t, Thread> threads_; 139 }; 140 141 } // namespace perfetto::trace_processor 142 143 #endif // SRC_TRACE_PROCESSOR_IMPORTERS_FUCHSIA_FUCHSIA_TRACE_TOKENIZER_H_ 144