• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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_CLOCK_CONVERTER_H_
18 #define SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_CLOCK_CONVERTER_H_
19 
20 #include <stdint.h>
21 
22 #include <array>
23 #include <cinttypes>
24 #include <map>
25 #include <random>
26 #include <set>
27 #include <vector>
28 
29 #include "perfetto/base/logging.h"
30 #include "perfetto/ext/base/status_or.h"
31 #include "perfetto/ext/base/string_utils.h"
32 #include "src/trace_processor/storage/trace_storage.h"
33 #include "src/trace_processor/types/trace_processor_context.h"
34 
35 #include "protos/perfetto/common/builtin_clock.pbzero.h"
36 #include "protos/perfetto/trace/clock_snapshot.pbzero.h"
37 
38 namespace perfetto {
39 namespace trace_processor {
40 
41 // Used for conversion to REAL and MONO clocks for provided timestamps. Can only
42 // be used after trace parsing. Only works if there has been at least one
43 // snapshot with a target clock. Data is based on clock snapshots table.
44 class ClockConverter {
45  public:
46   using ClockId = int64_t;
47   using Timestamp = int64_t;
48 
49   explicit ClockConverter(TraceProcessorContext*);
50 
51   // Converts trace time to REAL clock as string.
ToAbsTime(Timestamp ts)52   base::StatusOr<std::string> ToAbsTime(Timestamp ts) {
53     base::StatusOr<Timestamp> real_ts = FromTraceTime(kRealClock, ts);
54     if (!real_ts.ok())
55       return real_ts.status();
56     return TimeToStr(*real_ts);
57   }
58 
59   // Converts trace time to MONO clock time.
ToMonotonic(Timestamp ts)60   base::StatusOr<Timestamp> ToMonotonic(Timestamp ts) {
61     return FromTraceTime(kMonoClock, ts);
62   }
63 
64  private:
65   static constexpr int64_t kRealClock =
66       protos::pbzero::ClockSnapshot::Clock::REALTIME;
67   static constexpr int64_t kMonoClock = protos::pbzero::BUILTIN_CLOCK_MONOTONIC;
68 
69   // Timeline uses Trace Time clock as keys and other clocks time as values.
70   using Timeline = std::map<Timestamp, Timestamp>;
71 
72   // Reads the clocks snapshots table and fetches the data required for
73   // conversion. We initialize timelines of only selected clocks to minimize
74   // memory usage. Currently those are MONO and REAL clocks.
75   void MaybeInitialize();
76 
77   // Converts trace time to provided clock.
78   base::StatusOr<Timestamp> FromTraceTime(ClockId, Timestamp);
79 
80   // Converts timestamp to string.
81   std::string TimeToStr(Timestamp);
82 
83   TraceProcessorContext* context_;
84   bool is_initialized = false;
85   base::FlatHashMap<ClockId, Timeline> timelines_;
86 };
87 
88 }  // namespace trace_processor
89 }  // namespace perfetto
90 
91 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_CLOCK_CONVERTER_H_
92