Added SIOP_FLAG_KERNEL_DRIVER_ONLY to correspond to CRYPTO_ALG_KERN_DRIVER_ONLY if...
[cryptodev-linux.git] / lib / benchmark.c
blob37e9adedf669a4b018d749b3fecadefbf54e959e
1 /*
2 * Copyright (C) 2011 Free Software Foundation, Inc.
4 * This file is part of GnuTLS.
6 * GnuTLS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuTLS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <signal.h>
23 #include <sys/time.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include "benchmark.h"
28 int benchmark_must_finish = 0;
30 static void
31 alarm_handler (int signo)
33 benchmark_must_finish = 1;
36 int start_benchmark(struct benchmark_st * st)
38 int ret;
39 struct itimerval timer;
41 memset(st, 0, sizeof(*st));
43 st->old_handler = signal (SIGPROF, alarm_handler);
45 ret = gettimeofday (&st->start, NULL);
46 if (ret < 0) {
47 perror("gettimeofday");
48 return -1;
51 benchmark_must_finish = 0;
53 memset(&timer, 0, sizeof(timer));
54 timer.it_value.tv_sec = 0;
55 timer.it_value.tv_usec = 100*1000;
57 ret = setitimer(ITIMER_PROF, &timer, NULL);
58 if (ret < 0) {
59 perror("setitimer");
60 return -1;
63 return 0;
66 /* Returns -1 on error or 0 on success.
67 * elapsed: the elapsed time in milliseconds
69 int stop_benchmark(struct benchmark_st * st, unsigned long * elapsed)
71 unsigned long msecs;
72 struct timeval stop;
73 int ret;
75 signal(SIGPROF, st->old_handler);
77 ret = gettimeofday (&stop, NULL);
78 if (ret < 0)
79 return -1;
81 msecs = (stop.tv_sec * 1000 + stop.tv_usec / 1000 -
82 (st->start.tv_sec * 1000 + st->start.tv_usec / (1000)));
84 if (elapsed) *elapsed = msecs;
86 return 0;