| 1 | // Checked shared workloads, including calls that stay in the interpreter. |
| 2 | module main |
| 3 | |
| 4 | import arena |
| 5 | import json2 |
| 6 | import os |
| 7 | import scheme |
| 8 | import strconv |
| 9 | import time |
| 10 | |
| 11 | struct Measurement { |
| 12 | name string |
| 13 | elapsed_ns u64 |
| 14 | first_call_ns u64 |
| 15 | result i64 |
| 16 | calls int |
| 17 | warmup_calls int |
| 18 | warmup_native_hits u64 |
| 19 | warmup_native_deopts u64 |
| 20 | native_hits u64 |
| 21 | native_deopts u64 |
| 22 | warmup_cache_hits u64 |
| 23 | warmup_compilations u64 |
| 24 | cache_hits u64 |
| 25 | compilations u64 |
| 26 | } |
| 27 | |
| 28 | fn counters(vm &scheme.VM) (u64,u64) { |
| 29 | $if vile_native ? {return vm.native_execution_stats()} |
| 30 | return 0,0 |
| 31 | } |
| 32 | |
| 33 | fn compilation_counters(vm &scheme.VM) (u64,u64) { |
| 34 | $if vile_native ? {return vm.native_compilation_stats()} |
| 35 | return 0,0 |
| 36 | } |
| 37 | |
| 38 | fn measure() ! { |
| 39 | if os.args.len!=6 {return error('expected name, source, integer oracle, batch calls, warmup calls')} |
| 40 | source:=os.read_file(os.args[2])! |
| 41 | mut vm:=scheme.new_with_options(scheme.Options{max_steps:0,max_frames:0})! |
| 42 | vm.eval(source)! |
| 43 | $if vile_native ? {vm.with_native_execution(measure_calls)!} |
| 44 | $else {measure_calls(mut vm)!} |
| 45 | } |
| 46 | |
| 47 | fn measure_calls(mut vm scheme.VM) !scheme.Value { |
| 48 | name:=os.args[1] |
| 49 | expected:=strconv.parse_int(os.args[3],10,64)! |
| 50 | count:=strconv.atoi(os.args[4])! |
| 51 | warmup:=strconv.atoi(os.args[5])! |
| 52 | if count<1||warmup<1 {return error('call counts must be positive')} |
| 53 | procedure:=vm.lookup('run-batch')! |
| 54 | first_start:=time.sys_mono_now() |
| 55 | first:=vm.call(procedure,[scheme.integer(1)])! |
| 56 | first_ns:=time.sys_mono_now()-first_start |
| 57 | if first.as_integer()! !=expected {return error('first-call oracle failed')} |
| 58 | if warmup>1 { |
| 59 | warmed:=vm.call(procedure,[scheme.integer(warmup-1)])! |
| 60 | if warmed.as_integer()! !=expected {return error('warmup oracle failed')} |
| 61 | } |
| 62 | before_hits,before_deopts:=counters(vm) |
| 63 | before_cache,before_builds:=compilation_counters(vm) |
| 64 | args:=[scheme.integer(count)] |
| 65 | $if gcboehm ? {gc_collect()} |
| 66 | start:=time.sys_mono_now() |
| 67 | result:=vm.call(procedure,args)! |
| 68 | elapsed:=time.sys_mono_now()-start |
| 69 | actual:=result.as_integer()! |
| 70 | if actual!=expected {return error('benchmark oracle failed')} |
| 71 | after_hits,after_deopts:=counters(vm) |
| 72 | after_cache,after_builds:=compilation_counters(vm) |
| 73 | println(json2.encode(Measurement{name:name,elapsed_ns:elapsed,first_call_ns:first_ns,result:actual,calls:count,warmup_calls:warmup,warmup_native_hits:before_hits,warmup_native_deopts:before_deopts,native_hits:after_hits-before_hits,native_deopts:after_deopts-before_deopts,warmup_cache_hits:before_cache,warmup_compilations:before_builds,cache_hits:after_cache-before_cache,compilations:after_builds-before_builds})) |
| 74 | return result |
| 75 | } |
| 76 | |
| 77 | fn main() { |
| 78 | $if gcboehm ? {measure() or {panic(err)}} |
| 79 | $else { |
| 80 | mut owner:=arena.new() |
| 81 | owner.push() |
| 82 | defer {owner.pop();owner.free()} |
| 83 | measure() or {panic(err)} |
| 84 | } |
| 85 | } |
| 86 | |