xref: /aosp_15_r20/external/llvm-libc/src/time/linux/clock.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1*71db0c75SAndroid Build Coastguard Worker //===-- Linux implementation of the clock function ------------------------===//
2*71db0c75SAndroid Build Coastguard Worker //
3*71db0c75SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*71db0c75SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
5*71db0c75SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*71db0c75SAndroid Build Coastguard Worker //
7*71db0c75SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
8*71db0c75SAndroid Build Coastguard Worker 
9*71db0c75SAndroid Build Coastguard Worker #include "src/time/clock.h"
10*71db0c75SAndroid Build Coastguard Worker #include "hdr/time_macros.h"
11*71db0c75SAndroid Build Coastguard Worker #include "src/__support/CPP/limits.h"
12*71db0c75SAndroid Build Coastguard Worker #include "src/__support/common.h"
13*71db0c75SAndroid Build Coastguard Worker #include "src/__support/macros/config.h"
14*71db0c75SAndroid Build Coastguard Worker #include "src/__support/time/linux/clock_gettime.h"
15*71db0c75SAndroid Build Coastguard Worker #include "src/__support/time/units.h"
16*71db0c75SAndroid Build Coastguard Worker #include "src/errno/libc_errno.h"
17*71db0c75SAndroid Build Coastguard Worker 
18*71db0c75SAndroid Build Coastguard Worker namespace LIBC_NAMESPACE_DECL {
19*71db0c75SAndroid Build Coastguard Worker 
20*71db0c75SAndroid Build Coastguard Worker LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
21*71db0c75SAndroid Build Coastguard Worker   using namespace time_units;
22*71db0c75SAndroid Build Coastguard Worker   struct timespec ts;
23*71db0c75SAndroid Build Coastguard Worker   auto result = internal::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
24*71db0c75SAndroid Build Coastguard Worker   if (!result.has_value()) {
25*71db0c75SAndroid Build Coastguard Worker     libc_errno = result.error();
26*71db0c75SAndroid Build Coastguard Worker     return -1;
27*71db0c75SAndroid Build Coastguard Worker   }
28*71db0c75SAndroid Build Coastguard Worker 
29*71db0c75SAndroid Build Coastguard Worker   // The above syscall gets the CPU time in seconds plus nanoseconds.
30*71db0c75SAndroid Build Coastguard Worker   // The standard requires that we return clock_t(-1) if we cannot represent
31*71db0c75SAndroid Build Coastguard Worker   // clocks as a clock_t value.
32*71db0c75SAndroid Build Coastguard Worker   constexpr clock_t CLOCK_SECS_MAX =
33*71db0c75SAndroid Build Coastguard Worker       cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC;
34*71db0c75SAndroid Build Coastguard Worker   if (ts.tv_sec > CLOCK_SECS_MAX)
35*71db0c75SAndroid Build Coastguard Worker     return clock_t(-1);
36*71db0c75SAndroid Build Coastguard Worker   if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec)
37*71db0c75SAndroid Build Coastguard Worker     return clock_t(-1);
38*71db0c75SAndroid Build Coastguard Worker 
39*71db0c75SAndroid Build Coastguard Worker   // For the integer computation converting tv_nsec to clocks to work
40*71db0c75SAndroid Build Coastguard Worker   // correctly, we want CLOCKS_PER_SEC to be less than 1000000000.
41*71db0c75SAndroid Build Coastguard Worker   static_assert(1_s_ns > CLOCKS_PER_SEC,
42*71db0c75SAndroid Build Coastguard Worker                 "Expected CLOCKS_PER_SEC to be less than 1'000'000'000.");
43*71db0c75SAndroid Build Coastguard Worker   return clock_t(ts.tv_sec * CLOCKS_PER_SEC +
44*71db0c75SAndroid Build Coastguard Worker                  ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC));
45*71db0c75SAndroid Build Coastguard Worker }
46*71db0c75SAndroid Build Coastguard Worker 
47*71db0c75SAndroid Build Coastguard Worker } // namespace LIBC_NAMESPACE_DECL
48