-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_copy.c
296 lines (252 loc) · 7.74 KB
/
file_copy.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
#include <stdlib.h> // NULL, exit
#include <stdio.h> // printf, sprintf
#include <sys/types.h> // O_RDONLY, O_WRONLY
#include <sys/stat.h> // mkdir
#include <fcntl.h> // open
#include <unistd.h> // close
#include <sys/sendfile.h> // sendfile
#include <pthread.h> // pthread
#include <sys/wait.h> // wait
#include "testutils.h" // start, finish time structs
#include "log.h"
static int log_fd;
int generate_files(int w_numFiles, int w_fileSize)
{
if(mkdir("dummyfiles", S_IRWXU | S_IRWXO))
{
perror("mkdir");
return -1;
}
// generate dummy files for copying
for(int i = 0; i < w_numFiles; i++)
{
char filename[50] = { 0 };
int sprintfret = sprintf(filename, "%s%d", "dummyfiles/dummy", i);
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;
}
void* thread_copier(void* w_filename)
{
char* filename = w_filename;
int fdin, fdout;
char outFile[26] = {0};
int sprintfret = sprintf(outFile, "%s%s", filename, "_thread");
if(sprintfret < 0)
{
perror("sprintf");
return NULL;
}
if((fdin = open(filename, O_RDONLY)) == -1)
{
perror("open");
printf("the file was: %s\n", filename);
return NULL;
}
if((fdout = open(outFile, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXO | S_IRWXG)) == -1)
{
perror("open");
printf("the file was: %s\n", outFile);
// this return value does not need to be checked, right? :)
close(fdin);
return NULL;
}
off_t startOffset = 0;
struct stat fileinfo = {0};
fstat(fdin, &fileinfo);
int resbytes = sendfile(fdout, fdin, &startOffset, fileinfo.st_size);
if(resbytes < fileinfo.st_size)
{
perror("sendfile");
return NULL;
}
close(fdin);
close(fdout);
free(filename);
return NULL;
}
void file_copy(int w_numFiles, int w_fileSize, int w_drop_cache)
{
log_fd = open("performance_tester.log", O_RDWR | O_APPEND | O_CREAT | O_NONBLOCK, S_IRWXU);
log_write(log_fd, 1, "Multiple file copy measurement with threads & processes routine init.");
// sanity check
if(w_numFiles > 99 || w_numFiles < 1)
{
printf("File copying will not be tested because the user input was invalid. Please use 1-99 as number of files.\n");
return;
}
if(generate_files(w_numFiles, w_fileSize))
{
printf("Generating dummy files failed. Please try to manually remove the \"dummyfiles\" folder and its insides.\n");
return;
}
printf("\nMEASURING FILE TRANSFER SPEED IN THREADS VS FORKED PROCESSES\n");
printf("USING %d FILES EACH %d MiB IN SIZE\n", w_numFiles, w_fileSize);
printf("EACH FILE WILL HAVE ITS OWN THREAD/PROCESS PROCESSING IT\n");
start = malloc(sizeof(struct timespec));
finish = malloc(sizeof(struct timespec));
long totCopyTime = 0;
if(!start || !finish)
{
perror("malloc");
return;
}
// THREADS
pthread_t* threads = malloc(w_numFiles * sizeof(pthread_t));
if(!threads)
{
perror("malloc");
return;
}
// start timing for threads
clock_gettime(CLOCK_REALTIME, start);
// start copying of files using threads:
for(int i = 0; i < w_numFiles; i++)
{
char* filename = calloc(300, sizeof(char));
if(!filename)
{
perror("calloc");
return;
}
int sprintfret = sprintf(filename, "%s%d", "dummyfiles/dummy", i);
if(sprintfret < 0)
{
perror("sprintf");
free(filename);
return;
}
int ret = pthread_create(&threads[i], NULL, thread_copier, (void*)filename);
if(ret)
{
perror("pthread_create");
free(filename);
return;
}
}
for(int i = 0; i < w_numFiles; i++)
{
if(pthread_join(threads[i], NULL)) perror("pthread_join");
}
clock_gettime(CLOCK_REALTIME, finish);
totCopyTime = (finish->tv_nsec - start->tv_nsec);
printf("\nCopying of %d files (%d MiB each, total %d MiB) took %ld ns using threads.\n", w_numFiles, w_fileSize, w_numFiles * w_fileSize, totCopyTime);
clock_gettime(CLOCK_REALTIME, start);
// PROCESSES
pid_t child;
for(int i = 0; i < w_numFiles; i++)
{
if((child = fork()) == 0)
{
char filename[300] = {0};
int sprintfret = sprintf(filename, "%s%d", "dummyfiles/dummy", i);
char outFile[300] = {0};
sprintfret = sprintf(outFile, "%s%s", filename, "_fork");
if(sprintfret < 0)
{
perror("sprintf");
exit(0);
}
int fdin, fdout;
if((fdin = open(filename, O_RDONLY)) == -1)
{
perror("open");
printf("the file was: %s\n", filename);
exit(0);
}
if((fdout = open(outFile, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXO | S_IRWXG)) == -1)
{
perror("open");
printf("the file was: %s\n", outFile);
// this return value does not need to be checked, right? :)
close(fdin);
exit(0);
}
off_t startOffset = 0;
struct stat fileinfo = {0};
fstat(fdin, &fileinfo);
int resbytes = sendfile(fdout, fdin, &startOffset, fileinfo.st_size);
if(resbytes < fileinfo.st_size)
{
perror("sendfile");
exit(0);
}
close(fdin);
close(fdout);
exit(0);
}
}
pid_t waited;
while((waited = wait(NULL))> 0)
{
// waiting for the children to exit.
// here we could also check the exit values of them,
// but I didn't have time for that now.
}
clock_gettime(CLOCK_REALTIME, finish);
totCopyTime = (finish->tv_nsec - start->tv_nsec);
printf("\nCopying of %d files (%d MiB each, total %d MiB) took %ld ns using forks.\n", w_numFiles, w_fileSize, w_numFiles * w_fileSize, totCopyTime);
// clean up files copied by threads
for(int i = 0; i < w_numFiles; i++)
{
char filename[300] = {0};
int sprintfret = sprintf(filename, "%s%d%s", "dummyfiles/dummy", i, "_thread");
if(sprintfret < 0)
{
perror("sprintf");
return;
}
if(remove(filename)) perror("remove");
}
// clean up files copied by forks
for(int i = 0; i < w_numFiles; i++)
{
char filename[300] = {0};
int sprintfret = sprintf(filename, "%s%d%s", "dummyfiles/dummy", i, "_fork");
if(sprintfret < 0)
{
perror("sprintf");
return;
}
if(remove(filename)) perror("remove");
}
// FILE AND FOLDER CLEAN UP
for(int i = 0; i < w_numFiles; i++)
{
char filename[30] = {0};
int sprintfret = sprintf(filename, "%s%d", "dummyfiles/dummy", i);
if(sprintfret < 0)
{
perror("sprintf");
return;
}
if(remove(filename)) perror("remove");
}
if(remove("dummyfiles")) perror("remove");
close(log_fd);
free(threads);
free(start);
free(finish);
}