xref: /aosp_15_r20/external/perfetto/src/trace_processor/importers/common/clock_converter.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 REAL clock time.
ToRealtime(Timestamp ts)60   base::StatusOr<Timestamp> ToRealtime(Timestamp ts) {
61     return FromTraceTime(kRealClock, ts);
62   }
63 
64   // Converts trace time to MONO clock time.
ToMonotonic(Timestamp ts)65   base::StatusOr<Timestamp> ToMonotonic(Timestamp ts) {
66     return FromTraceTime(kMonoClock, ts);
67   }
68 
69  private:
70   static constexpr int64_t kRealClock =
71       protos::pbzero::ClockSnapshot::Clock::REALTIME;
72   static constexpr int64_t kMonoClock = protos::pbzero::BUILTIN_CLOCK_MONOTONIC;
73 
74   // Timeline uses Trace Time clock as keys and other clocks time as values.
75   using Timeline = std::map<Timestamp, Timestamp>;
76 
77   // Reads the clocks snapshots table and fetches the data required for
78   // conversion. We initialize timelines of only selected clocks to minimize
79   // memory usage. Currently those are MONO and REAL clocks.
80   void MaybeInitialize();
81 
82   // Converts trace time to provided clock.
83   base::StatusOr<Timestamp> FromTraceTime(ClockId, Timestamp);
84 
85   // Converts timestamp to string.
86   std::string TimeToStr(Timestamp);
87 
88   TraceProcessorContext* context_;
89   bool is_initialized = false;
90   base::FlatHashMap<ClockId, Timeline> timelines_;
91 };
92 
93 }  // namespace trace_processor
94 }  // namespace perfetto
95 
96 #endif  // SRC_TRACE_PROCESSOR_IMPORTERS_COMMON_CLOCK_CONVERTER_H_
97