xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/cpu_tracker.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/common/cpu_tracker.h"
18 
19 #include <cstdint>
20 #include <optional>
21 
22 #include "perfetto/base/logging.h"
23 #include "perfetto/ext/base/string_view.h"
24 #include "src/trace_processor/importers/common/machine_tracker.h"
25 #include "src/trace_processor/tables/metadata_tables_py.h"
26 
27 namespace perfetto::trace_processor {
28 
CpuTracker(TraceProcessorContext * context)29 CpuTracker::CpuTracker(TraceProcessorContext* context) : context_(context) {
30   // Preallocate ucpu of this machine for maintaining the relative order between
31   // ucpu and cpu.
32   auto machine_id = context_->machine_tracker->machine_id();
33   if (machine_id.has_value())
34     ucpu_offset_ = machine_id->value * kMaxCpusPerMachine;
35 
36   for (auto id = 0u; id < kMaxCpusPerMachine; id++) {
37     // Only populate the |machine_id| column. The |cpu| column is update only
38     // when the CPU is present.
39     tables::CpuTable::Row cpu_row;
40     cpu_row.machine_id = machine_id;
41     context_->storage->mutable_cpu_table()->Insert(cpu_row);
42   }
43 }
44 
SetCpuInfo(uint32_t cpu,base::StringView processor,uint32_t cluster_id,std::optional<uint32_t> capacity)45 tables::CpuTable::Id CpuTracker::SetCpuInfo(uint32_t cpu,
46                                             base::StringView processor,
47                                             uint32_t cluster_id,
48                                             std::optional<uint32_t> capacity) {
49   auto cpu_id = GetOrCreateCpu(cpu);
50 
51   auto cpu_row = context_->storage->mutable_cpu_table()->FindById(cpu_id);
52   PERFETTO_DCHECK(cpu_row.has_value());
53 
54   if (!processor.empty()) {
55     auto string_id = context_->storage->InternString(processor);
56     cpu_row->set_processor(string_id);
57   }
58   cpu_row->set_cluster_id(cluster_id);
59   if (capacity) {
60     cpu_row->set_capacity(*capacity);
61   }
62   return cpu_id;
63 }
64 
65 }  // namespace perfetto::trace_processor
66