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>
48 #include <bench-lfsr.h>
51 char global_id[IDLEN + 1];
53 * This macro checks two things:
54 * a) If in-flight requests are less than given iodepth
55 * b) If we have submitted all of the requests
57 #define CAN_SEND_REQUEST(__p) \
58 ((__p->status->submitted - __p->status->received < __p->iodepth) && \
59 (__p->status->submitted < __p->status->max) && \
62 #define CAN_VERIFY(__p) \
63 ((GET_FLAG(VERIFY, __p->flags) != VERIFY_NO) && __p->op == X_READ)
65 #define CAN_PRINT_PROGRESS(__p, __q) \
66 ((GET_FLAG(PROGRESS, __p->flags) == PROGRESS_YES) && \
67 (__p->status->received == __q))
69 void custom_peer_usage()
71 fprintf(stderr, "Custom peer options: \n"
72 " --------------------------------------------\n"
73 " -op | None | XSEG operation [read|write|info|delete]\n"
74 " --pattern | None | I/O pattern [seq|rand]\n"
75 " --verify | no | Verify written requests [no|meta|full]\n"
76 " -rc | None | Request cap\n"
77 " -to | None | Total objects\n"
78 " -ts | None | Total I/O size\n"
79 " -os | 4M | Object size\n"
80 " -bs | 4k | Block size\n"
81 " -tp | None | Target port\n"
82 " --iodepth | 1 | Number of in-flight I/O requests\n"
83 " --seed | None | Initialize LFSR and target names\n"
84 " --insanity| sane | Adjust insanity level of benchmark:\n"
85 " | | [sane|eccentric|manic|paranoid]\n"
86 " --progress| yes | Show progress of requests\n"
88 "Additional information:\n"
89 " --------------------------------------------\n"
90 " The -to and -ts options are mutually exclusive\n"
94 int custom_peer_init(struct peerd *peer, int argc, char *argv[])
97 char request_cap[MAX_ARG_LEN + 1];
98 char total_objects[MAX_ARG_LEN + 1];
99 char total_size[MAX_ARG_LEN + 1];
100 char object_size[MAX_ARG_LEN + 1];
101 char block_size[MAX_ARG_LEN + 1];
102 char op[MAX_ARG_LEN + 1];
103 char pattern[MAX_ARG_LEN + 1];
104 char insanity[MAX_ARG_LEN + 1];
105 char verify[MAX_ARG_LEN + 1];
106 char progress[MAX_ARG_LEN + 1];
107 struct xseg *xseg = peer->xseg;
108 unsigned int xseg_page_size = 1 << xseg->config.page_shift;
111 unsigned long seed = -1;
113 struct timespec timer_seed;
119 total_objects[0] = 0;
129 for (i = 0; i < nr_threads; i++) {
130 prefs = peer->thread[i]->priv;
131 prefs = malloc(sizeof(struct bench));
138 prefs = malloc(sizeof(struct bench));
143 memset(prefs, 0, sizeof(struct bench));
145 prefs->status = malloc(sizeof(struct req_status));
146 if (!prefs->status) {
151 memset(prefs->status, 0, sizeof(struct req_status));
153 //Begin reading the benchmark-specific arguments
154 BEGIN_READ_ARGS(argc, argv);
155 READ_ARG_STRING("-rc", request_cap, MAX_ARG_LEN);
156 READ_ARG_STRING("-op", op, MAX_ARG_LEN);
157 READ_ARG_STRING("--pattern", pattern, MAX_ARG_LEN);
158 READ_ARG_STRING("-to", total_objects, MAX_ARG_LEN);
159 READ_ARG_STRING("-ts", total_size, MAX_ARG_LEN);
160 READ_ARG_STRING("-os", object_size, MAX_ARG_LEN);
161 READ_ARG_STRING("-bs", block_size, MAX_ARG_LEN);
162 READ_ARG_ULONG("--iodepth", iodepth);
163 READ_ARG_ULONG("-tp", dst_port);
164 READ_ARG_ULONG("--seed", seed);
165 READ_ARG_STRING("--insanity", insanity, MAX_ARG_LEN);
166 READ_ARG_STRING("--verify", verify, MAX_ARG_LEN);
167 READ_ARG_STRING("--progress", progress, MAX_ARG_LEN);
170 /*****************************\
171 * Check I/O type parameters *
172 \*****************************/
174 //We support 4 xseg operations: X_READ, X_WRITE, X_DELETE, X_INFO
175 //The I/O pattern of these operations can be either sequential (seq) or
178 XSEGLOG2(&lc, E, "xseg operation needs to be supplied\n");
183 XSEGLOG2(&lc, E, "Invalid syntax: -op %s\n", op);
189 XSEGLOG2(&lc, E, "I/O pattern needs to be supplied\n");
192 r = read_pattern(pattern);
194 XSEGLOG2(&lc, E, "Invalid syntax: --pattern %s\n", pattern);
197 SET_FLAG(PATTERN, prefs->flags, r);
200 strcpy(verify, "no");
201 r = read_verify(verify);
203 XSEGLOG2(&lc, E, "Invalid syntax: --verify %s\n", verify);
206 SET_FLAG(VERIFY, prefs->flags, r);
208 //Default iodepth value is 1
212 prefs->iodepth = iodepth;
214 /**************************\
215 * Check timer parameters *
216 \**************************/
218 //Most of the times, not all timers need to be used.
219 //We can choose which timers will be used by adjusting the "insanity"
220 //level of the benchmark i.e. the obscurity of code paths (get request,
221 //submit request) that will be timed.
223 strcpy(insanity, "sane");
225 r = read_insanity(insanity);
227 XSEGLOG2(&lc, E, "Invalid syntax: --insanity %s\n", insanity);
230 SET_FLAG(INSANITY, prefs->flags, r);
232 /*****************************\
233 * Check I/O size parameters *
234 \*****************************/
236 //Block size (bs): Defaults to 4K.
237 //It must be a number followed by one of these characters:
239 //If not, it will be considered as size in bytes.
240 //Must be integer multiple of segment's page size (typically 4k).
242 strcpy(block_size,"4k");
244 prefs->bs = str2num(block_size);
246 XSEGLOG2(&lc, E, "Invalid syntax: -bs %s\n", block_size);
248 } else if (prefs->bs % xseg_page_size) {
249 XSEGLOG2(&lc, E, "Misaligned block size: %s\n", block_size);
253 //Total objects (to) or total I/O size (ts).
254 //Must have the same format as "block size"
255 //They are mutually exclusive
256 if (total_objects[0] && total_size[0]) {
257 XSEGLOG2(&lc, E, "Total objects and total size are mutually exclusive\n");
259 } else if (total_objects[0]) {
260 prefs->to = str2num(total_objects);
262 XSEGLOG2(&lc, E, "Invalid syntax: -to %s\n", total_objects);
265 //In this case, the maximum number of requests is the total number of
266 //objects we will handle
267 prefs->status->max = prefs->to;
268 } else if (total_size[0]) {
269 if (prefs->op != X_READ && prefs->op != X_WRITE) {
271 "Total objects must be supplied (required by -op %s)\n", op);
274 prefs->ts = str2num(total_size);
276 XSEGLOG2(&lc, E, "Invalid syntax: -ts %s\n", total_size);
278 } else if (prefs->ts % prefs->bs) {
279 XSEGLOG2(&lc, E, "Misaligned total I/O size: %s\n", total_size);
282 //In this case, the maximum number of requests is the number of blocks
283 //we need to cover the total I/O size
284 prefs->status->max = prefs->ts / prefs->bs;
286 XSEGLOG2(&lc, E, "Total objects or total size must be supplied\n");
290 //Object size (os): Defaults to 4M.
291 //Must have the same format as "block size"
292 //Must be integer multiple of "block size"
294 strcpy(object_size,"4M");
296 prefs->os = str2num(object_size);
298 XSEGLOG2(&lc, E, "Invalid syntax: -os %s\n", object_size);
300 } else if (prefs->os % prefs->bs) {
301 XSEGLOG2(&lc, E, "Misaligned object size: %s\n", object_size);
306 /*************************\
307 * Check port parameters *
308 \*************************/
311 XSEGLOG2(&lc, E, "Target port must be supplied\n");
315 prefs->src_port = peer->portno_start; //TODO: allow user to change this
316 prefs->dst_port = (xport) dst_port;
318 /*********************************\
319 * Create timers for all metrics *
320 \*********************************/
322 if (init_timer(&prefs->total_tm, INSANITY_SANE))
324 if (init_timer(&prefs->sub_tm, INSANITY_MANIC))
326 if (init_timer(&prefs->get_tm, INSANITY_PARANOID))
328 if (init_timer(&prefs->rec_tm, INSANITY_ECCENTRIC))
331 /*************************************\
332 * Initialize the LFSR and global_id *
333 \*************************************/
335 //We proceed to initialise the global_id, and seed variables.
337 clock_gettime(CLOCK_BENCH, &timer_seed);
338 seed = timer_seed.tv_nsec;
344 if (GET_FLAG(PATTERN, prefs->flags) == PATTERN_RAND) {
345 prefs->lfsr = malloc(sizeof(struct bench_lfsr));
351 r = lfsr_init(prefs->lfsr, prefs->status->max, seed, seed & 0xF);
352 if (r && set_by_hand) {
353 XSEGLOG2(&lc, E, "LFSR could not be initialized.\n");
361 /*********************************\
362 * Miscellaneous initializations *
363 \*********************************/
365 /* The request cap must be enforced only after the LFSR is initialized */
366 if (request_cap[0]) {
367 rc = str2num(request_cap);
369 XSEGLOG2(&lc, E, "Invalid syntax: -rc %s\n", request_cap);
371 } else if (rc > prefs->status->max) {
372 XSEGLOG2(&lc, E, "Request cap exceeds current request total.\n");
375 prefs->status->max = rc;
378 /* Benchmarking progress printing is on by default */
380 strcpy(progress, "yes");
381 r = read_progress(progress);
383 XSEGLOG2(&lc, E, "Invalid syntax: --progress %s\n", progress);
386 SET_FLAG(PROGRESS, prefs->flags, r);
389 peer->peerd_loop = bench_peerd_loop;
390 peer->priv = (void *) prefs;
391 XSEGLOG2(&lc, I, "Global ID is %s\n", global_id);
399 free(prefs->total_tm);
408 static int send_request(struct peerd *peer, struct bench *prefs)
410 struct xseg_request *req;
411 struct xseg *xseg = peer->xseg;
413 xport srcport = prefs->src_port;
414 xport dstport = prefs->dst_port;
419 uint64_t size = prefs->bs;
421 //srcport and dstport must already be provided by the user.
422 //returns struct xseg_request with basic initializations
423 XSEGLOG2(&lc, D, "Get new request\n");
424 timer_start(prefs, prefs->get_tm);
425 req = xseg_get_request(xseg, srcport, dstport, X_ALLOC);
427 XSEGLOG2(&lc, W, "Cannot get request\n");
430 timer_stop(prefs, prefs->get_tm, NULL);
432 //Allocate enough space for the data and the target's name
433 XSEGLOG2(&lc, D, "Prepare new request\n");
434 r = xseg_prep_request(xseg, req, TARGETLEN, size);
436 XSEGLOG2(&lc, W, "Cannot prepare request! (%lu, %llu)\n",
437 TARGETLEN, (unsigned long long)size);
438 goto put_xseg_request;
441 //Determine what the next target/chunk will be, based on I/O pattern
442 new = determine_next(prefs);
444 XSEGLOG2(&lc, I, "Our new request is %lu\n", new);
445 //Create a target of this format: "bench-<global_id>-<obj_no>"
446 create_target(prefs, req, new);
448 if (prefs->op == X_WRITE || prefs->op == X_READ) {
450 //Calculate the chunk's offset inside the object
451 req->offset = calculate_offset(prefs, new);
452 XSEGLOG2(&lc, D, "Offset of request %lu is %lu\n", new, req->offset);
454 if (prefs->op == X_WRITE)
455 create_chunk(prefs, req, new);
460 XSEGLOG2(&lc, D, "Allocate peer request\n");
461 pr = alloc_peer_req(peer);
463 XSEGLOG2(&lc, W, "Cannot allocate peer request (%ld remaining)\n",
464 peer->nr_ops - xq_count(&peer->free_reqs));
465 goto put_xseg_request;
468 pr->portno = srcport;
470 pr->priv = malloc(sizeof(struct timespec));
473 goto put_peer_request;
476 //XSEGLOG2(&lc, D, "Set request data\n");
477 r = xseg_set_req_data(xseg, req, pr);
479 XSEGLOG2(&lc, W, "Cannot set request data\n");
480 goto put_peer_request;
484 * Start measuring receive time.
485 * When we receive a request, we need to have its submission time to
486 * measure elapsed time. Thus, we memcpy its submission time to pr->priv.
487 * QUESTION: Is this the fastest way?
489 timer_start(prefs, prefs->rec_tm);
490 if (prefs->rec_tm->insanity <= GET_FLAG(INSANITY, prefs->flags))
491 memcpy(pr->priv, &prefs->rec_tm->start_time, sizeof(struct timespec));
493 //Submit the request from the source port to the target port
494 XSEGLOG2(&lc, D, "Submit request %lu\n", new);
495 timer_start(prefs, prefs->sub_tm);
496 p = xseg_submit(xseg, req, srcport, X_ALLOC);
498 XSEGLOG2(&lc, W, "Cannot submit request\n");
499 goto put_peer_request;
501 prefs->status->submitted++;
502 timer_stop(prefs, prefs->sub_tm, NULL);
504 //Send SIGIO to the process that has bound this port to inform that
506 r = xseg_signal(xseg, p);
508 // XSEGLOG2(&lc, W, "Cannot signal destination peer (reason %d)\n", r);
514 free_peer_req(peer, pr);
516 if (xseg_put_request(xseg, req, srcport))
517 XSEGLOG2(&lc, W, "Cannot put request\n");
522 * This function substitutes the default generic_peerd_loop of peer.c.
523 * It's plugged to struct peerd at custom peer's initialisation
525 int bench_peerd_loop(void *arg)
528 struct thread *t = (struct thread *) arg;
529 struct peerd *peer = t->peer;
532 struct peerd *peer = (struct peerd *) arg;
533 char id[4] = {'P','e','e','r'};
535 struct xseg *xseg = peer->xseg;
536 struct bench *prefs = peer->priv;
537 xport portno_start = peer->portno_start;
538 xport portno_end = peer->portno_end;
539 pid_t pid = syscall(SYS_gettid);
540 uint64_t threshold=1000/(1 + portno_end - portno_start);
541 uint64_t cached_prog_quantum = 0;
542 uint64_t prog_quantum = 0;
546 if (GET_FLAG(PROGRESS, prefs->flags) == PROGRESS_YES) {
547 prog_quantum = calculate_prog_quantum(prefs);
548 cached_prog_quantum = prog_quantum;
552 XSEGLOG2(&lc, I, "%s has tid %u.\n",id, pid);
553 xseg_init_local_signal(xseg, peer->portno_start);
555 timer_start(prefs, prefs->total_tm);
557 while (!(isTerminate() && all_peer_reqs_free(peer))) {
558 while (CAN_SEND_REQUEST(prefs)) {
559 xseg_cancel_wait(xseg, peer->portno_start);
560 XSEGLOG2(&lc, D, "...because %lu < %lu && %lu < %lu\n",
561 prefs->status->submitted - prefs->status->received,
562 prefs->iodepth, prefs->status->received,
564 XSEGLOG2(&lc, D, "Start sending new request\n");
565 r = send_request(peer, prefs);
569 //Heart of peerd_loop. This loop is common for everyone.
570 for (loops = threshold; loops > 0; loops--) {
572 xseg_prepare_wait(xseg, peer->portno_start);
574 if (UNLIKELY(CAN_PRINT_PROGRESS(prefs, prog_quantum))) {
575 prog_quantum += cached_prog_quantum;
576 print_progress(prefs);
579 if (check_ports(peer)) {
580 //If an old request has just been acked, the most sensible
581 //thing to do is to immediately send a new one
582 if (prefs->status->received < prefs->status->max)
588 //struct xseg_port *port = xseg_get_port(xseg, portno_start);
590 //q = XPTR_TAKE(port->request_queue, xseg->segment);
591 //XSEGLOG2(&lc, I, "%s goes to sleep with %u requests pending\n",
593 XSEGLOG2(&lc, I, "%s goes to sleep\n", id);
594 xseg_wait_signal(xseg, 10000000UL);
595 xseg_cancel_wait(xseg, peer->portno_start);
596 XSEGLOG2(&lc, I, "%s woke up\n", id);
599 XSEGLOG2(&lc, I, "peer->free_reqs = %d, peer->nr_ops = %d\n",
600 xq_count(&peer->free_reqs), peer->nr_ops);
604 void custom_peer_finalize(struct peerd *peer)
606 struct bench *prefs = peer->priv;
607 //TODO: Measure mean time, standard variation
609 if (!prefs->total_tm->completed)
610 timer_stop(prefs, prefs->total_tm, NULL);
612 if (GET_FLAG(PROGRESS, prefs->flags) == PROGRESS_YES)
613 print_progress(prefs);
617 print_remaining(prefs);
618 print_res(prefs, prefs->total_tm, "Total Requests");
623 * handle_received: +1 to our received requests.
624 * Do some sanity checks and then check if request is failed.
625 * If not try to verify the request if asked.
627 static void handle_received(struct peerd *peer, struct peer_req *pr)
629 //FIXME: handle null pointer
630 struct bench *prefs = peer->priv;
631 struct timer *rec = prefs->rec_tm;
633 prefs->status->received++;
635 //This is a serious error, so we must stop
636 XSEGLOG2(&lc, E, "Received peer request with no xseg request");
641 if ((GET_FLAG(INSANITY, prefs->flags) < rec->insanity) && !pr->priv) {
642 XSEGLOG2(&lc, W, "Cannot find submission time of request");
646 timer_stop(prefs, rec, pr->priv);
648 if (!(pr->req->state & XS_SERVED))
649 prefs->status->failed++;
650 else if (CAN_VERIFY(prefs) && read_chunk(prefs, pr->req))
651 prefs->status->corrupted++;
653 if (xseg_put_request(peer->xseg, pr->req, pr->portno))
654 XSEGLOG2(&lc, W, "Cannot put xseg request\n");
656 //QUESTION, can't we just keep the malloced memory for future use?
658 free_peer_req(peer, pr);
661 int dispatch(struct peerd *peer, struct peer_req *pr, struct xseg_request *req,
662 enum dispatch_reason reason)
665 case dispatch_accept:
666 //This is wrong, benchmarking peer should not accept requests,
668 XSEGLOG2(&lc, W, "Bench peer should not accept requests\n");
671 case dispatch_receive:
672 handle_received(peer, pr);