progs/catcrypt.1: Rephrase the descriptions of the available lists.
[catacomb] / progs / perftest.c
index f064c2a..267712b 100644 (file)
 #include <sys/time.h>
 #include <unistd.h>
 
+#ifdef HAVE_LINUX_PERF_EVENT_H
+#  include <linux/perf_event.h>
+#  include <asm/unistd.h>
+#endif
+
 #include <mLib/alloc.h>
+#include <mLib/bits.h>
 #include <mLib/dstr.h>
 #include <mLib/mdwopt.h>
 #include <mLib/quis.h>
 
 typedef struct opts {
   const char *name;                    /* Pre-configured named thing */
+  const char *opwhat;                  /* What to call operations */
   unsigned fbits;                      /* Field size bits */
   unsigned gbits;                      /* Group size bits */
   unsigned n;                          /* Number of factors */
   unsigned i;                          /* Number of intervals (or zero) */
+  unsigned k;                          /* Main loop batch size */
+  unsigned long sc;                    /* Scale factor */
   double t;                            /* Time for each interval (secs) */
   mp *e;                               /* Public exponent */
   unsigned f;                          /* Flags */
@@ -495,7 +504,9 @@ static void *ksched_init(opts *o)
     die(1, "must specify encryption scheme name");
   if ((c->c = gcipher_byname(o->name)) == 0)
     die(1, "encryption scheme `%s' not known", o->name);
-  c->ksz = keysz(o->gbits/8, c->c->keysz);
+  c->ksz = keysz(o->fbits/8, c->c->keysz);
+  if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8))
+    die(1, "bad key size %u for %s", o->fbits, o->name);
   c->k = xmalloc(c->ksz);
   rand_get(RAND_GLOBAL, c->k, c->ksz);
   return (c);
@@ -525,13 +536,16 @@ static void *enc_init(opts *o)
     die(1, "must specify encryption scheme name");
   if ((cc = gcipher_byname(o->name)) == 0)
     die(1, "encryption scheme `%s' not known", o->name);
-  ksz = keysz(0, cc->keysz);
+  ksz = keysz(o->fbits/8, cc->keysz);
+  if (o->fbits%8 || (o->fbits && ksz != o->fbits/8))
+    die(1, "bad key size %u for %s", o->fbits, o->name);
   k = xmalloc(ksz);
   rand_get(RAND_GLOBAL, k, ksz);
   c->c = GC_INIT(cc, k, ksz);
   xfree(k);
   c->sz = o->gbits ? o->gbits : 65536;
   c->n = o->n ? o->n : 16;
+  o->opwhat = "byte"; o->sc = c->n*c->sz;
   c->m = xmalloc(c->sz);
   return (c);
 }
@@ -562,6 +576,7 @@ static void *hash_init(opts *o)
     die(1, "hash function `%s' not known", o->name);
   c->sz = o->gbits ? o->gbits : 65536;
   c->n = o->n ? o->n : 16;
+  o->opwhat = "byte"; o->sc = c->n*c->sz;
   c->m = xmalloc(c->sz);
   return (c);
 }
@@ -596,6 +611,7 @@ static void *poly1305_jobinit(opts *o)
   rand_get(RAND_GLOBAL, c->s, sizeof(c->s));
   c->sz = o->gbits ? o->gbits : 65536;
   c->n = o->n ? o->n : 16;
+  o->opwhat = "byte"; o->sc = c->n*c->sz;
   c->m = xmalloc(c->sz);
   return (c);
 }
@@ -644,6 +660,73 @@ static const jobops jobtab[] = {
   { 0,                         0,                      0 }
 };
 
+/*----- Cycle counting ----------------------------------------------------*/
+
+typedef kludge64 cycles;
+static int cyclecount_active_p = 0;
+
+#if defined(__GNUC__) && (CPUFAM_X86 || CPUFAM_AMD64)
+
+static void init_cyclecount(void) { cyclecount_active_p = 1; }
+
+static cycles cyclecount(void)
+{
+  uint32 lo, hi;
+  kludge64 cy;
+
+  __asm__("rdtsc" : "=a"(lo), "=d"(hi));
+  SET64(cy, hi, lo);
+  return cy;
+}
+
+#elif defined(HAVE_LINUX_PERF_EVENT_H) && defined(HAVE_UINT64)
+
+static int perf_fd = -1;
+
+static void init_cyclecount(void)
+{
+  struct perf_event_attr attr = { 0 };
+
+  attr.type = PERF_TYPE_HARDWARE;
+  attr.size = sizeof(attr);
+  attr.config = PERF_COUNT_HW_CPU_CYCLES;
+  attr.disabled = 0;
+  attr.exclude_kernel = 1;
+  attr.exclude_hv = 1;
+
+  if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0)
+    moan("failed to open perf event: %s", strerror(errno));
+  else
+    cyclecount_active_p = 1;
+}
+
+static cycles cyclecount(void)
+{
+  kludge64 cy;
+  ssize_t n;
+
+  if (!cyclecount_active_p)
+    goto fail;
+  else if ((n = read(perf_fd, &cy.i, sizeof(cy.i))) != sizeof(cy.i)) {
+    if (n < 0) moan("error reading perf event: %s", strerror(errno));
+    else moan("unexpected short read from perf event");
+    cyclecount_active_p = 0; close(perf_fd); perf_fd = -1;
+    goto fail;
+  }
+end:
+  return (cy);
+fail:
+  SET64(cy, 0, 0);
+  goto end;
+}
+
+#else
+
+static void init_cyclecount(void) { cyclecount_active_p = 0; }
+static cycles cyclecount(void) { kludge64 cy; SET64(cy, 0, 0); return (cy); }
+
+#endif
+
 /*----- Main code ---------------------------------------------------------*/
 
 void version(FILE *fp)
