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];
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(prefs) \
58 ((prefs->status->submitted - prefs->status->received < prefs->iodepth) && \
59 (prefs->status->submitted < prefs->status->max))
61 #define CAN_VERIFY(prefs) \
62 ((GET_FLAG(VERIFY, prefs->flags) != VERIFY_NO) && prefs->op == X_READ)
64 void custom_peer_usage()
66 fprintf(stderr, "Custom peer options: \n"
67 " --------------------------------------------\n"
68 " -op | None | XSEG operation [read|write|info|delete]\n"
69 " --pattern | None | I/O pattern [seq|rand]\n"
70 " --verify | no | Verify written requests [no|meta|full]\n"
71 " -rc | None | Request cap\n"
72 " -to | None | Total objects\n"
73 " -ts | None | Total I/O size\n"
74 " -os | 4M | Object size\n"
75 " -bs | 4k | Block size\n"
76 " -tp | None | Target port\n"
77 " --iodepth | 1 | Number of in-flight I/O requests\n"
78 " --seed | None | Initialize LFSR and target names\n"
79 " --insanity| sane | Adjust insanity level of benchmark:\n"
80 " | | [sane|eccentric|manic|paranoid]\n"
82 "Additional information:\n"
83 " --------------------------------------------\n"
84 " The -to and -ts options are mutually exclusive\n"
88 int custom_peer_init(struct peerd *peer, int argc, char *argv[])
91 char request_cap[MAX_ARG_LEN + 1];
92 char total_objects[MAX_ARG_LEN + 1];
93 char total_size[MAX_ARG_LEN + 1];
94 char object_size[MAX_ARG_LEN + 1];
95 char block_size[MAX_ARG_LEN + 1];
96 char op[MAX_ARG_LEN + 1];
97 char pattern[MAX_ARG_LEN + 1];
98 char insanity[MAX_ARG_LEN + 1];
99 char verify[MAX_ARG_LEN + 1];
100 struct xseg *xseg = peer->xseg;
101 unsigned int xseg_page_size = 1 << xseg->config.page_shift;
104 unsigned long seed = -1;
106 struct timespec timer_seed;
112 total_objects[0] = 0;
121 for (i = 0; i < nr_threads; i++) {
122 prefs = peer->thread[i]->priv;
123 prefs = malloc(sizeof(struct bench));
130 prefs = malloc(sizeof(struct bench));
137 prefs->status = malloc(sizeof(struct req_status));
138 if (!prefs->status) {
142 memset(prefs->status, 0, sizeof(struct req_status));
144 //Begin reading the benchmark-specific arguments
145 BEGIN_READ_ARGS(argc, argv);
146 READ_ARG_STRING("-rc", request_cap, MAX_ARG_LEN);
147 READ_ARG_STRING("-op", op, MAX_ARG_LEN);
148 READ_ARG_STRING("--pattern", pattern, MAX_ARG_LEN);
149 READ_ARG_STRING("-to", total_objects, MAX_ARG_LEN);
150 READ_ARG_STRING("-ts", total_size, MAX_ARG_LEN);
151 READ_ARG_STRING("-os", object_size, MAX_ARG_LEN);
152 READ_ARG_STRING("-bs", block_size, MAX_ARG_LEN);
153 READ_ARG_ULONG("--iodepth", iodepth);
154 READ_ARG_ULONG("-tp", dst_port);
155 READ_ARG_ULONG("--seed", seed);
156 READ_ARG_STRING("--insanity", insanity, MAX_ARG_LEN);
157 READ_ARG_STRING("--verify", verify, MAX_ARG_LEN);
160 /*****************************\
161 * Check I/O type parameters *
162 \*****************************/
164 //We support 4 xseg operations: X_READ, X_WRITE, X_DELETE, X_INFO
165 //The I/O pattern of these operations can be either sequential (seq) or
168 XSEGLOG2(&lc, E, "xseg operation needs to be supplied\n");
173 XSEGLOG2(&lc, E, "Invalid syntax: -op %s\n", op);
179 XSEGLOG2(&lc, E, "I/O pattern needs to be supplied\n");
182 r = read_pattern(pattern);
184 XSEGLOG2(&lc, E, "Invalid syntax: --pattern %s\n", pattern);
187 SET_FLAG(PATTERN, prefs->flags, r);
190 strcpy(verify, "no");
191 r = read_verify(verify);
193 XSEGLOG2(&lc, E, "Invalid syntax: --verify %s\n", verify);
196 SET_FLAG(VERIFY, prefs->flags, r);
198 //Default iodepth value is 1
202 prefs->iodepth = iodepth;
204 /**************************\
205 * Check timer parameters *
206 \**************************/
208 //Most of the times, not all timers need to be used.
209 //We can choose which timers will be used by adjusting the "insanity"
210 //level of the benchmark i.e. the obscurity of code paths (get request,
211 //submit request) that will be timed.
213 strcpy(insanity, "sane");
215 r = read_insanity(insanity);
217 XSEGLOG2(&lc, E, "Invalid syntax: --insanity %s\n", insanity);
220 SET_FLAG(INSANITY, prefs->flags, r);
222 /*****************************\
223 * Check I/O size parameters *
224 \*****************************/
226 //Block size (bs): Defaults to 4K.
227 //It must be a number followed by one of these characters:
229 //If not, it will be considered as size in bytes.
230 //Must be integer multiple of segment's page size (typically 4k).
232 strcpy(block_size,"4k");
234 prefs->bs = str2num(block_size);
236 XSEGLOG2(&lc, E, "Invalid syntax: -bs %s\n", block_size);
238 } else if (prefs->bs % xseg_page_size) {
239 XSEGLOG2(&lc, E, "Misaligned block size: %s\n", block_size);
243 //Total objects (to) or total I/O size (ts).
244 //Must have the same format as "block size"
245 //They are mutually exclusive
246 if (total_objects[0] && total_size[0]) {
247 XSEGLOG2(&lc, E, "Total objects and total size are mutually exclusive\n");
249 } else if (total_objects[0]) {
250 prefs->to = str2num(total_objects);
252 XSEGLOG2(&lc, E, "Invalid syntax: -to %s\n", total_objects);
255 //In this case, the maximum number of requests is the total number of
256 //objects we will handle
257 prefs->status->max = prefs->to;
258 } else if (total_size[0]) {
259 if (prefs->op != X_READ && prefs->op != X_WRITE) {
261 "Total objects must be supplied (required by op %s)\n", op);
264 prefs->ts = str2num(total_size);
266 XSEGLOG2(&lc, E, "Invalid syntax: -ts %s\n", total_size);
268 } else if (prefs->ts % prefs->bs) {
269 XSEGLOG2(&lc, E, "Misaligned total I/O size: %s\n", total_size);
272 //In this case, the maximum number of requests is the number of blocks
273 //we need to cover the total I/O size
274 prefs->status->max = prefs->ts / prefs->bs;
276 XSEGLOG2(&lc, E, "Total objects or total size must be supplied\n");
280 //Object size (os): Defaults to 4M.
281 //Must have the same format as "block size"
282 //Must be integer multiple of "block size"
284 strcpy(object_size,"4M");
286 prefs->os = str2num(object_size);
288 XSEGLOG2(&lc, E, "Invalid syntax: -os %s\n", object_size);
290 } else if (prefs->os % prefs->bs) {
291 XSEGLOG2(&lc, E, "Misaligned object size: %s\n", object_size);
296 /*************************\
297 * Check port parameters *
298 \*************************/
301 XSEGLOG2(&lc, E, "Target port needs to be supplied\n");
305 prefs->src_port = peer->portno_start; //TODO: allow user to change this
306 prefs->dst_port = (xport) dst_port;
308 /*********************************\
309 * Create timers for all metrics *
310 \*********************************/
312 if (init_timer(&prefs->total_tm, INSANITY_SANE))
314 if (init_timer(&prefs->sub_tm, INSANITY_MANIC))
316 if (init_timer(&prefs->get_tm, INSANITY_PARANOID))
318 if (init_timer(&prefs->rec_tm, INSANITY_ECCENTRIC))
321 /*************************************\
322 * Initialize the LFSR and global_id *
323 \*************************************/
325 //We proceed to initialise the global_id, and seed variables.
327 clock_gettime(CLOCK_MONOTONIC_RAW, &timer_seed);
328 seed = timer_seed.tv_nsec;
334 if (GET_FLAG(PATTERN, prefs->flags) == PATTERN_RAND) {
335 prefs->lfsr = malloc(sizeof(struct bench_lfsr));
341 r = lfsr_init(prefs->lfsr, prefs->status->max, seed, seed & 0xF);
342 if (r && set_by_hand) {
343 XSEGLOG2(&lc, E, "LFSR could not be initialized.\n");
351 /****************************\
352 * Finalize initializations *
353 \****************************/
355 /* The request cap must be enforced only after the LFSR is initialized */
356 if (request_cap[0]) {
357 rc = str2num(request_cap);
359 XSEGLOG2(&lc, E, "Invalid syntax: -rc %s\n", request_cap);
361 } else if (rc > prefs->status->max) {
362 XSEGLOG2(&lc, E, "Request cap exceeds current request total.\n");
365 prefs->status->max = rc;
369 peer->peerd_loop = bench_peerd_loop;
370 peer->priv = (void *) prefs;
371 XSEGLOG2(&lc, I, "Global ID is %s\n", global_id);
379 free(prefs->total_tm);
388 static int send_request(struct peerd *peer, struct bench *prefs)
390 struct xseg_request *req;
391 struct xseg *xseg = peer->xseg;
393 xport srcport = prefs->src_port;
394 xport dstport = prefs->dst_port;
399 uint64_t size = prefs->bs;
401 //srcport and dstport must already be provided by the user.
402 //returns struct xseg_request with basic initializations
403 XSEGLOG2(&lc, D, "Get new request\n");
404 timer_start(prefs, prefs->get_tm);
405 req = xseg_get_request(xseg, srcport, dstport, X_ALLOC);
407 XSEGLOG2(&lc, W, "Cannot get request\n");
410 timer_stop(prefs, prefs->get_tm, NULL);
412 //Allocate enough space for the data and the target's name
413 XSEGLOG2(&lc, D, "Prepare new request\n");
414 r = xseg_prep_request(xseg, req, TARGETLEN, size);
416 XSEGLOG2(&lc, W, "Cannot prepare request! (%lu, %llu)\n",
417 TARGETLEN, (unsigned long long)size);
418 goto put_xseg_request;
421 //Determine what the next target/chunk will be, based on I/O pattern
422 new = determine_next(prefs);
424 XSEGLOG2(&lc, I, "Our new request is %lu\n", new);
425 //Create a target of this format: "bench-<global_id>-<obj_no>"
426 create_target(prefs, req, new);
428 if (prefs->op == X_WRITE || prefs->op == X_READ) {
430 //Calculate the chunk's offset inside the object
431 req->offset = calculate_offset(prefs, new);
432 XSEGLOG2(&lc, D, "Offset of request %lu is %lu\n", new, req->offset);
434 if (prefs->op == X_WRITE)
435 create_chunk(prefs, req, new);
440 XSEGLOG2(&lc, D, "Allocate peer request\n");
441 pr = alloc_peer_req(peer);
443 XSEGLOG2(&lc, W, "Cannot allocate peer request (%ld remaining)\n",
444 peer->nr_ops - xq_count(&peer->free_reqs));
445 goto put_xseg_request;
448 pr->portno = srcport;
450 pr->priv = malloc(sizeof(struct timespec));
453 goto put_peer_request;
456 //XSEGLOG2(&lc, D, "Set request data\n");
457 r = xseg_set_req_data(xseg, req, pr);
459 XSEGLOG2(&lc, W, "Cannot set request data\n");
460 goto put_peer_request;
464 * Start measuring receive time.
465 * When we receive a request, we need to have its submission time to
466 * measure elapsed time. Thus, we memcpy its submission time to pr->priv.
467 * QUESTION: Is this the fastest way?
469 timer_start(prefs, prefs->rec_tm);
470 if (prefs->rec_tm->insanity <= GET_FLAG(INSANITY, prefs->flags))
471 memcpy(pr->priv, &prefs->rec_tm->start_time, sizeof(struct timespec));
473 //Submit the request from the source port to the target port
474 XSEGLOG2(&lc, D, "Submit request %lu\n", new);
475 timer_start(prefs, prefs->sub_tm);
476 p = xseg_submit(xseg, req, srcport, X_ALLOC);
478 XSEGLOG2(&lc, W, "Cannot submit request\n");
479 goto put_peer_request;
481 prefs->status->submitted++;
482 timer_stop(prefs, prefs->sub_tm, NULL);
484 //Send SIGIO to the process that has bound this port to inform that
486 r = xseg_signal(xseg, p);
488 // XSEGLOG2(&lc, W, "Cannot signal destination peer (reason %d)\n", r);
494 free_peer_req(peer, pr);
496 if (xseg_put_request(xseg, req, srcport))
497 XSEGLOG2(&lc, W, "Cannot put request\n");
502 * This function substitutes the default generic_peerd_loop of peer.c.
503 * It's plugged to struct peerd at custom peer's initialisation
505 int bench_peerd_loop(void *arg)
508 struct thread *t = (struct thread *) arg;
509 struct peerd *peer = t->peer;
512 struct peerd *peer = (struct peerd *) arg;
513 char id[4] = {'P','e','e','r'};
515 struct xseg *xseg = peer->xseg;
516 struct bench *prefs = peer->priv;
517 xport portno_start = peer->portno_start;
518 xport portno_end = peer->portno_end;
519 uint64_t threshold=1000/(1 + portno_end - portno_start);
520 pid_t pid = syscall(SYS_gettid);
524 XSEGLOG2(&lc, I, "%s has tid %u.\n",id, pid);
525 xseg_init_local_signal(xseg, peer->portno_start);
527 timer_start(prefs, prefs->total_tm);
529 while (!isTerminate()) {
532 XSEGLOG2(&lc, D, "%s executes function\n", id);
533 xseg_cancel_wait(xseg, peer->portno_start);
540 while (CAN_SEND_REQUEST(prefs)) {
541 xseg_cancel_wait(xseg, peer->portno_start);
542 XSEGLOG2(&lc, D, "...because %lu < %lu && %lu < %lu\n",
543 prefs->status->submitted - prefs->status->received,
544 prefs->iodepth, prefs->status->received,
546 XSEGLOG2(&lc, D, "Start sending new request\n");
547 r = send_request(peer, prefs);
551 //Heart of peerd_loop. This loop is common for everyone.
552 for (loops = threshold; loops > 0; loops--) {
554 xseg_prepare_wait(xseg, peer->portno_start);
556 if (check_ports(peer)) {
557 //If an old request has just been acked, the most sensible
558 //thing to do is to immediately send a new one
559 if (prefs->status->received < prefs->status->max)
565 //struct xseg_port *port = xseg_get_port(xseg, portno_start);
567 //q = XPTR_TAKE(port->request_queue, xseg->segment);
568 //XSEGLOG2(&lc, I, "%s goes to sleep with %u requests pending\n",
570 XSEGLOG2(&lc, I, "%s goes to sleep\n", id);
577 xseg_wait_signal(xseg, 10000000UL);
578 xseg_cancel_wait(xseg, peer->portno_start);
579 XSEGLOG2(&lc, I, "%s woke up\n", id);
582 XSEGLOG2(&lc, I, "peer->free_reqs = %d, peer->nr_ops = %d\n",
583 xq_count(&peer->free_reqs), peer->nr_ops);
587 void custom_peer_finalize(struct peerd *peer)
589 struct bench *prefs = peer->priv;
590 //TODO: Measure mean time, standard variation
592 if (!prefs->total_tm->completed)
593 timer_stop(prefs, prefs->total_tm, NULL);
596 print_res(prefs, prefs->total_tm, "Total Requests");
601 * handle_received: +1 to our received requests.
602 * Do some sanity checks and then check if request is failed.
603 * If not try to verify the request if asked.
605 static void handle_received(struct peerd *peer, struct peer_req *pr)
607 //FIXME: handle null pointer
608 struct bench *prefs = peer->priv;
609 struct timer *rec = prefs->rec_tm;
611 prefs->status->received++;
613 //This is a serious error, so we must stop
614 XSEGLOG2(&lc, E, "Received peer request with no xseg request");
619 if ((GET_FLAG(INSANITY, prefs->flags) < rec->insanity) && !pr->priv) {
620 XSEGLOG2(&lc, W, "Cannot find submission time of request");
624 timer_stop(prefs, rec, pr->priv);
626 if (!(pr->req->state & XS_SERVED))
627 prefs->status->failed++;
628 else if (CAN_VERIFY(prefs) && read_chunk(prefs, pr->req))
629 prefs->status->corrupted++;
631 if (xseg_put_request(peer->xseg, pr->req, pr->portno))
632 XSEGLOG2(&lc, W, "Cannot put xseg request\n");
634 //QUESTION, can't we just keep the malloced memory for future use?
636 free_peer_req(peer, pr);
639 int dispatch(struct peerd *peer, struct peer_req *pr, struct xseg_request *req,
640 enum dispatch_reason reason)
643 case dispatch_accept:
644 //This is wrong, benchmarking peer should not accept requests,
646 XSEGLOG2(&lc, W, "Bench peer should not accept requests\n");
649 case dispatch_receive:
650 handle_received(peer, pr);