From b5f4106892254c748c49a42e07acd80964cb0bce Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Tue, 3 Jan 2023 09:12:43 +0100 Subject: [PATCH] fix(stats): more robust checks for native cputime --- lua/lazy/stats.lua | 27 ++++++++++++++++++--------- lua/lazy/view/render.lua | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lua/lazy/stats.lua b/lua/lazy/stats.lua index 4a2b02a..facc9dc 100644 --- a/lua/lazy/stats.lua +++ b/lua/lazy/stats.lua @@ -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 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 @@ -33,7 +32,6 @@ end function M.cputime() if M.C == nil then - M.C = false pcall(function() ffi.cdef([[ typedef long time_t; @@ -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() diff --git a/lua/lazy/view/render.lua b/lua/lazy/view/render.lua index e431d34..0784ff5 100644 --- a/lua/lazy/view/render.lua +++ b/lua/lazy/view/render.lua @@ -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 ")