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>
48 struct timespec delay = {0, 4000000};
51 uint64_t ts; //Total I/O size
52 uint64_t os; //Object size
53 uint64_t bs; //Block size
54 uint32_t iodepth; //Num of in-flight xseg reqs
59 int custom_peerd_loop(struct peerd *peer);
61 #define MAX_ARG_LEN 10
63 void custom_peer_usage()
65 fprintf(stderr, "Custom peer options: \n"
66 " --------------------------------------------\n"
67 " -ts | None | Total I/O size\n"
68 " -os | 4M | Object size\n"
69 " -bs | 4k | Block size\n"
70 " -dp | None | Destination port\n"
71 " --iodepth | 1 | Number of in-flight I/O requests\n"
76 * Convert string to size in bytes.
77 * If syntax is invalid, return 0. Values such as zero and non-integer
78 * multiples of segment's page size should not be accepted.
80 static uint64_t str2num(char *str)
85 num = strtoll(str, &unit, 10);
86 if (strlen(unit) > 1) //Invalid syntax
88 else if (strlen(unit) < 1) //Plain number in bytes
108 int custom_peer_init(struct peerd *peer, int argc, char *argv[])
111 char total_size[MAX_ARG_LEN + 1];
112 char object_size[MAX_ARG_LEN + 1];
113 char block_size[MAX_ARG_LEN + 1];
114 struct xseg *xseg = peer->xseg;
115 unsigned int xseg_page_size = 1 << xseg->config.page_shift;
122 prefs = malloc(sizeof(struct bench));
128 //Begin reading the benchmark-specific arguments
129 BEGIN_READ_ARGS(argc, argv);
130 READ_ARG_STRING("-ts", total_size, MAX_ARG_LEN);
131 READ_ARG_STRING("-os", object_size, MAX_ARG_LEN);
132 READ_ARG_STRING("-bs", block_size, MAX_ARG_LEN);
133 READ_ARG_ULONG("--iodepth", prefs->iodepth);
134 READ_ARG_ULONG("-dp", dst_port);
138 *************************
139 * Check size parameters *
140 *************************
143 //Block size (bs): Defaults to 4K.
144 //It must be a number followed by one of these characters: [k|K|m|M|g|G].
145 //If not, it will be considered as size in bytes.
146 //Must be integer multiple of segment's page size (typically 4k).
148 strcpy(block_size,"4k");
150 prefs->bs = str2num(block_size);
152 XSEGLOG2(&lc, E, "Invalid syntax: %s\n", block_size);
154 } else if (prefs->bs % xseg_page_size) {
155 XSEGLOG2(&lc, E, "Misaligned block size: %s\n", block_size);
159 //Total I/O size (ts): Must be supplied by user.
160 //Must have the same format as "total size"
161 //Must be integer multiple of "block size"
162 if (!total_size[0]) {
163 XSEGLOG2(&lc, E, "Total I/O size needs to be supplied\n");
167 prefs->ts = str2num(total_size);
169 XSEGLOG2(&lc, E, "Invalid syntax: %s\n", total_size);
171 } else if (prefs->ts % prefs->bs) {
172 XSEGLOG2(&lc, E, "Misaligned total I/O size: %s\n", total_size);
174 } else if (prefs->ts > xseg->segment_size) {
175 XSEGLOG2(&lc, E, "Total I/O size exceeds segment size\n", total_size);
179 //Object size (os): Defaults to 4M.
180 //Must have the same format as "total size"
181 //Must be integer multiple of "block size"
183 strcpy(object_size,"4M");
185 prefs->os = str2num(object_size);
187 XSEGLOG2(&lc, E, "Invalid syntax: %s\n", object_size);
189 } else if (prefs->os % prefs->bs) {
190 XSEGLOG2(&lc, E, "Misaligned object size: %s\n", object_size);
195 *************************
196 * Check port parameters *
197 *************************
201 XSEGLOG2(&lc, E, "Destination port needs to be supplied\n");
205 prefs->dst_port = (xport) dst_port;
208 **************************
209 * Customize struct peerd *
210 **************************
213 peer->custom_peerd_loop = custom_peerd_loop;
214 peer->priv = (void *) prefs;
224 * This function substitutes the default peerd_loop of peer.c.
225 * This is achieved by passing to gcc a CUSTOM_LOOP macro definition which is
226 * checked (ifdef) in peer.c
228 int custom_peerd_loop(struct peerd *peer)
232 if (peer->interactive_func)
233 peer->interactive_func();
234 for (i = 0; i < peer->nr_threads; i++) {
235 pthread_join(peer->thread[i].tid, NULL);
238 struct xseg *xseg = peer->xseg;
239 struct bench *prefs = peer->priv;
241 xport portno_start = peer->portno_start;
242 xport portno_end = peer->portno_end;
243 uint64_t threshold=1000/(1 + portno_end - portno_start);
244 pid_t pid =syscall(SYS_gettid);
247 uint64_t remaining = prefs->ts;
249 XSEGLOG2(&lc, I, "Peer has tid %u.\n", pid);
250 xseg_init_local_signal(xseg, peer->portno_start);
252 while (!isTerminate()
253 && xq_count(&peer->free_reqs) == peer->nr_ops
255 for (loops= threshold; loops > 0; loops--) {
257 xseg_prepare_wait(xseg, peer->portno_start);
258 if (check_ports(peer))
267 XSEGLOG2(&lc, I, "Peer goes to sleep\n");
268 xseg_wait_signal(xseg, 10000000UL);
269 xseg_cancel_wait(xseg, peer->portno_start);
270 XSEGLOG2(&lc, I, "Peer woke up\n");
272 custom_peer_finalize(peer);
273 xseg_quit_local_signal(xseg, peer->portno_start);
278 void custom_peer_finalize(struct peerd *peer)
283 int dispatch(struct peerd *peer, struct peer_req *pr, struct xseg_request *req,
284 enum dispatch_reason reason)
287 defer_request(peer, pr);
289 // printf("completing req id: %u (remote %u)\n", (unsigned int) (pr - peer->peer_reqs), (unsigned int) pr->req->priv);
290 // nanosleep(&delay,NULL);
291 // print_req(peer->xseg, pr->req);