getcycles.c 4.54 KB
Newer Older
Martin Schläffer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#include <stdio.h>
#include <stdlib.h>

#include "api.h"
#if defined(CRYPTO_AEAD)
#include "crypto_aead.h"
#elif defined(CRYPTO_HASH)
#include "crypto_hash.h"
#endif

#if !defined(__arm__) && !defined(_M_ARM)
#ifndef NDEBUG
#pragma message("Using RDTSC to count cycles")
#endif
#ifdef _MSC_VER
#include <intrin.h>
#define ALIGN(x)
#else
#include <x86intrin.h>
#define ALIGN(x) __attribute__((aligned(x)))
#endif
#define init_cpucycles()
#define cpucycles(cycles) cycles = __rdtsc()
//#define cpucycles(cycles) cycles = __rdtscp(&tmp)
#endif

#if defined(__ARM_ARCH_6__) || __ARM_ARCH == 6 || _M_ARM == 6
#define ALIGN(x) __attribute__((aligned(x)))
#ifndef NDEBUG
#pragma message("Using ARMv6 PMU to count cycles")
#endif
#define init_cpucycles() \
  __asm__ __volatile__("mcr p15, 0, %0, c15, c12, 0" ::"r"(1))
#define cpucycles(cycles) \
  __asm__ __volatile__("mrc p15, 0, %0, c15, c12, 1" : "=r"(cycles))
#elif defined(__arm__) || defined(_M_ARM)
#define ALIGN(x) __attribute__((aligned(x)))
#ifndef NDEBUG
#pragma message("Using ARMv7 PMU to count cycles")
#endif
#define init_cpucycles()                                                \
  __asm__ __volatile__("mcr p15, 0, %0, c9, c12, 0" ::"r"(17));         \
  __asm__ __volatile__("mcr p15, 0, %0, c9, c12, 1" ::"r"(0x8000000f)); \
  __asm__ __volatile__("mcr p15, 0, %0, c9, c12, 3" ::"r"(0x8000000f))
#define cpucycles(cycles) \
  __asm__ __volatile__("mrc p15, 0, %0, c9, c13, 0" : "=r"(cycles))
#endif

#define NUM_RUNS 16
#define NUM_BYTES 32768
#define MAX_LEN 32768
#define NUM_MLENS 7

unsigned long long mlens[] = {1, 8, 16, 32, 64, 1536, 32768};
unsigned char ALIGN(16) m[MAX_LEN];
#if defined(CRYPTO_AEAD)
unsigned long long alen = 0;
unsigned long long clen = 0;
unsigned char ALIGN(16) a[MAX_LEN];
unsigned char ALIGN(16) c[MAX_LEN + CRYPTO_ABYTES];
unsigned char ALIGN(16) nsec[CRYPTO_NSECBYTES ? CRYPTO_NSECBYTES : 1];
unsigned char ALIGN(16) npub[CRYPTO_NPUBBYTES ? CRYPTO_NPUBBYTES : 1];
unsigned char ALIGN(16) k[CRYPTO_KEYBYTES];
#elif defined(CRYPTO_HASH)
unsigned char ALIGN(16) h[CRYPTO_BYTES];
#endif

unsigned long long cycles[NUM_MLENS][NUM_RUNS * 2];
unsigned int tmp;

void init_input() {
  int i;
  for (i = 0; i < MAX_LEN; ++i) m[i] = rand();
#if defined(CRYPTO_AEAD)
  for (i = 0; i < MAX_LEN; ++i) a[i] = rand();
  for (i = 0; i < CRYPTO_KEYBYTES; ++i) k[i] = rand();
  for (i = 0; i < CRYPTO_NPUBBYTES; ++i) npub[i] = rand();
#endif
}

unsigned long long measure(unsigned long long mlen) {
  unsigned long long NREPS = NUM_BYTES / mlen;
  unsigned long long i;
#if defined(__arm__) || defined(_M_ARM)
  unsigned int before, after;
#else
  unsigned long long before, after;
#endif
  init_input();
  cpucycles(before);
  for (i = 0; i < NREPS; ++i)
#if defined(CRYPTO_AEAD)
    crypto_aead_encrypt(c, &clen, m, mlen, a, alen, nsec, npub, k);
#elif defined(CRYPTO_HASH)
    crypto_hash(h, m, mlen);
#endif
  cpucycles(after);
  return after - before;
}

int compare_uint64(const void* first, const void* second) {
  const unsigned long long* ia = (const unsigned long long*)first;
  const unsigned long long* ib = (const unsigned long long*)second;
  if (*ia > *ib) return 1;
  if (*ia < *ib) return -1;
  return 0;
}

int main(int argc, char* argv[]) {
  unsigned long long i, j;
  double factor = 1.0;
  if (argc == 2) factor = atof(argv[1]);

  init_cpucycles();

  for (i = 0; i < NUM_MLENS; ++i) {
    for (j = 0; j < NUM_RUNS; ++j) cycles[i][j] = measure(mlens[i]);
    qsort(cycles[i], NUM_RUNS, sizeof(unsigned long long), &compare_uint64);
  }

  printf("\nsorted cycles:\n");
  for (i = 0; i < NUM_MLENS; ++i) {
    unsigned long long NREPS = NUM_BYTES / mlens[i];
    printf("%5d: ", (int)mlens[i]);
    for (j = 0; j < NUM_RUNS; ++j) printf("%d ", (int)(cycles[i][j] / NREPS));
    printf("\n");
  }

  printf("\ncycles per byte (min,median):\n");
  for (i = 0; i < NUM_MLENS; ++i) {
    unsigned long long NREPS = NUM_BYTES / mlens[i];
    unsigned long long bytes = mlens[i] * NREPS;
    printf("%5d: %6.1f %6.1f\n", (int)mlens[i],
           factor * cycles[i][0] / bytes + 0.05,
           factor * cycles[i][NUM_RUNS / 2] / bytes + 0.05);
  }
  printf("\n");

  for (i = 0; i < NUM_MLENS; ++i) printf("| %5d ", (int)mlens[i]);
  printf("|\n");
  for (i = 0; i < NUM_MLENS; ++i) printf("|------:");
  printf("|\n");
  for (i = 0; i < NUM_MLENS; ++i) {
    unsigned long long NREPS = NUM_BYTES / mlens[i];
    unsigned long long bytes = mlens[i] * NREPS;
    if (mlens[i] <= 32)
      printf("| %5.0f ", factor * cycles[i][0] / bytes + 0.5);
    else
      printf("| %5.1f ", factor * cycles[i][0] / bytes + 0.05);
  }
  printf("|\n");

  return 0;
}