• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Linux implementation of the clock 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.h"
10 #include "hdr/time_macros.h"
11 #include "src/__support/CPP/limits.h"
12 #include "src/__support/common.h"
13 #include "src/__support/time/linux/clock_gettime.h"
14 #include "src/__support/time/units.h"
15 #include "src/errno/libc_errno.h"
16 
17 namespace LIBC_NAMESPACE {
18 
19 LLVM_LIBC_FUNCTION(clock_t, clock, ()) {
20   using namespace time_units;
21   struct timespec ts;
22   auto result = internal::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
23   if (!result.has_value()) {
24     libc_errno = result.error();
25     return -1;
26   }
27 
28   // The above syscall gets the CPU time in seconds plus nanoseconds.
29   // The standard requires that we return clock_t(-1) if we cannot represent
30   // clocks as a clock_t value.
31   constexpr clock_t CLOCK_SECS_MAX =
32       cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC;
33   if (ts.tv_sec > CLOCK_SECS_MAX)
34     return clock_t(-1);
35   if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec)
36     return clock_t(-1);
37 
38   // For the integer computation converting tv_nsec to clocks to work
39   // correctly, we want CLOCKS_PER_SEC to be less than 1000000000.
40   static_assert(1_s_ns > CLOCKS_PER_SEC,
41                 "Expected CLOCKS_PER_SEC to be less than 1'000'000'000.");
42   return clock_t(ts.tv_sec * CLOCKS_PER_SEC +
43                  ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC));
44 }
45 
46 } // namespace LIBC_NAMESPACE
47