-
Notifications
You must be signed in to change notification settings - Fork 12
/
tiotest.c
1529 lines (1230 loc) · 39.7 KB
/
tiotest.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Threaded io test
*
* Copyright (C) 1999-2008 Mika Kuoppala <miku at iki.fi>
*
* 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, 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.
*
*
*/
#include "constants.h"
#include "crc32.h"
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#define WRITE_TEST 0
#define RANDOM_WRITE_TEST 1
#define READ_TEST 2
#define RANDOM_READ_TEST 3
#define TEST_COUNT 4
#define CACHE_CONTROL_FILE "/proc/sys/vm/drop_caches"
#define CACHE_DROP_ALL_FLAG "3";
struct tt_rusage {
struct timeval startRealTime;
struct timeval startUserTime;
struct timeval startSysTime;
struct timeval stopRealTime;
struct timeval stopUserTime;
struct timeval stopSysTime;
};
typedef struct {
double avg, max;
unsigned long count, count1, count2;
} Latencies;
typedef struct {
pthread_t thread;
pthread_attr_t thread_attr;
char fileName[KBYTE];
TIO_off_t fileSizeInMBytes;
TIO_off_t fileOffset; // used in the "raw drives" case, offset into device, 0 otherwise
unsigned long numRandomOps;
unsigned long blockSize;
unsigned char* buffer;
unsigned bufferCrc;
unsigned long myNumber;
unsigned long blocksWritten;
struct tt_rusage writeTimings;
Latencies writeLatency;
unsigned long blocksRandomWritten;
struct tt_rusage randomWriteTimings;
Latencies randomWriteLatency;
unsigned long blocksRead;
struct tt_rusage readTimings;
Latencies readLatency;
unsigned long blocksRandomRead;
struct tt_rusage randomReadTimings;
Latencies randomReadLatency;
} ThreadData;
typedef void (*TestFunc)(ThreadData *);
typedef struct {
ThreadData* threads;
int numThreads;
struct tt_rusage totalTimeWrite;
struct tt_rusage totalTimeRandomWrite;
struct tt_rusage totalTimeRead;
struct tt_rusage totalTimeRandomRead;
} ThreadTest;
typedef struct {
char path[MAX_PATHS][KBYTE];
int pathsCount;
int fileSizeInMBytes;
int numThreads;
int blockSize;
int numRandomOps;
int verbose;
int terse;
int use_mmap;
int sequentialWriting;
int syncWriting;
int rawDrives;
int consistencyCheckData;
int showLatency;
long threadOffset;
int useThreadOffsetForFirstThread;
int testsToRun[TEST_COUNT];
int runRandomWrite;
int runRead;
int runRandomRead;
int flushCaches;
int openDirect;
/*
Debug level
This should be from 0 - 10
*/
int debugLevel;
} ArgumentOptions;
typedef struct
{
volatile int *child_status;
TestFunc fn;
ThreadData *d;
volatile int *pstart;
} StartData;
typedef int (*file_io_function) (int fd, TIO_off_t offset, ThreadData *d);
typedef int (*mmap_io_function) (void *loc, ThreadData *d);
typedef TIO_off_t (*file_offset_function) (TIO_off_t current_offset, ThreadData *d, unsigned int *seed);
typedef void * (*mmap_loc_function) (void *base_loc, void *current_loc, ThreadData *d, unsigned int *seed);
// operation functions
static int do_pwrite_operation(int fd, TIO_off_t offset, ThreadData *d);
static int do_pread_operation(int fd, TIO_off_t offset, ThreadData *d);
static int do_mmap_read_operation(void *loc, ThreadData *d);
static int do_mmap_write_operation(void *loc, ThreadData *d);
// offset functions
static TIO_off_t get_sequential_offset(TIO_off_t current_offset, ThreadData *d, unsigned int *seed);
static TIO_off_t get_random_offset(TIO_off_t current_offset, ThreadData *d, unsigned int *seed);
static void *get_sequential_loc(void *base_loc, void *current_loc, ThreadData *d, unsigned int *seed);
static void *get_random_loc(void *base_loc, void *current_loc, ThreadData *d, unsigned int *seed);
static const char* const versionStr = "tiotest v0.4.2 (C) 1999-2008 tiobench team <http://tiobench.sf.net/>";
static ArgumentOptions args;
static void t_log (int level, char *message)
{
if(args.debugLevel >= level)
fprintf(stderr, "%s\n", message);
}
static unsigned int get_random_seed()
{
unsigned int seed;
struct timeval r;
if(gettimeofday( &r, NULL ) == 0) {
seed = r.tv_usec;
} else {
seed = 0xDEADBEEF;
}
return seed;
}
static TIO_off_t get_random_number(const TIO_off_t max, unsigned int *seed)
{
unsigned long rr = rand_r(seed);
// if it doesn't give us enough random bits, add some more
if(RAND_MAX < max)
{
rr |= (rand_r(seed) << 16);
}
return (TIO_off_t) (rr % max);
}
static void timer_init(struct tt_rusage *t)
{
memset( t, 0, sizeof(struct tt_rusage) );
}
static void timer_start(struct tt_rusage *t)
{
struct rusage ru;
if(gettimeofday( &(t->startRealTime), NULL ))
{
perror("Error in timer_start from gettimeofday()\n");
exit(10);
}
if(getrusage( RUSAGE_SELF, &ru ))
{
perror("Error in timer_start from getrusage()\n");
exit(11);
}
memcpy( &(t->startUserTime), &(ru.ru_utime), sizeof( struct timeval ));
memcpy( &(t->startSysTime), &(ru.ru_stime), sizeof( struct timeval ));
}
static void timer_stop(struct tt_rusage *t)
{
struct rusage ru;
if( getrusage( RUSAGE_SELF, &ru ))
{
perror("Error in timer_stop from getrusage()\n");
exit(11);
}
if(gettimeofday( &(t->stopRealTime), NULL ))
{
perror("Error in timer_stop from gettimeofday()\n");
exit(10);
}
memcpy( &(t->stopUserTime), &(ru.ru_utime), sizeof( struct timeval ));
memcpy( &(t->stopSysTime), &(ru.ru_stime), sizeof( struct timeval ));
}
static unsigned long long tv_to_usec(const struct timeval* v)
{
return v->tv_sec * (1000*1000) + v->tv_usec;
}
static void update_latency_info(Latencies *lat, struct timeval tv_start,
struct timeval tv_stop)
{
double value;
value = tv_stop.tv_sec - tv_start.tv_sec;
value += (tv_stop.tv_usec - tv_start.tv_usec)/1000000.0;
if (value > lat->max)
lat->max = value;
lat->avg += value;
lat->count++;
if (value > (double)LATENCY_STAT1)
lat->count1++;
if (value > (double)LATENCY_STAT2)
lat->count2++;
return;
}
static void * tt_aligned_alloc(const ssize_t size)
{
caddr_t a;
a = TIO_mmap((caddr_t )0, size,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, (TIO_off_t)0);
if (a == MAP_FAILED) {
perror("Error " xstr(TIO_mmap) "()ing anonymous memory chunk");
exit(-1);
}
return a;
}
static void tt_aligned_free(caddr_t a, const ssize_t size)
{
munmap(a, size);
}
static void checkValidFileSize(const int value)
{
#ifndef USE_LARGEFILES
if (value > MAXINT / (1024*1024))
{
fprintf(stderr, "Specified file size too large, please specify something under 2GB\n");
exit(1);
}
#endif
}
static void checkIntZero(const int value, const char* const mess)
{
if (value <= 0)
{
fprintf(stderr, "%s",mess);
fprintf(stderr, "Try 'tiotest -h' for more information.\n");
exit(1);
}
}
static void checkLong(const long value, const char* const mess)
{
if (value < 0)
{
fprintf(stderr,"%s", mess);
fprintf(stderr, "Try 'tiotest -h' for more information\n");
exit(1);
}
}
static void print_option(const char* s,
const char* desc,
const char* def)
{
printf(" %s %s", s, desc);
if(def)
printf(" (default: %s)", def);
printf("\n");
}
static char *my_int_to_string(int a)
{
static char tempBuffer[128];
sprintf(tempBuffer, "%d", a);
return tempBuffer;
}
static void print_help_and_exit()
{
printf("%s\n", versionStr);
printf("Usage: tiotest [options]\n");
print_option("-f", "Filesize per thread in MBytes",
my_int_to_string(DEFAULT_FILESIZE));
print_option("-b", "Blocksize to use in bytes",
my_int_to_string(DEFAULT_BLOCKSIZE));
print_option("-d", "Directory for test files",
DEFAULT_DIRECTORY);
print_option("-t", "Number of concurrent test threads",
my_int_to_string(DEFAULT_THREADS));
print_option("-r", "Random I/O operations per thread",
my_int_to_string(DEFAULT_RANDOM_OPS));
print_option("-o", "Offset in Mb on disk between threads. Use with -R option",
0);
print_option("-k", "Skip test number n. Could be used several times.", 0);
print_option("-L", "Hide latency output", 0);
print_option("-R", "Use raw devices. Set device name with -d option", 0);
print_option("-T", "More terse output", 0);
print_option("-M", "Use mmap for I/O", 0);
print_option("-W", "Do writing phase sequentially", 0);
print_option("-S", "Do writing synchronously", 0);
print_option("-O", "Use offset from -o option for first thread. Use with -R option",
0);
print_option("-c",
"Consistency check data (will slow io and raise cpu%)",
0);
print_option("-D", "Debug level",
my_int_to_string(DEFAULT_DEBUG_LEVEL));
print_option("-F", "Flush OS caches before running test (requires root)", 0);
print_option("-X", "Use direct I/O to bypass buffer cache (blocksize must be a multiple of logical blocksize of underlying filesystem)", 0);
print_option("-h", "Print this help and exit", 0);
exit(1);
}
static void parse_args( ArgumentOptions* args, int argc, char *argv[] )
{
int c;
int once = 0;
while (1)
{
c = getopt( argc, argv, "f:b:d:t:r:D:k:o:hLRTWSOcMFX");
if (c == -1)
break;
switch (c)
{
case 'f':
args->fileSizeInMBytes = atoi(optarg);
checkIntZero(args->fileSizeInMBytes, "Wrong file size\n");
checkValidFileSize(args->fileSizeInMBytes);
break;
case 'b':
args->blockSize = atoi(optarg);
checkIntZero(args->blockSize, "Wrong block size\n");
break;
case 'd':
if (args->pathsCount < MAX_PATHS)
{
if (!once)
{
args->pathsCount = 0;
once = 1;
}
strcpy(args->path[args->pathsCount++], optarg);
}
break;
case 't':
args->numThreads = atoi(optarg);
checkIntZero(args->numThreads, "Wrong number of threads\n");
break;
case 'r':
args->numRandomOps = atoi(optarg);
checkIntZero(args->numRandomOps, "Wrong number of random I/O operations\n");
break;
case 'L':
args->showLatency = FALSE;
break;
case 'T':
args->terse = TRUE;
break;
case 'M':
args->use_mmap = TRUE;
break;
case 'W':
args->sequentialWriting = TRUE;
break;
case 'S':
args->syncWriting = TRUE;
break;
case 'R':
args->rawDrives = TRUE;
break;
case 'c':
args->consistencyCheckData = TRUE;
break;
case 'h':
print_help_and_exit();
break;
case 'D':
args->debugLevel = atoi(optarg);
break;
case 'o':
args->threadOffset = atol(optarg);
checkLong(args->threadOffset, "Wrong offset between threads\n");
break;
case 'O':
args->useThreadOffsetForFirstThread = TRUE;
break;
case 'F':
args->flushCaches = TRUE;
break;
case 'X':
args->openDirect = TRUE;
break;
case 'k':
{
const int i = atoi(optarg);
if (i < TEST_COUNT)
{
args->testsToRun[i] = 0;
break;
}
else
fprintf(stderr, "Wrong test number %d\n", i);
/* Go through */
}
case '?':
default:
fprintf(stderr, "Try 'tiotest -h' for more information\n");
exit(1);
break;
}
}
}
static int flush_caches()
{
int retVal = 0;
int fd;
if (geteuid() != 0)
{
fprintf(stderr, "Cache flushing requires root.\n");
}
else
{
fd = open(CACHE_CONTROL_FILE, O_WRONLY);
if (fd == -1)
{
fprintf(stderr, "%s: %s\n", strerror(errno),
CACHE_CONTROL_FILE);
}
else
{
char drop_all_message[] = CACHE_DROP_ALL_FLAG;
int length = strlen(drop_all_message);
int written = write(fd, drop_all_message,
length);
if (written == -1)
{
fprintf(stderr, "%s: %s\n", strerror(errno),
drop_all_message);
}
else if (written != length)
{
fprintf(stderr,
"Error: only wrote %d of %d bytes.\n",
written, length);
}
else
{
// successful write.
retVal = 1;
}
close(fd);
}
}
return retVal;
}
static void* do_generic_test(file_io_function io_func,
mmap_io_function mmap_func,
file_offset_function offset_func,
mmap_loc_function loc_func,
ThreadData *d, struct tt_rusage *timings,
Latencies *latencies,
int madvise_advice,
unsigned long *blockCount,
unsigned long io_ops)
{
int fd;
TIO_off_t blocks=((TIO_off_t)d->fileSizeInMBytes*MBYTE)/d->blockSize;
unsigned int seed = get_random_seed();
unsigned long orig_iops = io_ops;
int rc;
TIO_off_t bytesize=blocks*d->blockSize; /* truncates down to BS multiple */
// for now, always read/write, just easier
int openFlags = O_RDWR;
// if not a pre-existing device, add create flag
if (!args.rawDrives)
openFlags |= O_CREAT;
// if sync I/O requested, do it at open time
if( args.syncWriting )
openFlags |= O_SYNC;
// if direct I/O requested, do it at open time
if( args.openDirect )
openFlags |= O_DIRECT;
#ifdef USE_LARGEFILES
openFlags |= O_LARGEFILE;
#endif
fd = open(d->fileName, openFlags, 0600 );
if(fd == -1) {
fprintf(stderr, "%s: %s\n", strerror(errno), d->fileName);
return 0;
}
/* if doing real files, get them pre-allocated in size */
if (!args.rawDrives) {
t_log(LEVEL_DEBUG, "calling " xstr(TIO_ftruncate) "() on file descriptor");
rc = TIO_ftruncate(fd, bytesize); /* pre-allocate space */
if(rc != 0) {
perror(xstr(TIO_ftruncate) "() failed");
close(fd);
return 0;
}
}
if (args.flushCaches)
{
if (!flush_caches())
{
close(fd);
return 0;
}
}
timer_start( timings );
if(args.use_mmap)
{
/**
* MEMORY-MAPPED OPERATIONS
*/
unsigned long chunk_num;
// rounds the number of mmap chunks up, basically ceiling function
TIO_off_t num_mmap_chunks = bytesize/MMAP_CHUNK_SIZE + 1;
for(chunk_num=0; chunk_num < num_mmap_chunks; chunk_num++)
{
void *file_loc = NULL;
long this_chunk_offset = d->fileOffset + chunk_num*MMAP_CHUNK_SIZE;
long this_chunk_size = MIN(MMAP_CHUNK_SIZE, (TIO_off_t)bytesize - chunk_num*MMAP_CHUNK_SIZE);
long this_chunk_blocks = this_chunk_size / d->blockSize;
void *current_loc = NULL;
file_loc=TIO_mmap(NULL,this_chunk_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,
this_chunk_offset);
if(file_loc == MAP_FAILED) {
fprintf(stderr, "this_chunk_size=%ld, fd=%d, offset=" OFFSET_FORMAT
"\n", this_chunk_size, fd, d->fileOffset);
perror("Error " xstr(TIO_mmap) "()ing data file");
close(fd);
return 0;
}
madvise(file_loc, this_chunk_size, madvise_advice);
current_loc = file_loc - d->blockSize; // back-one hack for sequential case
while(io_ops--) {
int ret;
struct timeval tv_start, tv_stop;
current_loc = (*loc_func)(file_loc, current_loc, d, &(seed));
gettimeofday(&tv_start, NULL);
ret = mmap_func(current_loc, d);
if(ret != 0)
exit(ret);
if( args.syncWriting ) msync(current_loc, d->blockSize, MS_SYNC);
gettimeofday(&tv_stop, NULL);
update_latency_info(latencies, tv_start, tv_stop);
}
(*blockCount) += orig_iops; // take this out of the for loop, we don't handle errors that well
munmap(file_loc, this_chunk_size);
}
} else {
/**
* REGULAR I/O OPERATIONS
*/
//TIO_off_t current_offset = d->fileOffset;
TIO_off_t current_offset = d->fileOffset - d->blockSize; // back-one hack for sequential case
while(io_ops--)
{
struct timeval tv_start, tv_stop;
int ret;
current_offset = (*offset_func)(current_offset, d, &(seed));
gettimeofday(&tv_start, NULL);
ret = (*io_func)(fd, current_offset, d);
if(ret != 0)
exit(ret);
gettimeofday(&tv_stop, NULL);
update_latency_info(latencies, tv_start, tv_stop);
}
(*blockCount) += orig_iops; // take this out of the for loop, we don't handle errors that well
}
fsync(fd);
close(fd);
timer_stop( timings );
return 0;
}
static unsigned long get_number_of_blocks(ThreadData *d)
{
return (d->fileSizeInMBytes * MB) / d->blockSize;
}
static void do_read_test( ThreadData *d )
{
t_log(LEVEL_INFO, "Doing sequential read test");
do_generic_test(do_pread_operation, do_mmap_read_operation,
get_sequential_offset, get_sequential_loc,
d, &(d->readTimings), &(d->readLatency),
MADV_SEQUENTIAL, &(d->blocksRead), get_number_of_blocks(d));
}
static void do_write_test( ThreadData *d )
{
t_log(LEVEL_INFO, "Doing sequential write test");
do_generic_test(do_pwrite_operation, do_mmap_write_operation,
get_sequential_offset, get_sequential_loc,
d, &(d->writeTimings), &(d->writeLatency),
MADV_SEQUENTIAL, &(d->blocksWritten), get_number_of_blocks(d));
}
static void do_random_read_test( ThreadData *d )
{
t_log(LEVEL_INFO, "Doing random read test");
do_generic_test(do_pread_operation, do_mmap_read_operation,
get_random_offset, get_random_loc,
d, &(d->randomReadTimings), &(d->randomReadLatency),
MADV_RANDOM, &(d->blocksRandomRead), d->numRandomOps);
}
static void do_random_write_test( ThreadData *d )
{
t_log(LEVEL_INFO, "Doing random write test");
do_generic_test(do_pwrite_operation, do_mmap_write_operation,
get_random_offset, get_random_loc,
d, &(d->randomWriteTimings), &(d->randomWriteLatency),
MADV_RANDOM, &(d->blocksRandomWritten), d->numRandomOps);
}
static const TestFunc Tests[] = {
do_write_test,
do_random_write_test,
do_read_test,
do_random_read_test,
};
static void initialize_test( ThreadTest *d )
{
int i;
int pathLoadBalIdx = 0;
TIO_off_t offs, cur_offs[KBYTE] = {0};
assert(TEST_COUNT == (sizeof(Tests)/sizeof(TestFunc)));
memset( d, 0, sizeof(ThreadTest) );
d->numThreads = args.numThreads;
d->threads = calloc( d->numThreads, sizeof(ThreadData) );
if( d->threads == NULL )
{
perror("Error calloc()ing thread data memory");
exit(-1);
}
/* Initializing thread data */
if (args.rawDrives)
{
if (args.threadOffset != 0)
{
offs = (args.threadOffset + args.fileSizeInMBytes) * MBYTE;
if (args.useThreadOffsetForFirstThread)
{
int k;
for(k = 0; k < KBYTE; k++)
cur_offs[k] = (TIO_off_t)args.threadOffset * MBYTE;
}
}
else
offs = (TIO_off_t)args.fileSizeInMBytes * MBYTE;
}
else
offs = 0;
for(i = 0; i < d->numThreads; i++)
{
d->threads[i].myNumber = i;
d->threads[i].blockSize = args.blockSize;
d->threads[i].numRandomOps = args.numRandomOps;
d->threads[i].fileSizeInMBytes = args.fileSizeInMBytes;
if (args.rawDrives)
{
d->threads[i].fileOffset = cur_offs[pathLoadBalIdx];
cur_offs[pathLoadBalIdx] += offs;
sprintf(d->threads[i].fileName, "%s",
args.path[pathLoadBalIdx++]);
}
else
{
d->threads[i].fileOffset = 0;
sprintf(d->threads[i].fileName, "%s/_tiotest_pid%d.thr%d",
args.path[pathLoadBalIdx++], (int) getpid(), i);
}
if( pathLoadBalIdx >= args.pathsCount )
pathLoadBalIdx = 0;
pthread_attr_init( &(d->threads[i].thread_attr) );
pthread_attr_setscope(&(d->threads[i].thread_attr),
PTHREAD_SCOPE_SYSTEM);
d->threads[i].buffer = tt_aligned_alloc( d->threads[i].blockSize );
if( args.consistencyCheckData )
{
int j;
const unsigned long bsize = d->threads[i].blockSize;
unsigned char *b = d->threads[i].buffer;
for(j = 0; j < bsize; j++)
{
b[j] = rand() & 0xFF;
}
d->threads[i].bufferCrc = crc32(b, bsize, 0);
}
}
}
static void cleanup_test( ThreadTest *d )
{
int i;
for(i = 0; i < d->numThreads; i++)
{
if (!args.rawDrives)
unlink(d->threads[i].fileName);
tt_aligned_free( (char *)d->threads[i].buffer, d->threads[i].blockSize );
d->threads[i].buffer = 0;
pthread_attr_destroy( &(d->threads[i].thread_attr) );
}
free(d->threads);
d->threads = 0;
}
static void wait_for_threads( ThreadTest *d )
{
int i;
for(i = 0; i < d->numThreads; i++)
pthread_join(d->threads[i].thread, NULL);
}
static void* start_proc( void *data )
{
StartData *sd = (StartData*)data;
*sd->child_status = getpid();
if (sd->pstart != NULL)
while (*sd->pstart == 0) sleep(0);
sd->fn(sd->d);
return NULL;
}
static void do_test( ThreadTest *test, int testCase, int sequential,
struct tt_rusage *t, char *debugMessage )
{
int i;
volatile int *child_status;
StartData *sd;
int synccount;
volatile int start = 0;
assert(testCase < TEST_COUNT);
child_status = (volatile int *)calloc(test->numThreads, sizeof(int));
if (child_status == NULL)
{
perror("Error calloc()ing thread status memory");
return;
}
sd = (StartData*)calloc(test->numThreads, sizeof(StartData));
if (sd == NULL)
{
perror("Error calloc()ing thread start data memory");
free((int*)child_status);
return;
}
if (sequential)
timer_start(t);
for(i = 0; i < test->numThreads; i++)
{
sd[i].child_status = &child_status[i];
sd[i].fn = Tests[testCase];
sd[i].d = &test->threads[i];
if (sequential)
sd[i].pstart = NULL;
else
sd[i].pstart = &start;
if( pthread_create(
&(test->threads[i].thread),
&(test->threads[i].thread_attr),
start_proc,
(void *)&sd[i]))
{
perror("Error from pthread_create()");
free((int*)child_status);
free(sd);
exit(-1);
}
if(sequential)
{
t_log(LEVEL_INFO,"Waiting previous thread to finish before starting a new one");
pthread_join(test->threads[i].thread, NULL);
}
}
if(sequential)
timer_stop(t);
else
{
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
do
{
synccount = 0;
for(i = 0; i < test->numThreads; i++)
if (child_status[i])
synccount++;
if (synccount == test->numThreads)
break;
sleep(1);
gettimeofday(&tv2, NULL);
} while ((tv2.tv_sec - tv1.tv_sec) < 30);
if (synccount != test->numThreads)
{
fprintf(stderr, "Unable to start %d threads (started %d)\n",
test->numThreads, synccount);
start = 1;
wait_for_threads(test);
free((int*)child_status);
free(sd);
return;
}
t_log(LEVEL_INFO, "Created threads");
timer_start(t);
start = 1;
t_log(LEVEL_INFO, "Waiting threads");
wait_for_threads(test);
timer_stop(t);
}
free((int*)child_status);
free(sd);
t_log(LEVEL_INFO, "Done!");
}
static void do_tests( ThreadTest *thisTest )
{
struct tt_rusage *timeWrite = &(thisTest->totalTimeWrite);
struct tt_rusage *timeRandomWrite = &(thisTest->totalTimeRandomWrite);
struct tt_rusage *timeRead = &(thisTest->totalTimeRead);
struct tt_rusage *timeRandomRead = &(thisTest->totalTimeRandomRead);
timer_init( timeWrite );
timer_init( timeRandomWrite );
timer_init( timeRead );
timer_init( timeRandomRead );