Coverage Report

Created: 2025-06-13 06:43

/src/php-src/Zend/zend_hrtime.h
Line
Count
Source (jump to first uncovered line)
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_MACH_ABSOLUTE 0
37
#define ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC 0
38
#define ZEND_HRTIME_PLATFORM_HPUX    0
39
#define ZEND_HRTIME_PLATFORM_AIX     0
40
41
#if defined(_POSIX_TIMERS) && ((_POSIX_TIMERS > 0) || defined(__OpenBSD__)) && defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC)
42
# undef  ZEND_HRTIME_PLATFORM_POSIX
43
# define ZEND_HRTIME_PLATFORM_POSIX 1
44
#elif defined(_WIN32) || defined(_WIN64)
45
# undef  ZEND_HRTIME_PLATFORM_WINDOWS
46
# define ZEND_HRTIME_PLATFORM_WINDOWS 1
47
#elif HAVE_CLOCK_GETTIME_NSEC_NP
48
# undef  ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC
49
# define ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC 1
50
#elif defined(__APPLE__)
51
# undef  ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
52
# define ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE 1
53
#elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__)))
54
# undef  ZEND_HRTIME_PLATFORM_HPUX
55
# define ZEND_HRTIME_PLATFORM_HPUX 1
56
#elif defined(_AIX)
57
# undef  ZEND_HRTIME_PLATFORM_AIX
58
# define ZEND_HRTIME_PLATFORM_AIX 1
59
#endif
60
61
#define ZEND_HRTIME_AVAILABLE (ZEND_HRTIME_PLATFORM_POSIX || ZEND_HRTIME_PLATFORM_WINDOWS || ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE || ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC || ZEND_HRTIME_PLATFORM_HPUX || ZEND_HRTIME_PLATFORM_AIX)
62
63
BEGIN_EXTERN_C()
64
65
#if ZEND_HRTIME_PLATFORM_WINDOWS
66
67
ZEND_API extern double zend_hrtime_timer_scale;
68
69
#elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
70
71
# include <mach/mach_time.h>
72
# include <string.h>
73
ZEND_API extern mach_timebase_info_data_t zend_hrtime_timerlib_info;
74
75
#endif
76
77
2.01M
#define ZEND_NANO_IN_SEC UINT64_C(1000000000)
78
79
typedef uint64_t zend_hrtime_t;
80
81
void zend_startup_hrtime(void);
82
83
static zend_always_inline zend_hrtime_t zend_hrtime(void)
84
2.01M
{
85
#if ZEND_HRTIME_PLATFORM_WINDOWS
86
  LARGE_INTEGER lt = {{0}};
87
  QueryPerformanceCounter(&lt);
88
  return (zend_hrtime_t)((zend_hrtime_t)lt.QuadPart * zend_hrtime_timer_scale);
89
#elif ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC
90
  return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
91
#elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
92
  return (zend_hrtime_t)mach_absolute_time() * zend_hrtime_timerlib_info.numer / zend_hrtime_timerlib_info.denom;
93
#elif ZEND_HRTIME_PLATFORM_POSIX
94
  struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
95
2.01M
  if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC, &ts))) {
96
2.01M
    return ((zend_hrtime_t) ts.tv_sec * (zend_hrtime_t)ZEND_NANO_IN_SEC) + ts.tv_nsec;
97
2.01M
  }
98
0
  return 0;
99
#elif ZEND_HRTIME_PLATFORM_HPUX
100
  return (zend_hrtime_t) gethrtime();
101
#elif  ZEND_HRTIME_PLATFORM_AIX
102
  timebasestruct_t t;
103
  read_wall_time(&t, TIMEBASE_SZ);
104
  time_base_to_time(&t, TIMEBASE_SZ);
105
  return (zend_hrtime_t) t.tb_high * (zend_hrtime_t)NANO_IN_SEC + t.tb_low;
106
#else
107
  return 0;
