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