perf: track some additional cputimes

This commit is contained in:
Folke Lemaitre 2023-01-02 11:28:18 +01:00
parent 46997de1c9
commit d992387912
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 49 additions and 16 deletions

View File

@ -36,10 +36,11 @@ function M.setup(spec, opts)
-- load module cache before anything else -- load module cache before anything else
require("lazy.core.cache").setup(opts) require("lazy.core.cache").setup(opts)
require("lazy.stats").track("LazyStart")
local Util = require("lazy.core.util") local Util = require("lazy.core.util")
local Config = require("lazy.core.config") local Config = require("lazy.core.config")
local Loader = require("lazy.core.loader") local Loader = require("lazy.core.loader")
local Plugin = require("lazy.core.plugin")
Util.track({ plugin = "lazy.nvim" }) -- setup start Util.track({ plugin = "lazy.nvim" }) -- setup start
Util.track("module", vim.loop.hrtime() - start) Util.track("module", vim.loop.hrtime() - start)
@ -64,6 +65,7 @@ function M.setup(spec, opts)
-- all done! -- all done!
vim.cmd("do User LazyDone") vim.cmd("do User LazyDone")
require("lazy.stats").track("LazyDone")
end end
function M.stats() function M.stats()

View File

@ -1,3 +1,5 @@
local ffi = require("ffi")
local M = {} local M = {}
---@class LazyStats ---@class LazyStats
@ -10,35 +12,48 @@ M._stats = {
startuptime_cputime = false, startuptime_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>
times = {},
} }
function M.on_ui_enter() ---@type ffi.namespace*|boolean
if not M.C then M.C = nil
pcall(function() end)
end
function M.on_ui_enter()
M._stats.startuptime = M.track("UIEnter")
M._stats.startuptime_cputime = M.C ~= false
vim.cmd([[do User LazyVimStarted]])
end
function M.track(event)
local time = M.cputime()
M._stats.times[event] = time
return time
end
function M.cputime()
if M.C == nil then
local ok = pcall(function() local ok = pcall(function()
local ffi = require("ffi")
ffi.cdef([[ ffi.cdef([[
typedef long time_t; typedef long time_t;
typedef int clockid_t; typedef int clockid_t;
typedef struct timespec { typedef struct timespec {
time_t tv_sec; /* seconds */ time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */ long tv_nsec; /* nanoseconds */
} nanotime; } nanotime;
int clock_gettime(clockid_t clk_id, struct timespec *tp); int clock_gettime(clockid_t clk_id, struct timespec *tp);
]]) ]])
end)
M.C = ok and ffi.C or false
end
if M.C then
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)
M._stats.startuptime = 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
M._stats.startuptime_cputime = true else
end) return (vim.loop.hrtime() - require("lazy")._start) / 1e6
if not ok then
M._stats.startuptime = (vim.loop.hrtime() - require("lazy")._start) / 1e6
end end
vim.cmd([[do User LazyVimStarted]])
end end
function M.stats() function M.stats()

View File

@ -556,7 +556,23 @@ function M:profile()
:append("UIEnter", "LazySpecial") :append("UIEnter", "LazySpecial")
self:append(".") self:append(".")
end end
self:nl():nl() self:nl()
local times = {}
for event, time in pairs(require("lazy.stats").stats().times) do
times[#times + 1] = { event, self:ms(time * 1e6), "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) .. ")"
end
end
self:props(times, { indent = 2 })
self:nl()
self:append("Profile", "LazyH2"):nl():nl() self:append("Profile", "LazyH2"):nl():nl()
self self