mirror of
https://github.com/folke/lazy.nvim.git
synced 2025-04-11 01:42:32 +00:00
Merge 33c03c667a
into 6c3bda4aca
This commit is contained in:
commit
bfafbead54
3 changed files with 55 additions and 53 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
local ffi = require("ffi")
|
||||||
|
|
||||||
---@class LazyUtilCore
|
---@class LazyUtilCore
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
@ -7,6 +9,52 @@ local M = {}
|
||||||
M._profiles = { { name = "lazy" } }
|
M._profiles = { { name = "lazy" } }
|
||||||
M.is_win = jit.os:find("Windows")
|
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 data (string|{[string]:string})?
|
||||||
---@param time number?
|
---@param time number?
|
||||||
function M.track(data, time)
|
function M.track(data, time)
|
||||||
|
|
|
@ -1,74 +1,28 @@
|
||||||
local ffi = require("ffi")
|
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@class LazyStats
|
---@class LazyStats
|
||||||
M._stats = {
|
M._stats = {
|
||||||
-- startuptime in milliseconds till UIEnter
|
-- startuptime in milliseconds till UIEnter
|
||||||
startuptime = 0,
|
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
|
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*
|
|
||||||
M.C = nil
|
|
||||||
|
|
||||||
function M.on_ui_enter()
|
function M.on_ui_enter()
|
||||||
M._stats.startuptime = M.track("UIEnter")
|
local startuptime = M.track("UIEnter")
|
||||||
require("lazy.core.util").track({ start = "startuptime" }, M._stats.startuptime * 1e6)
|
M._stats.startuptime = startuptime / 1e6
|
||||||
|
require("lazy.core.util").track({ start = "startuptime" }, startuptime)
|
||||||
vim.api.nvim_exec_autocmds("User", { pattern = "LazyVimStarted", modeline = false })
|
vim.api.nvim_exec_autocmds("User", { pattern = "LazyVimStarted", modeline = false })
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.track(event)
|
function M.track(event)
|
||||||
local time = M.cputime()
|
local time = require("lazy.core.util").cputime()
|
||||||
M._stats.times[event] = time
|
M._stats.times[event] = time
|
||||||
return time
|
return time
|
||||||
end
|
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()
|
function M.stats()
|
||||||
M._stats.count = 0
|
M._stats.count = 0
|
||||||
M._stats.loaded = 0
|
M._stats.loaded = 0
|
||||||
|
|
|
@ -651,7 +651,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.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("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 ")
|
||||||
|
@ -668,14 +668,14 @@ function M:profile()
|
||||||
|
|
||||||
local times = {}
|
local times = {}
|
||||||
for event, time in pairs(require("lazy.stats").stats().times) do
|
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
|
end
|
||||||
table.sort(times, function(a, b)
|
table.sort(times, function(a, b)
|
||||||
return a.time < b.time
|
return a.time < b.time
|
||||||
end)
|
end)
|
||||||
for p, prop in ipairs(times) do
|
for p, prop in ipairs(times) do
|
||||||
if p > 1 then
|
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
|
||||||
end
|
end
|
||||||
self:props(times, { indent = 2 })
|
self:props(times, { indent = 2 })
|
||||||
|
|
Loading…
Add table
Reference in a new issue