• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 #include "src/trace_processor/storage/trace_storage.h"
23 #include "src/trace_processor/tables/metadata_tables_py.h"
24 #include "src/trace_processor/types/trace_processor_context.h"
25 
26 namespace perfetto::trace_processor {
27 
28 class TraceProcessorContext;
29 
30 class CpuTracker {
31  public:
32   // The CPU table Id serves as the 'ucpu' in sched_slice and related for
33   // joining with this table. To optimize for single-machine traces, this class
34   // assumes a maximum of |kMaxCpusPerMachine| CPUs per machine to maintain a
35   // relative order of |cpu| and |ucpu| by pre-allocating |kMaxCpusPerMachine|
36   // records in the CPU table. The mapping between |ucpu| and |cpu| becomes
37   // |cpu| = |ucpu| % |kMaxCpusPerMachine|.
38   static constexpr uint32_t kMaxCpusPerMachine = 4096;
39 
40   explicit CpuTracker(TraceProcessorContext*);
41   ~CpuTracker();
42 
GetOrCreateCpu(uint32_t cpu)43   tables::CpuTable::Id GetOrCreateCpu(uint32_t cpu) {
44     // CPU core number is in the range of 0..kMaxCpusPerMachine-1.
45     PERFETTO_CHECK(cpu < kMaxCpusPerMachine);
46     auto ucpu = ucpu_offset_ + cpu;
47     if (PERFETTO_LIKELY(cpu_ids_[cpu]))
48       return tables::CpuTable::Id(ucpu);
49 
50     // Populate the optional |cpu| column.
51     context_->storage->mutable_cpu_table()->mutable_cpu()->Set(ucpu, cpu);
52     return tables::CpuTable::Id(ucpu);
53   }
54 
55   // Sets or updates the information for the specified CPU in the CpuTable.
56   tables::CpuTable::Id SetCpuInfo(uint32_t cpu,
57                                   base::StringView processor,
58                                   uint32_t cluster_id);
59 
60  private:
61   TraceProcessorContext* const context_;
62 
63   // Tracks the mapping of CPU number to CpuTable::Id of the current
64   // machine.
65   std::bitset<kMaxCpusPerMachine> cpu_ids_;
66   uint32_t ucpu_offset_ = 0;
67 };
68 
69 }  // namespace perfetto::trace_processor
70 
71 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_CPU_TRACKER_H_
72