-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfstest.cc
221 lines (186 loc) · 5.41 KB
/
fstest.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
/************************************************************************
*
* 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
*
* 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 <execinfo.h>
#include "fstest.h"
#include "config.h"
static Config_fstest global_cfg;
#define BACKTRACE_MAX_SIZE 1024 * 1024
static void* backtrace_buffer[BACKTRACE_MAX_SIZE];
int do_exit(const char* func, const char *file, unsigned line, int code)
{
int err_fd = fileno(stderr);
int trace_len = backtrace(backtrace_buffer, BACKTRACE_MAX_SIZE);
backtrace_symbols_fd(backtrace_buffer, trace_len, err_fd);
fprintf(stderr, "%s() %s:%d Exit code %d\n", func, file, line, code);
exit(code);
}
/**
* Return the program wide configuration
*/
Config_fstest *get_global_cfg(void)
{
return &global_cfg;
}
using namespace std;
static char *cmd;
void usage(ostream &out)
{
out << endl;
out << cmd << " -h|--help - show help." << endl;
out << cmd << " [options] [<dir>] - directory on filesystem to test in." << endl;
out << endl;
out << "Options:\n";
out << " -p|--percent <percent> - goal percentage used of filesystem [90]." << endl;
out << " -t|--timeout <seconds> - goal timeout [-1]." << endl;
out << " -i|--immediate - check files immediately after writing them instead of" << endl
<< " letting the read-thread do it later on." << endl;
out << " --min-bits - min file size bits 2^n [20] (1MiB)." << endl;
out << " --max-bits - max file size bits 2^n. [30] (1GiB)." << endl;
out << endl;
}
/* Start the write_main thread here */
void *run_write_thread(void *arg)
{
Filesystem* t = (Filesystem *) arg;
t->write_main();
return NULL;
}
/* Start the write_main thread here */
void *run_read_thread(void *arg)
{
Filesystem* t = (Filesystem *) arg;
t->read_main();
return NULL;
}
void start_threads(void)
{
string dir = global_cfg.get_testdir();
size_t goal_percent = global_cfg.get_usage();
Filesystem * filesystem = new Filesystem(dir, goal_percent);
int rc;
pthread_t threads[2];
// struct pthread_arg tinfo[2];
// FIXME: We need a pthread wrapper class, our current way is ugly
int i = 0;
rc = pthread_create(&threads[i], NULL, run_write_thread, filesystem);
if (rc) {
cerr << "Failed to start thread " << i << ": "
<< strerror(rc) << endl;
EXIT(1);
}
i = 1;
rc = pthread_create(&threads[i], NULL, run_read_thread, filesystem);
if (rc) {
cerr << "Failed to start thread " << i << ": "
<< strerror(rc) << endl;
EXIT(1);
}
for (i = 0; i < 2; i++)
pthread_join(threads[i], NULL);
cout << "Thread " << i << "finished" << endl;
}
int main(int argc, char * const argv[])
{
int res;
struct stat statbuf;
cmd = argv[0];
// Getopt stuff
const char optstring[] = "hip:t:";
const struct option longopts[] = {
{ "help" , 0, NULL, 'h' },
{ "immediate", 0, NULL, 'i' },
{ "percent" , 1, NULL, 'p' },
{ "timeout" , 1, NULL, 't' },
{ "min-bits" , 1, NULL, 1 },
{ "max-bits" , 1, NULL, 2 },
{ NULL , 0, NULL, 0 }
};
int longindex = 0;
// Arguments
string testdir = "";
while((res = getopt_long(argc, argv, optstring, longopts, &longindex)) != -1) {
switch(res) {
case 'h':
usage(cout);
exit(0);
break;
case 'i':
global_cfg.set_immediate_check(true);
break;
case '?':
usage(cerr);
EXIT(1);
break;
case 'p':
global_cfg.set_usage(atoi(optarg) );
break;
case 't':
global_cfg.set_timeout(atoi(optarg) );
break;
case 1:
global_cfg.set_min_size_bits(atoi(optarg));
break;
case 2: global_cfg.set_max_size_bits(atoi(optarg));
break;
default:
printf ("Error: unknown option '%c'\n", res);
usage(cerr);
EXIT(1);
}
}
// Get optional arg <testdir> and add trailing '/'
if (optind < argc) {
testdir = argv[optind++];
}
if (optind < argc) {
int i;
cerr << "Error: extra args:";
for(i = optind; i < argc; ++i) {
cerr << " '" << argv[i] << "'";
}
cerr << endl;
usage(cerr);
EXIT(1);
}
// Check that testdir exists and is a directory
if (testdir.length() > 0) {
if (stat(testdir.c_str(), &statbuf) != 0) {
perror(testdir.c_str());
EXIT(1);
}
if (!S_ISDIR(statbuf.st_mode)) {
cerr << "Error: " << testdir << " is not a directory.\n";
EXIT(1);
}
}
global_cfg.set_testdir(testdir);
cout << "fstest v0.1\n";
cout << "Directory : " << ((testdir == "" ) ? "./" : testdir) << endl;
start_threads();
cout << "Done.\n";
RETURN(0);
}