fix(stats): use fallback for cputime on windows. Fixes #280

This commit is contained in:
Folke Lemaitre 2023-01-02 17:10:54 +01:00
parent c1a50a7fc5
commit ddcdc5e447
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
1 changed files with 4 additions and 4 deletions

View File

@ -21,7 +21,7 @@ M.C = nil
function M.on_ui_enter() function M.on_ui_enter()
M._stats.startuptime = M.track("UIEnter") M._stats.startuptime = M.track("UIEnter")
M._stats.startuptime_cputime = M.C ~= false M._stats.startuptime_cputime = M.C.clock_gettime ~= nil
vim.cmd([[do User LazyVimStarted]]) vim.cmd([[do User LazyVimStarted]])
end end
@ -33,7 +33,7 @@ end
function M.cputime() function M.cputime()
if M.C == nil then if M.C == nil then
local ok = pcall(function() pcall(function()
ffi.cdef([[ ffi.cdef([[
typedef long time_t; typedef long time_t;
typedef int clockid_t; typedef int clockid_t;
@ -44,9 +44,9 @@ function M.cputime()
int clock_gettime(clockid_t clk_id, struct timespec *tp); int clock_gettime(clockid_t clk_id, struct timespec *tp);
]]) ]])
end) end)
M.C = ok and ffi.C or false M.C = ffi.C
end end
if M.C then if M.C.clock_gettime then
local pnano = assert(ffi.new("nanotime[?]", 1)) local pnano = assert(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)