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.
38 #include <sys/types.h>
40 #include <sys/syscall.h>
51 #include <xseg/xseg.h>
55 #define PEER_TYPE "pthread"
57 #define PEER_TYPE "posix"
60 //FIXME this should not be defined here probably
61 #define MAX_SPEC_LEN 128
62 #define MAX_PIDFILE_LEN 512
64 volatile unsigned int terminated = 0;
65 unsigned int verbose = 0;
72 struct peerd *global_peer;
79 void (*func)(void *arg);
83 inline static struct thread* alloc_thread(struct peerd *peer)
85 xqindex idx = xq_pop_head(&peer->threads, 1);
88 return peer->thread + idx;
91 inline static void free_thread(struct peerd *peer, struct thread *t)
93 xqindex idx = t - peer->thread;
94 xq_append_head(&peer->threads, idx, 1);
98 inline static void __wake_up_thread(struct thread *t)
100 pthread_mutex_lock(&t->lock);
101 pthread_cond_signal(&t->cond);
102 pthread_mutex_unlock(&t->lock);
105 inline static void wake_up_thread(struct thread* t)
112 inline static int wake_up_next_thread(struct peerd *peer)
114 return (xseg_signal(peer->xseg, peer->portno_start));
119 * extern is needed if this function is going to be called by another file
120 * such as bench-xseg.c
123 void signal_handler(int signal)
125 XSEGLOG2(&lc, I, "Caught signal. Terminating gracefully");
128 wake_up_next_thread(global_peer);
132 void renew_logfile(int signal)
134 XSEGLOG2(&lc, I, "Caught signal. Renewing logfile");
135 renew_logctx(&lc, NULL, verbose, NULL, REOPEN_FILE);
138 static int setup_signals(struct peerd *peer)
145 sigemptyset(&sa.sa_mask);
147 sa.sa_handler = signal_handler;
148 r = sigaction(SIGTERM, &sa, NULL);
151 r = sigaction(SIGINT, &sa, NULL);
154 r = sigaction(SIGQUIT, &sa, NULL);
158 sa.sa_handler = renew_logfile;
159 r = sigaction(SIGUSR1, &sa, NULL);
166 inline int canDefer(struct peerd *peer)
168 return !(peer->defer_portno == NoPort);
171 void print_req(struct xseg *xseg, struct xseg_request *req)
173 char target[64], data[64];
174 char *req_target, *req_data;
175 unsigned int end = (req->targetlen> 63) ? 63 : req->targetlen;
176 req_target = xseg_get_target(xseg, req);
177 req_data = xseg_get_data(xseg, req);
180 strncpy(target, req_target, end);
182 strncpy(data, req_data, 63);
184 printf("req id:%lu, op:%u %llu:%lu serviced: %lu, reqstate: %u\n"
185 "src: %u, transit: %u, dst: %u effective dst: %u\n"
186 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
187 (unsigned long)(req),
188 (unsigned int)req->op,
189 (unsigned long long)req->offset,
190 (unsigned long)req->size,
191 (unsigned long)req->serviced,
192 (unsigned int)req->state,
193 (unsigned int)req->src_portno,
194 (unsigned int)req->transit_portno,
195 (unsigned int)req->dst_portno,
196 (unsigned int)req->effective_dst_portno,
197 (unsigned int)req->targetlen, target,
198 (unsigned long long)req->datalen, data);
201 void log_pr(char *msg, struct peer_req *pr)
203 char target[64], data[64];
204 char *req_target, *req_data;
205 struct peerd *peer = pr->peer;
206 struct xseg *xseg = pr->peer->xseg;
207 req_target = xseg_get_target(xseg, pr->req);
208 req_data = xseg_get_data(xseg, pr->req);
209 /* null terminate name in case of req->target is less than 63 characters,
210 * and next character after name (aka first byte of next buffer) is not
213 unsigned int end = (pr->req->targetlen> 63) ? 63 : pr->req->targetlen;
215 strncpy(target, req_target, end);
217 strncpy(data, req_data, 63);
219 printf("%s: req id:%u, op:%u %llu:%lu serviced: %lu, retval: %lu, reqstate: %u\n"
220 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
222 (unsigned int)(pr - peer->peer_reqs),
223 (unsigned int)pr->req->op,
224 (unsigned long long)pr->req->offset,
225 (unsigned long)pr->req->size,
226 (unsigned long)pr->req->serviced,
227 (unsigned long)pr->retval,
228 (unsigned int)pr->req->state,
229 (unsigned int)pr->req->targetlen, target,
230 (unsigned long long)pr->req->datalen, data);
234 inline struct peer_req *alloc_peer_req(struct peerd *peer)
236 xqindex idx = xq_pop_head(&peer->free_reqs, 1);
239 return peer->peer_reqs + idx;
242 inline void free_peer_req(struct peerd *peer, struct peer_req *pr)
244 xqindex idx = pr - peer->peer_reqs;
246 xq_append_head(&peer->free_reqs, idx, 1);
249 struct timeval resp_start, resp_end, resp_accum = {0, 0};
250 uint64_t responds = 0;
251 void get_responds_stats(){
252 printf("Time waiting respond %lu.%06lu sec for %llu times.\n",
253 //(unsigned int)(t - peer->thread),
254 resp_accum.tv_sec, resp_accum.tv_usec, (long long unsigned int) responds);
258 void fail(struct peerd *peer, struct peer_req *pr)
260 struct xseg_request *req = pr->req;
262 XSEGLOG2(&lc, D, "failing req %u", (unsigned int) (pr - peer->peer_reqs));
263 req->state |= XS_FAILED;
264 //xseg_set_req_data(peer->xseg, pr->req, NULL);
265 p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
266 xseg_signal(peer->xseg, p);
267 free_peer_req(peer, pr);
269 wake_up_next_thread(peer);
274 void complete(struct peerd *peer, struct peer_req *pr)
276 struct xseg_request *req = pr->req;
278 req->state |= XS_SERVED;
279 //xseg_set_req_data(peer->xseg, pr->req, NULL);
280 //gettimeofday(&resp_start, NULL);
281 p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
282 //gettimeofday(&resp_end, NULL);
284 //timersub(&resp_end, &resp_start, &resp_end);
285 //timeradd(&resp_end, &resp_accum, &resp_accum);
286 //printf("xseg_signal: %u\n", p);
287 xseg_signal(peer->xseg, p);
288 free_peer_req(peer, pr);
290 wake_up_next_thread(peer);
294 static void handle_accepted(struct peerd *peer, struct peer_req *pr,
295 struct xseg_request *req)
297 struct xseg_request *xreq = pr->req;
298 //assert xreq == req;
299 XSEGLOG2(&lc, D, "Handle accepted");
301 //xreq->state = XS_ACCEPTED;
303 dispatch(peer, pr, req, dispatch_accept);
306 static void handle_received(struct peerd *peer, struct peer_req *pr,
307 struct xseg_request *req)
309 //struct xseg_request *req = pr->req;
310 //assert req->state != XS_ACCEPTED;
311 XSEGLOG2(&lc, D, "Handle received \n");
312 dispatch(peer, pr, req, dispatch_receive);
315 struct timeval sub_start, sub_end, sub_accum = {0, 0};
316 uint64_t submits = 0;
317 void get_submits_stats(){
318 printf("Time waiting submit %lu.%06lu sec for %llu times.\n",
319 //(unsigned int)(t - peer->thread),
320 sub_accum.tv_sec, sub_accum.tv_usec, (long long unsigned int) submits);
323 int submit_peer_req(struct peerd *peer, struct peer_req *pr)
326 struct xseg_request *req = pr->req;
327 // assert req->portno == peer->portno ?
328 //TODO small function with error checking
329 XSEGLOG2 (&lc, D, "submitting peer req %u\n", (unsigned int)(pr - peer->peer_reqs));
330 ret = xseg_set_req_data(peer->xseg, req, (void *)(pr));
333 //printf("pr: %x , req_data: %x \n", pr, xseg_get_req_data(peer->xseg, req));
334 //gettimeofday(&sub_start, NULL);
335 ret = xseg_submit(peer->xseg, req, pr->portno, X_ALLOC);
336 //gettimeofday(&sub_end, NULL);
338 //timersub(&sub_end, &sub_start, &sub_end);
339 //timeradd(&sub_end, &sub_accum, &sub_accum);
342 xseg_signal(peer->xseg, ret);
346 int check_ports(struct peerd *peer)
348 struct xseg *xseg = peer->xseg;
349 xport portno_start = peer->portno_start;
350 xport portno_end = peer->portno_end;
351 struct xseg_request *accepted, *received;
356 for (i = portno_start; i <= portno_end; i++) {
359 if (!isTerminate()) {
360 pr = alloc_peer_req(peer);
362 accepted = xseg_accept(xseg, i, X_NONBLOCK);
366 xseg_cancel_wait(xseg, i);
367 handle_accepted(peer, pr, accepted);
371 free_peer_req(peer, pr);
375 received = xseg_receive(xseg, i, X_NONBLOCK);
377 r = xseg_get_req_data(xseg, received, (void **) &pr);
379 XSEGLOG2(&lc, W, "Received request with no pr data\n");
380 xport p = xseg_respond(peer->xseg, received, peer->portno_start, X_ALLOC);
382 XSEGLOG2(&lc, W, "Could not respond stale request");
383 xseg_put_request(xseg, received, portno_start);
386 xseg_signal(xseg, p);
389 //maybe perform sanity check for pr
390 xseg_cancel_wait(xseg, i);
391 handle_received(peer, pr, received);
401 static int peerd_loop(void *arg);
403 static void* thread_loop(void *arg)
405 struct thread *t = (struct thread *) arg;
406 struct peerd *peer = t->peer;
407 struct xseg *xseg = peer->xseg;
408 xport portno_start = peer->portno_start;
409 xport portno_end = peer->portno_end;
410 pid_t pid =syscall(SYS_gettid);
412 uint64_t threshold=1000/(1 + portno_end - portno_start);
414 XSEGLOG2(&lc, D, "thread %u\n", (unsigned int) (t- peer->thread));
415 XSEGLOG2(&lc, I, "Thread %u has tid %u.\n", (unsigned int) (t- peer->thread), pid);
416 xseg_init_local_signal(xseg, peer->portno_start);
417 for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
418 for(loops = threshold; loops > 0; loops--) {
420 xseg_prepare_wait(xseg, peer->portno_start);
421 if (check_ports(peer))
424 XSEGLOG2(&lc, I, "Thread %u goes to sleep\n", (unsigned int) (t- peer->thread));
425 xseg_wait_signal(xseg, 10000000UL);
426 xseg_cancel_wait(xseg, peer->portno_start);
427 XSEGLOG2(&lc, I, "Thread %u woke up\n", (unsigned int) (t- peer->thread));
429 wake_up_next_thread(peer);
430 custom_peer_finalize(peer);
434 int peerd_start_threads(struct peerd *peer)
437 uint32_t nr_threads = peer->nr_threads;
439 for (i = 0; i < nr_threads; i++) {
440 peer->thread[i].peer = peer;
441 pthread_cond_init(&peer->thread[i].cond,NULL);
442 pthread_mutex_init(&peer->thread[i].lock, NULL);
443 if (peer->custom_peerd_loop)
444 pthread_create(&peer->thread[i].tid, NULL, peer->custom_peerd_loop, (void *)(peer->thread + i));
446 pthread_create(&peer->thread[i].tid, NULL, peerd_loop, (void *)(peer->thread + i));
447 peer->thread[i].func = NULL;
448 peer->thread[i].arg = NULL;
451 for (i = 0; i < nr_threads; i++) {
452 pthread_join(peer->thread[i].tid, NULL);
454 //?: Is this re-ordering acceptable?
455 if (peer->interactive_func)
456 peer->interactive_func();
463 int defer_request(struct peerd *peer, struct peer_req *pr)
467 if (!canDefer(peer)){
468 XSEGLOG2(&lc, E, "Peer cannot defer requests");
471 p = xseg_forward(peer->xseg, pr->req, peer->defer_portno, pr->portno,
474 XSEGLOG2(&lc, E, "Cannot defer request %lx", pr->req);
477 r = xseg_signal(peer->xseg, p);
479 XSEGLOG2(&lc, W, "Cannot signal port %lu", p);
481 free_peer_req(peer, pr);
485 static int peerd_loop(void *arg)
488 struct thread *t = (struct thread *) arg;
489 struct peerd *peer = t->peer;
491 struct peerd *peer = (struct peerd *) arg;
493 struct xseg *xseg = peer->xseg;
494 xport portno_start = peer->portno_start;
495 xport portno_end = peer->portno_end;
496 pid_t pid =syscall(SYS_gettid);
497 uint64_t threshold=1000/(1 + portno_end - portno_start);
500 XSEGLOG2(&lc, I, "Peer has tid %u.\n", pid);
501 xseg_init_local_signal(xseg, peer->portno_start);
502 for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
503 for(loops= threshold; loops > 0; loops--) {
505 xseg_prepare_wait(xseg, peer->portno_start);
506 if (check_ports(peer))
515 XSEGLOG2(&lc, I, "Peer goes to sleep\n");
516 xseg_wait_signal(xseg, 10000000UL);
517 xseg_cancel_wait(xseg, peer->portno_start);
518 XSEGLOG2(&lc, I, "Peer woke up\n");
521 wake_up_next_thread(peer);
522 custom_peer_finalize(peer);
524 custom_peer_finalize(peer);
525 xseg_quit_local_signal(xseg, peer->portno_start);
530 static struct xseg *join(char *spec)
532 struct xseg_config config;
535 (void)xseg_parse_spec(spec, &config);
536 xseg = xseg_join(config.type, config.name, PEER_TYPE, NULL);
540 (void)xseg_create(&config);
541 return xseg_join(config.type, config.name, PEER_TYPE, NULL);
544 static struct peerd* peerd_init(uint32_t nr_ops, char* spec, long portno_start,
545 long portno_end, uint32_t nr_threads, xport defer_portno)
549 struct xseg_port *port;
554 peer = malloc(sizeof(struct peerd));
559 peer->nr_ops = nr_ops;
560 peer->defer_portno = defer_portno;
562 peer->nr_threads = nr_threads;
563 peer->thread = calloc(nr_threads, sizeof(struct thread));
567 peer->peer_reqs = calloc(nr_ops, sizeof(struct peer_req));
568 if (!peer->peer_reqs){
574 if (!xq_alloc_seq(&peer->free_reqs, nr_ops, nr_ops))
577 if (!xq_alloc_empty(&peer->threads, nr_threads))
580 if (xseg_initialize()){
581 printf("cannot initialize library\n");
584 peer->xseg = join(spec);
588 peer->portno_start = (xport) portno_start;
589 peer->portno_end= (xport) portno_end;
590 port = xseg_bind_port(peer->xseg, peer->portno_start, NULL);
592 printf("cannot bind to port %u\n", (unsigned int) peer->portno_start);
597 for (p = peer->portno_start + 1; p <= peer->portno_end; p++) {
598 struct xseg_port *tmp;
599 tmp = xseg_bind_port(peer->xseg, p, (void *)xseg_get_signal_desc(peer->xseg, port));
601 printf("cannot bind to port %u\n", (unsigned int) p);
606 printf("Peer on ports %u-%u\n", peer->portno_start,
609 for (i = 0; i < nr_ops; i++) {
610 peer->peer_reqs[i].peer = peer;
611 peer->peer_reqs[i].req = NULL;
612 peer->peer_reqs[i].retval = 0;
613 peer->peer_reqs[i].priv = NULL;
614 peer->peer_reqs[i].portno = NoPort;
616 peer->peer_reqs[i].cond = st_cond_new(); //FIXME err check
620 peer->interactive_func = NULL;
625 int pidfile_remove(char *path, int fd)
628 return (unlink(path));
631 int pidfile_write(int pid_fd)
634 snprintf(buf, sizeof(buf), "%ld", syscall(SYS_gettid));
637 lseek(pid_fd, 0, SEEK_SET);
638 int ret = write(pid_fd, buf, strlen(buf));
642 int pidfile_read(char *path, pid_t *pid)
644 char buf[16], *endptr;
647 int fd = open(path, O_RDONLY);
650 int ret = read(fd, buf, 15);
656 *pid = strtol(buf, &endptr, 10);
657 if (endptr != &buf[ret]){
665 int pidfile_open(char *path, pid_t *old_pid)
668 int fd = open(path, O_CREAT|O_EXCL|O_WRONLY, S_IWUSR);
671 pidfile_read(path, old_pid);
676 void usage(char *argv0)
678 fprintf(stderr, "Usage: %s [general options] [custom peer options]\n\n", argv0);
679 fprintf(stderr, "General peer options:\n"
680 " Option | Default | \n"
681 " --------------------------------------------\n"
682 " -g | None | Segment spec to join\n"
683 " -sp | NoPort | Start portno to bind\n"
684 " -ep | NoPort | End portno to bind\n"
685 " -p | NoPort | Portno to bind\n"
686 " -n | 16 | Number of ops\n"
687 " -v | 0 | Verbosity level\n"
688 " -l | None | Logfile \n"
689 " -d | No | Daemonize \n"
690 " --pidfile | None | Pidfile \n"
692 " -t | No | Number of threads \n"
699 int main(int argc, char *argv[])
701 struct peerd *peer = NULL;
704 long portno_start = -1, portno_end = -1, portno = -1;
707 int daemonize = 0, help = 0;
708 uint32_t nr_ops = 16;
709 uint32_t nr_threads = 1;
710 unsigned int debug_level = 0;
711 xport defer_portno = NoPort;
715 char spec[MAX_SPEC_LEN + 1];
716 char logfile[MAX_LOGFILE_LEN + 1];
717 char pidfile[MAX_PIDFILE_LEN + 1];
723 //capture here -g spec, -n nr_ops, -p portno, -t nr_threads -v verbose level
724 // -dp xseg_portno to defer blocking requests
726 //TODO print messages on arg parsing error
727 BEGIN_READ_ARGS(argc, argv);
728 READ_ARG_STRING("-g", spec, MAX_SPEC_LEN);
729 READ_ARG_ULONG("-sp", portno_start);
730 READ_ARG_ULONG("-ep", portno_end);
731 READ_ARG_ULONG("-p", portno);
732 READ_ARG_ULONG("-n", nr_ops);
733 READ_ARG_ULONG("-v", debug_level);
735 READ_ARG_ULONG("-t", nr_threads);
737 READ_ARG_ULONG("-dp", defer_portno);
738 READ_ARG_STRING("-l", logfile, MAX_LOGFILE_LEN);
739 READ_ARG_BOOL("-d", daemonize);
740 READ_ARG_BOOL("-h", help);
741 READ_ARG_BOOL("--help", help);
742 READ_ARG_STRING("--pidfile", pidfile, MAX_PIDFILE_LEN);
750 r = init_logctx(&lc, argv[0], debug_level, logfile,
751 REDIRECT_STDOUT|REDIRECT_STDERR);
753 XSEGLOG("Cannot initialize logging to logfile");
756 XSEGLOG2(&lc, D, "Main thread has tid %ld.\n", syscall(SYS_gettid));
759 pid_fd = pidfile_open(pidfile, &old_pid);
762 XSEGLOG2(&lc, E, "Daemon already running, pid: %d.", old_pid);
764 XSEGLOG2(&lc, E, "Cannot open or create pidfile");
771 if (daemon(0, 1) < 0){
772 XSEGLOG2(&lc, E, "Cannot daemonize");
778 pidfile_write(pid_fd);
780 //TODO perform argument sanity checks
781 verbose = debug_level;
783 portno_start = portno;
786 if (portno_start == -1 || portno_end == -1){
787 XSEGLOG2(&lc, E, "Portno or {portno_start, portno_end} must be supplied");
793 peer = peerd_init(nr_ops, spec, portno_start, portno_end, nr_threads, defer_portno);
799 r = custom_peer_init(peer, argc, argv);
804 peerd_start_threads(peer);
808 st_thread_t st = st_thread_create(peerd_loop, peer, 1, 0);
809 r = st_thread_join(st, NULL);
811 if (peer->custom_peerd_loop)
812 r = peer->custom_peerd_loop(peer);
814 r = peerd_loop(peer);
818 pidfile_remove(pidfile, pid_fd);