xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/cpu_tracker.h (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 #ifndef SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_CPU_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_CPU_TRACKER_H_
19 
20 #include <bitset>
21 #include <cstdint>
22 #include <optional>
23 
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/string_view.h"
26 #include "perfetto/public/compiler.h"
27 #include "src/trace_processor/storage/trace_storage.h"
28 #include "src/trace_processor/tables/metadata_tables_py.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30 
31 namespace perfetto::trace_processor {
32 
33 class TraceProcessorContext;
34 
35 class CpuTracker {
36  public:
37   // The CPU table Id serves as the 'ucpu' in sched_slice and related for
38   // joining with this table. To optimize for single-machine traces, this class
39   // assumes a maximum of |kMaxCpusPerMachine| CPUs per machine to maintain a
40   // relative order of |cpu| and |ucpu| by pre-allocating |kMaxCpusPerMachine|
41   // records in the CPU table. The mapping between |ucpu| and |cpu| becomes
42   // |cpu| = |ucpu| % |kMaxCpusPerMachine|.
43   static constexpr uint32_t kMaxCpusPerMachine = 4096;
44 
45   explicit CpuTracker(TraceProcessorContext*);
46 
GetOrCreateCpu(uint32_t cpu)47   tables::CpuTable::Id GetOrCreateCpu(uint32_t cpu) {
48     // CPU core number is in the range of 0..kMaxCpusPerMachine-1.
49     PERFETTO_CHECK(cpu < kMaxCpusPerMachine);
50     auto ucpu = ucpu_offset_ + cpu;
51     if (PERFETTO_LIKELY(cpu_ids_[cpu]))
52       return tables::CpuTable::Id(ucpu);
53     cpu_ids_.set(cpu);
54 
55     // Populate the optional |cpu| column.
56     auto& cpu_table = *context_->storage->mutable_cpu_table();
57     cpu_table[ucpu].set_cpu(cpu);
58     return tables::CpuTable::Id(ucpu);
59   }
60 
MarkCpuValid(uint32_t cpu)61   void MarkCpuValid(uint32_t cpu) {
62     PERFETTO_CHECK(cpu < kMaxCpusPerMachine);
63     if (PERFETTO_LIKELY(cpu_ids_[cpu])) {
64       return;
65     }
66     auto ucpu = ucpu_offset_ + cpu;
67     auto& cpu_table = *context_->storage->mutable_cpu_table();
68     cpu_table[ucpu].set_cpu(cpu);
69   }
70 
71   // Sets or updates the information for the specified CPU in the CpuTable.
72   tables::CpuTable::Id SetCpuInfo(uint32_t cpu,
73                                   base::StringView processor,
74                                   uint32_t cluster_id,
75                                   std::optional<uint32_t> capacity);
76 
77  private:
78   TraceProcessorContext* const context_;
79 
80   // Tracks the mapping of CPU number to CpuTable::Id of the current
81   // machine.
82   std::bitset<kMaxCpusPerMachine> cpu_ids_;
83   uint32_t ucpu_offset_ = 0;
84 };
85 
86 }  // namespace perfetto::trace_processor
87 
88 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_CPU_TRACKER_H_
89