@@ -672,12 +755,14 @@ Options:\n\
 -l, --list [ITEM...]   List all the various names of things.\n\
 \n\
 -C, --name=NAME                Select curve/DH-group/enc/hash name.\n\
--b, --field-bits       Field size for g-prime and rsa.\n\
+-b, --field-bits       Field size for g-prime and rsa;\n\
+                         key bits for ksched and enc.\n\
 -q, --no-check         Don't check field/group for validity.\n\
--B, --group-bits       Group size for g-prime; key size for ksched;\n\
-                         data size for enc and hash.\n\
--n, --factors=COUNT    Number of factors for {exp,mul}-sim.\n\
+-B, --group-bits       Group size for g-prime; data size for enc and hash.\n\
+-n, --factors=COUNT    Number of factors for {exp,mul}-sim;\n\
+                         inner iterations for enc and hash.\n\
 -i, --intervals=COUNT  Number of intervals to run for.  [0; forever]\n\
+-k, --batch=COUNT      Number of operations to batch between timer checks.\n\
 -t, --time=TIME                Length of an interval in seconds.  [1]\n\
 ");
 }
@@ -734,15 +819,16 @@ int main(int argc, char *argv[])
   opts o = { 0 };
   const jobops *j;
   struct timeval tv_next, tv_now;
-  double t, ttot;
-  unsigned n;
+  double t, ttot, cy, cytot;
+  unsigned n, k;
   unsigned long ii;
-  clock_t c_start, c_stop;
+  clock_t c0, c1;
+  kludge64 cy0, cy1, cydiff;
   double itot;
   void *p;
 
   ego(argv[0]);
-  o.t = 1;
+  o.t = 1; o.k = 1; o.sc = 1; o.opwhat = "op";
   for (;;) {
     static const struct option opts[] = {
       { "help",                0,              0,      'h' },
@@ -754,13 +840,14 @@ int main(int argc, char *argv[])
       { "group-bits",  OPTF_ARGREQ,    0,      'B' },
       { "factors",     OPTF_ARGREQ,    0,      'n' },
       { "intervals",   OPTF_ARGREQ,    0,      'i' },
+      { "batch",       OPTF_ARGREQ,    0,      'k' },
       { "public-exponent", OPTF_ARGREQ, 0,     'e' },
       { "time",                OPTF_ARGREQ,    0,      't' },
       { "no-check",    0,              0,      'q' },
       { 0,             0,              0,      0 }
     };
 
-    i = mdwopt(argc, argv, "hvulC:b:B:n:i:e:t:q", opts, 0, 0, 0);
+    i = mdwopt(argc, argv, "hvulC:b:B:n:i:k:e:t:q", opts, 0, 0, 0);
     if (i < 0) break;
     switch (i) {
       case 'h': help(stdout); exit(0);
@@ -778,6 +865,7 @@ int main(int argc, char *argv[])
        break;
       case 'i': o.i = uarg("interval count", optarg); break;
       case 't': o.t = farg("interval length", optarg); break;
+      case 'k': o.k = uarg("batch size", optarg); break;
       case 'q': o.f |= OF_NOCHECK; break;
       default: usage(stderr); exit(1);
     }
@@ -790,23 +878,29 @@ int main(int argc, char *argv[])
   p = j->init(&o);
 
   n = 0;
-  ttot = itot =         0;
+  ttot = itot = 0; cytot = 0; init_cyclecount();
   gettimeofday(&tv_now, 0);
   do {
     tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
     ii = 0;
-    c_start = clock();
+    c0 = clock(); cy0 = cyclecount();
     do {
-      j->run(p);
-      ii++;
+      for (k = 0; k < o.k; k++) { j->run(p); }
+      ii += k;
       gettimeofday(&tv_now, 0);
     } while (TV_CMP(&tv_now, <, &tv_next));
-    c_stop = clock();
-    t = (double)(c_stop - c_start)/CLOCKS_PER_SEC;
-    itot += ii;
-    ttot += t;
-    printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
+    cy1 = cyclecount(); c1 = clock();
+    t = (double)(c1 - c0)/CLOCKS_PER_SEC;
+    itot += ii; ttot += t;
+    printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f",
           n, ii, ii/t, itot/ttot);
+    if (cyclecount_active_p) {
+      SUB64(cydiff, cy1, cy0); cy = LO64(cydiff) + ldexp(HI64(cydiff), 32);
+      cytot += cy;
+      printf(" (cy/%s = %3f; avg cy/%s = %3f)",
+            o.opwhat, cy/ii/o.sc, o.opwhat, cytot/itot/o.sc);
+    }
+    putchar('\n');
     fflush(stdout);
     n++;
   } while (!o.i || n < o.i);