xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/ftrace/ftrace_sched_event_tracker.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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_FTRACE_FTRACE_SCHED_EVENT_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_FTRACE_FTRACE_SCHED_EVENT_TRACKER_H_
19 
20 #include <cstdint>
21 
22 #include <array>
23 
24 #include "perfetto/ext/base/string_view.h"
25 #include "src/trace_processor/importers/common/sched_event_state.h"
26 #include "src/trace_processor/storage/trace_storage.h"
27 #include "src/trace_processor/types/destructible.h"
28 #include "src/trace_processor/types/trace_processor_context.h"
29 
30 namespace perfetto {
31 namespace trace_processor {
32 
33 class EventTracker;
34 
35 // Tracks sched events and stores them into the storage as sched slices.
36 class FtraceSchedEventTracker : public Destructible {
37  public:
38   explicit FtraceSchedEventTracker(TraceProcessorContext*);
39   ~FtraceSchedEventTracker() override;
40 
41   FtraceSchedEventTracker(const FtraceSchedEventTracker&) = delete;
42   FtraceSchedEventTracker& operator=(const FtraceSchedEventTracker&) = delete;
43 
GetOrCreate(TraceProcessorContext * context)44   static FtraceSchedEventTracker* GetOrCreate(TraceProcessorContext* context) {
45     if (!context->ftrace_sched_tracker) {
46       context->ftrace_sched_tracker.reset(new FtraceSchedEventTracker(context));
47     }
48     return static_cast<FtraceSchedEventTracker*>(
49         context->ftrace_sched_tracker.get());
50   }
51 
52   // This method is called when a sched_switch event is seen in the trace.
53   // Virtual for testing.
54   virtual void PushSchedSwitch(uint32_t cpu,
55                                int64_t timestamp,
56                                uint32_t prev_pid,
57                                base::StringView prev_comm,
58                                int32_t prev_prio,
59                                int64_t prev_state,
60                                uint32_t next_pid,
61                                base::StringView next_comm,
62                                int32_t next_prio);
63 
64   void AddRawSchedSwitchEvent(uint32_t cpu,
65                               int64_t ts,
66                               UniqueTid prev_utid,
67                               uint32_t prev_pid,
68                               StringId prev_comm_id,
69                               int32_t prev_prio,
70                               int64_t prev_state,
71                               uint32_t next_pid,
72                               StringId next_comm_id,
73                               int32_t next_prio);
74 
75   // This method is called when parsing a sched_switch encoded in the compact
76   // format.
77   void PushSchedSwitchCompact(uint32_t cpu,
78                               int64_t ts,
79                               int64_t prev_state,
80                               uint32_t next_pid,
81                               int32_t next_prio,
82                               StringId next_comm_id,
83                               bool parse_only_into_raw);
84 
85   // This method is called when parsing a sched_waking encoded in the compact
86   // format. Note that the default encoding is handled by
87   // |EventTracker::PushInstant|.
88   void PushSchedWakingCompact(uint32_t cpu,
89                               int64_t ts,
90                               uint32_t wakee_pid,
91                               uint16_t target_cpu,
92                               uint16_t prio,
93                               StringId comm_id,
94                               uint16_t common_flags,
95                               bool parse_only_into_raw);
96 
97  private:
98   StringId TaskStateToStringId(int64_t task_state_int);
99 
100   static constexpr uint8_t kSchedSwitchMaxFieldId = 7;
101   std::array<StringId, kSchedSwitchMaxFieldId + 1> sched_switch_field_ids_;
102   StringId sched_switch_id_;
103 
104   static constexpr uint8_t kSchedWakingMaxFieldId = 5;
105   std::array<StringId, kSchedWakingMaxFieldId + 1> sched_waking_field_ids_;
106   StringId sched_waking_id_;
107 
108   TraceProcessorContext* const context_;
109 
110   SchedEventState sched_event_state_;
111 };
112 
113 }  // namespace trace_processor
114 }  // namespace perfetto
115 
116 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_FTRACE_FTRACE_SCHED_EVENT_TRACKER_H_
117