source: benchmark/bench.h @ a8367eb

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since a8367eb was ce9f9d4, checked in by caparsons <caparson@…>, 3 years ago

added mutexstmt benchmark to make

  • Property mode set to 100644
File size: 1.3 KB
Line 
1#pragma once
2
3#if defined(__cforall)
4extern "C" {
5#endif
6        #include <stdlib.h>
7        #include <stdint.h>                             // uint64_t
8        #include <unistd.h>                             // sysconf
9#if ! defined(__cforall)
10        #include <time.h>
11        #include <sys/time.h>
12#else
13}
14#include <time.hfa>
15#endif
16
17
18static inline uint64_t bench_time() {
19        struct timespec ts;
20        clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
21        return 1000000000LL * ts.tv_sec + ts.tv_nsec;
22} // bench_time
23
24
25#if defined(__cforall)
26struct test_spinlock {
27        volatile bool lock;
28};
29
30static inline void lock( test_spinlock & this ) {
31        for ( ;; ) {
32                if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
33        }
34}
35
36static inline void unlock( test_spinlock & this ) {
37        __atomic_clear( &this.lock, __ATOMIC_RELEASE );
38}
39#endif
40
41#ifndef BENCH_N
42#define BENCH_N 10000000
43#endif
44
45size_t times = BENCH_N;
46
47#define BENCH_START()                           \
48        if ( argc > 2 ) exit( EXIT_FAILURE );   \
49        if ( argc == 2 ) {                      \
50                times = atoi( argv[1] );        \
51        }
52
53#define BENCH(statement, output)                \
54        uint64_t StartTime, EndTime;            \
55        StartTime = bench_time();               \
56        statement;                              \
57        EndTime = bench_time();                 \
58        double output = (double)( EndTime - StartTime ) / times;
59
60
61#if defined(__cforall)
62Duration default_preemption() {
63        return 0;
64}
65#endif
66#if defined(__U_CPLUSPLUS__)
67unsigned int uDefaultPreemption() {
68        return 0;
69}
70#endif
Note: See TracBrowser for help on using the repository browser.