xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/sched_event_state.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_SCHED_EVENT_STATE_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCHED_EVENT_STATE_H_
19 
20 #include <limits>
21 #include <vector>
22 
23 #include "src/trace_processor/storage/trace_storage.h"
24 
25 namespace perfetto {
26 namespace trace_processor {
27 
28 // Responsible for keeping the state of pending sched events.
29 // TODO(rsavitski): consider folding back into ftrace parser. The ETW parser is
30 // probably better off replicating its own pending state struct.
31 class SchedEventState {
32  public:
33   // Information retained from the preceding sched_switch seen on a given cpu.
34   struct PendingSchedInfo {
35     // The pending scheduling slice that the next event will complete.
36     uint32_t pending_slice_storage_idx = std::numeric_limits<uint32_t>::max();
37 
38     // pid/utid/prio corresponding to the last sched_switch seen on this cpu
39     // (its "next_*" fields). There is some duplication with respect to the
40     // slices storage, but we don't always have a slice when decoding events in
41     // the compact format.
42     uint32_t last_pid = std::numeric_limits<uint32_t>::max();
43     UniqueTid last_utid = std::numeric_limits<UniqueTid>::max();
44     int32_t last_prio = std::numeric_limits<int32_t>::max();
45   };
46 
SchedEventState()47   SchedEventState() {
48     // Pre-allocate space for 128 CPUs, which should be enough for most hosts.
49     // It's OK if this number is too small, the vector will be grown on-demand.
50     pending_sched_per_cpu_.reserve(128);
51   }
52   SchedEventState(const SchedEventState&) = delete;
53   ~SchedEventState() = default;
54 
55   // Get the sched info for the given CPU, resizing the vector if necessary.
GetPendingSchedInfoForCpu(uint32_t cpu)56   PendingSchedInfo* GetPendingSchedInfoForCpu(uint32_t cpu) {
57     if (PERFETTO_UNLIKELY(cpu >= pending_sched_per_cpu_.size())) {
58       pending_sched_per_cpu_.resize(cpu + 1);
59     }
60     return &pending_sched_per_cpu_[cpu];
61   }
62 
63  private:
64   // Information retained from the preceding sched_switch seen on a given cpu.
65   std::vector<PendingSchedInfo> pending_sched_per_cpu_;
66 };
67 
68 }  // namespace trace_processor
69 }  // namespace perfetto
70 
71 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCHED_EVENT_STATE_H_
72