• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/trace_processor/importers/common/chunked_trace_reader.h"
21 #include "src/trace_processor/importers/fuchsia/fuchsia_trace_utils.h"
22 #include "src/trace_processor/importers/proto/proto_trace_reader.h"
23 #include "src/trace_processor/storage/trace_storage.h"
24 #include "src/trace_processor/types/task_state.h"
25 
26 namespace perfetto {
27 namespace trace_processor {
28 
29 class TraceProcessorContext;
30 
31 // The Fuchsia trace format is documented at
32 // https://fuchsia.googlesource.com/fuchsia/+/HEAD/docs/development/tracing/trace-format/README.md
33 class FuchsiaTraceTokenizer : public ChunkedTraceReader {
34  public:
35   explicit FuchsiaTraceTokenizer(TraceProcessorContext*);
36   ~FuchsiaTraceTokenizer() override;
37 
38   // ChunkedTraceReader implementation
39   util::Status Parse(TraceBlobView) override;
40   void NotifyEndOfFile() override;
41 
42  private:
43   struct ProviderInfo {
44     std::string name;
45 
46     std::unordered_map<uint64_t, StringId> string_table;
47     std::unordered_map<uint64_t, FuchsiaThreadInfo> thread_table;
48 
49     // Returns a StringId for the given FXT string ref id.
GetStringProviderInfo50     StringId GetString(uint64_t string_ref) {
51       auto search = string_table.find(string_ref);
52       if (search != string_table.end()) {
53         return search->second;
54       }
55       return kNullStringId;
56     }
57 
58     // Returns a FuchsiaThreadInfo for the given FXT thread ref id.
GetThreadProviderInfo59     FuchsiaThreadInfo GetThread(uint64_t thread_ref) {
60       auto search = thread_table.find(thread_ref);
61       if (search != thread_table.end()) {
62         return search->second;
63       }
64       return {0, 0};
65     }
66 
67     uint64_t ticks_per_second = 1000000000;
68   };
69 
70   // Tracks the state for updating sched slice and thread state tables.
71   struct Thread {
ThreadThread72     explicit Thread(uint64_t tid) : info{0, tid} {}
73 
74     FuchsiaThreadInfo info;
75     int64_t last_ts{0};
76     std::optional<tables::SchedSliceTable::RowNumber> last_slice_row;
77     std::optional<tables::ThreadStateTable::RowNumber> last_state_row;
78   };
79 
80   void SwitchFrom(Thread* thread,
81                   int64_t ts,
82                   uint32_t cpu,
83                   uint32_t thread_state);
84   void SwitchTo(Thread* thread, int64_t ts, uint32_t cpu, int32_t weight);
85   void Wake(Thread* thread, int64_t ts, uint32_t cpu);
86 
87   // Allocates or returns an existing Thread instance for the given tid.
GetThread(uint64_t tid)88   Thread& GetThread(uint64_t tid) {
89     auto search = threads_.find(tid);
90     if (search != threads_.end()) {
91       return search->second;
92     }
93     auto result = threads_.emplace(tid, tid);
94     return result.first->second;
95   }
96 
97   void ParseRecord(TraceBlobView);
98   void RegisterProvider(uint32_t, std::string);
99   StringId IdForOutgoingThreadState(uint32_t state);
100 
101   TraceProcessorContext* const context_;
102   std::vector<uint8_t> leftover_bytes_;
103 
104   // Proto reader creates state that the blobs it emits reference, so the
105   // proto_reader needs to live for as long as the tokenizer.
106   ProtoTraceReader proto_reader_;
107   std::vector<uint8_t> proto_trace_data_;
108 
109   std::unordered_map<uint32_t, std::unique_ptr<ProviderInfo>> providers_;
110   ProviderInfo* current_provider_;
111 
112   // Interned string ids for the relevant thread states.
113   StringId running_string_id_;
114   StringId runnable_string_id_;
115   StringId preempted_string_id_;
116   StringId waking_string_id_;
117   StringId blocked_string_id_;
118   StringId suspended_string_id_;
119   StringId exit_dying_string_id_;
120   StringId exit_dead_string_id_;
121 
122   // Interned string ids for record arguments.
123   StringId incoming_weight_id_;
124   StringId outgoing_weight_id_;
125   StringId weight_id_;
126   StringId process_id_;
127 
128   // Map from tid to Thread.
129   std::unordered_map<uint64_t, Thread> threads_;
130 };
131 
132 }  // namespace trace_processor
133 }  // namespace perfetto
134 
135 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_FUCHSIA_FUCHSIA_TRACE_TOKENIZER_H_
136