xntpd 3.3b from UDel
[unix-history] / contrib / xntpd / authstuff / authspeed.c
CommitLineData
09169146
GW
1/* authspeed.c,v 3.1 1993/07/06 01:04:54 jbj Exp
2 * authspeed - figure out how LONG it takes to do an NTP encryption
3 */
4
5#if defined(SYS_HPUX) || defined(SYS_AUX3) || defined(SYS_AUX2) || defined(SOLARIS) || defined(SYS_SVR4) || defined(SYS_PTX)
6#define FAKE_RUSAGE
7#endif
8
9#include <stdio.h>
10#include <sys/types.h>
11#include <sys/time.h>
12#include <sys/resource.h>
13#ifdef FAKE_RUSAGE
14#include <sys/param.h>
15#include <sys/times.h>
16#endif
17
18#include "ntp_fp.h"
19#include "ntp_stdlib.h"
20
21#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
22
23#define DEFLOOPS -1
24
25#define DEFDELAYLOOPS 20000
26#define DEFCOSTLOOPS 2000
27
28char *progname;
29int debug;
30
31struct timeval tstart, tend;
32#ifdef FAKE_RUSAGE
33struct tms rstart, rend;
34#define getrusage(foo, t) times(t)
35#define RUSAGE_SELF 0
36#else
37struct rusage rstart, rend;
38#endif
39
40l_fp dummy1, dummy2;
41U_LONG dummy3;
42
43U_LONG pkt[15];
44
45int totalcost = 0;
46double rtime;
47double vtime;
48
49int domd5 = 0;
50
51static void dodelay P((int));
52static void docheap P((int));
53static void docost P((int));
54static void subtime P((struct timeval *, struct timeval *, double *));
55
56/*
57 * main - parse arguments and handle options
58 */
59void
60main(argc, argv)
61int argc;
62char *argv[];
63{
64 int c;
65 int loops;
66 int i;
67 int errflg = 0;
68 extern int optind;
69 extern char *optarg;
70
71 progname = argv[0];
72 loops = DEFLOOPS;
73 while ((c = getopt_l(argc, argv, "cdmn:")) != EOF)
74 switch (c) {
75 case 'c':
76 totalcost++;
77 break;
78 case 'd':
79 ++debug;
80 break;
81 case 'm':
82 domd5 = 16; /* offset into list of keys */
83 break;
84 case 'n':
85 loops = atoi(optarg);
86 if (loops <= 0) {
87 (void) fprintf(stderr,
88 "%s: %s is unlikely to be a useful number of loops\n",
89 progname, optarg);
90 errflg++;
91 }
92 break;
93 default:
94 errflg++;
95 break;
96 }
97 if (errflg || optind == argc) {
98 (void) fprintf(stderr,
99 "usage: %s [-d] [-n loops] [ -c ] auth.samplekeys\n",
100 progname);
101 exit(2);
102 }
103 printf("Compute timing for ");
104 if (domd5)
105 printf("MD5");
106 else
107 printf("DES");
108 printf(" based authentication.\n");
109
110 init_auth();
111 authreadkeys(argv[optind]);
112 for (i = 0; i < 16; i++) {
113 if (!auth_havekey(i + domd5)) {
114 errflg++;
115 (void) fprintf(stderr, "%s: key %d missing\n",
116 progname, i + domd5);
117 }
118 }
119
120 if (errflg) {
121 (void) fprintf(stderr,
122 "%s: check syslog for errors, or use file with complete set of keys\n",
123 progname);
124 exit(1);
125 }
126
127 if (loops == DEFLOOPS) {
128 if (totalcost)
129 loops = DEFCOSTLOOPS;
130 else
131 loops = DEFDELAYLOOPS;
132 }
133
134 dummy1.l_ui = 0x80808080;
135 dummy1.l_uf = 0xffffff00;
136 dummy3 = 0x0aaaaaaa;
137
138 for (i = 0; i < 12; i++)
139 pkt[i] = i * 0x22222;
140
141 if (totalcost) {
142 if (totalcost > 1)
143 docheap(loops);
144 else
145 docost(loops);
146 } else {
147 dodelay(loops);
148 }
149
150 printf("total real time: %.3f\n", rtime);
151 printf("total CPU time: %.3f\n", vtime);
152 if (totalcost) {
153 printf("real cost (in seconds): %.6f\n",
154 rtime/(double)loops);
155 printf("CPU cost (in seconds): %.6f\n",
156 vtime/(double)loops);
157 printf("\nThis includes the cost of a decryption plus the\n");
158 printf("the cost of an encryption, i.e. the cost to process\n");
159 printf("a single authenticated packet.\n");
160 } else {
161 printf("authdelay in the configuration file\n");
162 printf("real authentication delay: %.6f\n",
163 rtime/(double)loops);
164 printf("authentication delay in CPU time: %.6f\n",
165 vtime/(double)loops);
166 printf("\nThe CPU delay is probably the best bet for\n");
167 printf("authdelay in the configuration file\n");
168 }
169 exit(0);
170}
171
172
173/*
174 * dodelay - do the delay measurement
175 */
176static void
177dodelay(loops)
178 int loops;
179{
180 double vtime1, rtime1, vtime2, rtime2;
181 register int loopcount;
182 /*
183 * If we're attempting to compute the cost of an auth2crypt()
184 * for first compute the total cost, then compute the
185 * cost of only doing the first step, auth1crypt(). What
186 * remains is the cost of auth2crypt.
187 */
188 loopcount = loops;
189 (void) gettimeofday(&tstart, (struct timezone *)0);
190 (void) getrusage(RUSAGE_SELF, &rstart);
191
192 while (loopcount-- > 0) {
193 auth1crypt((loops & 0xf) + domd5, pkt, 48);
194 L_ADDUF(&dummy1, dummy3);
195 auth2crypt((loops & 0xf) + domd5, pkt, 48);
196 }
197
198 (void) getrusage(RUSAGE_SELF, &rend);
199 (void) gettimeofday(&tend, (struct timezone *)0);
200
201 subtime(&tstart, &tend, &rtime1);
202#ifdef FAKE_RUSAGE
203 vtime1 = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
204#else
205 subtime(&rstart.ru_utime, &rend.ru_utime, &vtime1);
206#endif
207printf("Time for full encryptions is %f rusage %f real\n", vtime1, rtime1);
208 loopcount = loops;
209 (void) gettimeofday(&tstart, (struct timezone *)0);
210 (void) getrusage(RUSAGE_SELF, &rstart);
211
212 while (loopcount-- > 0) {
213 auth1crypt((loops & 0xf) + domd5, pkt, 48);
214 }
215
216 (void) getrusage(RUSAGE_SELF, &rend);
217 (void) gettimeofday(&tend, (struct timezone *)0);
218
219 subtime(&tstart, &tend, &rtime2);
220#ifdef FAKE_RUSAGE
221 vtime2 = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
222#else
223 subtime(&rstart.ru_utime, &rend.ru_utime, &vtime2);
224#endif
225
226printf("Time for auth1crypt is %f rusage %f real\n", vtime2, rtime2);
227 vtime = vtime1 - vtime2;
228 rtime = rtime1 - rtime2;
229}
230
231
232/*
233 * docheap - do the cost measurement the cheap way
234 */
235static void
236docheap(loops)
237 register int loops;
238{
239
240 (void) authhavekey(3 + domd5);
241
242 (void) gettimeofday(&tstart, (struct timezone *)0);
243 (void) getrusage(RUSAGE_SELF, &rstart);
244
245 while (loops-- > 0) {
246 auth1crypt(3 + domd5, pkt, 48);
247 L_ADDUF(&dummy1, dummy3);
248 auth2crypt(3 + domd5, pkt, 48);
249 (void) authdecrypt(3 + domd5, pkt, 48);
250 }
251
252 (void) getrusage(RUSAGE_SELF, &rend);
253 (void) gettimeofday(&tend, (struct timezone *)0);
254
255 subtime(&tstart, &tend, &rtime);
256#ifdef FAKE_RUSAGE
257 vtime = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
258#else
259 subtime(&rstart.ru_utime, &rend.ru_utime, &vtime);
260#endif
261}
262
263
264/*
265 * docost - do the cost measurement
266 */
267static void
268docost(loops)
269 register int loops;
270{
271
272 (void) gettimeofday(&tstart, (struct timezone *)0);
273 (void) getrusage(RUSAGE_SELF, &rstart);
274
275 while (loops-- > 0) {
276 auth1crypt((loops & 0xf) + domd5, pkt, 48);
277 L_ADDUF(&dummy1, dummy3);
278 auth2crypt((loops & 0xf) + domd5, pkt, 48);
279 (void) authdecrypt(((loops+1) & 0xf) + domd5, pkt, 48);
280 }
281
282 (void) getrusage(RUSAGE_SELF, &rend);
283 (void) gettimeofday(&tend, (struct timezone *)0);
284
285 subtime(&tstart, &tend, &rtime);
286#ifdef FAKE_RUSAGE
287 vtime = (rend.tms_utime - rstart.tms_utime) * 1.0 / HZ;
288#else
289 subtime(&rstart.ru_utime, &rend.ru_utime, &vtime);
290#endif
291}
292
293
294/*
295 * subtime - subtract two struct timevals, return double result
296 */
297static void
298subtime(tvs, tve, res)
299 struct timeval *tvs, *tve;
300 double *res;
301{
302 LONG sec;
303 LONG usec;
304
305 sec = tve->tv_sec - tvs->tv_sec;
306 usec = tve->tv_usec - tvs->tv_usec;
307
308 if (usec < 0) {
309 usec += 1000000;
310 sec--;
311 }
312
313 *res = (double)sec + (double)usec/1000000.;
314 return;
315}