122ea0e5dSSiva Chandra Reddy //===-- Linux implementation of the clock function ------------------------===// 222ea0e5dSSiva Chandra Reddy // 322ea0e5dSSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 422ea0e5dSSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information. 522ea0e5dSSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 622ea0e5dSSiva Chandra Reddy // 722ea0e5dSSiva Chandra Reddy //===----------------------------------------------------------------------===// 822ea0e5dSSiva Chandra Reddy 922ea0e5dSSiva Chandra Reddy #include "src/time/clock.h" 10d8e73752SSchrodinger ZHU Yifan #include "hdr/time_macros.h" 1122ea0e5dSSiva Chandra Reddy #include "src/__support/CPP/limits.h" 1222ea0e5dSSiva Chandra Reddy #include "src/__support/common.h" 135ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 14*e6cf5d28SSchrodinger ZHU Yifan #include "src/__support/time/clock_gettime.h" 15d8e73752SSchrodinger ZHU Yifan #include "src/__support/time/units.h" 16b98c1906SRaman Tenneti #include "src/errno/libc_errno.h" 1722ea0e5dSSiva Chandra Reddy 185ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 1922ea0e5dSSiva Chandra Reddy 2022ea0e5dSSiva Chandra Reddy LLVM_LIBC_FUNCTION(clock_t, clock, ()) { 21d8e73752SSchrodinger ZHU Yifan using namespace time_units; 2222ea0e5dSSiva Chandra Reddy struct timespec ts; 23d8e73752SSchrodinger ZHU Yifan auto result = internal::clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); 24123bf084SMikhail R. Gadelha if (!result.has_value()) { 25123bf084SMikhail R. Gadelha libc_errno = result.error(); 26123bf084SMikhail R. Gadelha return -1; 2722ea0e5dSSiva Chandra Reddy } 2822ea0e5dSSiva Chandra Reddy 292dc97921SMichael Jones // The above syscall gets the CPU time in seconds plus nanoseconds. 3022ea0e5dSSiva Chandra Reddy // The standard requires that we return clock_t(-1) if we cannot represent 3122ea0e5dSSiva Chandra Reddy // clocks as a clock_t value. 3222ea0e5dSSiva Chandra Reddy constexpr clock_t CLOCK_SECS_MAX = 3322ea0e5dSSiva Chandra Reddy cpp::numeric_limits<clock_t>::max() / CLOCKS_PER_SEC; 3422ea0e5dSSiva Chandra Reddy if (ts.tv_sec > CLOCK_SECS_MAX) 3522ea0e5dSSiva Chandra Reddy return clock_t(-1); 36d8e73752SSchrodinger ZHU Yifan if (ts.tv_nsec / 1_s_ns > CLOCK_SECS_MAX - ts.tv_sec) 3722ea0e5dSSiva Chandra Reddy return clock_t(-1); 3822ea0e5dSSiva Chandra Reddy 3922ea0e5dSSiva Chandra Reddy // For the integer computation converting tv_nsec to clocks to work 4022ea0e5dSSiva Chandra Reddy // correctly, we want CLOCKS_PER_SEC to be less than 1000000000. 41d8e73752SSchrodinger ZHU Yifan static_assert(1_s_ns > CLOCKS_PER_SEC, 42d8e73752SSchrodinger ZHU Yifan "Expected CLOCKS_PER_SEC to be less than 1'000'000'000."); 4322ea0e5dSSiva Chandra Reddy return clock_t(ts.tv_sec * CLOCKS_PER_SEC + 44d8e73752SSchrodinger ZHU Yifan ts.tv_nsec / (1_s_ns / CLOCKS_PER_SEC)); 4522ea0e5dSSiva Chandra Reddy } 4622ea0e5dSSiva Chandra Reddy 475ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 48