1 /* 2 +----------------------------------------------------------------------+ 3 | Copyright (c) The PHP Group | 4 +----------------------------------------------------------------------+ 5 | This source file is subject to version 3.01 of the PHP license, | 6 | that is bundled with this package in the file LICENSE, and is | 7 | available through the world-wide-web at the following url: | 8 | https://www.php.net/license/3_01.txt | 9 | If you did not receive a copy of the PHP license and are unable to | 10 | obtain it through the world-wide-web, please send a note to | 11 | license@php.net so we can mail you a copy immediately. | 12 +----------------------------------------------------------------------+ 13 | Author: Niklas Keller <kelunik@php.net> | 14 | Author: Anatol Belski <ab@php.net> | 15 +----------------------------------------------------------------------+ 16 */ 17 18 #ifndef ZEND_HRTIME_H 19 #define ZEND_HRTIME_H 20 21 #include "zend_portability.h" 22 #include "zend_types.h" 23 24 #ifdef HAVE_UNISTD_H 25 # include <unistd.h> 26 #endif 27 #ifndef PHP_WIN32 28 # include <time.h> 29 #endif 30 31 /* This file reuses code parts from the cross-platform timer library 32 Public Domain - 2011 Mattias Jansson / Rampant Pixels */ 33 34 #define ZEND_HRTIME_PLATFORM_POSIX 0 35 #define ZEND_HRTIME_PLATFORM_WINDOWS 0 36 #define ZEND_HRTIME_PLATFORM_APPLE 0 37 #define ZEND_HRTIME_PLATFORM_HPUX 0 38 #define ZEND_HRTIME_PLATFORM_AIX 0 39 40 #if defined(_POSIX_TIMERS) && ((_POSIX_TIMERS > 0) || defined(__OpenBSD__)) && defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC) 41 # undef ZEND_HRTIME_PLATFORM_POSIX 42 # define ZEND_HRTIME_PLATFORM_POSIX 1 43 #elif defined(_WIN32) || defined(_WIN64) 44 # undef ZEND_HRTIME_PLATFORM_WINDOWS 45 # define ZEND_HRTIME_PLATFORM_WINDOWS 1 46 #elif defined(__APPLE__) 47 # undef ZEND_HRTIME_PLATFORM_APPLE 48 # define ZEND_HRTIME_PLATFORM_APPLE 1 49 #elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__))) 50 # undef ZEND_HRTIME_PLATFORM_HPUX 51 # define ZEND_HRTIME_PLATFORM_HPUX 1 52 #elif defined(_AIX) 53 # undef ZEND_HRTIME_PLATFORM_AIX 54 # define ZEND_HRTIME_PLATFORM_AIX 1 55 #endif 56 57 #define ZEND_HRTIME_AVAILABLE (ZEND_HRTIME_PLATFORM_POSIX || ZEND_HRTIME_PLATFORM_WINDOWS || ZEND_HRTIME_PLATFORM_APPLE || ZEND_HRTIME_PLATFORM_HPUX || ZEND_HRTIME_PLATFORM_AIX) 58 59 BEGIN_EXTERN_C() 60 61 #if ZEND_HRTIME_PLATFORM_WINDOWS 62 63 ZEND_API extern double zend_hrtime_timer_scale; 64 65 #elif ZEND_HRTIME_PLATFORM_APPLE 66 67 # include <mach/mach_time.h> 68 # include <string.h> 69 ZEND_API extern mach_timebase_info_data_t zend_hrtime_timerlib_info; 70 71 #endif 72 73 #define ZEND_NANO_IN_SEC UINT64_C(1000000000) 74 75 typedef uint64_t zend_hrtime_t; 76 77 void zend_startup_hrtime(void); 78 zend_hrtime(void)79static zend_always_inline zend_hrtime_t zend_hrtime(void) 80 { 81 #if ZEND_HRTIME_PLATFORM_WINDOWS 82 LARGE_INTEGER lt = {0}; 83 QueryPerformanceCounter(<); 84 return (zend_hrtime_t)((zend_hrtime_t)lt.QuadPart * zend_hrtime_timer_scale); 85 #elif ZEND_HRTIME_PLATFORM_APPLE 86 return (zend_hrtime_t)mach_absolute_time() * zend_hrtime_timerlib_info.numer / zend_hrtime_timerlib_info.denom; 87 #elif ZEND_HRTIME_PLATFORM_POSIX 88 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 }; 89 if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC, &ts))) { 90 return ((zend_hrtime_t) ts.tv_sec * (zend_hrtime_t)ZEND_NANO_IN_SEC) + ts.tv_nsec; 91 } 92 return 0; 93 #elif ZEND_HRTIME_PLATFORM_HPUX 94 return (zend_hrtime_t) gethrtime(); 95 #elif ZEND_HRTIME_PLATFORM_AIX 96 timebasestruct_t t; 97 read_wall_time(&t, TIMEBASE_SZ); 98 time_base_to_time(&t, TIMEBASE_SZ); 99 return (zend_hrtime_t) t.tb_high * (zend_hrtime_t)NANO_IN_SEC + t.tb_low; 100 #else 101 return 0; 102 #endif 103 } 104 105 END_EXTERN_C() 106 107 #endif /* ZEND_HRTIME_H */ 108