Merge branches 'mdw/latin-ietf' and 'mdw/curve25519'
[catacomb] / progs / perftest.c
CommitLineData
e2edda68 1/* -*-c-*-
2 *
e2edda68 3 * Measure performance of various operations (Unix-specific)
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
e2edda68 9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
45c0fd36 16 *
e2edda68 17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
45c0fd36 21 *
e2edda68 22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28/*----- Header files ------------------------------------------------------*/
29
cd6eca43
MW
30#define _FILE_OFFSET_BITS 64
31
e2edda68 32#include "config.h"
33
34#include <errno.h>
35#include <limits.h>
36#include <math.h>
37#include <stdio.h>
38#include <string.h>
39#include <stdlib.h>
40#include <time.h>
41
42#include <sys/types.h>
43#include <sys/time.h>
44#include <unistd.h>
45
46#include <mLib/alloc.h>
47#include <mLib/dstr.h>
48#include <mLib/mdwopt.h>
49#include <mLib/quis.h>
50#include <mLib/report.h>
51#include <mLib/sub.h>
52#include <mLib/tv.h>
53
54#include "rand.h"
55#include "mp.h"
56#include "mprand.h"
57#include "fibrand.h"
58#include "rsa.h"
59#include "mpmont.h"
60#include "mpbarrett.h"
61#include "dh.h"
62#include "pgen.h"
63#include "ec.h"
64#include "group.h"
fc2d44af 65#include "x25519.h"
643eb1bb 66#include "x448.h"
d56fd9d1 67#include "ed25519.h"
e2edda68 68
c65df279 69#include "cc.h"
e2edda68 70#include "gcipher.h"
71#include "ghash.h"
72#include "gmac.h"
ef7cf21d 73#include "poly1305.h"
19e6e199 74
c65df279 75#include "ectab.h"
76#include "ptab.h"
e2edda68 77
78/*----- Options -----------------------------------------------------------*/
79
80typedef struct opts {
81 const char *name; /* Pre-configured named thing */
82 unsigned fbits; /* Field size bits */
83 unsigned gbits; /* Group size bits */
84 unsigned n; /* Number of factors */
85 unsigned i; /* Number of intervals (or zero) */
86 double t; /* Time for each interval (secs) */
e74e12bc
MW
87 unsigned f; /* Flags */
88#define OF_NOCHECK 1u /* Don't do group checking */
e2edda68 89} opts;
90
91/*----- Job switch --------------------------------------------------------*/
92
93/* --- Barrett exponentiation --- */
94
95typedef struct bar_ctx {
96 size_t n;
97 mpbarrett b;
98 mp_expfactor *e;
99} bar_ctx;
100
101static void *bar_init(opts *o)
102{
103 bar_ctx *c = CREATE(bar_ctx);
104 gprime_param gp;
105 qd_parse qd;
106 size_t i;
107
108 if (o->name) {
109 qd.p = o->name;
110 if (dh_parse(&qd, &gp))
111 die(1, "bad prime group: %s", qd.e);
112 } else {
113 if (!o->fbits) o->fbits = 1024;
114 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
115 }
116 mpbarrett_create(&c->b, gp.p);
117 if (!o->n) o->n = 1;
118 c->n = o->n;
119 c->e = xmalloc(c->n * sizeof(group_expfactor));
120 for (i = 0; i < c->n; i++) {
121 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
122 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
123 }
124 dh_paramfree(&gp);
125 return (c);
126}
127
128static void bar_run(void *cc)
129{
130 bar_ctx *c = cc;
131 mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
132 MP_DROP(d);
133}
134
135static void barsim_run(void *cc)
136{
137 bar_ctx *c = cc;
138 mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
139 MP_DROP(d);
140}
141
142/* --- Montgomery exponentiation --- */
143
144typedef struct mont_ctx {
145 size_t n;
146 mpmont m;
147 mp_expfactor *e;
148} mont_ctx;
149
150static void *mont_init(opts *o)
151{
152 mont_ctx *c = CREATE(mont_ctx);
153 gprime_param gp;
154 qd_parse qd;
155 size_t i;
156
157 if (o->name) {
158 qd.p = o->name;
159 if (dh_parse(&qd, &gp))
160 die(1, "bad prime group: %s", qd.e);
161 } else {
162 if (!o->fbits) o->fbits = 1024;
163 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
164 }
165 mpmont_create(&c->m, gp.p);
166 if (!o->n) o->n = 1;
167 c->n = o->n;
168 c->e = xmalloc(c->n * sizeof(mp_expfactor));
169 for (i = 0; i < c->n; i++) {
170 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
171 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
172 }
173 dh_paramfree(&gp);
174 return (c);
175}
176
177static void mont_run(void *cc)
178{
179 mont_ctx *c = cc;
180 mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
181 MP_DROP(d);
182}
183
184static void montsim_run(void *cc)
185{
186 mont_ctx *c = cc;
187 mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
188 MP_DROP(d);
189}
190
191/* --- Group exponentiation --- */
192
193typedef struct gr_ctx {
194 size_t n;
195 group *g;
196 group_expfactor *e;
197} gr_ctx;
198
199static void *grp_init(opts *o)
200{
201 gr_ctx *c = CREATE(gr_ctx);
202 const char *e;
203 gprime_param gp;
204 qd_parse qd;
205 size_t i;
206
207 if (o->name) {
208 qd.p = o->name;
209 if (dh_parse(&qd, &gp))
210 die(1, "bad prime group: %s", qd.e);
211 } else {
212 if (!o->fbits) o->fbits = 1024;
213 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
214 }
215 c->g = group_prime(&gp);
e74e12bc 216 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
e2edda68 217 die(1, "bad group: %s", e);
218 if (!o->n) o->n = 1;
219 c->n = o->n;
220 c->e = xmalloc(c->n * sizeof(group_expfactor));
221 for (i = 0; i < c->n; i++) {
222 c->e[i].base = G_CREATE(c->g);
223 G_FROMINT(c->g, c->e[i].base,
224 mprand_range(MP_NEW, gp.p, &rand_global, 0));
225 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
226 }
227 dh_paramfree(&gp);
228 return (c);
229}
230
231static void *grec_init(opts *o)
232{
233 gr_ctx *c = CREATE(gr_ctx);
234 const char *e;
235 ec_info ei;
236 ec p = EC_INIT;
237 size_t i;
238
239 if (!o->name)
240 die(1, "can't generate elliptic curves");
241 if ((e = ec_getinfo(&ei, o->name)) != 0)
242 die(1, "bad curve: %s", e);
243 c->g = group_ec(&ei);
e74e12bc 244 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
e2edda68 245 die(1, "bad group: %s", e);
246 if (!o->n) o->n = 1;
247 c->n = o->n;
248 c->e = xmalloc(c->n * sizeof(group_expfactor));
249 for (i = 0; i < c->n; i++) {
250 c->e[i].base = G_CREATE(c->g);
251 ec_rand(ei.c, &p, &rand_global);
252 G_FROMEC(c->g, c->e[i].base, &p);
253 c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
254 }
255 EC_DESTROY(&p);
256 return (c);
257}
258
259static void gr_run(void *cc)
260{
261 gr_ctx *c = cc;
262 ge *x = G_CREATE(c->g);
263 G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
264 G_DESTROY(c->g, x);
265}
266
267static void grsim_run(void *cc)
268{
269 gr_ctx *c = cc;
270 ge *x = G_CREATE(c->g);
271 G_MEXP(c->g, x, c->e, c->n);
272 G_DESTROY(c->g, x);
273}
274
fc2d44af
MW
275/* --- x25519 --- */
276
277typedef struct x25519_jobctx {
278 octet k[X25519_KEYSZ];
279 octet p[X25519_PUBSZ];
280} x25519_jobctx;
281
282static void *x25519_jobinit(opts *o)
283{
284 x25519_jobctx *c = CREATE(x25519_jobctx);
285 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
286 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
287 return (c);
288}
289
290static void x25519_jobrun(void *cc)
291 { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); }
292
643eb1bb
MW
293/* --- x448 --- */
294
295typedef struct x448_jobctx {
296 octet k[X448_KEYSZ];
297 octet p[X448_PUBSZ];
298} x448_jobctx;
299
300static void *x448_jobinit(opts *o)
301{
302 x448_jobctx *c = CREATE(x448_jobctx);
303 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
304 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
305 return (c);
306}
307
308static void x448_jobrun(void *cc)
309 { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); }
310
d56fd9d1
MW
311/* --- Ed25519 --- */
312
313typedef struct ed25519_signctx {
314 octet k[ED25519_KEYSZ];
315 octet K[ED25519_PUBSZ];
316 octet m[64];
317} ed25519_signctx;
318
319typedef struct ed25519_vrfctx {
320 octet K[ED25519_PUBSZ];
321 octet m[64];
322 octet sig[ED25519_SIGSZ];
323} ed25519_vrfctx;
324
325static void *ed25519_signinit(opts *o)
326{
327 ed25519_signctx *c = CREATE(ed25519_signctx);
328
329 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
330 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
331 ed25519_pubkey(c->K, c->k, sizeof(c->k));
332 return (c);
333}
334
335static void ed25519_signrun(void *cc)
336{
337 ed25519_signctx *c = cc;
338 octet sig[ED25519_SIGSZ];
339
340 ed25519_sign(sig, c->k, sizeof(c->k), c->K, c->m, sizeof(c->m));
341}
342
343static void *ed25519_vrfinit(opts *o)
344{
345 octet k[ED25519_KEYSZ];
346 ed25519_vrfctx *c = CREATE(ed25519_vrfctx);
347
348 rand_get(RAND_GLOBAL, k, sizeof(k));
349 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
350 ed25519_pubkey(c->K, k, sizeof(k));
351 ed25519_sign(c->sig, k, sizeof(k), c->K, c->m, sizeof(c->m));
352 return (c);
353}
354
355static void ed25519_vrfrun(void *cc)
356{
357 ed25519_vrfctx *c = cc;
358 ed25519_verify(c->K, c->m, sizeof(c->m), c->sig);
359}
360
e2edda68 361/* --- RSA --- */
362
363typedef struct rsapriv_ctx {
364 rsa_priv rp;
365 rsa_privctx rpc;
366 mp *m;
367} rsapriv_ctx;
368
369static void *rsapriv_init(opts *o)
370{
371 rsapriv_ctx *c = CREATE(rsapriv_ctx);
372
373 if (!o->fbits) o->fbits = 1024;
374 rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
375 rsa_privcreate(&c->rpc, &c->rp, 0);
376 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
377 return (c);
378}
379
380static void *rsaprivblind_init(opts *o)
381{
382 rsapriv_ctx *c = CREATE(rsapriv_ctx);
383
384 if (!o->fbits) o->fbits = 1024;
385 rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
386 rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
387 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
388 return (c);
389}
390
391static void rsapriv_run(void *cc)
392{
393 rsapriv_ctx *c = cc;
394 mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
395 MP_DROP(d);
396}
397
398typedef struct rsapub_ctx {
399 rsa_pub rp;
400 rsa_pubctx rpc;
401 mp *m;
402} rsapub_ctx;
403
404static void *rsapub_init(opts *o)
405{
406 rsapub_ctx *c = CREATE(rsapub_ctx);
407 rsa_priv rp;
408
409 if (!o->fbits) o->fbits = 1024;
410 rsa_gen(&rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
411 c->rp.n = MP_COPY(rp.n);
412 c->rp.e = MP_COPY(rp.e);
413 rsa_privfree(&rp);
414 rsa_pubcreate(&c->rpc, &c->rp);
415 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
416 return (c);
417}
418
419static void rsapub_run(void *cc)
420{
421 rsapub_ctx *c = cc;
422 mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
423 MP_DROP(d);
424}
425
426/* --- Symmetric encryption --- */
427
428typedef struct ksched_ctx {
429 const gccipher *c;
430 octet *k;
431 size_t ksz;
432} ksched_ctx;
433
434static void *ksched_init(opts *o)
435{
436 ksched_ctx *c = CREATE(ksched_ctx);
437 if (!o->name)
438 die(1, "must specify encryption scheme name");
439 if ((c->c = gcipher_byname(o->name)) == 0)
440 die(1, "encryption scheme `%s' not known", o->name);
441 c->ksz = keysz(o->gbits/8, c->c->keysz);
442 c->k = xmalloc(c->ksz);
443 rand_get(RAND_GLOBAL, c->k, c->ksz);
444 return (c);
445}
446
447static void ksched_run(void *cc)
448{
449 ksched_ctx *c = cc;
450 gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
451 GC_DESTROY(gc);
452}
453
454typedef struct enc_ctx {
455 gcipher *c;
456 octet *m;
457 size_t sz;
458 size_t n;
459} enc_ctx;
460
461static void *enc_init(opts *o)
462{
463 enc_ctx *c = CREATE(enc_ctx);
464 const gccipher *cc;
465 size_t ksz;
466 octet *k;
467 if (!o->name)
468 die(1, "must specify encryption scheme name");
469 if ((cc = gcipher_byname(o->name)) == 0)
470 die(1, "encryption scheme `%s' not known", o->name);
471 ksz = keysz(0, cc->keysz);
472 k = xmalloc(ksz);
473 rand_get(RAND_GLOBAL, k, ksz);
474 c->c = GC_INIT(cc, k, ksz);
475 xfree(k);
476 c->sz = o->gbits ? o->gbits : 65536;
477 c->n = o->n ? o->n : 16;
478 c->m = xmalloc(c->sz);
479 return (c);
480}
481
482static void enc_run(void *cc)
483{
484 enc_ctx *c = cc;
485 size_t i;
486 for (i = 0; i < c->n; i++)
487 GC_ENCRYPT(c->c, c->m, c->m, c->sz);
488}
489
490/* --- Hashing --- */
491
492typedef struct hash_ctx {
493 const gchash *h;
494 octet *m;
495 size_t sz;
496 size_t n;
497} hash_ctx;
498
499static void *hash_init(opts *o)
500{
501 hash_ctx *c = CREATE(hash_ctx);
502 if (!o->name)
503 die(1, "must specify hash function name");
504 if ((c->h = ghash_byname(o->name)) == 0)
505 die(1, "hash function `%s' not known", o->name);
506 c->sz = o->gbits ? o->gbits : 65536;
507 c->n = o->n ? o->n : 16;
508 c->m = xmalloc(c->sz);
509 return (c);
510}
511
512static void hash_run(void *cc)
513{
514 hash_ctx *c = cc;
515 size_t i;
516 ghash *h = GH_INIT(c->h);
517 for (i = 0; i < c->n; i++)
518 GH_HASH(h, c->m, c->sz);
519 GH_DONE(h, 0);
520 GH_DESTROY(h);
521}
522
ef7cf21d
MW
523/* --- Poly1305 --- */
524
525typedef struct poly1305_jobctx {
526 poly1305_key k;
527 octet s[POLY1305_MASKSZ];
528 octet *m;
529 size_t sz;
530 size_t n;
531} poly1305_jobctx;
532
533static void *poly1305_jobinit(opts *o)
534{
535 octet k[POLY1305_KEYSZ];
536 poly1305_jobctx *c = CREATE(poly1305_jobctx);
537 rand_get(RAND_GLOBAL, k, sizeof(k));
538 poly1305_keyinit(&c->k, k, sizeof(k));
539 rand_get(RAND_GLOBAL, c->s, sizeof(c->s));
540 c->sz = o->gbits ? o->gbits : 65536;
541 c->n = o->n ? o->n : 16;
542 c->m = xmalloc(c->sz);
543 return (c);
544}
545
546static void poly1305_jobrun(void *cc)
547{
548 poly1305_jobctx *c = cc;
549 poly1305_ctx ctx;
550 octet t[POLY1305_TAGSZ];
551 size_t i;
552 poly1305_macinit(&ctx, &c->k, c->s);
553 for (i = 0; i < c->n; i++) poly1305_hash(&ctx, c->m, c->sz);
554 poly1305_done(&ctx, t);
555}
556
e2edda68 557/* --- Job table --- */
558
c65df279 559typedef struct jobops {
e2edda68 560 const char *name;
561 void *(*init)(opts *);
562 void (*run)(void *);
563} jobops;
564
565static const jobops jobtab[] = {
45c0fd36 566 { "g-prime-exp", grp_init, gr_run },
e2edda68 567 { "g-ec-mul", grec_init, gr_run },
568 { "g-prime-exp-sim", grp_init, grsim_run },
569 { "g-ec-mul-sim", grec_init, grsim_run },
570 { "barrett-exp", bar_init, bar_run },
571 { "barrett-exp-sim", bar_init, barsim_run },
572 { "mont-exp", mont_init, mont_run },
573 { "mont-exp-sim", mont_init, montsim_run },
574 { "rsa-priv", rsapriv_init, rsapriv_run },
575 { "rsa-priv-blind", rsaprivblind_init, rsapriv_run },
576 { "rsa-pub", rsapub_init, rsapub_run },
fc2d44af 577 { "x25519", x25519_jobinit, x25519_jobrun },
643eb1bb 578 { "x448", x448_jobinit, x448_jobrun },
d56fd9d1
MW
579 { "ed25519-sign", ed25519_signinit, ed25519_signrun },
580 { "ed25519-vrf", ed25519_vrfinit, ed25519_vrfrun },
e2edda68 581 { "ksched", ksched_init, ksched_run },
582 { "enc", enc_init, enc_run },
583 { "hash", hash_init, hash_run },
ef7cf21d 584 { "poly1305", poly1305_jobinit, poly1305_jobrun },
e2edda68 585 { 0, 0, 0 }
586};
587
588/*----- Main code ---------------------------------------------------------*/
589
c65df279 590void version(FILE *fp)
e2edda68 591{
592 pquis(fp, "$, Catacomb " VERSION "\n");
593}
594
595static void usage(FILE *fp)
596{
597 pquis(fp, "Usage: $ [-options] job\n");
598}
599
600static void help(FILE *fp)
601{
602 version(fp);
603 putc('\n', fp);
604 usage(fp);
605 pquis(fp, "\n\
606Various performance tests.\n\
c65df279 607\n\
608Options:\n\
609\n\
610-h, --help Show this help text.\n\
611-v, --version Show program version number.\n\
612-u, --usage Show terse usage message.\n\
613-l, --list [ITEM...] List all the various names of things.\n\
614\n\
615-C, --name=NAME Select curve/DH-group/enc/hash name.\n\
616-b, --field-bits Field size for g-prime and rsa.\n\
78614e02 617-q, --no-check Don't check field/group for validity.\n\
c65df279 618-B, --group-bits Group size for g-prime; key size for ksched;\n\
619 data size for enc and hash.\n\
620-n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
621-i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
622-t, --time=TIME Length of an interval in seconds. [1]\n\
e2edda68 623");
624}
625
c65df279 626#define LISTS(LI) \
627 LI("Lists", list, \
628 listtab[i].name, listtab[i].name) \
629 LI("Jobs", job, \
630 jobtab[i].name, jobtab[i].name) \
631 LI("Elliptic curves", ec, \
632 ectab[i].name, ectab[i].name) \
633 LI("Diffie-Hellman groups", dh, \
634 ptab[i].name, ptab[i].name) \
635 LI("Encryption algorithms", cipher, \
636 gciphertab[i], gciphertab[i]->name) \
637 LI("Hash functions", hash, \
638 ghashtab[i], ghashtab[i]->name)
639
640MAKELISTTAB(listtab, LISTS)
641
e2edda68 642static unsigned uarg(const char *what, const char *p)
643{
644 char *q;
645 unsigned long u;
646 errno = 0;
647 u = strtoul(p, &q, 0);
648 if (*q || u > UINT_MAX || q == p || errno)
649 die(1, "bad %s `%s'", what, p);
650 return (u);
651}
652
653static double farg(const char *what, const char *p)
654{
655 char *q;
656 double f;
657 errno = 0;
658 f = strtod(p, &q);
659 if (*q || q == p || errno)
660 die(1, "bad %s `%s'", what, p);
661 return (f);
662}
663
664int main(int argc, char *argv[])
665{
666 int i;
667 opts o = { 0 };
668 const jobops *j;
669 struct timeval tv_next, tv_now;
670 double t, ttot;
671 unsigned n;
672 unsigned long ii;
673 clock_t c_start, c_stop;
674 double itot;
675 void *p;
676
677 ego(argv[0]);
678 o.t = 1;
679 for (;;) {
680 static const struct option opts[] = {
681 { "help", 0, 0, 'h' },
682 { "version", 0, 0, 'v' },
683 { "usage", 0, 0, 'u' },
c65df279 684 { "list", 0, 0, 'l' },
e2edda68 685 { "name", OPTF_ARGREQ, 0, 'C' },
686 { "field-bits", OPTF_ARGREQ, 0, 'b' },
687 { "group-bits", OPTF_ARGREQ, 0, 'B' },
688 { "factors", OPTF_ARGREQ, 0, 'n' },
689 { "intervals", OPTF_ARGREQ, 0, 'i' },
690 { "time", OPTF_ARGREQ, 0, 't' },
e74e12bc 691 { "no-check", 0, 0, 'q' },
e2edda68 692 { 0, 0, 0, 0 }
693 };
694
e74e12bc 695 i = mdwopt(argc, argv, "hvulC:b:B:n:i:t:q", opts, 0, 0, 0);
e2edda68 696 if (i < 0) break;
697 switch (i) {
698 case 'h': help(stdout); exit(0);
699 case 'v': version(stdout); exit(0);
700 case 'u': usage(stdout); exit(0);
c65df279 701 case 'l': exit(displaylists(listtab, argv + optind));
e2edda68 702 case 'C': o.name = optarg; break;
703 case 'b': o.fbits = uarg("field bits", optarg); break;
704 case 'B': o.gbits = uarg("subgroup bits", optarg); break;
705 case 'n': o.n = uarg("factor count", optarg); break;
706 case 'i': o.i = uarg("interval count", optarg); break;
707 case 't': o.t = farg("interval length", optarg); break;
e74e12bc 708 case 'q': o.f |= OF_NOCHECK; break;
e2edda68 709 default: usage(stderr); exit(1);
710 }
711 }
712 if (optind + 1 != argc) { usage(stderr); exit(1); }
713
714 for (j = jobtab; j->name; j++)
715 if (strcmp(j->name, argv[optind]) == 0) break;
716 if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
717 p = j->init(&o);
718
719 n = 0;
45c0fd36 720 ttot = itot = 0;
e2edda68 721 gettimeofday(&tv_now, 0);
722 do {
723 tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
724 ii = 0;
725 c_start = clock();
726 do {
727 j->run(p);
728 ii++;
729 gettimeofday(&tv_now, 0);
730 } while (TV_CMP(&tv_now, <, &tv_next));
731 c_stop = clock();
732 t = (double)(c_stop - c_start)/CLOCKS_PER_SEC;
733 itot += ii;
734 ttot += t;
735 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
736 n, ii, ii/t, itot/ttot);
737 fflush(stdout);
738 n++;
739 } while (!o.i || n < o.i);
740
741 return (0);
742}
743
744/*----- That's all, folks -------------------------------------------------*/