forked from drepper/min-initrd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
OS_Eval.c
1427 lines (1170 loc) · 35.7 KB
/
OS_Eval.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
#define _GNU_SOURCE
#include <sched.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <stdbool.h>
#include <signal.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <sys/epoll.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/un.h>
int counter=3;
bool isFirstIteration = false;
const char *home;
char *output_fn = NULL;
char *new_output_fn = NULL;
#define setup (struct timespec *fp) \
{struct timespec timeA ; \
struct timespec timeC; \
clock_gettime(CLOCK_MONOTONIC,&timeA);
#define setup_2_a (struct timespec *fp) \
{struct timespec timeA; \
struct timespec timeC;
#define setup_2_b clock_gettime(CLOCK_MONOTONIC,&timeA);
#define ending clock_gettime(CLOCK_MONOTONIC,&timeC); \
add_diff_to_sum(fp,timeC,timeA);\
return;}
#define end1 clock_gettime(CLOCK_MONOTONIC,&timeC);
#define end2 add_diff_to_sum(fp,timeC,timeA);\
return;}
#define final_formula ",=\"AVERAGE(B%d:INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN()-1,4),\"1\",\"%d\")))\",=\"STDEV.P(B%d:INDIRECT(SUBSTITUTE(ADDRESS(1,COLUMN()-2,4),\"1\",\"%d\")))\"\n",counter,counter,counter,counter
#define OUTPUT_FILE_PATH ""
#define OUTPUT_FN OUTPUT_FILE_PATH "output_file.csv"
#define NEW_OUTPUT_FN OUTPUT_FILE_PATH "new_output_file.csv"
#define DEBUG false
#define BASE_ITER 100
#define PAGE_SIZE 4096
void add_diff_to_sum(struct timespec *result,struct timespec a, struct timespec b)
{
if (result->tv_nsec +a.tv_nsec < b.tv_nsec)
{
result->tv_nsec = 1000000000+result->tv_nsec+a.tv_nsec-b.tv_nsec;
result->tv_sec = result->tv_sec+a.tv_sec-b.tv_sec-1;
}
else if (result->tv_nsec+a.tv_nsec-b.tv_nsec >=1000000000)
{
result->tv_nsec = result->tv_nsec+a.tv_nsec-b.tv_nsec-1000000000;
result->tv_sec = result->tv_sec+a.tv_sec-b.tv_sec+1;
}
else
{
result->tv_nsec = result->tv_nsec + a.tv_nsec - b.tv_nsec;
result->tv_sec = result->tv_sec +a.tv_sec - b.tv_sec;
}
}
struct timespec *calc_diff(struct timespec *smaller, struct timespec *bigger)
{
struct timespec *diff = (struct timespec *)malloc(sizeof(struct timespec));
if (smaller->tv_nsec > bigger->tv_nsec)
{
diff->tv_nsec = 1000000000 + bigger->tv_nsec - smaller->tv_nsec;
diff->tv_sec = bigger->tv_sec - 1 - smaller->tv_sec;
}
else
{
diff->tv_nsec = bigger->tv_nsec - smaller->tv_nsec;
diff->tv_sec = bigger->tv_sec - smaller->tv_sec;
}
return diff;
}
struct timespec *timeB;
struct timespec *timeD;
struct timespec* calc_average(struct timespec *sum, int size)
{
struct timespec *average = (struct timespec *)malloc(sizeof(struct timespec));
average->tv_sec = 0;
average->tv_nsec = 0;
if (size == 0) return average;
average->tv_nsec = sum->tv_nsec / size + sum->tv_sec % size * 1000000000 / size;
average->tv_sec = sum->tv_sec / size;
return average;
}
struct timespec *calc_sum(struct timespec ** array, int size)
{
struct timespec *sum = (struct timespec *)malloc(sizeof(struct timespec));
sum->tv_sec = 0;
sum->tv_nsec = 0;
for (int i = 0; i < size; i ++)
{
if (array[i]->tv_nsec >= (1000000000 - sum->tv_nsec))
{
sum->tv_sec = sum->tv_sec +1;
sum->tv_nsec = array[i]->tv_nsec - (1000000000 - sum->tv_nsec);
sum->tv_sec = sum->tv_sec + array[i]->tv_sec;
} else {
sum->tv_nsec = sum->tv_nsec + array[i]->tv_nsec;
sum->tv_sec = sum->tv_sec + array[i]->tv_sec;
}
}
return sum;
}
struct timespec *calc_sum2(struct timespec * array, int size)
{
struct timespec *sum = (struct timespec *)malloc(sizeof(struct timespec));
sum->tv_sec = 0;
sum->tv_nsec = 0;
for (int i = 0; i < size; i ++)
{
if (array[i].tv_nsec >= (1000000000 - sum->tv_nsec))
{
sum->tv_sec = sum->tv_sec +1;
sum->tv_nsec = array[i].tv_nsec - (1000000000 - sum->tv_nsec);
sum->tv_sec = sum->tv_sec + array[i].tv_sec;
} else {
sum->tv_nsec = sum->tv_nsec + array[i].tv_nsec;
sum->tv_sec = sum->tv_sec + array[i].tv_sec;
}
}
return sum;
}
int comp(const void *ele1, const void *ele2)
{
struct timespec t1 = *((struct timespec *)ele1);
struct timespec t2 = *((struct timespec *)ele2);
if (t1.tv_sec > t2.tv_sec) {
return 1;
}
else if (t1.tv_sec == t2.tv_sec) {
if (t1.tv_nsec > t2.tv_nsec) return 1;
else if (t1.tv_nsec == t2.tv_nsec) return 0;
else return -1;
}
else {
return -1;
}
}
typedef struct testInfo {
int iter;
const char *name;
} testInfo;
#define INPRECISION 0.05
#define K 5
struct timespec *calc_k_closest(struct timespec *timeArray, int size)
{
if(DEBUG) printf("in calc_k_closest\n");
qsort(timeArray, size, sizeof(struct timespec), comp);
struct timespec **k_closest = (struct timespec **) malloc (sizeof(struct timespec*) * K);
for (int ii = 0; ii < K; ii++)
k_closest[ii] = NULL;
struct timespec *prev = &timeArray[0];
int j = 0;
k_closest[j] = prev;
j ++;
for (int i = 1; i < size; i ++)
{
struct timespec *curr = &timeArray[i];
if(DEBUG) printf("curr %ld.%09ld\n",curr->tv_sec, curr->tv_nsec);
if (curr->tv_sec != 0 || prev->tv_sec != 0) {
if(DEBUG) printf("[warn] test run greater than 1 second: ");
if(DEBUG) printf("prev %ld.%09ld, ",prev->tv_sec, prev->tv_nsec);
if(DEBUG) printf("curr %ld.%09ld\n",curr->tv_sec, curr->tv_nsec);
j = 0;
} else {
double diff = curr->tv_nsec - prev->tv_nsec;
double ratioDiff = diff/(double)prev->tv_nsec;
if(DEBUG) printf("diff: %lf\n", ratioDiff);
if (ratioDiff > INPRECISION)
{
j = 0;
for (int ii = 0; ii < K; ii++)
k_closest[ii] = NULL;
}
else
{
k_closest[j] = curr;
j ++;
}
}
if (j == K) break;
prev = curr;
}
if (DEBUG && j != K) printf("only found the %d closest\n", j);
struct timespec* result = k_closest[0];
if(DEBUG) printf("result %ld.%09ld\n", result->tv_sec, result->tv_nsec);
free(k_closest);
return result;
}
void one_line_test(FILE *fp, FILE *copy, void (*f)(struct timespec*), testInfo *info){
struct timespec testStart, testEnd;
clock_gettime(CLOCK_MONOTONIC,&testStart);
printf("Performing test %s.\n", info->name);
int runs = info->iter;
printf("Total test iteration %d.\n", runs);
struct timespec* timeArray = (struct timespec *)malloc(sizeof(struct timespec) * runs);
for (int i=0; i < runs; i++) {
timeArray[i].tv_sec = 0;
timeArray[i].tv_nsec = 0;
(*f)(&timeArray[i]);
}
struct timespec *sum = calc_sum2(timeArray, runs);
struct timespec *average = calc_average(sum, runs);
struct timespec *kbest = calc_k_closest(timeArray, runs);
if (!isFirstIteration)
{
char ch;
while (1)
{
ch=fgetc(copy);
if (ch == '\n') break;
fputc(ch,fp);
}
} else {
fprintf(fp, "%10s", info->name);
fprintf(fp, " kbest:,");
}
fprintf(fp,"%ld.%09ld,\n",kbest->tv_sec, kbest->tv_nsec);
if (!isFirstIteration)
{
char ch;
while (1)
{
ch=fgetc(copy);
if (ch == '\n') break;
fputc(ch,fp);
}
} else {
fprintf(fp, "%10s", info->name);
fprintf(fp, " average:,");
}
fprintf(fp,"%ld.%09ld,\n",average->tv_sec, average->tv_nsec);
free(sum);
free(average);
free(timeArray);
clock_gettime(CLOCK_MONOTONIC,&testEnd);
struct timespec *diffTime = calc_diff(&testStart, &testEnd);
printf("Test took: %ld.%09ld seconds\n",diffTime->tv_sec, diffTime->tv_nsec);
free(diffTime);
return;
}
void one_line_test_v2(FILE *fp, FILE *copy, void (*f)(struct timespec*, int, int *), testInfo *info){
struct timespec testStart, testEnd;
clock_gettime(CLOCK_MONOTONIC,&testStart);
printf("Performing test %s.\n", info->name);
int runs = info->iter;
printf("Total test iteration %d.\n", runs);
struct timespec* timeArray = (struct timespec *)malloc(sizeof(struct timespec) * runs);
for (int i = 0; i < runs; i++) {
timeArray[i].tv_sec = 0;
timeArray[i].tv_nsec = 0;
}
for (int i = 0; i < runs; ) {
(*f)(timeArray, info->iter, &i);
}
struct timespec *sum = calc_sum2(timeArray, runs);
struct timespec *average = calc_average(sum, runs);
struct timespec *kbest = calc_k_closest(timeArray, runs);
if (!isFirstIteration)
{
char ch;
while (1)
{
ch=fgetc(copy);
if (ch == '\n') break;
fputc(ch,fp);
}
} else {
fprintf(fp, "%10s", info->name);
fprintf(fp, " kbest:,");
}
fprintf(fp,"%ld.%09ld,\n",kbest->tv_sec, kbest->tv_nsec);
if (!isFirstIteration)
{
char ch;
while (1)
{
ch=fgetc(copy);
if (ch == '\n') break;
fputc(ch,fp);
}
} else {
fprintf(fp, "%10s", info->name);
fprintf(fp, " average:,");
}
fprintf(fp,"%ld.%09ld,\n",average->tv_sec, average->tv_nsec);
free(sum);
free(average);
free(timeArray);
clock_gettime(CLOCK_MONOTONIC,&testEnd);
struct timespec *diffTime = calc_diff(&testStart, &testEnd);
printf("Test took: %ld.%09ld seconds\n",diffTime->tv_sec, diffTime->tv_nsec);
free(diffTime);
return;
}
void two_line_test(FILE *fp, FILE *copy, void (*f)(struct timespec*,struct timespec*), testInfo *info){
struct timespec testStart, testEnd;
clock_gettime(CLOCK_MONOTONIC,&testStart);
printf("Performing test %s.\n", info->name);
int runs = info->iter;
printf("Total test iteration %d.\n", runs);
struct timespec* timeArrayParent = (struct timespec *) malloc(sizeof(struct timespec) * runs);
struct timespec* timeArrayChild = (struct timespec *) malloc(sizeof(struct timespec) * runs);
for (int i=0; i < runs; i++)
{
timeArrayParent[i].tv_sec = 0;
timeArrayParent[i].tv_nsec = 0;
timeArrayChild[i].tv_sec = 0;
timeArrayChild[i].tv_nsec = 0;
(*f)(&timeArrayChild[i],&timeArrayParent[i]);
}
struct timespec *sumParent = calc_sum2(timeArrayParent, runs);
struct timespec *sumChild = calc_sum2(timeArrayChild, runs);
struct timespec *averageParent = calc_average(sumParent, runs);
struct timespec *averageChild = calc_average(sumChild, runs);
struct timespec **averages = (struct timespec **) malloc(2*sizeof(struct timespec *));
averages[0] = averageParent;
averages[1] = averageChild;
struct timespec *kbestParent = calc_k_closest(timeArrayParent, runs);
struct timespec *kbestChild = calc_k_closest(timeArrayChild, runs);
struct timespec **kbests = (struct timespec **) malloc(2*sizeof(struct timespec *));
kbests[0] = kbestParent;
kbests[1] = kbestChild;
char ch;
if(!isFirstIteration)
{
for (int i=0; i<2; i++)
{
while(1)
{
ch=fgetc(copy);
if(ch=='\n')
break;
fputc(ch,fp);
}
fprintf(fp,"%ld.%09ld,\n",kbests[i]->tv_sec, kbests[i]->tv_nsec);
while(1)
{
ch=fgetc(copy);
if(ch=='\n')
break;
fputc(ch,fp);
}
fprintf(fp,"%ld.%09ld,\n",averages[i]->tv_sec,averages[i]->tv_nsec);
}
}
else
{
fprintf(fp, "%10s", info->name);
fprintf(fp," kbest:,%ld.%09ld,\n",kbests[0]->tv_sec, kbests[0]->tv_nsec);
fprintf(fp, "%10s", info->name);
fprintf(fp," average:,%ld.%09ld,\n",averages[0]->tv_sec, averages[0]->tv_nsec);
fprintf(fp, "%10s", info->name);
fprintf(fp," Child kbest:,%ld.%09ld,\n",kbests[1]->tv_sec, kbests[1]->tv_nsec);
fprintf(fp, "%10s", info->name);
fprintf(fp," Child average:,%ld.%09ld,\n",averages[1]->tv_sec, averages[1]->tv_nsec);
}
free(timeArrayChild);
free(timeArrayParent);
free(averages);
free(kbests);
free(sumParent);
free(sumChild);
free(averageParent);
free(averageChild);
clock_gettime(CLOCK_MONOTONIC,&testEnd);
struct timespec *diffTime = calc_diff(&testStart, &testEnd);
printf("Test took: %ld.%09ld seconds\n",diffTime->tv_sec, diffTime->tv_nsec);
free(diffTime);
return;
}
void forkTest(struct timespec *childTime, struct timespec *parentTime)
{
struct timespec timeA;
struct timespec timeC;
timeB = mmap(NULL, sizeof(struct timespec), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
int status;
clock_gettime(CLOCK_MONOTONIC,&timeA);
int forkId = fork();
if (forkId == 0){
clock_gettime(CLOCK_MONOTONIC, timeB);
kill(getpid(),SIGINT);
printf("[error] unable to kill child process\n");
return;
} else if (forkId > 0){
clock_gettime(CLOCK_MONOTONIC,&timeC);
wait(&status);
add_diff_to_sum(childTime,*timeB,timeA);
add_diff_to_sum(parentTime,timeC,timeA);
} else {
printf("[error] fork failed.\n");
}
munmap(timeB, sizeof(struct timespec));
return;
}
void *thrdfnc(void *args)
{
clock_gettime(CLOCK_MONOTONIC,timeB);
pthread_exit(NULL);
}
void threadTest(struct timespec *childTime, struct timespec *parentTime)
{
struct timespec timeC;
timeB=(struct timespec *)malloc(sizeof(struct timespec));
timeD=(struct timespec *)malloc(sizeof(struct timespec));
pthread_t newThrd;
clock_gettime(CLOCK_MONOTONIC,timeD);
int er = pthread_create (&newThrd, NULL, thrdfnc, NULL);
clock_gettime(CLOCK_MONOTONIC,&timeC);
pthread_join(newThrd,NULL);
add_diff_to_sum(parentTime,timeC,*timeD);
add_diff_to_sum(childTime,*timeB,*timeD);
free(timeB);
timeB = NULL;
free(timeD);
timeD = NULL;
return;
}
void getpid_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
clock_gettime(CLOCK_MONOTONIC, &startTime);
syscall(SYS_getpid);
clock_gettime(CLOCK_MONOTONIC, &endTime);
add_diff_to_sum(diffTime, endTime, startTime);
return;
}
int file_size = -1;
void read_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
char *buf_in = (char *) malloc (sizeof(char) * file_size);
int fd =open("/root/test_file.txt", O_RDONLY);
if (fd < 0) printf("invalid fd in read: %d\n", fd);
clock_gettime(CLOCK_MONOTONIC, &startTime);
syscall(SYS_read, fd, buf_in, file_size);
clock_gettime(CLOCK_MONOTONIC, &endTime);
close(fd);
add_diff_to_sum(diffTime, endTime, startTime);
free(buf_in);
return;
}
void read_warmup() {
char *buf_out = (char *) malloc (sizeof(char) * file_size);
for (int i = 0; i < file_size; i++) {
buf_out[i] = 'a';
}
int fd = open("/root/test_file.txt", O_CREAT | O_WRONLY);
if (fd < 0) printf("invalid fd in write: %d\n", fd);
syscall(SYS_write, fd, buf_out, file_size);
close(fd);
char *buf_in = (char *) malloc (sizeof(char) * file_size);
fd =open("/root/test_file.txt", O_RDONLY);
if (fd < 0) printf("invalid fd in read: %d\n", fd);
for (int i = 0; i < 1; i ++) {
syscall(SYS_read, fd, buf_in, file_size);
}
close(fd);
free(buf_out);
free(buf_in);
return;
}
void write_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
char *buf = (char *) malloc (sizeof(char) * file_size);
for (int i = 0; i < file_size; i++) {
buf[i] = 'a';
}
int fd = open("/root/test_file.txt", O_CREAT | O_WRONLY);
if (fd < 0) printf("invalid fd in write: %d\n", fd);
clock_gettime(CLOCK_MONOTONIC, &startTime);
syscall(SYS_write, fd, buf, file_size);
clock_gettime(CLOCK_MONOTONIC,&endTime);
close(fd);
add_diff_to_sum(diffTime, endTime, startTime);
free(buf);
return;
}
void mmap_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
int fd =open("/root/test_file.txt", O_RDONLY);
if (fd < 0) printf("invalid fd%d\n", fd);
clock_gettime(CLOCK_MONOTONIC, &startTime);
void *addr = (void *)syscall(SYS_mmap, NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
clock_gettime(CLOCK_MONOTONIC,&endTime);
syscall(SYS_munmap, addr, file_size);
close(fd);
add_diff_to_sum(diffTime, endTime, startTime);
return;
}
void page_fault_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
int fd =open("/root/test_file.txt", O_RDONLY);
if (fd < 0) printf("invalid fd%d\n", fd);
void *addr = (void *)syscall(SYS_mmap, NULL, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
clock_gettime(CLOCK_MONOTONIC, &startTime);
char a = *((char *)addr);
clock_gettime(CLOCK_MONOTONIC,&endTime);
printf("read: %c\n", a);
syscall(SYS_munmap, addr, file_size);
close(fd);
add_diff_to_sum(diffTime, endTime, startTime);
return;
}
void cpu_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
double start = 9903290.789798798;
double div = 3232.32;
clock_gettime(CLOCK_MONOTONIC,&startTime);
for (int i = 0; i < 500000; i ++) {
start = start / div;
}
clock_gettime(CLOCK_MONOTONIC,&endTime);
add_diff_to_sum(diffTime, endTime, startTime);
return;
}
void ref_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
clock_gettime(CLOCK_MONOTONIC, &startTime);
clock_gettime(CLOCK_MONOTONIC,&endTime);
add_diff_to_sum(diffTime, endTime, startTime);
return;
}
void munmap_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
int fd =open("/root/test_file.txt", O_RDWR);
if (fd < 0) printf("invalid fd%d\n", fd);
void *addr = (void *)syscall(SYS_mmap, NULL, file_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
for (int i = 0; i < file_size; i++) {
((char *)addr)[i] = 'b';
}
clock_gettime(CLOCK_MONOTONIC, &startTime);
syscall(SYS_munmap, addr, file_size);
clock_gettime(CLOCK_MONOTONIC,&endTime);
close(fd);
add_diff_to_sum(diffTime, endTime, startTime);
return;
}
int fd_count = -1;
void select_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
fd_set rfds;
struct timeval tv;
int retval;
FD_ZERO(&rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
int fds[fd_count];
int maxFd = -1;
for (int i = 0; i < fd_count; i++) {
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) printf("invalid fd in select: %d\n", fd);
if (fd > maxFd) maxFd = fd;
FD_SET(fd, &rfds);
fds[i] = fd;
}
clock_gettime(CLOCK_MONOTONIC, &startTime);
retval = syscall(SYS_select, maxFd + 1, &rfds, NULL, NULL, &tv);
clock_gettime(CLOCK_MONOTONIC, &endTime);
add_diff_to_sum(diffTime, endTime, startTime);
if (retval != fd_count) {
printf("[error] select return unexpected: %d\n", retval);
}
for (int i = 0; i < fd_count; i++) {
int retval = close(fds[i]);
if (retval == -1) printf ("[error] close failed in select test %d.\n", fds[i]);
}
return;
}
void poll_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
int retval;
int fds[fd_count];
struct pollfd pfds[fd_count];
memset(pfds, 0, sizeof(pfds));
for (int i = 0; i < fd_count; i++) {
char name[10];
name[0] = 'f';
name[1] = 'i';
name[2] = 'l';
name[3] = 'e';
int j = i;
int index = 4;
while (j > 0) {
name[index] = 48 + j%10;
j = j/10;
index ++;
}
name[index] = '\0';
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) printf("invalid fd in poll: %d\n", fd);
pfds[i].fd = fd;
pfds[i].events = POLLIN;
fds[i] = fd;
}
clock_gettime(CLOCK_MONOTONIC, &startTime);
retval = syscall(SYS_poll, pfds, fd_count, 0);
clock_gettime(CLOCK_MONOTONIC, &endTime);
add_diff_to_sum(diffTime, endTime, startTime);
if (retval != fd_count) {
printf("[error] poll return unexpected: %d\n", retval);
}
for (int i = 0; i < fd_count; i++) {
retval = close(fds[i]);
if (retval == -1) printf ("[error] close failed in poll test %d.\n", fds[i]);
}
return;
}
void epoll_test(struct timespec *diffTime) {
struct timespec startTime, endTime;
int retval;
int fds[fd_count];
int epfd = epoll_create(fd_count);
for (int i = 0; i < fd_count; i++) {
char name[10];
name[0] = 'f';
name[1] = 'i';
name[2] = 'l';
name[3] = 'e';
int j = i;
int index = 4;
while (j > 0) {
name[index] = 48 + j%10;
j = j/10;
index ++;
}
name[index] = '\0';
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) printf("[error] invalid fd in epoll: %d\n", fd);
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = fd;
retval = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event);
if (retval == -1) {
printf("[error] epoll_ctl failed.\n");
}
fds[i] = fd;
}
struct epoll_event *events = (struct epoll_event *)malloc(fd_count * sizeof(struct epoll_event));
clock_gettime(CLOCK_MONOTONIC, &startTime);
retval = epoll_wait(epfd, events, fd_count, 0);
clock_gettime(CLOCK_MONOTONIC, &endTime);
add_diff_to_sum(diffTime, endTime, startTime);
free(events);
if (retval != fd_count) {
printf("[error] epoll return unexpected: %d\n", retval);
}
retval = close(epfd);
if (retval == -1) printf ("[error] close epfd failed in epoll test %d.\n", epfd);
for (int i = 0; i < fd_count; i++) {
retval = close(fds[i]);
if (retval == -1) printf ("[error] close failed in epoll test %d.\n", fds[i]);
}
return;
}
void context_switch_test(struct timespec *diffTime) {
int iter = 1000;
struct timespec startTime, endTime;
int fds1[2], fds2[2], retval;
retval = pipe(fds1);
if (retval != 0) printf("[error] failed to open pipe1.\n");
retval = pipe(fds2);
if (retval != 0) printf("[error] failed to open pipe2.\n");
char w = 'a', r;
cpu_set_t cpuset;
int prio;
retval = sched_getaffinity(getpid(), sizeof(cpuset), &cpuset);
if (retval == -1) printf("[error] failed to get affinity.\n");
prio = getpriority(PRIO_PROCESS, 0);
if (prio == -1) printf("[error] failed to get priority.\n");
int forkId = fork();
if (forkId > 0) { // is parent
retval = close(fds1[0]);
if (retval != 0) printf("[error] failed to close fd1.\n");
retval = close(fds2[1]);
if (retval != 0) printf("[error] failed to close fd2.\n");
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(0, &set);
retval = sched_setaffinity(getpid(), sizeof(set), &set);
if (retval == -1) printf("[error] failed to set processor affinity.\n");
retval = setpriority(PRIO_PROCESS, 0, -20);
if (retval == -1) printf("[error] failed to set process priority.\n");
read(fds2[0], &r, 1);
clock_gettime(CLOCK_MONOTONIC, &startTime);
for (int i = 0; i < iter; i++) {
write(fds1[1], &w, 1);
read(fds2[0], &r, 1);
}
clock_gettime(CLOCK_MONOTONIC, &endTime);
int status;
wait(&status);
close(fds1[1]);
close(fds2[0]);
} else if (forkId == 0){
retval = close(fds1[1]);
if (retval != 0) printf("[error] failed to close fd1.\n");
retval = close(fds2[0]);
if (retval != 0) printf("[error] failed to close fd2.\n");
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(0, &set);
retval = sched_setaffinity(getpid(), sizeof(set), &set);
if (retval == -1) printf("[error] failed to set processor affinity.\n");
retval = setpriority(PRIO_PROCESS, 0, -20);
if (retval == -1) printf("[error] failed to set process priority.\n");
write(fds2[1], &w, 1);
for (int i = 0; i < iter; i++) {
read(fds1[0], &r, 1);
write(fds2[1], &w, 1);
}
kill(getpid(), SIGINT);
printf("[error] unable to kill child process\n");
return;
} else {
printf("[error] failed to fork.\n");
}
retval = sched_setaffinity(getpid(), sizeof(cpuset), &cpuset);
if (retval == -1) printf("[error] failed to restore affinity.\n");
retval = setpriority(PRIO_PROCESS, 0, prio);
if (retval == -1) printf("[error] failed to restore priority.\n");
struct timespec sum;
sum.tv_sec = 0;
sum.tv_nsec = 0;
add_diff_to_sum(&sum, endTime, startTime);
struct timespec *diff = calc_average(&sum, iter);
diffTime->tv_sec = diff->tv_sec;
diffTime->tv_nsec = diff->tv_nsec;
free(diff);
}
int msg_size = -1;
int curr_iter_limit = -1;
#define sock "/TEST_DIR/socket"
void send_test(struct timespec *timeArray, int iter, int *i) {
int retval;
int fds1[2], fds2[2];
retval = pipe(fds1);
if (retval != 0) printf("[error] failed to open pipe1.\n");
retval = pipe(fds2);
if (retval != 0) printf("[error] failed to open pipe1.\n");
char w = 'b', r;
struct sockaddr_un server_addr;
memset(&server_addr, 0, sizeof(struct sockaddr_un));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, home, sizeof(server_addr.sun_path) - 1);
strncpy(server_addr.sun_path, sock, sizeof(server_addr.sun_path) - 1);
int forkId = fork();
if (forkId < 0) {
printf("[error] fork failed.\n");
return;
}
if (forkId == 0) {
close(fds1[0]);
close(fds2[1]);
struct sockaddr_un client_addr;
socklen_t client_addr_len;
int fd_server = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd_server < 0) printf("[error] failed to open server socket.\n");
retval = bind(fd_server, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_un));
if (retval == -1) printf("[error] failed to bind.\n");
retval = listen(fd_server, 10);
if (retval == -1) printf("[error] failed to listen.\n");
if (DEBUG) printf("Waiting for connection\n");
write(fds1[1], &w, 1);
int fd_connect = accept(fd_server, (struct sockaddr *) &client_addr, &client_addr_len);
if (DEBUG) printf("Connection accepted.\n");
read(fds2[0], &r, 1);
remove(sock);
close(fd_server);
close(fd_connect);
close(fds1[1]);
close(fds2[0]);
kill(getpid(),SIGINT);
printf("[error] unable to kill child process\n");
return;
} else {
struct timespec startTime, endTime;
close(fds1[1]);
close(fds2[0]);
read(fds1[0], &r, 1);
int fd_client = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd_client < 0) printf("[error] failed to open client socket.\n");
retval = connect(fd_client, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_un));
if (retval == -1) printf("[error] failed to connect.\n");
char *buf = (char *) malloc (sizeof(char) * msg_size);
for (int i = 0; i < msg_size; i++) {
buf[i] = 'a';
}
retval = send(fd_client, buf, msg_size, MSG_DONTWAIT);
for (int j = 0; *i < iter & j < curr_iter_limit; (*i) ++, j++) {
clock_gettime(CLOCK_MONOTONIC,&startTime);
retval = syscall(SYS_sendto, fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0);
clock_gettime(CLOCK_MONOTONIC,&endTime);
add_diff_to_sum(&timeArray[*i], endTime, startTime);
if (retval == -1) {
printf("[error] failed to send.\n");
}
}
write(fds2[1], &w, 1);
close(fd_client);
close(fds1[0]);
close(fds2[1]);
free(buf);
int status;
wait(&status);
}
}
void recv_test(struct timespec *timeArray, int iter, int *i) {
int retval;
int fds1[2], fds2[2];
retval = pipe(fds1);
if (retval != 0) {
printf("[error] failed to open pipe1.\n");
}
retval = pipe(fds2);
if (retval != 0) {
printf("[error] failed to open pipe2.\n");
}