xref: /aosp_15_r20/external/perfetto/src/base/clock_snapshots.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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 "perfetto/ext/base/clock_snapshots.h"
18 
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/base/time.h"
21 #include "protos/perfetto/common/builtin_clock.pbzero.h"
22 
23 namespace perfetto::base {
24 
CaptureClockSnapshots()25 ClockSnapshotVector CaptureClockSnapshots() {
26   ClockSnapshotVector snapshot_data;
27 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE) && \
28     !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) &&   \
29     !PERFETTO_BUILDFLAG(PERFETTO_OS_NACL)
30   struct {
31     clockid_t id;
32     protos::pbzero::BuiltinClock type;
33     struct timespec ts;
34   } clocks[] = {
35       {CLOCK_BOOTTIME, protos::pbzero::BUILTIN_CLOCK_BOOTTIME, {0, 0}},
36       {CLOCK_REALTIME_COARSE,
37        protos::pbzero::BUILTIN_CLOCK_REALTIME_COARSE,
38        {0, 0}},
39       {CLOCK_MONOTONIC_COARSE,
40        protos::pbzero::BUILTIN_CLOCK_MONOTONIC_COARSE,
41        {0, 0}},
42       {CLOCK_REALTIME, protos::pbzero::BUILTIN_CLOCK_REALTIME, {0, 0}},
43       {CLOCK_MONOTONIC, protos::pbzero::BUILTIN_CLOCK_MONOTONIC, {0, 0}},
44       {CLOCK_MONOTONIC_RAW,
45        protos::pbzero::BUILTIN_CLOCK_MONOTONIC_RAW,
46        {0, 0}},
47   };
48   // First snapshot all the clocks as atomically as we can.
49   for (auto& clock : clocks) {
50     if (clock_gettime(clock.id, &clock.ts) == -1)
51       PERFETTO_DLOG("clock_gettime failed for clock %d", clock.id);
52   }
53   for (auto& clock : clocks) {
54     snapshot_data.push_back(ClockReading(
55         static_cast<uint32_t>(clock.type),
56         static_cast<uint64_t>(base::FromPosixTimespec(clock.ts).count())));
57   }
58 #else  // OS_APPLE || OS_WIN && OS_NACL
59   auto wall_time_ns = static_cast<uint64_t>(base::GetWallTimeNs().count());
60   // The default trace clock is boot time, so we always need to emit a path to
61   // it. However since we don't actually have a boot time source on these
62   // platforms, pretend that wall time equals boot time.
63   snapshot_data.push_back(
64       ClockReading(protos::pbzero::BUILTIN_CLOCK_BOOTTIME, wall_time_ns));
65   snapshot_data.push_back(
66       ClockReading(protos::pbzero::BUILTIN_CLOCK_MONOTONIC, wall_time_ns));
67 #endif
68 
69 #if PERFETTO_BUILDFLAG(PERFETTO_ARCH_CPU_X86_64)
70   // X86-specific but OS-independent TSC clocksource
71   snapshot_data.push_back(
72       ClockReading(protos::pbzero::BUILTIN_CLOCK_TSC, base::Rdtsc()));
73 #endif  // PERFETTO_BUILDFLAG(PERFETTO_ARCH_CPU_X86_64)
74 
75   return snapshot_data;
76 }
77 
78 }  // namespace perfetto
79