profile.lua 6.4 KB

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