-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock_sync.cpp
1323 lines (980 loc) · 35.7 KB
/
clock_sync.cpp
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
#include"clock_sync.h"
//////////////
// DataList //
//////////////
std::string my_output_dir = std::string("./data/");
void DataList::init(int id){
// printf("Gonna malloc %lu\n", DL_len*sizeof(ts_t));
node_id = id;
printf("The node id is %d\n", node_id);
this->timestamp = (ts_t*)malloc(DL_len*sizeof(ts_t));
this->upper_dot = (ts_t*)malloc(DL_len*sizeof(ts_t));
this->lower_dot = (ts_t*)malloc(DL_len*sizeof(ts_t));
this->state = (char*)malloc(DL_len*sizeof(char));
assert(timestamp&&upper_dot&&lower_dot&&state);
for(int i=0; i<DL_len; i++){
state[i] = PROBE_INV;
upper_dot[i] = 0;
lower_dot[i] = 0;
}
start_ts = 0;
idx_start = 0;
idx_end = 0;
fitting_flag = 0;
// open the files to output data
#ifdef OUTPUT_MODE
char path_buffer[128];
sprintf(path_buffer, "%supper_%d.txt", my_output_dir.c_str(), node_id);
ofs_upper.open(path_buffer, std::ios::out);
sprintf(path_buffer, "%slower_%d.txt", my_output_dir.c_str(), node_id);
ofs_lower.open(path_buffer, std::ios::out);
sprintf(path_buffer, "%sinfo_%d.txt", my_output_dir.c_str(), node_id);
ofs_info.open(path_buffer, std::ios::out);
sprintf(path_buffer, "%sdot_%d.txt", my_output_dir.c_str(), node_id);
cns_dot.open(path_buffer, std::ios::out);
sprintf(path_buffer, "%scns_info_%d.txt", my_output_dir.c_str(), node_id);
cns_info.open(path_buffer, std::ios::out);
if(!ofs_upper || !ofs_lower || !ofs_info || !cns_dot){
fprintf(stderr, "Open file failed\n");
assert(false);
}
#endif
};
void DataList::finalize(){
// close the files
#ifdef OUTPUT_MODE
ofs_upper.close();
ofs_lower.close();
ofs_info.close();
cns_dot.close();
cns_info.close();
#endif
free(timestamp);
free(upper_dot);
free(lower_dot);
free(state);
}
void DataList::InsertLocalProbe(int request_id, ts_t ts, ts_t upper, ts_t lower){
// we assume local probe inserts continuously with regard to request ID
int idx = request_id%DL_len;
if(state[idx]!=PROBE_INV){
printf("Warning: overwriting valid data %d.\n", idx);
state[idx] = PROBE_INV;
assert(false);
}
timestamp[idx] = ts;
upper_dot[idx] = upper;
lower_dot[idx] = lower;
__asm__ __volatile__("" ::: "memory");
state[idx] |= PROBE_LR;
}
// we assume identical request ID in a short run(long enough to fill an int variable), so this step doesn't consider concurrency issue regarding request ID.
void DataList::InsertRemoteUpper(int request_id, ts_t upper){
int idx = request_id%DL_len;
assert(state[idx]&PROBE_LR);
// printf("Insert remote upper %d\n", idx);
// assert(!(state[idx]&PROBE_UPR));
if(state[idx]&PROBE_UPR){
printf("PROBE UPPER failed in %d\n", idx);
}
upper_dot[idx] = upper - upper_dot[idx];
// insert the dot into file
#ifdef OUTPUT_MODE
ofs_upper<<timestamp[idx]<<" "<<upper_dot[idx]<<std::endl;
#endif
__asm__ __volatile__("" ::: "memory");
state[idx] |= PROBE_UPR;
if(state[idx]==PROBE_OK)
tick(idx);
}
void DataList::InsertRemoteLower(int request_id, ts_t lower){
int idx = request_id%DL_len;
assert(state[idx]&PROBE_LR);
// printf("Insert remote lower %d\n", idx);
// assert(!(state[idx]&PROBE_LWR));
if(state[idx]&PROBE_LWR){
printf("PROBE LOWER failed in %d\n", idx);
}
lower_dot[idx] = lower - lower_dot[idx];
// insert the dot into file
#ifdef OUTPUT_MODE
ofs_lower<<timestamp[idx]<<" "<<lower_dot[idx]<<std::endl;
#endif
__asm__ __volatile__("" ::: "memory");
state[idx] |= PROBE_LWR;
if(state[idx]==PROBE_OK)
tick(idx);
}
void* SVM_fitting(void* argv){
// cpu_set_t mask;
// CPU_ZERO(&mask);
// CPU_SET(20, &mask);
// sched_setaffinity(0, sizeof(mask), &mask);
DataList* dl = (DataList*)argv;
GlobalTimer* gtimer = (GlobalTimer*)dl->gtimer;
int start = dl->idx_start;
int end = dl->idx_end;
if(end<start)
end += DL_len;
dl->svm_id++;
// printf("start SVM fitting with start is %d, end is %d\n", start, end);
// start SVM fitting
svm my_svm(dl->timestamp[start], dl->lower_dot[start]);
#ifdef OUTPUT_MODE
ts_t last_ts = dl->timestamp[start];
bool valid;
#endif
for(int i=start; i<=end; i++){ // insert the dot
int idx = i%DL_len;
#ifdef OUTPUT_MODE
valid=false;
#endif
if(dl->state[idx]&PROBE_UPR){
my_svm.insert_dot(dl->timestamp[idx], dl->upper_dot[idx], 0);
#ifdef OUTPUT_MODE
valid = true;
#endif
}
else
printf("Upper probe %d not ready\n", idx);
if(dl->state[idx]&PROBE_LWR){
my_svm.insert_dot(dl->timestamp[idx], dl->lower_dot[idx], 1);
#ifdef OUTPUT_MODE
valid = true;
#endif
}
else
printf("Lower probe %d not ready\n", idx);
// drop the data
#ifdef OUTPUT_MODE
if(valid)
last_ts = dl->timestamp[idx];
#endif
__asm__ __volatile__("":::"memory");
dl->state[idx] = PROBE_INV;
}
// remark the flag
__asm__ __volatile__("":::"memory");
dl->idx_start = (dl->idx_end+1)%DL_len;
dl->fitting_flag = 0;
// get SVM results
double k, b, distance; // k without unit, b in ns
// ts_t start_time = get_real_clock();
my_svm.get_result(&k, &b, &distance);
// ts_t end_time = get_real_clock();
b+=MODE_COMPENSATION; // this is the compensation for RC/UD difference
// printf("SVM calculation %lld ms\n", (end_time-start_time)/1000000);
ts_t base_x =dl->timestamp[start];
ts_t x_mid = base_x + (fit_interval/2)*BILLION;
ts_t y_mid = k*(fit_interval/2)*BILLION + b;
// output
#ifdef OUTPUT_MODE
dl->ofs_info<<std::scientific<<k<<" "<<std::fixed<<b<<" "<<distance<<" "<<my_svm.get_init_x()<<" "<<my_svm.get_init_y()<<" "<<last_ts<<std::endl;
#endif
// set Master node's new dot
// printf("Goona insert dot %lld, %lld\n", x_mid, y_mid);
gtimer->clockmanager->InsertResDot(x_mid, y_mid);
return NULL;
}
void DataList::tick(int idx){
if(!fitting_flag){ // make sure no fitting is in process
// if the boundary arrives
if(timestamp[idx]<timestamp[idx_start]+fit_interval*(int)1e9)
return;
// modify the flag. Note that we insert dot in a serial method, so there is no need for concurrency control
fitting_flag = 1;
idx_end = idx;
// printf("Gonna do SVM when idx is %d\n", idx_end);
pthread_create(&fitting_thread, NULL, SVM_fitting, (void*)this);
}
}
//////////////////
// ClockManager //
//////////////////
void ClockManager::init(GlobalTimer* timer_pointer, int type){
for(int i=0; i<RECORD_MAX; i++){
edge_valid[i] = false;
mid_valid[i] = false;
}
this->gtimer = timer_pointer;
// get the device list for clock info
struct ibv_device** dev_list = ibv_get_device_list(NULL);
struct ibv_device* ib_dev = *dev_list;
ctx = ibv_open_device(ib_dev);
memset(&vex, 0, sizeof(ibv_values_ex));
mlx5dv_get_clock_info(ctx, &clock_info);
vex.comp_mask = IBV_VALUES_MASK_RAW_CLOCK;
#ifdef OUTPUT_MODE
char path_buffer[128];
if(type==0)
sprintf(path_buffer, "%scns_edge_%d.txt", my_output_dir.c_str(), gtimer->node_id);
else if(type==1)
sprintf(path_buffer, "%snns_edge_%d.txt", my_output_dir.c_str(), gtimer->node_id);
else
assert(false);
ofs_edge.open(path_buffer, std::ios::out);
#endif
// get start clock
this->type = type;
if(type==0){
_interval = CNS_interval*1000000000;
start_x = get_cpu_time();
start_y = get_nic_time();
}
else if(type==1){
start_x = get_nic_time();
start_y = 0; // typically, delta won't be so large, so we set it 0
_interval = fit_interval*1000000000;
}
else
assert(false);
slot_idx = 0;
edge_idx = 0;
delete_idx = RECORD_MAX/2;
// printf("[Debug] type %d, start_x is %lld, start_y is %lld\n", type, start_x, start_y);
}
void ClockManager::finalize(){
#ifdef OUTPUT_MODE
ofs_edge.close();
#endif
}
void ClockManager::InsertResDot(ts_t x, ts_t y){
#ifdef OUTPUT_MODE
ofs_edge<<x<<" "<<y<<" ";
#endif
// get the index
double x_ = (double)(x-start_x);
assert(x_>0);
double y_ = (double)(y-start_y);
// if(type==1)
// printf("[Debug] %f = %lld - %lld\n", y_, y, start_y);
int idx = slot_idx;
slot_idx = (slot_idx+1)%RECORD_MAX;
// printf("Type %d insert %lld in %d\n", type, x, idx);
// assert(!mid_valid[idx]);
if(mid_valid[idx]){
printf("[Type %d] Error in slot %d, previous ts is %lld, now ts is %lld\n", type, idx%RECORD_MAX, (ts_t)mid_x[idx]+start_x, x);
// assert(false);
}
// we try at most 5 history count in case that the previous result is not ready yet due to spike SVM calculation time
int idx1;
bool valid=false;
for(int i=0; i<5; i++){
idx1 = (idx - i + RECORD_MAX)%RECORD_MAX;
if(mid_valid[idx1]){
valid=true;
break;
}
}
if(!valid&>imer->is_warmup_done()){
printf("Previous TS %d is not ready yet\n", idx);
assert(false);
}
mid_x[idx] = x_;
mid_y[idx] = y_;
mid_valid[idx] = true;
mid_valid[delete_idx] = false;
// printf("[Type %d] Insert dot in slot %d\n", type, slot_idx%RECORD_MAX);
double xp = mid_x[idx1];
double yp = mid_y[idx1];
#ifdef OUTPUT_MODE
ofs_edge<<(ts_t)x_+start_x<<" "<<(ts_t)y_+start_y<<" ";
#endif
// put a new dot into the NIC array. Different method may behave differently
#if fitting_method==FM_SVM
double delta_x = (SVM_delay+0.5)*_interval;
assert(delta_x>0);
double target_x = delta_x + x_;
double target_y = (y_-yp)*delta_x/(x_-xp)+y_;
// insert the dot
idx = edge_idx%RECORD_MAX;
edge_x[idx] = target_x;
edge_y[idx] = target_y;
#ifdef OUTPUT_MODE
ofs_edge<<(ts_t)target_x+start_x<<" "<<(ts_t)target_y+start_y<<std::endl;
#endif
__asm__ __volatile__("" ::: "memory");
edge_valid[idx] = true;
edge_valid[delete_idx] = false; // periodically GC
#elif fitting_method==FM_LSVM
assert(false);
#else
assert(false);
#endif
edge_idx++;
// printf("[Type %d] Delete dot in slot %d\n", type, delete_idx);
delete_idx = (delete_idx+1)%RECORD_MAX;
}
ts_t ClockManager::GetModifiedValue(ts_t timestamp){
if(type==1 && !gtimer->rcc_master)
return 0;
if(!gtimer->is_warmup_done())
return 0;
// note that "timestamp" should already be the NIC ts
double delta = (double)(timestamp-start_x);
assert(delta>0);
int index = edge_idx%RECORD_MAX;
int idx1, idx2;
int valid_cnt = 0;
for(int i=0; i<5; i++){
if(valid_cnt==1){
idx2 = (index - i + RECORD_MAX)%RECORD_MAX;
if(edge_valid[idx2]){
valid_cnt++;
break;
}
}
else{
idx1 = (index - i + RECORD_MAX)%RECORD_MAX;
if(edge_valid[idx1])
valid_cnt++;
}
}
if(valid_cnt!=2){
printf("In TS: target idx %d valid count %d less than 2 after 5 try!\n", index, valid_cnt);
return 0;
}
double x1 = edge_x[idx1%RECORD_MAX];
double x2 = edge_x[idx2%RECORD_MAX];
double y1 = edge_y[idx1%RECORD_MAX];
double y2 = edge_y[idx2%RECORD_MAX];
double target_y = (delta-x2)*(y2-y1)/(x2-x1) + y2;
ts_t result = (ts_t)target_y + start_y;
return result;
}
ts_t ClockManager::GetRealTime(ts_t timestamp){
return timestamp+GetModifiedValue(timestamp);
}
ts_t ClockManager::get_nic_time(){
assert(!ibv_query_rt_values_ex(ctx, &vex));
return mlx5dv_ts_to_ns(&clock_info, vex.raw_clock.tv_nsec);
}
ts_t ClockManager::get_cpu_time(){
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_nsec + 1000000000*ts.tv_sec;
}
/////////////////
// GlobalTimer //
/////////////////
typedef struct{
tsocket_context* ctx;
char ip_addr[128];
int port;
}master_info;
typedef struct{
tsocket_context_ud* ctx;
char ip_addr[128];
int port;
}master_info_ud;
void* master_run_rc(void* argv){
// this function enable asyn connection of master function
master_info* info = (master_info*)argv;
assert(!tsocket_connect(info->ctx, info->ip_addr, info->port));
return NULL;
}
void* master_run_ud(void* argv){
// this function enable asyn connection of master function
master_info_ud* info = (master_info_ud*)argv;
assert(tsocket_connect_ud(info->ctx, info->ip_addr, info->port)!=-1);
return NULL;
}
void GlobalTimer::init(int local_port, int id){
port = local_port;
SlaveCount = 0;
_is_done = false;
node_id = id;
CNS_warmup_done = false;
local_warmup_done = false;
global_warmup_done = false;
datalist = new DataList;
datalist->init(node_id);
datalist->gtimer = this;
clockmanager = new ClockManager;
clockmanager->init(this, 1);
clockmanager_cpu = new ClockManager;
clockmanager_cpu->init(this, 0);
// step 1: create RDMA socket
rcc_center = create_rdma(); // create socket with center
udc = create_tsocket_ud();
rcc_list = NULL;
rcc_master = NULL;
// step 2: build RDMA connection with center
printf("The port ID is %d\n", local_port);
assert(!rdma_bind(rcc_center, (int)(local_port+ts_port_delta)));
// step 3: get new-work structure from center node
// post recv
cent2master msg;
int rc = 1;
while(rc)
rc = rdma_recv(rcc_center, (char*)(&msg), sizeof(cent2master), NULL);
printf("Got network topologies from the Center.\n");
// build connection with the other node. We fix that master use "connect" while slave use "bind" function
// step 4: Master nodes asynchronously connect to the Slave nodes
master_info rc_info;
master_info_ud ud_info;
if(msg.master_port!=-1){
rcc_master = create_tsocket();
strcpy(father_ip, msg.master_ip);
strcpy(rc_info.ip_addr, msg.master_ip);
strcpy(ud_info.ip_addr, msg.master_ip);
rc_info.port = msg.master_port;
ud_info.port = msg.master_port+UD_port_delta;
rc_info.ctx = rcc_master;
ud_info.ctx = udc;
printf("Gonna connect to %s,%d\n", rc_info.ip_addr, rc_info.port);
fflush(stdout);
pthread_create(&master_rc, NULL, master_run_rc, &rc_info);
pthread_create(&master_ud, NULL, master_run_ud, &ud_info);
}
// step 5: Slave nodes synchronously bind the ports
if(msg.slave_port_num){
rcc_list = (tsocket_context**) malloc(sizeof(tsocket_context*)*msg.slave_port_num);
SlaveCount = msg.slave_port_num;
}
int col=0;
while(msg.slave_port[col]!=-1){
printf("Gonna bind on %d\n", msg.slave_port[col]);
// build RC connection
rcc_list[col] = create_tsocket();
assert(!tsocket_bind(rcc_list[col], msg.slave_port[col]));
// build UD connection
int ud_qp_idx = tsocket_bind_ud(udc, msg.slave_port[col]+UD_port_delta);
assert((ud_qp_idx-1)==col); // here should also minus one
col++;
}
assert(col==SlaveCount);
assert(ud_batch>=(lcu_batch*SlaveCount));
fprintf(stdout, "Sync connection built with other nodes.\n");
// wait for the master connection finish
if(msg.master_port!=-1){
pthread_join(master_rc, NULL);
pthread_join(master_ud, NULL);
}
}
typedef struct{
DataList* datalist;
char* buffer;
}extract_s;
void* extract_data(void* argv){
// we don't want extraction to block probing
extract_s* es = (extract_s*)argv;
DataList* datalist = es->datalist;
batch_data* bd = (batch_data*)es->buffer;
for(int i=0; i<bd->data_num; i++){
int id = bd->request_id[i];
ts_t timestamp = bd->timestamp[i];
bool is_rc = bd->is_rc_data[i];
if(is_rc){
datalist->InsertRemoteLower(id, timestamp);
}
else{
datalist->InsertRemoteUpper(id, timestamp);
}
}
return NULL;
}
void* master_run(void* argv){
GlobalTimer* gtimer = (GlobalTimer*) argv;
// master side starts the probing, collects ts
ts_t ts_rc, probe_start, probe_end;
// ts_t epoch_ts, now_ts;
int request_id = 0; // this id use locally, so we don't do concurrency control
int max_request_id = 2147483640; // be a little smaller
char buffer[lcu_buffer_size];
ts_t ts_array[ud_batch];
int qp_idx_array[ud_batch];
DataList* datalist = gtimer->datalist;
extract_s extract_struct;
extract_struct.buffer = buffer;
extract_struct.datalist = datalist;
// post recv for large request
tsocket_post_recv(gtimer->rcc_master, 1, 8);
int epoch_count = 0;
int time_count = 0;
printf("Master thread runs.\n");
// int upper_idx = 0, lower_idx=0;
while(!gtimer->is_done()){
time_count++;
if(time_count==dot_num){
epoch_count++;
time_count = 0;
if(epoch_count>warmup_epoch)
gtimer->local_warmup_done = true;
}
probe_start = get_real_clock();
// RC send & poll
tsocket_send(gtimer->rcc_master, request_id, buffer, 0, &ts_rc);
// UD send & poll
tsocket_send_ud(gtimer->udc, 0, request_id, buffer, 0);
int poll_num = tsocket_poll_send(gtimer->udc, ts_array, qp_idx_array);
assert(poll_num==1&&qp_idx_array[0]==0);
// insert the starting probe data
datalist->InsertLocalProbe(request_id, ts_rc, ts_array[0], ts_rc); // we let the UD ts to be the upper
// poll the recv oppotunistically. small batch should use sync way, while large batch should use async way
if(!tsocket_recv(gtimer->rcc_master, buffer, lcu_buffer_size)) // large chunk arrives
// async way
{
pthread_create(>imer->extract_thread, NULL, extract_data, &extract_struct);
}
// wait for the duration passes
probe_end = get_real_clock();
int delta = interval - (int)(probe_end-probe_start)/1000; // in us, denoting the remaining time
if(delta<0){
// fprintf(stdout, "Warning: sampling Frequency too large, exceeding %d us.\n", -delta);
;
}
else{
// assert(delta>=0&&delta<=1000);
if((delta<0 || delta>interval)){
printf("Error delta: The delta is %d\n", delta);
// assert(false);
delta = 0;
}
usleep(delta);
}
request_id = (request_id+1)%max_request_id;
}
// send a final msg
// we use -2 to denote end of the probing
tsocket_send(gtimer->rcc_master, -2, buffer, 0, NULL);
printf("Master thread quits\n");
return NULL;
}
void* slave_run(void* argv){
printf("Slave thread runs.\n");
GlobalTimer* gtimer = (GlobalTimer*) argv;
// the buffer array to do batch send
batch_data buffer_array[MAX_PAIR];
for(int i=0; i<MAX_PAIR; i++)
buffer_array[i].data_num = 0;
// receive related
ts_t ts_array[ud_batch];
int qp_idx_array[ud_batch];
int request_id_array[ud_batch];
int slave_count = gtimer->SlaveCount;
// post recveiver for RC
for(int i=0; i<slave_count; i++)
tsocket_post_recv(gtimer->rcc_list[i], 0, ud_batch);
// we don't need to allocate receiver for UD here, as it's already done in its creation function
tsocket_context** slave_list = gtimer->rcc_list;
tsocket_context_ud* udc = gtimer->udc;
assert(slave_count==udc->opp_qpn-1);
int finish_count=0; // mark for finish
bool is_end[MAX_PAIR];
for(int i=0; i<MAX_PAIR; i++)
is_end[i] = false;
while(finish_count<slave_count){
ts_t start = get_real_clock();
// batch recv RC request
for(int i=0; i<slave_count; i++){
if(is_end[i])
continue;
// batch poll request from a single node
int request_count = tsocket_poll_recv_rc(slave_list[i], ts_array, request_id_array);
// put the message into the array
for(int j=0; j<request_count; j++){
// first see if remote send end message
int request_id = request_id_array[j];
if(request_id==-2){
assert(!is_end[i]);
is_end[i] = true;
finish_count++;
break;
}
buffer_array[i].request_id[buffer_array[i].data_num] = request_id;
// modify the ts accroding father's node
buffer_array[i].timestamp[buffer_array[i].data_num] = gtimer->get_modify_ts(ts_array[j]);
// buffer_array[i].timestamp[buffer_array[i].data_num] = ts_array[j];
buffer_array[i].is_rc_data[buffer_array[i].data_num] = true;
buffer_array[i].data_num++;
// if(i==0)
// printf("Insert lower %d\n", request_id);
if(buffer_array[i].data_num==lcu_batch){
// a batch full, send the data to remote
tsocket_send(slave_list[i], -1, (char*)&buffer_array[i], sizeof(batch_data), NULL);
buffer_array[i].data_num = 0;
}
}
if(!is_end[i])
tsocket_post_recv(slave_list[i], 0, request_count);
}
// batch recv UD request
int request_count = tsocket_poll_recv(udc, ts_array, qp_idx_array, request_id_array, slave_count);
// put the message into the array
for(int j=0; j<request_count; j++){
// first see if remote send end message
int request_id = request_id_array[j];
int i = qp_idx_array[j]-1; // mind that this mapping exists
if(is_end[i]) // this node has already close
continue;
buffer_array[i].request_id[buffer_array[i].data_num] = request_id;
// modify the ts accroding father's node
buffer_array[i].timestamp[buffer_array[i].data_num] = gtimer->get_modify_ts(ts_array[j]);
// buffer_array[i].timestamp[buffer_array[i].data_num] = ts_array[j];
buffer_array[i].is_rc_data[buffer_array[i].data_num] = false;
buffer_array[i].data_num++;
// if(i==0)
// printf("Insert upper %d\n", request_id);
if(buffer_array[i].data_num==lcu_batch){
// a batch full, send the data to remote
tsocket_send(slave_list[i], -1, (char*)&buffer_array[i], sizeof(batch_data), NULL);
buffer_array[i].data_num = 0;
}
}
tsocket_recv_ud(udc, request_count);
ts_t end = get_real_clock();
ts_t res = slave_batch_time - (end-start)/1000;
if(res>0)
usleep(res);
}
printf("Slave thread quits\n");
gtimer->set_done(); // when slaves_run quits, the sync is done. This will make the master_sync done, which will recursively stop the sync tree from leaf to root.
return NULL;
}
bool cns_comp(cns_map_t a, cns_map_t b){
return a.delta < b.delta;
}
void* cpu_nic_cs_thread(void* argv){
// this thread periodically synchronizes cpu time and nic ts counter
GlobalTimer* gtimer = (GlobalTimer*) argv;
ClockManager* manager = gtimer->clockmanager_cpu;
// step 1: do get nic test to get an upper bound of filter
/* use fixed value now, to be done in the future */
double ts_filter_percentage = CNS_filter_percentage;
int filter_end = (int)(ts_filter_percentage*recalculation_epoch);
long long int cpu_save[recalculation_epoch];
long long int NIC_save[recalculation_epoch];
cns_map_t mapping[recalculation_epoch];
// step 2: when sync is not done, poll nic ts in certain interval
ts_t cpu_ts, nic_ts, cpu_start, cpu_end;
// some counting number
int epoch_count = 0;
int epoch_round = 0;
ts_t x_start = manager->get_cpu_time();
ts_t y_start = manager->get_nic_time();
while(1){
ts_t start_ts = get_real_clock();
// find a probe small enough
// probe
cpu_start = manager->get_cpu_time();
nic_ts = manager->get_nic_time();
cpu_end = manager->get_cpu_time();
int cpu_delta = (int)(cpu_end - cpu_start);
// cpu_ts = alpha*cpu_start + (1-alpha)*cpu_end
cpu_ts = cpu_end - (ts_t)(CNS_alpha*cpu_delta);
// save the data
int idx_ = epoch_count%recalculation_epoch;
mapping[idx_].delta = cpu_delta;
mapping[idx_].idx = idx_;
cpu_save[idx_] = cpu_ts-x_start;
NIC_save[idx_] = nic_ts-y_start;
epoch_count++;
#ifdef OUTPUT_MODE
gtimer->datalist->cns_dot<<cpu_start<<" "<<cpu_end<<" "<<nic_ts<<std::endl;
#endif
if(!gtimer->CNS_warmup_done && epoch_round>=CNS_warmup_epoch)
gtimer->CNS_warmup_done = true;
int dur = (int)(get_real_clock() - start_ts);
int res = CNS_gap - dur/1000;
if(res<0){
// fprintf(stdout, "Warning: CNS sampling Frequency too large, exceeding %d us.\n", -res);
;
}
else{
// assert(delta>=0&&delta<=1000);
if(res<0 || res>CNS_gap){
printf("Error delta: The delta is %d\n", res);
// assert(false);
res = 0;
}
usleep(res);
}
if(epoch_count%recalculation_epoch==0){
epoch_round++;
// filter the dot
std::sort(mapping, mapping+recalculation_epoch, cns_comp);
// use the LR
long long int x_sum=0, y_sum=0;
for(int i=0; i<filter_end; i++){
int true_idx = mapping[i].idx;
x_sum += cpu_save[true_idx];
y_sum += NIC_save[true_idx];
}
// get the result
long long int mid_x = x_sum/filter_end + x_start;
long long int mid_y = y_sum/filter_end + y_start;
manager->InsertResDot(mid_x, mid_y);
#ifdef OUTPUT_MODE
gtimer->datalist->cns_info<<mid_x<<" "<<mid_y<<std::endl;
gtimer->datalist->cns_dot<<"DEADBEAF"<<std::endl;
#endif
if(gtimer->is_done())
break;
}
}
fprintf(stdout, "Nic-CPU-sync thread quits.\n");
return NULL;
}
void GlobalTimer::run(){
if(rcc_master)
pthread_create(&master_thread, NULL, master_run, (void*)this);
if(SlaveCount!=0)
pthread_create(&slave_thread, NULL, slave_run, (void*)this);
pthread_create(&cpu_thread, NULL, cpu_nic_cs_thread, (void*)this);
wait_warmup_done();
}
bool GlobalTimer::is_local_warmup_done(){
if(!rcc_master)
local_warmup_done = true;
return local_warmup_done&&CNS_warmup_done;
}
bool GlobalTimer::is_warmup_done(){
return global_warmup_done;
}
void GlobalTimer::wait_warmup_done(){
// ts_t tp1 = get_real_clock();
while(!is_local_warmup_done())
sched_yield();
// ts_t tp2 = get_real_clock();
printf("local warmup done.\n");
// send msg to center
msg2cent msg;
msg.is_warmup_done = true;
msg.msg_id = 111;
send_to_center(&msg);
// ts_t tp3 = get_real_clock();
// recv msg from center
msg2master msg2;
recv_from_center(&msg2);
if(msg2.msg_id!=222){
printf("Wrong msg id %d\n", msg2.msg_id);
assert(false);
}
global_warmup_done = true;
printf("global warmup done.\n");
// ts_t tp4 = get_real_clock();
// printf("[Inside Info] time duration are: %lld, %lld, %lld ms\n", (tp2-tp1)/1000000, (tp3-tp2)/1000000, (tp4-tp3)/1000000);
}
void GlobalTimer::finalize(){
// join all sync connection
if(rcc_master)
pthread_join(master_thread, NULL);
if(SlaveCount!=0)
pthread_join(slave_thread, NULL);