• 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_SCHED_EVENT_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCHED_EVENT_TRACKER_H_
19 
20 #include <cstdint>
21 
22 #include "perfetto/public/compiler.h"
23 #include "src/trace_processor/importers/common/cpu_tracker.h"
24 #include "src/trace_processor/importers/common/event_tracker.h"
25 #include "src/trace_processor/storage/trace_storage.h"
26 #include "src/trace_processor/types/destructible.h"
27 #include "src/trace_processor/types/trace_processor_context.h"
28 
29 namespace perfetto::trace_processor {
30 
31 // Tracks per-cpu scheduling events, storing them as slices in the |sched|
32 // table.
33 class SchedEventTracker : public Destructible {
34  public:
35   PERFETTO_ALWAYS_INLINE
SchedEventTracker(TraceProcessorContext * context)36   explicit SchedEventTracker(TraceProcessorContext* context)
37       : context_(context) {}
38   SchedEventTracker(const SchedEventTracker&) = delete;
39   ~SchedEventTracker() override;
40 
41   PERFETTO_ALWAYS_INLINE
AddStartSlice(uint32_t cpu,int64_t ts,UniqueTid next_utid,int32_t next_prio)42   uint32_t AddStartSlice(uint32_t cpu,
43                          int64_t ts,
44                          UniqueTid next_utid,
45                          int32_t next_prio) {
46     // Open a new scheduling slice, corresponding to the task that was
47     // just switched to. Set the duration to -1, to indicate that the event is
48     // not finished. Duration will be updated later after event finish.
49     auto* sched = context_->storage->mutable_sched_slice_table();
50     // Get the unique CPU Id over all machines from the CPU table.
51     auto ucpu = context_->cpu_tracker->GetOrCreateCpu(cpu);
52     auto row_and_id = sched->Insert(
53         {ts, /* duration */ -1, next_utid, kNullStringId, next_prio, ucpu});
54     SchedId sched_id = row_and_id.id;
55     return sched->FindById(sched_id)->ToRowNumber().row_number();
56   }
57 
58   PERFETTO_ALWAYS_INLINE
ClosePendingSlice(uint32_t pending_slice_idx,int64_t ts,StringId prev_state)59   void ClosePendingSlice(uint32_t pending_slice_idx,
60                          int64_t ts,
61                          StringId prev_state) {
62     auto* slices = context_->storage->mutable_sched_slice_table();
63     auto r = (*slices)[pending_slice_idx];
64     r.set_dur(ts - r.ts());
65     r.set_end_state(prev_state);
66   }
67 
68  private:
69   TraceProcessorContext* const context_;
70 };
71 
72 }  // namespace perfetto::trace_processor
73 
74 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCHED_EVENT_TRACKER_H_
75