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 static int setup_signals()
102 sigemptyset(&sa.sa_mask);
104 sa.sa_handler = signal_handler;
105 r = sigaction(SIGTERM, &sa, NULL);
108 r = sigaction(SIGINT, &sa, NULL);
111 r = sigaction(SIGQUIT, &sa, NULL);
117 inline int canDefer(struct peerd *peer)
119 return !(peer->defer_portno == NoPort);
122 void print_req(struct xseg *xseg, struct xseg_request *req)
124 char target[64], data[64];
125 char *req_target, *req_data;
126 unsigned int end = (req->targetlen> 63) ? 63 : req->targetlen;
127 req_target = xseg_get_target(xseg, req);
128 req_data = xseg_get_data(xseg, req);
131 strncpy(target, req_target, end);
133 strncpy(data, req_data, 63);
135 printf("req id:%lu, op:%u %llu:%lu serviced: %lu, reqstate: %u\n"
136 "src: %u, st: %u, dst: %u dt: %u\n"
137 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
138 (unsigned long)(req),
139 (unsigned int)req->op,
140 (unsigned long long)req->offset,
141 (unsigned long)req->size,
142 (unsigned long)req->serviced,
143 (unsigned int)req->state,
144 (unsigned int)req->src_portno,
145 (unsigned int)req->src_transit_portno,
146 (unsigned int)req->dst_portno,
147 (unsigned int)req->dst_transit_portno,
148 (unsigned int)req->targetlen, target,
149 (unsigned long long)req->datalen, data);
152 void log_pr(char *msg, struct peer_req *pr)
154 char target[64], data[64];
155 char *req_target, *req_data;
156 struct peerd *peer = pr->peer;
157 struct xseg *xseg = pr->peer->xseg;
158 req_target = xseg_get_target(xseg, pr->req);
159 req_data = xseg_get_data(xseg, pr->req);
160 /* null terminate name in case of req->target is less than 63 characters,
161 * and next character after name (aka first byte of next buffer) is not
164 unsigned int end = (pr->req->targetlen> 63) ? 63 : pr->req->targetlen;
166 strncpy(target, req_target, end);
168 strncpy(data, req_data, 63);
170 printf("%s: req id:%u, op:%u %llu:%lu serviced: %lu, retval: %lu, reqstate: %u\n"
171 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
173 (unsigned int)(pr - peer->peer_reqs),
174 (unsigned int)pr->req->op,
175 (unsigned long long)pr->req->offset,
176 (unsigned long)pr->req->size,
177 (unsigned long)pr->req->serviced,
178 (unsigned long)pr->retval,
179 (unsigned int)pr->req->state,
180 (unsigned int)pr->req->targetlen, target,
181 (unsigned long long)pr->req->datalen, data);
185 inline struct peer_req *alloc_peer_req(struct peerd *peer)
187 xqindex idx = xq_pop_head(&peer->free_reqs, 1);
190 return peer->peer_reqs + idx;
193 inline void free_peer_req(struct peerd *peer, struct peer_req *pr)
195 xqindex idx = pr - peer->peer_reqs;
197 xq_append_head(&peer->free_reqs, idx, 1);
200 struct timeval resp_start, resp_end, resp_accum = {0, 0};
201 uint64_t responds = 0;
202 void get_responds_stats(){
203 printf("Time waiting respond %lu.%06lu sec for %llu times.\n",
204 //(unsigned int)(t - peer->thread),
205 resp_accum.tv_sec, resp_accum.tv_usec, (long long unsigned int) responds);
209 void fail(struct peerd *peer, struct peer_req *pr)
211 struct xseg_request *req = pr->req;
213 XSEGLOG2(&lc, D, "failing req %u", (unsigned int) (pr - peer->peer_reqs));
214 req->state |= XS_FAILED;
215 //xseg_set_req_data(peer->xseg, pr->req, NULL);
216 p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
217 xseg_signal(peer->xseg, p);
218 free_peer_req(peer, pr);
220 wake_up_next_thread(peer);
225 void complete(struct peerd *peer, struct peer_req *pr)
227 struct xseg_request *req = pr->req;
229 req->state |= XS_SERVED;
230 //xseg_set_req_data(peer->xseg, pr->req, NULL);
231 //gettimeofday(&resp_start, NULL);
232 p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
233 //gettimeofday(&resp_end, NULL);
235 //timersub(&resp_end, &resp_start, &resp_end);
236 //timeradd(&resp_end, &resp_accum, &resp_accum);
237 //printf("xseg_signal: %u\n", p);
238 xseg_signal(peer->xseg, p);
239 free_peer_req(peer, pr);
241 wake_up_next_thread(peer);
245 static void handle_accepted(struct peerd *peer, struct peer_req *pr,
246 struct xseg_request *req)
248 struct xseg_request *xreq = pr->req;
249 //assert xreq == req;
250 XSEGLOG2(&lc, D, "Handle accepted");
252 //xreq->state = XS_ACCEPTED;
254 dispatch(peer, pr, req, dispatch_accept);
257 static void handle_received(struct peerd *peer, struct peer_req *pr,
258 struct xseg_request *req)
260 //struct xseg_request *req = pr->req;
261 //assert req->state != XS_ACCEPTED;
262 XSEGLOG2(&lc, D, "Handle received \n");
263 dispatch(peer, pr, req, dispatch_receive);
266 struct timeval sub_start, sub_end, sub_accum = {0, 0};
267 uint64_t submits = 0;
268 void get_submits_stats(){
269 printf("Time waiting submit %lu.%06lu sec for %llu times.\n",
270 //(unsigned int)(t - peer->thread),
271 sub_accum.tv_sec, sub_accum.tv_usec, (long long unsigned int) submits);
274 int submit_peer_req(struct peerd *peer, struct peer_req *pr)
277 struct xseg_request *req = pr->req;
278 // assert req->portno == peer->portno ?
279 //TODO small function with error checking
280 XSEGLOG2 (&lc, D, "submitting peer req %u\n", (unsigned int)(pr - peer->peer_reqs));
281 ret = xseg_set_req_data(peer->xseg, req, (void *)(pr));
284 //printf("pr: %x , req_data: %x \n", pr, xseg_get_req_data(peer->xseg, req));
285 //gettimeofday(&sub_start, NULL);
286 ret = xseg_submit(peer->xseg, req, pr->portno, X_ALLOC);
287 //gettimeofday(&sub_end, NULL);
289 //timersub(&sub_end, &sub_start, &sub_end);
290 //timeradd(&sub_end, &sub_accum, &sub_accum);
293 xseg_signal(peer->xseg, ret);
297 static int check_ports(struct peerd *peer)
299 struct xseg *xseg = peer->xseg;
300 xport portno_start = peer->portno_start;
301 xport portno_end = peer->portno_end;
302 struct xseg_request *accepted, *received;
307 for (i = portno_start; i <= portno_end; i++) {
310 if (!isTerminate()) {
311 pr = alloc_peer_req(peer);
313 accepted = xseg_accept(xseg, i, X_NONBLOCK);
317 xseg_cancel_wait(xseg, i);
318 handle_accepted(peer, pr, accepted);
322 free_peer_req(peer, pr);
326 received = xseg_receive(xseg, i, X_NONBLOCK);
328 r = xseg_get_req_data(xseg, received, (void **) &pr);
330 XSEGLOG2(&lc, W, "Received request with no pr data\n");
331 xport p = xseg_respond(peer->xseg, received, peer->portno_start, X_ALLOC);
333 XSEGLOG2(&lc, W, "Could not respond stale request");
334 xseg_put_request(xseg, received, portno_start);
337 xseg_signal(xseg, p);
340 //maybe perform sanity check for pr
341 xseg_cancel_wait(xseg, i);
342 handle_received(peer, pr, received);
352 int thread_execute(struct peerd *peer, void (*func)(void *arg), void *arg)
354 struct thread *t = alloc_thread(peer);
361 // we could hijack a thread
365 static void* thread_loop(void *arg)
367 struct thread *t = (struct thread *) arg;
368 struct peerd *peer = t->peer;
369 struct xseg *xseg = peer->xseg;
370 xport portno_start = peer->portno_start;
371 xport portno_end = peer->portno_end;
372 pid_t pid =syscall(SYS_gettid);
374 uint64_t threshold=1000/(1 + portno_end - portno_start);
376 XSEGLOG2(&lc, D, "thread %u\n", (unsigned int) (t- peer->thread));
378 XSEGLOG2(&lc, I, "Thread %u has tid %u.\n", (unsigned int) (t- peer->thread), pid);
379 xseg_init_local_signal(xseg, peer->portno_start);
380 for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
382 XSEGLOG2(&lc, D, "Thread %u executes function\n", (unsigned int) (t- peer->thread));
383 xseg_cancel_wait(xseg, peer->portno_start);
390 for(loops= threshold; loops > 0; loops--) {
392 xseg_prepare_wait(xseg, peer->portno_start);
393 if (check_ports(peer))
396 XSEGLOG2(&lc, I, "Thread %u goes to sleep\n", (unsigned int) (t- peer->thread));
397 xseg_wait_signal(xseg, 10000000UL);
398 xseg_cancel_wait(xseg, peer->portno_start);
399 XSEGLOG2(&lc, I, "Thread %u woke up\n", (unsigned int) (t- peer->thread));
401 wake_up_next_thread(peer);
405 int peerd_start_threads(struct peerd *peer)
408 uint32_t nr_threads = peer->nr_threads;
410 for (i = 0; i < nr_threads; i++) {
411 peer->thread[i].peer = peer;
412 pthread_cond_init(&peer->thread[i].cond,NULL);
413 pthread_mutex_init(&peer->thread[i].lock, NULL);
414 pthread_create(&peer->thread[i].tid, NULL, thread_loop, (void *)(peer->thread + i));
415 peer->thread[i].func = NULL;
416 peer->thread[i].arg = NULL;
423 void defer_request(struct peerd *peer, struct peer_req *pr)
425 // assert canDefer(peer);
426 // xseg_submit(peer->xseg, peer->defer_portno, pr->req);
427 // xseg_signal(peer->xseg, peer->defer_portno);
428 // free_peer_req(peer, pr);
431 static int peerd_loop(struct peerd *peer)
435 if (peer->interactive_func)
436 peer->interactive_func();
437 for (i = 0; i < peer->nr_threads; i++) {
438 pthread_join(peer->thread[i].tid, NULL);
441 struct xseg *xseg = peer->xseg;
442 xport portno_start = peer->portno_start;
443 xport portno_end = peer->portno_end;
444 uint64_t threshold=1000/(1 + portno_end - portno_start);
445 pid_t pid =syscall(SYS_gettid);
448 XSEGLOG2(&lc, I, "Peer has tid %u.\n", pid);
449 xseg_init_local_signal(xseg, peer->portno_start);
450 for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
451 for(loops= threshold; loops > 0; loops--) {
453 xseg_prepare_wait(xseg, peer->portno_start);
454 if (check_ports(peer))
462 XSEGLOG2(&lc, I, "Peer goes to sleep\n");
463 xseg_wait_signal(xseg, 10000000UL);
464 xseg_cancel_wait(xseg, peer->portno_start);
465 XSEGLOG2(&lc, I, "Peer woke up\n");
470 xseg_quit_local_signal(xseg, peer->portno_start);
475 static struct xseg *join(char *spec)
477 struct xseg_config config;
480 (void)xseg_parse_spec(spec, &config);
481 xseg = xseg_join(config.type, config.name, PEER_TYPE, NULL);
485 (void)xseg_create(&config);
486 return xseg_join(config.type, config.name, PEER_TYPE, NULL);
489 static struct peerd* peerd_init(uint32_t nr_ops, char* spec, long portno_start,
490 long portno_end, uint32_t nr_threads, uint32_t defer_portno)
494 struct xseg_port *port;
499 peer = malloc(sizeof(struct peerd));
504 peer->nr_ops = nr_ops;
505 peer->defer_portno = defer_portno;
507 peer->nr_threads = nr_threads;
508 peer->thread = calloc(nr_threads, sizeof(struct thread));
512 peer->peer_reqs = calloc(nr_ops, sizeof(struct peer_req));
513 if (!peer->peer_reqs){
519 if (!xq_alloc_seq(&peer->free_reqs, nr_ops, nr_ops))
522 if (!xq_alloc_empty(&peer->threads, nr_threads))
525 if (xseg_initialize()){
526 printf("cannot initialize library\n");
529 peer->xseg = join(spec);
533 peer->portno_start = (xport) portno_start;
534 peer->portno_end= (xport) portno_end;
535 port = xseg_bind_port(peer->xseg, peer->portno_start, NULL);
537 printf("cannot bind to port %u\n", (unsigned int) peer->portno_start);
542 for (p = peer->portno_start + 1; p <= peer->portno_end; p++) {
543 struct xseg_port *tmp;
544 tmp = xseg_bind_port(peer->xseg, p, (void *)xseg_get_signal_desc(peer->xseg, port));
546 printf("cannot bind to port %u\n", (unsigned int) p);
551 printf("Peer on ports %u-%u\n", peer->portno_start,
554 for (i = 0; i < nr_ops; i++) {
555 peer->peer_reqs[i].peer = peer;
556 peer->peer_reqs[i].req = NULL;
557 peer->peer_reqs[i].retval = 0;
558 peer->peer_reqs[i].priv = NULL;
559 peer->peer_reqs[i].portno = NoPort;
561 peer->peer_reqs[i].cond = st_cond_new(); //FIXME err check
565 peer->interactive_func = NULL;
570 int pidfile_remove(char *path, int fd)
573 return (unlink(path));
576 int pidfile_write(int pid_fd)
579 snprintf(buf, sizeof(buf), "%ld", syscall(SYS_gettid));
582 lseek(pid_fd, 0, SEEK_SET);
583 int ret = write(pid_fd, buf, strlen(buf));
587 int pidfile_read(char *path, pid_t *pid)
589 char buf[16], *endptr;
592 int fd = open(path, O_RDONLY);
595 int ret = read(fd, buf, 15);
601 *pid = strtol(buf, &endptr, 10);
602 if (endptr != &buf[ret]){
610 int pidfile_open(char *path, pid_t *old_pid)
613 int fd = open(path, O_CREAT|O_EXCL|O_WRONLY);
615 if (errno == -EEXIST)
616 pidfile_read(path, old_pid);
621 int main(int argc, char *argv[])
623 struct peerd *peer = NULL;
626 int i, r, daemonize = 0;
627 long portno_start = -1, portno_end = -1, portno = -1;
629 uint32_t nr_ops = 16;
630 uint32_t nr_threads = 16 ;
631 unsigned int debug_level = 0;
632 uint32_t defer_portno = NoPort;
633 char *logfile = NULL;
634 char *pidfile = NULL;
638 //capture here -g spec, -n nr_ops, -p portno, -t nr_threads -v verbose level
639 // -dp xseg_portno to defer blocking requests
641 //TODO print messages on arg parsing error
642 //TODO string checking
644 for (i = 1; i < argc; i++) {
645 if (!strcmp(argv[i], "-g") && i + 1 < argc) {
651 if (!strcmp(argv[i], "-sp") && i + 1 < argc) {
652 portno_start = strtoul(argv[i+1], NULL, 10);
657 if (!strcmp(argv[i], "-ep") && i + 1 < argc) {
658 portno_end = strtoul(argv[i+1], NULL, 10);
663 if (!strcmp(argv[i], "-p") && i + 1 < argc) {
664 portno = strtoul(argv[i+1], NULL, 10);
669 if (!strcmp(argv[i], "-n") && i + 1 < argc) {
670 nr_ops = strtoul(argv[i+1], NULL, 10);
674 if (!strcmp(argv[i], "-v") && i + 1 < argc ) {
675 debug_level = atoi(argv[i+1]);
679 if (!strcmp(argv[i], "-t") && i + 1 < argc ) {
680 nr_threads = strtoul(argv[i+1], NULL, 10);
684 if (!strcmp(argv[i], "-dp") && i + 1 < argc ) {
685 defer_portno = strtoul(argv[i+1], NULL, 10);
689 if (!strcmp(argv[i], "-l") && i + 1 < argc ) {
694 if (!strcmp(argv[i], "-d")) {
698 if (!strcmp(argv[i], "--pidfile") && i + 1 < argc ) {
705 r = init_logctx(&lc, argv[0], debug_level, logfile);
707 XSEGLOG("Cannot initialize logging to logfile");
712 dup2(fileno(lc.logfile), 1);
713 dup2(fileno(lc.logfile), 2);
715 XSEGLOG2(&lc, D, "Main thread has tid %ld.\n", syscall(SYS_gettid));
718 pid_fd = pidfile_open(pidfile, &old_pid);
721 XSEGLOG2(&lc, E, "Daemon already running, pid: %d.", old_pid);
723 XSEGLOG2(&lc, E, "Cannot open or create pidfile");
730 if (daemon(0, 1) < 0){
731 XSEGLOG2(&lc, E, "Cannot daemonize");
737 pidfile_write(pid_fd);
739 //TODO perform argument sanity checks
740 verbose = debug_level;
742 portno_start = portno;
748 peer = peerd_init(nr_ops, spec, portno_start, portno_end, nr_threads, defer_portno);
753 r = custom_peer_init(peer, argc, argv);
757 peerd_start_threads(peer);
761 st_thread_t st = st_thread_create(peerd_loop, peer, 1, 0);
762 r = st_thread_join(st, NULL);
764 r = peerd_loop(peer);
768 pidfile_remove(pidfile, pid_fd);