Skip to content

Commit

Permalink
fix(stats): more robust checks for native cputime
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 3, 2023
1 parent 06db1ec commit b5f4106
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions lua/lazy/stats.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ M._stats = {
-- 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.
startuptime_cputime = false,
real_cputime = false,
count = 0, -- total number of plugins
loaded = 0, -- number of loaded plugins
---@type table<string, number>
times = {},
}

---@type ffi.namespace*|boolean
---@type ffi.namespace*
M.C = nil

function M.on_ui_enter()
M._stats.startuptime = M.track("UIEnter")
M._stats.startuptime_cputime = M.C ~= false
vim.cmd([[do User LazyVimStarted]])
end

Expand All @@ -33,7 +32,6 @@ end

function M.cputime()
if M.C == nil then
M.C = false
pcall(function()
ffi.cdef([[
typedef long time_t;
Expand All @@ -44,19 +42,30 @@ function M.cputime()
} nanotime;
int clock_gettime(clockid_t clk_id, struct timespec *tp);
]])
if ffi.C.clock_gettime then
M.C = ffi.C
end
M.C = ffi.C
end)
end
if M.C then

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) / 1e6 + tonumber(pnano[0].tv_nsec) / 1e6
else
end

local function fallback()
return (vim.loop.hrtime() - require("lazy")._start) / 1e6
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

function M.stats()
Expand Down
2 changes: 1 addition & 1 deletion lua/lazy/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ function M:profile()
local stats = require("lazy.stats").stats()
local ms = (math.floor(stats.startuptime * 100 + 0.5) / 100)
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("."):nl()
self:append("This is more accurate than ")
Expand Down

0 comments on commit b5f4106

Please sign in to comment.