• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_EVENT_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_EVENT_TRACKER_H_
19 
20 #include <array>
21 #include <limits>
22 
23 #include "perfetto/base/string_view.h"
24 #include "perfetto/base/utils.h"
25 #include "src/trace_processor/trace_storage.h"
26 
27 namespace perfetto {
28 namespace trace_processor {
29 
30 class TraceProcessorContext;
31 
32 // This class takes sched events from the trace and processes them to store
33 // as sched slices.
34 class EventTracker {
35  public:
36   explicit EventTracker(TraceProcessorContext*);
37   EventTracker(const EventTracker&) = delete;
38   EventTracker& operator=(const EventTracker&) = delete;
39   virtual ~EventTracker();
40 
41   // This method is called when a sched switch event is seen in the trace.
42   virtual void PushSchedSwitch(uint32_t cpu,
43                                int64_t timestamp,
44                                uint32_t prev_pid,
45                                base::StringView prev_comm,
46                                int32_t prev_prio,
47                                int64_t prev_state,
48                                uint32_t next_pid,
49                                base::StringView next_comm,
50                                int32_t next_prio);
51 
52   // This method is called when a counter event is seen in the trace.
53   virtual RowId PushCounter(int64_t timestamp,
54                             double value,
55                             StringId name_id,
56                             int64_t ref,
57                             RefType ref_type,
58                             bool resolve_utid_to_upid = false);
59 
60   // This method is called when a instant event is seen in the trace.
61   virtual RowId PushInstant(int64_t timestamp,
62                             StringId name_id,
63                             double value,
64                             int64_t ref,
65                             RefType ref_type,
66                             bool resolve_utid_to_upid = false);
67 
68   // Called at the end of trace to flush any events which are pending to the
69   // storage.
70   void FlushPendingEvents();
71 
72  private:
73   // Represents a slice which is currently pending.
74   struct PendingSchedSlice {
75     size_t storage_index = std::numeric_limits<size_t>::max();
76     uint32_t next_pid = 0;
77   };
78 
79   // Represents a counter event which is currently pending upid resolution.
80   struct PendingUpidResolutionCounter {
81     uint32_t row = 0;
82     StringId name_id = 0;
83     UniqueTid utid = 0;
84   };
85 
86   // Represents a instant event which is currently pending upid resolution.
87   struct PendingUpidResolutionInstant {
88     uint32_t row = 0;
89     UniqueTid utid = 0;
90   };
91 
92   // Store pending sched slices for each CPU.
93   std::array<PendingSchedSlice, base::kMaxCpus> pending_sched_per_cpu_{};
94 
95   // Store the rows in the counters table which need upids resolved.
96   std::vector<PendingUpidResolutionCounter> pending_upid_resolution_counter_;
97 
98   // Store the rows in the instants table which need upids resolved.
99   std::vector<PendingUpidResolutionInstant> pending_upid_resolution_instant_;
100 
101   // Timestamp of the previous event. Used to discard events arriving out
102   // of order.
103   int64_t prev_timestamp_ = 0;
104 
105   static constexpr uint8_t kSchedSwitchMaxFieldId = 7;
106   std::array<StringId, kSchedSwitchMaxFieldId + 1> sched_switch_field_ids_;
107   StringId sched_switch_id_;
108 
109   TraceProcessorContext* const context_;
110 };
111 }  // namespace trace_processor
112 }  // namespace perfetto
113 
114 #endif  // SRC_TRACE_PROCESSOR_EVENT_TRACKER_H_
115