-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
541 lines (477 loc) · 16.2 KB
/
main.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* Disktest
* Copyright (c) International Business Machines Corp., 2001
*
*
* 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 to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Please send e-mail to yardleyb@us.ibm.com if you have
* questions or comments.
*
* Project Website: TBD
*
* $Id: main.c,v 1.37.2.1 2009/01/21 00:53:26 yardleyb Exp $
*
*/
#include <stdio.h>
#ifdef WINDOWS
#include <windows.h>
#include <winioctl.h>
#include <io.h>
#include <process.h>
#include <sys/stat.h>
#include "getopt.h"
#else
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include "defs.h"
#include "globals.h"
#include "main.h"
#include "usage.h"
#include "sfunc.h"
#include "parse.h"
#include "childmain.h"
#include "threading.h"
#include "dump.h"
#include "timer.h"
#include "stats.h"
#include "signals.h"
/* global */
child_args_t cleanArgs;
test_env_t cleanEnv;
char hostname[HOSTNAME_SIZE]; /* global system hostname */
void linear_read_write_test(test_ll_t *test)
{
int i;
if(test->args->flags & CLD_FLG_W) {
test->env->bContinue = TRUE;
test->env->request_lba.wLBA = test->args->start_lba;
test->args->test_state = DIRCT_INC(test->args->test_state);
test->env->lastAction.oper = WRITER;
test->args->test_state = SET_OPER_W(test->args->test_state);
test->args->test_state = SET_wFST_TIME(test->args->test_state);
/* srand(test->args->seed); * reseed so we can re create the same random transfers */
memset(test->env->action_list,0,sizeof(action_t)*test->args->t_kids);
test->env->action_list_entry = 0;
test->env->wcount = 0;
test->env->gw_start_time = 0;
test->env->gw_stop_time = 0;
if(test->args->flags & CLD_FLG_CYC)
if(test->args->cycles == 0) {
pMsg(INFO,test->args, "Starting write pass, cycle %lu\n", (unsigned long) test->env->pass_count);
} else {
pMsg(INFO,test->args, "Starting write pass, cycle %lu of %lu\n", (unsigned long) test->env->pass_count, test->args->cycles);
}
else {
pMsg(INFO,test->args, "Starting write pass\n");
}
CreateTestChild(ChildTimer, test);
for(i=0;i<test->args->t_kids;i++) {
CreateTestChild(ChildMain, test);
}
/* Wait for the writers to finish */
cleanUpTestChildren(test);
}
/* If the write test failed don't start the read test */
if(!(TST_STS(test->args->test_state))) { return; }
if(test->args->flags & CLD_FLG_R) {
test->env->bContinue = TRUE;
test->env->request_lba.rLBA = test->args->start_lba;
test->args->test_state = DIRCT_INC(test->args->test_state);
test->env->lastAction.oper = READER;
test->args->test_state = SET_OPER_R(test->args->test_state);
test->args->test_state = SET_rFST_TIME(test->args->test_state);
/* srand(test->args->seed); * reseed so we can re create the same random transfers */
memset(test->env->action_list,0,sizeof(action_t)*test->args->t_kids);
test->env->action_list_entry = 0;
test->env->rcount = 0;
test->env->gr_start_time = 0;
test->env->gr_stop_time = 0;
if(test->args->flags & CLD_FLG_CYC)
if(test->args->cycles == 0) {
pMsg(INFO,test->args, "Starting read pass, cycle %lu\n", (unsigned long) test->env->pass_count);
} else {
pMsg(INFO,test->args, "Starting read pass, cycle %lu of %lu\n", (unsigned long) test->env->pass_count, test->args->cycles);
}
else {
pMsg(INFO,test->args, "Starting read pass\n");
}
CreateTestChild(ChildTimer, test);
for(i=0;i<test->args->t_kids;i++) {
CreateTestChild(ChildMain, test);
}
/* Wait for the readers to finish */
cleanUpTestChildren(test);
}
}
unsigned long init_data(test_ll_t *test, unsigned char **data_buffer_unaligned)
{
int i;
unsigned long data_buffer_size;
#ifdef WINDOWS
if(CreateMutex(NULL, FALSE, "gbl") == NULL) {
pMsg(ERR, test->args, "Failed to create semaphore, error = %u\n", GetLastError());
return(GetLastError());
}
if((test->env->mutexs.MutexACTION = CreateMutex(NULL, FALSE, NULL)) == NULL) {
pMsg(ERR, test->args, "Failed to create semaphore, error = %u\n", GetLastError());
return(GetLastError());
}
if((test->env->mutexs.MutexIO = CreateMutex(NULL, FALSE, NULL)) == NULL) {
pMsg(ERR, test->args, "Failed to create semaphore, error = %u\n", GetLastError());
return(GetLastError());
}
if((test->env->mutexs.MutexSTATS = CreateMutex(NULL, FALSE, NULL)) == NULL) {
pMsg(ERR, test->args, "Failed to create semaphore, error = %u\n", GetLastError());
return(GetLastError());
}
#else
mutexs_t mutexs = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER };
test->env->mutexs = mutexs;
#endif
if(test->args->seed == 0) test->args->seed = test->args->pid;
srand(test->args->seed);
/* create bitmap to hold write/read context: each bit is an LBA */
/* the stuff before BMP_OFFSET is the data for child/thread shared context */
test->env->bmp_siz = (((((size_t)test->args->vsiz)/test->args->ltrsiz)/8) == 0) ? 1 : ((((size_t)test->args->vsiz)/test->args->ltrsiz)/8);
if (((test->args->vsiz/test->args->ltrsiz)/8) != 0) test->env->bmp_siz += 1; /* account for rounding error */
/* We use that same data buffer for static data, so alloc here. */
data_buffer_size = ((test->args->htrsiz*BLK_SIZE)*2);
if((*data_buffer_unaligned = (unsigned char *) ALLOC(data_buffer_size+ALIGNSIZE)) == NULL) {
pMsg(ERR,test->args, "Failed to allocate static data buffer memory.\n");
return(-1);
}
/* create list to hold lbas currently be written */
if((test->env->action_list = (action_t *) ALLOC(sizeof(action_t)*test->args->t_kids)) == NULL) {
pMsg(ERR,test->args, "Failed to allocate static data buffer memory.\n");
return(-1);
}
test->env->data_buffer = (unsigned char *) BUFALIGN(*data_buffer_unaligned);
if((test->env->shared_mem = (void *) ALLOC(test->env->bmp_siz+BMP_OFFSET)) == NULL) {
pMsg(ERR, test->args, "Failed to allocate bitmap memory\n");
return(-1);
}
memset(test->env->shared_mem,0,test->env->bmp_siz+BMP_OFFSET);
memset(test->env->data_buffer,0,data_buffer_size);
memset(test->env->action_list,0,sizeof(action_t)*test->args->t_kids);
test->env->action_list_entry = 0;
test->env->request_lba.rLBA = test->env->request_lba.wLBA = test->args->start_lba;
test->args->test_state = SET_STS_PASS(test->args->test_state);
test->args->test_state = SET_wFST_TIME(test->args->test_state);
test->args->test_state = SET_rFST_TIME(test->args->test_state);
test->args->test_state = DIRCT_INC(test->args->test_state);
if(test->args->flags & CLD_FLG_W) {
test->env->lastAction.oper = WRITER;
test->args->test_state = SET_OPER_W(test->args->test_state);
} else {
test->env->lastAction.oper = READER;
test->args->test_state = SET_OPER_R(test->args->test_state);
}
/* prefill the data buffer with data for compares and writes */
switch(test->args->flags & CLD_FLG_PTYPS) {
case CLD_FLG_FPTYPE :
for(i=0;i<sizeof(test->args->pattern);i++) {
if((test->args->pattern & (((OFF_T) 0xff) << (((sizeof(test->args->pattern)-1)-i)*8))) != 0) break;
}
/* special case for pattern = 0 */
if(i == sizeof(test->args->pattern)) i = 0;
fill_buffer(test->env->data_buffer, data_buffer_size, &test->args->pattern, sizeof(test->args->pattern)-i, CLD_FLG_FPTYPE);
break;
case CLD_FLG_RPTYPE :
fill_buffer(test->env->data_buffer, data_buffer_size, NULL, 0, CLD_FLG_RPTYPE);
break;
case CLD_FLG_CPTYPE :
fill_buffer(test->env->data_buffer, data_buffer_size, 0, 0, CLD_FLG_CPTYPE);
case CLD_FLG_LPTYPE :
break;
default :
pMsg(WARN, test->args, "Unknown fill pattern\n");
return(-1);
}
return(0);
}
#ifdef WINDOWS
DWORD WINAPI threadedMain(test_ll_t *test)
#else
void *threadedMain(void *vtest)
#endif
{
#ifndef WINDOWS
test_ll_t *test = (test_ll_t *) vtest;
#endif
unsigned char *data_buffer_unaligned = NULL;
unsigned long ulRV;
int i;
unsigned char *sharedMem;
extern unsigned long glb_run;
extern unsigned long glb_flags;
extern int signal_action;
test->args->pid = GETPID();
init_gbl_data(test->env);
if(make_assumptions(test->args) < 0) {
glb_flags |= GLB_FLG_FAILED;
TEXIT(GETLASTERROR());
}
if(check_conclusions(test->args) < 0) {
glb_flags |= GLB_FLG_FAILED;
TEXIT(GETLASTERROR());
}
if(test->args->flags & CLD_FLG_DUMP) {
/*
* All we are doing is dumping filespec data to STDOUT, so
* we will do this here and be done.
*/
do_dump(test->args);
TEXIT(GETLASTERROR());
} else {
ulRV = init_data(test, &data_buffer_unaligned);
if(ulRV != 0) {
glb_flags |= GLB_FLG_FAILED;
TEXIT(ulRV);
}
}
pMsg(START, test->args, "Start args: %s\n", test->args->argstr);
/*
* This loop takes care of passes
*/
do {
test->env->pass_count++;
test->env->start_time = time(NULL);
if(test->args->flags & CLD_FLG_RPTYPE) { /* force random data to be different each cycle */
fill_buffer(test->env->data_buffer, ((test->args->htrsiz*BLK_SIZE)*2), NULL, 0, CLD_FLG_RPTYPE);
}
sharedMem = test->env->shared_mem;
memset(sharedMem+BMP_OFFSET,0,test->env->bmp_siz);
if((test->args->flags & CLD_FLG_LINEAR) && !(test->args->flags & CLD_FLG_NTRLVD)) {
linear_read_write_test(test);
} else {
/* we only reset the end time if not running a linear read / write test */
test->env->end_time = test->env->start_time + test->args->run_time;
test->env->bContinue = TRUE;
test->env->request_lba.wLBA = test->args->start_lba;
test->args->test_state = DIRCT_INC(test->args->test_state);
test->args->test_state = SET_wFST_TIME(test->args->test_state);
test->args->test_state = SET_rFST_TIME(test->args->test_state);
if(test->args->flags & CLD_FLG_W) {
test->env->lastAction.oper = WRITER;
test->args->test_state = SET_OPER_W(test->args->test_state);
} else {
test->env->lastAction.oper = READER;
test->args->test_state = SET_OPER_R(test->args->test_state);
}
memset(test->env->action_list,0,sizeof(action_t)*test->args->t_kids);
test->env->action_list_entry = 0;
test->env->wcount = 0;
test->env->rcount = 0;
test->env->gr_start_time = 0;
test->env->gw_start_time = 0;
test->env->gr_stop_time = 0;
test->env->gw_stop_time = 0;
if(test->args->flags & CLD_FLG_CYC)
if(test->args->cycles == 0) {
pMsg(INFO,test->args, "Starting pass %lu\n", (unsigned long) test->env->pass_count);
} else {
pMsg(INFO,test->args, "Starting pass %lu of %lu\n", (unsigned long) test->env->pass_count, test->args->cycles);
}
else {
pMsg(INFO,test->args, "Starting pass\n");
}
CreateTestChild(ChildTimer, test);
for(i=0;i<test->args->t_kids;i++) {
CreateTestChild(ChildMain, test);
}
/* Wait for the children to finish */
cleanUpTestChildren(test);
}
update_cyc_stats(test->args, test->env);
if((test->args->flags & CLD_FLG_CYC) && (test->args->flags & CLD_FLG_PCYC)) {
print_stats(test->args, test->env, CYCLE);
}
update_gbl_stats(test->env);
if(signal_action & SIGNAL_STOP) { break; } /* user request to stop */
if((glb_run == 0)) { break; } /* global request to stop */
if(!(test->args->flags & CLD_FLG_CYC)) {
break; /* leave, unless cycle testing */
} else {
if((test->args->cycles > 0) && (test->env->pass_count >= test->args->cycles)) {
break; /* leave, cycle testing complete */
}
}
} while(TST_STS(test->args->test_state));
print_stats(test->args, test->env, TOTAL);
FREE(data_buffer_unaligned);
FREE(test->env->shared_mem);
#ifdef WINDOWS
CloseHandle(OpenMutex(SYNCHRONIZE, TRUE, "gbl"));
CloseHandle(test->env->mutexs.MutexACTION);
CloseHandle(test->env->mutexs.MutexIO);
CloseHandle(test->env->mutexs.MutexSTATS);
#endif
if(TST_STS(test->args->test_state)) {
if (signal_action & SIGNAL_STOP) {
pMsg(END, test->args, "User Interrupt: Test Done (Passed)\n");
} else {
pMsg(END, test->args, "Test Done (Passed)\n");
}
} else {
if (signal_action & SIGNAL_STOP) {
pMsg(END, test->args, "User Interrupt: Test Done (Failed)\n");
} else {
pMsg(END, test->args, "Test Done (Failed)\n");
}
}
TEXIT(GETLASTERROR());
}
/*
* Creates a new test structure and adds it to the list of
* test structures already available. Allocate all memory
* needed by the new test.
*
* Returns the newly created test structure
*/
test_ll_t *getNewTest(test_ll_t *testList) {
test_ll_t *pNewTest;
if((pNewTest = (test_ll_t *)ALLOC(sizeof(test_ll_t))) == NULL) {
pMsg(ERR, &cleanArgs, "%d : Could not allocate memory for new test.\n", GETLASTERROR());
return NULL;
}
memset(pNewTest, 0, sizeof(test_ll_t));
if((pNewTest->args = (child_args_t *)ALLOC(sizeof(child_args_t))) == NULL) {
pMsg(ERR, &cleanArgs, "%d : Could not allocate memory for new test.\n", GETLASTERROR());
FREE(pNewTest);
return NULL;
}
if((pNewTest->env = (test_env_t *)ALLOC(sizeof(test_env_t))) == NULL) {
pMsg(ERR, &cleanArgs, "%d : Could not allocate memory for new test.\n", GETLASTERROR());
FREE(pNewTest->args);
FREE(pNewTest);
return NULL;
}
memcpy(pNewTest->args, &cleanArgs, sizeof(child_args_t));
memcpy(pNewTest->env, &cleanEnv, sizeof(test_env_t));
pNewTest->next = testList;
testList = pNewTest;
return pNewTest;
}
test_ll_t *run() {
test_ll_t *newTest = NULL, *lastTest = NULL;
if(cleanArgs.flags & CLD_FLG_FSLIST) {
char *filespec = cleanArgs.device;
char *aFilespec = NULL;
FILE *file = NULL;
struct stat stat_buf;
int rv;
rv = stat(filespec, &stat_buf);
if(0 == rv) {
if(!IS_FILE(stat_buf.st_mode)) {
pMsg(ERR, &cleanArgs, "Can't use non-regular file with file list option, -F.\n");
return newTest;
}
} else {
pMsg(ERR, &cleanArgs, "Can't stat file list file.\n");
return newTest;
}
if((aFilespec = (char *)ALLOC(80)) == NULL) {
pMsg(ERR, &cleanArgs, "Could not allocate memory to read file");
return newTest;
}
file = fopen(filespec, "r");
if(file == NULL) {
pMsg(
ERR,
&cleanArgs,
"%s is not a regular file, could not be opened for reading, or was not found.",
filespec);
return newTest;
}
while(!feof(file)) {
memset(aFilespec, 0, 80);
fscanf(file, "%79s", aFilespec);
if(aFilespec[0] != 0) { /* if we read something useful */
lastTest = newTest;
newTest = getNewTest(lastTest);
if(newTest != lastTest) {
memset(newTest->args->device, 0, DEV_NAME_LEN);
strncpy(newTest->args->device, aFilespec, strlen(aFilespec));
createChild(threadedMain, newTest);
} else {
newTest = lastTest;
break;
}
}
}
fclose(file);
FREE(aFilespec);
} else {
newTest = getNewTest(newTest);
if(newTest != NULL) {
createChild(threadedMain, newTest);
}
}
return newTest;
}
int main(int argc, char **argv)
{
extern time_t global_start_time;
extern unsigned long glb_flags; /* global flags GLB_FLG_xxx */
int i;
#ifdef WINDOWS
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if(err != 0) {
pMsg(WARN, &cleanArgs, "Windows setup of Winsock failed, can't retrieve host name, continuing");
}
#endif
setup_sig_mask();
memset(hostname, 0, HOSTNAME_SIZE);
gethostname(hostname, HOSTNAME_SIZE);
setbuf(stdout, (char *)NULL);
glb_flags = 0;
global_start_time = time(NULL);
strncpy(cleanArgs.device, "No filespec", strlen("No filespec"));
cleanArgs.stop_lba = -1;
cleanArgs.stop_blk = -1;
cleanArgs.ioTimeout = DEFAULT_IO_TIMEOUT;
cleanArgs.flags |= CLD_FLG_ALLDIE;
cleanArgs.flags |= CLD_FLG_ERR_REREAD;
cleanArgs.flags |= CLD_FLG_LBA_SYNC;
for(i=1;i<argc-1;i++) {
strncat(cleanArgs.argstr, argv[i], (MAX_ARG_LEN-1)-strlen(cleanArgs.argstr));
strncat(cleanArgs.argstr, " ", (MAX_ARG_LEN-1)-strlen(cleanArgs.argstr));
}
if(fill_cld_args(argc, argv, &cleanArgs) < 0) return(-1);
cleanUp(run());
#ifdef WINDOWS
WSACleanup();
#endif
if(glb_flags & GLB_FLG_FAILED) {
return -1;
} else {
return 0;
}
}