源战役客户端
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

228 righe
6.2 KiB

4 settimane fa
  1. LuaProfiler = {}
  2. LuaProfiler.is_playing = false
  3. function LuaProfiler.SwitchState()
  4. LuaProfiler.is_playing = not LuaProfiler.is_playing
  5. if LuaProfiler.is_playing then
  6. LuaProfiler.start()
  7. else
  8. LuaProfiler.stop()
  9. end
  10. end
  11. -- get the function title
  12. function LuaProfiler._func_title(funcinfo)
  13. -- check
  14. assert(funcinfo)
  15. -- the function name
  16. local name = funcinfo.name or 'none_name'
  17. -- the function line
  18. -- print("_func_title = ", name)
  19. local line = string.format("%d", funcinfo.linedefined or 0)
  20. -- the function source
  21. local source = funcinfo.short_src or 'C_FUNC'
  22. -- if os.isfile(source) then
  23. -- source = path.relative(source, xmake._PROGRAM_DIR)
  24. -- end
  25. -- make title
  26. return string.format("%-30s: %s: %s", name, source, line), source
  27. end
  28. -- get the function report
  29. function LuaProfiler._func_report(funcinfo)
  30. -- get the function title
  31. local title, source = LuaProfiler._func_title(funcinfo)
  32. -- get the function report
  33. local report = LuaProfiler._REPORTS_BY_TITLE[title]
  34. if not report then
  35. -- init report
  36. report =
  37. {
  38. title = LuaProfiler._func_title(funcinfo)
  39. , source = source
  40. , callcount = 0
  41. , totaltime = 0
  42. }
  43. -- save it
  44. LuaProfiler._REPORTS_BY_TITLE[title] = report
  45. table.insert(LuaProfiler._REPORTS, report)
  46. end
  47. -- ok?
  48. return report
  49. end
  50. -- profiling call
  51. function LuaProfiler._profiling_call(funcinfo)
  52. -- get the function report
  53. local report = LuaProfiler._func_report(funcinfo)
  54. assert(report)
  55. -- save the call time
  56. report.calltime = os.clock()
  57. -- update the call count
  58. report.callcount = report.callcount + 1
  59. end
  60. -- profiling return
  61. function LuaProfiler._profiling_return(funcinfo)
  62. -- get the stoptime
  63. local stoptime = os.clock()
  64. -- get the function report
  65. local report = LuaProfiler._func_report(funcinfo)
  66. assert(report)
  67. -- update the total time
  68. if report.calltime and report.calltime > 0 then
  69. report.totaltime = report.totaltime + (stoptime - report.calltime)
  70. report.calltime = 0
  71. end
  72. end
  73. -- the profiling handler
  74. function LuaProfiler._profiling_handler(hooktype)
  75. -- the function info
  76. local funcinfo = debug.getinfo(2, 'nS')
  77. -- print("_profiling_handler = ", hooktype)
  78. -- dispatch it
  79. if hooktype == "call" then
  80. LuaProfiler._profiling_call(funcinfo)
  81. elseif hooktype == "return" then
  82. LuaProfiler._profiling_return(funcinfo)
  83. end
  84. end
  85. -- the tracing handler
  86. function LuaProfiler._tracing_handler(hooktype)
  87. -- the function info
  88. local funcinfo = debug.getinfo(2, 'nS')
  89. -- is call?
  90. if hooktype == "call" then
  91. -- is xmake function?
  92. local name = funcinfo.name
  93. local source = funcinfo.short_src or 'C_FUNC'
  94. if name then-- and os.isfile(source) then
  95. -- the function line
  96. local line = string.format("%d", funcinfo.linedefined or 0)
  97. -- get the relative source
  98. -- source = path.relative(source, xmake._PROGRAM_DIR)
  99. -- trace it
  100. print(string.format("%-30s: %s: %s", name, source, line))
  101. end
  102. end
  103. end
  104. -- start profiling
  105. function LuaProfiler.start(mode)
  106. -- trace?
  107. if mode and mode == "trace" then
  108. debug.sethook(LuaProfiler._tracing_handler, 'cr', 0)
  109. else
  110. -- init reports
  111. LuaProfiler._REPORTS = {}
  112. LuaProfiler._REPORTS_BY_TITLE = {}
  113. LuaProfiler._CUSTOM_REPORTS = ""
  114. print("----------------------------------------------->>LuaProfiler.start")
  115. -- save the start time
  116. LuaProfiler._STARTIME = os.clock()
  117. -- start to hook
  118. debug.sethook(LuaProfiler._profiling_handler, 'cr', 0)
  119. end
  120. end
  121. -- stop profiling
  122. function LuaProfiler.stop(mode)
  123. -- trace?
  124. if mode and mode == "trace" then
  125. -- stop to hook
  126. debug.sethook()
  127. else
  128. print("----------------------------------------------->>LuaProfiler.stop")
  129. -- save the stop time
  130. LuaProfiler._STOPTIME = os.clock()
  131. -- stop to hook
  132. debug.sethook()
  133. -- calculate the total time
  134. local totaltime = LuaProfiler._STOPTIME - LuaProfiler._STARTIME
  135. print("totaltime = " , totaltime)
  136. -- sort reports
  137. table.sort(LuaProfiler._REPORTS, function(a, b)
  138. return a.totaltime > b.totaltime
  139. end)
  140. -- show reports
  141. local total_percent = 0
  142. local class_list = {}
  143. for _, report in ipairs(LuaProfiler._REPORTS) do
  144. -- calculate percent
  145. local percent = (report.totaltime / totaltime) * 100
  146. total_percent = total_percent + percent
  147. if percent < 0.1 then
  148. break
  149. end
  150. local vo = class_list[report.source]
  151. if vo == nil then
  152. vo = {}
  153. class_list[report.source] = vo
  154. vo.totaltime = 0
  155. vo.percent = 0
  156. vo.callcount = 0
  157. end
  158. vo.totaltime = vo.totaltime + report.totaltime
  159. vo.percent = vo.percent + percent
  160. vo.callcount = vo.callcount + report.callcount
  161. -- trace
  162. local temp_str = string.format("totaltime = %6.3f, percent = %6.2f%%, count =%7d, pos = %s", report.totaltime, percent, report.callcount, report.title)
  163. -- print(temp_str)
  164. LuaProfiler._CUSTOM_REPORTS = LuaProfiler._CUSTOM_REPORTS .. temp_str .."\n"
  165. end
  166. print("----------------------------------------------->>class_list")
  167. local new_list = {}
  168. for k,v in pairs(class_list) do
  169. v.source = k
  170. table.insert(new_list, v)
  171. end
  172. table.sort(new_list, function(a, b)
  173. return a.totaltime > b.totaltime
  174. end)
  175. for source, info in ipairs(new_list) do
  176. local temp_str = string.format("totaltime = %6.3f, percent = %6.2f%%, count =%7d, pos = %s", info.totaltime, info.percent, info.callcount, info.source)
  177. -- print(temp_str)
  178. LuaProfiler._CUSTOM_REPORTS = LuaProfiler._CUSTOM_REPORTS .. temp_str .."\n"
  179. end
  180. end
  181. end