fix(stats): better support for different `time_t` sizes. See #2049

This commit is contained in:
Folke Lemaitre 2025-10-09 10:44:19 +02:00
parent 9f559d0e9d
commit 1ea3c40857
No known key found for this signature in database
GPG Key ID: 9B52594D560070AB
1 changed files with 4 additions and 4 deletions

View File

@ -35,11 +35,10 @@ function M.cputime()
if M.C == nil then if M.C == nil then
pcall(function() pcall(function()
ffi.cdef([[ ffi.cdef([[
typedef long time_t;
typedef int clockid_t; typedef int clockid_t;
typedef struct timespec { typedef struct timespec {
time_t tv_sec; /* seconds */ int64_t tv_sec; /* Use fixed 64-bit type for portability */
long tv_nsec; /* nanoseconds */ long tv_nsec; /* nanoseconds */
} nanotime; } nanotime;
int clock_gettime(clockid_t clk_id, struct timespec *tp); int clock_gettime(clockid_t clk_id, struct timespec *tp);
]]) ]])
@ -48,7 +47,8 @@ function M.cputime()
end end
local function real() local function real()
local pnano = assert(ffi.new("nanotime[?]", 1)) -- Zero-initialize to handle 32-bit systems where only lower 32 bits are written
local pnano = ffi.new("nanotime[1]")
local CLOCK_PROCESS_CPUTIME_ID = jit.os == "OSX" and 12 or 2 local CLOCK_PROCESS_CPUTIME_ID = jit.os == "OSX" and 12 or 2
ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, pnano) ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, pnano)
return tonumber(pnano[0].tv_sec) * 1e3 + tonumber(pnano[0].tv_nsec) / 1e6 return tonumber(pnano[0].tv_sec) * 1e3 + tonumber(pnano[0].tv_nsec) / 1e6