2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
39 #include <sys/syscall.h>
40 #include <sys/types.h>
42 #include <xseg/xseg.h>
47 #include <bench-xseg.h>
52 #define PRINT_SIG(__who, __sig) \
53 fprintf(stdout, "%s (%lu): id %lu, object %lu, offset %lu\n", \
54 #__who, (uint64_t)(__sig), \
55 ((struct signature *)__sig)->id, \
56 ((struct signature *)__sig)->object, \
57 ((struct signature *)__sig)->offset);
59 struct timespec delay = {0, 4000000};
61 /******************************\
62 * Static miscellaneous tools *
63 \******************************/
64 static inline uint64_t __get_id()
66 return atol(global_id + 6); /* cut the "bench-" part*/
69 static inline uint64_t __get_object_from_name(char *name)
71 return atol(name + IDLEN); /* cut the "bench-908135-" part*/
74 static inline uint64_t __get_object(struct bench *prefs, uint64_t new)
77 new = new / (prefs->os / prefs->bs);
81 static inline int __snap_to_bound8(uint64_t space)
83 return space > 8 ? 8 : space;
86 static inline double __timespec2double(struct timespec num)
88 return (double) (num.tv_sec * pow(10, 9) + num.tv_nsec);
91 static inline void __write_sig(struct bench_lfsr *sg, uint64_t *d, uint64_t s,
98 /* Write random numbers (based on global_id) every 24 bytes */
99 /* TODO: Should we use memcpy? */
100 for (i = pos; i < (s / 8) - (3 - pos); i += 3)
101 *(d + i) = lfsr_next(sg);
103 /* special care for last chunk */
104 last_val = lfsr_next(sg);
105 space_left = s - (i * 8);
106 memcpy(d + i, &last_val, __snap_to_bound8(space_left));
109 static inline int __read_sig(struct bench_lfsr *sg, uint64_t *d, uint64_t s,
116 /* TODO: Should we use memcmp? */
117 for (i = pos; i < (s / 8) - (3 - pos); i += 3) {
118 if (*(d + i) != lfsr_next(sg))
121 /* special care for last chunk */
122 last_val = lfsr_next(sg);
123 space_left = s - (i * 8);
124 if (memcmp(d + i, &last_val, __snap_to_bound8(space_left)))
131 * Seperates a double number in seconds, msec, usec, nsec
132 * Expects a number in nanoseconds (e.g. a number from timespec2double)
134 static struct tm_result __separate_by_order(double num)
136 struct tm_result res;
138 //The format we expect is the following:
140 // |-s-|-ms-|-us-|-ns|
141 //num = 123 456 789 012 . 000000000000
142 res.s = num / pow(10,9);
143 num = fmod(num, pow(10,9));
144 res.ms = num / pow(10,6);
145 num = fmod(num, pow(10,6));
147 res.ns = fmod(num, 1000);
152 static void __calculate_bw(struct bench *prefs, double iops, struct bw *bw)
154 bw->val = iops * prefs->bs;
155 strcpy(bw->unit, "B/s");
160 bw->val = bw->val / 1024;
161 strcpy(bw->unit, "KB/s");
166 bw->val = bw->val / 1024;
167 strcpy(bw->unit, "MB/s");
172 bw->val = bw->val / 1024;
173 strcpy(bw->unit, "GB/s");
176 static double __calculate_iops(struct bench *prefs, double elapsed_ns)
178 /* elapsed_ns is in nanoseconds, so we convert it to seconds */
179 double elapsed = elapsed_ns / pow(10,9);
180 return (prefs->status->received / elapsed);
183 /******************************\
184 * Argument-parsing functions *
185 \******************************/
188 * Convert string to size in bytes.
189 * If syntax is invalid, return 0. Values such as zero and non-integer
190 * multiples of segment's page size should not be accepted.
192 uint64_t str2num(char *str)
197 num = strtoll(str, &unit, 10);
198 if (strlen(unit) > 1) //Invalid syntax
200 else if (strlen(unit) < 1) //Plain number in bytes
221 * Converts struct timespec to double (units in nanoseconds)
223 int read_insanity(char *insanity)
225 if (strncmp(insanity, "sane", MAX_ARG_LEN + 1) == 0)
226 return INSANITY_SANE;
227 if (strncmp(insanity, "eccentric", MAX_ARG_LEN + 1) == 0)
228 return INSANITY_ECCENTRIC;
229 if (strncmp(insanity, "manic", MAX_ARG_LEN + 1) == 0)
230 return INSANITY_MANIC;
231 if (strncmp(insanity, "paranoid", MAX_ARG_LEN + 1) == 0)
232 return INSANITY_PARANOID;
236 int read_op(char *op)
238 if (strncmp(op, "read", MAX_ARG_LEN + 1) == 0)
240 if (strncmp(op, "write", MAX_ARG_LEN + 1) == 0)
242 if (strncmp(op, "info", MAX_ARG_LEN + 1) == 0)
244 if (strncmp(op, "delete", MAX_ARG_LEN + 1) == 0)
249 int read_verify(char *verify)
251 if (strncmp(verify, "no", MAX_ARG_LEN + 1) == 0)
253 if (strncmp(verify, "meta", MAX_ARG_LEN + 1) == 0)
255 if (strncmp(verify, "full", MAX_ARG_LEN + 1) == 0)
260 int read_progress(char *progress)
262 if (strncmp(progress, "no", MAX_ARG_LEN + 1) == 0)
264 if (strncmp(progress, "yes", MAX_ARG_LEN + 1) == 0)
269 int read_pattern(char *pattern)
271 if (strncmp(pattern, "seq", MAX_ARG_LEN + 1) == 0)
273 if (strncmp(pattern, "rand", MAX_ARG_LEN + 1) == 0)
278 /*******************\
280 \*******************/
282 void print_io_stats(struct bench *prefs, double elapsed)
288 * We could malloc struct bw in __calculate_bw, but it's safer in cases when
289 * there is no memory left.
291 iops = __calculate_iops(prefs, elapsed);
292 __calculate_bw(prefs, iops, &bw);
294 fprintf(stdout, " ~~~~~~~~~~~~~~~~~~~~~~~~\n");
295 if (prefs->op == X_READ || prefs->op == X_WRITE)
296 fprintf(stdout, "Bandwidth: %.3lf %s\n", bw.val, bw.unit);
297 fprintf(stdout, "IOPS: %.3lf\n", iops);
300 void print_stats(struct bench *prefs)
303 "Requests total: %10lu\n"
304 "Requests submitted: %10lu\n"
305 "Requests received: %10lu\n"
306 "Requests failed: %10lu\n",
308 prefs->status->submitted,
309 prefs->status->received,
310 prefs->status->failed);
311 if ((prefs->op == X_READ) && (GET_FLAG(VERIFY, prefs->flags) != VERIFY_NO))
312 fprintf(stdout, "Requests corrupted: %10lu\n", prefs->status->corrupted);
313 fprintf(stdout, "\n");
317 void print_remaining(struct bench *prefs)
321 remaining = prefs->status->max - prefs->status->received;
323 fprintf(stdout, "Requests remaining: %10lu\n", remaining);
325 fprintf(stdout, "All requests have been served.\n");
329 void print_res(struct bench *prefs)
332 struct tm_result res, res_rec;
336 tm = prefs->total_tm;
337 sum = __timespec2double(tm->sum);
338 res = __separate_by_order(sum);
340 fprintf(stdout, "\n");
341 fprintf(stdout, " Benchmark results\n");
342 fprintf(stdout, " ========================\n");
343 fprintf(stdout, " |-s-||-ms-|-us-|-ns-|\n");
344 fprintf(stdout, "Total time: %3u. %03u %03u %03u\n",
345 res.s, res.ms, res.us, res.ns);
347 if (!prefs->status->received) {
353 if (GET_FLAG(INSANITY, prefs->flags) < tm->insanity)
356 sum_rec = __timespec2double(tm->sum);
357 res_rec = __separate_by_order(sum_rec / prefs->status->received);
359 fprintf(stdout, "Avg. latency: %3u. %03u %03u %03u\n",
360 res_rec.s, res_rec.ms, res_rec.us, res_rec.ns);
363 print_io_stats(prefs, sum);
367 void print_progress(struct bench *prefs)
371 if ((prefs->op == X_READ) && (GET_FLAG(VERIFY, prefs->flags) != VERIFY_NO))
374 fprintf(stdout, "\033[%dA\033[J", lines);
378 /**************************\
379 * Benchmarking functions *
380 \**************************/
382 void create_id(unsigned long seed)
384 if (seed >= pow(10, 9))
385 XSEGLOG2(&lc, W, "Seed larger than 10^9, only its first 9 digits will "
388 //nanoseconds can't be more than 9 digits
389 snprintf(global_id, IDLEN + 1, "bench-%09lu", seed);
392 void create_target(struct bench *prefs, struct xseg_request *req,
395 struct xseg *xseg = prefs->peer->xseg;
397 char buf[TARGETLEN + 1];
399 req_target = xseg_get_target(xseg, req);
401 //For read/write, the target object may not correspond to `new`, which is
402 //actually the chunk number.
403 new = __get_object(prefs, new);
404 snprintf(buf, TARGETLEN + 1, "%s-%016lu", global_id, new);
405 strncpy(req_target, buf, TARGETLEN);
406 XSEGLOG2(&lc, D, "Target name of request is %s\n", buf);
410 uint64_t determine_next(struct bench *prefs)
412 if (GET_FLAG(PATTERN, prefs->flags) == PATTERN_SEQ)
413 return prefs->status->submitted;
415 return lfsr_next(prefs->lfsr);
418 uint64_t calculate_offset(struct bench *prefs, uint64_t new)
421 return (new * prefs->bs) % prefs->os;
426 uint64_t calculate_prog_quantum(struct bench *prefs)
428 return round((double)prefs->status->max / 20.0);
433 * ***********************************************
434 * `create_chunk` handles 3 identifiers:
435 * 1. The benchmark's global_id
436 * 2. The object's number
437 * 3. The chunk offset in the object
439 * ************************************************
440 * `readwrite_chunk_full` takes the above 3 identifiers and feeds them as seeds
441 * in 63-bit LFSRs. The numbers generated are written consecutively in chunk's
442 * memory range. For example, for a 72-byte chunk:
444 * || 1 | 2 | 3 | 1 | 2 | 3 | 1 | 2 | 3 ||
445 * ^ 8 16 24 32 40 48 56 64 ^
450 * 1,2,3 differ between each iteration
452 * **************************************************
453 * `_create_chunk_meta` simply writes the above 3 ids in the start and end of
454 * the chunk's memory range, so it should be much faster (but less safe)
456 * **************************************************
457 * In both cases, special care is taken not to exceed the chunk's memory range.
458 * Also, the bare minimum chunk to verify should be 48 bytes. This limit is set
459 * by reeadwrite_chunk_meta, which expects to write in a memory at least this
462 * **************************************************
463 * Note: The diagram above also represents the x86_64's endianness.
464 * Endianness must be taken into careful consideration when examining a memory
467 static int readwrite_chunk_full(struct xseg *xseg, struct xseg_request *req,
468 uint64_t id, uint64_t object)
470 struct bench_lfsr id_lfsr;
471 struct bench_lfsr obj_lfsr;
472 struct bench_lfsr off_lfsr;
473 uint64_t *d = (uint64_t *)xseg_get_data(xseg, req);
474 uint64_t s = req->size;
476 /* Create 63-bit LFSRs */
477 lfsr_init(&id_lfsr, 0x7FFFFFFFFFFFFFFF, id, 0);
478 lfsr_init(&obj_lfsr, 0x7FFFFFFFFFFFFFFF, object, 0);
479 lfsr_init(&off_lfsr, 0x7FFFFFFFFFFFFFFF, req->offset, 0);
481 if (s < sizeof(struct signature)) {
482 XSEGLOG2(&lc, E, "Too small chunk size (%lu butes). Leaving.", s);
487 * Every write operation has its read counterpart which, if it finds any
488 * corruption, returns 1
491 if (req->op == X_WRITE) {
492 __write_sig(&id_lfsr, d, s, 0);
493 __write_sig(&obj_lfsr, d, s, 1);
494 __write_sig(&off_lfsr, d, s, 2);
496 if (__read_sig(&id_lfsr, d, s, 0))
498 if (__read_sig(&obj_lfsr, d, s, 1))
500 if(__read_sig(&off_lfsr, d, s, 2))
507 static int readwrite_chunk_meta(struct xseg *xseg, struct xseg_request *req,
508 uint64_t id, uint64_t object)
510 char *d = xseg_get_data(xseg, req);
511 uint64_t s = req->size;
512 struct signature sig;
513 int sig_s = sizeof(struct signature);
518 sig.offset = req->offset;
521 XSEGLOG2(&lc, E, "Too small chunk size (%lu butes). Leaving.", s);
525 //PRINT_SIG(expected, (&sig));
526 /* Read/Write chunk signature both at its start and at its end */
527 if (req->op == X_WRITE) {
528 memcpy(d, &sig, sig_s);
529 memcpy(d + s - sig_s, &sig, sig_s);
531 if (memcmp(d, &sig, sig_s))
533 else if (memcmp(d + s - sig_s, &sig, sig_s))
536 //PRINT_SIG(start, d);
537 //PRINT_SIG(end, (d + s - sig_s));
542 * We want these functions to be as fast as possible in case we haven't asked
544 * TODO: Make them prettier but keep the speed of this implementation
546 void create_chunk(struct bench *prefs, struct xseg_request *req, uint64_t new)
548 struct xseg *xseg = prefs->peer->xseg;
553 verify = GET_FLAG(VERIFY, prefs->flags);
559 object = __get_object(prefs, new);
560 readwrite_chunk_meta(xseg, req, id, object);
564 object = __get_object(prefs, new);
565 readwrite_chunk_full(xseg, req, id, object);
568 XSEGLOG2(&lc, W, "Unexpected verification mode: %d\n", verify);
572 int read_chunk(struct bench *prefs, struct xseg_request *req)
574 struct xseg *xseg = prefs->peer->xseg;
581 verify = GET_FLAG(VERIFY, prefs->flags);
587 target = xseg_get_target(xseg, req);
588 object = __get_object_from_name(target);
589 r = readwrite_chunk_meta(xseg, req, id, object);
593 target = xseg_get_target(xseg, req);
594 object = __get_object_from_name(target);
595 r = readwrite_chunk_full(xseg, req, id, object);
598 XSEGLOG2(&lc, W, "Unexpected verification mode: %d\n", verify);