fix(stats): more robust checks for native cputime

This commit is contained in:
Folke Lemaitre 2023-01-03 09:12:43 +01:00
parent 06db1ec3c6
commit b5f4106892
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 19 additions and 10 deletions

View File

@ -9,19 +9,18 @@ M._stats = {
-- when true, startuptime is the accurate cputime for the Neovim process. (Linux & Macos) -- 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 -- 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. -- when false, startuptime is calculated based on a delta with a timestamp when lazy started.
startuptime_cputime = false, real_cputime = false,
count = 0, -- total number of plugins count = 0, -- total number of plugins
loaded = 0, -- number of loaded plugins loaded = 0, -- number of loaded plugins
---@type table<string, number> ---@type table<string, number>
times = {}, times = {},
} }
---@type ffi.namespace*|boolean ---@type ffi.namespace*
M.C = nil 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
vim.cmd([[do User LazyVimStarted]]) vim.cmd([[do User LazyVimStarted]])
end end
@ -33,7 +32,6 @@ end
function M.cputime() function M.cputime()
if M.C == nil then if M.C == nil then
M.C = false
pcall(function() pcall(function()
ffi.cdef([[ ffi.cdef([[
typedef long time_t; typedef long time_t;
@ -44,19 +42,30 @@ function M.cputime()
} nanotime; } nanotime;
int clock_gettime(clockid_t clk_id, struct timespec *tp); int clock_gettime(clockid_t clk_id, struct timespec *tp);
]]) ]])
if ffi.C.clock_gettime then
M.C = ffi.C M.C = ffi.C
end
end) end)
end end
if M.C then
local function real()
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)
return tonumber(pnano[0].tv_sec) / 1e6 + tonumber(pnano[0].tv_nsec) / 1e6 return tonumber(pnano[0].tv_sec) / 1e6 + tonumber(pnano[0].tv_nsec) / 1e6
else end
local function fallback()
return (vim.loop.hrtime() - require("lazy")._start) / 1e6 return (vim.loop.hrtime() - require("lazy")._start) / 1e6
end end
local ok, ret = pcall(real)
if ok then
M.cputime = real
M._stats.real_cputime = true
return ret
else
M.cputime = fallback
return fallback()
end
end end
function M.stats() function M.stats()

View File

@ -543,7 +543,7 @@ function M:profile()
local stats = require("lazy.stats").stats() local stats = require("lazy.stats").stats()
local ms = (math.floor(stats.startuptime * 100 + 0.5) / 100) local ms = (math.floor(stats.startuptime * 100 + 0.5) / 100)
self:append("Startuptime: ", "LazyH2"):append(ms .. "ms", "Number"):nl():nl() self:append("Startuptime: ", "LazyH2"):append(ms .. "ms", "Number"):nl():nl()
if stats.startuptime_cputime then if stats.real_cputime then
self:append("Based on the actual CPU time of the Neovim process till "):append("UIEnter", "LazySpecial") self:append("Based on the actual CPU time of the Neovim process till "):append("UIEnter", "LazySpecial")
self:append("."):nl() self:append("."):nl()
self:append("This is more accurate than ") self:append("This is more accurate than ")