diff options
| author | ruki <[email protected]> | 2022-10-28 22:38:12 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-10-28 22:38:12 +0800 |
| commit | 2dcbc30142e15588356974067c36e377c72b7dd1 (patch) | |
| tree | b63db621dfbf606f80bdcc4b03ac94caf28280c1 | |
| parent | dedb33d508aa6582d24674d108466b2bdbf5c9d8 (diff) | |
improve profiler
| -rw-r--r-- | xmake/core/base/profiler.lua | 10 | ||||
| -rw-r--r-- | xmake/core/base/scheduler.lua | 16 | ||||
| -rw-r--r-- | xmake/core/main.lua | 16 |
3 files changed, 26 insertions, 16 deletions
diff --git a/xmake/core/base/profiler.lua b/xmake/core/base/profiler.lua index 6754e746d..429715eae 100644 --- a/xmake/core/base/profiler.lua +++ b/xmake/core/base/profiler.lua @@ -50,7 +50,7 @@ end -- get the function report function profiler:_func_report(funcinfo) local key = self:_func_key(funcinfo) - local report = self._REPORTS_BY_TITLE[key] + local report = self._REPORTS_BY_KEY[key] if not report then report = { @@ -58,7 +58,7 @@ function profiler:_func_report(funcinfo) , callcount = 0 , totaltime = 0 } - self._REPORTS_BY_TITLE[key] = report + self._REPORTS_BY_KEY[key] = report table.insert(self._REPORTS, report) end return report @@ -110,9 +110,9 @@ function profiler:start() if mode and mode == "trace" then debug.sethook(profiler._tracing_handler, 'cr', 0) else - self._REPORTS = {} - self._REPORTS_BY_TITLE = {} - self._STARTIME = os.clock() + self._REPORTS = self._REPORTS or {} + self._REPORTS_BY_KEY = self._REPORTS_BY_KEY or {} + self._STARTIME = self._STARTIME or os.clock() debug.sethook(profiler._profiling_handler, 'cr', 0) end end diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index ae8014dce..fe6522cad 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -360,6 +360,18 @@ function scheduler:_co_groups_resume() return resumed_count end +-- get profiler +function scheduler:_profiler() + local profiler = self._PROFILE + if profiler == nil then + profiler = require("base/profiler") + if not profiler:enabled() then + profiler = false + end + end + return profiler or nil +end + -- start a new coroutine task function scheduler:co_start(cotask, ...) return self:co_start_named(nil, cotask, ...) @@ -383,6 +395,10 @@ function scheduler:co_start_withopt(opt, cotask, ...) -- start coroutine local co co = _coroutine.new(coname, coroutine.create(function(...) + local profiler = self:_profiler() + if profiler and profiler:enabled() then + profiler:start() + end self:_co_curdir_update() self:_co_curenvs_update() cotask(...) diff --git a/xmake/core/main.lua b/xmake/core/main.lua index c2e086fe8..4e061f45d 100644 --- a/xmake/core/main.lua +++ b/xmake/core/main.lua @@ -275,28 +275,22 @@ Or you can add `--root` option or XMAKE_ROOT=y to allow run as root temporarily. -- run task scheduler:enable(true) scheduler:co_start_named("xmake " .. taskname, function () - - -- start profiling - if profiler:enabled() then - profiler:start() - end - - -- do task local ok, errors = taskinst:run() if not ok then os.raise(errors) end - -- stop profiling - if profiler:enabled() then - profiler:stop() - end end) ok, errors = scheduler:runloop() if not ok then return main._exit(ok, errors) end + -- stop profiling + if profiler:enabled() then + profiler:stop() + end + -- exit normally return main._exit(true) end |
