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 #include "src/trace_processor/fuchsia_trace_parser.h"
18
19 #include "src/trace_processor/process_tracker.h"
20 #include "src/trace_processor/slice_tracker.h"
21
22 namespace perfetto {
23 namespace trace_processor {
24
25 namespace {
26 // Record Types
27 constexpr uint32_t kEvent = 4;
28
29 // Event Types
30 constexpr uint32_t kDurationBegin = 2;
31 constexpr uint32_t kDurationEnd = 3;
32 constexpr uint32_t kDurationComplete = 4;
33 } // namespace
34
FuchsiaTraceParser(TraceProcessorContext * context)35 FuchsiaTraceParser::FuchsiaTraceParser(TraceProcessorContext* context)
36 : context_(context) {}
37
38 FuchsiaTraceParser::~FuchsiaTraceParser() = default;
39
ParseFtracePacket(uint32_t,int64_t,TraceSorter::TimestampedTracePiece)40 void FuchsiaTraceParser::ParseFtracePacket(uint32_t,
41 int64_t,
42 TraceSorter::TimestampedTracePiece) {
43 PERFETTO_FATAL("Fuchsia Trace Parser cannot handle ftrace packets.");
44 }
45
ParseTracePacket(int64_t,TraceSorter::TimestampedTracePiece ttp)46 void FuchsiaTraceParser::ParseTracePacket(
47 int64_t,
48 TraceSorter::TimestampedTracePiece ttp) {
49 PERFETTO_DCHECK(ttp.fuchsia_provider_view != nullptr);
50
51 // The timestamp is also present in the record, so we'll ignore the one passed
52 // as an argument.
53 const uint64_t* current =
54 reinterpret_cast<const uint64_t*>(ttp.blob_view.data());
55 FuchsiaProviderView* provider_view = ttp.fuchsia_provider_view.get();
56 ProcessTracker* procs = context_->process_tracker.get();
57 SliceTracker* slices = context_->slice_tracker.get();
58
59 uint64_t header = *current++;
60 uint32_t record_type = fuchsia_trace_utils::ReadField<uint32_t>(header, 0, 3);
61 switch (record_type) {
62 case kEvent: {
63 uint32_t event_type =
64 fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 19);
65 uint32_t n_args =
66 fuchsia_trace_utils::ReadField<uint32_t>(header, 20, 23);
67 uint32_t thread_ref =
68 fuchsia_trace_utils::ReadField<uint32_t>(header, 24, 31);
69 uint32_t cat_ref =
70 fuchsia_trace_utils::ReadField<uint32_t>(header, 32, 47);
71 uint32_t name_ref =
72 fuchsia_trace_utils::ReadField<uint32_t>(header, 48, 63);
73
74 int64_t ts = fuchsia_trace_utils::ReadTimestamp(
75 ¤t, provider_view->get_ticks_per_second());
76 fuchsia_trace_utils::ThreadInfo tinfo;
77 if (fuchsia_trace_utils::IsInlineThread(thread_ref)) {
78 tinfo = fuchsia_trace_utils::ReadInlineThread(¤t);
79 } else {
80 tinfo = provider_view->GetThread(thread_ref);
81 }
82 StringId cat;
83 if (fuchsia_trace_utils::IsInlineString(cat_ref)) {
84 cat = context_->storage->InternString(
85 fuchsia_trace_utils::ReadInlineString(¤t, cat_ref));
86 } else {
87 cat = provider_view->GetString(cat_ref);
88 }
89 StringId name;
90 if (fuchsia_trace_utils::IsInlineString(name_ref)) {
91 name = context_->storage->InternString(
92 fuchsia_trace_utils::ReadInlineString(¤t, name_ref));
93 } else {
94 name = provider_view->GetString(name_ref);
95 }
96
97 // Skip over all the args, so that the |ReadTimestamp| call for complete
98 // durations has the pointer in the right place.
99 for (uint32_t i = 0; i < n_args; i++) {
100 uint64_t arg_header = *current;
101 uint32_t arg_size_words =
102 fuchsia_trace_utils::ReadField<uint32_t>(arg_header, 4, 15);
103 current += arg_size_words;
104 }
105
106 switch (event_type) {
107 case kDurationBegin: {
108 UniqueTid utid =
109 procs->UpdateThread(static_cast<uint32_t>(tinfo.tid),
110 static_cast<uint32_t>(tinfo.pid));
111 slices->Begin(ts, utid, cat, name);
112 break;
113 }
114 case kDurationEnd: {
115 UniqueTid utid =
116 procs->UpdateThread(static_cast<uint32_t>(tinfo.tid),
117 static_cast<uint32_t>(tinfo.pid));
118 slices->End(ts, utid, cat, name);
119 break;
120 }
121 case kDurationComplete: {
122 int64_t end_ts = fuchsia_trace_utils::ReadTimestamp(
123 ¤t, provider_view->get_ticks_per_second());
124 UniqueTid utid =
125 procs->UpdateThread(static_cast<uint32_t>(tinfo.tid),
126 static_cast<uint32_t>(tinfo.pid));
127 slices->Scoped(ts, utid, cat, name, end_ts - ts);
128 break;
129 }
130 }
131 break;
132 }
133 default: {
134 PERFETTO_DFATAL("Unknown record type %d in FuchsiaTraceParser",
135 record_type);
136 break;
137 }
138 }
139 }
140
141 } // namespace trace_processor
142 } // namespace perfetto
143