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