/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #if defined USE_POSIX_TIME #include #elif defined __MACH__ #include #else #include #endif #ifdef USE_POSIX_TIME namespace { const double NANOSECONDS_IN_A_SECOND = 1000000000; } // namespace #endif #ifdef __MACH__ namespace { inline double getConversionFactor() { double conversionFactor; mach_timebase_info_data_t info; mach_timebase_info(&info); conversionFactor = static_cast(info.numer) / info.denom; return conversionFactor; } } // namespace #endif namespace facebook::react { #if defined USE_POSIX_TIME inline double getCPUTimeNanos() { struct timespec time {}; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time); return static_cast(time.tv_sec) * NANOSECONDS_IN_A_SECOND + static_cast(time.tv_nsec); } inline bool hasAccurateCPUTimeNanosForBenchmarks() { return true; } #elif defined __MACH__ inline double getCPUTimeNanos() { static auto conversionFactor = getConversionFactor(); uint64_t time = mach_absolute_time(); return static_cast(time) * conversionFactor; } inline bool hasAccurateCPUTimeNanosForBenchmarks() { return true; } #else inline double getCPUTimeNanos() { auto now = std::chrono::steady_clock::now(); return static_cast( std::chrono::duration_cast( now.time_since_epoch()) .count()); } inline bool hasAccurateCPUTimeNanosForBenchmarks() { return false; } #endif } // namespace facebook::react