• 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 "src/trace_processor/importers/common/event_tracker.h"
21 
22 #include "src/trace_processor/importers/common/cpu_tracker.h"
23 #include "src/trace_processor/storage/trace_storage.h"
24 #include "src/trace_processor/types/destructible.h"
25 #include "src/trace_processor/types/trace_processor_context.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 
30 // Tracks per-cpu scheduling events, storing them as slices in the |sched|
31 // table.
32 class SchedEventTracker : public Destructible {
33  public:
34   PERFETTO_ALWAYS_INLINE
SchedEventTracker(TraceProcessorContext * context)35   explicit SchedEventTracker(TraceProcessorContext* context)
36       : context_(context) {}
37   SchedEventTracker(const SchedEventTracker&) = delete;
38   ~SchedEventTracker() override;
39 
40   PERFETTO_ALWAYS_INLINE
AddStartSlice(uint32_t cpu,int64_t ts,UniqueTid next_utid,int32_t next_prio)41   uint32_t AddStartSlice(uint32_t cpu,
42                          int64_t ts,
43                          UniqueTid next_utid,
44                          int32_t next_prio) {
45     // Open a new scheduling slice, corresponding to the task that was
46     // just switched to. Set the duration to -1, to indicate that the event is
47     // not finished. Duration will be updated later after event finish.
48     auto* sched = context_->storage->mutable_sched_slice_table();
49     // Get the unique CPU Id over all machines from the CPU table.
50     auto ucpu = context_->cpu_tracker->GetOrCreateCpu(cpu);
51     auto row_and_id = sched->Insert(
52         {ts, /* duration */ -1, next_utid, kNullStringId, next_prio, ucpu});
53     SchedId sched_id = row_and_id.id;
54     return *sched->id().IndexOf(sched_id);
55   }
56 
57   PERFETTO_ALWAYS_INLINE
UpdateEventTrackerTimestamp(int64_t ts,const char * event_name,size_t stats)58   bool UpdateEventTrackerTimestamp(int64_t ts,
59                                    const char* event_name,
60                                    size_t stats) {
61     // Post sorter stage, all events should be globally timestamp ordered.
62     int64_t max_ts = context_->event_tracker->max_timestamp();
63     if (ts < max_ts) {
64       PERFETTO_ELOG("%s event out of order by %.4f ms, skipping", event_name,
65                     static_cast<double>(max_ts - ts) / 1e6);
66       context_->storage->IncrementStats(stats);
67       return false;
68     }
69     context_->event_tracker->UpdateMaxTimestamp(ts);
70     return true;
71   }
72 
73   PERFETTO_ALWAYS_INLINE
ClosePendingSlice(uint32_t pending_slice_idx,int64_t ts,StringId prev_state)74   void ClosePendingSlice(uint32_t pending_slice_idx,
75                          int64_t ts,
76                          StringId prev_state) {
77     auto* slices = context_->storage->mutable_sched_slice_table();
78 
79     int64_t duration = ts - slices->ts()[pending_slice_idx];
80     slices->mutable_dur()->Set(pending_slice_idx, duration);
81 
82     // We store the state as a uint16 as we only consider values up to 2048
83     // when unpacking the information inside; this allows savings of 48 bits
84     // per slice.
85     slices->mutable_end_state()->Set(pending_slice_idx, prev_state);
86   }
87 
88  private:
89   TraceProcessorContext* const context_;
90 };
91 
92 }  // namespace trace_processor
93 }  // namespace perfetto
94 
95 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_SCHED_EVENT_TRACKER_H_
96