| 1 | //===---------- GPU implementation of the clock_gettime function ----------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/time/clock_gettime.h" |
| 10 | |
| 11 | #include "src/__support/common.h" |
| 12 | #include "src/__support/macros/config.h" |
| 13 | #include "src/__support/time/clock_gettime.h" |
| 14 | #include "src/__support/time/gpu/time_utils.h" |
| 15 | |
| 16 | namespace LIBC_NAMESPACE_DECL { |
| 17 | namespace internal { |
| 18 | constexpr uint64_t TICKS_PER_SEC = 1000000000UL; |
| 19 | |
| 20 | ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) { |
| 21 | if (clockid != CLOCK_MONOTONIC || !ts) |
| 22 | return cpp::unexpected(-1); |
| 23 | |
| 24 | uint64_t ns_per_tick = TICKS_PER_SEC / GPU_CLOCKS_PER_SEC; |
| 25 | uint64_t ticks = gpu::fixed_frequency_clock(); |
| 26 | |
| 27 | ts->tv_nsec = (ticks * ns_per_tick) % TICKS_PER_SEC; |
| 28 | ts->tv_sec = (ticks * ns_per_tick) / TICKS_PER_SEC; |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | } // namespace internal |
| 33 | } // namespace LIBC_NAMESPACE_DECL |
| 34 | |