3 .\" Manual for benchmarking core
5 .\" (c) 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH bench 3mLib "9 March 2024" "Straylight/Edgeware" "mLib utilities library"
32 .\" @bench_createtimer
38 .\"--------------------------------------------------------------------------
40 bench \- low-level benchmarking tools
42 .\"--------------------------------------------------------------------------
46 .B "#include <mLib/bench.h>"
49 .B "struct bench_time {"
52 .B " struct { kludge64 s; uint32 ns; } ts;"
59 .B "struct bench_timing {"
66 .B "#define BTF_T0 0u"
67 .B "#define BTF_T1 ..."
68 .B "struct bench_timerops {"
69 .BI " void (*describe)(struct bench_timer *" bt ", dstr *" d );
70 .ta 2n +\w'\fBint (*now)('u
71 .BI " int (*now)(struct bench_timer *" bt ,
72 .BI " struct bench_time *" t_out ", unsigned " f );
73 .ta 2n +\w'\void (*diff)('u
74 .BI " void (*diff)(struct bench_timer *" bt ,
75 .BI " struct bench_timing *" delta_out ,
76 .BI " const struct bench_time *" t0 ,
77 .BI " const struct bench_time *" t1 );
78 .BI " void (*destroy)(struct bench_timer *" bt );
80 .B "struct bench_timer {"
81 .B " const struct bench_timerops *ops;"
84 .B "struct bench_state {"
86 .B " double target_s;"
90 .BI "typedef void bench_fn(unsigned long " n ", void *" ctx );
92 .B "#define BTF_TIMEOK ..."
93 .B "#define BTF_CYOK ..."
94 .B "#define BTF_CLB ..."
95 .B "#define BTF_ANY (BTF_TIMEOK | BTF_CYOK)"
97 .B "struct bench_timer *bench_createtimer(void);"
99 .BI "int bench_init(struct bench_state *" b ", struct bench_timer *" tm );
100 .BI "void bench_destroy(struct bench_state *" b );
101 .BI "int bench_calibrate(struct bench_state *" b );
102 .ta \w'\fBint bench_measure('u
103 .BI "int bench_measure(struct bench_state *" b ", struct bench_timing *" t_out ,
104 .BI " double " base ", bench_fn *" fn ", void *" ctx );
107 .\"--------------------------------------------------------------------------
112 provides declarations and defintions
113 for performing low-level benchmarks.
117 This function will be described in detail later,
119 it calls a caller-provided function,
120 instructing it to run adaptively chosen numbers of iterations,
121 in order to get a reasonably reliable measurement of its running time,
122 and then reports its results by filling in a structure.
124 With understanding this function as our objective,
125 we must examine all of the pieces involved in making it work.
127 .SS Timers in general
130 is a gadget which is capable of reporting the current time,
131 in seconds (ideally precise to tiny fractions of a second),
132 and/or in CPU cycles.
133 A timer is represented by a pointer to an object of type
134 .BR "struct bench_timer" .
135 This structure has a single member,
138 .BR "struct bench_timerops" ,
139 which is a table of function pointers;
140 typically, a timer has more data following this,
141 but this fact is not exposed to applications.
143 The function pointers in
144 .B "struct bench_timerops"
149 must always point to the timer object itself.
151 .IB tm ->ops->describe( tm ", " d)
152 Write a description of the timer to the dynamic string
155 .IB tm ->ops->now( tm ", " t_out ", " f )
156 Store the current time in
162 to indicate that this is the second call in a pair;
163 leave it clear for the first call.
166 flag is defined to be zero for symmetry.)
167 Return zero on success
170 return \-1 if timing failed but
171 trying again immediately has a reasonable chance of success.
173 .IB tm ->ops->diff( tm ", " delta_out ", " t0 ", " t1 )
176 the difference between the two times
181 .IB tm ->ops->destroy( tm )
183 releasing all of the resources that it holds.
187 structure reports the difference between two times,
188 as determined by a timer's
196 is set if the passage-of-time measurement in
200 is set if the cycle count in
213 if the timer returned any valid timing information.
216 The number of iterations performed by the benchmark function
217 on its satisfactory run,
222 The time taken for the satisfactory run of the benchmark function,
230 The number of CPU cycles used
231 in the satisfactory run of the benchmark function,
239 .B "struct bench_time"
240 represents a single instant in time,
241 as captured by a timer's
244 The use of this structure is a private matter for the timer:
245 the only hard requirement is that the
247 function should be able to compute the difference between two times.
248 However, the intent is that
249 a passage-of-time measurement is stored in the
252 a cycle count is stored in the
261 if the passage-of-time or cycle count respectively are valid.
263 .SS The built-in timer
266 constructs and returns a timer.
267 It takes a single argument,
270 from which it reads configuration information.
273 fails, it returns a null pointer.
277 pointer may safely be null,
278 in which case a default configuration will be used.
281 set this pointer to a value supplied by a user,
282 e.g., through a command-line argument,
283 environment variable, or
286 The built-in timer makes use of one or two
288 a `clock' subtimer to measure the passage of time,
289 and possibly a `cycle' subtimer to count CPU cycles.
291 The configuration string consists of a sequence of words
292 separated by whitespace.
293 There may be additional whitespace at the start and end of the string.
294 The words recognized are as follows.
297 Prints a list of the available clock and cycle subtimers
301 Use the first of the listed clock subtimers
302 to initialize successfully
303 as the clock subtimer.
304 If none of the subtimers can be initialized,
305 then construction of the timer as a whole fails.
308 Use the first of the listed subtimers
309 to initialize successfully
310 as the cycle subtimer.
311 If none of the subtimers can be initialized,
312 then construction of the timer as a whole fails.
314 The clock subtimers are as follows.
315 Not all of them will be available on every platform.
317 .B linux-x86-perf-rdpmc-hw-cycles
318 This is a dummy companion to the similarly named cycle subtimer;
319 see its description below.
321 .B posix-thread-cputime
322 Measures the passage of time using
323 .BR clock_gettime (2),
325 .B CLOCK_\%THREAD_\%CPUTIME_\%ID
329 Measures the passage of time using
333 is part of the original ANSI\ C standard,
334 this subtimer should always be available.
335 However, it may produce unhelpful results
336 if other threads are running.
338 The cycle subtimers are as follows.
339 Not all of them will be available on every platform.
341 .B linux-perf-read-hw-cycles
342 Counts CPU cycles using the Linux-specific
343 .BR perf_event_open (2)
345 .BR PERF_\%COUNT_\%HW_\%CPU_\%CYCLES
347 Only available on Linux.
348 It will fail to initialize
349 if access to performance counters is restricted,
351 .B /proc/sys/kernel/perf_event_paranoid
354 .B linux-perf-rdpmc-hw-cycles
355 Counts CPU cycles using the Linux-specific
356 .BR perf_event_open (2)
359 .B linux-x86-perf-read-hw-cycles
361 except that it additionally uses the i386/AMD64
366 together with information provided by the kernel
367 through a memory-mapped page
368 to do its measurements without any system call overheads.
369 It does passage-of-time and cycle counting in a single operation,
370 so no separate clock subtimer is required:
371 the similarly-named clock subtimer does nothing
372 except check that the
373 .B linux-x86-perf-rdpmc-hw-cycles
374 cycle subtimer has been selected.
375 This is almost certainly the best choice if it's available.
378 Counts CPU cycles using the x86
381 This instruction is not really suitable for performance measurement:
382 it gives misleading results on CPUs with variable clock frequency.
385 Counts CPU cycles using the x86
388 This has the downsides of
391 but also fails to detect when the thread has been suspended
392 or transferred to a different CPU core
393 and gives misleading answers in this case.
394 Not really recommended.
397 A dummy cycle counter,
398 which will initialize successfully
399 and then fail to report cycle counts.
400 This is a reasonable fallback in many situations.
402 The built-in preference order for clock subtimers,
403 from most to least preferred, is
404 .BR linux-x86-perf-rdpmc-hw-cycles ,
406 .BR posix-thread-cputime ,
409 The built-in preference order for cycle subtimers,
410 from most to least preferred, is
411 .BR linux-x86-perf-rdpmc-hw-cycles
413 .BR linux-x86-perf-read-hw-cycles ,
421 .SS The benchmark state
424 tracks the information needed to measure performance of functions.
425 It is represented by a
426 .B struct bench_state
429 The benchmark state is initialized by calling
431 passing the address of the state structure to be initialized,
432 and a pointer to a timer.
435 is called with a non-null timer pointer,
436 then it will not fail;
437 the benchmark state will be initialized,
438 and the function returns zero.
439 If the timer pointer is null,
442 attempts to construct a timer for itself
444 .BR bench_createtimer .
446 then the benchmark state will be initialized,
447 and the function returns zero.
449 the timer becomes owned by the benchmark state:
452 on the benchmark state will destroy the timer.
455 is called with a null timer pointer,
456 and its attempt to create a timer for itself fails,
460 the benchmark state is not initialized
461 and can safely be discarded;
465 on the unsuccessfully benchmark state is safe and has no effect.
470 releases any resources it holds,
471 most notably its timer, if any.
474 .B struct bench_state
475 is defined in the header file,
476 only two members are available for use by applications.
479 A word containing flags.
482 The target time for which to try run a benchmark, in seconds.
483 After initialization, this is set to 1.0,
484 though applications can override it.
486 Before the benchmark state can be used in measurements,
489 This is performed by calling
491 on the benchmark state.
492 Calibration takes a noticeable amount of time
493 (currently about 0.25\*,s),
494 so it makes sense to defer it until it's known to be necessary.
496 Calibration is carried out separately, but in parallel,
497 for the timer's passage-of-time measurement and cycle counter.
498 Either or both of these calibrations can succeed or fail;
499 if passage-of-time calibration fails,
500 then cycle count calibration is impossible.
504 sets flag in the benchmark state's
507 if passage-of-time calibration succeeded,
510 if cycle-count calibration succeeded,
515 is set unconditionally,
516 as a persistent indication that calibration has been attempted.
520 function returns zero if it successfully calibrated
521 at least the passage-of-time measurement;
522 otherwise, it returns \-1.
525 is called for a second or subsequent time on the same benchmark state,
526 it returns immediately,
527 either returning 0 or \-1
528 according to whether passage-of-time had previously been calibrated.
532 .I benchmark function
535 .BI "void " fn "(unsigned long " n ", void *" ctx );
537 When called, it should perform the operation to be measured
542 argument is a pointer passed into
544 for the benchmark function's own purposes.
548 receives five arguments.
551 points to the benchmark state to be used.
555 .BR struct bench_timing
556 in which the measurement should be left.
557 This structure is described below.
560 is a count of the number of operations performed
561 by each iteration of the benchmark function.
564 is a benchmark function, described above.
567 is a pointer to be passed to the benchmark function.
569 does not interpret this pointer in any way.
573 function calls its benchark function repeatedly
574 with different iteration counts
576 with the objective that the call take approximately
578 seconds, as established in the benchmark state.
585 is satisfied when a call takes at least
587 Once the function finds a satisfactory number of iterations,
588 it stores the results in
590 If measurement succeeds, then
594 most likely because the timer failed \(en
597 .\"--------------------------------------------------------------------------
603 .\"--------------------------------------------------------------------------
606 Mark Wooding, <mdw@distorted.org.uk>
608 .\"----- That's all, folks --------------------------------------------------