-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench-critbit.c
178 lines (140 loc) · 3.12 KB
/
bench-critbit.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "critbit.h"
static inline uint64_t
rdtsc(void)
{
uint32_t eax = 0, edx;
__asm__ __volatile__("rdtscp"
: "+a" (eax), "=d" (edx)
:
: "%ecx", "memory");
return (((uint64_t)edx << 32) | eax);
}
struct critbit {
char *key;
};
CRITBIT_HEAD_PROTOTYPE(critbit);
CRITBIT_GENERATE_INLINE(critbit, critbit, str, key);
static uint64_t __thread allocsize;
struct td_arg {
long i;
int nobjs;
};
static void
cb_free(void *t, void *n)
{
(void)t;
free(n);
}
void *
thr_cb_worker(void *o)
{
struct td_arg *args = (struct td_arg *)o;
CRITBIT_HEAD(critbit) critbit;
long int i = 0;
struct stat sb;
uint64_t s, e;
char fname[8];
uint64_t off;
char **keys;
int fd, len;
char td[4];
char *buf;
CRITBIT_INIT(critbit, &critbit, cb_free, NULL);
allocsize = 0;
snprintf(td, 4, "td%ld", args->i+1);
snprintf(fname, 8, "words%ld", args->i + 1);
fd = open(fname, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "fname %s\n", fname);
perror("open");
}
if (fstat(fd, &sb) < 0) {
perror("fstat");
}
buf = mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0);
if (buf == MAP_FAILED) {
perror("mmap");
}
keys = calloc(args->nobjs, sizeof(*keys));
off = 0;
s = rdtsc();
for (i = 0; i < args->nobjs; i++) {
struct critbit_node *n;
struct critbit *c;
char *nl;
n = calloc(1, critbit_node_size());
c = calloc(1, sizeof(*c));
nl = memchr(buf, '\n', sb.st_size - off);
*nl = '\0';
keys[i] = buf;
c->key = strdup(buf);
len = strlen(buf);
off += len + 1;
CRITBIT_INSERT(critbit, &critbit, n, c);
c = CRITBIT_GET(critbit, &critbit, buf);
buf = nl + 1;
allocsize += critbit_node_size() + sizeof (*c) + len;
}
e = rdtsc();
fprintf(stderr, "AGT: T%ld: %"PRIu64" alloced %"PRIu64"\n", args->i, e-s, allocsize);
s = rdtsc();
for (i = 0; i < args->nobjs; i++) {
volatile struct critbit *c __attribute__((unused));
c = CRITBIT_GET(critbit, &critbit, keys[i]);
}
e = rdtsc();
fprintf(stderr, "LUT: T%ld: %"PRIu64" alloced %"PRIu64"\n", args->i, e-s, allocsize);
return NULL;
}
void
usage(void)
{
fprintf(stderr, "Usage: ht <tds> <objs>\n"
"\ttds:\tNumber of threads to spawn\n"
"\tobjs:\tNumber of objects per thread\n"
);
exit(-1);
}
int
main(int argc, char **argv)
{
long int ntds = 0, nobjs = 0, i;
struct td_arg *args;
pthread_attr_t attr;
pthread_t *tds;
if (argc < 3) {
usage();
}
ntds = strtol(argv[1], NULL, 10);
if (ntds <= 0 || (errno == ERANGE && ntds == LONG_MAX)) {
usage();
}
nobjs = strtol(argv[2], NULL, 10);
if (nobjs <= 0 || (errno == ERANGE && nobjs == LONG_MAX)) {
usage();
}
tds = malloc(ntds * sizeof(*tds));
args = malloc(ntds * sizeof(*args));
pthread_attr_init(&attr);
for (i = 0; i < ntds; i++) {
args[i].i = i;
args[i].nobjs = nobjs;
pthread_create(&tds[i], &attr, thr_cb_worker, &args[i]);
}
for (i = 0; i < ntds; i++) {
pthread_join(tds[i], NULL);
}
return 0;
}