-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_transfer.c
355 lines (288 loc) · 9.49 KB
/
file_transfer.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
#include <sys/types.h> // O_RDONLY, O_WRONLY
#include <time.h> // struct timespec, clock_gettime()
#include <sys/stat.h> // mkdir
#include <sys/mman.h> // mmap
#include <fcntl.h> // open
#include <unistd.h> // close
#include <limits.h> // PIPE_BUF
#include <stdio.h> // printf, sprintf
#include <stdlib.h> // NULL, exit
#include <pthread.h> // threading, barriers
#include <string.h> // memcpy
#include "testutils.h" // start, finish time structs
#include "log.h"
static int log_fd;
int remove_dummyfile()
{
char filename[17] = {0};
int sprintfret = sprintf(filename, "%s", "dummyfiles/dummy");
if(sprintfret < 0)
{
perror("sprintf");
return -1;
}
if(remove(filename))
{
perror("remove");
return -2;
}
if(remove("dummyfiles"))
{
perror("remove");
return -3;
}
return 0;
}
int generate_file(int w_fileSize)
{
if(mkdir("dummyfiles", S_IRWXU | S_IRWXO))
{
perror("mkdir");
return -1;
}
char filename[17] = { 0 };
int sprintfret = sprintf(filename, "%s", "dummyfiles/dummy");
if(sprintfret < 0)
{
perror("sprintf");
return -2;
}
FILE *fp=fopen(filename, "w");
if(!fp)
{
perror("fopen");
return -3;
}
int trunc = ftruncate(fileno(fp), 1000*1000*100);
if(trunc)
{
perror("ftruncate");
return -4;
}
int fcloseret = fclose(fp);
if(fcloseret)
{
perror("fclose");
return -5;
}
return 0;
}
int pipe_transfer(int w_bufferSize, int w_fileSize)
{
int bufferSize = w_bufferSize;
if(w_bufferSize > PIPE_BUF || w_bufferSize < 1)
{
printf("Buffer size of %d bytes was too large. Using maximum value of %d\n", w_bufferSize, PIPE_BUF);
bufferSize = PIPE_BUF;
}
// create pipe
int pipefd[2];
if(pipe(pipefd)) return -1;
start = mmap(NULL, sizeof(struct timespec), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
finish = mmap(NULL, sizeof(struct timespec), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if(start == MAP_FAILED || finish == MAP_FAILED) return -2;
// set pipe buffer size
if((fcntl(pipefd[0], F_SETPIPE_SZ, bufferSize)) != bufferSize)
{
munmap(start, sizeof(struct timespec));
munmap(finish, sizeof(struct timespec));
return -3;
}
pid_t child;
if((child = fork()) == 0)
{
clock_gettime(CLOCK_REALTIME, start);
// child, here we send the data to parent.
FILE* fp = fopen("dummyfiles/dummy", "rb");
if(!fp)
{
perror("File opening failed.");
exit(0);
}
int charsRead = 0;
char buffer[bufferSize];
// write loop
while((charsRead = fread(buffer, 1, bufferSize, fp)) == bufferSize)
{
if((write(pipefd[1], buffer, charsRead)) == -1)
{
perror("write");
}
}
if((write(pipefd[1], buffer, charsRead)) == -1)
{
perror("write");
exit(0);
}
int fcloseret = fclose(fp);
if(fcloseret)
{
perror("fclose");
exit(0);
}
close(pipefd[1]);
exit(0);
}
else
{
// parent, we receive the file here
char buffer[bufferSize];
FILE* fp = fopen("dummyfiles/dummywrite", "wb");
if(!fp)
{
perror("File opening failed.");
return -1;
}
int charsWritten = 0;
int readret;
if((readret = read(pipefd[0], buffer, bufferSize)) != bufferSize)
{
perror("read");
return -1;
}
// reading loop, read until not getting bufferSize bytes
do
{
if(readret != bufferSize) printf("Something went wrong, please restart the test.\n");
if((fwrite(buffer, 1, bufferSize, fp)) != bufferSize)
{
perror("fwrite");
}
} while((readret = read(pipefd[0], buffer, bufferSize)) == bufferSize);
if((charsWritten = fwrite(buffer, 1, readret, fp)) != readret)
{
perror("fwrite");
}
fclose(fp);
clock_gettime(CLOCK_REALTIME, finish);
}
double total = (finish->tv_nsec - start->tv_nsec);
printf("Pipe\t\ttransferring %d MiB file with %d byte-sized buffers takes %.2f us. That's %f MiB/s.\n", w_fileSize, w_bufferSize, (double)(total / 1000), (1 / ((double)total / 1000000000)) * w_fileSize);
// remove the copied file
if(remove("dummyfiles/dummywrite"))
{
perror("remove");
munmap(start, sizeof(struct timespec));
munmap(finish, sizeof(struct timespec));
return -1;
}
munmap(start, sizeof(struct timespec));
munmap(finish, sizeof(struct timespec));
return 0;
}
void* shared_memory_sender(void* w_args)
{
struct shared_memory_args_struct* args = (struct shared_memory_args_struct*)w_args;
FILE* fp = fopen("dummyfiles/dummy", "rb");
if(!fp)
{
perror("File opening failed.");
return NULL;
}
int charsRead = 0;
char buffer[*args->bufferSize];
pthread_barrier_wait(args->barr);
while((charsRead = fread(buffer, 1, *args->bufferSize, fp)) == *args->bufferSize)
{
pthread_barrier_wait(args->barr);
*(args->bytesToRead) = charsRead;
memcpy(args->filebuff, buffer, *args->bufferSize);
pthread_barrier_wait(args->barr);
}
pthread_barrier_wait(args->barr);
*(args->bytesToRead) = charsRead;
memcpy(args->filebuff, buffer, charsRead);
fclose(fp);
return NULL;
}
int shared_memory_transfer(int w_bufferSize, int w_fileSize)
{
int tempBuf = w_bufferSize;
if(w_bufferSize < 1)
{
printf("Invalid buffer size for shared memory transfer. Using default size of %d bytes.\n", PIPE_BUF);
tempBuf = PIPE_BUF;
}
pthread_barrier_t* barr;
unsigned char* filebuff;
unsigned int* bytesToRead;
int* bufferSize;
barr = mmap(NULL, sizeof(pthread_barrier_t), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
filebuff = mmap(NULL, tempBuf, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
start = mmap(NULL, sizeof(struct timespec), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
finish = mmap(NULL, sizeof(struct timespec), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
bytesToRead = mmap(NULL, sizeof(unsigned int), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
bufferSize = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
if(barr == MAP_FAILED || filebuff == MAP_FAILED || start == MAP_FAILED || finish == MAP_FAILED || bytesToRead == MAP_FAILED || bufferSize == MAP_FAILED)
{
return -1;
}
*bufferSize = tempBuf;
pthread_barrier_init(barr, NULL, 2);
struct shared_memory_args_struct args;
args.barr = barr;
args.bytesToRead = bytesToRead;
args.filebuff = filebuff;
args.bufferSize = bufferSize;
pthread_t threadCreation;
clock_gettime(CLOCK_REALTIME, start);
pthread_create(&threadCreation, NULL, shared_memory_sender, (void*)&args);
// in parent, we receive file here
FILE* fp = fopen("dummyfiles/dummywrite", "wb");
int readBytes;
if(!fp)
{
perror("File opening failed.");
return -1;
}
do
{
pthread_barrier_wait(barr);
pthread_barrier_wait(barr);
} while((readBytes = fwrite(filebuff, 1, *bytesToRead, fp)) == *bufferSize);
printf("last readBytes was %d\n", readBytes);
fclose(fp);
clock_gettime(CLOCK_REALTIME, finish);
long total = (finish->tv_nsec - start->tv_nsec);
printf("\nShared memory\ttransferring %d MiB file with %d byte-sized buffers takes %.2f us. That's %f MiB/s.\n", w_fileSize, w_bufferSize, (double)(total / 1000), (1 / ((double)total / 1000000000)) * w_fileSize);
// remove the copied file
if(remove("dummyfiles/dummywrite"))
{
perror("remove");
munmap(start, sizeof(struct timespec));
munmap(finish, sizeof(struct timespec));
return -1;
}
pthread_join(threadCreation, NULL);
munmap(barr, sizeof(pthread_barrier_t));
munmap(filebuff, *bufferSize);
munmap(start, sizeof(struct timespec));
munmap(finish, sizeof(struct timespec));
munmap(bytesToRead, sizeof(unsigned int));
munmap(bufferSize, sizeof(int));
return 0;
}
int message_queue_transfer()
{
return 0;
}
void file_transfer(int w_fileSize, int w_BufferSize, int w_dropCache)
{
log_fd = open("performance_tester.log", O_RDWR | O_APPEND | O_CREAT | O_NONBLOCK, S_IRWXU);
log_write(log_fd, 1, "File transfer speed measurement with pipes & shared memory routine done.");
printf("\nMEASURING PIPE vs SHARED MEMORY FILE TRANSFER SPEED\n");
if(generate_file(w_fileSize))
{
perror("generating dummy file failed. Please try to remove the dummyfile folder by hand");
return;
}
if(shared_memory_transfer(w_BufferSize, w_fileSize)) perror("shared_memory_transfer");
if(pipe_transfer(w_BufferSize, w_fileSize)) perror("pipe_transfer");
if(remove_dummyfile())
{
perror("generating dummy file failed. Please try to remove the dummyfile folder by hand");
return;
}
close(log_fd);
return;
}