• 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_IMPORTERS_COMMON_EVENT_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_EVENT_TRACKER_H_
19 
20 #include <array>
21 #include <limits>
22 
23 #include "perfetto/ext/base/string_view.h"
24 #include "perfetto/ext/base/utils.h"
25 #include "src/trace_processor/importers/common/args_tracker.h"
26 #include "src/trace_processor/storage/trace_storage.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
31 class TraceProcessorContext;
32 
33 // Tracks sched events, instants, and counters.
34 class EventTracker {
35  public:
36   using SetArgsCallback = std::function<void(ArgsTracker::BoundInserter*)>;
37 
38   explicit EventTracker(TraceProcessorContext*);
39   EventTracker(const EventTracker&) = delete;
40   EventTracker& operator=(const EventTracker&) = delete;
41   virtual ~EventTracker();
42 
43   // Adds a counter event to the counters table returning the index of the
44   // newly added row.
45   virtual base::Optional<CounterId> PushCounter(int64_t timestamp,
46                                                 double value,
47                                                 TrackId track_id);
48 
49   // Adds a counter event with args to the counters table returning the index of
50   // the newly added row.
51   base::Optional<CounterId> PushCounter(int64_t timestamp,
52                                         double value,
53                                         TrackId track_id,
54                                         SetArgsCallback args_callback);
55 
56   // Adds a counter event to the counters table for counter events which
57   // should be associated with a process but only have a thread context
58   // (e.g. rss_stat events).
59   //
60   // This function will resolve the utid to a upid when the events are
61   // flushed (see |FlushPendingEvents()|).
62   virtual base::Optional<CounterId> PushProcessCounterForThread(
63       int64_t timestamp,
64       double value,
65       StringId name_id,
66       UniqueTid utid);
67 
68   // This method is called when a instant event is seen in the trace.
69   virtual InstantId PushInstant(int64_t timestamp,
70                                 StringId name_id,
71                                 int64_t ref,
72                                 RefType ref_type,
73                                 bool resolve_utid_to_upid = false);
74 
75   // Called at the end of trace to flush any events which are pending to the
76   // storage.
77   void FlushPendingEvents();
78 
79   // For SchedEventTracker.
max_timestamp()80   int64_t max_timestamp() const { return max_timestamp_; }
UpdateMaxTimestamp(int64_t ts)81   void UpdateMaxTimestamp(int64_t ts) {
82     max_timestamp_ = std::max(ts, max_timestamp_);
83   }
84 
85  private:
86   // Represents a counter event which is currently pending upid resolution.
87   struct PendingUpidResolutionCounter {
88     uint32_t row = 0;
89     StringId name_id = kNullStringId;
90     UniqueTid utid = 0;
91   };
92 
93   // Represents a instant event which is currently pending upid resolution.
94   struct PendingUpidResolutionInstant {
95     uint32_t row = 0;
96     UniqueTid utid = 0;
97   };
98 
99   // Store the rows in the counters table which need upids resolved.
100   std::vector<PendingUpidResolutionCounter> pending_upid_resolution_counter_;
101 
102   // Store the rows in the instants table which need upids resolved.
103   std::vector<PendingUpidResolutionInstant> pending_upid_resolution_instant_;
104 
105   // Timestamp of the previous event. Used to discard events arriving out
106   // of order.
107   int64_t max_timestamp_ = 0;
108 
109   TraceProcessorContext* const context_;
110 };
111 }  // namespace trace_processor
112 }  // namespace perfetto
113 
114 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_EVENT_TRACKER_H_
115