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