-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpeer.c
1127 lines (1014 loc) · 30.2 KB
/
peer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2010-2014 GRNET S.A.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sched.h>
#include <pwd.h>
#include <grp.h>
#include <xseg/xseg.h>
#ifdef MT
#include <pthread.h>
#endif
#include "peer.h"
#ifdef MT
#ifdef ST_THREADS
#error "MT and ST_THREADS defines are mutually exclusive"
#endif
#endif
#define PEER_TYPE "posixfd"
//FIXME this should not be defined here probably
#define MAX_SPEC_LEN 128
#define MAX_PIDFILE_LEN 512
#define MAX_CPUS_LEN 512
/* Define the cpus on which the threads/process will be pinned */
struct cpu_list {
int cpus[48];
int len;
};
struct cpu_list cpu_list;
volatile unsigned int terminated = 0;
unsigned int verbose = 0;
struct log_ctx lc;
#ifdef ST_THREADS
uint32_t ta = 0;
#endif
#ifdef MT
struct peerd *global_peer;
static pthread_key_t threadkey;
inline static int wake_up_next_thread(struct peerd *peer)
{
return (xseg_signal(peer->xseg, peer->portno_start));
}
#endif
/*
* extern is needed if this function is going to be called by another file
* such as bench-xseg.c
*/
void signal_handler(int signal)
{
// XSEGLOG2(&lc, I, "Caught signal. Terminating gracefully");
terminated = 1;
#ifdef MT
wake_up_next_thread(global_peer);
#endif
}
/*
* We want to both print the backtrace and dump the core. To do so, we fork a
* process that prints its stack trace, that should be the same as the parents.
* Then we wait for 1 sec and we abort. The reason we don't abort immediately
* is because it may interrupt the printing of the backtrace.
*/
void segv_handler(int signal)
{
if (fork() == 0) {
xseg_printtrace();
_exit(1);
}
sleep(1);
abort();
}
void renew_logfile(int signal)
{
// XSEGLOG2(&lc, I, "Caught signal. Renewing logfile");
renew_logctx(&lc, NULL, verbose, NULL, REOPEN_FILE);
}
static int setup_signals(struct peerd *peer)
{
int r;
struct sigaction sa;
struct rlimit rlim;
#ifdef MT
global_peer = peer;
#endif
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = signal_handler;
r = sigaction(SIGTERM, &sa, NULL);
if (r < 0) {
return r;
}
r = sigaction(SIGINT, &sa, NULL);
if (r < 0) {
return r;
}
r = sigaction(SIGQUIT, &sa, NULL);
if (r < 0) {
return r;
}
/*
* Get the current limits for core files and raise them to the largest
* value possible
*/
if (getrlimit(RLIMIT_CORE, &rlim) < 0) {
return r;
}
rlim.rlim_cur = rlim.rlim_max;
if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
return r;
}
/* Install handler for segfaults */
sa.sa_handler = segv_handler;
r = sigaction(SIGSEGV, &sa, NULL);
if (r < 0) {
return r;
}
sa.sa_handler = renew_logfile;
r = sigaction(SIGUSR1, &sa, NULL);
return r;
}
inline int canDefer(struct peerd *peer)
{
return !(peer->defer_portno == NoPort);
}
void print_req(struct xseg *xseg, struct xseg_request *req)
{
char target[64], data[64];
char *req_target, *req_data;
unsigned int end = (req->targetlen > 63) ? 63 : req->targetlen;
req_target = xseg_get_target(xseg, req);
req_data = xseg_get_data(xseg, req);
strncpy(target, req_target, end);
target[end] = '\0';
strncpy(data, req_data, 63);
data[63] = '\0';
printf("req id:%lu, op:%u %llu:%lu serviced: %lu, reqstate: %u\n"
"src: %u, transit: %u, dst: %u effective dst: %u\n"
"target[%u]:'%s', data[%llu]:\n%s------------------\n\n",
(unsigned long) (req),
(unsigned int) req->op,
(unsigned long long) req->offset,
(unsigned long) req->size,
(unsigned long) req->serviced,
(unsigned int) req->state,
(unsigned int) req->src_portno,
(unsigned int) req->transit_portno,
(unsigned int) req->dst_portno,
(unsigned int) req->effective_dst_portno,
(unsigned int) req->targetlen, target,
(unsigned long long) req->datalen, data);
}
void log_pr(char *msg, struct peer_req *pr)
{
char target[64], data[64];
char *req_target, *req_data;
struct peerd *peer = pr->peer;
struct xseg *xseg = pr->peer->xseg;
req_target = xseg_get_target(xseg, pr->req);
req_data = xseg_get_data(xseg, pr->req);
/* null terminate name in case of req->target is less than 63 characters,
* and next character after name (aka first byte of next buffer) is not
* null
*/
unsigned int end = (pr->req->targetlen > 63) ? 63 : pr->req->targetlen;
if (verbose) {
strncpy(target, req_target, end);
target[end] = '\0';
strncpy(data, req_data, 63);
data[63] = '\0';
printf
("%s: req id:%u, op:%u %llu:%lu serviced: %lu, retval: %lu, reqstate: %u\n"
"target[%u]:'%s', data[%llu]:\n%s------------------\n\n", msg,
(unsigned int) (pr - peer->peer_reqs), (unsigned int) pr->req->op,
(unsigned long long) pr->req->offset,
(unsigned long) pr->req->size, (unsigned long) pr->req->serviced,
(unsigned long) pr->retval, (unsigned int) pr->req->state,
(unsigned int) pr->req->targetlen, target,
(unsigned long long) pr->req->datalen, data);
}
}
#ifdef MT
inline struct peer_req *alloc_peer_req(struct peerd *peer, struct thread *t)
{
struct peer_req *pr;
xqindex idx = xq_pop_head(&t->free_thread_reqs);
if (idx != Noneidx) {
goto out;
}
/* try to steal from another thread */
/*
int i;
struct thread *nt;
for (i = t->thread_no + 1; i < (t->thread_no + peer->nr_threads); i++) {
nt = &peer->thread[(t->thread_no + i) % peer->nr_threads];
if (!xq_count(&nt->free_thread_reqs)) {
continue;
}
idx = xq_pop_head(&nt->free_thread_reqs);
if (idx != Noneidx) {
goto out;
}
}
*/
return NULL;
out:
pr = peer->peer_reqs + idx;
pr->thread_no = t - peer->thread;
return pr;
}
#else
/*
* free_reqs is a queue that simply contains pointer offsets to the peer_reqs
* queue. If a pointer from peer_reqs is popped, we are certain that the
* associated memory in peer_reqs is free to use
*/
inline struct peer_req *alloc_peer_req(struct peerd *peer)
{
xqindex idx = xq_pop_head(&peer->free_reqs);
if (idx == Noneidx) {
return NULL;
}
return peer->peer_reqs + idx;
}
#endif
inline void free_peer_req(struct peerd *peer, struct peer_req *pr)
{
xqindex idx = pr - peer->peer_reqs;
pr->req = NULL;
#ifdef MT
struct thread *t = &peer->thread[pr->thread_no];
xq_append_head(&t->free_thread_reqs, idx);
#else
xq_append_head(&peer->free_reqs, idx);
#endif
}
/*
* Count all free reqs in peer.
* Racy, if multithreaded, but the sum should monotonicly increase when checked
* after a termination signal is catched.
*/
int all_peer_reqs_free(struct peerd *peer)
{
uint32_t free_reqs = 0;
#ifdef MT
int i;
for (i = 0; i < peer->nr_threads; i++) {
free_reqs += xq_count(&peer->thread[i].free_thread_reqs);
}
#else
free_reqs = xq_count(&peer->free_reqs);
#endif
if (free_reqs == peer->nr_ops) {
return 1;
}
return 0;
}
struct timeval resp_start, resp_end, resp_accum = { 0, 0 };
uint64_t responds = 0;
void get_responds_stats()
{
printf("Time waiting respond %lu.%06lu sec for %llu times.\n",
//(unsigned int)(t - peer->thread),
resp_accum.tv_sec, resp_accum.tv_usec,
(long long unsigned int) responds);
}
//FIXME error check
void fail(struct peerd *peer, struct peer_req *pr)
{
struct xseg_request *req = pr->req;
uint32_t p;
if (req) {
XSEGLOG2(&lc, D, "failing req %u",
(unsigned int) (pr - peer->peer_reqs));
req->state |= XS_FAILED;
//xseg_set_req_data(peer->xseg, pr->req, NULL);
p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
xseg_signal(peer->xseg, p);
}
free_peer_req(peer, pr);
#ifdef MT
wake_up_next_thread(peer);
#endif
}
//FIXME error check
void complete(struct peerd *peer, struct peer_req *pr)
{
struct xseg_request *req = pr->req;
uint32_t p;
if (req) {
req->state |= XS_SERVED;
//xseg_set_req_data(peer->xseg, pr->req, NULL);
//gettimeofday(&resp_start, NULL);
p = xseg_respond(peer->xseg, req, pr->portno, X_ALLOC);
//gettimeofday(&resp_end, NULL);
//responds++;
//timersub(&resp_end, &resp_start, &resp_end);
//timeradd(&resp_end, &resp_accum, &resp_accum);
//printf("xseg_signal: %u\n", p);
xseg_signal(peer->xseg, p);
}
free_peer_req(peer, pr);
#ifdef MT
wake_up_next_thread(peer);
#endif
}
static void handle_accepted(struct peerd *peer, struct peer_req *pr,
struct xseg_request *req)
{
struct xseg_request *xreq = pr->req;
//assert xreq == req;
XSEGLOG2(&lc, D, "Handle accepted");
xreq->serviced = 0;
//xreq->state = XS_ACCEPTED;
pr->retval = 0;
dispatch(peer, pr, req, dispatch_accept);
}
static void handle_received(struct peerd *peer, struct peer_req *pr,
struct xseg_request *req)
{
//struct xseg_request *req = pr->req;
//assert req->state != XS_ACCEPTED;
XSEGLOG2(&lc, D, "Handle received \n");
dispatch(peer, pr, req, dispatch_receive);
}
struct timeval sub_start, sub_end, sub_accum = { 0, 0 };
uint64_t submits = 0;
void get_submits_stats()
{
printf("Time waiting submit %lu.%06lu sec for %llu times.\n",
//(unsigned int)(t - peer->thread),
sub_accum.tv_sec, sub_accum.tv_usec,
(long long unsigned int) submits);
}
int submit_peer_req(struct peerd *peer, struct peer_req *pr)
{
uint32_t ret;
struct xseg_request *req = pr->req;
// assert req->portno == peer->portno ?
//TODO small function with error checking
XSEGLOG2(&lc, D, "submitting peer req %u\n",
(unsigned int) (pr - peer->peer_reqs));
ret = xseg_set_req_data(peer->xseg, req, (void *) (pr));
if (ret < 0) {
return -1;
}
//printf("pr: %x , req_data: %x \n", pr, xseg_get_req_data(peer->xseg, req));
//gettimeofday(&sub_start, NULL);
ret = xseg_submit(peer->xseg, req, pr->portno, X_ALLOC);
//gettimeofday(&sub_end, NULL);
//submits++;
//timersub(&sub_end, &sub_start, &sub_end);
//timeradd(&sub_end, &sub_accum, &sub_accum);
if (ret == NoPort) {
return -1;
}
xseg_signal(peer->xseg, ret);
return 0;
}
#ifdef MT
int check_ports(struct peerd *peer, struct thread *t)
#else
int check_ports(struct peerd *peer)
#endif
{
struct xseg *xseg = peer->xseg;
xport portno_start = peer->portno_start;
xport portno_end = peer->portno_end;
struct xseg_request *accepted, *received;
struct peer_req *pr;
xport i;
int r, c = 0;
for (i = portno_start; i <= portno_end; i++) {
accepted = NULL;
received = NULL;
if (!isTerminate()) {
#ifdef MT
pr = alloc_peer_req(peer, t);
#else
pr = alloc_peer_req(peer);
#endif
if (pr) {
accepted = xseg_accept(xseg, i, X_NONBLOCK);
if (accepted) {
pr->req = accepted;
pr->portno = i;
xseg_cancel_wait(xseg, i);
handle_accepted(peer, pr, accepted);
c = 1;
} else {
free_peer_req(peer, pr);
}
}
}
received = xseg_receive(xseg, i, X_NONBLOCK);
if (received) {
r = xseg_get_req_data(xseg, received, (void **) &pr);
if (r < 0 || !pr) {
XSEGLOG2(&lc, W, "Received request with no pr data\n");
xport p =
xseg_respond(peer->xseg, received, peer->portno_start,
X_ALLOC);
if (p == NoPort) {
XSEGLOG2(&lc, W, "Could not respond stale request");
xseg_put_request(xseg, received, portno_start);
continue;
} else {
xseg_signal(xseg, p);
}
} else {
//maybe perform sanity check for pr
xseg_cancel_wait(xseg, i);
handle_received(peer, pr, received);
c = 1;
}
}
}
return c;
}
#ifdef MT
static void *thread_loop(void *arg)
{
struct thread *t = (struct thread *) arg;
struct peerd *peer = t->peer;
struct xseg *xseg = peer->xseg;
xport portno_start = peer->portno_start;
xport portno_end = peer->portno_end;
pid_t pid = syscall(SYS_gettid);
uint64_t loops;
uint64_t threshold = 1000 / (1 + portno_end - portno_start);
XSEGLOG2(&lc, D, "thread %u\n", (unsigned int) (t - peer->thread));
XSEGLOG2(&lc, I, "Thread %u has tid %u.\n",
(unsigned int) (t - peer->thread), pid);
for (; !(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
for (loops = threshold; loops > 0; loops--) {
if (loops == 1) {
xseg_prepare_wait(xseg, peer->portno_start);
}
if (check_ports(peer, t)) {
loops = threshold;
}
}
XSEGLOG2(&lc, I, "Thread %u goes to sleep\n",
(unsigned int) (t - peer->thread));
xseg_wait_signal(xseg, peer->sd, 10000000UL);
xseg_cancel_wait(xseg, peer->portno_start);
XSEGLOG2(&lc, I, "Thread %u woke up\n",
(unsigned int) (t - peer->thread));
}
wake_up_next_thread(peer);
custom_peer_finalize(peer);
return NULL;
}
void *init_thread_loop(void *arg)
{
struct thread *t = (struct thread *) arg;
struct peerd *peer = t->peer;
pthread_t thread = pthread_self();
long int thread_num = t - t->peer->thread;
char *thread_id;
char *tmp;
int i, thread_id_size;
cpu_set_t mask;
int r;
if (cpu_list.len) {
CPU_ZERO(&mask);
CPU_SET(cpu_list.cpus[thread_num], &mask);
r = pthread_setaffinity_np(thread, sizeof(mask), &mask);
if (r < 0) {
perror("sched_setaffinity");
return NULL;
}
}
/*
* We need an identifier for every thread that will spin in peerd_loop.
*/
thread_id_size = asprintf(&tmp, "Thread %ld", thread_num);
thread_id = malloc((thread_id_size + 1) * sizeof(char));
memcpy(thread_id, tmp, thread_id_size + 1);
free(tmp);
t->arg = (void *) thread_id;
pthread_setspecific(threadkey, t);
//Start thread loop
(void) peer->peerd_loop(t);
wake_up_next_thread(peer);
custom_peer_finalize(peer);
return NULL;
}
int peerd_start_threads(struct peerd *peer)
{
int i;
uint32_t nr_threads = peer->nr_threads;
//TODO err check
for (i = 0; i < nr_threads; i++) {
peer->thread[i].thread_no = i;
peer->thread[i].peer = peer;
pthread_create(&peer->thread[i].tid, NULL,
init_thread_loop, (void *) (peer->thread + i));
}
if (peer->interactive_func) {
peer->interactive_func();
}
for (i = 0; i < nr_threads; i++) {
pthread_join(peer->thread[i].tid, NULL);
}
xseg_quit_local_signal(peer->xseg, peer->portno_start);
return 0;
}
#endif
int defer_request(struct peerd *peer, struct peer_req *pr)
{
int r;
xport p;
if (!canDefer(peer)) {
XSEGLOG2(&lc, E, "Peer cannot defer requests");
return -1;
}
p = xseg_forward(peer->xseg, pr->req, peer->defer_portno, pr->portno,
X_ALLOC);
if (p == NoPort) {
XSEGLOG2(&lc, E, "Cannot defer request %lx", pr->req);
return -1;
}
r = xseg_signal(peer->xseg, p);
if (r < 0) {
XSEGLOG2(&lc, W, "Cannot signal port %lu", p);
}
free_peer_req(peer, pr);
return 0;
}
/*
* generic_peerd_loop is a general-purpose port-checker loop that is
* suitable both for multi-threaded and single-threaded peers.
*/
static int generic_peerd_loop(void *arg)
{
#ifdef MT
struct thread *t = (struct thread *) arg;
struct peerd *peer = t->peer;
char *id = t->arg;
#else
struct peerd *peer = (struct peerd *) arg;
char id[4] = { 'P', 'e', 'e', 'r' };
#endif
struct xseg *xseg = peer->xseg;
xport portno_start = peer->portno_start;
xport portno_end = peer->portno_end;
pid_t pid = syscall(SYS_gettid);
uint64_t threshold = peer->threshold;
threshold /= (1 + portno_end - portno_start);
threshold += 1;
uint64_t loops;
uint64_t test;
XSEGLOG2(&lc, I, "%s has tid %u.\n", id, pid);
//for (;!(isTerminate() && xq_count(&peer->free_reqs) == peer->nr_ops);) {
for (; !(isTerminate() && all_peer_reqs_free(peer));) {
//Heart of peerd_loop. This loop is common for everyone.
for (loops = threshold; loops > 0; loops--) {
if (loops == 1)
xseg_prepare_wait(xseg, peer->portno_start);
#ifdef MT
test = check_ports(peer, t);
#else
test = check_ports(peer);
#endif
if (test)
loops = threshold;
}
#ifdef ST_THREADS
if (ta) {
st_sleep(0);
continue;
}
#endif
XSEGLOG2(&lc, I, "%s goes to sleep\n", id);
xseg_wait_signal(xseg, peer->sd, 10000000UL);
xseg_cancel_wait(xseg, peer->portno_start);
XSEGLOG2(&lc, I, "%s woke up\n", id);
}
return 0;
}
static int init_peerd_loop(struct peerd *peer)
{
struct xseg *xseg = peer->xseg;
cpu_set_t mask;
int r;
if (cpu_list.len) {
CPU_ZERO(&mask);
CPU_SET(cpu_list.cpus[0], &mask);
r = sched_setaffinity(0, sizeof(mask), &mask);
if (r < 0) {
perror("sched_setaffinity");
return -1;
}
}
peer->peerd_loop(peer);
custom_peer_finalize(peer);
xseg_quit_local_signal(xseg, peer->portno_start);
return 0;
}
#ifdef ST_THREADS
static void *st_peerd_loop(void *peer)
{
struct peerd *peerd = peer;
return init_peerd_loop(peerd) ? (void *) -1 : (void *) 0;
}
#endif
static struct xseg *join(char *spec)
{
struct xseg_config config;
struct xseg *xseg;
(void) xseg_parse_spec(spec, &config);
xseg = xseg_join(config.type, config.name, PEER_TYPE, NULL);
if (xseg) {
return xseg;
}
(void) xseg_create(&config);
return xseg_join(config.type, config.name, PEER_TYPE, NULL);
}
static struct peerd *peerd_init(uint32_t nr_ops, char *spec, long portno_start,
long portno_end, uint32_t nr_threads,
xport defer_portno, uint64_t threshold)
{
int i, r;
struct peerd *peer;
struct xseg_port *port;
void *sd = NULL;
xport p;
#ifdef ST_THREADS
st_init();
#endif
peer = malloc(sizeof(struct peerd));
if (!peer) {
perror("malloc");
return NULL;
}
peer->nr_ops = nr_ops;
peer->defer_portno = defer_portno;
peer->threshold = threshold;
#ifdef MT
peer->nr_threads = nr_threads;
peer->thread = calloc(nr_threads, sizeof(struct thread));
if (!peer->thread) {
goto malloc_fail;
}
if (!xq_alloc_empty(&peer->threads, nr_threads)) {
goto malloc_fail;
}
for (i = 0; i < nr_threads; i++) {
if (!xq_alloc_empty(&peer->thread[i].free_thread_reqs, nr_ops)) {
goto malloc_fail;
}
}
for (i = 0; i < nr_ops; i++) {
__xq_append_head(&peer->thread[i % nr_threads].free_thread_reqs,
(xqindex) i);
}
pthread_key_create(&threadkey, NULL);
#else
if (!xq_alloc_seq(&peer->free_reqs, nr_ops, nr_ops)) {
goto malloc_fail;
}
if (peer->free_reqs.size < peer->nr_ops) {
peer->nr_ops = peer->free_reqs.size;
}
#endif
peer->peer_reqs = calloc(nr_ops, sizeof(struct peer_req));
if (!peer->peer_reqs) {
malloc_fail:
perror("malloc");
return NULL;
}
if (xseg_initialize()) {
printf("cannot initialize library\n");
return NULL;
}
peer->xseg = join(spec);
if (!peer->xseg) {
return NULL;
}
peer->portno_start = (xport) portno_start;
peer->portno_end = (xport) portno_end;
/*
* Start binding ports from portno_start to portno_end.
* The first port we bind will have its signal_desc initialized by xseg
* and the same signal_desc will be used for all the other ports.
*/
peer->sd = NULL;
for (p = peer->portno_start; p <= peer->portno_end; p++) {
port = xseg_bind_port(peer->xseg, p, peer->sd);
if (!port) {
printf("cannot bind to port %u\n", (unsigned int) p);
return NULL;
}
if (p == peer->portno_start) {
peer->sd = xseg_get_signal_desc(peer->xseg, port);
}
}
printf("Peer on ports %u-%u\n", peer->portno_start, peer->portno_end);
r = xseg_init_local_signal(peer->xseg, peer->portno_start);
if (r < 0) {
XSEGLOG2(&lc, E, "Could not initialize local signals");
return NULL;
}
for (i = 0; i < nr_ops; i++) {
peer->peer_reqs[i].peer = peer;
peer->peer_reqs[i].req = NULL;
peer->peer_reqs[i].retval = 0;
peer->peer_reqs[i].priv = NULL;
peer->peer_reqs[i].portno = NoPort;
#ifdef ST_THREADS
peer->peer_reqs[i].cond = st_cond_new(); //FIXME err check
#endif
}
//Plug default peerd_loop. This can change later on by custom_peer_init.
peer->peerd_loop = generic_peerd_loop;
#ifdef MT
peer->interactive_func = NULL;
#endif
return peer;
}
int pidfile_remove(char *path, int fd)
{
close(fd);
return (unlink(path));
}
int pidfile_write(int pid_fd)
{
char buf[16];
snprintf(buf, sizeof(buf), "%ld", syscall(SYS_gettid));
buf[15] = '\0';
lseek(pid_fd, 0, SEEK_SET);
int ret = write(pid_fd, buf, strlen(buf));
return ret;
}
int pidfile_read(char *path, pid_t * pid)
{
char buf[16], *endptr;
*pid = 0;
int fd = open(path, O_RDONLY);
if (fd < 0) {
return -1;
}
int ret = read(fd, buf, 15);
buf[15] = '\0';
close(fd);
if (ret < 0) {
return -1;
} else {
*pid = strtol(buf, &endptr, 10);
if (endptr != &buf[ret]) {
*pid = 0;
return -1;
}
}
return 0;
}
int pidfile_open(char *path, pid_t * old_pid)
{
//nfs version > 3
int fd = open(path, O_CREAT | O_EXCL | O_WRONLY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
if (fd < 0) {
if (errno == EEXIST) {
pidfile_read(path, old_pid);
}
}
return fd;
}
int get_cpu_list(char *cpus, struct cpu_list *cpu_list)
{
char *tok, *rem;
int i = 0;
while ((tok = strtok(cpus, ",")) != NULL) {
cpu_list->cpus[i++] = strtol(tok, &rem, 10);
if (strlen(rem) > 0) { /* Not a number */
return -1;
}
cpus = NULL;
}
cpu_list->len = i;
return 0;
}
void usage(char *argv0)
{
fprintf(stderr, "Usage: %s [general options] [custom peer options]\n\n",
argv0);
fprintf(stderr,
"General peer options:\n" " Option | Default | \n"
" --------------------------------------------\n"
" -g | None | Segment spec to join\n"
" -sp | NoPort | Start portno to bind\n"
" -ep | NoPort | End portno to bind\n"
" -p | NoPort | Portno to bind\n"
" -n | 16 | Number of ops\n"
" -v | 0 | Verbosity level\n"
" -l | None | Logfile \n"
" -d | No | Daemonize \n"
" --pidfile | None | Pidfile \n"
" -uid | None | Set real EUID \n"
" -gid | None | Set real EGID \n"
#ifdef MT
" -t | No | Number of threads \n"
#endif
" --cpus | No | Coma-separated list of CPUs\n"
" | | to pin the process or threads\n" "\n");
custom_peer_usage();
}
int main(int argc, char *argv[])
{
struct peerd *peer = NULL;
//parse args
int r;
long portno_start = -1, portno_end = -1, portno = -1;
//set defaults here
int daemonize = 0, help = 0;
uint32_t nr_ops = 16;
uint32_t nr_threads = 1;
uint64_t threshold = 1000;
unsigned int debug_level = 0;
xport defer_portno = NoPort;
pid_t old_pid = 0;
int pid_fd = -1;
uid_t cur_uid, uid = -1;
gid_t cur_gid, gid = -1;
mode_t peer_umask = PEER_DEFAULT_UMASK;
char spec[MAX_SPEC_LEN + 1];
char logfile[MAX_LOGFILE_LEN + 1];
char pidfile[MAX_PIDFILE_LEN + 1];
char cpus[MAX_CPUS_LEN + 1];
char *username = NULL;
logfile[0] = '\0';
pidfile[0] = '\0';
spec[0] = '\0';
cpus[0] = '\0';
//capture here -g spec, -n nr_ops, -p portno, -t nr_threads -v verbose level
// -dp xseg_portno to defer blocking requests
// -l log file ?
//TODO print messages on arg parsing error
BEGIN_READ_ARGS(argc, argv);
READ_ARG_STRING("-g", spec, MAX_SPEC_LEN);
READ_ARG_ULONG("-sp", portno_start);
READ_ARG_ULONG("-ep", portno_end);
READ_ARG_ULONG("-p", portno);
READ_ARG_ULONG("-n", nr_ops);
READ_ARG_ULONG("-v", debug_level);
READ_ARG_ULONG("-uid", uid);
READ_ARG_ULONG("-gid", gid);
#ifdef MT
READ_ARG_ULONG("-t", nr_threads);
#endif
READ_ARG_ULONG("-dp", defer_portno);
READ_ARG_STRING("-l", logfile, MAX_LOGFILE_LEN);
READ_ARG_BOOL("-d", daemonize);
READ_ARG_BOOL("-h", help);
READ_ARG_BOOL("--help", help);
READ_ARG_ULONG("--threshold", threshold);
READ_ARG_STRING("--cpus", cpus, MAX_CPUS_LEN);
READ_ARG_STRING("--pidfile", pidfile, MAX_PIDFILE_LEN);
READ_ARG_ULONG("--umask", peer_umask);
END_READ_ARGS();
if (help) {
usage(argv[0]);
return 0;
}
if (gid != -1) {
struct group *gr;
gr = getgrgid(gid);
if (!gr) {
XSEGLOG2(&lc, E, "Group %d not found", gid);
return -1;
}
}
if (uid != -1) {
struct passwd *pw;
pw = getpwuid(uid);
if (!pw) {
XSEGLOG2(&lc, E, "User %d not found", uid);
return -1;
}
username = pw->pw_name;
if (gid == -1) {