Nanos.xs

filter: Statementstmt 94.1% 16 / 17 Branchbran 75.0% 6 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.0% 22 / 25 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Time/Nanos.pm ↑ Summary
1 /*
2 * Nanos.xs - XS implementation of Time::Nanos
3 *
4 * Provides a single XS function, hrtime(), that returns a high-resolution
5 * timestamp in nanoseconds. Two backends are supported:
6 *
7 * HAS_CLOCK_GETTIME - POSIX clock_gettime(2) (Linux, macOS, BSD, ...)
8 * HAS_WINHR - Windows high-resolution timers
9 *
10 * If neither is defined at compile time, hrtime() croaks at runtime.
11 */
12  
13 #include "EXTERN.h"
14 #include "perl.h"
15 #include "XSUB.h"
16 #include <stdint.h>
17  
18 #ifdef HAS_CLOCK_GETTIME
19 #include <time.h>
20 #include <errno.h>
21 #include <string.h>
22 #endif
23  
24 #ifdef HAS_WINHR
25 #include <windows.h>
26 #endif
27  
28 /*
29 * Populate *sec_out / *nsec_out with the current time from the requested
30 * clock source.
31 * use_realtime=1 -> CLOCK_REALTIME (wall-clock),
32 * use_realtime=0 -> CLOCK_MONOTONIC (time since boot, immune to jumps).
33 */
34 #if defined(HAS_CLOCK_GETTIME) || defined(HAS_WINHR)
35 static void
36 15 get_hrtime(int use_realtime, uint64_t *sec_out, uint64_t *nsec_out)
37 {
38 #ifdef HAS_CLOCK_GETTIME
39 {
40 struct timespec ts;
41 15 clockid_t clock_id = use_realtime ? CLOCK_REALTIME : CLOCK_MONOTONIC;
42 15 ▶ if (clock_gettime(clock_id, &ts) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (clock_gettime(clock_id, &ts) != 0) { 0 15 15
43 0 croak("hrtime(): clock_gettime() failed: %s", strerror(errno));
44 }
45 15 *sec_out = (uint64_t)ts.tv_sec;
46 15 *nsec_out = (uint64_t)ts.tv_nsec;
47 }
48 #else
49 if (use_realtime) {
50 /* Windows: GetSystemTimePreciseAsFileTime gives 100 ns intervals
51 * since 1601-01-01. Adjust epoch to 1970-01-01 and convert. */
52 FILETIME ft;
53 ULARGE_INTEGER ul;
54 GetSystemTimePreciseAsFileTime(&ft);
55 ul.LowPart = ft.dwLowDateTime;
56 ul.HighPart = ft.dwHighDateTime;
57 ul.QuadPart -= 116444736000000000ULL; /* 1601 -> 1970 offset */
58 *sec_out = ul.QuadPart / 10000000ULL; /* 100 ns -> sec */
59 *nsec_out = (ul.QuadPart % 10000000ULL) * 100ULL; /* remainder -> ns */
60 } else {
61 /* Windows: QueryPerformanceCounter for monotonic time */
62 LARGE_INTEGER freq, counter;
63 int64_t remainder;
64 if (!QueryPerformanceFrequency(&freq)) {
65 croak("hrtime(): QueryPerformanceFrequency() failed");
66 }
67 if (!QueryPerformanceCounter(&counter)) {
68 croak("hrtime(): QueryPerformanceCounter() failed");
69 }
70 *sec_out = (uint64_t)(counter.QuadPart / freq.QuadPart);
71 remainder = counter.QuadPart % freq.QuadPart;
72 *nsec_out = (uint64_t)((remainder * 1000000000LL) / freq.QuadPart);
73 }
74 #endif
75 15 }
76 #endif
77  
78 MODULE = Time::Nanos PACKAGE = Time::Nanos
79  
80 PROTOTYPES: DISABLE
81  
82 void
83 hrtime(int clock_source)
84 PPCODE:
85 /*
86 * Returns a nanosecond-precision timestamp as a single integer.
87 *
88 * clock_source:
89 * 0 -> CLOCK_REALTIME (wall-clock, default)
90 * 1 -> CLOCK_MONOTONIC (time since boot, immune to jumps)
91 *
92 * Any other value croaks.
93 */
94 #if !defined(HAS_CLOCK_GETTIME) && !defined(HAS_WINHR)
95 croak("hrtime(): high-resolution clock is not available on this platform");
96 #else
97 {
98 uint64_t sec_part, nsec_part;
99 16 int use_realtime = 0;
100  
101 16 ▶ if (clock_source == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (clock_source == 0) { 13 3 16
102 13 use_realtime = 1;
103 3 ▶ } else if (clock_source == 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (clock_source == 1) { 2 1 3
104 2 use_realtime = 0;
105 } else {
106 1 croak("hrtime(): invalid clock source %d (valid: 0 = realtime, 1 = monotonic)", clock_source);
107 }
108  
109 15 get_hrtime(use_realtime, &sec_part, &nsec_part);
110  
111 {
112 15 uint64_t total = sec_part * 1000000000ULL + nsec_part;
113  
114 15 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 15 15
115 #if UVSIZE >= 8
116 /* 64-bit UV (typical): exact integer nanoseconds. */
117 15 PUSHs(sv_2mortal(newSVuv((UV)total)));
118 #else
119 /* 32-bit UV perl: a 64-bit ns cannot fit in UV and a plain
120 * scalar cannot hold a 64-bit int, so return it as an NV
121 * (double). This avoids silently wrapping to garbage; the
122 * value loses precision (~256 ns at the current epoch) but
123 * stays "close" and keeps the array split sane. */
124 PUSHs(sv_2mortal(newSVnv((NV)total)));
125 #endif
126 }
127 }
128 #endif
« blib/lib/Time/Nanos.pm ↑ Summary