xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/gecko/gecko_trace_parser_impl.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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/gecko/gecko_trace_parser_impl.h"
18 
19 #include <cstdint>
20 
21 #include "perfetto/base/compiler.h"
22 #include "src/trace_processor/importers/common/process_tracker.h"
23 #include "src/trace_processor/importers/common/stack_profile_tracker.h"
24 #include "src/trace_processor/importers/gecko/gecko_event.h"
25 #include "src/trace_processor/storage/trace_storage.h"
26 #include "src/trace_processor/tables/profiler_tables_py.h"
27 #include "src/trace_processor/types/trace_processor_context.h"
28 
29 namespace perfetto::trace_processor::gecko_importer {
30 
31 namespace {
32 
33 template <typename T>
GeckoOneOf()34 constexpr uint32_t GeckoOneOf() {
35   return base::variant_index<GeckoEvent::OneOf, T>();
36 }
37 
38 }  // namespace
39 
GeckoTraceParserImpl(TraceProcessorContext * context)40 GeckoTraceParserImpl::GeckoTraceParserImpl(TraceProcessorContext* context)
41     : context_(context) {}
42 
43 GeckoTraceParserImpl::~GeckoTraceParserImpl() = default;
44 
ParseGeckoEvent(int64_t ts,GeckoEvent evt)45 void GeckoTraceParserImpl::ParseGeckoEvent(int64_t ts, GeckoEvent evt) {
46   switch (evt.oneof.index()) {
47     case GeckoOneOf<GeckoEvent::ThreadMetadata>(): {
48       auto thread = std::get<GeckoEvent::ThreadMetadata>(evt.oneof);
49       UniqueTid utid =
50           context_->process_tracker->UpdateThread(thread.tid, thread.pid);
51       context_->process_tracker->UpdateThreadNameByUtid(
52           utid, thread.name, ThreadNamePriority::kOther);
53       break;
54     }
55     case GeckoOneOf<GeckoEvent::StackSample>():
56       auto sample = std::get<GeckoEvent::StackSample>(evt.oneof);
57       auto* ss = context_->storage->mutable_cpu_profile_stack_sample_table();
58       tables::CpuProfileStackSampleTable::Row row;
59       row.ts = ts;
60       row.callsite_id = sample.callsite_id;
61       row.utid = context_->process_tracker->GetOrCreateThread(sample.tid);
62       ss->Insert(row);
63       break;
64   }
65 }
66 
67 }  // namespace perfetto::trace_processor::gecko_importer
68