8 #include <sys/syscall.h>
19 #define PEER_TYPE "pthread"
21 #define PEER_TYPE "posix"
24 volatile unsigned int terminated = 0;
25 unsigned int verbose = 0;
37 void (*func)(void *arg);
42 inline static struct thread* alloc_thread(struct peerd *peer)
44 xqindex idx = xq_pop_head(&peer->threads, 1);
47 return peer->thread + idx;
50 inline static void free_thread(struct peerd *peer, struct thread *t)
52 xqindex idx = t - peer->thread;
53 xq_append_head(&peer->threads, idx, 1);
57 inline static void __wake_up_thread(struct thread *t)
59 pthread_mutex_lock(&t->lock);
60 pthread_cond_signal(&t->cond);
61 pthread_mutex_unlock(&t->lock);
64 inline static void wake_up_thread(struct thread* t)
71 inline static int wake_up_next_thread(struct peerd *peer)
73 return (xseg_signal(peer->xseg, peer->portno_start));
78 static inline int isTerminate()
80 /* ta doesn't need to be taken into account, because the main loops
81 * doesn't check the terminated flag if ta is not 0.
84 return (!ta & terminated);
92 void signal_handler(int signal)
94 XSEGLOG2(&lc, I, "Caught signal. Terminating gracefully");
98 void renew_logfile(int signal)
100 XSEGLOG2(&lc, I, "Caught signal. Renewing logfile");
101 renew_logctx(&lc, NULL, lc.log_level, NULL, REOPEN_FILE);
104 static int setup_signals()
108 sigemptyset(&sa.sa_mask);
110 sa.sa_handler = signal_handler;
111 r = sigaction(SIGTERM, &sa, NULL);
114 r = sigaction(SIGINT, &sa, NULL);
117 r = sigaction(SIGQUIT, &sa, NULL);
121 sa.sa_handler = renew_logfile;
122 r = sigaction(SIGUSR1, &sa, NULL);
129 inline int canDefer(struct peerd *peer)
131 return !(peer->defer_portno == NoPort);
134 void print_req(struct xseg *xseg, struct xseg_request *req)
136 char target[64], data[64];
137 char *req_target, *req_data;
138 unsigned int end = (req->targetlen> 63) ? 63 : req->targetlen;
139 req_target = xseg_get_target(xseg, req);
140 req_data = xseg_get_data(xseg, req);
143 strncpy(target, req_target, end);
145 strncpy(data, req_data, 63);
147 printf("req id:%lu, op:%u %llu:%lu serviced: %lu, reqstate: %u\n"
148 "src: %u, st: %u, dst: %u dt: %u\n"
149 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
150 (unsigned long)(req),
151 (unsigned int)req->op,
152 (unsigned long long)req->offset,
153 (unsigned long)req->size,
154 (unsigned long)req->serviced,
155 (unsigned int)req->state,
156 (unsigned int)req->src_portno,
157 (unsigned int)req->src_transit_portno,
158 (unsigned int)req->dst_portno,
159 (unsigned int)req->dst_transit_portno,
160 (unsigned int)req->targetlen, target,
161 (unsigned long long)req->datalen, data);
164 void log_pr(char *msg, struct peer_req *pr)
166 char target[64], data[64];
167 char *req_target, *req_data;
168 struct peerd *peer = pr->peer;
169 struct xseg *xseg = pr->peer->xseg;
170 req_target = xseg_get_target(xseg, pr->req);
171 req_data = xseg_get_data(xseg, pr->req);
172 /* null terminate name in case of req->target is less than 63 characters,
173 * and next character after name (aka first byte of next buffer) is not
176 unsigned int end = (pr->req->targetlen> 63) ? 63 : pr->req->targetlen;
178 strncpy(target, req_target, end);
180 strncpy(data, req_data, 63);
182 printf("%s: req id:%u, op:%u %llu:%lu serviced: %lu, retval: %lu, reqstate: %u\n"
183 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
185 (unsigned int)(pr - peer->peer_reqs),
186 (unsigned int)pr->req->op,
187 (unsigned long long)pr->req->offset,
188 (unsigned long)pr->req->size,
189 (unsigned long)pr->req->serviced,
190 (unsigned long)pr->retval,
191 (unsigned int)pr->req->state,
192 (unsigned int)pr->req->targetlen, target,
193 (unsigned long long)pr->req->datalen, data);
197 inline struct peer_req *alloc_peer_req(struct peerd *peer)
199 xqindex idx = xq_pop_head(&peer->free_reqs, 1);
202 return peer->peer_reqs + idx;
205 inline void free_peer_req(struct peerd *peer, struct peer_req *pr)
207 xqindex idx = pr - peer->peer_reqs;
209 xq_append_head(&peer->free_reqs, idx, 1);
212 struct timeval resp_start, resp_end, resp_accum = {0, 0};
213 uint64_t responds = 0;
214 void get_responds_stats(){
215 printf("Time waiting respond %lu.%06lu sec for %llu times.\n",
216 //(unsigned int)(t - peer->thread),
217 resp_accum.tv_sec, resp_accum.tv_usec, (long long unsigned int) responds);
221 void fail(struct peerd *peer, struct peer_req *pr)
223 struct xseg_request *req = pr->req;
225 XSEGLOG2(&lc, D, "failing req %u", (unsigned int) (pr - peer->peer_reqs));
226 req->state |= XS_FAILED;
227 //xseg_set_req_data(peer->xseg, pr->req, NULL);
228 p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
229 xseg_signal(peer->xseg, p);
230 free_peer_req(peer, pr);
232 wake_up_next_thread(peer);
237 void complete(struct peerd *peer, struct peer_req *pr)
239 struct xseg_request *req = pr->req;
241 req->state |= XS_SERVED;
242 //xseg_set_req_data(peer->xseg, pr->req, NULL);
243 //gettimeofday(&resp_start, NULL);
244 p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
245 //gettimeofday(&resp_end, NULL);
247 //timersub(&resp_end, &resp_start, &resp_end);
248 //timeradd(&resp_end, &resp_accum, &resp_accum);
249 //printf("xseg_signal: %u\n", p);
250 xseg_signal(peer->xseg, p);
251 free_peer_req(peer, pr);
253 wake_up_next_thread(peer);
257 static void handle_accepted(struct peerd *peer, struct peer_req *pr,
258 struct xseg_request *req)
260 struct xseg_request *xreq = pr->req;
261 //assert xreq == req;
262 XSEGLOG2(&lc, D, "Handle accepted");
264 //xreq->state = XS_ACCEPTED;
266 dispatch(peer, pr, req, dispatch_accept);
269 static void handle_received(struct peerd *peer, struct peer_req *pr,
270 struct xseg_request *req)
272 //struct xseg_request *req = pr->req;
273 //assert req->state != XS_ACCEPTED;
274 XSEGLOG2(&lc, D, "Handle received \n");
275 dispatch(peer, pr, req, dispatch_receive);
278 struct timeval sub_start, sub_end, sub_accum = {0, 0};
279 uint64_t submits = 0;
280 void get_submits_stats(){
281 printf("Time waiting submit %lu.%06lu sec for %llu times.\n",
282 //(unsigned int)(t - peer->thread),
283 sub_accum.tv_sec, sub_accum.tv_usec, (long long unsigned int) submits);
286 int submit_peer_req(struct peerd *peer, struct peer_req *pr)
289 struct xseg_request *req = pr->req;
290 // assert req->portno == peer->portno ?
291 //TODO small function with error checking
292 XSEGLOG2 (&lc, D, "submitting peer req %u\n", (unsigned int)(pr - peer->peer_reqs));
293 ret = xseg_set_req_data(peer->xseg, req, (void *)(pr));
296 //printf("pr: %x , req_data: %x \n", pr, xseg_get_req_data(peer->xseg, req));
297 //gettimeofday(&sub_start, NULL);
298 ret = xseg_submit(peer->xseg, req, pr->portno, X_ALLOC);
299 //gettimeofday(&sub_end, NULL);
301 //timersub(&sub_end, &sub_start, &sub_end);
302 //timeradd(&sub_end, &sub_accum, &sub_accum);
305 xseg_signal(peer->xseg, ret);
309 static int check_ports(struct peerd *peer)
311 struct xseg *xseg = peer->xseg;
312 xport portno_start = peer->portno_start;
313 xport portno_end = peer->portno_end;
314 struct xseg_request *accepted, *received;
319 for (i = portno_start; i <= portno_end; i++) {
322 if (!isTerminate()) {
323 pr = alloc_peer_req(peer);
325 accepted = xseg_accept(xseg, i, X_NONBLOCK);
329 xseg_cancel_wait(xseg, i);
330 handle_accepted(peer, pr, accepted);
334 free_peer_req(peer, pr);
338 received = xseg_receive(xseg, i, X_NONBLOCK);
340 r = xseg_get_req_data(xseg, received, (void **) &pr);
342 XSEGLOG2(&lc, W, "Received request with no pr data\n");
343 xport p = xseg_respond(peer->xseg, received, peer->portno_start, X_ALLOC);
345 XSEGLOG2(&lc, W, "Could not respond stale request");
346 xseg_put_request(xseg, received, portno_start);
349 xseg_signal(xseg, p);
352 //maybe perform sanity check for pr
353 xseg_cancel_wait(xseg, i);
354 handle_received(peer, pr, received);
364 int thread_execute(struct peerd *peer, void (*func)(void *arg), void *arg)
366 struct thread *t = alloc_thread(peer);
373 // we could hijack a thread
377 static void* thread_loop(void *arg)
379 struct thread *t = (struct thread *) arg;
380 struct peerd *peer = t->peer;
381 struct xseg *xseg = peer->xseg;
382 xport portno_start = peer->portno_start;
383 xport portno_end = peer->portno_end;
384 pid_t pid =syscall(SYS_gettid);
386 uint64_t threshold=1000/(1 + portno_end - portno_start);
388 XSEGLOG2(&lc, D, "thread %u\n", (unsigned int) (t- peer->thread));
390 XSEGLOG2(&lc, I, "Thread %u has tid %u.\n", (unsigned int) (t- peer->thread), pid);
391 xseg_init_local_signal(xseg, peer->portno_start);
392 for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
394 XSEGLOG2(&lc, D, "Thread %u executes function\n", (unsigned int) (t- peer->thread));
395 xseg_cancel_wait(xseg, peer->portno_start);
402 for(loops= threshold; loops > 0; loops--) {
404 xseg_prepare_wait(xseg, peer->portno_start);
405 if (check_ports(peer))
408 XSEGLOG2(&lc, I, "Thread %u goes to sleep\n", (unsigned int) (t- peer->thread));
409 xseg_wait_signal(xseg, 10000000UL);
410 xseg_cancel_wait(xseg, peer->portno_start);
411 XSEGLOG2(&lc, I, "Thread %u woke up\n", (unsigned int) (t- peer->thread));
413 wake_up_next_thread(peer);
417 int peerd_start_threads(struct peerd *peer)
420 uint32_t nr_threads = peer->nr_threads;
422 for (i = 0; i < nr_threads; i++) {
423 peer->thread[i].peer = peer;
424 pthread_cond_init(&peer->thread[i].cond,NULL);
425 pthread_mutex_init(&peer->thread[i].lock, NULL);
426 pthread_create(&peer->thread[i].tid, NULL, thread_loop, (void *)(peer->thread + i));
427 peer->thread[i].func = NULL;
428 peer->thread[i].arg = NULL;
435 void defer_request(struct peerd *peer, struct peer_req *pr)
437 // assert canDefer(peer);
438 // xseg_submit(peer->xseg, peer->defer_portno, pr->req);
439 // xseg_signal(peer->xseg, peer->defer_portno);
440 // free_peer_req(peer, pr);
443 static int peerd_loop(struct peerd *peer)
447 if (peer->interactive_func)
448 peer->interactive_func();
449 for (i = 0; i < peer->nr_threads; i++) {
450 pthread_join(peer->thread[i].tid, NULL);
453 struct xseg *xseg = peer->xseg;
454 xport portno_start = peer->portno_start;
455 xport portno_end = peer->portno_end;
456 uint64_t threshold=1000/(1 + portno_end - portno_start);
457 pid_t pid =syscall(SYS_gettid);
460 XSEGLOG2(&lc, I, "Peer has tid %u.\n", pid);
461 xseg_init_local_signal(xseg, peer->portno_start);
462 for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
463 for(loops= threshold; loops > 0; loops--) {
465 xseg_prepare_wait(xseg, peer->portno_start);
466 if (check_ports(peer))
474 XSEGLOG2(&lc, I, "Peer goes to sleep\n");
475 xseg_wait_signal(xseg, 10000000UL);
476 xseg_cancel_wait(xseg, peer->portno_start);
477 XSEGLOG2(&lc, I, "Peer woke up\n");
482 xseg_quit_local_signal(xseg, peer->portno_start);
487 static struct xseg *join(char *spec)
489 struct xseg_config config;
492 (void)xseg_parse_spec(spec, &config);
493 xseg = xseg_join(config.type, config.name, PEER_TYPE, NULL);
497 (void)xseg_create(&config);
498 return xseg_join(config.type, config.name, PEER_TYPE, NULL);
501 static struct peerd* peerd_init(uint32_t nr_ops, char* spec, long portno_start,
502 long portno_end, uint32_t nr_threads, uint32_t defer_portno)
506 struct xseg_port *port;
511 peer = malloc(sizeof(struct peerd));
516 peer->nr_ops = nr_ops;
517 peer->defer_portno = defer_portno;
519 peer->nr_threads = nr_threads;
520 peer->thread = calloc(nr_threads, sizeof(struct thread));
524 peer->peer_reqs = calloc(nr_ops, sizeof(struct peer_req));
525 if (!peer->peer_reqs){
531 if (!xq_alloc_seq(&peer->free_reqs, nr_ops, nr_ops))
534 if (!xq_alloc_empty(&peer->threads, nr_threads))
537 if (xseg_initialize()){
538 printf("cannot initialize library\n");
541 peer->xseg = join(spec);
545 peer->portno_start = (xport) portno_start;
546 peer->portno_end= (xport) portno_end;
547 port = xseg_bind_port(peer->xseg, peer->portno_start, NULL);
549 printf("cannot bind to port %u\n", (unsigned int) peer->portno_start);
554 for (p = peer->portno_start + 1; p <= peer->portno_end; p++) {
555 struct xseg_port *tmp;
556 tmp = xseg_bind_port(peer->xseg, p, (void *)xseg_get_signal_desc(peer->xseg, port));
558 printf("cannot bind to port %u\n", (unsigned int) p);
563 printf("Peer on ports %u-%u\n", peer->portno_start,
566 for (i = 0; i < nr_ops; i++) {
567 peer->peer_reqs[i].peer = peer;
568 peer->peer_reqs[i].req = NULL;
569 peer->peer_reqs[i].retval = 0;
570 peer->peer_reqs[i].priv = NULL;
571 peer->peer_reqs[i].portno = NoPort;
573 peer->peer_reqs[i].cond = st_cond_new(); //FIXME err check
577 peer->interactive_func = NULL;
582 int pidfile_remove(char *path, int fd)
585 return (unlink(path));
588 int pidfile_write(int pid_fd)
591 snprintf(buf, sizeof(buf), "%ld", syscall(SYS_gettid));
594 lseek(pid_fd, 0, SEEK_SET);
595 int ret = write(pid_fd, buf, strlen(buf));
599 int pidfile_read(char *path, pid_t *pid)
601 char buf[16], *endptr;
604 int fd = open(path, O_RDONLY);
607 int ret = read(fd, buf, 15);
613 *pid = strtol(buf, &endptr, 10);
614 if (endptr != &buf[ret]){
622 int pidfile_open(char *path, pid_t *old_pid)
625 int fd = open(path, O_CREAT|O_EXCL|O_WRONLY);
627 if (errno == -EEXIST)
628 pidfile_read(path, old_pid);
633 int main(int argc, char *argv[])
635 struct peerd *peer = NULL;
638 int i, r, daemonize = 0;
639 long portno_start = -1, portno_end = -1, portno = -1;
641 uint32_t nr_ops = 16;
642 uint32_t nr_threads = 16 ;
643 unsigned int debug_level = 0;
644 uint32_t defer_portno = NoPort;
645 char *logfile = NULL;
646 char *pidfile = NULL;
650 //capture here -g spec, -n nr_ops, -p portno, -t nr_threads -v verbose level
651 // -dp xseg_portno to defer blocking requests
653 //TODO print messages on arg parsing error
654 //TODO string checking
656 for (i = 1; i < argc; i++) {
657 if (!strcmp(argv[i], "-g") && i + 1 < argc) {
663 if (!strcmp(argv[i], "-sp") && i + 1 < argc) {
664 portno_start = strtoul(argv[i+1], NULL, 10);
669 if (!strcmp(argv[i], "-ep") && i + 1 < argc) {
670 portno_end = strtoul(argv[i+1], NULL, 10);
675 if (!strcmp(argv[i], "-p") && i + 1 < argc) {
676 portno = strtoul(argv[i+1], NULL, 10);
681 if (!strcmp(argv[i], "-n") && i + 1 < argc) {
682 nr_ops = strtoul(argv[i+1], NULL, 10);
686 if (!strcmp(argv[i], "-v") && i + 1 < argc ) {
687 debug_level = atoi(argv[i+1]);
691 if (!strcmp(argv[i], "-t") && i + 1 < argc ) {
692 nr_threads = strtoul(argv[i+1], NULL, 10);
696 if (!strcmp(argv[i], "-dp") && i + 1 < argc ) {
697 defer_portno = strtoul(argv[i+1], NULL, 10);
701 if (!strcmp(argv[i], "-l") && i + 1 < argc ) {
706 if (!strcmp(argv[i], "-d")) {
710 if (!strcmp(argv[i], "--pidfile") && i + 1 < argc ) {
717 r = init_logctx(&lc, argv[0], debug_level, logfile,
718 REDIRECT_STDOUT|REDIRECT_STDERR);
720 XSEGLOG("Cannot initialize logging to logfile");
723 XSEGLOG2(&lc, D, "Main thread has tid %ld.\n", syscall(SYS_gettid));
726 pid_fd = pidfile_open(pidfile, &old_pid);
729 XSEGLOG2(&lc, E, "Daemon already running, pid: %d.", old_pid);
731 XSEGLOG2(&lc, E, "Cannot open or create pidfile");
738 if (daemon(0, 1) < 0){
739 XSEGLOG2(&lc, E, "Cannot daemonize");
745 pidfile_write(pid_fd);
747 //TODO perform argument sanity checks
748 verbose = debug_level;
750 portno_start = portno;
756 peer = peerd_init(nr_ops, spec, portno_start, portno_end, nr_threads, defer_portno);
761 r = custom_peer_init(peer, argc, argv);
765 peerd_start_threads(peer);
769 st_thread_t st = st_thread_create(peerd_loop, peer, 1, 0);
770 r = st_thread_join(st, NULL);
772 r = peerd_loop(peer);
776 pidfile_remove(pidfile, pid_fd);