From 118ae0a780208ff68e533f57bc6f0f0f16c528a2 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sat, 10 Aug 2019 17:28:58 +0100 Subject: [PATCH] test/kwbench.c, etc.: Tidy up and publish the keyword benchmark I used. --- test/Makefile.am | 3 ++ test/kwbench-back.c | 47 ++++++++++++++++++++ test/kwbench.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test/kwbench.h | 47 ++++++++++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 test/kwbench-back.c create mode 100644 test/kwbench.c create mode 100644 test/kwbench.h diff --git a/test/Makefile.am b/test/Makefile.am index 8145746..34ca415 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -73,6 +73,9 @@ check-local:: kwtest kwtest.ref check_PROGRAMS += rat -include rat.c-dep rat.h-dep +check_PROGRAMS += kwbench +kwbench_SOURCES = kwbench.c kwbench-back.c kwbench.h + EXTRA_DIST += rat.sod rat.ref nodist_rat_SOURCES = rat.c rat.h BUILT_SOURCES += $(nodist_rat_SOURCES) diff --git a/test/kwbench-back.c b/test/kwbench-back.c new file mode 100644 index 0000000..a6d5009 --- /dev/null +++ b/test/kwbench-back.c @@ -0,0 +1,47 @@ +/* -*-c-*- + * + * Keyword benchmark, backend + * + * (c) 2019 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the Sensible Object Design, an object system for C. + * + * SOD is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * SOD is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with SOD. If not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include + +#include "keyword.h" +#include "kwbench.h" + +void kwbench_trivial(struct myobject *obj, int x, int y, int z) + { obj->x = x; obj->y = y; obj->z = z; } + +#define kwbench_KWSET(_) \ + _(int, x, 0) \ + _(int, y, 0) \ + _(int, z, 0) +KWSET_STRUCT(kwbench); +static KWSET_PARSEFN(kwbench) +KWCALL void kwbench_keywords(struct myobject *obj, KWTAIL) +{ + KWPARSE(kwbench); + obj->x = kw.x; obj->y = kw.y; obj->z = kw.z; +} + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/test/kwbench.c b/test/kwbench.c new file mode 100644 index 0000000..2a63f40 --- /dev/null +++ b/test/kwbench.c @@ -0,0 +1,121 @@ +/* -*-c-*- + * + * Keyword benchmark, frontend + * + * (c) 2019 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the Sensible Object Design, an object system for C. + * + * SOD is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * SOD is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with SOD. If not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include + +#include "keyword.h" +#include "kwbench.h" + +#if defined(__GNUC__) && (defined(__i386__) || defined(__amd64__)) +#define TIMEWHAT "cy" +typedef unsigned long long timer; +static void init_timing(void) { ; } +static timer when(void) +{ + uint32_t lo, hi; + + __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi)); + return (((timer)hi << 32) | lo); +} +#elif defined(__linux__) +#include +#include +#include + +#define TIMEWHAT "cy" +typedef uint64_t timer; +static int perf_fd = -1; +static void init_timing(void) +{ + struct perf_event_attr attr = { 0 }; + + attr.type = PERF_TYPE_HARDWARE; + attr.size = sizeof(attr); + attr.config = PERF_COUNT_HW_CPU_CYCLES; + attr.disabled = 0; + attr.exclude_kernel = 1; + attr.exclude_hv = 1; + + if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0) + perror("failed to open perf event"); +} +static timer when(void) +{ + timer cy; + ssize_t n; + + if (perf_fd == -1) goto fail; + n = read(perf_fd, &cy, sizeof(cy)); + if (n != sizeof(cy)) { + if (n < 0) perror("failed to read perf event"); + else fprintf(stderr, "unexpected short read from perf event\n"); + close(perf_fd); perf_fd = -1; + goto fail; + } + return (cy); +fail: + return (0); +} +#else +#include +#define TIMEWHAT "ns" +typedef double timer; +static void init_timing(void) { ; } +static timer when(void) +{ + struct timespec tv; + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tv); + return (1e9*(timer)tv.tv_sec + tv.tv_nsec); +} +#endif + +#define NITER 100000000 + +int main(void) +{ + timer t0, t1; + double t; + int i; + struct myobject obj; + + init_timing(); + + t0 = when(); + for (i = 0; i < NITER; i++) kwbench_trivial(&obj, 4, 0, 0); + t1 = when(); + t = t1 - t0; printf("trivial: %g "TIMEWHAT"\n", t/NITER); + + t0 = when(); + for (i = 0; i < NITER; i++) + kwbench_keywords(&obj, KWARGS(K(z, 19))); + t1 = when(); + t = t1 - t0; printf("keywords: %g "TIMEWHAT"\n", t/NITER); + + return (0); +} + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/test/kwbench.h b/test/kwbench.h new file mode 100644 index 0000000..28a4aea --- /dev/null +++ b/test/kwbench.h @@ -0,0 +1,47 @@ +/* -*-c-*- + * + * Keyword benchmark, header + * + * (c) 2019 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of the Sensible Object Design, an object system for C. + * + * SOD is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * SOD is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with SOD. If not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef KWBENCH_H +#define KWBENCH_H + +#ifdef __cplusplus + extern "C" { +#endif + +#include "keyword.h" + +struct myobject { int x, y, z; }; + +extern void kwbench_trivial(struct myobject */*obj*/, + int /*x*/, int /*y*/, int /*z*/); + +extern KWCALL void kwbench_keywords(struct myobject */*obj*/, KWTAIL); + +#ifdef __cplusplus + } +#endif + +#endif -- 2.11.0