2 * Copyright 2009 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * This file was originally written by Colin Percival as part of the Tarsnap
27 * online backup system.
29 #include "scrypt_platform.h"
37 #include "crypto_scrypt.h"
39 #include "scryptenc_cpuperf.h"
41 #ifdef HAVE_CLOCK_GETTIME
43 static clock_t clocktouse;
46 getclockres(double * resd)
51 * Try clocks in order of preference until we find one which works.
52 * (We assume that if clock_getres works, clock_gettime will, too.)
53 * The use of if/else/if/else/if/else rather than if/elif/elif/else
54 * is ugly but legal, and allows us to #ifdef things appropriately.
57 if (clock_getres(CLOCK_VIRTUAL, &res) == 0)
58 clocktouse = CLOCK_VIRTUAL;
61 #ifdef CLOCK_MONOTONIC
62 if (clock_getres(CLOCK_MONOTONIC, &res) == 0)
63 clocktouse = CLOCK_MONOTONIC;
66 if (clock_getres(CLOCK_REALTIME, &res) == 0)
67 clocktouse = CLOCK_REALTIME;
71 /* Convert clock resolution to a double. */
72 *resd = res.tv_sec + res.tv_nsec * 0.000000001;
78 getclocktime(struct timespec * ts)
81 if (clock_gettime(clocktouse, ts))
89 getclockres(double * resd)
92 *resd = 1.0 / CLOCKS_PER_SEC;
98 getclocktime(struct timespec * ts)
102 if (gettimeofday(&tv, NULL))
104 ts->tv_sec = tv.tv_sec;
105 ts->tv_nsec = tv.tv_usec * 1000;
112 getclockdiff(struct timespec * st, double * diffd)
116 if (getclocktime(&en))
118 *diffd = (en.tv_nsec - st->tv_nsec) * 0.000000001 +
119 (en.tv_sec - st->tv_sec);
125 * scryptenc_cpuperf(opps):
126 * Estimate the number of salsa20/8 cores which can be executed per second,
127 * and return the value via opps.
130 scryptenc_cpuperf(double * opps)
136 /* Get the clock resolution. */
137 if (getclockres(&resd))
141 fprintf(stderr, "Clock resolution is %f\n", resd);
144 /* Loop until the clock ticks. */
145 if (getclocktime(&st))
149 if (crypto_scrypt(NULL, 0, NULL, 0, 16, 1, 1, NULL, 0))
152 /* Has the clock ticked? */
153 if (getclockdiff(&st, &diffd))
159 /* Could how many scryps we can do before the next tick. */
160 if (getclocktime(&st))
164 if (crypto_scrypt(NULL, 0, NULL, 0, 128, 1, 1, NULL, 0))
167 /* We invoked the salsa20/8 core 512 times. */
170 /* Check if we have looped for long enough. */
171 if (getclockdiff(&st, &diffd))
178 fprintf(stderr, "%ju salsa20/8 cores performed in %f seconds\n",
179 (uintmax_t)i, diffd);
182 /* We can do approximately i salsa20/8 cores per diffd seconds. */