9 #include <sys/syscall.h>
13 #define REARRANGE(__fun_name__, __format__, ...) __format__ "%s", __fun_name__, ##__VA_ARGS__
14 #define LOG(level, ...) \
16 if (level <= verbose) { \
17 fprintf(stderr, "%s: " REARRANGE( __func__ , ## __VA_ARGS__, "" )); \
22 unsigned int verbose = 0;
30 void (*func)(void *arg);
35 inline int canDefer(struct peerd *peer)
37 return !(peer->defer_portno == NoPort);
40 void print_req(struct xseg *xseg, struct xseg_request *req)
42 char target[64], data[64];
43 char *req_target, *req_data;
44 unsigned int end = (req->targetlen> 63) ? 63 : req->targetlen;
45 req_target = xseg_get_target(xseg, req);
46 req_data = xseg_get_data(xseg, req);
49 strncpy(target, req_target, end);
51 strncpy(data, req_data, 63);
53 printf("req id:%lu, op:%u %llu:%lu serviced: %lu, reqstate: %u\n"
54 "src: %u, st: %u, dst: %u dt: %u\n"
55 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
57 (unsigned int)req->op,
58 (unsigned long long)req->offset,
59 (unsigned long)req->size,
60 (unsigned long)req->serviced,
61 (unsigned int)req->state,
62 (unsigned int)req->src_portno,
63 (unsigned int)req->src_transit_portno,
64 (unsigned int)req->dst_portno,
65 (unsigned int)req->dst_transit_portno,
66 (unsigned int)req->targetlen, target,
67 (unsigned long long)req->datalen, data);
70 void log_pr(char *msg, struct peer_req *pr)
72 char target[64], data[64];
73 char *req_target, *req_data;
74 struct peerd *peer = pr->peer;
75 struct xseg *xseg = pr->peer->xseg;
76 req_target = xseg_get_target(xseg, pr->req);
77 req_data = xseg_get_data(xseg, pr->req);
78 /* null terminate name in case of req->target is less than 63 characters,
79 * and next character after name (aka first byte of next buffer) is not
82 unsigned int end = (pr->req->targetlen> 63) ? 63 : pr->req->targetlen;
84 strncpy(target, req_target, end);
86 strncpy(data, req_data, 63);
88 printf("%s: req id:%u, op:%u %llu:%lu serviced: %lu, retval: %lu, reqstate: %u\n"
89 "target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
91 (unsigned int)(pr - peer->peer_reqs),
92 (unsigned int)pr->req->op,
93 (unsigned long long)pr->req->offset,
94 (unsigned long)pr->req->size,
95 (unsigned long)pr->req->serviced,
96 (unsigned long)pr->retval,
97 (unsigned int)pr->req->state,
98 (unsigned int)pr->req->targetlen, target,
99 (unsigned long long)pr->req->datalen, data);
103 inline struct peer_req *alloc_peer_req(struct peerd *peer)
105 xqindex idx = xq_pop_head(&peer->free_reqs, 1);
108 return peer->peer_reqs + idx;
111 inline void free_peer_req(struct peerd *peer, struct peer_req *pr)
113 xqindex idx = pr - peer->peer_reqs;
115 xq_append_head(&peer->free_reqs, idx, 1);
118 inline static struct thread* alloc_thread(struct peerd *peer)
120 xqindex idx = xq_pop_head(&peer->threads, 1);
123 return peer->thread + idx;
126 inline static void free_thread(struct peerd *peer, struct thread *t)
128 xqindex idx = t - peer->thread;
129 xq_append_head(&peer->threads, idx, 1);
133 inline static void __wake_up_thread(struct thread *t)
135 pthread_mutex_lock(&t->lock);
136 pthread_cond_signal(&t->cond);
137 pthread_mutex_unlock(&t->lock);
140 inline static void wake_up_thread(struct thread* t)
147 inline static int wake_up_next_thread(struct peerd *peer)
149 //struct thread *t = alloc_thread(peer);
152 return (xseg_signal(peer->xseg, peer->portno));
155 struct timeval resp_start, resp_end, resp_accum = {0, 0};
156 uint64_t responds = 0;
157 void get_responds_stats(){
158 printf("Time waiting respond %lu.%06lu sec for %llu times.\n",
159 //(unsigned int)(t - peer->thread),
160 resp_accum.tv_sec, resp_accum.tv_usec, (long long unsigned int) responds);
164 void fail(struct peerd *peer, struct peer_req *pr)
166 struct xseg_request *req = pr->req;
168 XSEGLOG2(&lc, D, "failing req %u", (unsigned int) (pr - peer->peer_reqs));
169 req->state |= XS_FAILED;
170 //xseg_set_req_data(peer->xseg, pr->req, NULL);
171 p = xseg_respond(peer->xseg, req, peer->portno, X_ALLOC);
172 xseg_signal(peer->xseg, p);
173 free_peer_req(peer, pr);
174 wake_up_next_thread(peer);
178 void complete(struct peerd *peer, struct peer_req *pr)
180 struct xseg_request *req = pr->req;
182 req->state |= XS_SERVED;
183 //xseg_set_req_data(peer->xseg, pr->req, NULL);
184 //gettimeofday(&resp_start, NULL);
185 p = xseg_respond(peer->xseg, req, peer->portno, X_ALLOC);
186 //gettimeofday(&resp_end, NULL);
188 //timersub(&resp_end, &resp_start, &resp_end);
189 //timeradd(&resp_end, &resp_accum, &resp_accum);
190 //printf("xseg_signal: %u\n", p);
191 xseg_signal(peer->xseg, p);
192 free_peer_req(peer, pr);
193 wake_up_next_thread(peer);
196 void pending(struct peerd *peer, struct peer_req *pr)
198 pr->req->state = XS_PENDING;
201 static void handle_accepted(struct peerd *peer, struct peer_req *pr,
202 struct xseg_request *req)
204 struct xseg_request *xreq = pr->req;
205 //assert xreq == req;
206 XSEGLOG2(&lc, D, "Handle accepted");
208 //xreq->state = XS_ACCEPTED;
210 dispatch(peer, pr, req);
213 static void handle_received(struct peerd *peer, struct peer_req *pr,
214 struct xseg_request *req)
216 //struct xseg_request *req = pr->req;
217 //assert req->state != XS_ACCEPTED;
218 XSEGLOG2(&lc, D, "Handle received \n");
219 dispatch(peer, pr, req);
222 struct timeval sub_start, sub_end, sub_accum = {0, 0};
223 uint64_t submits = 0;
224 void get_submits_stats(){
225 printf("Time waiting submit %lu.%06lu sec for %llu times.\n",
226 //(unsigned int)(t - peer->thread),
227 sub_accum.tv_sec, sub_accum.tv_usec, (long long unsigned int) submits);
230 int submit_peer_req(struct peerd *peer, struct peer_req *pr)
233 struct xseg_request *req = pr->req;
234 // assert req->portno == peer->portno ?
235 //TODO small function with error checking
236 XSEGLOG2 (&lc, D, "submitting peer req %u\n", (unsigned int)(pr - peer->peer_reqs));
237 ret = xseg_set_req_data(peer->xseg, req, (void *)(pr));
240 //printf("pr: %x , req_data: %x \n", pr, xseg_get_req_data(peer->xseg, req));
241 //gettimeofday(&sub_start, NULL);
242 ret = xseg_submit(peer->xseg, req, peer->portno, X_ALLOC);
243 //gettimeofday(&sub_end, NULL);
245 //timersub(&sub_end, &sub_start, &sub_end);
246 //timeradd(&sub_end, &sub_accum, &sub_accum);
249 xseg_signal(peer->xseg, ret);
253 int thread_execute(struct peerd *peer, void (*func)(void *arg), void *arg)
255 struct thread *t = alloc_thread(peer);
262 // we could hijack a thread
266 static void* thread_loop(void *arg)
268 struct thread *t = (struct thread *) arg;
269 struct peerd *peer = t->peer;
270 struct xseg *xseg = peer->xseg;
271 uint32_t portno = peer->portno;
273 uint64_t threshold=1000;
274 pid_t pid =syscall(SYS_gettid);
276 struct xseg_request *accepted, *received;
279 XSEGLOG2(&lc, D, "thread %u\n", (unsigned int) (t- peer->thread));
281 XSEGLOG2(&lc, I, "Thread %u has tid %u.\n", (unsigned int) (t- peer->thread), pid);
282 xseg_init_local_signal(xseg, portno);
285 XSEGLOG2(&lc, D, "Thread %u executes function\n", (unsigned int) (t- peer->thread));
286 xseg_cancel_wait(xseg, portno);
293 for(loops= threshold; loops > 0; loops--) {
297 xseg_prepare_wait(xseg, portno);
299 // if (xq_count(&peer->xport->request_queue)){
300 pr = alloc_peer_req(peer);
302 accepted = xseg_accept(xseg, peer->portno);
304 XSEGLOG2(&lc, D, "Thread %u accepted\n", (unsigned int) (t- peer->thread));
306 xseg_cancel_wait(xseg, portno);
307 wake_up_next_thread(peer);
308 handle_accepted(peer, pr, accepted);
312 free_peer_req(peer, pr);
316 // if (xq_count(&peer->xport->reply_queue)){
317 received = xseg_receive(xseg, peer->portno);
319 //printf("received req id: %u\n", received - xseg->requests);
320 //print_req(peer->xseg, received);
321 r = xseg_get_req_data(xseg, received, (void **) &pr);
323 //FIXME what to do here ?
324 XSEGLOG2(&lc, W, "Received request with no pr data\n");
325 xseg_respond(peer->xseg, received, peer->portno, X_ALLOC);
328 //fail(peer, received);
329 //assert pr->req == received;
330 xseg_cancel_wait(xseg, portno);
331 wake_up_next_thread(peer);
332 handle_received(peer, pr, received);
337 XSEGLOG2(&lc, I, "Thread %u goes to sleep\n", (unsigned int) (t- peer->thread));
338 xseg_wait_signal(xseg, 10000000UL);
339 xseg_cancel_wait(xseg, portno);
340 XSEGLOG2(&lc, I, "Thread %u woke up\n", (unsigned int) (t- peer->thread));
345 void defer_request(struct peerd *peer, struct peer_req *pr)
347 // assert canDefer(peer);
348 // xseg_submit(peer->xseg, peer->defer_portno, pr->req);
349 // xseg_signal(peer->xseg, peer->defer_portno);
350 // free_peer_req(peer, pr);
353 static int peerd_loop(struct peerd *peer)
355 if (peer->interactive_func)
356 peer->interactive_func();
358 pthread_join(peer->thread[0].tid, NULL);
363 static struct xseg *join(char *spec)
365 struct xseg_config config;
368 (void)xseg_parse_spec(spec, &config);
369 xseg = xseg_join(config.type, config.name, "pthread", NULL);
373 (void)xseg_create(&config);
374 return xseg_join(config.type, config.name, "pthread", NULL);
377 int peerd_start_threads(struct peerd *peer)
380 uint32_t nr_threads = peer->nr_threads;
382 for (i = 0; i < nr_threads; i++) {
383 peer->thread[i].peer = peer;
384 pthread_cond_init(&peer->thread[i].cond,NULL);
385 pthread_mutex_init(&peer->thread[i].lock, NULL);
386 pthread_create(&peer->thread[i].tid, NULL, thread_loop, (void *)(peer->thread + i));
387 peer->thread[i].func = NULL;
388 peer->thread[i].arg = NULL;
394 static struct peerd* peerd_init(uint32_t nr_ops, char* spec, long portno, uint32_t nr_threads, uint32_t defer_portno)
398 peer = malloc(sizeof(struct peerd));
403 peer->nr_ops = nr_ops;
404 peer->defer_portno = defer_portno;
405 peer->nr_threads = nr_threads;
407 peer->thread = calloc(nr_threads, sizeof(struct thread));
410 peer->peer_reqs = calloc(nr_ops, sizeof(struct peer_req));
411 if (!peer->peer_reqs){
417 if (!xq_alloc_seq(&peer->free_reqs, nr_ops, nr_ops))
419 if (!xq_alloc_empty(&peer->threads, nr_threads))
422 if (xseg_initialize()){
423 printf("cannot initialize library\n");
426 peer->xseg = join(spec);
430 peer->xport = xseg_bind_port(peer->xseg, portno, NULL);
432 printf("cannot bind to port %ld\n", portno);
435 printf("%lx\n", (unsigned long) peer->xport);
436 peer->portno = xseg_portno(peer->xseg, peer->xport);
437 printf("Peer on port %u/%u\n", peer->portno,
438 peer->xseg->config.nr_ports);
440 for (i = 0; i < nr_ops; i++) {
441 peer->peer_reqs[i].peer = peer;
442 peer->peer_reqs[i].req = NULL;
443 peer->peer_reqs[i].retval = 0;
444 peer->peer_reqs[i].priv = NULL;
446 peer->interactive_func = NULL;
451 int main(int argc, char *argv[])
453 struct peerd *peer = NULL;
459 uint32_t nr_ops = 16;
460 uint32_t nr_threads = 16 ;
461 unsigned int debug_level = 0;
462 uint32_t defer_portno = NoPort;
463 char *logfile = NULL;
465 //capture here -g spec, -n nr_ops, -p portno, -t nr_threads -v verbose level
466 // -dp xseg_portno to defer blocking requests
468 //TODO print messages on arg parsing error
470 for (i = 1; i < argc; i++) {
471 if (!strcmp(argv[i], "-g") && i + 1 < argc) {
477 if (!strcmp(argv[i], "-p") && i + 1 < argc) {
478 portno = strtoul(argv[i+1], NULL, 10);
483 if (!strcmp(argv[i], "-n") && i + 1 < argc) {
484 nr_ops = strtoul(argv[i+1], NULL, 10);
488 if (!strcmp(argv[i], "-v") && i + 1 < argc ) {
489 debug_level = atoi(argv[i+1]);
493 if (!strcmp(argv[i], "-t") && i + 1 < argc ) {
494 nr_threads = strtoul(argv[i+1], NULL, 10);
498 if (!strcmp(argv[i], "-dp") && i + 1 < argc ) {
499 defer_portno = strtoul(argv[i+1], NULL, 10);
503 if (!strcmp(argv[i], "-l") && i + 1 < argc ) {
510 init_logctx(&lc, argv[0], debug_level, logfile);
511 XSEGLOG2(&lc, D, "Main thread has tid %ld.\n", syscall(SYS_gettid));
513 //TODO perform argument sanity checks
514 verbose = debug_level;
517 peer = peerd_init(nr_ops, spec, portno, nr_threads, defer_portno);
520 r = custom_peer_init(peer, argc, argv);
523 peerd_start_threads(peer);
524 return peerd_loop(peer);