Chinglin Yu | 76788d2 | 2024-02-21 15:49:43 +0800 | [diff] [blame] | 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 | |
Lalit Maganti | 1e7aef1 | 2024-10-13 14:19:09 +0100 | [diff] [blame] | 17 | #include "perfetto/ext/base/clock_snapshots.h" |
Chinglin Yu | 76788d2 | 2024-02-21 15:49:43 +0800 | [diff] [blame] | 18 | |
| 19 | #include "perfetto/base/build_config.h" |
| 20 | #include "perfetto/base/time.h" |
| 21 | #include "protos/perfetto/common/builtin_clock.pbzero.h" |
| 22 | |
Lalit Maganti | 1e7aef1 | 2024-10-13 14:19:09 +0100 | [diff] [blame] | 23 | namespace perfetto::base { |
Chinglin Yu | 76788d2 | 2024-02-21 15:49:43 +0800 | [diff] [blame] | 24 | |
| 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. |
Daniele Di Proietto | 5cee71f | 2024-02-22 11:44:22 +0000 | [diff] [blame] | 63 | snapshot_data.push_back( |
Chinglin Yu | 76788d2 | 2024-02-21 15:49:43 +0800 | [diff] [blame] | 64 | ClockReading(protos::pbzero::BUILTIN_CLOCK_BOOTTIME, wall_time_ns)); |
Daniele Di Proietto | 5cee71f | 2024-02-22 11:44:22 +0000 | [diff] [blame] | 65 | snapshot_data.push_back( |
Chinglin Yu | 76788d2 | 2024-02-21 15:49:43 +0800 | [diff] [blame] | 66 | ClockReading(protos::pbzero::BUILTIN_CLOCK_MONOTONIC, wall_time_ns)); |
| 67 | #endif |
| 68 | |
Oystein Eftevaag | f0feebb | 2024-03-20 12:48:50 -0700 | [diff] [blame] | 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 | |
Chinglin Yu | 76788d2 | 2024-02-21 15:49:43 +0800 | [diff] [blame] | 75 | return snapshot_data; |
| 76 | } |
| 77 | |
| 78 | } // namespace perfetto |