1 /*
2 * Copyright (C) 2022 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 #include "src/trace_processor/importers/common/clock_converter.h"
18
19 #include <time.h>
20
21 #include <algorithm>
22 #include <atomic>
23 #include <cinttypes>
24 #include <queue>
25
26 #include "perfetto/base/logging.h"
27 #include "perfetto/ext/base/hash.h"
28 #include "src/trace_processor/storage/trace_storage.h"
29 #include "src/trace_processor/types/trace_processor_context.h"
30
31 #include "protos/perfetto/common/builtin_clock.pbzero.h"
32 #include "protos/perfetto/trace/clock_snapshot.pbzero.h"
33
34 namespace perfetto {
35 namespace trace_processor {
36
ClockConverter(TraceProcessorContext * context)37 ClockConverter::ClockConverter(TraceProcessorContext* context)
38 : context_(context) {}
39
MaybeInitialize()40 void ClockConverter::MaybeInitialize() {
41 if (is_initialized)
42 return;
43
44 is_initialized = true;
45 timelines_.Insert(kRealClock, {});
46 timelines_.Insert(kMonoClock, {});
47 for (auto it = context_->storage->clock_snapshot_table().IterateRows(); it;
48 ++it) {
49 if (it.clock_id() == kRealClock || it.clock_id() == kMonoClock)
50 timelines_.Find(it.clock_id())->emplace(it.ts(), it.clock_value());
51 }
52 }
53
FromTraceTime(ClockId clock_id,Timestamp ts)54 base::StatusOr<ClockConverter::Timestamp> ClockConverter::FromTraceTime(
55 ClockId clock_id,
56 Timestamp ts) {
57 MaybeInitialize();
58
59 Timeline* timeline = timelines_.Find(clock_id);
60 if (!timeline) {
61 return base::ErrStatus(
62 "Provided clock has not been found in the converter "
63 "clocks.");
64 }
65
66 if (timeline->empty()) {
67 return base::ErrStatus("Target clock is not in the trace.");
68 }
69
70 auto next_snapshot = timeline->lower_bound(ts);
71
72 // If lower bound was not found, it means that the ts was higher then the last
73 // one. If that's the case we look for thhe last element and return clock
74 // value for this + offset.
75 if (next_snapshot == timeline->end()) {
76 next_snapshot--;
77 return next_snapshot->second + ts - next_snapshot->first;
78 }
79
80 // If there is a snapshot with this ts or lower bound is the first snapshot,
81 // we have no other option then to return the clock value for this snapshot.
82 if (next_snapshot == timeline->begin() || next_snapshot->first == ts)
83 return next_snapshot->second;
84
85 auto prev_snapshot = next_snapshot;
86 prev_snapshot--;
87
88 // The most truthful way to calculate the clock value is to use this formula,
89 // as there is no reason to assume that the clock is monotonistic. This
90 // prevents us from going back in time.
91 return std::min(prev_snapshot->second + ts - prev_snapshot->first,
92 next_snapshot->second);
93 }
94
TimeToStr(Timestamp ts)95 std::string ClockConverter::TimeToStr(Timestamp ts) {
96 constexpr int64_t one_second_in_ns = 1LL * 1000LL * 1000LL * 1000LL;
97 int64_t s = ts / one_second_in_ns;
98 int64_t ns = ts % one_second_in_ns;
99
100 time_t time_s = static_cast<time_t>(s);
101 struct tm* time_tm = gmtime(&time_s);
102
103 int seconds = time_tm->tm_sec;
104 int minutes = time_tm->tm_min;
105 int hours = time_tm->tm_hour;
106 int day = time_tm->tm_mday;
107 int month = time_tm->tm_mon + 1;
108 int year = time_tm->tm_year + 1900;
109
110 base::StackString<64> buf("%04d-%02d-%02dT%02d:%02d:%02d.%09" PRId64, year,
111 month, day, hours, minutes, seconds, ns);
112 return buf.ToStdString();
113 }
114
115 } // namespace trace_processor
116 } // namespace perfetto
117