3088c97309460353ab00e235349550ee55bec9f3
[platform/upstream/nettle.git] / examples / timing.c
1 /* timing.c
2
3    Copyright (C) 2013 Niels Möller
4
5    This file is part of GNU Nettle.
6
7    GNU Nettle is free software: you can redistribute it and/or
8    modify it under the terms of either:
9
10      * the GNU Lesser General Public License as published by the Free
11        Software Foundation; either version 3 of the License, or (at your
12        option) any later version.
13
14    or
15
16      * the GNU General Public License as published by the Free
17        Software Foundation; either version 2 of the License, or (at your
18        option) any later version.
19
20    or both in parallel, as here.
21
22    GNU Nettle is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    General Public License for more details.
26
27    You should have received copies of the GNU General Public License and
28    the GNU Lesser General Public License along with this program.  If
29    not, see http://www.gnu.org/licenses/.
30 */
31
32 #if HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include "timing.h"
37
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44
45 #if HAVE_CLOCK_GETTIME && defined CLOCK_PROCESS_CPUTIME_ID
46 #define TRY_CLOCK_GETTIME 1
47 struct timespec cgt_start;
48
49 static void NORETURN PRINTF_STYLE(1,2)
50 die(const char *format, ...)
51 {
52   va_list args;
53   va_start(args, format);
54   vfprintf(stderr, format, args);
55   va_end(args);
56
57   exit(EXIT_FAILURE);
58 }
59
60 static int
61 cgt_works_p(void)
62 {
63   struct timespec now;
64   return clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &now) == 0;
65 }
66
67 static void
68 cgt_time_start(void)
69 {
70   if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cgt_start) < 0)
71     die("clock_gettime failed: %s\n", strerror(errno));
72 }
73
74 static double
75 cgt_time_end(void)
76 {
77   struct timespec end;
78   if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end) < 0)
79     die("clock_gettime failed: %s\n", strerror(errno));
80
81   return end.tv_sec - cgt_start.tv_sec
82     + 1e-9 * (end.tv_nsec - cgt_start.tv_nsec);
83 }
84 #endif /* !HAVE_CLOCK_GETTIME */
85
86 static clock_t clock_start;
87
88 static void
89 clock_time_start(void)
90 {
91   clock_start = clock();
92 }
93
94 static double
95 clock_time_end(void)
96 {
97   return (double) (clock() - (clock_start)) / CLOCKS_PER_SEC;
98 }
99
100 void (*time_start)(void) = clock_time_start;
101 double (*time_end)(void) = clock_time_end;
102
103 void time_init(void)
104 {
105   /* Choose timing function */
106 #if TRY_CLOCK_GETTIME
107   if (cgt_works_p())
108     {
109       time_start = cgt_time_start;
110       time_end = cgt_time_end;
111     }
112   else
113     {
114       fprintf(stderr, "clock_gettime not working, falling back to clock\n");
115       time_start = clock_time_start;
116       time_end = clock_time_end;
117     }
118 #endif
119 }