xref: /aosp_15_r20/external/perfetto/src/base/clock_snapshots.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1*6dbdd20aSAndroid Build Coastguard Worker /*
2*6dbdd20aSAndroid Build Coastguard Worker  * Copyright (C) 2024 The Android Open Source Project
3*6dbdd20aSAndroid Build Coastguard Worker  *
4*6dbdd20aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*6dbdd20aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*6dbdd20aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*6dbdd20aSAndroid Build Coastguard Worker  *
8*6dbdd20aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*6dbdd20aSAndroid Build Coastguard Worker  *
10*6dbdd20aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*6dbdd20aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*6dbdd20aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*6dbdd20aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*6dbdd20aSAndroid Build Coastguard Worker  * limitations under the License.
15*6dbdd20aSAndroid Build Coastguard Worker  */
16*6dbdd20aSAndroid Build Coastguard Worker 
17*6dbdd20aSAndroid Build Coastguard Worker #include "perfetto/ext/base/clock_snapshots.h"
18*6dbdd20aSAndroid Build Coastguard Worker 
19*6dbdd20aSAndroid Build Coastguard Worker #include "perfetto/base/build_config.h"
20*6dbdd20aSAndroid Build Coastguard Worker #include "perfetto/base/time.h"
21*6dbdd20aSAndroid Build Coastguard Worker #include "protos/perfetto/common/builtin_clock.pbzero.h"
22*6dbdd20aSAndroid Build Coastguard Worker 
23*6dbdd20aSAndroid Build Coastguard Worker namespace perfetto::base {
24*6dbdd20aSAndroid Build Coastguard Worker 
CaptureClockSnapshots()25*6dbdd20aSAndroid Build Coastguard Worker ClockSnapshotVector CaptureClockSnapshots() {
26*6dbdd20aSAndroid Build Coastguard Worker   ClockSnapshotVector snapshot_data;
27*6dbdd20aSAndroid Build Coastguard Worker #if !PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) && \
28*6dbdd20aSAndroid Build Coastguard Worker     !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) &&   \
29*6dbdd20aSAndroid Build Coastguard Worker     !PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
30*6dbdd20aSAndroid Build Coastguard Worker   struct {
31*6dbdd20aSAndroid Build Coastguard Worker     clockid_t id;
32*6dbdd20aSAndroid Build Coastguard Worker     protos::pbzero::BuiltinClock type;
33*6dbdd20aSAndroid Build Coastguard Worker     struct timespec ts;
34*6dbdd20aSAndroid Build Coastguard Worker   } clocks[] = {
35*6dbdd20aSAndroid Build Coastguard Worker       {CLOCK_BOOTTIME, protos::pbzero::BUILTIN_CLOCK_BOOTTIME, {0, 0}},
36*6dbdd20aSAndroid Build Coastguard Worker       {CLOCK_REALTIME_COARSE,
37*6dbdd20aSAndroid Build Coastguard Worker        protos::pbzero::BUILTIN_CLOCK_REALTIME_COARSE,
38*6dbdd20aSAndroid Build Coastguard Worker        {0, 0}},
39*6dbdd20aSAndroid Build Coastguard Worker       {CLOCK_MONOTONIC_COARSE,
40*6dbdd20aSAndroid Build Coastguard Worker        protos::pbzero::BUILTIN_CLOCK_MONOTONIC_COARSE,
41*6dbdd20aSAndroid Build Coastguard Worker        {0, 0}},
42*6dbdd20aSAndroid Build Coastguard Worker       {CLOCK_REALTIME, protos::pbzero::BUILTIN_CLOCK_REALTIME, {0, 0}},
43*6dbdd20aSAndroid Build Coastguard Worker       {CLOCK_MONOTONIC, protos::pbzero::BUILTIN_CLOCK_MONOTONIC, {0, 0}},
44*6dbdd20aSAndroid Build Coastguard Worker       {CLOCK_MONOTONIC_RAW,
45*6dbdd20aSAndroid Build Coastguard Worker        protos::pbzero::BUILTIN_CLOCK_MONOTONIC_RAW,
46*6dbdd20aSAndroid Build Coastguard Worker        {0, 0}},
47*6dbdd20aSAndroid Build Coastguard Worker   };
48*6dbdd20aSAndroid Build Coastguard Worker   // First snapshot all the clocks as atomically as we can.
49*6dbdd20aSAndroid Build Coastguard Worker   for (auto& clock : clocks) {
50*6dbdd20aSAndroid Build Coastguard Worker     if (clock_gettime(clock.id, &clock.ts) == -1)
51*6dbdd20aSAndroid Build Coastguard Worker       PERFETTO_DLOG("clock_gettime failed for clock %d", clock.id);
52*6dbdd20aSAndroid Build Coastguard Worker   }
53*6dbdd20aSAndroid Build Coastguard Worker   for (auto& clock : clocks) {
54*6dbdd20aSAndroid Build Coastguard Worker     snapshot_data.push_back(ClockReading(
55*6dbdd20aSAndroid Build Coastguard Worker         static_cast<uint32_t>(clock.type),
56*6dbdd20aSAndroid Build Coastguard Worker         static_cast<uint64_t>(base::FromPosixTimespec(clock.ts).count())));
57*6dbdd20aSAndroid Build Coastguard Worker   }
58*6dbdd20aSAndroid Build Coastguard Worker #else  // OS_APPLE || OS_WIN && OS_NACL
59*6dbdd20aSAndroid Build Coastguard Worker   auto wall_time_ns = static_cast<uint64_t>(base::GetWallTimeNs().count());
60*6dbdd20aSAndroid Build Coastguard Worker   // The default trace clock is boot time, so we always need to emit a path to
61*6dbdd20aSAndroid Build Coastguard Worker   // it. However since we don't actually have a boot time source on these
62*6dbdd20aSAndroid Build Coastguard Worker   // platforms, pretend that wall time equals boot time.
63*6dbdd20aSAndroid Build Coastguard Worker   snapshot_data.push_back(
64*6dbdd20aSAndroid Build Coastguard Worker       ClockReading(protos::pbzero::BUILTIN_CLOCK_BOOTTIME, wall_time_ns));
65*6dbdd20aSAndroid Build Coastguard Worker   snapshot_data.push_back(
66*6dbdd20aSAndroid Build Coastguard Worker       ClockReading(protos::pbzero::BUILTIN_CLOCK_MONOTONIC, wall_time_ns));
67*6dbdd20aSAndroid Build Coastguard Worker #endif
68*6dbdd20aSAndroid Build Coastguard Worker 
69*6dbdd20aSAndroid Build Coastguard Worker #if PERFETTO_BUILDFLAG(PERFETTO_ARCH_CPU_X86_64)
70*6dbdd20aSAndroid Build Coastguard Worker   // X86-specific but OS-independent TSC clocksource
71*6dbdd20aSAndroid Build Coastguard Worker   snapshot_data.push_back(
72*6dbdd20aSAndroid Build Coastguard Worker       ClockReading(protos::pbzero::BUILTIN_CLOCK_TSC, base::Rdtsc()));
73*6dbdd20aSAndroid Build Coastguard Worker #endif  // PERFETTO_BUILDFLAG(PERFETTO_ARCH_CPU_X86_64)
74*6dbdd20aSAndroid Build Coastguard Worker 
75*6dbdd20aSAndroid Build Coastguard Worker   return snapshot_data;
76*6dbdd20aSAndroid Build Coastguard Worker }
77*6dbdd20aSAndroid Build Coastguard Worker 
78*6dbdd20aSAndroid Build Coastguard Worker }  // namespace perfetto
79