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/etm/etm_tracker.h"
18
19 #include <cstdint>
20 #include <memory>
21
22 #include "perfetto/base/status.h"
23 #include "perfetto/ext/base/flat_hash_map.h"
24 #include "perfetto/trace_processor/trace_blob_view.h"
25 #include "src/trace_processor/importers/etm/storage_handle.h"
26 #include "src/trace_processor/importers/etm/target_memory.h"
27 #include "src/trace_processor/importers/etm/types.h"
28 #include "src/trace_processor/importers/etm/util.h"
29 #include "src/trace_processor/storage/trace_storage.h"
30 #include "src/trace_processor/tables/etm_tables_py.h"
31 #include "src/trace_processor/types/trace_processor_context.h"
32
33 namespace perfetto::trace_processor::etm {
34
35 // static
36
EtmTracker(TraceProcessorContext * context)37 EtmTracker::EtmTracker(TraceProcessorContext* context) : context_(context) {}
38 EtmTracker::~EtmTracker() = default;
39
AddSessionData(tables::EtmV4SessionTable::Id session_id,std::vector<TraceBlobView> traces)40 void EtmTracker::AddSessionData(tables::EtmV4SessionTable::Id session_id,
41 std::vector<TraceBlobView> traces) {
42 uint32_t trace_set_id = context_->storage->etm_v4_trace_table().row_count();
43
44 for (auto& trace : traces) {
45 auto trace_id = context_->storage->mutable_etm_v4_trace_table()
46 ->Insert({session_id, trace_set_id,
47 static_cast<int64_t>(trace.size())})
48 .id;
49 StorageHandle(context_).StoreTrace(trace_id, std::move(trace));
50 }
51 }
52
53 base::FlatSet<tables::EtmV4ConfigurationTable::Id>
InsertEtmV4Config(PerCpuConfiguration per_cpu_configs)54 EtmTracker::InsertEtmV4Config(PerCpuConfiguration per_cpu_configs) {
55 base::FlatSet<tables::EtmV4ConfigurationTable::Id> res;
56 uint32_t set_id = context_->storage->etm_v4_configuration_table().row_count();
57 for (auto it = per_cpu_configs.GetIterator(); it; ++it) {
58 uint32_t cpu = it.key();
59 std::unique_ptr<Configuration> config = std::move(it.value());
60 const EtmV4Config etm_v4_config = config->etm_v4_config();
61 tables::EtmV4ConfigurationTable::Row row;
62 row.set_id = set_id;
63
64 row.cpu = cpu;
65 row.cs_trace_id = etm_v4_config.getTraceID();
66 row.core_profile =
67 context_->storage->InternString(ToString(etm_v4_config.coreProfile()));
68 row.arch_version =
69 context_->storage->InternString(ToString(etm_v4_config.archVersion()));
70
71 row.major_version = etm_v4_config.MajVersion();
72 row.minor_version = etm_v4_config.MinVersion();
73
74 row.max_speculation_depth = etm_v4_config.MaxSpecDepth();
75 row.max_speculation_depth = etm_v4_config.MaxSpecDepth();
76
77 row.bool_flags = 0;
78 if (etm_v4_config.hasCycleCountI()) {
79 row.bool_flags |= ETM_V4_CONFIGURATION_TABLE_FLAG_HAS_CYCLE_COUNT;
80 }
81 if (etm_v4_config.enabledTS()) {
82 row.bool_flags |= ETM_V4_CONFIGURATION_TABLE_FLAG_TS_ENABLED;
83 }
84
85 auto id =
86 context_->storage->mutable_etm_v4_configuration_table()->Insert(row).id;
87 res.insert(id);
88 StorageHandle(context_).StoreEtmV4Config(id, std::move(config));
89 }
90 return res;
91 }
92
Finalize()93 base::Status EtmTracker::Finalize() {
94 TargetMemory::InitStorage(context_);
95 return base::OkStatus();
96 }
97
98 } // namespace perfetto::trace_processor::etm
99