-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.cc
489 lines (392 loc) · 11.9 KB
/
filesystem.cc
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
/************************************************************************
*
* Filesystem stress and verify
*
* Authors: Goswin von Brederlow <[email protected]>
* Bernd Schubert <[email protected]>
*
* Copyright (C) 2007 Q-leap Networks, Goswin von Brederlow
* 2010 DataDirect Networks, Bernd Schubert
* 2013 Fraunhofer ITWM, Bernd Schubert
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write_main to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
************************************************************************/
#include "fstest.h"
#include "config.h"
static int stats_interval = 60;
//int size_max = 35; // 32GiB
//int stats_interval = 900;
using namespace std;
/* filesystem constructor */
Filesystem::Filesystem(string dir, size_t percent)
{
this->last_read_index = 0;
this->goal_percent = percent;
pthread_mutex_init(&this->mutex, NULL);
this->error_detected = false;
this->terminated = false;
this->max_files = get_global_cfg()->get_max_files();
// Create working dir
root_dir = new Dir(dir, this);
memset(&stats_all, 0, sizeof(stats_all));
this->fsfree = 0;
this->fssize = 0;
this->fsused = 0;
this->update_stats(false);
this->fs_use_goal = (this->fssize * percent) / 100;
cout << "Filesystem size : " << this->fssize / KILO << " kiB" << endl;
cout << "Filesystem free : " << this->fsfree << endl;
cout << "Filesystem used : " << ((this->fsused * 100) / this->fssize) << "%" << endl;
cout << "Filesystem use-goal : " << this->fs_use_goal / KILO << "kiB" << endl;
if ( (fssize - fsfree) >= this->fs_use_goal) {
cerr << "Error: Filesystem already above % used goal: "
<< this->fsused * 100.0 / fssize << " >= "
<< goal_percent << endl;
exit(1);
}
was_full = false;
}
Filesystem::~Filesystem(void)
{
this->lock();
if (this->root_dir != NULL && !this->error_detected) {
delete root_dir;
root_dir = NULL;
}
this->unlock();
pthread_mutex_destroy(&this->mutex);
}
void Filesystem::check_terminate_and_sleep(unsigned seconds)
{
if (this->terminated)
pthread_exit(NULL);
usleep(seconds * 1E6);
}
/* Update statistics about this filesystem
* Filesystem has to be locked */
void Filesystem::update_stats(bool size_only)
{
struct statvfs statvfsbuf;
// Get FS stats
if (statvfs(root_dir->path().c_str(), &statvfsbuf) != 0) {
perror("statvfs(): ");
EXIT(1);
}
#ifdef DEBUG
cout << "statvfsbuf.f_blocks=" << statvfsbuf.f_blocks <<endl
<< "statvfsbuf.f_frsize=" << statvfsbuf.f_frsize <<endl
<< "statvfsbuf.f_bavail=" << statvfsbuf.f_bavail
<< endl;
#endif
this->fssize = (uint64_t) statvfsbuf.f_blocks * statvfsbuf.f_frsize;
this->fsfree = (uint64_t) statvfsbuf.f_bavail * statvfsbuf.f_frsize;
this->fsused = this->fssize - this->fsfree;
if (size_only)
return;
// TODO: overload an op?*
this->stats_all.read += this->stats_now.read;
this->stats_all.write += this->stats_now.write;
this->stats_all.num_files += this->stats_now.num_files;
this->stats_all.num_read_files += this->stats_now.num_read_files;
this->stats_all.num_written_files += this->stats_now.num_written_files;
memset(&stats_old, 0, sizeof(stats_old));
memset(&stats_now, 0, sizeof(stats_now));
stats_old.time = time(NULL);
stats_now.time = time(NULL);
}
/**
* free some disk space if usage above goal
*/
void Filesystem::free_space(size_t fsize)
{
if (this->error_detected || this->terminated)
pthread_exit(NULL); // Don't delete anything, just exit immediately
this->update_stats(true);
int retry_count = 0;
while ((this->fsused + (uint64_t) fsize > this->fs_use_goal
&& this->files.size() > QL_FSTEST_MIN_NUM_FILES) ||
this->files.size() > this->max_files)
{
retry_count++;
if (!this->was_full) {
this->was_full = true;
cout << "Going into write/delete mode" << endl;
}
// Remove a file
this->lock();
int nfiles = this->files.size();
if (nfiles < 1) {
this->unlock();
break;
}
int idx = random() % nfiles;
this->file_iter = this->files.begin() + idx;
File *file = *this->file_iter;
// Don't delete a file that is in read or not checked yet
// Our read loop does not like that
if (file->trylock()) {
// File probably just in read
this->unlock();
continue;
}
this->unlock();
string fname = file->fname;
int nchecks = file->num_checks;
#if 0
// Check if read-thread already has verified it
if (nchecks == 0) {
file->unlock();
continue;
}
#endif
file->set_in_delete();
// check the file a last time
if (nchecks < 10) {
// cout << "num-files: " << this->files.size() <<
// " Unlink check: " << fname << endl;
if (file->check() ) {
this->error_detected = true;
// we exit the write_main thread
file->unlock();
pthread_exit(NULL);
}
// cout << "Unlink check done: " << fname << endl;
}
file->unlock();
this->lock();
delete file;
this->files.erase(this->file_iter);
this->update_stats(true);
this->unlock();
}
#if 0
if (retry_count)
cout << "Used after unlink: " << this->fsused / KILO << " kIB" <<
" retryCount: " << retry_count <<
" num files: " << this->files.size() <<
" used + new-file-size: " <<
(this->fsused + (uint64_t) fsize) / KILO << "kiB" <<
endl;
#endif
}
/** write_main thread
* when it deletes a file, it will start a read, though
*/
void Filesystem::write_main(void)
{
int level = 1;
new Dir(root_dir, 1);
ssize_t timeout = get_global_cfg()->get_timeout();
stats_all.time = time(NULL);
cout << "Starting test : " << ctime(&stats_old.time);
while((this->error_detected == false) && (this->terminated == false)) {
// cout << "all_dirs: " << all_dirs.size() << endl;
// cout << "active_dirs: " << active_dirs.size() << endl;
// Pick a random directory
unsigned dir_idx = random() % active_dirs.size();
// cout << "Picked " << active_dirs[num]->path() << endl;
Dir* dir = active_dirs[dir_idx];
// Create file
File *file = new File(dir);
// free some space by inDelete a file,
this->free_space(file->get_fsize() );
file->fwrite();
// cout << "Lock file sytem" << endl;
this->lock(); // LOCK FILESYSTEM
dir->add_file(file);
this->files.push_back(file);
this->stats_now.write += file->get_fsize();
this->stats_now.num_files++;
this->stats_now.num_written_files++;
// cout << "dir->num_files: " << active_dirs[num]->fsize() << endl;
// cout << "files: " << files.size() << endl;
// Remove dir from active_dirs if full
if (dir->get_num_files() >= dir->get_max_files())
this->active_dirs.erase(this->active_dirs.begin() + dir_idx);
if (active_dirs.size() == 0) {
++level;
cout << "Going to level " << level << endl;
active_dirs = all_dirs;
new Dir(root_dir, level);
};
// Output stats
stats_now.time = time(NULL);
if (stats_now.time - stats_old.time > stats_interval) {
double t = stats_now.time - stats_old.time;
double write = (stats_now.write - stats_old.write) / t / MEGA;
double read = (stats_now.read - stats_old.read) / t / MEGA;
double files = (stats_now.num_files - stats_old.num_files) / t;
cout << stats_now.time << " write: " << stats_now.write / GIGA
<< " GiB [" << write << " MiB/s] read: " << stats_now.read / GIGA
<< " GiB [" << read << " MiB/s] Files: " << stats_now.num_files
<< " [" << files << " files/s] # " << ctime(&stats_now.time)
<< " idx write: " << this->files.size()
<< " idx read: " << this->last_read_index
<< endl;
cout.flush();
this->update_stats(false);
}
// Check if the timeout is reached
ssize_t passed_time = stats_now.time - stats_all.time;
//cout << "Init-t: " << stats_all.time << " now-t: " << stats_now.time
// << " passed-t: " << passed_time << " timeout: " << timeout << std::endl;
if ((timeout != -1) && (passed_time > timeout)) {
cout << "Timeout reached. Now leaving!" << endl;
this->terminated = true;
}
// cout << "UnLock file sytem" << endl;
this->unlock(); // UNLOCK FILESYSTEM
// Some filesystems prefer writes over reads. But we don't want
// to let the reads to fall too far behind. Use arbitrary limit
// of 20 files
if (!this->was_full) {
while (this->last_read_index + 100 < this->files.size())
check_terminate_and_sleep(1);
} else {
while (this->stats_now.num_written_files > this->stats_now.num_read_files + 20)
check_terminate_and_sleep(1);
}
}
if (this->error_detected)
{
if (!(get_global_cfg()->get_error_immediate_stop()))
cout << "Error detected, leaving write thread!" << endl;
else
{
cout << "Error detected, aborting!" << endl;
exit(EXIT_FAILURE);
}
}
pthread_exit(NULL);
}
/* Read from the beginning to the end, if end is reached
* continue at the beginning
*/
void Filesystem::read_main(void)
{
unsigned long index = 0;
start_again:
this->lock();
// wait until the 2nd file is being written
while (this->files.size() < 2 && !this->error_detected) {
this->unlock();
sched_yield();
check_terminate_and_sleep(1);
goto start_again;
}
#ifdef DEBUG
cerr << "Starting to read files" << endl;
#endif
File *file = this->get_file_locked(index);
// now lock the file, still a chance of a race, until the unlink()
// methods also lock the filesystem first -> FIXME
file->lock();
this->unlock();
while(true) {
// file is locked here
int fsize = file->get_fsize();
if (file->is_being_deleted() ) {
file->unlock();
goto newindex;
}
if (file->check() )
{
this->error_detected = true;
if (get_global_cfg()->get_error_immediate_stop())
{
cout << "Error detected, aborting!" << endl;
exit(EXIT_FAILURE);
}
}
if (this->terminated)
pthread_exit(NULL);
file->unlock();
this->lock();
this->last_read_index = index;
this->stats_now.read += fsize;
this->stats_now.num_read_files++;
this->unlock();
// no lock here anymore
newindex:
index++;
if (this->was_full == false) {
while (index + 20 >= this->files.size() &&
!this->error_detected) {
// we want writes to be slightly ahead of reads
// due to the page cache
// give it some time to write_main new data
check_terminate_and_sleep(1);
if (this->was_full) {
// Filesystem was full
cout << "Re-starting to read from index 0 (was "
<< index << ")" << endl;
index = 0;
break;
}
}
} else if (index >= this->files.size()) {
// Filesystem was full
cout << "Re-starting to read from index 0 (was "
<< index << ")" << endl;
index = 0;
}
this->lock();
// double check, now that we have locked it
if (index >= this->files.size()) {
this->unlock();
index = 0;
goto start_again;
}
File *tmp;
tmp = this->files.at(index);
if (tmp->trylock()) {
this->unlock();
// file is busy, take next file
goto newindex;
}
this->unlock();
file = tmp;
}
}
void Filesystem::lock(void)
{
int rc = pthread_mutex_lock(&this->mutex);
if (rc) {
cerr << "Failed to lock filesystem" << " : " << strerror(rc);
perror(": ");
EXIT(1);
}
}
void Filesystem::unlock(void)
{
int rc = pthread_mutex_unlock(&this->mutex);
if (rc) {
cerr << "Failed to lock filesystem" << " : " << strerror(rc);
perror(": ");
EXIT(1);
}
}
int Filesystem::trylock(void)
{
int rc = pthread_mutex_trylock(&this->mutex);
if (rc && rc != EBUSY) {
cerr << "Failed to lock filesystem" << " : " << strerror(rc);
perror(": ");
EXIT(1);
}
RETURN(rc);
}