mirror of
https://github.com/folke/lazy.nvim.git
synced 2025-04-20 13:26:45 +00:00
feat(stat): use cputime for all timing related functions
This commit is contained in:
parent
d8f26efd45
commit
33c03c667a
3 changed files with 55 additions and 53 deletions
|
@ -1,3 +1,5 @@
|
|||
local ffi = require("ffi")
|
||||
|
||||
---@class LazyUtilCore
|
||||
local M = {}
|
||||
|
||||
|
@ -7,6 +9,52 @@ local M = {}
|
|||
M._profiles = { { name = "lazy" } }
|
||||
M.is_win = jit.os:find("Windows")
|
||||
|
||||
-- when true, startuptime is the accurate cputime for the Neovim process. (Linux & macOS)
|
||||
-- this is more accurate than `nvim --startuptime`, and as such will be slightly higher
|
||||
-- when false, startuptime is calculated based on a delta with a timestamp when lazy started.
|
||||
M.real_cputime = false
|
||||
|
||||
---@type ffi.namespace*
|
||||
M.C = nil
|
||||
|
||||
function M.cputime()
|
||||
if M.C == nil then
|
||||
pcall(function()
|
||||
ffi.cdef([[
|
||||
typedef long time_t;
|
||||
typedef int clockid_t;
|
||||
typedef struct timespec {
|
||||
time_t tv_sec; /* seconds */
|
||||
long tv_nsec; /* nanoseconds */
|
||||
} nanotime;
|
||||
int clock_gettime(clockid_t clk_id, struct timespec *tp);
|
||||
]])
|
||||
M.C = ffi.C
|
||||
end)
|
||||
end
|
||||
|
||||
local function real()
|
||||
local pnano = assert(ffi.new("nanotime[?]", 1))
|
||||
local CLOCK_PROCESS_CPUTIME_ID = jit.os == "OSX" and 12 or 2
|
||||
ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, pnano)
|
||||
return tonumber(pnano[0].tv_sec) * 1e9 + tonumber(pnano[0].tv_nsec)
|
||||
end
|
||||
|
||||
local function fallback()
|
||||
return (vim.uv.hrtime() - require("lazy")._start)
|
||||
end
|
||||
|
||||
local ok, ret = pcall(real)
|
||||
if ok then
|
||||
M.cputime = real
|
||||
M.real_cputime = true
|
||||
return ret
|
||||
else
|
||||
M.cputime = fallback
|
||||
return fallback()
|
||||
end
|
||||
end
|
||||
|
||||
---@param data (string|{[string]:string})?
|
||||
---@param time number?
|
||||
function M.track(data, time)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue