source: doc/theses/colby_parsons_MMAth/benchmarks/mutex_stmt/bench.h @ 6e6989c

ADTast-experimental
Last change on this file since 6e6989c was 6e6989c, checked in by caparsons <caparson@…>, 14 months ago

added mutex stmt benchmarking

  • Property mode set to 100644
File size: 1.5 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#define L1 l1
18#define L2 L1, l2
19#define L3 L2, l3
20#define L4 L3, l4
21#define L5 L4, l5
22#define L6 L5, l6
23#define L7 L6, l7
24#define L8 L7, l8
25
26static inline uint64_t bench_time() {
27        struct timespec ts;
28        clock_gettime( CLOCK_THREAD_CPUTIME_ID, &ts );
29        return 1000000000LL * ts.tv_sec + ts.tv_nsec;
30} // bench_time
31
32
33#if defined(__cforall)
34struct test_spinlock {
35        volatile bool lock;
36};
37
38static inline void lock( test_spinlock & this ) {
39        for ( ;; ) {
40                if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
41        }
42}
43
44static inline void unlock( test_spinlock & this ) {
45        __atomic_clear( &this.lock, __ATOMIC_RELEASE );
46}
47#endif
48
49size_t threads = 1;
50
51#define BENCH_START()                           \
52        if ( argc > 2 ) exit( EXIT_FAILURE );   \
53        if ( argc == 2 ) {                      \
54                threads = atoi( argv[1] );      \
55        }
56
57#define BENCH(statement, output, done_flag)             \
58        uint64_t count = 0;             \
59    while (true) {          \
60        statement;                              \
61    count++;                \
62    if (done_flag) break; \
63    }                       \
64    __atomic_add_fetch(&output, count, __ATOMIC_SEQ_CST);
65        // EndTime = bench_time();                      \
66        // double output = (double)( EndTime - StartTime ) / times;
67
68
69#if defined(__cforall)
70Duration default_preemption() {
71        return 0;
72}
73#endif
74#if defined(__U_CPLUSPLUS__)
75unsigned int uDefaultPreemption() {
76        return 0;
77}
78#endif
Note: See TracBrowser for help on using the repository browser.