chiark / gitweb /
doc/: Switch to a manually maintained bibliography database.
[sod] / test / kwbench.c
1 /* -*-c-*-
2  *
3  * Keyword benchmark, frontend
4  *
5  * (c) 2019 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of the Sensible Object Design, an object system for C.
11  *
12  * SOD is free software: you can redistribute it and/or modify it under
13  * the terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 2 of the License, or (at your
15  * option) any later version.
16  *
17  * SOD is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20  * for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with SOD.  If not, write to the Free Software Foundation, Inc.,
24  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #include <stdint.h>
28 #include <stdio.h>
29
30 #include "keyword.h"
31 #include "kwbench.h"
32
33 #if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__))
34 #define TIMEWHAT "cy"
35 typedef unsigned long long timer;
36 static void init_timing(void) { ; }
37 static timer when(void)
38 {
39   uint32_t lo, hi;
40
41   __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
42   return (((timer)hi << 32) | lo);
43 }
44 #elif defined(__linux__)
45 #include <linux/perf_event.h>
46 #include <asm/unistd.h>
47 #include <unistd.h>
48
49 #define TIMEWHAT "cy"
50 typedef uint64_t timer;
51 static int perf_fd = -1;
52 static void init_timing(void)
53 {
54   struct perf_event_attr attr = { 0 };
55
56   attr.type = PERF_TYPE_HARDWARE;
57   attr.size = sizeof(attr);
58   attr.config = PERF_COUNT_HW_CPU_CYCLES;
59   attr.disabled = 0;
60   attr.exclude_kernel = 1;
61   attr.exclude_hv = 1;
62
63   if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0)
64     perror("failed to open perf event");
65 }
66 static timer when(void)
67 {
68   timer cy;
69   ssize_t n;
70
71   if (perf_fd == -1) goto fail;
72   n = read(perf_fd, &cy, sizeof(cy));
73   if (n != sizeof(cy)) {
74     if (n < 0) perror("failed to read perf event");
75     else fprintf(stderr, "unexpected short read from perf event\n");
76     close(perf_fd); perf_fd = -1;
77     goto fail;
78   }
79   return (cy);
80 fail:
81   return (0);
82 }
83 #else
84 #include <time.h>
85 #define TIMEWHAT "ns"
86 typedef double timer;
87 static void init_timing(void) { ; }
88 static timer when(void)
89 {
90   struct timespec tv;
91   clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tv);
92   return (1e9*(timer)tv.tv_sec + tv.tv_nsec);
93 }
94 #endif
95
96 #define NITER 100000000
97
98 int main(void)
99 {
100   timer t0, t1;
101   double t;
102   int i;
103   struct myobject obj;
104
105   init_timing();
106
107   t0 = when();
108   for (i = 0; i < NITER; i++) kwbench_trivial(&obj, 4, 0, 0);
109   t1 = when();
110   t = t1 - t0; printf("trivial: %g "TIMEWHAT"\n", t/NITER);
111
112   t0 = when();
113   for (i = 0; i < NITER; i++)
114     kwbench_keywords(&obj, KWARGS(K(z, 19)));
115   t1 = when();
116   t = t1 - t0; printf("keywords: %g "TIMEWHAT"\n", t/NITER);
117
118   return (0);
119 }
120
121 /*----- That's all, folks -------------------------------------------------*/