-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathycsb_player.cc
373 lines (322 loc) · 11.9 KB
/
ycsb_player.cc
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <thread>
#include <assert.h>
#include <atomic>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <boost/smart_ptr/detail/spinlock.hpp>
#include "FifoQueue.h"
#include <vector>
#include <libmemcached/memcached.h>
#define PRIVATE private
#include "Cycles.h"
static const bool takeLatencySamples = false;
static const size_t maxSamples = 1 * 1000 * 1000;
int VALUE_LENGTH = 25;
// If true, when we do a get() and it isn't the expected length, do a new
// set with the new length. This simulates updating the cache when software
// adds a new field.
bool UPDATE_CHANGED_VALUE_LENGTH = true;
// XXX Not reason why this is should a separate flag from the '-s' parameter, no?
bool USE_LENGTH_FROM_FILE = true;
static char randomChars[100000];
#define MEMCACHED_THREADS 16
std::atomic<uint64_t> getAttempts(0);
std::atomic<uint64_t> getFailures(0);
std::atomic<uint64_t> setAttempts(0);
std::atomic<uint64_t> setFailures(0);
uint32_t linesProcessed = 0;
// Set to true to cause memcached worker threads to quit
static volatile bool threadsQuit = false;
class Operation {
public:
enum OperationType {
INVALID,
GET,
SET
};
Operation()
: type(INVALID)
, key()
, valueLength(0)
{
}
enum OperationType type;
char key[100];
size_t valueLength;
};
#define MAX_QUEUE_LENGTH 1000
FifoQueue<Operation> queue;
boost::detail::spinlock queueLock;
void
issueSet(memcached_st* memc, char* key, int valueLen)
{
assert(valueLen <= (int)sizeof(randomChars));
char* value = &randomChars[random() % (sizeof(randomChars) - valueLen)];
setAttempts++;
memcached_return rc = memcached_set(memc, key, strlen(key), value, valueLen, (time_t)0, (uint32_t)0);
if (rc != MEMCACHED_SUCCESS) {
//fprintf(stderr, "set rc == %d (%s)\n", (int)rc, memcached_strerror(memc, rc));
setFailures++;
}
}
void
issueGet(memcached_st* memc, char* key, std::vector<uint64_t>& getSamples)
{
memcached_return rc;
uint32_t flags;
size_t valueLength;
getAttempts++;
uint64_t start;
if (takeLatencySamples)
start = RAMCloud::Cycles::rdtsc();
char* ret = memcached_get(memc, key, strlen(key), &valueLength, &flags, &rc);
if (ret == NULL) {
//fprintf(stderr, "get rc == %d (%s)\n", (int)rc, memcached_strerror(memc, rc));
getFailures++;
// should just be a cache miss. handle by adding it to the cache.
if (rc == MEMCACHED_NOTFOUND) {
issueSet(memc, key, VALUE_LENGTH);
} else {
fprintf(stderr, "unexpected get error: %s\n", memcached_strerror(memc, rc));
exit(1);
}
} else {
if (takeLatencySamples &&
(start & 0xfff) == 0x010 &&
getSamples.size() != maxSamples)
{
getSamples.emplace_back(RAMCloud::Cycles::rdtsc() - start);
}
if (UPDATE_CHANGED_VALUE_LENGTH && (int)valueLength != VALUE_LENGTH) {
getFailures++;
issueSet(memc, key, VALUE_LENGTH);
}
free(ret);
}
}
void
memcachedThread()
{
std::vector<uint64_t> getSamples{};
std::vector<uint64_t> setSamples{};
if (takeLatencySamples) {
getSamples.reserve(maxSamples);
setSamples.reserve(maxSamples);
}
memcached_st* memc = memcached_create(NULL);
memcached_return rc;
#if 0
rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
if (rc != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set binary protocol\n");
exit(1);
}
rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, 1);
if (rc != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set udp protocol\n");
exit(1);
}
#endif
rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1);
if (rc != MEMCACHED_SUCCESS) {
fprintf(stderr, "failed to set non-blocking IO\n");
exit(1);
}
memcached_server_st* servers = memcached_server_list_append(NULL, "127.0.0.1", 12000, &rc);
if (servers == NULL) {
fprintf(stderr, "memcached_server_list_append failed: %d\n", (int)rc);
exit(1);
}
rc = memcached_server_push(memc, servers);
if (rc != MEMCACHED_SUCCESS) {
fprintf(stderr, "memcached_server_push failed: %d (%s)\n", (int)rc, memcached_strerror(memc, rc));
exit(1);
}
while (!threadsQuit) {
queueLock.lock();
if (queue.empty()) {
queueLock.unlock();
continue;
}
Operation op = queue.pop();
queueLock.unlock();
if (op.type == Operation::GET) {
issueGet(memc, op.key, getSamples);
} else if (op.type == Operation::SET) {
uint64_t start;
if (takeLatencySamples)
start = RAMCloud::Cycles::rdtsc();
if (takeLatencySamples &&
(start & 0xfff) == 0x010 &&
setSamples.size() != maxSamples)
{
setSamples.emplace_back(start);
issueSet(memc, op.key, op.valueLength);
setSamples.back() = RAMCloud::Cycles::rdtsc() - setSamples.back();
} else {
issueSet(memc, op.key, op.valueLength);
}
} else {
fprintf(stderr, "invalid operation!\n");
exit(1);
}
}
for (uint64_t s : getSamples)
printf("GET %lu ns\n", RAMCloud::Cycles::toNanoseconds(s));
for (uint64_t s : setSamples)
printf("SET %lu ns\n", RAMCloud::Cycles::toNanoseconds(s));
fprintf(stderr, "memcached worker thread exiting\n");
}
int
getValueLength(char* line)
{
if (USE_LENGTH_FROM_FILE) {
// XXX- this only works if a single field is given
if (strchr(line, '[') != NULL)
return (int)(strchr(line, ']') - strchr(line, '[')) - 10;
// if there are no fields explicitly listed, then assume this has been run
// through ycsb_munge.py and the key was stripped in favour of its byte length
int length;
sscanf(line, "%*s %*s %*s %d\n", &length);
return length;
}
return VALUE_LENGTH;
}
// this method can parse through about 4M operations/sec from the ycsb
// text output
void
handleOp(char *line)
{
/*
* READ usertable user6622674881006267921 [ <all fields>]
* INSERT usertable user8183854946431771896 [ field0=8#?(;?4%4*'4#0$"=/$*9"/)-!?36?7#>8>"-0$&2(2"0+)) &'-;+7 ()7%->56.!;2<086;-!#.9067 01(=!%3<$;7$#7#,; ]
*/
bool queueFull = true;
while (queueFull) {
queueLock.lock();
queueFull = (queue.size() == MAX_QUEUE_LENGTH);
queueLock.unlock();
if (queueFull)
usleep(100);
}
queueLock.lock();
if (line[0] == 'R') {
queue.push(Operation());
queue.back().type = Operation::GET;
sscanf(line, "READ usertable %s [", queue.back().key);
linesProcessed++;
} else if (line[0] == 'I') {
queue.push(Operation());
queue.back().type = Operation::SET;
sscanf(line, "INSERT usertable %s [", queue.back().key);
queue.back().valueLength = getValueLength(line);
linesProcessed++;
} else if (line[0] == 'U') {
queue.push(Operation());
queue.back().type = Operation::SET;
sscanf(line, "UPDATE usertable %s [", queue.back().key);
queue.back().valueLength = getValueLength(line);
linesProcessed++;
}
queueLock.unlock();
}
int
main(int argc, char** argv)
{
int opt;
char* progname = argv[0];
uint32_t periodicity = 100000;
while ((opt = getopt(argc, argv, "fP:s:")) != -1) {
switch (opt) {
case 'f':
USE_LENGTH_FROM_FILE = false;
break;
case 'P':
periodicity = atoi(optarg);
break;
case 's':
VALUE_LENGTH = atoi(optarg);
break;
}
}
argc -= optind;
argv += optind;
if (argc < 1) {
fprintf(stderr, "usage: %s ycsb-basic-workload-dump [...]\n", progname);
exit(1);
}
for (int i = 0; i < (int)sizeof(randomChars); i++)
randomChars[i] = '!' + (random() % ('~' - '!' + 1));
printf("#spinning %d memcached worker threads\n", MEMCACHED_THREADS);
std::thread* threads[MEMCACHED_THREADS];
for (int i = 0; i < MEMCACHED_THREADS; i++)
threads[i] = new std::thread(memcachedThread);
printf("# UPDATE_CHANGED_VALUE_LENGTH = %s\n", (UPDATE_CHANGED_VALUE_LENGTH) ? "true" : "false");
printf("# USE_LENGTH_FROM_FILE = %s\n", (USE_LENGTH_FROM_FILE) ? "true" : "false");
printf("# VALUE_LENGTH = %d (ONLY APPLIES IF !USE_LENGTH_FROM_FILE)\n", VALUE_LENGTH);
char buf[1000];
uint64_t start = RAMCloud::Cycles::rdtsc();
uint64_t lastGetAttempts = 0;
uint64_t lastGetFailures = 0;
uint64_t lastSetAttempts = 0;
//uint64_t lastSetFailures = 0;
uint32_t lastLinesProcessed = 0;
double lastElapsed = 0;
while (argc > 0) {
printf("# Using workload file [%s]\n", argv[0]);
FILE* fp = fopen(argv[0], "r");
assert(fp != NULL);
while (fgets(buf, sizeof(buf), fp) != NULL) {
handleOp(buf);
if ((linesProcessed - lastLinesProcessed) == periodicity) {
lastLinesProcessed = linesProcessed;
#if 0
printf("----------------------\n");
printf("Get Attempts: %e\n", (double)getAttempts);
printf(" Failures: %e (%.5f%% misses)\n", (double)getFailures, (double)getFailures / (double)getAttempts * 100);
printf(" /sec: %lu\n", (uint64_t)(((double)getAttempts) / elapsed));
printf(" Fails Last %.0fs: %e (%.5f%% misses)\n",
outputInterval,
(double)(getFailures - lastGetFailures),
(double)(getFailures - lastGetFailures) / (double)(getAttempts - lastGetAttempts) * 100);
printf("Set Attempts: %e\n", (double)setAttempts);
printf(" Failures: %e (%.5f%% failures)\n", (double)setFailures, (double)setFailures / (double)setAttempts * 100);
printf(" /sec: %lu\n", (uint64_t)(((double)setAttempts) / elapsed));
printf(" Fails Last %.0fs: %e (%.5f%% of attempts)\n",
outputInterval
(double)(setFailures - lastSetFailures),
(double)(setFailures - lastSetFailures) / (double)(setAttempts - lastSetAttempts) * 100);
#endif
uint64_t newGets = getAttempts - lastGetAttempts;
uint64_t newSets = setAttempts - lastSetAttempts;
uint64_t newAttempts = newGets + newSets;
double elapsed = RAMCloud::Cycles::toSeconds(RAMCloud::Cycles::rdtsc() - start);
double periodSecs = elapsed - lastElapsed;
lastElapsed = elapsed;
printf("%-10u lines %.1f s %e ops %.5f%% misses %.5f%% recently %e set failures %.2f op/s %.2f current op/s\n",
linesProcessed,
elapsed,
(double)(getAttempts + setAttempts),
(double)getFailures / (double)getAttempts * 100,
(double)(getFailures - lastGetFailures) / (double)(getAttempts - lastGetAttempts)* 100,
(double)setFailures,
double(getAttempts + setAttempts) / elapsed,
double(newAttempts) / periodSecs);
fflush(stdout);
lastGetAttempts = getAttempts;
lastGetFailures = getFailures;
lastSetAttempts = setAttempts;
//lastSetFailures = setFailures;
}
}
argc--;
argv++;
VALUE_LENGTH *= 2;
}
threadsQuit = true;
for (int i = 0; i < MEMCACHED_THREADS; i++)
threads[i]->join();
return 0;
}