forked from efficient/memc3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memc3_util.c
148 lines (123 loc) · 2.72 KB
/
memc3_util.c
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/shm.h>
#define __USE_GNU
#include <sched.h>
#include "memcached.h"
#include "memc3_util.h"
void print_memc3_settings()
{
printf("memc3 settings:\n");
#ifdef MEMC3_ASSOC_CHAIN
printf("\thashtable = built-in hashtable\n");
#endif
#ifdef MEMC3_CACHE_LRU
printf("\teviction = LRU\n");
#endif
#ifdef MEMC3_CACHE_CLOCK
printf("\teviction = CLOCK\n");
#endif
#ifdef DO_PERF_COUNTING
printf("\t+perf_counting\n");
#endif
#ifdef MEMC3_ENABLE_HUGEPAGE
printf("\t+hugepage\n");
#endif
#ifdef MEMC3_ENABLE_INT_KEYCMP
printf("\t+int_keycmp\n");
#endif
#ifdef MEMC3_ASSOC_CUCKOO
printf("\t+cuckoo \n");
#ifdef MEMC3_ENABLE_TAG
printf("\t+tag\n");
#endif
#if (MEMC3_ASSOC_CUCKOO_WIDTH > 1)
printf("\t+%d-way cuckoo_path\n", MEMC3_ASSOC_CUCKOO_WIDTH);
#endif
#endif
#ifdef MEMC3_LOCK_GLOBAL
printf("\t+global lock\n");
#endif
#ifdef MEMC3_LOCK_NONE
printf("\t+no locking\n");
#endif
#ifdef MEMC3_LOCK_OPT
printf("\t+opt lock\n");
#endif
#ifdef MEMC3_LOCK_FINEGRAIN
printf("\t+bucket lock\n");
#endif
printf("\n");
}
#define HUGEPAGE_SIZE 2097152
void *alloc(size_t size)
{
#if defined(MEMC3_ENABLE_HUGEPAGE) && defined(__linux__)
if (size % HUGEPAGE_SIZE != 0)
size = (size / HUGEPAGE_SIZE + 1) * HUGEPAGE_SIZE;
int shmid = shmget(IPC_PRIVATE, size, /*IPC_CREAT |*/ SHM_HUGETLB | SHM_R | SHM_W);
if (shmid == -1) {
perror("shmget failed");
exit(EXIT_FAILURE);
}
void *p = shmat(shmid, NULL, 0);
if (p == (void *)-1) {
perror("Shared memory attach failed");
exit(EXIT_FAILURE);
}
if (shmctl(shmid, IPC_RMID, NULL) == -1) {
perror("shmctl failed");
}
return p;
#else
void *p = malloc(size);
if (NULL == p) {
perror("malloc failed");
assert(0);
}
return p;
#endif
}
void dealloc(void *p)
{
#ifdef MEMC3_ENABLE_HUGEPAGE
if (shmdt(p)) {
perror("");
assert(0);
}
#else
free(p);
#endif
}
double timeval_diff(struct timeval *start,
struct timeval *end)
{
/* Calculate the second difference*/
double r = end->tv_sec - start->tv_sec;
/* Calculate the microsecond difference */
if (end->tv_usec > start->tv_usec)
r += (end->tv_usec - start->tv_usec)/1000000.0;
else if (end->tv_usec < start->tv_usec)
r -= (start->tv_usec - end->tv_usec)/1000000.0;
return r;
}
int get_cpunum()
{
int num = 0;
#ifdef __linux__
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
sched_getaffinity(0, sizeof(cpuset), &cpuset);
for (int i = 0; i < 32; i++)
{
if (CPU_ISSET(i, &cpuset))
num++;
}
#endif
return num;
}