Skip to content

feat(stat): use cputime for all timing related functions #1901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions lua/lazy/core/util.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local ffi = require("ffi")

---@class LazyUtilCore
local M = {}

Expand All @@ -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)
Expand Down
54 changes: 4 additions & 50 deletions lua/lazy/stats.lua
Original file line number Diff line number Diff line change
@@ -1,74 +1,28 @@
local ffi = require("ffi")

local M = {}

---@class LazyStats
M._stats = {
-- startuptime in milliseconds till UIEnter
startuptime = 0,
-- 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.
real_cputime = false,
count = 0, -- total number of plugins
loaded = 0, -- number of loaded plugins
---@type table<string, number>
times = {},
}

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

function M.on_ui_enter()
M._stats.startuptime = M.track("UIEnter")
require("lazy.core.util").track({ start = "startuptime" }, M._stats.startuptime * 1e6)
local startuptime = M.track("UIEnter")
M._stats.startuptime = startuptime / 1e6
require("lazy.core.util").track({ start = "startuptime" }, startuptime)
vim.api.nvim_exec_autocmds("User", { pattern = "LazyVimStarted", modeline = false })
end

function M.track(event)
local time = M.cputime()
local time = require("lazy.core.util").cputime()
M._stats.times[event] = time
return time
end

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

local function fallback()
return (vim.uv.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()
M._stats.count = 0
M._stats.loaded = 0
Expand Down
6 changes: 3 additions & 3 deletions lua/lazy/view/render.lua
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,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.real_cputime then
if Util.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 All @@ -668,14 +668,14 @@ function M:profile()

local times = {}
for event, time in pairs(require("lazy.stats").stats().times) do
times[#times + 1] = { event, self:ms(time * 1e6), "Bold", time = time }
times[#times + 1] = { event, self:ms(time), "Bold", time = time }
end
table.sort(times, function(a, b)
return a.time < b.time
end)
for p, prop in ipairs(times) do
if p > 1 then
prop[2] = prop[2] .. " (+" .. self:ms((prop.time - times[p - 1].time) * 1e6) .. ")"
prop[2] = prop[2] .. " (+" .. self:ms(prop.time - times[p - 1].time) .. ")"
end
end
self:props(times, { indent = 2 })
Expand Down
Loading