1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

sync: implement pool.work_on_items to process a list of items in parallel

This commit is contained in:
Alexander Medvednikov
2020-03-04 20:28:42 +01:00
committed by GitHub
parent 136aa763a3
commit b0ece3a9d8
7 changed files with 423 additions and 215 deletions

View File

@ -3,7 +3,6 @@ module main
import os
import compiler.tests.repl.runner
import benchmark
import runtime
import sync
import filepath
@ -28,77 +27,65 @@ fn test_the_v_compiler_can_be_invoked() {
struct Session {
mut:
options runner.RunnerOptions
bmark benchmark.Benchmark
ntask int
ntask_mtx &sync.Mutex
waitgroup &sync.WaitGroup
options runner.RunnerOptions
bmark benchmark.Benchmark
}
fn test_all_v_repl_files() {
mut session := &Session{
options: runner.new_options()
bmark: benchmark.new_benchmark()
ntask: 0
ntask_mtx: sync.new_mutex()
waitgroup: sync.new_waitgroup()
}
// warmup, and ensure that the vrepl is compiled in single threaded mode if it does not exist
runner.run_repl_file(os.cachedir(), session.options.vexec, 'vlib/compiler/tests/repl/nothing.repl') or {
panic(err)
}
session.bmark.set_total_expected_steps( session.options.files.len )
mut ncpus := 0
ncpus = runtime.nr_cpus()
session.bmark.set_total_expected_steps(session.options.files.len)
mut pool_repl := sync.new_pool_processor({
callback: worker_repl
})
pool_repl.set_shared_context(session)
$if windows {
// See: https://docs.microsoft.com/en-us/cpp/build/reference/fs-force-synchronous-pdb-writes?view=vs-2019
ncpus = 1
// See: https://docs.microsoft.com/en-us/cpp/build/reference/fs-force-synchronous-pdb-writes?view=vs-2019
pool_repl.set_max_jobs(1)
}
session.waitgroup.add( ncpus )
for i:=0; i < ncpus; i++ {
go process_in_thread(session,i)
}
session.waitgroup.wait()
pool_repl.work_on_items<string>(session.options.files)
session.bmark.stop()
println(session.bmark.total_message('total time spent running REPL files'))
}
fn process_in_thread( session mut Session, thread_id int ){
fn worker_repl(p mut sync.PoolProcessor, idx int, thread_id int) voidptr {
cdir := os.cachedir()
mut tls_bench := benchmark.new_benchmark()
tls_bench.set_total_expected_steps( session.bmark.nexpected_steps )
for {
session.ntask_mtx.lock()
session.ntask++
idx := session.ntask-1
session.ntask_mtx.unlock()
if idx >= session.options.files.len { break }
tls_bench.cstep = idx
tfolder := filepath.join( cdir, 'vrepl_tests_$idx')
if os.is_dir( tfolder ) {
os.rmdir_all( tfolder )
}
os.mkdir( tfolder ) or { panic(err) }
file := session.options.files[ idx ]
session.bmark.step()
tls_bench.step()
fres := runner.run_repl_file(tfolder, session.options.vexec, file) or {
session.bmark.fail()
tls_bench.fail()
os.rmdir_all( tfolder )
eprintln(tls_bench.step_message_fail(err))
assert false
continue
}
session.bmark.ok()
tls_bench.ok()
os.rmdir_all( tfolder )
println(tls_bench.step_message_ok(fres))
assert true
mut session := &Session(p.get_shared_context())
mut tls_bench := &benchmark.Benchmark(p.get_thread_context(idx))
if isnil(tls_bench) {
tls_bench = benchmark.new_benchmark_pointer()
tls_bench.set_total_expected_steps(session.bmark.nexpected_steps)
p.set_thread_context(idx, tls_bench)
}
session.waitgroup.done()
tls_bench.cstep = idx
tfolder := filepath.join(cdir,'vrepl_tests_$idx')
if os.is_dir(tfolder) {
os.rmdir_all(tfolder)
}
os.mkdir(tfolder) or {
panic(err)
}
file := p.get_string_item(idx)
session.bmark.step()
tls_bench.step()
fres := runner.run_repl_file(tfolder, session.options.vexec, file) or {
session.bmark.fail()
tls_bench.fail()
os.rmdir_all(tfolder)
eprintln(tls_bench.step_message_fail(err))
assert false
return sync.no_result
}
session.bmark.ok()
tls_bench.ok()
os.rmdir_all(tfolder)
println(tls_bench.step_message_ok(fres))
assert true
return sync.no_result
}