/src/php-src/Zend/zend_hrtime.c
Line | Count | Source |
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 | | #include "zend.h" |
19 | | #include "zend_hrtime.h" |
20 | | |
21 | | /* This file reuses code parts from the cross-platform timer library |
22 | | Public Domain - 2011 Mattias Jansson / Rampant Pixels */ |
23 | | |
24 | | #if ZEND_HRTIME_PLATFORM_POSIX |
25 | | |
26 | | # include <unistd.h> |
27 | | # include <time.h> |
28 | | # include <string.h> |
29 | | |
30 | | ZEND_API clockid_t zend_hrtime_posix_clock_id = CLOCK_MONOTONIC; |
31 | | |
32 | | #elif ZEND_HRTIME_PLATFORM_WINDOWS |
33 | | |
34 | | # define WIN32_LEAN_AND_MEAN |
35 | | |
36 | | ZEND_API double zend_hrtime_timer_scale = .0; |
37 | | |
38 | | #elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE |
39 | | |
40 | | # include <mach/mach_time.h> |
41 | | # include <string.h> |
42 | | ZEND_API mach_timebase_info_data_t zend_hrtime_timerlib_info = { |
43 | | .numer = 0, |
44 | | .denom = 1, |
45 | | }; |
46 | | |
47 | | #elif ZEND_HRTIME_PLATFORM_HPUX |
48 | | |
49 | | # include <sys/time.h> |
50 | | |
51 | | #elif ZEND_HRTIME_PLATFORM_AIX |
52 | | |
53 | | # include <sys/time.h> |
54 | | # include <sys/systemcfg.h> |
55 | | |
56 | | #endif |
57 | | |
58 | | void zend_startup_hrtime(void) |
59 | 16 | { |
60 | | #if ZEND_HRTIME_PLATFORM_WINDOWS |
61 | | |
62 | | LARGE_INTEGER tf = {0}; |
63 | | if (QueryPerformanceFrequency(&tf) || 0 != tf.QuadPart) { |
64 | | zend_hrtime_timer_scale = (double)ZEND_NANO_IN_SEC / (zend_hrtime_t)tf.QuadPart; |
65 | | } |
66 | | |
67 | | #elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE |
68 | | |
69 | | mach_timebase_info(&zend_hrtime_timerlib_info); |
70 | | |
71 | | #elif ZEND_HRTIME_PLATFORM_POSIX |
72 | | |
73 | 16 | struct timespec ts; |
74 | | |
75 | 16 | #ifdef CLOCK_MONOTONIC_RAW |
76 | 16 | if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) { |
77 | 16 | zend_hrtime_posix_clock_id = CLOCK_MONOTONIC_RAW; |
78 | 16 | return; |
79 | 16 | } |
80 | 0 | #endif |
81 | | |
82 | 0 | if (EXPECTED(0 == clock_gettime(zend_hrtime_posix_clock_id, &ts))) { |
83 | 0 | return; |
84 | 0 | } |
85 | | |
86 | | // zend_error mechanism is not initialized at that point |
87 | 0 | fprintf(stderr, "No working CLOCK_MONOTONIC* found, this should never happen\n"); |
88 | 0 | abort(); |
89 | |
|
90 | 0 | #endif |
91 | 0 | } |