-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipes.c
More file actions
183 lines (160 loc) · 4.51 KB
/
pipes.c
File metadata and controls
183 lines (160 loc) · 4.51 KB
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
#define _GNU_SOURCE
#include <sched.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "hwtimer.h"
#define LATENCY_RUNS 100
#define die(msg) \
do { \
char str[100]; \
snprintf(str, 100, "%s(%d): %s", __func__, __LINE__, msg); \
perror(str); \
exit(1); \
} while(0) \
void set_affinity(int cpuid) {
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(cpuid, &set);
if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
perror("sched_affinity");
}
void parent(int *fd, int *fd1, int size, int tput) {
int info;
char *buffer = calloc(sizeof(char), size);
if (buffer == NULL) {
die("cannot alloc memory for rx");
}
set_affinity(0);
if (tput == 0) {
int i;
for (i = 0; i < LATENCY_RUNS; i++) {
ssize_t r = 0, ret;
while ((ret = read(fd[0], buffer, size)) > 0) {
r += ret;
if (r == size) {
write(fd1[1], (void *)buffer, size);
break;
}
}
if (ret == -1)
perror("Error in receiving packets");
}
} else {
// TPUT test, we will receive atleast a 100MB of data
int num_pkts = (100 * 1024 * 1024) / size;
// avoid errors due to remainder
ssize_t tot_size = num_pkts * size;
ssize_t r = 0, ret;
// naive receive...
while ((ret = read(fd[0], buffer, size)) > 0) {
r += ret;
if (r == tot_size) {
write(fd1[1], (void *)buffer, 1);
break;
}
}
if (ret == -1) {
die("Error in receiving packets");
}
}
wait(&info);
free(buffer);
}
void child(int *fd, int *fd1, int size, int tput) {
char *buffer = calloc(sizeof(char), size);
if (buffer == NULL) {
die("cannot alloc memory for tx");
}
set_affinity(1);
if (tput == 0) {
int i;
hwtimer_t tsc_t;
uint64_t ns_time[LATENCY_RUNS];
for (i = 0; i < LATENCY_RUNS; i++) {
ssize_t r = 0, ret;
init_timer(&tsc_t);
start_timer(&tsc_t);
if (write(fd[1], (void *)buffer, size) == -1) {
die("child: write error");
}
// Do a round trip
while ((ret = read(fd1[0], buffer, size)) > 0) {
r += ret;
if (r == size) {
break;
}
}
stop_timer(&tsc_t);
ns_time[i]=get_timer_ns(&tsc_t)/2;
}
for (i = 0; i<LATENCY_RUNS;i++){
printf("%lu", ns_time[i]);
if(i!=LATENCY_RUNS-1){
printf("\n");
}
}
} else {
// TPUT test, send atleast a 100MB of data
int num_pkts = (100 * 1024 * 1024) / size;
ssize_t tot_size = num_pkts * size;
int i = 0;
ssize_t ret;
hwtimer_t tsc_t;
uint64_t ns_time;
init_timer(&tsc_t);
start_timer(&tsc_t);
for (i = 0; i < num_pkts; i++) {
if (write(fd[1], (void *)buffer, size) == -1) {
die("Send error in child");
}
}
// receive ack
if ((ret = read(fd1[0], buffer, size)) > 0) {
// calculate timings
}
stop_timer(&tsc_t);
ns_time=get_timer_ns(&tsc_t);
printf("%f \n", (1000)*((float)tot_size/ns_time));
if (ret == -1) {
die("Child: Error in receiving ack");
}
}
free(buffer);
}
int main(int argc, char **argv) {
int pid;
int ch;
int tput = 0;
unsigned long int size = 4;
int fd[2], fd1[2];
while ((ch = getopt(argc, argv, "p:s:t")) != -1) {
switch (ch) {
case 't':
tput = 1;
break;
case 's':
size = strtoul(optarg, NULL, 10);
break;
case '?':
default:
die("Not enough arguments");
}
}
if (pipe(fd) == -1 || pipe(fd1) == -1)
die("pipe");
//printf("running pipe ipc for bufsize = %lu, tput = %d\n", size, tput);
pid = fork();
if (pid == -1) {
die("Unable to fork");
} else if (pid != 0) {
// In parent, do parent related stuff
parent(fd, fd1, size, tput);
} else {
// In child process
child(fd, fd1, size, tput);
}
return 0;
}