• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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_CLOCK_TRACKER_H_
18 #define SRC_TRACE_PROCESSOR_CLOCK_TRACKER_H_
19 
20 #include <stdint.h>
21 
22 #include <array>
23 #include <vector>
24 
25 #include "perfetto/base/logging.h"
26 #include "perfetto/base/optional.h"
27 
28 namespace perfetto {
29 namespace trace_processor {
30 
31 class TraceProcessorContext;
32 
33 enum ClockDomain : uint32_t {
34   kBootTime,   // Monotonic, counts also time in suspend mode.
35   kMonotonic,  // Monotonic, doesn't advance when the device is suspended.
36   kRealTime,   // Real time clock, can move backward (e.g. NTP adjustements).
37   kNumClockDomains
38 };
39 
40 class ClockTracker {
41  public:
42   explicit ClockTracker(TraceProcessorContext*);
43   virtual ~ClockTracker();
44 
45   // Push a snapshot that tells what is the corresponding trace time for the
46   // given |clock_time_ns| in the given clock domain. This is typically called
47   // by the code that reads the ClockSnapshot packet.
48   void SyncClocks(ClockDomain, int64_t clock_time_ns, int64_t trace_time_ns);
49 
50   // Converts the passed time in the given clock domain to the global trace
51   // time (CLOCK_BOOTTIME for Android traces).
52   base::Optional<int64_t> ToTraceTime(ClockDomain, int64_t clock_time_ns);
53 
GetFirstTimestamp(ClockDomain domain)54   int64_t GetFirstTimestamp(ClockDomain domain) const {
55     PERFETTO_DCHECK(!clocks_[domain].empty());
56     return clocks_[domain].front().clock_time_ns;
57   }
58 
59  private:
60   ClockTracker(const ClockTracker&) = delete;
61   ClockTracker& operator=(const ClockTracker&) = delete;
62 
63   struct ClockSnapshot {
64     int64_t clock_time_ns;
65     int64_t trace_time_ns;
66   };
67 
68   TraceProcessorContext* const context_;
69   using ClockSnapshotVector = std::vector<ClockSnapshot>;
70   std::array<ClockSnapshotVector, kNumClockDomains> clocks_;
71 };
72 
73 }  // namespace trace_processor
74 }  // namespace perfetto
75 
76 #endif  // SRC_TRACE_PROCESSOR_CLOCK_TRACKER_H_
77