108
#endif
109
2.01M
}
Unexecuted instantiation: php_date.c:zend_hrtime
Unexecuted instantiation: astro.c:zend_hrtime
Unexecuted instantiation: dow.c:zend_hrtime
Unexecuted instantiation: parse_date.c:zend_hrtime
Unexecuted instantiation: parse_tz.c:zend_hrtime
Unexecuted instantiation: parse_posix.c:zend_hrtime
Unexecuted instantiation: timelib.c:zend_hrtime
Unexecuted instantiation: tm2unixtime.c:zend_hrtime
Unexecuted instantiation: unixtime2tm.c:zend_hrtime
Unexecuted instantiation: parse_iso_intervals.c:zend_hrtime
Unexecuted instantiation: interval.c:zend_hrtime
Unexecuted instantiation: php_pcre.c:zend_hrtime
Unexecuted instantiation: exif.c:zend_hrtime
Unexecuted instantiation: hash_adler32.c:zend_hrtime
Unexecuted instantiation: hash_crc32.c:zend_hrtime
Unexecuted instantiation: hash_fnv.c:zend_hrtime
Unexecuted instantiation: hash_gost.c:zend_hrtime
Unexecuted instantiation: hash_haval.c:zend_hrtime
Unexecuted instantiation: hash_joaat.c:zend_hrtime
Unexecuted instantiation: hash_md.c:zend_hrtime
Unexecuted instantiation: hash_murmur.c:zend_hrtime
Unexecuted instantiation: hash_ripemd.c:zend_hrtime
Unexecuted instantiation: hash_sha_ni.c:zend_hrtime
Unexecuted instantiation: hash_sha_sse2.c:zend_hrtime
Unexecuted instantiation: hash_sha.c:zend_hrtime
Unexecuted instantiation: hash_sha3.c:zend_hrtime
Unexecuted instantiation: hash_snefru.c:zend_hrtime
Unexecuted instantiation: hash_tiger.c:zend_hrtime
Unexecuted instantiation: hash_whirlpool.c:zend_hrtime
Unexecuted instantiation: hash_xxhash.c:zend_hrtime
Unexecuted instantiation: hash.c:zend_hrtime
Unexecuted instantiation: json_encoder.c:zend_hrtime
Unexecuted instantiation: json_parser.tab.c:zend_hrtime
Unexecuted instantiation: json_scanner.c:zend_hrtime
Unexecuted instantiation: json.c:zend_hrtime
Unexecuted instantiation: php_lexbor.c:zend_hrtime
Unexecuted instantiation: csprng.c:zend_hrtime
Unexecuted instantiation: engine_mt19937.c:zend_hrtime
Unexecuted instantiation: engine_pcgoneseq128xslrr64.c:zend_hrtime
Unexecuted instantiation: engine_secure.c:zend_hrtime
Unexecuted instantiation: engine_user.c:zend_hrtime
Unexecuted instantiation: engine_xoshiro256starstar.c:zend_hrtime
Unexecuted instantiation: gammasection.c:zend_hrtime
Unexecuted instantiation: random.c:zend_hrtime
Unexecuted instantiation: randomizer.c:zend_hrtime
Unexecuted instantiation: zend_utils.c:zend_hrtime
Unexecuted instantiation: php_reflection.c:zend_hrtime
Unexecuted instantiation: php_spl.c:zend_hrtime
Unexecuted instantiation: spl_array.c:zend_hrtime
Unexecuted instantiation: spl_directory.c:zend_hrtime
Unexecuted instantiation: spl_dllist.c:zend_hrtime
Unexecuted instantiation: spl_exceptions.c:zend_hrtime
Unexecuted instantiation: spl_fixedarray.c:zend_hrtime
Unexecuted instantiation: spl_functions.c:zend_hrtime
Unexecuted instantiation: spl_heap.c:zend_hrtime
Unexecuted instantiation: spl_iterators.c:zend_hrtime
Unexecuted instantiation: spl_observer.c:zend_hrtime
Unexecuted instantiation: array.c:zend_hrtime
Unexecuted instantiation: assert.c:zend_hrtime
Unexecuted instantiation: base64.c:zend_hrtime
Unexecuted instantiation: basic_functions.c:zend_hrtime
Unexecuted instantiation: browscap.c:zend_hrtime
Unexecuted instantiation: crc32_x86.c:zend_hrtime
Unexecuted instantiation: crc32.c:zend_hrtime
Unexecuted instantiation: credits.c:zend_hrtime
Unexecuted instantiation: crypt.c:zend_hrtime
Unexecuted instantiation: css.c:zend_hrtime
Unexecuted instantiation: datetime.c:zend_hrtime
Unexecuted instantiation: dir.c:zend_hrtime
Unexecuted instantiation: dl.c:zend_hrtime
Unexecuted instantiation: dns.c:zend_hrtime
Unexecuted instantiation: exec.c:zend_hrtime
Unexecuted instantiation: file.c:zend_hrtime
Unexecuted instantiation: filestat.c:zend_hrtime
Unexecuted instantiation: filters.c:zend_hrtime
Unexecuted instantiation: flock_compat.c:zend_hrtime
Unexecuted instantiation: formatted_print.c:zend_hrtime
Unexecuted instantiation: fsock.c:zend_hrtime
Unexecuted instantiation: ftok.c:zend_hrtime
Unexecuted instantiation: ftp_fopen_wrapper.c:zend_hrtime
Unexecuted instantiation: head.c:zend_hrtime
Unexecuted instantiation: hrtime.c:zend_hrtime
Unexecuted instantiation: html.c:zend_hrtime
Unexecuted instantiation: http_fopen_wrapper.c:zend_hrtime
Unexecuted instantiation: http.c:zend_hrtime
Unexecuted instantiation: image.c:zend_hrtime
Unexecuted instantiation: incomplete_class.c:zend_hrtime
Unexecuted instantiation: info.c:zend_hrtime
Unexecuted instantiation: iptc.c:zend_hrtime
Unexecuted instantiation: levenshtein.c:zend_hrtime
Unexecuted instantiation: link.c:zend_hrtime
Unexecuted instantiation: mail.c:zend_hrtime
Unexecuted instantiation: math.c:zend_hrtime
Unexecuted instantiation: md5.c:zend_hrtime
Unexecuted instantiation: metaphone.c:zend_hrtime
Unexecuted instantiation: microtime.c:zend_hrtime
Unexecuted instantiation: net.c:zend_hrtime
Unexecuted instantiation: pack.c:zend_hrtime
Unexecuted instantiation: pageinfo.c:zend_hrtime
Unexecuted instantiation: password.c:zend_hrtime
Unexecuted instantiation: php_fopen_wrapper.c:zend_hrtime
Unexecuted instantiation: proc_open.c:zend_hrtime
Unexecuted instantiation: quot_print.c:zend_hrtime
Unexecuted instantiation: scanf.c:zend_hrtime
Unexecuted instantiation: sha1.c:zend_hrtime
Unexecuted instantiation: soundex.c:zend_hrtime
Unexecuted instantiation: streamsfuncs.c:zend_hrtime
Unexecuted instantiation: string.c:zend_hrtime
Unexecuted instantiation: strnatcmp.c:zend_hrtime
Unexecuted instantiation: syslog.c:zend_hrtime
Unexecuted instantiation: type.c:zend_hrtime
Unexecuted instantiation: uniqid.c:zend_hrtime
Unexecuted instantiation: url_scanner_ex.c:zend_hrtime
Unexecuted instantiation: url.c:zend_hrtime
Unexecuted instantiation: user_filters.c:zend_hrtime
Unexecuted instantiation: uuencode.c:zend_hrtime
Unexecuted instantiation: var_unserializer.c:zend_hrtime
Unexecuted instantiation: var.c:zend_hrtime
Unexecuted instantiation: versioning.c:zend_hrtime
Unexecuted instantiation: crypt_sha256.c:zend_hrtime
Unexecuted instantiation: crypt_sha512.c:zend_hrtime
Unexecuted instantiation: php_crypt_r.c:zend_hrtime
Unexecuted instantiation: php_uri.c:zend_hrtime
Unexecuted instantiation: php_uri_common.c:zend_hrtime
Unexecuted instantiation: explicit_bzero.c:zend_hrtime
Unexecuted instantiation: fopen_wrappers.c:zend_hrtime
Unexecuted instantiation: getopt.c:zend_hrtime
Unexecuted instantiation: main.c:zend_hrtime
Unexecuted instantiation: network.c:zend_hrtime
Unexecuted instantiation: output.c:zend_hrtime
Unexecuted instantiation: php_content_types.c:zend_hrtime
Unexecuted instantiation: php_ini_builder.c:zend_hrtime
Unexecuted instantiation: php_ini.c:zend_hrtime
Unexecuted instantiation: php_glob.c:zend_hrtime
Unexecuted instantiation: php_odbc_utils.c:zend_hrtime
Unexecuted instantiation: php_open_temporary_file.c:zend_hrtime
Unexecuted instantiation: php_scandir.c:zend_hrtime
Unexecuted instantiation: php_syslog.c:zend_hrtime
Unexecuted instantiation: php_ticks.c:zend_hrtime
Unexecuted instantiation: php_variables.c:zend_hrtime
Unexecuted instantiation: reentrancy.c:zend_hrtime
Unexecuted instantiation: rfc1867.c:zend_hrtime
Unexecuted instantiation: safe_bcmp.c:zend_hrtime
Unexecuted instantiation: SAPI.c:zend_hrtime
Unexecuted instantiation: snprintf.c:zend_hrtime
Unexecuted instantiation: spprintf.c:zend_hrtime
Unexecuted instantiation: strlcat.c:zend_hrtime
Unexecuted instantiation: strlcpy.c:zend_hrtime
Unexecuted instantiation: cast.c:zend_hrtime
Unexecuted instantiation: filter.c:zend_hrtime
Unexecuted instantiation: glob_wrapper.c:zend_hrtime
Unexecuted instantiation: memory.c:zend_hrtime
Unexecuted instantiation: mmap.c:zend_hrtime
Unexecuted instantiation: plain_wrapper.c:zend_hrtime
Unexecuted instantiation: streams.c:zend_hrtime
Unexecuted instantiation: transports.c:zend_hrtime
Unexecuted instantiation: userspace.c:zend_hrtime
Unexecuted instantiation: xp_socket.c:zend_hrtime
Unexecuted instantiation: block_pass.c:zend_hrtime
Unexecuted instantiation: compact_literals.c:zend_hrtime
Unexecuted instantiation: compact_vars.c:zend_hrtime
Unexecuted instantiation: dce.c:zend_hrtime
Unexecuted instantiation: dfa_pass.c:zend_hrtime
Unexecuted instantiation: escape_analysis.c:zend_hrtime
Unexecuted instantiation: nop_removal.c:zend_hrtime
Unexecuted instantiation: optimize_func_calls.c:zend_hrtime
Unexecuted instantiation: optimize_temp_vars_5.c:zend_hrtime
Unexecuted instantiation: pass1.c:zend_hrtime
Unexecuted instantiation: pass3.c:zend_hrtime
Unexecuted instantiation: sccp.c:zend_hrtime
Unexecuted instantiation: scdf.c:zend_hrtime
Unexecuted instantiation: zend_call_graph.c:zend_hrtime
Unexecuted instantiation: zend_cfg.c:zend_hrtime
Unexecuted instantiation: zend_dfg.c:zend_hrtime
Unexecuted instantiation: zend_dump.c:zend_hrtime
Unexecuted instantiation: zend_func_info.c:zend_hrtime
Unexecuted instantiation: zend_inference.c:zend_hrtime
Unexecuted instantiation: zend_optimizer.c:zend_hrtime
Unexecuted instantiation: zend_ssa.c:zend_hrtime
Unexecuted instantiation: zend_alloc.c:zend_hrtime
Unexecuted instantiation: zend_API.c:zend_hrtime
Unexecuted instantiation: zend_ast.c:zend_hrtime
Unexecuted instantiation: zend_attributes.c:zend_hrtime
Unexecuted instantiation: zend_builtin_functions.c:zend_hrtime
Unexecuted instantiation: zend_call_stack.c:zend_hrtime
Unexecuted instantiation: zend_closures.c:zend_hrtime
Unexecuted instantiation: zend_compile.c:zend_hrtime
Unexecuted instantiation: zend_constants.c:zend_hrtime
Unexecuted instantiation: zend_cpuinfo.c:zend_hrtime
Unexecuted instantiation: zend_default_classes.c:zend_hrtime
Unexecuted instantiation: zend_dtrace.c:zend_hrtime
Unexecuted instantiation: zend_enum.c:zend_hrtime
Unexecuted instantiation: zend_exceptions.c:zend_hrtime
Unexecuted instantiation: zend_execute_API.c:zend_hrtime
Unexecuted instantiation: zend_execute.c:zend_hrtime
Unexecuted instantiation: zend_extensions.c:zend_hrtime
Unexecuted instantiation: zend_fibers.c:zend_hrtime
Unexecuted instantiation: zend_float.c:zend_hrtime
zend_gc.c:zend_hrtime
Line
Count
Source
84
2.01M
{
85
#if ZEND_HRTIME_PLATFORM_WINDOWS
86
  LARGE_INTEGER lt = {{0}};
87
  QueryPerformanceCounter(&lt);
88
  return (zend_hrtime_t)((zend_hrtime_t)lt.QuadPart * zend_hrtime_timer_scale);
89
#elif ZEND_HRTIME_PLATFORM_APPLE_GETTIME_NSEC
90
  return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
91
#elif ZEND_HRTIME_PLATFORM_APPLE_MACH_ABSOLUTE
92
  return (zend_hrtime_t)mach_absolute_time() * zend_hrtime_timerlib_info.numer / zend_hrtime_timerlib_info.denom;
93
#elif ZEND_HRTIME_PLATFORM_POSIX
94
  struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
95
2.01M
  if (EXPECTED(0 == clock_gettime(CLOCK_MONOTONIC, &ts))) {
96
2.01M
    return ((zend_hrtime_t) ts.tv_sec * (zend_hrtime_t)ZEND_NANO_IN_SEC) + ts.tv_nsec;
97
2.01M
  }
98
0
  return 0;
99
#elif ZEND_HRTIME_PLATFORM_HPUX
100
  return (zend_hrtime_t) gethrtime();
101
#elif  ZEND_HRTIME_PLATFORM_AIX
102
  timebasestruct_t t;
103
  read_wall_time(&t, TIMEBASE_SZ);
104
  time_base_to_time(&t, TIMEBASE_SZ);
105
  return (zend_hrtime_t) t.tb_high * (zend_hrtime_t)NANO_IN_SEC + t.tb_low;
106
#else
107
  return 0;
108
#endif
109
2.01M
}
Unexecuted instantiation: zend_gdb.c:zend_hrtime
Unexecuted instantiation: zend_generators.c:zend_hrtime
Unexecuted instantiation: zend_hash.c:zend_hrtime
Unexecuted instantiation: zend_highlight.c:zend_hrtime
Unexecuted instantiation: zend_hrtime.c:zend_hrtime
Unexecuted instantiation: zend_inheritance.c:zend_hrtime
Unexecuted instantiation: zend_ini_parser.c:zend_hrtime
Unexecuted instantiation: zend_ini_scanner.c:zend_hrtime
Unexecuted instantiation: zend_ini.c:zend_hrtime
Unexecuted instantiation: zend_interfaces.c:zend_hrtime
Unexecuted instantiation: zend_iterators.c:zend_hrtime
Unexecuted instantiation: zend_language_parser.c:zend_hrtime
Unexecuted instantiation: zend_language_scanner.c:zend_hrtime
Unexecuted instantiation: zend_lazy_objects.c:zend_hrtime
Unexecuted instantiation: zend_list.c:zend_hrtime
Unexecuted instantiation: zend_llist.c:zend_hrtime
Unexecuted instantiation: zend_multibyte.c:zend_hrtime
Unexecuted instantiation: zend_object_handlers.c:zend_hrtime
Unexecuted instantiation: zend_objects_API.c:zend_hrtime
Unexecuted instantiation: zend_objects.c:zend_hrtime
Unexecuted instantiation: zend_observer.c:zend_hrtime
Unexecuted instantiation: zend_opcode.c:zend_hrtime
Unexecuted instantiation: zend_operators.c:zend_hrtime
Unexecuted instantiation: zend_property_hooks.c:zend_hrtime
Unexecuted instantiation: zend_ptr_stack.c:zend_hrtime
Unexecuted instantiation: zend_signal.c:zend_hrtime
Unexecuted instantiation: zend_smart_str.c:zend_hrtime
Unexecuted instantiation: zend_sort.c:zend_hrtime
Unexecuted instantiation: zend_stack.c:zend_hrtime
Unexecuted instantiation: zend_stream.c:zend_hrtime
Unexecuted instantiation: zend_string.c:zend_hrtime
Unexecuted instantiation: zend_strtod.c:zend_hrtime
Unexecuted instantiation: zend_system_id.c:zend_hrtime
Unexecuted instantiation: zend_variables.c:zend_hrtime
Unexecuted instantiation: zend_virtual_cwd.c:zend_hrtime
Unexecuted instantiation: zend_vm_opcodes.c:zend_hrtime
Unexecuted instantiation: zend_weakrefs.c:zend_hrtime
Unexecuted instantiation: zend.c:zend_hrtime
Unexecuted instantiation: internal_functions_cli.c:zend_hrtime
Unexecuted instantiation: fuzzer-parser.c:zend_hrtime
Unexecuted instantiation: fuzzer-sapi.c:zend_hrtime
Unexecuted instantiation: fuzzer-tracing-jit.c:zend_hrtime
Unexecuted instantiation: fuzzer-exif.c:zend_hrtime
Unexecuted instantiation: fuzzer-unserialize.c:zend_hrtime
Unexecuted instantiation: fuzzer-function-jit.c:zend_hrtime
Unexecuted instantiation: fuzzer-json.c:zend_hrtime
Unexecuted instantiation: fuzzer-unserializehash.c:zend_hrtime
Unexecuted instantiation: fuzzer-execute.c:zend_hrtime
110
111
END_EXTERN_C()
112
113
#endif /* ZEND_HRTIME_H */