-
Notifications
You must be signed in to change notification settings - Fork 0
/
linden_common.h
124 lines (98 loc) · 2.3 KB
/
linden_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef COMMON_H
#define COMMON_H
#define _GNU_SOURCE
#include <inttypes.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#include "gsl/rng/gsl_rng.h"
#include "fraser.h"
#if defined(__linux__)
#include <time.h>
#include <sched.h>
#include <sys/syscall.h>
#endif
#if defined(__APPLE__)
#include <mach/mach_time.h>
#endif
#define DCL_ALIGN __attribute__((aligned (2*CACHE_LINE_SIZE)))
#define CACHELINE __attribute__((aligned (1*CACHE_LINE_SIZE)))
#define ATPAGESIZE __attribute__((aligned (PAGESIZE)))
#define SQR(x) (x)*(x)
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
typedef struct barrier {
pthread_cond_t complete;
pthread_mutex_t mutex;
int count;
int crossing;
} barrier_t;
typedef struct thread_args_s
{
pthread_t thread;
int id;
gsl_rng *rng;
int measure;
int cycles;
char pad[128];
} thread_args_t;
#define E(c) \
do { \
int _c = (c); \
if (_c < 0) { \
fprintf(stderr, "E: %s: %d: %s\n", \
__FILE__, __LINE__, #c); \
} \
} while (0)
#define E_en(c) \
do { \
int _c = (c); \
if (_c != 0) { \
fprintf(stderr, strerror(_c)); \
} \
} while (0)
#define E_NULL(c) \
do { \
if ((c) == NULL) { \
perror("E_NULL"); \
} \
} while (0)
#if defined(__x86_64__)
/* accurate time measurements on late recent cpus */
static inline uint64_t __attribute__((always_inline))
read_tsc_p()
{
uint64_t tsc;
__asm__ __volatile__ ("rdtscp\n"
"shl $32, %%rdx\n"
"or %%rdx, %%rax"
: "=a"(tsc)
:
: "%rcx", "%rdx");
return tsc;
}
#define IMB() __asm__ __volatile__("mfence":::"memory")
#define IRMB() __asm__ __volatile__("lfence":::"memory")
#define IWMB() __asm__ __volatile__("sfence":::"memory")
#else
#error Unsupported architecture
#endif // __x86_64__
#if defined(__linux__)
extern pid_t gettid(void);
#ifdef LINDEN_PIN
extern void pin(pid_t t, int cpu);
#endif
#endif
extern void gettime(struct timespec *t);
extern struct timespec timediff(struct timespec, struct timespec);
#endif