profile.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. local M = {}
  2. local api, uv = vim.api, vim.loop
  3. local std_data = vim.fn.stdpath "data"
  4. local std_config = vim.fn.stdpath "config"
  5. local vimruntime = os.getenv "VIMRUNTIME"
  6. local function load_buffer(title, lines)
  7. local bufnr = api.nvim_create_buf(false, false)
  8. api.nvim_buf_set_lines(bufnr, 0, 0, false, lines)
  9. api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
  10. api.nvim_buf_set_option(bufnr, "buftype", "nofile")
  11. api.nvim_buf_set_option(bufnr, "swapfile", false)
  12. api.nvim_buf_set_option(bufnr, "modifiable", false)
  13. api.nvim_buf_set_name(bufnr, title)
  14. api.nvim_set_current_buf(bufnr)
  15. end
  16. local function mod_path(path)
  17. if not path then
  18. return "?"
  19. end
  20. path = path:gsub(std_data .. "/site/pack/packer/", "<PACKER>/")
  21. path = path:gsub(std_data .. "/", "<STD_DATA>/")
  22. path = path:gsub(std_config .. "/", "<STD_CONFIG>/")
  23. path = path:gsub(vimruntime .. "/", "<VIMRUNTIME>/")
  24. return path
  25. end
  26. local function time_tostr(x)
  27. if x == 0 then
  28. return "?"
  29. end
  30. return string.format("%8.3fms", x)
  31. end
  32. local function mem_tostr(x)
  33. local unit = ""
  34. for _, u in ipairs { "K", "M", "G" } do
  35. if x < 1000 then
  36. break
  37. end
  38. x = x / 1000
  39. unit = u
  40. end
  41. return string.format("%1.1f%s", x, unit)
  42. end
  43. function M.print_profile(I)
  44. local mod_profile = I.modpaths.profile
  45. local chunk_profile = I.chunks.profile
  46. if not mod_profile and not chunk_profile then
  47. print "Error: profiling was not enabled"
  48. return
  49. end
  50. local total_resolve = 0
  51. local total_load = 0
  52. local modules = {}
  53. for path, m in pairs(chunk_profile) do
  54. m.load = m.load_end - m.load_start
  55. m.load = m.load / 1000000
  56. m.path = mod_path(path)
  57. end
  58. local module_content_width = 0
  59. for module, m in pairs(mod_profile) do
  60. m.resolve = 0
  61. if m.resolve_end then
  62. m.resolve = m.resolve_end - m.resolve_start
  63. m.resolve = m.resolve / 1000000
  64. end
  65. m.module = module:gsub("/", ".")
  66. m.loader = m.loader or m.loader_guess
  67. local path = I.modpaths.cache[module]
  68. local path_prof = chunk_profile[path]
  69. m.path = mod_path(path)
  70. if path_prof then
  71. chunk_profile[path] = nil
  72. m.load = path_prof.load
  73. m.ploader = path_prof.loader
  74. else
  75. m.load = 0
  76. m.ploader = "NA"
  77. end
  78. total_resolve = total_resolve + m.resolve
  79. total_load = total_load + m.load
  80. if #module > module_content_width then
  81. module_content_width = #module
  82. end
  83. modules[#modules + 1] = m
  84. end
  85. table.sort(modules, function(a, b)
  86. return (a.resolve + a.load) > (b.resolve + b.load)
  87. end)
  88. local paths = {}
  89. local total_paths_load = 0
  90. for _, m in pairs(chunk_profile) do
  91. paths[#paths + 1] = m
  92. total_paths_load = total_paths_load + m.load
  93. end
  94. table.sort(paths, function(a, b)
  95. return a.load > b.load
  96. end)
  97. local lines = {}
  98. local function add(fmt, ...)
  99. local args = { ... }
  100. for i, a in ipairs(args) do
  101. if type(a) == "number" then
  102. args[i] = time_tostr(a)
  103. end
  104. end
  105. lines[#lines + 1] = string.format(fmt, unpack(args))
  106. end
  107. local time_cell_width = 12
  108. local loader_cell_width = 11
  109. local time_content_width = time_cell_width - 2
  110. local loader_content_width = loader_cell_width - 2
  111. local module_cell_width = module_content_width + 2
  112. local tcwl = string.rep("─", time_cell_width)
  113. local lcwl = string.rep("─", loader_cell_width)
  114. local mcwl = string.rep("─", module_cell_width + 2)
  115. local n = string.rep("─", 200)
  116. local module_cell_format = "%-" .. module_cell_width .. "s"
  117. local loader_format = "%-" .. loader_content_width .. "s"
  118. local line_format = "%s │ %s │ %s │ %s │ %s │ %s"
  119. local row_fmt = line_format:format(
  120. " %" .. time_content_width .. "s",
  121. loader_format,
  122. "%" .. time_content_width .. "s",
  123. loader_format,
  124. module_cell_format,
  125. "%s"
  126. )
  127. local title_fmt = line_format:format(
  128. " %-" .. time_content_width .. "s",
  129. loader_format,
  130. "%-" .. time_content_width .. "s",
  131. loader_format,
  132. module_cell_format,
  133. "%s"
  134. )
  135. local title1_width = time_cell_width + loader_cell_width - 1
  136. local title1_fmt = ("%s │ %s │"):format(" %-" .. title1_width .. "s", "%-" .. title1_width .. "s")
  137. add "Note: this report is not a measure of startup time. Only use this for comparing"
  138. add "between cached and uncached loads of Lua modules"
  139. add ""
  140. add "Cache files:"
  141. for _, f in ipairs { I.chunks.path, I.modpaths.path } do
  142. local size = vim.loop.fs_stat(f).size
  143. add(" %s %s", f, mem_tostr(size))
  144. end
  145. add ""
  146. add("%s─%s┬%s─%s┐", tcwl, lcwl, tcwl, lcwl)
  147. add(title1_fmt, "Resolve", "Load")
  148. add("%s┬%s┼%s┬%s┼%s┬%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
  149. add(title_fmt, "Time", "Method", "Time", "Method", "Module", "Path")
  150. add("%s┼%s┼%s┼%s┼%s┼%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
  151. add(row_fmt, total_resolve, "", total_load, "", "Total", "")
  152. add("%s┼%s┼%s┼%s┼%s┼%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
  153. for _, p in ipairs(modules) do
  154. add(row_fmt, p.resolve, p.loader, p.load, p.ploader, p.module, p.path)
  155. end
  156. add("%s┴%s┴%s┴%s┴%s┴%s", tcwl, lcwl, tcwl, lcwl, mcwl, n)
  157. if #paths > 0 then
  158. add ""
  159. add(n)
  160. local f3 = " %" .. time_content_width .. "s │ %" .. loader_content_width .. "s │ %s"
  161. add "Files loaded with no associated module"
  162. add("%s┬%s┬%s", tcwl, lcwl, n)
  163. add(f3, "Time", "Loader", "Path")
  164. add("%s┼%s┼%s", tcwl, lcwl, n)
  165. add(f3, total_paths_load, "", "Total")
  166. add("%s┼%s┼%s", tcwl, lcwl, n)
  167. for _, p in ipairs(paths) do
  168. add(f3, p.load, p.loader, p.path)
  169. end
  170. add("%s┴%s┴%s", tcwl, lcwl, n)
  171. add ""
  172. end
  173. load_buffer("Impatient Profile Report", lines)
  174. end
  175. M.setup = function(profile)
  176. local _require = require
  177. -- luacheck: ignore 121
  178. require = function(mod)
  179. local basename = mod:gsub("%.", "/")
  180. if not profile[basename] then
  181. profile[basename] = {}
  182. profile[basename].resolve_start = uv.hrtime()
  183. profile[basename].loader_guess = "C"
  184. end
  185. return _require(mod)
  186. end
  187. -- Add profiling around all the loaders
  188. local pl = package.loaders
  189. for i = 1, #pl do
  190. local l = pl[i]
  191. pl[i] = function(mod)
  192. local basename = mod:gsub("%.", "/")
  193. profile[basename].loader_guess = i == 1 and "preloader" or "loader #" .. i
  194. return l(mod)
  195. end
  196. end
  197. end
  198. return M