chiark / gitweb /
*.[ch]: Remove unnecessary header files.
[mLib] / test / bench.c
1 /* -*-c-*-
2  *
3  * Benchmarking support
4  *
5  * (c) 2023 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the mLib utilities library.
11  *
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.
16  *
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.
21  *
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,
25  * USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "config.h"
31
32 #include <ctype.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <math.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <time.h>
39
40 #include "alloc.h"
41 #include "arena.h"
42 #include "bench.h"
43 #include "bits.h"
44 #include "dstr.h"
45 #include "linreg.h"
46 #include "macros.h"
47
48 #if GCC_VERSION_P(4, 5) && (defined(__i386__) || defined(__x86_64__))
49 #  include <cpuid.h>
50 #  define CPUID_1D_TSC (1u << 4)
51 #  define CPUID_1xD_TSCP (1u << 27)
52 #  define USE_X86_RDTSC 1
53 #endif
54
55 #if defined(HAVE_LINUX_PERF_EVENT_H) && defined(HAVE_UINT64)
56 #  include <sys/syscall.h>
57 #  include <sys/types.h>
58 #  include <unistd.h>
59 #  include <linux/perf_event.h>
60 #  ifdef HAVE_VALGRIND_VALGRIND_H
61 #    include <valgrind/valgrind.h>
62 #  endif
63 #  define USE_LINUX_PERFEVENT 1
64 #  if GCC_VERSION_P(4, 5) && (defined(__i386__) || defined(__x86_64__))
65 #    include <sys/mman.h>
66 #    define USE_LINUX_PERFEVRDPMC 1
67 #  endif
68 #endif
69
70 /*----- Data structures ---------------------------------------------------*/
71
72 enum { CLK, CY, NTIMER };
73
74 struct timer {
75   struct bench_timer _t;
76   arena *a;
77   const struct timer_ops *ops[NTIMER];  /* subtimers for clock and cycles */
78   union {
79 #ifdef USE_X86_RDTSC
80     unsigned tscaux;                    /* `ia32_tsc_aux' for `ldtscp' */
81 #endif
82 #ifdef USE_LINUX_PERFEVENT
83     int fd;                             /* vanilla `perf_event_open' */
84 #endif
85 #ifdef USE_LINUX_PERFEVRDPMC
86     struct {                            /* `perf_event_open' with `rdpmc' */
87       const volatile void *map; size_t sz; /* memory-mapped info */
88       pid_t owner;                      /*   owning thread id */
89     } pmc;
90 #endif
91   } u_cy;                               /* state for cycle measurement */
92 };
93
94 struct timer_ops {
95   const char *name;                     /* timer name */
96   unsigned f;                           /* flags */
97 /* ... @BTF_...OK@ flags */             /*   expected results */
98 #define TF_SECRET 16u                   /*   don't try this automatically */
99   int (*init)(struct timer */*t*/);     /* initialization function */
100   int (*preflight)(struct timer */*t*/); /* preflight checks */
101   int (*now)(struct timer */*t*/,       /* read current */
102              struct bench_time */*t_out*/, unsigned /*f*/);
103   void (*diff)(struct timer */*t*/,     /* difference */
104                struct bench_timing */*t_inout*/,
105                const struct bench_time */*t0*/,
106                const struct bench_time */*t1*/);
107   void (*teardown)(struct timer */*t*/); /* release held resources */
108 };
109
110 /*----- Preliminaries -----------------------------------------------------*/
111
112 #define NS_PER_S 1000000000
113
114 /* --- @debug@ --- *
115  *
116  * Arguments:   @const char *fmt@ = format control string
117  *              @...@ = format arguemnts
118  *
119  * Returns:     ---
120  *
121  * Use:         Maybe report a debugging message to standard error.
122  */
123
124 static PRINTF_LIKE(1, 2) void debug(const char *fmt, ...)
125 {
126   const char *p;
127   va_list ap;
128
129   p = getenv("MLIB_BENCH_DEBUG");
130   if (p && *p != 'n' && *p != '0') {
131     va_start(ap, fmt);
132     fputs("mLib BENCH: ", stderr);
133     vfprintf(stderr, fmt, ap);
134     fputc('\n', stderr);
135     va_end(ap);
136   }
137 }
138
139 #ifdef HAVE_UINT64
140 #  define FLOATK64(k) ((double)(k).i)
141 #else
142 #  define FLOATK64(k) ((double)(k).lo + 4294967296.0*(double)(k).hi)
143 #endif
144
145 /* --- @diff_ts@ --- *
146  *
147  * Arguments:   @struct timer *t@ = timer structure
148  *              @struct bench_timing *delta_inout@ = where to put the result
149  *              @const struct time *t0, *t1@ = two input times
150  *
151  * Returns:     ---
152  *
153  * Use:         Calculates a time difference for timers using the
154  *              @struct timespec@-like time format.
155  */
156
157 static void diff_ts(struct timer *t, struct bench_timing *delta_inout,
158                     const struct bench_time *t0, const struct bench_time *t1)
159 {
160   unsigned f = t0->f&t1->f;
161   kludge64 delta_s;
162   uint32 delta_ns;
163
164   if (f&BTF_TIMEOK) {
165
166     /* Calculate the integer differences in seconds and nanoseconds
167      * independently.  To avoid underflow, though, add a second's worth of
168      * nanoseconds which we'll subtract off later.
169      */
170     SUB64(delta_s, t1->t.ts.s, t0->t.ts.s);
171     delta_ns = t1->t.ts.ns + NS_PER_S - t0->t.ts.ns;
172
173     /* Hack if they're both equal. */
174     if (ZERO64(delta_s) && !delta_ns) delta_ns = 1;
175
176     /* And apply the nanoseconds difference.  To prevent underflow, pre-
177      * emptively borrow one from the integer difference.
178      */
179     delta_inout->t = FLOATK64(delta_s) - 1.0 + delta_ns/(double)NS_PER_S;
180
181     /* Done. */
182     delta_inout->f |= BTF_TIMEOK;
183   }
184 }
185
186 /* --- @diff_cycles@ --- *
187  *
188  * Arguments:   @struct timer *t@ = timer structure
189  *              @struct bench_timing *delta_inout@ = where to put the result
190  *              @const struct time *t0, *t1@ = two input times
191  *
192  * Returns:     ---
193  *
194  * Use:         Calculates a time difference for cycle-counting timers.
195  */
196
197 static void diff_cycles(struct timer *t, struct bench_timing *delta_inout,
198                         const struct bench_time *t0,
199                         const struct bench_time *t1)
200 {
201   unsigned f = t0->f&t1->f;
202   kludge64 delta_cy;
203
204   if (f&BTF_CYOK) {
205     SUB64(delta_cy, t1->cy, t0->cy); delta_inout->cy = FLOATK64(delta_cy);
206     if (!delta_inout->cy) delta_inout->cy = 1;
207     delta_inout->f |= BTF_CYOK;
208   }
209 }
210
211 #undef FLOATK64
212
213 /* --- @normalize@ --- *
214  *
215  * Arguments:   @double *x_inout@ = address of a value to normalize
216  *              @const char **unit_out@ = address to store unit prefix
217  *              @double scale@ = scale factor for unit steps
218  *
219  * Returns:     ---
220  *
221  * Use:         Adjust @*x_inout@ by a power of @scale@, and set @*unit_out@
222  *              so that printing the two reflects the original value with an
223  *              appropriate SI unit scaling.  The @scale@ should be 1024 for
224  *              binary quantities, most notably memory sizes, or 1000 for
225  *              other quantities.
226  */
227
228 static void normalize(double *x_inout, const char **unit_out, double scale)
229 {
230   static const char
231     *const nothing = "",
232     *const big[] = { "k", "M", "G", "T", "P", "E", 0 },
233     *const little[] = { "m", "ยต", "n", "p", "f", "a", 0 };
234   const char *const *u;
235   double x = *x_inout, s;
236
237   if (x >= 0) s = +1.0;
238   else { x = -x; s = -1.0; }
239
240   if (x && x < 1)
241     for (u = little, x *= scale; x < 1 && u[1]; u++, x *= scale);
242   else if (x >= scale)
243     for (u = big, x /= scale; x >= scale && u[1]; u++, x /= scale);
244   else
245     u = &nothing;
246
247   *x_inout = s*x; *unit_out = *u;
248 }
249
250 /*----- The null timer ----------------------------------------------------*/
251
252 /* This is a timer which does nothing, in case we don't have any better
253  * ideas.
254  */
255
256 static int null_init(struct timer *t) { return (0); }
257 static int null_now(struct timer *t, struct bench_time *t_out, unsigned f)
258   { return (0); }
259 static int null_preflight(struct timer *t) { return (0); }
260 static void null_diff(struct timer *t, struct bench_timing *delta_inout,
261                       const struct bench_time *t0,
262                       const struct bench_time *t1)
263   { ; }
264 static void null_teardown(struct timer *t) { ; }
265
266 static const struct timer_ops null_ops =
267   { "null", 0,
268     null_init, null_preflight, null_now, null_diff, null_teardown };
269 #define NULL_ENT &null_ops,
270
271 /*----- The broken clock --------------------------------------------------*/
272
273 /* This is a cycle counter which does nothing, in case we don't have any
274  * better ideas.
275  */
276
277 static int broken_init(struct timer *t) { return (-1); }
278
279 static const struct timer_ops broken_ops =
280   { "broken", TF_SECRET,
281     broken_init, null_preflight, null_now, null_diff, null_teardown };
282 #define BROKEN_ENT &broken_ops,
283
284 /*----- Linux performance counters ----------------------------------------*/
285
286 /* This is a cycle counter which uses the Linux performance event system,
287  * which is probably the best choice if it's available.
288  */
289
290 #if defined(HAVE_LINUX_PERF_EVENT_H) && defined(HAVE_UINT64)
291
292 /* --- @perfevent_open@ --- *
293  *
294  * Arguments:   ---
295  *
296  * Returns:     File descriptor, or %$-1$%.
297  *
298  * Use:         Open a performance measurement descriptor set up to count CPU
299  *              cycles.
300  */
301
302 static int perfevent_open(void)
303 {
304   struct perf_event_attr attr = { 0 };
305   int fd;
306
307   attr.type = PERF_TYPE_HARDWARE;
308   attr.size = sizeof(attr);
309   attr.config = PERF_COUNT_HW_CPU_CYCLES;
310   attr.disabled = 0;
311   attr.exclude_kernel = 1;
312   attr.exclude_hv = 1;
313
314   fd = syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0);
315   if (fd < 0) {
316     debug("couldn't open perf event: %s", strerror(errno));
317     return (-1);
318   }
319
320   return (fd);
321 }
322
323 static int perfevent_now(struct timer *t,
324                          struct bench_time *t_out, unsigned f)
325 {
326   ssize_t n;
327
328   n = read(t->u_cy.fd, &t_out->cy.i, sizeof(t_out->cy.i));
329     if (n != sizeof(t_out->cy.i)) {
330       debug("failed to read perf-event counter: %s", strerror(errno));
331       return (0);
332     }
333   t_out->f |= BTF_CYOK; return (0);
334 }
335
336 static void perfevent_teardown(struct timer *t)
337   { close(t->u_cy.fd); }
338
339 static int perfevent_init(struct timer *t)
340 {
341   int fd = -1, rc;
342
343   fd = perfevent_open(); if (fd < 0) { rc = -1; goto end; }
344   t->u_cy.fd = fd; fd = -1; rc = 0;
345 end:
346   if (fd != -1) close(fd);
347   return (rc);
348 }
349
350 static const struct timer_ops perfevent_ops =
351   { "linux-perf-read-hw-cycles", BTF_CYOK,
352     perfevent_init, null_preflight, perfevent_now,
353     diff_cycles, perfevent_teardown };
354 #define PERFEVENT_VANILLA_CYENT &perfevent_ops,
355
356 #  if GCC_VERSION_P(4, 5) && (defined(__i386__) || defined(__x86_64__))
357
358 /* Special syscall-free version for x86 using `rdpmc' instruction. *
359  *
360  * This is a bit weird because it does both kinds of measurement in a single
361  * operation.
362  */
363
364 static int perfevrdpmc_now(struct timer *t,
365                            struct bench_time *t_out, unsigned f)
366 {
367   const volatile struct perf_event_mmap_page *map = t->u_cy.pmc.map;
368   unsigned long long tsc = tsc, toff = toff, tenb = tenb;
369   unsigned long long cy = cy, cyoff = cyoff;
370   unsigned long long m, hi, lo;
371   unsigned tshift = tshift, tmult = tmult, q0, q1, ff;
372
373   /* Repeat until we can complete this job without the buffer changing in the
374    * middle.
375    */
376   q0 = map->lock;
377   __atomic_thread_fence(__ATOMIC_ACQ_REL);
378   for (;;) {
379     ff = 0;
380
381     /* Read the passage-of-time information. */
382     if (map->cap_user_time) {
383       tenb = map->time_enabled;
384       tsc = __builtin_ia32_rdtsc();
385       tshift = map->time_shift;
386       tmult = map->time_mult;
387       toff = map->time_offset;
388       ff |= BTF_TIMEOK;
389     }
390
391     /* Read the performance-counter information. */
392     if (map->cap_user_rdpmc) {
393       cy = __builtin_ia32_rdpmc(map->index - 1);
394       cyoff = map->offset;
395       ff |= BTF_CYOK;
396     }
397
398     /* Check the sequence number again. */
399     __atomic_thread_fence(__ATOMIC_ACQ_REL);
400     q1 = map->lock;
401     if (q0 == q1) break;
402     q0 = q1;
403   }
404
405   if (ff&BTF_TIMEOK) {
406     /* We have a raw reference-cycle count %$n$% (@tsc@), and parameters
407      * %$a$%, %$w$% and %$t_0$%, such that %$a n/2^w + t_0$% gives a time in
408      * nanoseconds.
409      */
410
411     m = (1ull << tshift) - 1;
412     hi = tsc >> tshift; lo = tsc&m;
413     t_out->t.rawns.i = hi*tmult + (lo*tmult >> tshift) + toff + tenb;
414     t_out->f |= BTF_TIMEOK;
415   }
416
417   if (ff&BTF_CYOK) {
418     /* We have the cycle count. */
419
420     t_out->cy.i = cy + cyoff;
421     t_out->f |= BTF_CYOK;
422   }
423   return (0);
424 }
425
426 static void perfevrdpmc_diff(struct timer *t,
427                              struct bench_timing *delta_inout,
428                              const struct bench_time *t0,
429                              const struct bench_time *t1)
430 {
431   unsigned long long delta_ns;
432   unsigned f = t0->f&t1->f;
433
434   if (f&BTF_TIMEOK) {
435     delta_ns = t1->t.rawns.i - t0->t.rawns.i; if (!delta_ns) delta_ns = 1;
436     delta_inout->t = delta_ns/(double)NS_PER_S;
437     delta_inout->f |= BTF_TIMEOK;
438   }
439
440   if (f&BTF_CYOK) {
441     delta_inout->cy = t1->cy.i - t0->cy.i;
442     if (!delta_inout->cy) delta_inout->cy = 1;
443     delta_inout->f |= BTF_CYOK;
444   }
445 }
446
447 static void perfevrdpmc_unmap
448   (const volatile struct perf_event_mmap_page *map, size_t mapsz)
449   { if (map) munmap(UNQUALIFY(struct perf_event_mmap_page, map), mapsz); }
450
451 static void perfevrdpmc_teardown(struct timer *t)
452   { perfevrdpmc_unmap(t->u_cy.pmc.map, t->u_cy.pmc.sz); }
453
454 static int perfevrdpmc_setup(struct timer *t)
455 {
456   const volatile struct perf_event_mmap_page *map = 0;
457   int pgsz, mapsz = 0, fd = -1, rc;
458
459   /* The rules say we must allocate %$1 + 2^n$% pages, so we need to know how
460    * big a page is.
461    */
462   pgsz = sysconf(_SC_PAGESIZE);
463     if (pgsz < 0) {
464       debug("failed to discover page size!: %s", strerror(errno));
465       rc = -1; goto end;
466     }
467
468   /* Open the measurement descriptor and map it. */
469   fd = perfevent_open(); if (!fd) return (-1);
470   mapsz = 2*pgsz;
471   map = mmap(0, mapsz, PROT_READ, MAP_SHARED, fd, 0);
472     if (map == MAP_FAILED) {
473       debug("failed to map perf event: %s", strerror(errno));
474       return (-1);
475     }
476
477   t->u_cy.pmc.map = map; t->u_cy.pmc.sz = mapsz; map = 0;
478   t->u_cy.pmc.owner = syscall(SYS_gettid); rc = 0;
479 end:
480   if (fd != -1) close(fd);
481   perfevrdpmc_unmap(map, mapsz);
482   return (rc);
483 }
484
485 static int perfevrdpmc_preflight(struct timer *t)
486 {
487   if (!t->u_cy.pmc.map) { debug("retry perf event map setup"); goto reopen; }
488   if (t->u_cy.pmc.owner != syscall(SYS_gettid)) {
489     debug("pid changed: reopen perf event map");
490     perfevrdpmc_unmap(t->u_cy.pmc.map, t->u_cy.pmc.sz);
491     t->u_cy.pmc.map = 0; goto reopen;
492   }
493   return (0);
494
495 reopen:
496   if (perfevrdpmc_setup(t)) return (-1);
497   return (0);
498 }
499
500 static int perfevrdpmc_cyinit(struct timer *t)
501 {
502   unsigned a, b, c, d;
503
504 #  ifdef HAVE_VALGRIND_VALGRIND_H
505   /* Valgrind doesn't like `rdpmc' instructions, so just bail. */
506   if (RUNNING_ON_VALGRIND) return (-1);
507 #  endif
508
509   /* We need `rdtsc' to do the passage-of-time measurement. */
510   if (!__get_cpuid(1, &a, &b, &c, &d) || !(d&CPUID_1D_TSC))
511     { debug("no `rdtsc' instrunction"); return (-1); }
512
513   /* Set things up. */
514   if (perfevrdpmc_setup(t)) return (-1);
515   return (0);
516 }
517
518 static const struct timer_ops perfevrdpmc_cyops =
519   { "linux-x86-perf-rdpmc-hw-cycles", BTF_TIMEOK | BTF_CYOK,
520     perfevrdpmc_cyinit, perfevrdpmc_preflight, perfevrdpmc_now,
521     perfevrdpmc_diff, perfevrdpmc_teardown };
522
523 static int perfevrdpmc_clkinit(struct timer *t)
524 {
525   if (t->ops[CY] != &perfevrdpmc_cyops) {
526     debug("`linux-x86-perf-rdpmc-hw-cycles' not set as cycle subtimer");
527     return(-1);
528   }
529   return (0);
530 }
531
532 static const struct timer_ops perfevrdpmc_clkops =
533   { "linux-x86-perf-rdpmc-hw-cycles", 0,
534     perfevrdpmc_clkinit, null_preflight, null_now,
535     null_diff, null_teardown };
536
537 #    define PERFEVENT_RDPMC_CLKENT &perfevrdpmc_clkops,
538 #    define PERFEVENT_RDPMC_CYENT &perfevrdpmc_cyops,
539
540 #  else
541 #    define PERFEVENT_RDPMC_CLKENT
542 #    define PERFEVENT_RDPMC_CYENT
543 #  endif
544
545 #  define PERFEVENT_CLKENT PERFEVENT_RDPMC_CLKENT
546 #  define PERFEVENT_CYENT PERFEVENT_RDPMC_CYENT PERFEVENT_VANILLA_CYENT
547 #else
548 #  define PERFEVENT_CLKENT
549 #  define PERFEVENT_CYENT
550 #endif
551
552 /*----- Intel time-stamp counter ------------------------------------------*/
553
554 /* This is a cycle counter based on the Intel `rdtsc' instruction.  It's not
555  * really suitable for performance measurement because it gets confused by
556  * CPU frequency adjustments.
557  */
558
559 #if GCC_VERSION_P(4, 5) && (defined(__i386__) || defined(__x86_64__))
560
561 static int x86rdtsc_now(struct timer *t,
562                         struct bench_time *t_out, unsigned f)
563   { t_out->cy.i = __builtin_ia32_rdtsc(); t_out->f |= BTF_CYOK; return (0); }
564
565 static int x86rdtsc_init(struct timer *t)
566 {
567   unsigned a, b, c, d;
568
569   if (!__get_cpuid(1, &a, &b, &c, &d) || !(d&CPUID_1D_TSC))
570     { debug("no `rdtsc' instrunction"); return (-1); }
571   t->u_cy.tscaux = ~0u;
572   return (0);
573 }
574
575 static int x86rdtscp_now(struct timer *t,
576                          struct bench_time *t_out, unsigned f)
577 {
578   unsigned tscaux;
579   unsigned long long n;
580
581   n = __builtin_ia32_rdtscp(&tscaux);
582   if (!(f&BTF_T1))
583     t->u_cy.tscaux = tscaux;
584   else if (t->u_cy.tscaux != tscaux) {
585     debug("tscaux mismatch: new 0x%08x /= old 0x%08x",
586           tscaux, t->u_cy.tscaux);
587     return (-1);
588   }
589   t_out->cy.i = n; t_out->f |= BTF_CYOK; return (0);
590 }
591
592 static int x86rdtscp_init(struct timer *t)
593 {
594   unsigned a, b, c, d;
595
596   if (!__get_cpuid(0x80000001, &a, &b, &c, &d) || !(d&CPUID_1xD_TSCP))
597     { debug("no `rdtscp' instrunction"); return (-1); }
598   return (0);
599 }
600
601 static const struct timer_ops x86rdtsc_ops =
602   { "x86-rdtsc", BTF_CYOK,
603     x86rdtsc_init, null_preflight, x86rdtsc_now,
604     diff_cycles, null_teardown };
605 static const struct timer_ops x86rdtscp_ops =
606   { "x86-rdtscp", BTF_CYOK,
607     x86rdtscp_init, null_preflight,
608     x86rdtscp_now, diff_cycles, null_teardown };
609
610 #  define X86RDTSC_CYENT &x86rdtscp_ops, &x86rdtsc_ops,
611 #else
612 #  define X86RDTSC_CYENT
613 #endif
614
615 /*----- POSIX `clock_gettime' ---------------------------------------------*/
616
617 /* This is a real-time clock based on the POSIX time interface, with up to
618  * nanosecond precision.
619  */
620
621 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_THREAD_CPUTIME_ID)
622
623 static int gettime_now(struct timer *t, struct bench_time *t_out, unsigned f)
624 {
625   struct timespec now;
626
627   if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now))
628     { debug("error reading POSIX clock: %s", strerror(errno)); return (0); }
629   ASSIGN64(t_out->t.ts.s, now.tv_sec); t_out->t.ts.ns = now.tv_nsec;
630   t_out->f |= BTF_TIMEOK; return (0);
631 }
632
633 static const struct timer_ops gettime_ops =
634   { "posix-thread-cputime", BTF_TIMEOK,
635     null_init, null_preflight, gettime_now, diff_ts, null_teardown };
636
637 #  define GETTIME_CLKENT &gettime_ops,
638 #else
639 #  define GETTIME_CLKENT
640 #endif
641
642 /*----- Standard C `clock' ------------------------------------------------*/
643
644 /* This is a real-time clock based on the C `clock' function which is
645  * guaranteed to be available, though it's not likely to be very good.
646  */
647
648 static int clock_now(struct timer *t, struct bench_time *t_out, unsigned f)
649 {
650   clock_t now;
651
652   now = clock();
653     if (now == (clock_t)-1) {
654       debug("error reading standard clock: %s", strerror(errno));
655       return (0);
656     }
657   t_out->t.clk = now; t_out->f |= BTF_TIMEOK; return (0);
658 }
659
660 static void clock_diff(struct timer *t, struct bench_timing *delta_inout,
661                         const struct bench_time *t0,
662                         const struct bench_time *t1)
663 {
664   clock_t delta_clk;
665   unsigned f = t0->f&t1->f;
666
667   if (f&BTF_TIMEOK) {
668     delta_clk = t1->t.clk - t0->t.clk; if (!delta_clk) delta_clk = 1;
669     delta_inout->t = delta_clk/(double)CLOCKS_PER_SEC;
670     delta_inout->f |= BTF_TIMEOK;
671   }
672 }
673
674 static const struct timer_ops clock_ops =
675   { "stdc-clock", BTF_TIMEOK, null_init, null_preflight, clock_now,
676     clock_diff, null_teardown };
677
678 #define CLOCK_CLKENT &clock_ops,
679
680 /*----- Timing setup ------------------------------------------------------*/
681
682 /* Tables of timing sources. */
683 static const struct timer_ops
684   *const clktab[] = { PERFEVENT_CLKENT
685                       GETTIME_CLKENT
686                       CLOCK_CLKENT
687                       BROKEN_ENT
688                       0 },
689   *const cytab[] = { PERFEVENT_CYENT
690                      X86RDTSC_CYENT
691                      NULL_ENT
692                      BROKEN_ENT
693                      0 };
694
695 static const struct timertab {
696   const char *what;
697   const char *env;
698   const struct timer_ops *const *opstab;
699 } timertab[] = {
700   { "clock",    "MLIB_BENCH_CLKTIMER",          clktab },
701   { "cycle",    "MLIB_BENCH_CYCLETIMER",        cytab }
702 };
703
704 /* --- @find_timer@ --- *
705  *
706  * Arguments:   @const char *name@ = timer name
707  *              @size_t sz@ = length of name
708  *              @unsigned tm@ = which subtimer we're looking for
709  *
710  * Returns:     The table entry matching the given name, or null if there
711  *              isn't one.
712  */
713
714 static const struct timer_ops *find_timer(const char *name, size_t sz,
715                                           unsigned tm)
716 {
717   const struct timer_ops *const *tt;
718
719   for (tt = timertab[tm].opstab; *tt; tt++) {
720     if (strlen((*tt)->name) == sz &&
721         MEMCMP(name, ==, (*tt)->name, sz))
722       return (*tt);
723   }
724   debug("%s timer `%.*s' not found",
725         timertab[tm].what, (int)sz, name); return (0);
726 }
727
728 /* --- @try_timer@ --- *
729  *
730  * Arguments:   @struct timer *t@ = timer structure
731  *              @const struct timer_ops *ops@ = timer ops
732  *              @unsigned tm@ = which subtimer we're setting
733  *
734  * Returns:     Zero on success, %$-1$% if timer failed.
735  *
736  * Use:         Tries to initialize the timer @t@, reporting a debug message
737  *              if it worked.
738  */
739
740 static int try_timer(struct timer *t,
741                      const struct timer_ops *ops, unsigned tm)
742 {
743   struct bench_time t0, t1;
744   struct bench_timing delta;
745   int rc;
746   unsigned f = 0;
747 #define f_teardown 1u
748
749   if (ops->init(t)) { rc = -1; goto end; }
750   f |= f_teardown;
751
752   if (ops->preflight(t)) { rc = -1; goto end; }
753   t0.f = t1.f = 0;
754   do {
755     while (ops->now(t, &t0, BTF_T0));
756   } while (ops->now(t, &t1, BTF_T1));
757   delta.f = 0; ops->diff(t, &delta, &t0, &t1);
758   if ((ops->f ^ delta.f)&BTF_ANY) { rc = -1; goto end; }
759
760   debug("selected %s timer `%s'", timertab[tm].what, ops->name);
761   t->ops[tm] = ops; f &= ~f_teardown; rc = 0;
762
763 end:
764   if (f&f_teardown) ops->teardown(t);
765   return (rc);
766
767 #undef f_teardown
768 }
769
770 /* --- @select_timer@ --- *
771  *
772  * Arguments:   @struct timer *t@ = timer structure
773  *              @unsigned tm@ = which subtimer we're setting
774  *              @const char *config@, @size_t sz@ = config string
775  *
776  * Returns:     Zero on success, %$-1$% if timer failed.
777  *
778  * Use:         Select a timer from the table.  If the environment variable
779  *              is set, then parse a comma-separated list of timer names and
780  *              use the first one listed that seems to work; otherwise, try
781  *              the timers in the table in order.
782  */
783
784 static int select_timer(struct timer *t, unsigned tm,
785                         const char *config, size_t sz)
786 {
787   const char *p, *l;
788   const struct timer_ops *ops, *const *tt;
789
790   if (!config) {
791     for (tt = timertab[tm].opstab; *tt; tt++)
792       if (!((*tt)->f&TF_SECRET) && !try_timer(t, *tt, tm)) return (0);
793   } else {
794     l = config + sz;
795     for (;;) {
796       p = memchr(config, ',', l - config); if (!p) p = l;
797       ops = find_timer(config, p - config, tm);
798       if (ops && !try_timer(t, ops, tm)) return (0);
799       if (p >= l) break;
800       config = p + 1;
801     }
802   }
803   debug("no suitable %s timer found", timertab[tm].what); return (-1);
804 }
805
806 /* Bench timer operations. */
807 static void timer_describe(struct bench_timer *tm, dstr *d)
808 {
809   struct timer *t = (struct timer *)tm;
810   unsigned i;
811
812   dstr_puts(d, "builtin: ");
813   for (i = 0; i < NTIMER; i++) {
814     if (i) dstr_puts(d, ", ");
815     dstr_putf(d, "%s = %s", timertab[i].what, t->ops[i]->name);
816   }
817 }
818
819 static int timer_preflight(struct bench_timer *tm)
820 {
821   struct timer *t = (struct timer *)tm;
822   unsigned i;
823
824   for (i = 0; i < NTIMER; i++) if (t->ops[i]->preflight(t)) return (-1);
825   return (0);
826 }
827
828 static int timer_now(struct bench_timer *tm,
829                      struct bench_time *t_out, unsigned f)
830 {
831   struct timer *t = (struct timer *)tm;
832   unsigned i;
833
834   t_out->f = 0;
835   for (i = 0; i < NTIMER; i++) if (t->ops[i]->now(t, t_out, f)) return (-1);
836   return (0);
837 }
838
839 static void timer_diff(struct bench_timer *tm,
840                        struct bench_timing *t_out,
841                        const struct bench_time *t0,
842                        const struct bench_time *t1)
843 {
844   struct timer *t = (struct timer *)tm;
845   unsigned i;
846
847   t_out->f = 0;
848   for (i = 0; i < NTIMER; i++) t->ops[i]->diff(t, t_out, t0, t1);
849 }
850
851 static void timer_destroy(struct bench_timer *tm)
852 {
853   struct timer *t = (struct timer *)tm;
854   unsigned i;
855
856   if (!t) return;
857   for (i = 0; i < NTIMER; i++)
858     if (t->ops[i]) t->ops[i]->teardown(t);
859   x_free(t->a, t);
860 }
861
862 static const struct bench_timerops timer_ops =
863   { timer_describe, timer_preflight, timer_now, timer_diff, timer_destroy };
864
865 /* --- @bench_createtimer@ --- *
866  *
867  * Arguments:   @const char *config@ = timer configuration string
868  *
869  * Returns:     A freshly constructed standard timer object.
870  *
871  * Use:         Allocate a timer.  Dispose of it by calling
872  *              @tm->ops->destroy(tm)@ when you're done.
873  *
874  *              Applications should not set configuration strings except as
875  *              established by user action, e.g., from a command-line option,
876  *              environment variable, or configuration file.
877  */
878
879 struct bench_timer *bench_createtimer(const char *config)
880 {
881   struct timer *t = 0;
882   struct bench_timer *ret = 0;
883   struct { const char *p; size_t sz; } tmconf[NTIMER] = { 0 };
884   const struct timer_ops *const *tt;
885   const char *p, *l; size_t n, nn;
886   unsigned i;
887
888   /* Parse the configuration string. */
889   if (config) {
890
891     /* The first thing to do is find the end of the string. */
892     l = config + strlen(config);
893
894     for (;;) {
895       /* Process the whitespace-sparated words of the string one by one. */
896
897       /* Skip over any initial whitespace.  If we hit the end of the string
898        * then we're done.
899        */
900       for (;;)
901         if (config >= l) goto done_config;
902         else if (!ISSPACE(*config)) break;
903         else config++;
904
905       /* There's definitely a word here.  Find the end of it. */
906       for (p = config; p < l && !ISSPACE(*p); p++);
907       nn = p - config;
908
909       /* Try various simple keywords. */
910 #define MATCHP(lit) (nn == sizeof(lit) - 1 && MEMCMP(config, ==, lit, nn))
911
912       if (MATCHP("list")) {
913         /* The `list' keyword requests lists of the available timer
914          * implementations.
915          */
916
917         for (i = 0; i < NTIMER; i++) {
918           printf("%s timers:", timertab[i].what);
919           for (tt = timertab[i].opstab; *tt; tt++)
920             if (!((*tt)->f&TF_SECRET)) printf(" %s", (*tt)->name);
921           putchar('\n');
922         }
923         goto next_config;
924       }
925
926 #undef MATCHP
927
928       /* Otherwise it's an assignment, setting a subtimer list. */
929       p = memchr(config, '=', nn);
930       if (!p)
931         n = nn;
932       else {
933         n = p - config;
934         for (i = 0; i < NTIMER; i++)
935           if (STRNCMP(config, ==, timertab[i].what, n) &&
936               !timertab[i].what[n]) {
937             if (tmconf[i].p)
938               debug("duplicate %s timer list", timertab[i].what);
939             tmconf[i].p = config + n + 1; tmconf[i].sz = nn - n - 1;
940             goto next_config;
941           }
942       }
943       debug("unrecognized config keyword `%.*s'", (int)n, config);
944
945       /* Move on to the next word. */
946     next_config:
947       config += nn;
948     }
949   done_config:;
950   }
951
952   /* Override these settings from the environment. */
953   for (i = 0; i < NTIMER; i++) {
954     p = getenv(timertab[i].env);
955     if (p) { tmconf[i].p = p; tmconf[i].sz = strlen(p); }
956   }
957
958   /* All seems well.  Allocate the timer object. */
959   XNEW(t); t->a = arena_global;
960   for (i = 0; i < NTIMER; i++) t->ops[i] = 0;
961
962   /* Try to set up the subtimers. */
963   for (i = NTIMER; i--; )
964     if (select_timer(t, i, tmconf[i].p, tmconf[i].sz)) goto end;
965
966   /* All is done. */
967   t->_t.ops = &timer_ops; t->_t.ref = 1; ret = &t->_t; t = 0;
968 end:
969   if (t) timer_destroy(&t->_t);
970   return (ret);
971 }
972
973 /*----- Benchmarking ------------------------------------------------------*/
974
975 /* --- @bench_init@ --- *
976  *
977  * Arguments:   @struct bench_state *b@ = bench state to initialize
978  *              @struct bench_timer *tm@ = timer to attach, or null
979  *
980  * Returns:     Zero on success, %$-1$% on failure.
981  *
982  * Use:         Initialize the benchmark state.  On success, the timer state
983  *              still needs to be calibrated (use @bench_calibrate@) before
984  *              it can be used, but this will be done automatically by
985  *              @bench_measure@ if it's not done by hand earlier.  The timer
986  *              is now owned by the benchmark state and will be destroyed by
987  *              @bench_destroy@.
988  *
989  *              The only reason for failure is if @tm@ was null on entry,
990  *              and automatic construction of a timer failed.  The state is
991  *              safe to discard, but calling @bench_destroy@ is safe too.
992  */
993
994 int bench_init(struct bench_state *b, struct bench_timer *tm)
995 {
996   int rc;
997
998   b->tm = 0;
999
1000   if (!tm) {
1001     tm = bench_createtimer(0);
1002     if (!tm) { rc = -1; goto end; }
1003   }
1004
1005   b->tm = tm; b->target_s = 1.0; b->f = 0; rc = 0;
1006 end:
1007   return (rc);
1008 }
1009
1010 /* --- @bench_destroy@ --- *
1011  *
1012  * Arguments:   @struct bench_state *b@ = bench state
1013  *
1014  * Returns:     ---
1015  *
1016  * Use:         Destroy the benchmark state, releasing the resources that it
1017  *              holds.
1018  */
1019
1020 void bench_destroy(struct bench_state *b)
1021   { if (b->tm && !--b->tm->ref) { b->tm->ops->destroy(b->tm); b->tm = 0; } }
1022
1023 /* --- @spin@ --- *
1024  *
1025  * Arguments:   @unsigned long n@ = iteration count
1026  *              @void *ctx@ = context pointer (ignored)
1027  *
1028  * Returns:     ---
1029  *
1030  * Use:         Does nothing at all for @n@ iterations.  Used to calibrate
1031  *              the benchmarking state.
1032  */
1033
1034 static void spin(unsigned long n, void *ctx)
1035   { while (n--) RELAX; }
1036
1037 /* --- @bench_calibrate@ --- *
1038  *
1039  * Arguments:   @struct bench_state *b@ = bench state
1040  *              @unsigned f@ = calibration flags
1041  *
1042  * Returns:     Zero on success, %$-1$% if calibration failed.
1043  *
1044  * Use:         Calibrate the benchmark state, so that it can be used to
1045  *              measure performance reasonably accurately.
1046  *
1047  *              Calibration will take into account how the subject code is
1048  *              going to be located.  If you're going to use @BENCH_MEASURE@
1049  *              to measure a piece of literal code, then leave @f@ zero.  If
1050  *              the code to be measured is going to be executed via an
1051  *              indirect branch, e.g., through the @measure@ function, then
1052  *              set @BTF_INDIRECT@.
1053  */
1054
1055 #define T_CLB 0.0625                    /* calibration time limit */
1056
1057 int bench_calibrate(struct bench_state *b, unsigned f)
1058 {
1059   struct linreg lr_clk = LINREG_INIT, lr_cy = LINREG_INIT;
1060   struct bench_timer *tm = b->tm;
1061   struct bench_timing delta;
1062   double n, r;
1063   unsigned i, tf = BTF_ANY;
1064   BENCH_TIMELOOP_DECLS;
1065   int rc;
1066
1067   /* The model here is that a timing loop has a fixed overhead as we enter
1068    * and leave (e.g., to do with the indirect branch into the code), and
1069    * per-iteration overheads as we check the counter and loop back.  We aim
1070    * to split these apart using linear regression.
1071    */
1072
1073   /* If we've already calibrated then there's nothing to do. */
1074   if (b->f&BTF_CLB) return (b->f&BTF_ANY ? 0 : -1);
1075
1076   /* Run the timer preflight check. */
1077   if (tm->ops->preflight(tm)) { rc = -1; goto end; }
1078
1079   /* Exercise the inner loop a few times to educate the branch predictor.
1080    * This is only useful if we're executing via an indirect call.
1081    */
1082   if (f&BTF_INDIRECT) {
1083     for (i = 0; i < 50; i++)
1084       BENCH_TIMELOOP_TAG(setup, b->tm, &delta, 10000, ;)
1085         LAUNDER(&spin)(_bench_n, 0);
1086   }
1087
1088   /* Now we measure idle loops until they take sufficiently long -- or we run
1089    * out of counter.
1090    */
1091   debug("calibrating...");
1092   n = 1.0;
1093   for (;;) {
1094
1095     /* Measure @n@ iterations of the idle loop. */
1096     if (f&BTF_INDIRECT)
1097       BENCH_TIMELOOP_TAG(calibrate, b->tm, &delta, n, ;)
1098         LAUNDER(&spin)(_bench_n, 0);
1099     else
1100       BENCH_TIMELOOP_TAG(calibrate, b->tm, &delta, n, ;)
1101         while (_bench_n--) RELAX;
1102     tf &= delta.f; if (!(tf&BTF_TIMEOK)) { rc = -1; goto end; }
1103
1104     /* Register the timings with the regression machinery. */
1105     linreg_update(&lr_clk, n, delta.t);
1106     if (!(tf&BTF_CYOK))
1107       debug("  n = %10.0f; t = %12g s", n, delta.t);
1108     else {
1109       linreg_update(&lr_cy, n, delta.cy);
1110       debug("  n = %10.0f; t = %12g s, cy = %10.0f", n, delta.t, delta.cy);
1111     }
1112
1113     /* If we're done then stop. */
1114     if (delta.t >= T_CLB) break;
1115     if (n >= ULONG_MAX - n/3) break;
1116
1117     /* Update the counter and continue. */
1118     n += n/3.0 + 1.0;
1119   }
1120
1121   /* Now run the linear regression to extract the constant and per-iteration
1122    * overheads.
1123    */
1124   linreg_fit(&lr_clk, &b->clk.m, &b->clk.c, &r);
1125   debug("clock overhead = (%g n + %g) s (r = %g)", b->clk.m, b->clk.c, r);
1126   if (tf&BTF_CYOK) {
1127     linreg_fit(&lr_cy, &b->cy.m, &b->cy.c, &r);
1128     debug("cycle overhead = (%g n + %g) cy (r = %g)", b->cy.m, b->cy.c, r);
1129   }
1130
1131   /* We're done. */
1132   rc = 0;
1133 end:
1134   b->f |= tf | BTF_CLB;                 /* no point trying again */
1135   return (rc);
1136 }
1137
1138 /* --- @bench_preflight@ --- *
1139  *
1140  * Arguments:   @struct bench_state *b@ = benchmark state
1141  *
1142  * Returns:     Zero on success, %$-1$% on failure.
1143  *
1144  * Use:         Prepares for benchmarking on the current thread.  Current
1145  *              checks are that the timer is calibrated and that it can
1146  *              successfully measure time; the timer preflight is also run.
1147  *
1148  *              Users are unlikely to find this function useful: it's called
1149  *              automatically by the @BENCH_MEASURE@ macro and the
1150  *              @bench_measure@ function.
1151  */
1152
1153 int bench_preflight(struct bench_state *b)
1154 {
1155   struct bench_timer *tm = b->tm;
1156
1157   if (!(b->f&BTF_CLB)) return (-1);
1158   if (!(b->f&BTF_TIMEOK)) return (-1);
1159   if (tm->ops->preflight(tm)) return (-1);
1160   debug("measuring...");
1161   return (0);
1162 }
1163
1164 /* --- @bench_adapt@ --- *
1165  *
1166  * Arguments:   @double *n_inout@ = number of iterations, updated
1167  *              @double target_s@ = target time in seconds
1168  *              @const struct bench_timing *t@ = timing from the previous run
1169  *
1170  * Returns:     Nonzero if the measurement is sufficient; zero to run again.
1171  *
1172  * Use:         This function determines a suitable number of iterations of a
1173  *              benchmark function to perform next.  It is used in a loop
1174  *              such as the following.
1175  *
1176  *                      @double n = 1.0;@
1177  *                      @struct bench_timing t;@
1178  *
1179  *                      @do {@
1180  *                        (run @n@ iterations; set @t@ to the timing)
1181  *                      @} while (!bench_adapt(b, &n, &t));@
1182  *
1183  *              On entry, @*n_inout@ should be the number of iterations
1184  *              performed by the previous pass, and @*t@ the resulting time;
1185  *              the @BTF_TIMEOK@ flag must be set @t->f@.  If the timing is
1186  *              sufficient -- @t->t@ is sufficiently close to @target_s@
1187  *              -- then the function returns nonzero to indicate that
1188  *              measurement is complete.  Otherwise, it sets @*n_inout@ to a
1189  *              new, larger iteration count and returns zero to indicate that
1190  *              a further pass is necessary.
1191  */
1192
1193 int bench_adapt(double *n_inout, double target_s,
1194                 const struct bench_timing *t)
1195 {
1196   double n = *n_inout, nn;
1197
1198   /* Dump the results for debugging. */
1199   if (!(t->f&BTF_CYOK)) debug("  n = %10.0f; t = %12g", n, t->t);
1200   else debug("  n = %10.0f; t = %12g, cy = %10.0f", n, t->t, t->cy);
1201
1202   /* Suppose the timer loop %$n$% iterations in %$t$% seconds.  Our ideal
1203    * time is %$T$% seconds.  If %$t \ge T/\sqrt{2}$%, we're happy.
1204    * Otherwise, we need to scale up the iteration count.  The obvious next
1205    * choice is %$n' = n T/t$%.  Alas, rounding is a problem: if
1206    * %$T/t < 1 + 1/n$% then %$\floor{n T/t} = n$% and we will make no
1207    * progress.  We know that %$T/t > \sqrt{2}%, so this can only happen when
1208    * %$1 + 1/n > \sqrt{2}$%, i.e., when %$n < \sqrt{2} + 1$%.  On the other
1209    * hand, if %$T/t < 1 + 1/n$% then %$t (n + 1)/n > T$%, so just trying
1210    * again with %$n' = n + 1$% iterations will very likely work.
1211    */
1212   if (t->t >= 0.707*target_s) return (1);
1213   nn = n*target_s/t->t; modf(nn, &nn);
1214   *n_inout = nn > n ? nn : n + 1;
1215   return (0);
1216 }
1217
1218 /* --- @bench_adjust@ --- *
1219  *
1220  * Arguments:   @struct bench_state *b@ = benchmark state
1221  *              @struct bench_timing *t_inout@ = timing to adjust
1222  *              @double n@ = number of external iterations performed
1223  *              @double base@ = number of internal operations per external
1224  *                      iteration
1225  *
1226  * Returns:     ---
1227  *
1228  * Use:         Adjusts a raw timing, as captured by @BENCH_TIMELOOP@,
1229  *              according to the calibration data captured in @b@.
1230  *              On exit, the timing data is updated, and @t->n@ is set to the
1231  *              product @n*base@.
1232  */
1233
1234 void bench_adjust(struct bench_state *b,
1235                   struct bench_timing *t_inout, double n, double base)
1236 {
1237
1238   /* Adjust according to the calibration. */
1239   t_inout->t -= n*b->clk.m + b->clk.c;
1240   if (t_inout->f&BTF_CYOK) t_inout->cy -= n*b->cy.m + b->cy.c;
1241
1242   /* Report the results, if debugging. */
1243   if (!(t_inout->f&BTF_CYOK)) debug("  adjusted t' = %12g", t_inout->t);
1244   else debug("  adjusted t' = %12g, cy' = %10.0f", t_inout->t, t_inout->cy);
1245   if (!(t_inout->f&BTF_CYOK))
1246     debug("  %g s per iter; %g iters/s", t_inout->t/n, n/t_inout->t);
1247   else
1248     debug("  %g s (%g cy) per iter; %g iters/s",
1249           t_inout->t/n, t_inout->cy/n, n/t_inout->t);
1250
1251   /* All done. */
1252   t_inout->n = n*base;
1253 }
1254
1255 /* --- @bench_measure@ --- *
1256  *
1257  * Arguments:   @struct bench_state *b@ = benchmark state
1258  *              @struct bench_timing *t_out@ = where to leave the timing
1259  *              @double base@ = number of internal units per call
1260  *              @bench_fn *fn@, @void *ctx@ = benchmark function to run
1261  *
1262  * Returns:     Zero on success, %$-1$% if timing failed.
1263  *
1264  * Use:         Measure a function.  The function @fn@ is called adaptively
1265  *              with an iteration count @n@ set so as to run for
1266  *              approximately @b->target_s@ seconds.
1267  *
1268  *              The result is left in @*t_out@, with @t_out->n@ counting the
1269  *              final product of the iteration count and @base@ (which might,
1270  *              e.g., reflect the number of inner iterations the function
1271  *              performs, or the number of bytes it processes per iteration).
1272  *
1273  *              To get useful results, the benchmark state should have been
1274  *              calibrated for indirect calling -- i.e., with @BTF_INDIRECT@.
1275  */
1276
1277 int bench_measure(struct bench_state *b, struct bench_timing *t_out,
1278                   double base, bench_fn *fn, void *ctx)
1279 {
1280   BENCH_MEASURE_DECLS;
1281   int rc;
1282
1283   BENCH_MEASURE(b, rc, t_out, base) fn(_bench_n, ctx);
1284   return (rc);
1285 }
1286
1287 /*----- Reporting ---------------------------------------------------------*/
1288
1289 /* --- @bench_report@ --- *
1290  *
1291  * Arguments:   @const struct gprintf_ops *gops, void *gp@ = output formatter
1292  *              @unsigned unit@ = unit processed by the benchmark function
1293  *              @const struct bench_timing *t@ = benchmark result
1294  *
1295  * Returns:     ---
1296  *
1297  * Use:         Format, to the output identified by @gops@ and @go@, a
1298  *              human-readable report of the benchmarking result @t@.  No
1299  *              newline is appended.
1300  *
1301  *              The output format is subject to change in later versions.
1302  */
1303
1304 void bench_report(const struct gprintf_ops *gops, void *go,
1305                   unsigned unit, const struct bench_timing *t)
1306 {
1307   double scale, x, n = t->n;
1308   const char *u, *what, *whats;
1309
1310   assert(t->f&BTF_TIMEOK);
1311
1312   switch (unit) {
1313     case BTU_OP:
1314       gprintf(gops, go, "%.0f iterations ", n);
1315       what = "op"; whats = "ops"; scale = 1000;
1316       break;
1317     case BTU_BYTE:
1318       x = n; normalize(&x, &u, 1024); gprintf(gops, go, "%.3f %sB ", x, u);
1319       what = whats = "B"; scale = 1024;
1320       break;
1321     default:
1322       assert(0);
1323   }
1324
1325   x = t->t; normalize(&x, &u, 1000);
1326   gprintf(gops, go, "in %.3f %ss", x, u);
1327   if (t->f&BTF_CYOK) {
1328     x = t->cy; normalize(&x, &u, 1000);
1329     gprintf(gops, go, " (%.3f %scy)", x, u);
1330   }
1331   gprintf(gops, go, ": ");
1332
1333   x = n/t->t; normalize(&x, &u, scale);
1334     gprintf(gops, go, "%.3f %s%s/s", x, u, whats);
1335   x = t->t/n; normalize(&x, &u, 1000);
1336     gprintf(gops, go, ", %.3f %ss/%s", x, u, what);
1337   if (t->f&BTF_CYOK) {
1338     x = t->cy/n; normalize(&x, &u, 1000);
1339       gprintf(gops, go, " (%.3f %scy/%s)", x, u, what);
1340   }
1341 }
1342
1343 /*----- That's all, folks -------------------------------------------------*/