progs/perftest.c: Report cycle counts per operation where possible.
[catacomb] / progs / perftest.c
1 /* -*-c-*-
2 *
3 * Measure performance of various operations (Unix-specific)
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
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.
16 *
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.
21 *
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
30 #define _FILE_OFFSET_BITS 64
31
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 #ifdef HAVE_LINUX_PERF_EVENT_H
47 # include <linux/perf_event.h>
48 # include <asm/unistd.h>
49 #endif
50
51 #include <mLib/alloc.h>
52 #include <mLib/bits.h>
53 #include <mLib/dstr.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
57 #include <mLib/sub.h>
58 #include <mLib/tv.h>
59
60 #include "rand.h"
61 #include "mp.h"
62 #include "mprand.h"
63 #include "fibrand.h"
64 #include "rsa.h"
65 #include "mpint.h"
66 #include "mptext.h"
67 #include "mpmont.h"
68 #include "mpbarrett.h"
69 #include "dh.h"
70 #include "pgen.h"
71 #include "ec.h"
72 #include "group.h"
73 #include "x25519.h"
74 #include "x448.h"
75 #include "ed25519.h"
76 #include "ed448.h"
77
78 #include "cc.h"
79 #include "gcipher.h"
80 #include "ghash.h"
81 #include "gmac.h"
82 #include "poly1305.h"
83
84 #include "ectab.h"
85 #include "ptab.h"
86
87 /*----- Options -----------------------------------------------------------*/
88
89 typedef struct opts {
90 const char *name; /* Pre-configured named thing */
91 const char *opwhat; /* What to call operations */
92 unsigned fbits; /* Field size bits */
93 unsigned gbits; /* Group size bits */
94 unsigned n; /* Number of factors */
95 unsigned i; /* Number of intervals (or zero) */
96 unsigned k; /* Main loop batch size */
97 unsigned long sc; /* Scale factor */
98 double t; /* Time for each interval (secs) */
99 mp *e; /* Public exponent */
100 unsigned f; /* Flags */
101 #define OF_NOCHECK 1u /* Don't do group checking */
102 } opts;
103
104 /*----- Job switch --------------------------------------------------------*/
105
106 /* --- Barrett exponentiation --- */
107
108 typedef struct bar_ctx {
109 size_t n;
110 mpbarrett b;
111 mp_expfactor *e;
112 } bar_ctx;
113
114 static void *bar_init(opts *o)
115 {
116 bar_ctx *c = CREATE(bar_ctx);
117 gprime_param gp;
118 qd_parse qd;
119 size_t i;
120
121 if (o->name) {
122 qd.p = o->name;
123 if (dh_parse(&qd, &gp))
124 die(1, "bad prime group: %s", qd.e);
125 } else {
126 if (!o->fbits) o->fbits = 1024;
127 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
128 }
129 mpbarrett_create(&c->b, gp.p);
130 if (!o->n) o->n = 1;
131 c->n = o->n;
132 c->e = xmalloc(c->n * sizeof(group_expfactor));
133 for (i = 0; i < c->n; i++) {
134 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
135 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
136 }
137 dh_paramfree(&gp);
138 return (c);
139 }
140
141 static void bar_run(void *cc)
142 {
143 bar_ctx *c = cc;
144 mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
145 MP_DROP(d);
146 }
147
148 static void barsim_run(void *cc)
149 {
150 bar_ctx *c = cc;
151 mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
152 MP_DROP(d);
153 }
154
155 /* --- Montgomery exponentiation --- */
156
157 typedef struct mont_ctx {
158 size_t n;
159 mpmont m;
160 mp_expfactor *e;
161 } mont_ctx;
162
163 static void *mont_init(opts *o)
164 {
165 mont_ctx *c = CREATE(mont_ctx);
166 gprime_param gp;
167 qd_parse qd;
168 size_t i;
169
170 if (o->name) {
171 qd.p = o->name;
172 if (dh_parse(&qd, &gp))
173 die(1, "bad prime group: %s", qd.e);
174 } else {
175 if (!o->fbits) o->fbits = 1024;
176 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
177 }
178 mpmont_create(&c->m, gp.p);
179 if (!o->n) o->n = 1;
180 c->n = o->n;
181 c->e = xmalloc(c->n * sizeof(mp_expfactor));
182 for (i = 0; i < c->n; i++) {
183 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
184 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
185 }
186 dh_paramfree(&gp);
187 return (c);
188 }
189
190 static void mont_run(void *cc)
191 {
192 mont_ctx *c = cc;
193 mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
194 MP_DROP(d);
195 }
196
197 static void montsim_run(void *cc)
198 {
199 mont_ctx *c = cc;
200 mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
201 MP_DROP(d);
202 }
203
204 /* --- Group exponentiation --- */
205
206 typedef struct gr_ctx {
207 size_t n;
208 group *g;
209 group_expfactor *e;
210 } gr_ctx;
211
212 static void *grp_init(opts *o)
213 {
214 gr_ctx *c = CREATE(gr_ctx);
215 const char *e;
216 gprime_param gp;
217 qd_parse qd;
218 size_t i;
219
220 if (o->name) {
221 qd.p = o->name;
222 if (dh_parse(&qd, &gp))
223 die(1, "bad prime group: %s", qd.e);
224 } else {
225 if (!o->fbits) o->fbits = 1024;
226 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
227 }
228 c->g = group_prime(&gp);
229 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
230 die(1, "bad group: %s", e);
231 if (!o->n) o->n = 1;
232 c->n = o->n;
233 c->e = xmalloc(c->n * sizeof(group_expfactor));
234 for (i = 0; i < c->n; i++) {
235 c->e[i].base = G_CREATE(c->g);
236 G_FROMINT(c->g, c->e[i].base,
237 mprand_range(MP_NEW, gp.p, &rand_global, 0));
238 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
239 }
240 dh_paramfree(&gp);
241 return (c);
242 }
243
244 static void *grec_init(opts *o)
245 {
246 gr_ctx *c = CREATE(gr_ctx);
247 const char *e;
248 ec_info ei;
249 ec p = EC_INIT;
250 size_t i;
251
252 if (!o->name)
253 die(1, "can't generate elliptic curves");
254 if ((e = ec_getinfo(&ei, o->name)) != 0)
255 die(1, "bad curve: %s", e);
256 c->g = group_ec(&ei);
257 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
258 die(1, "bad group: %s", e);
259 if (!o->n) o->n = 1;
260 c->n = o->n;
261 c->e = xmalloc(c->n * sizeof(group_expfactor));
262 for (i = 0; i < c->n; i++) {
263 c->e[i].base = G_CREATE(c->g);
264 ec_rand(ei.c, &p, &rand_global);
265 G_FROMEC(c->g, c->e[i].base, &p);
266 c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
267 }
268 EC_DESTROY(&p);
269 return (c);
270 }
271
272 static void gr_run(void *cc)
273 {
274 gr_ctx *c = cc;
275 ge *x = G_CREATE(c->g);
276 G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
277 G_DESTROY(c->g, x);
278 }
279
280 static void grsim_run(void *cc)
281 {
282 gr_ctx *c = cc;
283 ge *x = G_CREATE(c->g);
284 G_MEXP(c->g, x, c->e, c->n);
285 G_DESTROY(c->g, x);
286 }
287
288 /* --- x25519 --- */
289
290 typedef struct x25519_jobctx {
291 octet k[X25519_KEYSZ];
292 octet p[X25519_PUBSZ];
293 } x25519_jobctx;
294
295 static void *x25519_jobinit(opts *o)
296 {
297 x25519_jobctx *c = CREATE(x25519_jobctx);
298 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
299 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
300 return (c);
301 }
302
303 static void x25519_jobrun(void *cc)
304 { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); }
305
306 /* --- x448 --- */
307
308 typedef struct x448_jobctx {
309 octet k[X448_KEYSZ];
310 octet p[X448_PUBSZ];
311 } x448_jobctx;
312
313 static void *x448_jobinit(opts *o)
314 {
315 x448_jobctx *c = CREATE(x448_jobctx);
316 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
317 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
318 return (c);
319 }
320
321 static void x448_jobrun(void *cc)
322 { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); }
323
324 /* --- Ed25519 --- */
325
326 typedef struct ed25519_signctx {
327 octet k[ED25519_KEYSZ];
328 octet K[ED25519_PUBSZ];
329 octet m[64];
330 } ed25519_signctx;
331
332 typedef struct ed25519_vrfctx {
333 octet K[ED25519_PUBSZ];
334 octet m[64];
335 octet sig[ED25519_SIGSZ];
336 } ed25519_vrfctx;
337
338 static void *ed25519_signinit(opts *o)
339 {
340 ed25519_signctx *c = CREATE(ed25519_signctx);
341
342 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
343 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
344 ed25519_pubkey(c->K, c->k, sizeof(c->k));
345 return (c);
346 }
347
348 static void ed25519_signrun(void *cc)
349 {
350 ed25519_signctx *c = cc;
351 octet sig[ED25519_SIGSZ];
352
353 ed25519_sign(sig, c->k, sizeof(c->k), c->K, c->m, sizeof(c->m));
354 }
355
356 static void *ed25519_vrfinit(opts *o)
357 {
358 octet k[ED25519_KEYSZ];
359 ed25519_vrfctx *c = CREATE(ed25519_vrfctx);
360
361 rand_get(RAND_GLOBAL, k, sizeof(k));
362 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
363 ed25519_pubkey(c->K, k, sizeof(k));
364 ed25519_sign(c->sig, k, sizeof(k), c->K, c->m, sizeof(c->m));
365 return (c);
366 }
367
368 static void ed25519_vrfrun(void *cc)
369 {
370 ed25519_vrfctx *c = cc;
371 ed25519_verify(c->K, c->m, sizeof(c->m), c->sig);
372 }
373
374 /* --- Ed448 --- */
375
376 typedef struct ed448_signctx {
377 octet k[ED448_KEYSZ];
378 octet K[ED448_PUBSZ];
379 octet m[64];
380 } ed448_signctx;
381
382 typedef struct ed448_vrfctx {
383 octet K[ED448_PUBSZ];
384 octet m[64];
385 octet sig[ED448_SIGSZ];
386 } ed448_vrfctx;
387
388 static void *ed448_signinit(opts *o)
389 {
390 ed448_signctx *c = CREATE(ed448_signctx);
391
392 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
393 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
394 ed448_pubkey(c->K, c->k, sizeof(c->k));
395 return (c);
396 }
397
398 static void ed448_signrun(void *cc)
399 {
400 ed448_signctx *c = cc;
401 octet sig[ED448_SIGSZ];
402
403 ed448_sign(sig, c->k, sizeof(c->k), c->K, 0, 0, 0, c->m, sizeof(c->m));
404 }
405
406 static void *ed448_vrfinit(opts *o)
407 {
408 octet k[ED448_KEYSZ];
409 ed448_vrfctx *c = CREATE(ed448_vrfctx);
410
411 rand_get(RAND_GLOBAL, k, sizeof(k));
412 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
413 ed448_pubkey(c->K, k, sizeof(k));
414 ed448_sign(c->sig, k, sizeof(k), c->K, 0, 0, 0, c->m, sizeof(c->m));
415 return (c);
416 }
417
418 static void ed448_vrfrun(void *cc)
419 {
420 ed448_vrfctx *c = cc;
421 ed448_verify(c->K, 0, 0, 0, c->m, sizeof(c->m), c->sig);
422 }
423
424 /* --- RSA --- */
425
426 typedef struct rsapriv_ctx {
427 rsa_priv rp;
428 rsa_privctx rpc;
429 mp *m;
430 } rsapriv_ctx;
431
432 static void *rsapriv_init(opts *o)
433 {
434 rsapriv_ctx *c = CREATE(rsapriv_ctx);
435
436 if (!o->fbits) o->fbits = 1024;
437 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
438 rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
439 rsa_privcreate(&c->rpc, &c->rp, 0);
440 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
441 return (c);
442 }
443
444 static void *rsaprivblind_init(opts *o)
445 {
446 rsapriv_ctx *c = CREATE(rsapriv_ctx);
447
448 if (!o->fbits) o->fbits = 1024;
449 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
450 rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
451 rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
452 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
453 return (c);
454 }
455
456 static void rsapriv_run(void *cc)
457 {
458 rsapriv_ctx *c = cc;
459 mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
460 MP_DROP(d);
461 }
462
463 typedef struct rsapub_ctx {
464 rsa_pub rp;
465 rsa_pubctx rpc;
466 mp *m;
467 } rsapub_ctx;
468
469 static void *rsapub_init(opts *o)
470 {
471 rsapub_ctx *c = CREATE(rsapub_ctx);
472 rsa_priv rp;
473
474 if (!o->fbits) o->fbits = 1024;
475 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
476 rsa_gen_e(&rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
477 c->rp.n = MP_COPY(rp.n);
478 c->rp.e = MP_COPY(rp.e);
479 rsa_privfree(&rp);
480 rsa_pubcreate(&c->rpc, &c->rp);
481 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
482 return (c);
483 }
484
485 static void rsapub_run(void *cc)
486 {
487 rsapub_ctx *c = cc;
488 mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
489 MP_DROP(d);
490 }
491
492 /* --- Symmetric encryption --- */
493
494 typedef struct ksched_ctx {
495 const gccipher *c;
496 octet *k;
497 size_t ksz;
498 } ksched_ctx;
499
500 static void *ksched_init(opts *o)
501 {
502 ksched_ctx *c = CREATE(ksched_ctx);
503 if (!o->name)
504 die(1, "must specify encryption scheme name");
505 if ((c->c = gcipher_byname(o->name)) == 0)
506 die(1, "encryption scheme `%s' not known", o->name);
507 c->ksz = keysz(o->fbits/8, c->c->keysz);
508 if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8))
509 die(1, "bad key size %u for %s", o->fbits, o->name);
510 c->k = xmalloc(c->ksz);
511 rand_get(RAND_GLOBAL, c->k, c->ksz);
512 return (c);
513 }
514
515 static void ksched_run(void *cc)
516 {
517 ksched_ctx *c = cc;
518 gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
519 GC_DESTROY(gc);
520 }
521
522 typedef struct enc_ctx {
523 gcipher *c;
524 octet *m;
525 size_t sz;
526 size_t n;
527 } enc_ctx;
528
529 static void *enc_init(opts *o)
530 {
531 enc_ctx *c = CREATE(enc_ctx);
532 const gccipher *cc;
533 size_t ksz;
534 octet *k;
535 if (!o->name)
536 die(1, "must specify encryption scheme name");
537 if ((cc = gcipher_byname(o->name)) == 0)
538 die(1, "encryption scheme `%s' not known", o->name);
539 ksz = keysz(o->fbits/8, cc->keysz);
540 if (o->fbits%8 || (o->fbits && ksz != o->fbits/8))
541 die(1, "bad key size %u for %s", o->fbits, o->name);
542 k = xmalloc(ksz);
543 rand_get(RAND_GLOBAL, k, ksz);
544 c->c = GC_INIT(cc, k, ksz);
545 xfree(k);
546 c->sz = o->gbits ? o->gbits : 65536;
547 c->n = o->n ? o->n : 16;
548 o->opwhat = "byte"; o->sc = c->n*c->sz;
549 c->m = xmalloc(c->sz);
550 return (c);
551 }
552
553 static void enc_run(void *cc)
554 {
555 enc_ctx *c = cc;
556 size_t i;
557 for (i = 0; i < c->n; i++)
558 GC_ENCRYPT(c->c, c->m, c->m, c->sz);
559 }
560
561 /* --- Hashing --- */
562
563 typedef struct hash_ctx {
564 const gchash *h;
565 octet *m;
566 size_t sz;
567 size_t n;
568 } hash_ctx;
569
570 static void *hash_init(opts *o)
571 {
572 hash_ctx *c = CREATE(hash_ctx);
573 if (!o->name)
574 die(1, "must specify hash function name");
575 if ((c->h = ghash_byname(o->name)) == 0)
576 die(1, "hash function `%s' not known", o->name);
577 c->sz = o->gbits ? o->gbits : 65536;
578 c->n = o->n ? o->n : 16;
579 o->opwhat = "byte"; o->sc = c->n*c->sz;
580 c->m = xmalloc(c->sz);
581 return (c);
582 }
583
584 static void hash_run(void *cc)
585 {
586 hash_ctx *c = cc;
587 size_t i;
588 ghash *h = GH_INIT(c->h);
589 for (i = 0; i < c->n; i++)
590 GH_HASH(h, c->m, c->sz);
591 GH_DONE(h, 0);
592 GH_DESTROY(h);
593 }
594
595 /* --- Poly1305 --- */
596
597 typedef struct poly1305_jobctx {
598 poly1305_key k;
599 octet s[POLY1305_MASKSZ];
600 octet *m;
601 size_t sz;
602 size_t n;
603 } poly1305_jobctx;
604
605 static void *poly1305_jobinit(opts *o)
606 {
607 octet k[POLY1305_KEYSZ];
608 poly1305_jobctx *c = CREATE(poly1305_jobctx);
609 rand_get(RAND_GLOBAL, k, sizeof(k));
610 poly1305_keyinit(&c->k, k, sizeof(k));
611 rand_get(RAND_GLOBAL, c->s, sizeof(c->s));
612 c->sz = o->gbits ? o->gbits : 65536;
613 c->n = o->n ? o->n : 16;
614 o->opwhat = "byte"; o->sc = c->n*c->sz;
615 c->m = xmalloc(c->sz);
616 return (c);
617 }
618
619 static void poly1305_jobrun(void *cc)
620 {
621 poly1305_jobctx *c = cc;
622 poly1305_ctx ctx;
623 octet t[POLY1305_TAGSZ];
624 size_t i;
625 poly1305_macinit(&ctx, &c->k, c->s);
626 for (i = 0; i < c->n; i++) poly1305_hash(&ctx, c->m, c->sz);
627 poly1305_done(&ctx, t);
628 }
629
630 /* --- Job table --- */
631
632 typedef struct jobops {
633 const char *name;
634 void *(*init)(opts *);
635 void (*run)(void *);
636 } jobops;
637
638 static const jobops jobtab[] = {
639 { "g-prime-exp", grp_init, gr_run },
640 { "g-ec-mul", grec_init, gr_run },
641 { "g-prime-exp-sim", grp_init, grsim_run },
642 { "g-ec-mul-sim", grec_init, grsim_run },
643 { "barrett-exp", bar_init, bar_run },
644 { "barrett-exp-sim", bar_init, barsim_run },
645 { "mont-exp", mont_init, mont_run },
646 { "mont-exp-sim", mont_init, montsim_run },
647 { "rsa-priv", rsapriv_init, rsapriv_run },
648 { "rsa-priv-blind", rsaprivblind_init, rsapriv_run },
649 { "rsa-pub", rsapub_init, rsapub_run },
650 { "x25519", x25519_jobinit, x25519_jobrun },
651 { "x448", x448_jobinit, x448_jobrun },
652 { "ed25519-sign", ed25519_signinit, ed25519_signrun },
653 { "ed25519-vrf", ed25519_vrfinit, ed25519_vrfrun },
654 { "ed448-sign", ed448_signinit, ed448_signrun },
655 { "ed448-vrf", ed448_vrfinit, ed448_vrfrun },
656 { "ksched", ksched_init, ksched_run },
657 { "enc", enc_init, enc_run },
658 { "hash", hash_init, hash_run },
659 { "poly1305", poly1305_jobinit, poly1305_jobrun },
660 { 0, 0, 0 }
661 };
662
663 /*----- Cycle counting ----------------------------------------------------*/
664
665 typedef kludge64 cycles;
666 static int cyclecount_active_p = 0;
667
668 #if defined(__GNUC__) && (CPUFAM_X86 || CPUFAM_AMD64)
669
670 static void init_cyclecount(void) { cyclecount_active_p = 1; }
671
672 static cycles cyclecount(void)
673 {
674 uint32 lo, hi;
675 kludge64 cy;
676
677 __asm__("rdtsc" : "=a"(lo), "=d"(hi));
678 SET64(cy, hi, lo);
679 return cy;
680 }
681
682 #elif defined(HAVE_LINUX_PERF_EVENT_H) && defined(HAVE_UINT64)
683
684 static int perf_fd = -1;
685
686 static void init_cyclecount(void)
687 {
688 struct perf_event_attr attr = { 0 };
689
690 attr.type = PERF_TYPE_HARDWARE;
691 attr.size = sizeof(attr);
692 attr.config = PERF_COUNT_HW_CPU_CYCLES;
693 attr.disabled = 0;
694 attr.exclude_kernel = 1;
695 attr.exclude_hv = 1;
696
697 if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0)
698 moan("failed to open perf event: %s", strerror(errno));
699 else
700 cyclecount_active_p = 1;
701 }
702
703 static cycles cyclecount(void)
704 {
705 kludge64 cy;
706 ssize_t n;
707
708 if (!cyclecount_active_p)
709 goto fail;
710 else if ((n = read(perf_fd, &cy.i, sizeof(cy.i))) != sizeof(cy.i)) {
711 if (n < 0) moan("error reading perf event: %s", strerror(errno));
712 else moan("unexpected short read from perf event");
713 cyclecount_active_p = 0; close(perf_fd); perf_fd = -1;
714 goto fail;
715 }
716 end:
717 return (cy);
718 fail:
719 SET64(cy, 0, 0);
720 goto end;
721 }
722
723 #else
724
725 static void init_cyclecount(void) { cyclecount_active_p = 0; }
726 static cycles cyclecount(void) { kludge64 cy; SET64(cy, 0, 0); return (cy); }
727
728 #endif
729
730 /*----- Main code ---------------------------------------------------------*/
731
732 void version(FILE *fp)
733 {
734 pquis(fp, "$, Catacomb " VERSION "\n");
735 }
736
737 static void usage(FILE *fp)
738 {
739 pquis(fp, "Usage: $ [-options] job\n");
740 }
741
742 static void help(FILE *fp)
743 {
744 version(fp);
745 putc('\n', fp);
746 usage(fp);
747 pquis(fp, "\n\
748 Various performance tests.\n\
749 \n\
750 Options:\n\
751 \n\
752 -h, --help Show this help text.\n\
753 -v, --version Show program version number.\n\
754 -u, --usage Show terse usage message.\n\
755 -l, --list [ITEM...] List all the various names of things.\n\
756 \n\
757 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
758 -b, --field-bits Field size for g-prime and rsa;\n\
759 key bits for ksched and enc.\n\
760 -q, --no-check Don't check field/group for validity.\n\
761 -B, --group-bits Group size for g-prime; data size for enc and hash.\n\
762 -n, --factors=COUNT Number of factors for {exp,mul}-sim;\n\
763 inner iterations for enc and hash.\n\
764 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
765 -k, --batch=COUNT Number of operations to batch between timer checks.\n\
766 -t, --time=TIME Length of an interval in seconds. [1]\n\
767 ");
768 }
769
770 #define LISTS(LI) \
771 LI("Lists", list, \
772 listtab[i].name, listtab[i].name) \
773 LI("Jobs", job, \
774 jobtab[i].name, jobtab[i].name) \
775 LI("Elliptic curves", ec, \
776 ectab[i].name, ectab[i].name) \
777 LI("Diffie-Hellman groups", dh, \
778 ptab[i].name, ptab[i].name) \
779 LI("Encryption algorithms", cipher, \
780 gciphertab[i], gciphertab[i]->name) \
781 LI("Hash functions", hash, \
782 ghashtab[i], ghashtab[i]->name)
783
784 MAKELISTTAB(listtab, LISTS)
785
786 static unsigned uarg(const char *what, const char *p)
787 {
788 char *q;
789 unsigned long u;
790 errno = 0;
791 u = strtoul(p, &q, 0);
792 if (*q || u > UINT_MAX || q == p || errno)
793 die(1, "bad %s `%s'", what, p);
794 return (u);
795 }
796
797 static mp *mparg(const char *what, const char *p)
798 {
799 char *q;
800 mp *x = mp_readstring(MP_NEW, p, &q, 0);
801 if (!x || *q) die(1, "bad %s `%s'", what, p);
802 return (x);
803 }
804
805 static double farg(const char *what, const char *p)
806 {
807 char *q;
808 double f;
809 errno = 0;
810 f = strtod(p, &q);
811 if (*q || q == p || errno)
812 die(1, "bad %s `%s'", what, p);
813 return (f);
814 }
815
816 int main(int argc, char *argv[])
817 {
818 int i;
819 opts o = { 0 };
820 const jobops *j;
821 struct timeval tv_next, tv_now;
822 double t, ttot, cy, cytot;
823 unsigned n, k;
824 unsigned long ii;
825 clock_t c0, c1;
826 kludge64 cy0, cy1, cydiff;
827 double itot;
828 void *p;
829
830 ego(argv[0]);
831 o.t = 1; o.k = 1; o.sc = 1; o.opwhat = "op";
832 for (;;) {
833 static const struct option opts[] = {
834 { "help", 0, 0, 'h' },
835 { "version", 0, 0, 'v' },
836 { "usage", 0, 0, 'u' },
837 { "list", 0, 0, 'l' },
838 { "name", OPTF_ARGREQ, 0, 'C' },
839 { "field-bits", OPTF_ARGREQ, 0, 'b' },
840 { "group-bits", OPTF_ARGREQ, 0, 'B' },
841 { "factors", OPTF_ARGREQ, 0, 'n' },
842 { "intervals", OPTF_ARGREQ, 0, 'i' },
843 { "batch", OPTF_ARGREQ, 0, 'k' },
844 { "public-exponent", OPTF_ARGREQ, 0, 'e' },
845 { "time", OPTF_ARGREQ, 0, 't' },
846 { "no-check", 0, 0, 'q' },
847 { 0, 0, 0, 0 }
848 };
849
850 i = mdwopt(argc, argv, "hvulC:b:B:n:i:k:e:t:q", opts, 0, 0, 0);
851 if (i < 0) break;
852 switch (i) {
853 case 'h': help(stdout); exit(0);
854 case 'v': version(stdout); exit(0);
855 case 'u': usage(stdout); exit(0);
856 case 'l': exit(displaylists(listtab, argv + optind));
857 case 'C': o.name = optarg; break;
858 case 'b': o.fbits = uarg("field bits", optarg); break;
859 case 'B': o.gbits = uarg("subgroup bits", optarg); break;
860 case 'n': o.n = uarg("factor count", optarg); break;
861 case 'e':
862 mp_drop(o.e); o.e = mparg("public exponent", optarg);
863 if (MP_CMP(o.e, <, MP_THREE) || MP_EVENP(o.e))
864 die(1, "invalid public exponent");
865 break;
866 case 'i': o.i = uarg("interval count", optarg); break;
867 case 't': o.t = farg("interval length", optarg); break;
868 case 'k': o.k = uarg("batch size", optarg); break;
869 case 'q': o.f |= OF_NOCHECK; break;
870 default: usage(stderr); exit(1);
871 }
872 }
873 if (optind + 1 != argc) { usage(stderr); exit(1); }
874
875 for (j = jobtab; j->name; j++)
876 if (strcmp(j->name, argv[optind]) == 0) break;
877 if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
878 p = j->init(&o);
879
880 n = 0;
881 ttot = itot = 0; cytot = 0; init_cyclecount();
882 gettimeofday(&tv_now, 0);
883 do {
884 tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
885 ii = 0;
886 c0 = clock(); cy0 = cyclecount();
887 do {
888 for (k = 0; k < o.k; k++) { j->run(p); }
889 ii += k;
890 gettimeofday(&tv_now, 0);
891 } while (TV_CMP(&tv_now, <, &tv_next));
892 cy1 = cyclecount(); c1 = clock();
893 t = (double)(c1 - c0)/CLOCKS_PER_SEC;
894 itot += ii; ttot += t;
895 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f",
896 n, ii, ii/t, itot/ttot);
897 if (cyclecount_active_p) {
898 SUB64(cydiff, cy1, cy0); cy = LO64(cydiff) + ldexp(HI64(cydiff), 32);
899 cytot += cy;
900 printf(" (cy/%s = %3f; avg cy/%s = %3f)",
901 o.opwhat, cy/ii/o.sc, o.opwhat, cytot/itot/o.sc);
902 }
903 putchar('\n');
904 fflush(stdout);
905 n++;
906 } while (!o.i || n < o.i);
907
908 return (0);
909 }
910
911 /*----- That's all, folks -------------------------------------------------*/