diff options
| author | ruki <[email protected]> | 2025-03-19 23:23:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-04-08 15:31:54 +0800 |
| commit | f8346ab6e40bdcf9506f1400f74860bbcea764ff (patch) | |
| tree | f865425c73e199f314c0600d08809f8768e190c3 | |
| parent | 6785ce5822f298ce64684548dfc9d35ec8dd5030 (diff) | |
improve jobgraph tests
| -rw-r--r-- | tests/modules/async/run_jobgraph.lua | 13 | ||||
| -rw-r--r-- | xmake/modules/async/jobgraph.lua | 44 | ||||
| -rw-r--r-- | xmake/modules/async/runjobs.lua | 5 |
3 files changed, 51 insertions, 11 deletions
diff --git a/tests/modules/async/run_jobgraph.lua b/tests/modules/async/run_jobgraph.lua index bcf5be2fd..ec9c9175f 100644 --- a/tests/modules/async/run_jobgraph.lua +++ b/tests/modules/async/run_jobgraph.lua @@ -2,22 +2,23 @@ import("core.base.scheduler") import("async.jobgraph") import("async.runjobs") -function _jobfunc(job, opt) - print("%s: run job (%s)", scheduler.co_running(), job.name) +function _jobfunc(index, total, opt) + print("%s: run job (%d/%d)", scheduler.co_running(), index, total) local dt = os.mclock() os.sleep(1000) dt = os.mclock() - dt - print("%s: run job (%s) end, progress: %s, dt: %d ms", scheduler.co_running(), job.name, opt.progress, dt) + print("%s: run job (%d/%d) end, progress: %s, dt: %d ms", scheduler.co_running(), index, total, opt.progress, dt) end function main() print("==================================== test jobpool ====================================") local jobs = jobgraph.new() - jobs:add_job("job/root", _jobfunc) + jobs:add("job/root", _jobfunc) for i = 1, 3 do - jobs:add_job("job/" .. i, _jobfunc) + jobs:add("job/" .. i, _jobfunc) for j = 1, 50 do - jobs:add_job("job/" .. i .. "/" .. j, _jobfunc) + jobs:add("job/" .. i .. "/" .. j, _jobfunc) + jobs:add_deps("job/" .. i .. "/" .. j, "job/" .. i, "job/root") end end t = os.mclock() diff --git a/xmake/modules/async/jobgraph.lua b/xmake/modules/async/jobgraph.lua index 30513ae56..313bc2d11 100644 --- a/xmake/modules/async/jobgraph.lua +++ b/xmake/modules/async/jobgraph.lua @@ -24,7 +24,16 @@ import("core.base.list") import("core.base.graph") -- define module -local jobgraph = jobgraph or object {_init = {"_jobs", "_graph"}} +local jobqueue = jobqueue or object {_init = {"_jobgraph"}} +local jobgraph = jobgraph or object {_init = {"_jobs", "_graph", "_dirty"}} + +-- remove the given job from the job queue +function jobqueue:remove(job) +end + +-- get a free job from the job queue +function jobqueue:getfree() +end -- get jobs function jobgraph:jobs() @@ -34,24 +43,49 @@ end -- add a job to the jobgraph -- -- e.g. --- jobgraph:add_job("xxx", function (job, opt) +-- jobgraph:add("xxx", function (index, total, opt) -- end) -- -- @param name the job name -- @param run the job run command/script -- @param opt the job options -- -function jobgraph:add_job(name, run, opt) +function jobgraph:add(name, run, opt) local job = {name = name, run = run, opt = opt} self:jobs():insert(job) + self._dirty = true +end + +-- remove a given job +function jobgraph:remove(name) + self._dirty = true +end + +-- add job deps, e.g. add_deps(a, b, c, ...): a -> b -> c, ... +function jobgraph:add_deps(...) + local deps = table.pack(...) +end + +-- add jog group +function jobgraph:add_group(name, callback) +end + +-- build a job queue +function jobgraph:build() + return jobqueue {self} +end + +-- get job size +function jobgraph:size() + return self:jobs():size() end -- tostring function jobgraph:__tostring() - return string.format("<jobgraph:%s>", self:jobs():size()) + return string.format("<jobgraph:%s>", self:size()) end -- new a jobgraph function new() - return jobgraph {list.new(), graph.new(true)} + return jobgraph {list.new(), graph.new(true), false} end diff --git a/xmake/modules/async/runjobs.lua b/xmake/modules/async/runjobs.lua index 0c2a6f366..959eb1e77 100644 --- a/xmake/modules/async/runjobs.lua +++ b/xmake/modules/async/runjobs.lua @@ -64,6 +64,11 @@ function main(name, jobs, opt) local group_name = name local jobs_cb = type(jobs) == "function" and jobs or nil assert(timeout < 60000, "runjobs: invalid timeout!") + + -- build jobs queue + if jobs.build then + jobs = jobs:build() + end assert(jobs, "runjobs: no jobs!") -- show waiting tips? |
