1*c54f35caSApple OSS Distributions#!/usr/local/bin/recon 2*c54f35caSApple OSS Distributionsrequire 'strict' 3*c54f35caSApple OSS Distributions 4*c54f35caSApple OSS Distributionslocal benchrun = require 'benchrun' 5*c54f35caSApple OSS Distributionslocal perfdata = require 'perfdata' 6*c54f35caSApple OSS Distributionslocal sysctl = require 'sysctl' 7*c54f35caSApple OSS Distributionslocal csv = require 'csv' 8*c54f35caSApple OSS Distributions 9*c54f35caSApple OSS Distributionslocal kDefaultNumWrites = 10000000000 10*c54f35caSApple OSS Distributions 11*c54f35caSApple OSS Distributionslocal benchmark = benchrun.new { 12*c54f35caSApple OSS Distributions name = 'xnu.per_cpu_counter', 13*c54f35caSApple OSS Distributions version = 1, 14*c54f35caSApple OSS Distributions arg = arg, 15*c54f35caSApple OSS Distributions modify_argparser = function(parser) 16*c54f35caSApple OSS Distributions parser:argument{ 17*c54f35caSApple OSS Distributions name = 'path', 18*c54f35caSApple OSS Distributions description = 'Path to benchmark binary' 19*c54f35caSApple OSS Distributions } 20*c54f35caSApple OSS Distributions parser:option{ 21*c54f35caSApple OSS Distributions name = '--cpu-workers', 22*c54f35caSApple OSS Distributions description = 'Number of cpu workers' 23*c54f35caSApple OSS Distributions } 24*c54f35caSApple OSS Distributions parser:flag{ 25*c54f35caSApple OSS Distributions name = '--through-max-workers', 26*c54f35caSApple OSS Distributions description = 'Run benchmark for [1..n] cpu workers' 27*c54f35caSApple OSS Distributions } 28*c54f35caSApple OSS Distributions parser:flag{ 29*c54f35caSApple OSS Distributions name = '--through-max-workers-fast', 30*c54f35caSApple OSS Distributions description = 'Run benchmark for [1..2] and each power of four value in [4..n] cpu workers' 31*c54f35caSApple OSS Distributions } 32*c54f35caSApple OSS Distributions parser:option { 33*c54f35caSApple OSS Distributions name = "--num-writes", 34*c54f35caSApple OSS Distributions description = "number of writes", 35*c54f35caSApple OSS Distributions default = kDefaultNumWrites 36*c54f35caSApple OSS Distributions } 37*c54f35caSApple OSS Distributions parser:option{ 38*c54f35caSApple OSS Distributions name = '--variant', 39*c54f35caSApple OSS Distributions description = 'Which benchmark variant to run (scalable, atomic, or racy)', 40*c54f35caSApple OSS Distributions default = 'scalable', 41*c54f35caSApple OSS Distributions choices = {"scalable", "atomic", "racy"} 42*c54f35caSApple OSS Distributions } 43*c54f35caSApple OSS Distributions end 44*c54f35caSApple OSS Distributions} 45*c54f35caSApple OSS Distributions 46*c54f35caSApple OSS Distributionsassert(benchmark.opt.path, "No path supplied for fault throughput binary") 47*c54f35caSApple OSS Distributions 48*c54f35caSApple OSS Distributionslocal ncpus, err = sysctl('hw.logicalcpu_max') 49*c54f35caSApple OSS Distributionsassert(ncpus > 0, 'invalid number of logical cpus') 50*c54f35caSApple OSS Distributionslocal cpu_workers = tonumber(benchmark.opt.cpu_workers) or ncpus 51*c54f35caSApple OSS Distributions 52*c54f35caSApple OSS Distributionslocal writes_per_second = perfdata.unit.custom('writes/sec') 53*c54f35caSApple OSS Distributionslocal tests = {} 54*c54f35caSApple OSS Distributions 55*c54f35caSApple OSS Distributionsfunction QueueTest(num_cores) 56*c54f35caSApple OSS Distributions table.insert(tests, { 57*c54f35caSApple OSS Distributions path = benchmark.opt.path, 58*c54f35caSApple OSS Distributions num_cores = num_cores, 59*c54f35caSApple OSS Distributions }) 60*c54f35caSApple OSS Distributionsend 61*c54f35caSApple OSS Distributions 62*c54f35caSApple OSS Distributionsif benchmark.opt.through_max_workers then 63*c54f35caSApple OSS Distributions for i = 1, cpu_workers do 64*c54f35caSApple OSS Distributions QueueTest(i) 65*c54f35caSApple OSS Distributions end 66*c54f35caSApple OSS Distributionselseif benchmark.opt.through_max_workers_fast then 67*c54f35caSApple OSS Distributions local i = 1 68*c54f35caSApple OSS Distributions while i <= cpu_workers do 69*c54f35caSApple OSS Distributions QueueTest(i) 70*c54f35caSApple OSS Distributions -- Always do a run with two threads to see what the first part of 71*c54f35caSApple OSS Distributions -- the scaling curve looks like 72*c54f35caSApple OSS Distributions -- (and to measure perf on dual core systems). 73*c54f35caSApple OSS Distributions if i == 1 and cpu_workers >= 2 then 74*c54f35caSApple OSS Distributions QueueTest(i + 1) 75*c54f35caSApple OSS Distributions end 76*c54f35caSApple OSS Distributions i = i * 4 77*c54f35caSApple OSS Distributions end 78*c54f35caSApple OSS Distributionselse 79*c54f35caSApple OSS Distributions QueueTest(cpu_workers) 80*c54f35caSApple OSS Distributionsend 81*c54f35caSApple OSS Distributions 82*c54f35caSApple OSS Distributionsfor _, test in ipairs(tests) do 83*c54f35caSApple OSS Distributions local args = {test.path, benchmark.opt.variant, benchmark.opt.num_writes, test.num_cores, 84*c54f35caSApple OSS Distributions echo = true} 85*c54f35caSApple OSS Distributions for out in benchmark:run(args) do 86*c54f35caSApple OSS Distributions local result = out:match("-----Results-----\n(.*)") 87*c54f35caSApple OSS Distributions benchmark:assert(result, "Unable to find result data in output") 88*c54f35caSApple OSS Distributions local data = csv.openstring(result, {header = true}) 89*c54f35caSApple OSS Distributions for field in data:lines() do 90*c54f35caSApple OSS Distributions for k, v in pairs(field) do 91*c54f35caSApple OSS Distributions local unit = writes_per_second 92*c54f35caSApple OSS Distributions local larger_better = true 93*c54f35caSApple OSS Distributions if k == "loss" then 94*c54f35caSApple OSS Distributions unit = percentage 95*c54f35caSApple OSS Distributions larger_better = false 96*c54f35caSApple OSS Distributions end 97*c54f35caSApple OSS Distributions benchmark.writer:add_value(k, unit, tonumber(v), { 98*c54f35caSApple OSS Distributions [perfdata.larger_better] = larger_better, 99*c54f35caSApple OSS Distributions threads = test.num_cores, 100*c54f35caSApple OSS Distributions variant = benchmark.opt.variant 101*c54f35caSApple OSS Distributions }) 102*c54f35caSApple OSS Distributions end 103*c54f35caSApple OSS Distributions end 104*c54f35caSApple OSS Distributions end 105*c54f35caSApple OSS Distributionsend 106*c54f35caSApple OSS Distributions 107*c54f35caSApple OSS Distributionsbenchmark:finish() 108