forked from jsmithlabs/supercop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure-anything.c
181 lines (159 loc) · 3.66 KB
/
measure-anything.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* measure-anything.c version 20170713
* D. J. Bernstein
* Public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/resource.h>
#include "kernelrandombytes.h"
#include "cpucycles.h"
#include "cpuid.h"
#include "measure.h"
static void printword(const char *s)
{
if (!*s) putchar('-');
while (*s) {
if (*s == ' ') putchar('_');
else if (*s == '\t') putchar('_');
else if (*s == '\r') putchar('_');
else if (*s == '\n') putchar('_');
else putchar(*s);
++s;
}
putchar(' ');
}
static void printnum(long long x)
{
printf("%lld ",x);
}
static void fail(const char *why)
{
fprintf(stderr,"measure: fatal: %s\n",why);
exit(111);
}
unsigned char *alignedcalloc(unsigned long long len)
{
unsigned char *x = (unsigned char *) calloc(1,len + 128);
if (!x) fail("out of memory");
/* will never deallocate so shifting is ok */
x += 63 & (-(unsigned long) x);
return x;
}
static long long cyclespersecond;
static void printimplementations(void)
{
int i;
printword("implementation");
printword(primitiveimplementation);
printword(implementationversion);
printf("\n"); fflush(stdout);
for (i = 0;sizenames[i];++i) {
printword(sizenames[i]);
printnum(sizes[i]);
printf("\n"); fflush(stdout);
}
printword("cpuid");
printword(cpuid);
printf("\n"); fflush(stdout);
printword("cpucycles_persecond");
printnum(cyclespersecond);
printf("\n"); fflush(stdout);
printword("cpucycles_implementation");
printword(cpucycles_implementation);
printf("\n"); fflush(stdout);
printword("compiler");
printword(COMPILER);
#if defined(__VERSION__) && !defined(__ICC)
printword(__VERSION__);
#elif defined(__xlc__)
printword(__xlc__);
#elif defined(__ICC)
{
char buf[256];
sprintf(buf, "%d.%d.%d", __ICC/100, __ICC%100,
__INTEL_COMPILER_BUILD_DATE);
printword(buf);
}
#elif defined(__PGIC__)
{
char buf[256];
sprintf(buf, "%d.%d.%d", __PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__);
printword(buf);
}
#elif defined(__SUNPRO_C)
{
char buf[256];
int major, minor, micro;
micro = __SUNPRO_C & 0xf;
minor = (__SUNPRO_C >> 4) & 0xf;
major = (__SUNPRO_C >> 8) & 0xf;
if (micro)
sprintf(buf, "%d.%d.%d", major, minor, micro);
else
sprintf(buf, "%d.%d", major, minor);
printword(buf);
}
#else
printword("unknown compiler version");
#endif
printf("\n"); fflush(stdout);
}
void printentry(long long mbytes,const char *measuring,long long *m,long long mlen)
{
long long i;
long long j;
long long belowj;
long long abovej;
printword(measuring);
if (mbytes >= 0) printnum(mbytes); else printword("");
if (mlen > 0) {
for (j = 0;j + 1 < mlen;++j) {
belowj = 0;
for (i = 0;i < mlen;++i) if (m[i] < m[j]) ++belowj;
abovej = 0;
for (i = 0;i < mlen;++i) if (m[i] > m[j]) ++abovej;
if (belowj * 2 < mlen && abovej * 2 < mlen) break;
}
printnum(m[j]);
if (mlen > 1) {
for (i = 0;i < mlen;++i) printnum(m[i]);
}
}
printf("\n"); fflush(stdout);
}
void limits()
{
#ifdef RLIM_INFINITY
struct rlimit r;
r.rlim_cur = 0;
r.rlim_max = 0;
#ifdef RLIMIT_NOFILE
setrlimit(RLIMIT_NOFILE,&r);
#endif
#ifdef RLIMIT_NPROC
setrlimit(RLIMIT_NPROC,&r);
#endif
#ifdef RLIMIT_CORE
setrlimit(RLIMIT_CORE,&r);
#endif
#endif
}
static unsigned char randombyte[1];
int main()
{
cyclespersecond = cpucycles();
cyclespersecond = cpucycles();
cyclespersecond = cpucycles_persecond();
kernelrandombytes(randombyte,1);
preallocate();
limits();
printimplementations();
allocate();
measure();
return 0;
}