-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_lebench.c
1795 lines (1502 loc) · 40.8 KB
/
new_lebench.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
// Code adapted from https://github.com/LinuxPerfStudy/LEBench
#define _GNU_SOURCE
#include <sched.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/wait.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/un.h>
#include <x86intrin.h>
#include <inttypes.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#define MAX_SIZE 8192
#define PF_MAX_SIZE 100 * 4096
#define LOOP 1000
#define STEP 256
#define PF_STEP 4096
#define CENT ((MAX_SIZE / STEP) / 100)
#define PF_CENT ((PF_MAX_SIZE / PF_STEP) / 100)
#define ADDR_HINT 0x300000000000
#define CPU1 14
FILE *fp;
struct Record
{
size_t size;
struct timespec start;
struct timespec end;
};
//---------------------------------------------------------------------
#ifdef USE_VMALLOC
extern void *vmalloc(unsigned long size);
extern void vfree(const void *addr);
#endif
//---------------------------------------------------------------------
#ifdef BYPASS
extern ssize_t bp_write(int fd, const void *buf, size_t count);
extern ssize_t bp_read(int fd, void *buf, size_t count);
extern void *bp_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
extern int bp_munmap(void *addr, size_t length);
extern int bp_select(int nfds, fd_set *restrict readfds, fd_set *restrict writefds, fd_set *restrict exceptfds, struct timeval *restrict timeout);
extern int bp_poll(struct pollfd *fds, nfds_t nfds, int timeout);
extern int bp_epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);
extern pid_t bp_getppid(void);
extern pid_t bp_getpid(void);
extern ssize_t bp_sendto(int socket, const void *message, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len);
extern ssize_t bp_recvfrom(int socket, void *restrict buffer, size_t length, int flags, struct sockaddr *restrict address, socklen_t *restrict address_len);
#endif
//---------------------------------------------------------------------
#ifdef DEBUG
#define DEBUG 1
#else
#define DEBUG 0
#endif
//---------------------------------------------------------------------
void calc_diff(struct timespec *diff, struct timespec *bigger, struct timespec *smaller)
{
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;
}
}
void calc_sum(struct timespec *sum, struct timespec *bigger, struct timespec *smaller)
{
sum->tv_sec = 0;
sum->tv_nsec = 0;
if (smaller->tv_nsec >= (1000000000 - bigger->tv_nsec))
{
bigger->tv_sec = bigger->tv_sec + 1;
sum->tv_nsec = smaller->tv_nsec - (1000000000 - bigger->tv_nsec);
sum->tv_sec = bigger->tv_sec + smaller->tv_sec;
}
else
{
sum->tv_nsec = bigger->tv_nsec + smaller->tv_nsec;
sum->tv_sec = bigger->tv_sec + smaller->tv_sec;
}
}
void add_diff_to_sum(struct timespec *result, struct timespec *a, struct timespec *b)
{
struct timespec diff;
struct timespec tmp;
calc_diff(&diff, a, b);
tmp.tv_sec = result->tv_sec;
tmp.tv_nsec = result->tv_nsec;
calc_sum(result, &tmp, &diff);
}
void calc_average(struct timespec *average, struct timespec *sum, int size)
{
average->tv_sec = 0;
average->tv_nsec = 0;
if (size == 0)
return;
average->tv_nsec = sum->tv_nsec / size + sum->tv_sec % size * 1000000000 / size;
average->tv_sec = sum->tv_sec / size;
}
//---------------------------------------------------------------------
void getpid_bench(void)
{
struct timespec diff = {0, 0};
int l;
int loop = 100000;
struct Record *runs;
runs = mmap(NULL, sizeof(struct Record) * loop, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memset(runs, 0, sizeof(struct Record) * loop);
for (l = 0; l < loop; l++)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
#ifdef BYPASS
bp_getpid();
#else
syscall(SYS_getpid);
#endif
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
}
for (l = 0; l < loop; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
munmap(runs, sizeof(struct Record) * loop);
return;
}
void getppid_bench(void)
{
struct timespec diff = {0, 0};
int l;
int loop = 100000;
struct Record *runs;
runs = mmap(NULL, sizeof(struct Record) * loop, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memset(runs, 0, sizeof(struct Record) * loop);
for (l = 0; l < loop; l++)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
#ifdef BYPASS
bp_getppid();
#else
syscall(SYS_getppid);
#endif
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
}
for (l = 0; l < loop; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
munmap(runs, sizeof(struct Record) * loop);
return;
}
void clock_bench(void)
{
struct timespec diff = {0, 0};
int l;
int loop = 100000;
struct Record *runs;
runs = mmap(NULL, sizeof(struct Record) * loop, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memset(runs, 0, sizeof(struct Record) * loop);
for (l = 0; l < loop; l++)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
}
for (l = 0; l < loop; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
munmap(runs, sizeof(struct Record) * loop);
return;
}
void cpu_bench(void)
{
struct timespec diff = {0, 0};
int i, l;
double start;
double div;
int loop = 1000;
struct Record *runs;
runs = mmap(NULL, sizeof(struct Record) * loop, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memset(runs, 0, sizeof(struct Record) * loop);
for (l = 0; l < loop; l++)
{
start = 9903290.789798798;
div = 3232.32;
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
for (i = 0; i < 500000; i++)
{
start = start / div;
}
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
}
for (l = 0; l < loop; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
munmap(runs, sizeof(struct Record) * loop);
return;
}
void write_bench(int file_size)
{
struct timespec diff = {0, 0};
char *buf;
int fd, i, l;
struct Record *runs;
#if defined(USE_VMALLOC)
buf = (char *)vmalloc(sizeof(char) * file_size);
runs = (struct Record*)vmalloc(sizeof(struct Record) * LOOP * 10);
#elif defined(USE_MALLOC)
buf = (char *)malloc(sizeof(char) * file_size);
runs = (struct Record*)malloc(sizeof(struct Record) * LOOP * 10);
#else
buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * file_size,
PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
runs = (struct Record*)mmap(NULL, sizeof(struct Record) * LOOP * 10, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
for (i = 0; i < file_size; i++)
{
buf[i] = i % 93 + 33;
}
fd = open("test_file.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd < 0)
{
printf("invalid fd in write: %d\n", fd);
exit(0);
}
memset(runs, 0, sizeof(struct Record) * LOOP * 10);
for (l = 0; l < LOOP * 10; l++)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
#ifdef BYPASS
bp_write(fd, buf, file_size);
#else
syscall(SYS_write, fd, buf, file_size);
#endif
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
}
close(fd);
for (l = 0; l < LOOP * 10; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
#if defined(USE_VMALLOC)
vfree(buf);
vfree(runs);
#elif defined(USE_MALLOC)
free(buf);
free(runs);
#else
syscall(SYS_munmap, buf, file_size);
munmap(runs, sizeof(struct Record) * LOOP * 10);
#endif
return;
}
void read_bench(int file_size)
{
struct timespec diff = {0, 0};
char *buf;
int fd, l, i;
struct Record *runs;
#if defined(USE_VMALLOC)
buf = (char *)vmalloc(sizeof(char) * file_size);
runs = (struct Record*)vmalloc(sizeof(struct Record) * LOOP * 10);
#elif defined(USE_MALLOC)
buf = (char *)malloc(sizeof(char) * file_size);
runs = (struct Record*)malloc(sizeof(struct Record) * LOOP * 10);
#else
buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * file_size,
PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
runs = (struct Record*)mmap(NULL, sizeof(struct Record) * LOOP * 10, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
for (i = 0; i < file_size; i++)
{
buf[i] = i % 93;
}
fd = open("test_file.txt", O_RDONLY);
if (fd < 0)
{
perror("invalid fd in read\n");
exit(0);
}
memset(runs, 0, sizeof(struct Record) * LOOP * 10);
for (l = 0; l < LOOP * 10; l++)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
#ifdef BYPASS
bp_read(fd, buf, file_size);
#else
syscall(SYS_read, fd, buf, file_size);
#endif
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
}
close(fd);
for (l = 0; l < LOOP * 10; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
#if defined(USE_VMALLOC)
vfree(buf);
vfree(runs);
#elif defined(USE_MALLOC)
free(buf);
free(runs);
#else
syscall(SYS_munmap, buf, file_size);
munmap(runs, sizeof(struct Record) * LOOP * 10);
#endif
return;
}
#define sock "./my_sock"
void send_bench(int msg_size)
{
struct timespec diff = {0, 0};
// need to set affinity of parent and child to different cpus
int retval, forkId, status, l;
int fds1[2], fds2[2];
char w = 'b', r;
char *buf;
struct sockaddr_un server_addr;
struct Record *runs;
memset(&server_addr, 0, sizeof(struct sockaddr_un));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, sock, sizeof(server_addr.sun_path) - 1);
// create a buffer (this needs to be mmap)
#if defined(USE_VMALLOC)
buf = (char *)vmalloc(sizeof(char) * msg_size);
runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP);
#elif defined(USE_MALLOC)
buf = (char *)malloc(sizeof(char) * msg_size);
runs = (struct Record *)malloc(sizeof(struct Record) * LOOP);
#else
buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * msg_size,
PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
// write character 'a' in the buffer
for (int i = 0; i < msg_size; i++)
{
buf[i] = 'a';
}
memset(runs, 0, sizeof(struct Record) * LOOP);
for (l = 0; l < LOOP; l++)
{
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");
forkId = fork();
if (forkId < 0)
{
printf("[error] fork failed.\n");
return;
}
if (forkId == 0)
{
close(fds1[0]); // close the read end of pipe 1
close(fds2[1]); // close the write end of pipe 2
int fd_server = socket(AF_UNIX, SOCK_STREAM, 0); // create a socket for server
if (fd_server < 0)
printf("[error] failed to open server socket.\n");
// bind the socket to a filename so communication can occur
retval = bind(fd_server, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un));
if (retval == -1)
printf("[error] failed to bind.\n");
// Listen on the socket
retval = listen(fd_server, 10);
if (retval == -1)
printf("[error] failed to listen.\n");
if (DEBUG)
printf("Waiting for connection\n");
// Write to pipe 1 to let parent know we are listening
write(fds1[1], &w, 1);
// wait for connection
int fd_connect = accept(fd_server, (struct sockaddr *)0,
(socklen_t *)0);
if (DEBUG)
printf("Connection accepted.\n");
// Read update from parent on pipe 2
read(fds2[0], &r, 1);
// remove sockets and close file descriptors and pipes
remove(sock);
close(fd_server);
close(fd_connect);
close(fds1[1]);
close(fds2[0]);
// job done, time to exit
exit(0);
}
else
{
close(fds1[1]); // close the write end of pipe 1
close(fds2[0]); // close the read end of pipe 2
// Wait for update from child
read(fds1[0], &r, 1);
// create socket
int fd_client = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd_client < 0)
printf("[error] failed to open client socket.\n");
// connect to child address
retval = connect(fd_client, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un));
if (retval == -1)
printf("[error] failed to connect.\n");
// send the buffer over to child (for warm-up)
retval = send(fd_client, buf, msg_size, MSG_DONTWAIT);
// send buffer over to child and measure latency
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
#ifdef BYPASS
retval = bp_sendto(fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0);
#else
retval = syscall(SYS_sendto, fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0);
#endif
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
if (retval == -1)
{
printf("[error %d] failed to send. %s\n", errno, strerror(errno));
}
// update child so clean-up can happen
write(fds2[1], &w, 1);
// do cleanup your-self
close(fd_client);
close(fds1[0]);
close(fds2[1]);
wait(&status);
}
}
for (l = 0; l < LOOP; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
#if defined(USE_VMALLOC)
vfree(buf);
vfree(runs);
#elif defined(USE_MALLOC)
free(buf);
free(runs);
#else
syscall(SYS_munmap, buf, msg_size);
munmap(runs, sizeof(struct Record) * LOOP);
#endif
return;
}
void recv_bench(int msg_size)
{
struct timespec diff = {0, 0};
// need to set affinity of parent and child to different cpus
int retval, forkId, status, l;
int fds1[2], fds2[2];
char w = 'b', r;
char *buf;
struct sockaddr_un server_addr;
struct Record *runs;
if (DEBUG)
printf("recv_bench(%d)\n", msg_size);
memset(&server_addr, 0, sizeof(struct sockaddr_un));
server_addr.sun_family = AF_UNIX;
strncpy(server_addr.sun_path, sock, sizeof(server_addr.sun_path) - 1);
// create a buffer (this needs to be mmap)
#if defined(USE_VMALLOC)
buf = (char *)vmalloc(sizeof(char) * msg_size);
runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP);
#elif defined(USE_MALLOC)
buf = (char *)malloc(sizeof(char) * msg_size);
runs = (struct Record *)malloc(sizeof(struct Record) * LOOP);
#else
buf = (char *)syscall(SYS_mmap, (void *)ADDR_HINT, sizeof(char) * msg_size,
PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
memset(runs, 0, sizeof(struct Record) * LOOP);
for (l = 0; l < LOOP; l++)
{
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");
forkId = fork();
if (forkId < 0)
{
printf("[error] fork failed.\n");
return;
}
if (forkId > 0)
{ // parent
close(fds1[0]); // close the read end of pipe 1
close(fds2[1]); // close the write end of pipe 2
// create a socket for server
int fd_server = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd_server < 0)
printf("[error] failed to open server socket.\n");
// bind the socket to a filename so communication can occur
retval = bind(fd_server, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un));
if (retval == -1)
printf("[error] failed to bind.\n");
// Listen on the socket
retval = listen(fd_server, 11);
if (retval == -1)
printf("[error] failed to listen.\n");
if (DEBUG)
printf("Waiting for connection\n");
// Write to pipe 1 to let parent know we are listening
write(fds1[1], &w, 1);
// wait for connection
int fd_connect = accept(fd_server, (struct sockaddr *)0,
(socklen_t *)0);
if (DEBUG)
printf("Connection accepted.\n");
// Read update from parent on pipe 2
read(fds2[0], &r, 1);
// recv data from child (for warm-up)
retval = recv(fd_connect, buf, msg_size, MSG_DONTWAIT);
// recv data from child and measure latency
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
#ifdef BYPASS
retval = bp_recvfrom(fd_connect, buf, msg_size, MSG_DONTWAIT, NULL, NULL);
#else
retval = syscall(SYS_recvfrom, fd_connect, buf, msg_size, MSG_DONTWAIT, NULL, NULL);
#endif
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
if (retval == -1)
{
printf("[error %d] failed to send. %s\n", errno, strerror(errno));
}
// update child so clean-up can happen
write(fds1[1], &w, 1);
// remove sockets and close file descriptors and pipes
remove(sock);
close(fd_server);
close(fd_connect);
close(fds1[1]);
close(fds2[0]);
wait(&status);
}
else
{
close(fds1[1]); // close the write end of pipe 1
close(fds2[0]); // close the read end of pipe 2
// Wait for update from parent
read(fds1[0], &r, 1);
// create socket
int fd_client = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd_client < 0)
printf("[error] failed to open client socket.\n");
// connect to parent address
retval = connect(fd_client, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_un));
if (retval == -1)
printf("[error] failed to connect.\n");
// write character 'a' in the buffer
for (int i = 0; i < msg_size; i++)
{
buf[i] = i % 93;
}
// send data over to parent + one more copy for warm up
for (l = 0; l < 2; l++)
{
retval = syscall(SYS_sendto, fd_client, buf, msg_size, MSG_DONTWAIT, NULL, 0);
if (retval == -1)
{
printf("[error %d] failed to send. %s\n", errno, strerror(errno));
}
}
// tell parent it should start reading
write(fds2[1], &w, 1);
// wait for parent
read(fds1[0], &r, 1);
// do cleanup your-self
close(fd_client);
close(fds1[0]);
close(fds2[1]);
// job done, time to exit
exit(0);
}
}
for (l = 0; l < LOOP; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%d,%ld.%09ld\n", l, msg_size, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
#if defined(USE_VMALLOC)
vfree(buf);
vfree(runs);
#elif defined(USE_MALLOC)
free(buf);
free(runs);
#else
syscall(SYS_munmap, buf, msg_size);
munmap(runs, sizeof(struct Record) * LOOP);
#endif
return;
}
struct timespec *forkTime;
void fork_bench(void)
{
struct timespec diff = {0, 0}, ave= {0, 0};
forkTime = mmap(NULL, sizeof(struct timespec) * LOOP, PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
int forkId, l, status;
struct Record *runs;
#if defined(USE_VMALLOC)
runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP);
#elif defined(USE_MALLOC)
runs = (struct Record *)malloc(sizeof(struct Record) * LOOP);
#else
runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
memset(runs, 0, sizeof(struct Record) * LOOP);
memset(forkTime, 0, sizeof(struct timespec) * LOOP);
for (l = 0; l < LOOP; l++)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
forkId = fork();
if (forkId == 0)
{
clock_gettime(CLOCK_MONOTONIC, &forkTime[l]);
exit(0);
}
else if (forkId > 0)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
wait(&status);
}
else
{
printf("[error] fork failed.\n");
fflush(stdout);
}
}
for (l = 0; l < LOOP; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
calc_diff(&ave, forkTime, &runs[l].start);
fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec, ave.tv_sec, ave.tv_nsec);
}
fflush(fp);
munmap(forkTime, sizeof(struct timespec));
#if defined(USE_VMALLOC)
vfree(runs);
#elif defined(USE_MALLOC)
free(runs);
#else
munmap(runs, sizeof(struct Record) * LOOP);
#endif
return;
}
void *thrdfnc(void *args)
{
clock_gettime(CLOCK_MONOTONIC, (struct timespec*)args);
pthread_exit(0);
}
void thread_bench(void)
{
struct timespec diff = {0, 0}, ave= {0, 0};
int l;
pthread_t newThrd;
struct timespec *threads;
struct Record *runs;
#if defined(USE_VMALLOC)
runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP);
threads = (struct timespec *)vmalloc(sizeof(struct timespec) * LOOP);
#elif defined(USE_MALLOC)
runs = (struct Record *)malloc(sizeof(struct Record) * LOOP);
threads = (struct timespec *)malloc(sizeof(struct timespec) * LOOP);
#else
runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
threads = (struct timespec *)mmap(NULL, sizeof(struct timespec) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
memset(runs, 0, sizeof(struct Record) * LOOP);
memset(threads, 0, sizeof(struct timespec) * LOOP);
for (l = 0; l < LOOP; l++)
{
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
pthread_create(&newThrd, NULL, thrdfnc, &threads[l]);
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
pthread_join(newThrd, NULL);
}
for (l = 0; l < LOOP; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
calc_diff(&ave, &threads[l], &runs[l].start);
fprintf(fp, "%d,%ld.%09ld,%ld.%09ld\n", l, diff.tv_sec, diff.tv_nsec, ave.tv_sec, ave.tv_nsec);
}
fflush(fp);
#if defined(USE_VMALLOC)
vfree(runs);
vfree(threads);
#elif defined(USE_MALLOC)
free(runs);
free(threads);
#else
munmap(runs, sizeof(struct Record) * LOOP);
munmap(threads, sizeof(struct timespec) * LOOP);
#endif
return;
}
void pagefault_bench(int file_size)
{
struct timespec diff = {0, 0};
struct Record *runs;
int l, i;
char *addr;
#if defined(USE_VMALLOC)
runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP);
#elif defined(USE_MALLOC)
runs = (struct Record *)malloc(sizeof(struct Record) * LOOP);
#else
runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
memset(runs, 0, sizeof(struct Record) * LOOP);
for (l = 0; l < LOOP; l++)
{
addr = (char *)mmap((void *)ADDR_HINT, file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
i = 0;
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
while (i < file_size)
{
addr[i] = i % 93 + 33;
i = i + 4096;
}
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
munmap(addr, file_size);
}
for (l = 0; l < LOOP; l++)
{
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
#if defined(USE_VMALLOC)
vfree(runs);
#elif defined(USE_MALLOC)
free(runs);
#else
munmap(runs, sizeof(struct Record) * LOOP);
#endif
return;
}
void stack_pagefault_bench(int file_size)
{
int l, i;
char *addr;
struct Record *runs;
#if defined(USE_VMALLOC)
runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP);
#elif defined(USE_MALLOC)
runs = (struct Record *)malloc(sizeof(struct Record) * LOOP);
#else
runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
memset(runs, 0, sizeof(struct Record) * LOOP);
for (l = 0; l < LOOP; l++)
{
addr = (char *)alloca(file_size * sizeof(long));
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
i = 0;
while (i < file_size)
{
addr[i] = i % 93 + 33;
i = i + 4096;
}
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
}
for (l = 0; l < LOOP; l++)
{
struct timespec diff;
calc_diff(&diff, &runs[l].end, &runs[l].start);
fprintf(fp, "%d,%d,%ld.%09ld\n", l, file_size, diff.tv_sec, diff.tv_nsec);
}
fflush(fp);
#if defined(USE_VMALLOC)
vfree(runs);
#elif defined(USE_MALLOC)
free(runs);
#else
munmap(runs, sizeof(struct Record) * LOOP);
#endif
return;
}
static void fault_around_bench(int file_size)
{
struct timespec diff = {0, 0};
struct Record *runs;
int l, i;
char *addr;
#if defined(USE_VMALLOC)
runs = (struct Record *)vmalloc(sizeof(struct Record) * LOOP);
#elif defined(USE_MALLOC)
runs = (struct Record *)malloc(sizeof(struct Record) * LOOP);
#else
runs = (struct Record *)mmap(NULL, sizeof(struct Record) * LOOP, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
int fd = open("test_file.txt", O_RDONLY);
memset(runs, 0, sizeof(struct Record) * LOOP);
for (l = 0; l < LOOP; l++)
{
addr = (char *)mmap((void *)ADDR_HINT, file_size, PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
i = 0;
clock_gettime(CLOCK_MONOTONIC, &runs[l].start);
char a = *addr;
clock_gettime(CLOCK_MONOTONIC, &runs[l].end);
munmap(addr, file_size);
}