-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.h
119 lines (106 loc) · 2.88 KB
/
util.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
#ifndef __AFS_UTIL_H__
#define __AFS_UTIL_H__
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sched.h>
#include <assert.h>
#define MB (1024*1024)
#define GB (1024*1024*1024)
#define afs_likely(x) __builtin_expect ((x), 1)
#define afs_unlikely(x) __builtin_expect ((x), 0)
#define afs_assert(x, ...) \
do {\
if (afs_unlikely(!(x))) {\
fprintf (stderr, "Assertion failed at %s (%s:%d): ", #x, \
__FILE__, __LINE__);\
fprintf (stderr, "" __VA_ARGS__);\
fflush(stderr);\
exit(EXIT_FAILURE);\
}\
} while (0)
#define LOG_MSG(...) \
do { \
fprintf(stderr, __VA_ARGS__); \
fflush(stderr);\
} while (0)
#define LOG_WARN(...) \
do { \
fprintf(stderr, "[WARN] " __VA_ARGS__); \
fflush(stderr);\
} while(0)
#define LOG_ERR(...) \
do { \
fprintf(stderr, "[ERROR] " __VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)
#ifdef DEBUG
#define LOG_DEBUG(...) \
do { \
fprintf(stderr, "[DEBUG]" __VA_ARGS__); \
fflush(stderr); \
} while (0)
#else
#define LOG_DEBUG(...)
#endif
static inline uint64_t now_us ()
{
// Use POSIX gettimeofday function to get precise time.
struct timeval tv;
gettimeofday (&tv, NULL);
return (tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec);
}
static inline void *stopwatch_start ()
{
uint64_t *watch = (uint64_t*) malloc (sizeof (uint64_t));
*watch = now_us ();
return (void*) watch;
}
static inline unsigned long stopwatch_stop (void *watch_)
{
uint64_t end = now_us ();
uint64_t start = *(uint64_t*) watch_;
free (watch_);
return (unsigned long) (end - start);
}
int cmp (const void * a, const void * b);
int cmp_lf (const void * a, const void * b);
int cmp_ll (const void * a, const void * b);
int cmp_int32 (const void * a, const void * b);
int cmp_size (const void * a, const void * b);
void quitmemory(void * pointer);
long long ll_abs(long long a);
//#define CPU_CORE 16
//static void pin_to_cpu(int cpu) {
// cpu_set_t cpuset; // set of CPUs allocating to the LVRM
//
// /*
// if (getuid()) {
// // exit if user is not root
// LOG_ERR("Root required\n");
// return;
// }
// */
//
// CPU_ZERO(&cpuset);
// CPU_SET(cpu, &cpuset);
// if (pthread_setaffinity_np(pthread_self(), sizeof(cpuset), &cpuset) < 0) {
// fprintf(stderr, "set thread affinity failed\n");
// }
// cpu_set_t get;
// CPU_ZERO(&get);
// if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
// fprintf(stderr, "get thread affinity failed\n");
// }
// for (int j = 0; j < CPU_CORE; j++) {
// if (CPU_ISSET(j, &get)) {
// LOG_MSG(" running in processor %d\n", j);
// }
// }
//}
#endif // AFS_UTIL_HPP_