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];
54 * This macro checks two things:
55 * a) If in-flight requests are less than given iodepth
56 * b) If we have submitted all of the requests
58 #define CAN_SEND_REQUEST(prefs) \
59 prefs->sub_tm->completed - prefs->rec_tm->completed < prefs->iodepth && \
60 prefs->sub_tm->completed < prefs->max_requests \
62 void custom_peer_usage()
64 fprintf(stderr, "Custom peer options: \n"
65 " --------------------------------------------\n"
66 " -op | None | XSEG operation [read|write|info|delete]\n"
67 " --pattern | None | I/O pattern [seq|rand]\n"
68 " --verify | no | Verify written requests [no|meta|hash]\n"
69 " -to | None | Total objects (not for read/write)\n"
70 " -ts | None | Total I/O size\n"
71 " -os | 4M | Object size\n"
72 " -bs | 4k | Block size\n"
73 " -tp | None | Target port\n"
74 " --iodepth | 1 | Number of in-flight I/O requests\n"
75 " --seed | None | Initialize LFSR and target names\n"
76 " --insanity| sane | Adjust insanity level of benchmark:\n"
77 " | | [sane|eccentric|manic|paranoid]\n"
81 int custom_peer_init(struct peerd *peer, int argc, char *argv[])
84 char total_objects[MAX_ARG_LEN + 1];
85 char total_size[MAX_ARG_LEN + 1];
86 char object_size[MAX_ARG_LEN + 1];
87 char block_size[MAX_ARG_LEN + 1];
88 char op[MAX_ARG_LEN + 1];
89 char pattern[MAX_ARG_LEN + 1];
90 char insanity[MAX_ARG_LEN + 1];
91 char verify[MAX_ARG_LEN + 1];
92 struct xseg *xseg = peer->xseg;
93 unsigned int xseg_page_size = 1 << xseg->config.page_shift;
96 unsigned long seed = -1;
97 struct timespec timer_seed;
103 total_objects[0] = 0;
111 for (i = 0; i < nr_threads; i++) {
112 prefs = peer->thread[i]->priv;
113 prefs = malloc(sizeof(struct bench));
120 prefs = malloc(sizeof(struct bench));
127 //Begin reading the benchmark-specific arguments
128 BEGIN_READ_ARGS(argc, argv);
129 READ_ARG_STRING("-op", op, MAX_ARG_LEN);
130 READ_ARG_STRING("--pattern", pattern, MAX_ARG_LEN);
131 READ_ARG_STRING("-to", total_objects, MAX_ARG_LEN);
132 READ_ARG_STRING("-ts", total_size, MAX_ARG_LEN);
133 READ_ARG_STRING("-os", object_size, MAX_ARG_LEN);
134 READ_ARG_STRING("-bs", block_size, MAX_ARG_LEN);
135 READ_ARG_ULONG("--iodepth", iodepth);
136 READ_ARG_ULONG("-tp", dst_port);
137 READ_ARG_ULONG("--seed", seed);
138 READ_ARG_STRING("--insanity", insanity, MAX_ARG_LEN);
139 READ_ARG_STRING("--verify", verify, MAX_ARG_LEN);
142 /*****************************\
143 * Check I/O type parameters *
144 \*****************************/
146 //We support 4 xseg operations: X_READ, X_WRITE, X_DELETE, X_INFO
147 //The I/O pattern of these operations can be either sequential (seq) or
150 XSEGLOG2(&lc, E, "xseg operation needs to be supplied\n");
155 XSEGLOG2(&lc, E, "Invalid syntax: -op %s\n", op);
161 XSEGLOG2(&lc, E, "I/O pattern needs to be supplied\n");
164 r = read_pattern(pattern);
166 XSEGLOG2(&lc, E, "Invalid syntax: --pattern %s\n", pattern);
169 prefs->flags |= (uint8_t)r;
172 strcpy(verify, "no");
173 r = read_verify(verify);
175 XSEGLOG2(&lc, E, "Invalid syntax: --verify %s\n", verify);
179 //Default iodepth value is 1
183 prefs->iodepth = iodepth;
185 /**************************\
186 * Check timer parameters *
187 \**************************/
189 //Most of the times, not all timers need to be used.
190 //We can choose which timers will be used by adjusting the "insanity"
191 //level of the benchmark i.e. the obscurity of code paths (get request,
192 //submit request) that will be timed.
194 strcpy(insanity, "sane");
196 prefs->insanity = read_insanity(insanity);
197 if (prefs->insanity < 0) {
198 XSEGLOG2(&lc, E, "Invalid syntax: --insanity %s\n", insanity);
203 * If we have a request other than read/write, we don't need to check
204 * about size parameters, but only how many objects we want to affect
206 if (prefs->op != X_READ && prefs->op != X_WRITE) {
208 /***************************\
209 * Check object parameters *
210 \***************************/
212 if (!total_objects[0]) {
214 "Total number of objects needs to be supplied\n");
217 prefs->to = str2num(total_objects);
219 XSEGLOG2(&lc, E, "Invalid syntax: -to %s\n", total_objects);
223 //In this case, the maximum number of requests is the total number of
224 //objects we will handle
225 prefs->max_requests = prefs->to;
228 /*************************\
229 * Check size parameters *
230 \*************************/
232 //Block size (bs): Defaults to 4K.
233 //It must be a number followed by one of these characters:
235 //If not, it will be considered as size in bytes.
236 //Must be integer multiple of segment's page size (typically 4k).
238 strcpy(block_size,"4k");
240 prefs->bs = str2num(block_size);
242 XSEGLOG2(&lc, E, "Invalid syntax: -bs %s\n", block_size);
244 } else if (prefs->bs % xseg_page_size) {
245 XSEGLOG2(&lc, E, "Misaligned block size: %s\n", block_size);
249 //Total I/O size (ts): Must be supplied by user.
250 //Must have the same format as "total size"
251 //Must be integer multiple of "block size"
252 if (!total_size[0]) {
253 XSEGLOG2(&lc, E, "Total I/O size needs to be supplied\n");
257 prefs->ts = str2num(total_size);
259 XSEGLOG2(&lc, E, "Invalid syntax: -ts %s\n", total_size);
261 } else if (prefs->ts % prefs->bs) {
262 XSEGLOG2(&lc, E, "Misaligned total I/O size: %s\n", total_size);
264 } else if (prefs->ts > xseg->segment_size) {
266 "Total I/O size exceeds segment size\n", total_size);
270 //Object size (os): Defaults to 4M.
271 //Must have the same format as "total size"
272 //Must be integer multiple of "block size"
274 strcpy(object_size,"4M");
276 prefs->os = str2num(object_size);
278 XSEGLOG2(&lc, E, "Invalid syntax: -os %s\n", object_size);
280 } else if (prefs->os % prefs->bs) {
281 XSEGLOG2(&lc, E, "Misaligned object size: %s\n", object_size);
285 //In this case, the maximum number of requests is the number of blocks
286 //we need to cover the total I/O size
287 prefs->max_requests = prefs->ts / prefs->bs;
290 /*************************\
291 * Check port parameters *
292 \*************************/
295 XSEGLOG2(&lc, E, "Target port needs to be supplied\n");
299 prefs->src_port = peer->portno_start; //TODO: allow user to change this
300 prefs->dst_port = (xport) dst_port;
302 /*********************************\
303 * Create timers for all metrics *
304 \*********************************/
306 if (init_timer(&prefs->total_tm, TM_SANE))
308 if (init_timer(&prefs->sub_tm, TM_MANIC))
310 if (init_timer(&prefs->get_tm, TM_PARANOID))
312 if (init_timer(&prefs->rec_tm, TM_ECCENTRIC))
315 /********************************\
316 * Customize struct peerd/prefs *
317 \********************************/
322 //We proceed to initialise the global_id, and seed variables.
324 clock_gettime(CLOCK_MONOTONIC_RAW, &timer_seed);
325 seed = timer_seed.tv_nsec;
331 if ((prefs->flags & (1 << PATTERN_FLAG)) == IO_RAND) {
332 prefs->lfsr = malloc(sizeof(struct bench_lfsr));
338 r = lfsr_init(prefs->lfsr, prefs->max_requests, seed, seed & 0xF);
339 if (r && set_by_hand) {
340 XSEGLOG2(&lc, E, "LFSR could not be initialized\n");
347 XSEGLOG2(&lc, I, "Global ID is %s\n", global_id);
349 peer->peerd_loop = custom_peerd_loop;
350 peer->priv = (void *) prefs;
358 free(prefs->total_tm);
367 static int send_request(struct peerd *peer, struct bench *prefs)
369 struct xseg_request *req;
370 struct xseg *xseg = peer->xseg;
372 xport srcport = prefs->src_port;
373 xport dstport = prefs->dst_port;
378 uint64_t size = prefs->bs;
380 //srcport and dstport must already be provided by the user.
381 //returns struct xseg_request with basic initializations
382 XSEGLOG2(&lc, D, "Get new request\n");
383 timer_start(prefs, prefs->get_tm);
384 req = xseg_get_request(xseg, srcport, dstport, X_ALLOC);
386 XSEGLOG2(&lc, W, "Cannot get request\n");
389 timer_stop(prefs, prefs->get_tm, NULL);
391 //Allocate enough space for the data and the target's name
392 XSEGLOG2(&lc, D, "Prepare new request\n");
393 r = xseg_prep_request(xseg, req, TARGETLEN, size);
395 XSEGLOG2(&lc, W, "Cannot prepare request! (%lu, %llu)\n",
396 TARGETLEN, (unsigned long long)size);
397 goto put_xseg_request;
400 //Determine what the next target/chunk will be, based on I/O pattern
401 new = determine_next(prefs);
402 XSEGLOG2(&lc, I, "Our new request is %lu\n", new);
403 //Create a target of this format: "bench-<global_id>-<obj_no>"
404 create_target(prefs, req, new);
406 if (prefs->op == X_WRITE || prefs->op == X_READ) {
408 //Calculate the chunk offset inside the object
409 req->offset = (new * prefs->bs) % prefs->os;
410 XSEGLOG2(&lc, D, "Offset of request %lu is %lu\n", new, req->offset);
412 if (prefs->op == X_WRITE)
413 create_chunk(prefs, req, new);
419 XSEGLOG2(&lc, D, "Allocate peer request\n");
420 pr = alloc_peer_req(peer);
422 XSEGLOG2(&lc, W, "Cannot allocate peer request (%ld remaining)\n",
423 peer->nr_ops - xq_count(&peer->free_reqs));
424 goto put_xseg_request;
427 pr->portno = srcport;
429 pr->priv = malloc(sizeof(struct timespec));
432 goto put_peer_request;
435 //XSEGLOG2(&lc, D, "Set request data\n");
436 r = xseg_set_req_data(xseg, req, pr);
438 XSEGLOG2(&lc, W, "Cannot set request data\n");
439 goto put_peer_request;
443 * Start measuring receive time.
444 * When we receive a request, we need to have its submission time to
445 * measure elapsed time. Thus, we memcpy its submission time to pr->priv.
446 * QUESTION: Is this the fastest way?
448 timer_start(prefs, prefs->rec_tm);
449 if (prefs->rec_tm->insanity <= prefs->insanity)
450 memcpy(pr->priv, &prefs->rec_tm->start_time, sizeof(struct timespec));
452 //Submit the request from the source port to the target port
453 XSEGLOG2(&lc, D, "Submit request %lu\n", new);
454 timer_start(prefs, prefs->sub_tm);
455 p = xseg_submit(xseg, req, srcport, X_ALLOC);
457 XSEGLOG2(&lc, W, "Cannot submit request\n");
458 goto put_peer_request;
460 timer_stop(prefs, prefs->sub_tm, NULL);
462 //Send SIGIO to the process that has bound this port to inform that
464 r = xseg_signal(xseg, p);
466 // XSEGLOG2(&lc, W, "Cannot signal destination peer (reason %d)\n", r);
472 free_peer_req(peer, pr);
474 if (xseg_put_request(xseg, req, srcport))
475 XSEGLOG2(&lc, W, "Cannot put request\n");
480 * This function substitutes the default generic_peerd_loop of peer.c.
481 * It's plugged to struct peerd at custom peer's initialisation
483 int custom_peerd_loop(void *arg)
486 struct thread *t = (struct thread *) arg;
487 struct peerd *peer = t->peer;
490 struct peerd *peer = (struct peerd *) arg;
491 char id[4] = {'P','e','e','r'};
493 struct xseg *xseg = peer->xseg;
494 struct bench *prefs = peer->priv;
495 xport portno_start = peer->portno_start;
496 xport portno_end = peer->portno_end;
497 uint64_t threshold=1000/(1 + portno_end - portno_start);
498 pid_t pid = syscall(SYS_gettid);
502 XSEGLOG2(&lc, I, "%s has tid %u.\n",id, pid);
503 xseg_init_local_signal(xseg, peer->portno_start);
505 timer_start(prefs, prefs->total_tm);
507 while (!isTerminate()) {
510 XSEGLOG2(&lc, D, "%s executes function\n", id);
511 xseg_cancel_wait(xseg, peer->portno_start);
518 while (CAN_SEND_REQUEST(prefs)) {
519 xseg_cancel_wait(xseg, peer->portno_start);
520 XSEGLOG2(&lc, D, "...because %lu < %lu && %lu < %lu\n",
521 prefs->sub_tm->completed - prefs->rec_tm->completed,
522 prefs->iodepth, prefs->sub_tm->completed,
523 prefs->max_requests);
524 XSEGLOG2(&lc, D, "Start sending new request\n");
525 r = send_request(peer, prefs);
529 //Heart of peerd_loop. This loop is common for everyone.
530 for (loops = threshold; loops > 0; loops--) {
532 xseg_prepare_wait(xseg, peer->portno_start);
534 if (check_ports(peer)) {
535 //If an old request has just been acked, the most sensible
536 //thing to do is to immediately send a new one
537 if (prefs->rec_tm->completed < prefs->max_requests)
543 //struct xseg_port *port = xseg_get_port(xseg, portno_start);
545 //q = XPTR_TAKE(port->request_queue, xseg->segment);
546 //XSEGLOG2(&lc, I, "%s goes to sleep with %u requests pending\n",
548 XSEGLOG2(&lc, I, "%s goes to sleep\n", id);
555 xseg_wait_signal(xseg, 10000000UL);
556 xseg_cancel_wait(xseg, peer->portno_start);
557 XSEGLOG2(&lc, I, "%s woke up\n", id);
560 XSEGLOG2(&lc, I, "peer->free_reqs = %d, peer->nr_ops = %d\n",
561 xq_count(&peer->free_reqs), peer->nr_ops);
565 void custom_peer_finalize(struct peerd *peer)
567 struct bench *prefs = peer->priv;
568 //TODO: Measure mean time, standard variation
570 if (!prefs->total_tm->completed)
571 timer_stop(prefs, prefs->total_tm, NULL);
574 print_res(prefs, prefs->total_tm, "Total Requests");
579 static void handle_received(struct peerd *peer, struct peer_req *pr)
581 //FIXME: handle null pointer
582 struct bench *prefs = peer->priv;
583 struct timer *rec = prefs->rec_tm;
586 //This is a serious error, so we must stop
587 XSEGLOG2(&lc, E, "Received peer request with no xseg request");
593 XSEGLOG2(&lc, W, "Cannot find submission time of request");
597 timer_stop(prefs, rec, pr->priv);
599 if (xseg_put_request(peer->xseg, pr->req, pr->portno))
600 XSEGLOG2(&lc, W, "Cannot put xseg request\n");
602 //QUESTION, can't we just keep the malloced memory for future use?
604 free_peer_req(peer, pr);
607 int dispatch(struct peerd *peer, struct peer_req *pr, struct xseg_request *req,
608 enum dispatch_reason reason)
611 case dispatch_accept:
612 //This is wrong, benchmarking peer should not accept requests,
614 XSEGLOG2(&lc, W, "Bench peer should not accept requests\n");
617 case dispatch_receive:
618 handle_received(peer, pr);