/src/php-src/Zend/zend_hrtime.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Author: Niklas Keller <kelunik@php.net> | |
12 | | | Author: Anatol Belski <ab@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | #include "zend.h" |
17 | | #include "zend_hrtime.h" |
18 | | |
19 | | /* This file reuses code parts from the cross-platform timer library |
20 | | Public Domain - 2011 Mattias Jansson / Rampant Pixels */ |
21 | | |
22 | | #if ZEND_HRTIME_PLATFORM_POSIX |
23 | | |
24 | | # include <unistd.h> |
25 | | # include <time.h> |
26 | | # include <string.h> |
27 | | |
28 | | ZEND_API clockid_t zend_hrtime_posix_clock_id = CLOCK_MONOTONIC; |
29 | | |
30 | | #elif ZEND_HRTIME_PLATFORM_WINDOWS |
31 | | |
32 | | # define WIN32_LEAN_AND_MEAN |
33 | | |
34 | | ZEND_API double zend_hrtime_timer_scale = .0; |
35 | | |
36 | | #elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE |
37 | | |
38 | | # include <mach/mach_time.h> |
39 | | # include <string.h> |
40 | | ZEND_API mach_timebase_info_data_t zend_hrtime_timerlib_info = { |
41 | | .numer = 0, |
42 | | .denom = 1, |
43 | | }; |
44 | | |
45 | | #elif ZEND_HRTIME_PLATFORM_HPUX |
46 | | |
47 | | # include <sys/time.h> |
48 | | |
49 | | #elif ZEND_HRTIME_PLATFORM_AIX |
50 | | |
51 | | # include <sys/time.h> |
52 | | # include <sys/systemcfg.h> |
53 | | |
54 | | #endif |
55 | | |
56 | | void zend_startup_hrtime(void) |
57 | 2 | { |
58 | | #if ZEND_HRTIME_PLATFORM_WINDOWS |
59 | | |
60 | | LARGE_INTEGER tf = {0}; |
61 | | if (QueryPerformanceFrequency(&tf) || 0 != tf.QuadPart) { |
62 | | zend_hrtime_timer_scale = (double)ZEND_NANO_IN_SEC / (zend_hrtime_t)tf.QuadPart; |
63 | | } |
64 | | |
65 | | #elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE |
66 | | |
67 | | mach_timebase_info(&zend_hrtime_timerlib_info); |
68 | | |
69 | | #elif ZEND_HRTIME_PLATFORM_POSIX |
70 | | |
71 | 2 | struct timespec ts; |
72 | | |
73 | 2 | #ifdef CLOCK_MONOTONIC_RAW |
74 | 2 | if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC_RAW, &ts))) { |
75 | 2 | zend_hrtime_posix_clock_id = CLOCK_MONOTONIC_RAW; |
76 | 2 | return; |
77 | 2 | } |
78 | 0 | #endif |
79 | | |
80 | 0 | if (EXPECTED(0 == clock_gettime(zend_hrtime_posix_clock_id, &ts))) { |
81 | 0 | return; |
82 | 0 | } |
83 | | |
84 | | // zend_error mechanism is not initialized at that point |
85 | 0 | fprintf(stderr, "No working CLOCK_MONOTONIC* found, this should never happen\n"); |
86 | 0 | abort(); |
87 | |
|
88 | 0 | #endif |
89 | 0 | } |