• 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 #include "src/trace_processor/importers/fuchsia/fuchsia_trace_tokenizer.h"
18 
19 #include <cinttypes>
20 #include <limits>
21 
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/string_view.h"
24 #include "perfetto/trace_processor/trace_blob.h"
25 #include "src/trace_processor/importers/common/process_tracker.h"
26 #include "src/trace_processor/importers/common/slice_tracker.h"
27 #include "src/trace_processor/importers/fuchsia/fuchsia_record.h"
28 #include "src/trace_processor/importers/fuchsia/fuchsia_trace_parser.h"
29 #include "src/trace_processor/importers/proto/proto_trace_parser.h"
30 #include "src/trace_processor/importers/proto/proto_trace_reader.h"
31 #include "src/trace_processor/sorter/trace_sorter.h"
32 #include "src/trace_processor/types/task_state.h"
33 #include "src/trace_processor/types/trace_processor_context.h"
34 
35 namespace perfetto {
36 namespace trace_processor {
37 
38 namespace {
39 
40 using fuchsia_trace_utils::ArgValue;
41 
42 // Record types
43 constexpr uint32_t kMetadata = 0;
44 constexpr uint32_t kInitialization = 1;
45 constexpr uint32_t kString = 2;
46 constexpr uint32_t kThread = 3;
47 constexpr uint32_t kEvent = 4;
48 constexpr uint32_t kBlob = 5;
49 constexpr uint32_t kKernelObject = 7;
50 constexpr uint32_t kSchedulerEvent = 8;
51 
52 constexpr uint32_t kSchedulerEventLegacyContextSwitch = 0;
53 constexpr uint32_t kSchedulerEventContextSwitch = 1;
54 constexpr uint32_t kSchedulerEventThreadWakeup = 2;
55 
56 // Metadata types
57 constexpr uint32_t kProviderInfo = 1;
58 constexpr uint32_t kProviderSection = 2;
59 constexpr uint32_t kProviderEvent = 3;
60 
61 // Thread states
62 constexpr uint32_t kThreadNew = 0;
63 constexpr uint32_t kThreadRunning = 1;
64 constexpr uint32_t kThreadSuspended = 2;
65 constexpr uint32_t kThreadBlocked = 3;
66 constexpr uint32_t kThreadDying = 4;
67 constexpr uint32_t kThreadDead = 5;
68 
69 // Zircon object types
70 constexpr uint32_t kZxObjTypeProcess = 1;
71 constexpr uint32_t kZxObjTypeThread = 2;
72 
73 constexpr int32_t kIdleWeight = std::numeric_limits<int32_t>::min();
74 
75 }  // namespace
76 
FuchsiaTraceTokenizer(TraceProcessorContext * context)77 FuchsiaTraceTokenizer::FuchsiaTraceTokenizer(TraceProcessorContext* context)
78     : context_(context),
79       proto_reader_(context),
80       running_string_id_(context->storage->InternString("Running")),
81       runnable_string_id_(context->storage->InternString("R")),
82       preempted_string_id_(context->storage->InternString("R+")),
83       waking_string_id_(context->storage->InternString("W")),
84       blocked_string_id_(context->storage->InternString("S")),
85       suspended_string_id_(context->storage->InternString("T")),
86       exit_dying_string_id_(context->storage->InternString("Z")),
87       exit_dead_string_id_(context->storage->InternString("X")),
88       incoming_weight_id_(context->storage->InternString("incoming_weight")),
89       outgoing_weight_id_(context->storage->InternString("outgoing_weight")),
90       weight_id_(context->storage->InternString("weight")),
91       process_id_(context->storage->InternString("process")) {
92   RegisterProvider(0, "");
93 }
94 
95 FuchsiaTraceTokenizer::~FuchsiaTraceTokenizer() = default;
96 
Parse(TraceBlobView blob)97 util::Status FuchsiaTraceTokenizer::Parse(TraceBlobView blob) {
98   size_t size = blob.size();
99 
100   // The relevant internal state is |leftover_bytes_|. Each call to Parse should
101   // maintain the following properties, unless a fatal error occurs in which
102   // case it should return false and no assumptions should be made about the
103   // resulting internal state:
104   //
105   // 1) Every byte passed to |Parse| has either been passed to |ParseRecord| or
106   // is present in |leftover_bytes_|, but not both.
107   // 2) |leftover_bytes_| does not contain a complete record.
108   //
109   // Parse is responsible for creating the "full" |TraceBlobView|s, which own
110   // the underlying data. Generally, there will be one such view. However, if
111   // there is a record that started in an earlier call, then a new buffer is
112   // created here to make the bytes in that record contiguous.
113   //
114   // Because some of the bytes in |data| might belong to the record starting in
115   // |leftover_bytes_|, we track the offset at which the following record will
116   // start.
117   size_t byte_offset = 0;
118 
119   // Look for a record starting with the leftover bytes.
120   if (leftover_bytes_.size() + size < 8) {
121     // Even with the new bytes, we can't even read the header of the next
122     // record, so just add the new bytes to |leftover_bytes_| and return.
123     leftover_bytes_.insert(leftover_bytes_.end(), blob.data() + byte_offset,
124                            blob.data() + size);
125     return util::OkStatus();
126   }
127   if (!leftover_bytes_.empty()) {
128     // There is a record starting from leftover bytes.
129     if (leftover_bytes_.size() < 8) {
130       // Header was previously incomplete, but we have enough now.
131       // Copy bytes into |leftover_bytes_| so that the whole header is present,
132       // and update |byte_offset| and |size| accordingly.
133       size_t needed_bytes = 8 - leftover_bytes_.size();
134       leftover_bytes_.insert(leftover_bytes_.end(), blob.data() + byte_offset,
135                              blob.data() + needed_bytes);
136       byte_offset += needed_bytes;
137       size -= needed_bytes;
138     }
139     // Read the record length from the header.
140     uint64_t header =
141         *reinterpret_cast<const uint64_t*>(leftover_bytes_.data());
142     uint32_t record_len_words =
143         fuchsia_trace_utils::ReadField<uint32_t>(header, 4, 15);
144     uint32_t record_len_bytes = record_len_words * sizeof(uint64_t);
145 
146     // From property (2) above, leftover_bytes_ must have had less than a full
147     // record to start with. We padded leftover_bytes_ out to read the header,
148     // so it may now be a full record (in the case that the record consists of
149     // only the header word), but it still cannot have any extra bytes.
150     PERFETTO_DCHECK(leftover_bytes_.size() <= record_len_bytes);
151     size_t missing_bytes = record_len_bytes - leftover_bytes_.size();
152 
153     if (missing_bytes <= size) {
154       // We have enough bytes to complete the partial record. Create a new
155       // buffer for that record.
156       TraceBlob buf = TraceBlob::Allocate(record_len_bytes);
157       memcpy(buf.data(), leftover_bytes_.data(), leftover_bytes_.size());
158       memcpy(buf.data() + leftover_bytes_.size(), blob.data() + byte_offset,
159              missing_bytes);
160       byte_offset += missing_bytes;
161       size -= missing_bytes;
162       leftover_bytes_.clear();
163       ParseRecord(TraceBlobView(std::move(buf)));
164     } else {
165       // There are not enough bytes for the full record. Add all the bytes we
166       // have to leftover_bytes_ and wait for more.
167       leftover_bytes_.insert(leftover_bytes_.end(), blob.data() + byte_offset,
168                              blob.data() + byte_offset + size);
169       return util::OkStatus();
170     }
171   }
172 
173   TraceBlobView full_view = blob.slice_off(byte_offset, size);
174 
175   // |record_offset| is a number of bytes past |byte_offset| where the record
176   // under consideration starts. As a result, it must always be in the range [0,
177   // size-8]. Any larger offset means we don't have enough bytes for the header.
178   size_t record_offset = 0;
179   while (record_offset + 8 <= size) {
180     uint64_t header =
181         *reinterpret_cast<const uint64_t*>(full_view.data() + record_offset);
182     uint32_t record_len_bytes =
183         fuchsia_trace_utils::ReadField<uint32_t>(header, 4, 15) *
184         sizeof(uint64_t);
185     if (record_len_bytes == 0)
186       return util::ErrStatus("Unexpected record of size 0");
187 
188     if (record_offset + record_len_bytes > size)
189       break;
190 
191     TraceBlobView record = full_view.slice_off(record_offset, record_len_bytes);
192     ParseRecord(std::move(record));
193 
194     record_offset += record_len_bytes;
195   }
196 
197   leftover_bytes_.insert(leftover_bytes_.end(),
198                          full_view.data() + record_offset,
199                          full_view.data() + size);
200 
201   TraceBlob perfetto_blob =
202       TraceBlob::CopyFrom(proto_trace_data_.data(), proto_trace_data_.size());
203   proto_trace_data_.clear();
204 
205   return proto_reader_.Parse(TraceBlobView(std::move(perfetto_blob)));
206 }
207 
IdForOutgoingThreadState(uint32_t state)208 StringId FuchsiaTraceTokenizer::IdForOutgoingThreadState(uint32_t state) {
209   switch (state) {
210     case kThreadNew:
211     case kThreadRunning:
212       return runnable_string_id_;
213     case kThreadBlocked:
214       return blocked_string_id_;
215     case kThreadSuspended:
216       return suspended_string_id_;
217     case kThreadDying:
218       return exit_dying_string_id_;
219     case kThreadDead:
220       return exit_dead_string_id_;
221     default:
222       return kNullStringId;
223   }
224 }
225 
SwitchFrom(Thread * thread,int64_t ts,uint32_t cpu,uint32_t thread_state)226 void FuchsiaTraceTokenizer::SwitchFrom(Thread* thread,
227                                        int64_t ts,
228                                        uint32_t cpu,
229                                        uint32_t thread_state) {
230   TraceStorage* storage = context_->storage.get();
231   ProcessTracker* procs = context_->process_tracker.get();
232 
233   StringId state = IdForOutgoingThreadState(thread_state);
234   UniqueTid utid = procs->UpdateThread(static_cast<uint32_t>(thread->info.tid),
235                                        static_cast<uint32_t>(thread->info.pid));
236 
237   const auto duration = ts - thread->last_ts;
238   thread->last_ts = ts;
239 
240   // Close the slice record if one is open for this thread.
241   if (thread->last_slice_row.has_value()) {
242     auto row_ref = thread->last_slice_row->ToRowReference(
243         storage->mutable_sched_slice_table());
244     row_ref.set_dur(duration);
245     row_ref.set_end_state(state);
246     thread->last_slice_row.reset();
247   }
248 
249   // Close the state record if one is open for this thread.
250   if (thread->last_state_row.has_value()) {
251     auto row_ref = thread->last_state_row->ToRowReference(
252         storage->mutable_thread_state_table());
253     row_ref.set_dur(duration);
254     thread->last_state_row.reset();
255   }
256 
257   // Open a new state record to track the duration of the outgoing
258   // state.
259   tables::ThreadStateTable::Row state_row;
260   state_row.ts = ts;
261   state_row.cpu = cpu;
262   state_row.dur = -1;
263   state_row.state = state;
264   state_row.utid = utid;
265   auto state_row_number =
266       storage->mutable_thread_state_table()->Insert(state_row).row_number;
267   thread->last_state_row = state_row_number;
268 }
269 
SwitchTo(Thread * thread,int64_t ts,uint32_t cpu,int32_t weight)270 void FuchsiaTraceTokenizer::SwitchTo(Thread* thread,
271                                      int64_t ts,
272                                      uint32_t cpu,
273                                      int32_t weight) {
274   TraceStorage* storage = context_->storage.get();
275   ProcessTracker* procs = context_->process_tracker.get();
276 
277   UniqueTid utid = procs->UpdateThread(static_cast<uint32_t>(thread->info.tid),
278                                        static_cast<uint32_t>(thread->info.pid));
279 
280   const auto duration = ts - thread->last_ts;
281   thread->last_ts = ts;
282 
283   // Close the state record if one is open for this thread.
284   if (thread->last_state_row.has_value()) {
285     auto row_ref = thread->last_state_row->ToRowReference(
286         storage->mutable_thread_state_table());
287     row_ref.set_dur(duration);
288     thread->last_state_row.reset();
289   }
290 
291   // Open a new slice record for this thread.
292   tables::SchedSliceTable::Row slice_row;
293   slice_row.ts = ts;
294   slice_row.cpu = cpu;
295   slice_row.dur = -1;
296   slice_row.utid = utid;
297   slice_row.priority = weight;
298   auto slice_row_number =
299       storage->mutable_sched_slice_table()->Insert(slice_row).row_number;
300   thread->last_slice_row = slice_row_number;
301 
302   // Open a new state record for this thread.
303   tables::ThreadStateTable::Row state_row;
304   state_row.ts = ts;
305   state_row.cpu = cpu;
306   state_row.dur = -1;
307   state_row.state = running_string_id_;
308   state_row.utid = utid;
309   auto state_row_number =
310       storage->mutable_thread_state_table()->Insert(state_row).row_number;
311   thread->last_state_row = state_row_number;
312 }
313 
Wake(Thread * thread,int64_t ts,uint32_t cpu)314 void FuchsiaTraceTokenizer::Wake(Thread* thread, int64_t ts, uint32_t cpu) {
315   TraceStorage* storage = context_->storage.get();
316   ProcessTracker* procs = context_->process_tracker.get();
317 
318   UniqueTid utid = procs->UpdateThread(static_cast<uint32_t>(thread->info.tid),
319                                        static_cast<uint32_t>(thread->info.pid));
320 
321   const auto duration = ts - thread->last_ts;
322   thread->last_ts = ts;
323 
324   // Close the state record if one is open for this thread.
325   if (thread->last_state_row.has_value()) {
326     auto row_ref = thread->last_state_row->ToRowReference(
327         storage->mutable_thread_state_table());
328     row_ref.set_dur(duration);
329     thread->last_state_row.reset();
330   }
331 
332   // Open a new state record for this thread.
333   tables::ThreadStateTable::Row state_row;
334   state_row.ts = ts;
335   state_row.cpu = cpu;
336   state_row.dur = -1;
337   state_row.state = waking_string_id_;
338   state_row.utid = utid;
339   auto state_row_number =
340       storage->mutable_thread_state_table()->Insert(state_row).row_number;
341   thread->last_state_row = state_row_number;
342 }
343 
344 // Most record types are read and recorded in |TraceStorage| here directly.
345 // Event records are sorted by timestamp before processing, so instead of
346 // recording them in |TraceStorage| they are given to |TraceSorter|. In order to
347 // facilitate the parsing after sorting, a small view of the provider's string
348 // and thread tables is passed alongside the record. See |FuchsiaProviderView|.
ParseRecord(TraceBlobView tbv)349 void FuchsiaTraceTokenizer::ParseRecord(TraceBlobView tbv) {
350   TraceStorage* storage = context_->storage.get();
351   ProcessTracker* procs = context_->process_tracker.get();
352   TraceSorter* sorter = context_->sorter.get();
353 
354   fuchsia_trace_utils::RecordCursor cursor(tbv.data(), tbv.length());
355   uint64_t header;
356   if (!cursor.ReadUint64(&header)) {
357     context_->storage->IncrementStats(stats::fuchsia_invalid_event);
358     return;
359   }
360 
361   uint32_t record_type = fuchsia_trace_utils::ReadField<uint32_t>(header, 0, 3);
362 
363   // All non-metadata events require current_provider_ to be set.
364   if (record_type != kMetadata && current_provider_ == nullptr) {
365     context_->storage->IncrementStats(stats::fuchsia_invalid_event);
366     return;
367   }
368 
369   // Adapters for FuchsiaTraceParser::ParseArgs.
370   const auto intern_string = [this](base::StringView string) {
371     return context_->storage->InternString(string);
372   };
373   const auto get_string = [this](uint16_t index) {
374     return current_provider_->GetString(index);
375   };
376 
377   switch (record_type) {
378     case kMetadata: {
379       uint32_t metadata_type =
380           fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 19);
381       switch (metadata_type) {
382         case kProviderInfo: {
383           uint32_t provider_id =
384               fuchsia_trace_utils::ReadField<uint32_t>(header, 20, 51);
385           uint32_t name_len =
386               fuchsia_trace_utils::ReadField<uint32_t>(header, 52, 59);
387           base::StringView name_view;
388           if (!cursor.ReadInlineString(name_len, &name_view)) {
389             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
390             return;
391           }
392           RegisterProvider(provider_id, name_view.ToStdString());
393           break;
394         }
395         case kProviderSection: {
396           uint32_t provider_id =
397               fuchsia_trace_utils::ReadField<uint32_t>(header, 20, 51);
398           current_provider_ = providers_[provider_id].get();
399           break;
400         }
401         case kProviderEvent: {
402           // TODO(bhamrick): Handle buffer fill events
403           PERFETTO_DLOG(
404               "Ignoring provider event. Events may have been dropped");
405           break;
406         }
407       }
408       break;
409     }
410     case kInitialization: {
411       if (!cursor.ReadUint64(&current_provider_->ticks_per_second)) {
412         context_->storage->IncrementStats(stats::fuchsia_invalid_event);
413         return;
414       }
415       break;
416     }
417     case kString: {
418       uint32_t index = fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 30);
419       if (index != 0) {
420         uint32_t len = fuchsia_trace_utils::ReadField<uint32_t>(header, 32, 46);
421         base::StringView s;
422         if (!cursor.ReadInlineString(len, &s)) {
423           context_->storage->IncrementStats(stats::fuchsia_invalid_event);
424           return;
425         }
426         StringId id = storage->InternString(s);
427 
428         current_provider_->string_table[index] = id;
429       }
430       break;
431     }
432     case kThread: {
433       uint32_t index = fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 23);
434       if (index != 0) {
435         FuchsiaThreadInfo tinfo;
436         if (!cursor.ReadInlineThread(&tinfo)) {
437           context_->storage->IncrementStats(stats::fuchsia_invalid_event);
438           return;
439         }
440 
441         current_provider_->thread_table[index] = tinfo;
442       }
443       break;
444     }
445     case kEvent: {
446       uint32_t thread_ref =
447           fuchsia_trace_utils::ReadField<uint32_t>(header, 24, 31);
448       uint32_t cat_ref =
449           fuchsia_trace_utils::ReadField<uint32_t>(header, 32, 47);
450       uint32_t name_ref =
451           fuchsia_trace_utils::ReadField<uint32_t>(header, 48, 63);
452 
453       // Build the FuchsiaRecord for the event, i.e. extract the thread
454       // information if not inline, and any non-inline strings (name, category
455       // for now, arg names and string values in the future).
456       FuchsiaRecord record(std::move(tbv));
457       record.set_ticks_per_second(current_provider_->ticks_per_second);
458 
459       uint64_t ticks;
460       if (!cursor.ReadUint64(&ticks)) {
461         context_->storage->IncrementStats(stats::fuchsia_invalid_event);
462         return;
463       }
464       int64_t ts = fuchsia_trace_utils::TicksToNs(
465           ticks, current_provider_->ticks_per_second);
466       if (ts < 0) {
467         storage->IncrementStats(stats::fuchsia_timestamp_overflow);
468         return;
469       }
470 
471       if (fuchsia_trace_utils::IsInlineThread(thread_ref)) {
472         // Skip over inline thread
473         cursor.ReadInlineThread(nullptr);
474       } else {
475         record.InsertThread(thread_ref,
476                             current_provider_->GetThread(thread_ref));
477       }
478 
479       if (fuchsia_trace_utils::IsInlineString(cat_ref)) {
480         // Skip over inline string
481         cursor.ReadInlineString(cat_ref, nullptr);
482       } else {
483         record.InsertString(cat_ref, current_provider_->GetString(cat_ref));
484       }
485 
486       if (fuchsia_trace_utils::IsInlineString(name_ref)) {
487         // Skip over inline string
488         cursor.ReadInlineString(name_ref, nullptr);
489       } else {
490         record.InsertString(name_ref, current_provider_->GetString(name_ref));
491       }
492 
493       uint32_t n_args =
494           fuchsia_trace_utils::ReadField<uint32_t>(header, 20, 23);
495       for (uint32_t i = 0; i < n_args; i++) {
496         const size_t arg_base = cursor.WordIndex();
497         uint64_t arg_header;
498         if (!cursor.ReadUint64(&arg_header)) {
499           storage->IncrementStats(stats::fuchsia_invalid_event);
500           return;
501         }
502         uint32_t arg_type =
503             fuchsia_trace_utils::ReadField<uint32_t>(arg_header, 0, 3);
504         uint32_t arg_size_words =
505             fuchsia_trace_utils::ReadField<uint32_t>(arg_header, 4, 15);
506         uint32_t arg_name_ref =
507             fuchsia_trace_utils::ReadField<uint32_t>(arg_header, 16, 31);
508 
509         if (fuchsia_trace_utils::IsInlineString(arg_name_ref)) {
510           // Skip over inline string
511           cursor.ReadInlineString(arg_name_ref, nullptr);
512         } else {
513           record.InsertString(arg_name_ref,
514                               current_provider_->GetString(arg_name_ref));
515         }
516 
517         if (arg_type == ArgValue::ArgType::kString) {
518           uint32_t arg_value_ref =
519               fuchsia_trace_utils::ReadField<uint32_t>(arg_header, 32, 47);
520           if (fuchsia_trace_utils::IsInlineString(arg_value_ref)) {
521             // Skip over inline string
522             cursor.ReadInlineString(arg_value_ref, nullptr);
523           } else {
524             record.InsertString(arg_value_ref,
525                                 current_provider_->GetString(arg_value_ref));
526           }
527         }
528 
529         cursor.SetWordIndex(arg_base + arg_size_words);
530       }
531 
532       sorter->PushFuchsiaRecord(ts, std::move(record));
533       break;
534     }
535     case kBlob: {
536       constexpr uint32_t kPerfettoBlob = 3;
537       uint32_t blob_type =
538           fuchsia_trace_utils::ReadField<uint32_t>(header, 48, 55);
539       if (blob_type == kPerfettoBlob) {
540         FuchsiaRecord record(std::move(tbv));
541         uint32_t blob_size =
542             fuchsia_trace_utils::ReadField<uint32_t>(header, 32, 46);
543         uint32_t name_ref =
544             fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 31);
545 
546         // We don't need the name, but we still need to parse it in case it is
547         // inline
548         if (fuchsia_trace_utils::IsInlineString(name_ref)) {
549           base::StringView name_view;
550           if (!cursor.ReadInlineString(name_ref, &name_view)) {
551             storage->IncrementStats(stats::fuchsia_invalid_event);
552             return;
553           }
554         }
555 
556         // Append the Blob into the embedded perfetto bytes -- we'll parse them
557         // all after the main pass is done.
558         if (!cursor.ReadBlob(blob_size, proto_trace_data_)) {
559           storage->IncrementStats(stats::fuchsia_invalid_event);
560           return;
561         }
562       }
563       break;
564     }
565     case kKernelObject: {
566       uint32_t obj_type =
567           fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 23);
568       uint32_t name_ref =
569           fuchsia_trace_utils::ReadField<uint32_t>(header, 24, 39);
570 
571       uint64_t obj_id;
572       if (!cursor.ReadUint64(&obj_id)) {
573         storage->IncrementStats(stats::fuchsia_invalid_event);
574         return;
575       }
576 
577       StringId name = StringId();
578       if (fuchsia_trace_utils::IsInlineString(name_ref)) {
579         base::StringView name_view;
580         if (!cursor.ReadInlineString(name_ref, &name_view)) {
581           storage->IncrementStats(stats::fuchsia_invalid_event);
582           return;
583         }
584         name = storage->InternString(name_view);
585       } else {
586         name = current_provider_->GetString(name_ref);
587       }
588 
589       switch (obj_type) {
590         case kZxObjTypeProcess: {
591           // Note: Fuchsia pid/tids are 64 bits but Perfetto's tables only
592           // support 32 bits. This is usually not an issue except for
593           // artificial koids which have the 2^63 bit set. This is used for
594           // things such as virtual threads.
595           procs->SetProcessMetadata(
596               static_cast<uint32_t>(obj_id), std::optional<uint32_t>(),
597               base::StringView(storage->GetString(name)), base::StringView());
598           break;
599         }
600         case kZxObjTypeThread: {
601           uint32_t n_args =
602               fuchsia_trace_utils::ReadField<uint32_t>(header, 40, 43);
603 
604           auto maybe_args = FuchsiaTraceParser::ParseArgs(
605               cursor, n_args, intern_string, get_string);
606           if (!maybe_args.has_value()) {
607             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
608             return;
609           }
610 
611           uint64_t pid = 0;
612           for (const auto arg : *maybe_args) {
613             if (arg.name == process_id_) {
614               if (arg.value.Type() != ArgValue::ArgType::kKoid) {
615                 storage->IncrementStats(stats::fuchsia_invalid_event);
616                 return;
617               }
618               pid = arg.value.Koid();
619             }
620           }
621 
622           Thread& thread = GetThread(obj_id);
623           thread.info.pid = pid;
624 
625           UniqueTid utid = procs->UpdateThread(static_cast<uint32_t>(obj_id),
626                                                static_cast<uint32_t>(pid));
627           storage->mutable_thread_table()->mutable_name()->Set(utid, name);
628           break;
629         }
630         default: {
631           PERFETTO_DLOG("Skipping Kernel Object record with type %d", obj_type);
632           break;
633         }
634       }
635       break;
636     }
637     case kSchedulerEvent: {
638       // Context switch records come in order, so they do not need to go through
639       // TraceSorter.
640       uint32_t event_type =
641           fuchsia_trace_utils::ReadField<uint32_t>(header, 60, 63);
642       switch (event_type) {
643         case kSchedulerEventLegacyContextSwitch: {
644           uint32_t cpu =
645               fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 23);
646           uint32_t outgoing_state =
647               fuchsia_trace_utils::ReadField<uint32_t>(header, 24, 27);
648           uint32_t outgoing_thread_ref =
649               fuchsia_trace_utils::ReadField<uint32_t>(header, 28, 35);
650           int32_t outgoing_priority =
651               fuchsia_trace_utils::ReadField<int32_t>(header, 44, 51);
652           uint32_t incoming_thread_ref =
653               fuchsia_trace_utils::ReadField<uint32_t>(header, 36, 43);
654           int32_t incoming_priority =
655               fuchsia_trace_utils::ReadField<int32_t>(header, 52, 59);
656 
657           int64_t ts;
658           if (!cursor.ReadTimestamp(current_provider_->ticks_per_second, &ts)) {
659             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
660             return;
661           }
662           if (ts == -1) {
663             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
664             return;
665           }
666 
667           FuchsiaThreadInfo outgoing_thread_info;
668           if (fuchsia_trace_utils::IsInlineThread(outgoing_thread_ref)) {
669             if (!cursor.ReadInlineThread(&outgoing_thread_info)) {
670               context_->storage->IncrementStats(stats::fuchsia_invalid_event);
671               return;
672             }
673           } else {
674             outgoing_thread_info =
675                 current_provider_->GetThread(outgoing_thread_ref);
676           }
677           Thread& outgoing_thread = GetThread(outgoing_thread_info.tid);
678 
679           FuchsiaThreadInfo incoming_thread_info;
680           if (fuchsia_trace_utils::IsInlineThread(incoming_thread_ref)) {
681             if (!cursor.ReadInlineThread(&incoming_thread_info)) {
682               context_->storage->IncrementStats(stats::fuchsia_invalid_event);
683               return;
684             }
685           } else {
686             incoming_thread_info =
687                 current_provider_->GetThread(incoming_thread_ref);
688           }
689           Thread& incoming_thread = GetThread(incoming_thread_info.tid);
690 
691           // Idle threads are identified by pid == 0 and prio == 0.
692           const bool incoming_is_idle =
693               incoming_thread.info.pid == 0 && incoming_priority == 0;
694           const bool outgoing_is_idle =
695               outgoing_thread.info.pid == 0 && outgoing_priority == 0;
696 
697           // Handle switching away from the currently running thread.
698           if (!outgoing_is_idle) {
699             SwitchFrom(&outgoing_thread, ts, cpu, outgoing_state);
700           }
701 
702           // Handle switching to the new currently running thread.
703           if (!incoming_is_idle) {
704             SwitchTo(&incoming_thread, ts, cpu, incoming_priority);
705           }
706           break;
707         }
708         case kSchedulerEventContextSwitch: {
709           const uint32_t argument_count =
710               fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 19);
711           const uint32_t cpu =
712               fuchsia_trace_utils::ReadField<uint32_t>(header, 20, 35);
713           const uint32_t outgoing_state =
714               fuchsia_trace_utils::ReadField<uint32_t>(header, 36, 39);
715 
716           int64_t ts;
717           if (!cursor.ReadTimestamp(current_provider_->ticks_per_second, &ts)) {
718             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
719             return;
720           }
721           if (ts < 0) {
722             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
723             return;
724           }
725 
726           uint64_t outgoing_tid;
727           if (!cursor.ReadUint64(&outgoing_tid)) {
728             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
729             return;
730           }
731           Thread& outgoing_thread = GetThread(outgoing_tid);
732 
733           uint64_t incoming_tid;
734           if (!cursor.ReadUint64(&incoming_tid)) {
735             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
736             return;
737           }
738           Thread& incoming_thread = GetThread(incoming_tid);
739 
740           auto maybe_args = FuchsiaTraceParser::ParseArgs(
741               cursor, argument_count, intern_string, get_string);
742           if (!maybe_args.has_value()) {
743             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
744             return;
745           }
746 
747           int32_t incoming_weight = 0;
748           int32_t outgoing_weight = 0;
749 
750           for (const auto& arg : *maybe_args) {
751             if (arg.name == incoming_weight_id_) {
752               if (arg.value.Type() != ArgValue::ArgType::kInt32) {
753                 context_->storage->IncrementStats(stats::fuchsia_invalid_event);
754                 return;
755               }
756               incoming_weight = arg.value.Int32();
757             } else if (arg.name == outgoing_weight_id_) {
758               if (arg.value.Type() != ArgValue::ArgType::kInt32) {
759                 context_->storage->IncrementStats(stats::fuchsia_invalid_event);
760                 return;
761               }
762               outgoing_weight = arg.value.Int32();
763             }
764           }
765 
766           const bool incoming_is_idle = incoming_weight == kIdleWeight;
767           const bool outgoing_is_idle = outgoing_weight == kIdleWeight;
768 
769           // Handle switching away from the currently running thread.
770           if (!outgoing_is_idle) {
771             SwitchFrom(&outgoing_thread, ts, cpu, outgoing_state);
772           }
773 
774           // Handle switching to the new currently running thread.
775           if (!incoming_is_idle) {
776             SwitchTo(&incoming_thread, ts, cpu, incoming_weight);
777           }
778           break;
779         }
780         case kSchedulerEventThreadWakeup: {
781           const uint32_t argument_count =
782               fuchsia_trace_utils::ReadField<uint32_t>(header, 16, 19);
783           const uint32_t cpu =
784               fuchsia_trace_utils::ReadField<uint32_t>(header, 20, 35);
785 
786           int64_t ts;
787           if (!cursor.ReadTimestamp(current_provider_->ticks_per_second, &ts)) {
788             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
789             return;
790           }
791           if (ts < 0) {
792             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
793             return;
794           }
795 
796           uint64_t waking_tid;
797           if (!cursor.ReadUint64(&waking_tid)) {
798             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
799             return;
800           }
801           Thread& waking_thread = GetThread(waking_tid);
802 
803           auto maybe_args = FuchsiaTraceParser::ParseArgs(
804               cursor, argument_count, intern_string, get_string);
805           if (!maybe_args.has_value()) {
806             context_->storage->IncrementStats(stats::fuchsia_invalid_event);
807             return;
808           }
809 
810           int32_t waking_weight = 0;
811 
812           for (const auto& arg : *maybe_args) {
813             if (arg.name == weight_id_) {
814               if (arg.value.Type() != ArgValue::ArgType::kInt32) {
815                 context_->storage->IncrementStats(stats::fuchsia_invalid_event);
816                 return;
817               }
818               waking_weight = arg.value.Int32();
819             }
820           }
821 
822           const bool waking_is_idle = waking_weight == kIdleWeight;
823           if (!waking_is_idle) {
824             Wake(&waking_thread, ts, cpu);
825           }
826           break;
827         }
828         default:
829           PERFETTO_DLOG("Skipping unknown scheduler event type %d", event_type);
830           break;
831       }
832 
833       break;
834     }
835     default: {
836       PERFETTO_DLOG("Skipping record of unknown type %d", record_type);
837       break;
838     }
839   }
840 }
841 
RegisterProvider(uint32_t provider_id,std::string name)842 void FuchsiaTraceTokenizer::RegisterProvider(uint32_t provider_id,
843                                              std::string name) {
844   std::unique_ptr<ProviderInfo> provider(new ProviderInfo());
845   provider->name = name;
846   current_provider_ = provider.get();
847   providers_[provider_id] = std::move(provider);
848 }
849 
NotifyEndOfFile()850 void FuchsiaTraceTokenizer::NotifyEndOfFile() {}
851 
852 }  // namespace trace_processor
853 }  // namespace perfetto
854