summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2016-03-27 21:55:01 +0800
committerruki <[email protected]>2016-03-27 21:55:01 +0800
commitf86b3d2ae37f1bb0633246f7d1cea81f996b7d48 (patch)
tree2df385090d3582f684f9777037e1264ff30551d6
parent6d429805c8699f6a27cf3e8d5297391e2c7b592c (diff)
add trace for profiler
-rw-r--r--xmake/core/base/profiler.lua103
-rw-r--r--xmake/core/main.lua4
2 files changed, 73 insertions, 34 deletions
diff --git a/xmake/core/base/profiler.lua b/xmake/core/base/profiler.lua
index 31a2fd093..db2d671a2 100644
--- a/xmake/core/base/profiler.lua
+++ b/xmake/core/base/profiler.lua
@@ -79,8 +79,8 @@ function profiler:_func_report(funcinfo)
return report
end
--- hook call
-function profiler:_hook_call(funcinfo)
+-- profiling call
+function profiler:_profiling_call(funcinfo)
-- get the function report
local report = self:_func_report(funcinfo)
@@ -94,8 +94,8 @@ function profiler:_hook_call(funcinfo)
end
--- hook return
-function profiler:_hook_return(funcinfo)
+-- profiling return
+function profiler:_profiling_return(funcinfo)
-- get the stoptime
local stoptime = os.clock()
@@ -110,56 +110,95 @@ function profiler:_hook_return(funcinfo)
end
end
--- the hook handler
-function profiler._hook_handler(hooktype)
+-- the profiling handler
+function profiler._profiling_handler(hooktype)
-- the function info
local funcinfo = debug.getinfo(2, 'nS')
-- dispatch it
if hooktype == "call" then
- profiler:_hook_call(funcinfo)
+ profiler:_profiling_call(funcinfo)
elseif hooktype == "return" then
- profiler:_hook_return(funcinfo)
+ profiler:_profiling_return(funcinfo)
end
end
--- start profiling
-function profiler:start()
+-- the tracing handler
+function profiler._tracing_handler(hooktype)
+
+ -- the function info
+ local funcinfo = debug.getinfo(2, 'nS')
+
+ -- is call?
+ if hooktype == "call" then
+
+ -- is xmake function?
+ local name = funcinfo.name
+ local source = funcinfo.short_src or 'C_FUNC'
+ if name and os.isfile(source) then
+
+ -- the function line
+ local line = string.format("%d", funcinfo.linedefined or 0)
+
+ -- get the relative source
+ source = path.relative(source, xmake._PROGRAM_DIR)
- -- init reports
- self._REPORTS = {}
- self._REPORTS_BY_TITLE = {}
+ -- trace it
+ utils.printf("%-30s: %s: %s", name, source, line)
+ end
+ end
+end
- -- save the start time
- self._STARTIME = os.clock()
+-- start profiling
+function profiler:start(mode)
- -- start to hook
- debug.sethook(profiler._hook_handler, 'cr', 0)
+ -- trace?
+ if mode and mode == "trace" then
+ debug.sethook(profiler._tracing_handler, 'cr', 0)
+ else
+ -- init reports
+ self._REPORTS = {}
+ self._REPORTS_BY_TITLE = {}
+ -- save the start time
+ self._STARTIME = os.clock()
+
+ -- start to hook
+ debug.sethook(profiler._profiling_handler, 'cr', 0)
+ end
end
-- stop profiling
-function profiler:stop()
+function profiler:stop(mode)
- -- save the stop time
- self._STOPTIME = os.clock()
+ -- trace?
+ if mode and mode == "trace" then
- -- stop to hook
- debug.sethook()
+ -- stop to hook
+ debug.sethook()
- -- calculate the total time
- local totaltime = self._STOPTIME - self._STARTIME
+ else
- -- sort reports
- table.sort(self._REPORTS, function(a, b)
- return a.totaltime > b.totaltime
- end)
+ -- save the stop time
+ self._STOPTIME = os.clock()
- -- show reports
- for _, report in ipairs(self._REPORTS) do
- utils.printf("%04.3f, %5.2f%%, %7d, %s", report.totaltime, (report.totaltime / totaltime) * 100, report.callcount, report.title)
- end
+ -- stop to hook
+ debug.sethook()
+
+ -- calculate the total time
+ local totaltime = self._STOPTIME - self._STARTIME
+
+ -- sort reports
+ table.sort(self._REPORTS, function(a, b)
+ return a.totaltime > b.totaltime
+ end)
+
+ -- show reports
+ for _, report in ipairs(self._REPORTS) do
+ utils.printf("%04.3f, %5.2f%%, %7d, %s", report.totaltime, (report.totaltime / totaltime) * 100, report.callcount, report.title)
+ end
+ end
end
-- return module
diff --git a/xmake/core/main.lua b/xmake/core/main.lua
index dab1627db..d856fc686 100644
--- a/xmake/core/main.lua
+++ b/xmake/core/main.lua
@@ -101,7 +101,7 @@ end
function main.done()
-- start profiling
--- profiler:start()
+ profiler:start()
-- init
main._init()
@@ -123,7 +123,7 @@ function main.done()
end
-- stop profiling
--- profiler:stop()
+ profiler:stop()
-- ok
return 0