-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdthammer.c
executable file
·3539 lines (3214 loc) · 109 KB
/
dthammer.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
/*
* Copyright 2021 NetApp
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*
* dthammer.c - I/O Behavior for hammer tool.
*
* Author: Robin T. Miller
* Date Created: April 30th, 2014
*
* Modification History:
*
* February 4th, 2023 by Robin T. Miller, Chris Nelson, & John Hollowell
* Fix segmentation fault when overwriting a file encounters a file
* system full condition, due to writefile() freeing the file structure.
*
* November 9, 2021 by Chris Nelson (nelc@netapp.com)
* Add MIT license, in order to distribute to FOSS community so it can
* be used and maintained by a larger audience, particularly for the
* public dt at https://github.com/RobinTMiller/dt
*
* March 30th, 2016 by Robin T. Miller
* Add hammer mapping function. This gets called when the program name is
* "hammer" to map hammer options to dts' hammer I/O behavior options.
*
* March 27th, 2016 by Robin T. Miller
* Adding support for maxdata option to avoid file system full conditions.
* Also adding support for maxfiles option to provide this flexibility.
*
* December 10th, 2015 by Robin T. Miller
* Adding help examples.
*
* July 31st, 2015 by Robin T. Miller
* Fix logic issue when bufmodes=unbuffered and -maxfsize is specifed.
* Whence DIO is enabled, a later sanity check improperly set maxfsize!
*
* April 20th, 2015 by Robin T. Miller
* Add handle to inode_lookup() to avoid extra open request.
* On Windows, do not inode_add() for streams file of dup ID.
*
* March 31st, 2014 by Robin T. Miller
* Added parsing for runtime= and threads= options, since sio
* options can (optionally) start with '-', while dt option do not!
* This can (and did) break NACL automation, which included the '-',
* so adding this parsing to match the documentation and help text.
*
* March 17th, 2015 by Robin T. Miller
* Add a deleting files flag, so I/O monitoring does NOT cancel our
* thread, if this cleanup takes longer than the term wait time.
*
* June 2nd, 2014 by Robin T. Miller
* Add support for Direct I/O, thus page aligned buffers and rounding
* I/O sized to the device size (usually 512, but dsize=value sets otherwise).
*
* May 30th, 2014 by Robin T. Miller
* Make changes to free the full file path, and recreate it when that
* file entry is used. With many files, this will help conserve memory!
*
* May 28th, 2014 by Robin T. Miller
* Make changes so I/O monitoring will report the correct file name.
* Basically setup dt's di_dname field with the file name being operated on.
* Note: The full file path is setup, since multiple hammer threads use the
* same file names.
*
* May 23rd, 2014 by Robin T. Miller
* Removed change directory API's, since this is NFS for multiple threads.
* Sadly, these API's set the global current directory for the process, and no
* API exists to allow this per thread (too bad). This required adding full
* paths for files, but thankfully the impact is minimized by the change below
* to optimize memory. But behold, we can now run multiple hammer threads! ;)
* Note: If memory does pose an issue, we can dynamically create full paths.
*
* May 22nd, 2014 by Robin T. Miller
* Update allocfile() to dynamically allocate memory for the file path,
* rather than use hardcoded 64 byte path array (saves memory), and ensure
* this memory is freed in freefile().
*
* May 21st, 2014 by Robin T. Miller
* When creating the directory, clearly indicate disk full conditions.
* When removing the base streams file, honor the "hasdir" flag, since
* this will be false on disk full errors.
* Added -onlydelete option to *only* delete files during cleanup.
* Previously file reads and locks were performed when cleaning up files.
* Note: The original hammer behavior persists without -onlydelete option.
*/
#include "dt.h"
#include <assert.h>
#define COPYRIGHT "Copyright (c) 2012 Network Appliance, Inc. All rights reserved."
#ifndef VERSION
#define VERSION "$Id: hammer.c#11 $"
#endif
/*
* Definitions:
*/
#define HAMMER_DEFAULT_THREAD_COUNT 1
#define HAMMER_DEFAULT_RUNTIME -1
#define HAMMER_MAX_TERM_TIME (60 * 5)
#define DEFAULT_BLOCK_SIZE_MIN BLOCK_SIZE
#define DEFAULT_BLOCK_SIZE_MAX (128 * KBYTE_SIZE)
#define DEFAULT_FILE_SIZE_MIN 1
#define DEFAULT_FILE_SIZE_MAX (5 * MBYTE_SIZE)
#define DEFAULT_MODE mixed
#define DEFAULT_RANDOM_BSIZE True
#define DEFAULT_HALT_ON_ALL_ERRORS True
#define DEFAULT_HALT_ON_FILE_ERRORS True
#define DEFAULT_HALT_ON_LOCK_ERRORS True
#define DEFAULT_HALT_ON_CORRUPTIONS True
#define DEFAULT_KEEP_DISK_FULL False
#define DEFAULT_NOFLUSH False
#define DEFAULT_DELETE_ONLY False
#define DEFAULT_FLUSH_ONLY False
#define DEFAULT_ITERATIONS 0
#define DEFAULT_NOCLEANUP False
#define DEFAULT_RETRY_DISC True
#if defined(WIN32)
# define DEFAULT_NOSTREAMS False
#else /* !defined(WIN32) */
# define DEFAULT_NOSTREAMS True
#endif /* defined(WIN32) */
#define DEFAULT_LOCK_DEBUG True
#define DEFAULT_LOCK_TEST False
#define DEFAULT_UNLOCK_CHANCE 100
/*
* Internal Hammer Errors:
*/
#define HAMMER_SUCCESS SUCCESS
#define HAMMER_FAILURE 1
#define HAMMER_DISK_FULL -1
#define HAMMER_NO_STREAMS -2
#define LOCK_FULL_RANGE 0
#define LOCK_PARTIAL_RANGE 1
#define HAMMER_LOGFILE "hamlog.txt"
/* lock flags */
#define FLAG_LOCK_READ 'r'
#define FLAG_LOCK_WRITE 'w'
#define FLAG_UNLOCK 'u'
#ifndef O_BINARY
# define O_BINARY 0
#endif
/*
* Note: This format *only* displays the low 8 hex digits (with leading zeros),
* of the 64-bit value, which is Ok for the way hammer uses it!
*/
#define LL0XFMT "0x"LLHXFMT
#define KILOBYTE ((int64_t)1024)
#define MEGABYTE ((int64_t)1024 * KILOBYTE)
#define GIGABYTE ((int64_t)1024 * MEGABYTE)
#define TERABYTE ((int64_t)1024 * GIGABYTE)
#define freemem(mptr) if (mptr) { free(mptr); mptr = NULL; }
#define freestr(strp) if (strp) { memset(strp, 0xdd, strlen(strp)); free(strp); strp = NULL; }
#define STREQ(s1, s2) (strcmp((s1), (s2)) == 0)
#define start_timer(dip) (void)timer(dip, True)
#define stop_timer(dip) timer(dip, False)
#define MINFSIZE 1
#define MINBSIZE 1
#define MAXFSIZE INT64_MAX
#define RNDFSIZE(dip,hmrp) rnd64(dip, hmrp->minfsize, hmrp->maxfsize)
#define MAXBLOCKSIZE (64 * MBYTE_SIZE)
#define RNDBSIZE(dip,hmrp) (int)rnd(dip, hmrp->minbsize, hmrp->filebufsize)
#define KPS(bytes,secs) (((bytes) / 1024.0) / (secs))
/*
* This is the data repeated in each block written.
*/
#define SIGNATURE "HAMR"
#define SIGNATURE_LENGTH (sizeof SIGNATURE - 1)
#define CLIENTNAME_TERMCHAR 0x7f
typedef struct datablock {
char signature[SIGNATURE_LENGTH];
Offset_t offset; /* Offset of this datablock in the file. */
uint32_t fileid; /* Unique number associated with file. */
uint32_t timestamp; /* time_t when the file was written. */
uint32_t pid; /* Process ID that wrote the file. */
char clientname[1]; /* clientname + CLIENTNAME_TERMCHAR */
} datablock_t;
#define FILESIG 0xc0edbabe
typedef struct hammer_file {
uint32_t sig; /* The file signature (above). */
hbool_t hasdir; /* Has directory flag. */
hbool_t is_disk_full; /* Is the disk full flag. */
char *path; /* Pointer to the file path. */
char *colon; /* Pointer to ':' (if any). */
char *fpath; /* Pointer to the full path. */
uint32_t fileid; /* The file ID (random number). */
uint32_t timestamp; /* The file timestamp. */
int64_t size; /* The size of the file. */
struct hammer_file *prev, *next;
struct hammer_file *base;
} hammer_file_t;
/*
* Inodes allocated to/freed from hammer.
* (Assume that nothing else is running on the target volume.)
*/
#define INODE_HASHTABLE_SIZE 65521
#define INODE_HASH(inode) ((inode) % INODE_HASHTABLE_SIZE)
typedef struct hammer_inode {
os_ino_t ino;
struct hammer_inode *next;
} hammer_inode_t;
typedef struct hammer_mode {
int lower, upper;
} hammer_mode_t;
/*
* hammer Specific Parameters (options):
*/
typedef struct hammer_parameters {
hbool_t disk_filled;
hbool_t randombsize;
int filebufsize;
int next_action;
hbool_t background;
hbool_t keep_disk_full;
hbool_t noflush;
hbool_t onlydelete;
hbool_t onlyflush;
hbool_t nostreams;
hbool_t nocleanup;
hbool_t wantcore;
hbool_t nofilercore;
hbool_t testfilercore;
hbool_t lock_files;
int unlock_chance;
hbool_t halt_on_all_errors;
hbool_t halt_on_file_errors;
hbool_t halt_on_lock_errors;
hbool_t halt_on_data_corruption;
hbool_t inode_check;
uint64_t num_iterations;
uint64_t max_iterations;
int64_t minfsize;
int64_t maxfsize;
int minbsize;
int maxbsize;
time_t max_runtime;
hammer_mode_t *mode;
hammer_mode_t *lock_mode; /* lock mode selection (randomization control) */
} hammer_parameters_t;
/*
* hammer Thread Specific Information:
*
* Note: There's only one hammer thread, but keeping this consistent with
* other I/O behaviors (parameters vs. thread data).
*/
typedef struct hammer_thread_info {
dinfo_t *dip;
pid_t mypid;
time_t whenstart;
char *filebuf;
char *clientname;
char *clientver;
char *curdir;
char *logdir;
char *corrupted_file;
Offset_t corrupted_offset;
int ncopies;
uint64_t file_number;
int64_t nfiles;
int64_t nfiles_when_full;
hammer_file_t *head;
hammer_file_t *lastwrittenfile;
datablock_t *datablock;
uint32_t datablocklen;
hammer_inode_t *inode_hash_table[INODE_HASHTABLE_SIZE];
struct timeval start;
char logtime_buf[TIME_BUFFER_SIZE];
char *uncpath;
/* Error Counters: */
uint32_t file_errors;
uint32_t lock_errors;
uint32_t data_corruptions;
} hammer_thread_info_t;
typedef struct hammer_information {
hammer_parameters_t hammer_parameters;
hammer_thread_info_t hammer_thread_info;
} hammer_information_t;
/*
* Local Definitions:
*/
static char *disk_full_str = "disk is full";
/*
* hammer File Operations:
*/
#define INVALID_ACTION -1
#define CREATEFILE 0
#define OWRITEFILE 1
#define TRUNCFILE 2
#define DELETEFILE 3
#define RENAMEFILE 4
#define READFILE 1000
/* Note: Not tuneable, so leave as static (for now). */
static struct hammer_mode creates[] = {
{ 1, 80 }, /* CREATEFILE 80% */
{ 81, 85 }, /* RENAMEFILE 5% */
{ 86, 90 }, /* OWRITEFILE 5% */
{ 91, 95 }, /* TRUNCFILE 5% */
{ 96, 100 }, /* DELETEFILE 5% */
};
static struct hammer_mode mixed[] = {
{ 1, 40 }, /* CREATEFILE 40% */
{ 41, 55 }, /* RENAMEFILE 15% */
{ 56, 70 }, /* OWRITEFILE 15% */
{ 71, 85 }, /* TRUNCFILE 15% */
{ 86, 100 }, /* DELETEFILE 15% */
};
static struct hammer_mode overwrites[] = {
{ 1, 20 }, /* CREATEFILE 20% */
{ 21, 25 }, /* RENAMEFILE 5% */
{ 26, 85 }, /* OWRITEFILE 60% */
{ 86, 90 }, /* TRUNCFILE 5% */
{ 91, 100 }, /* DELETEFILE 10% */
};
static struct hammer_mode lck_full[] = {
{ 1, 80 }, /* FULL LOCK 80% */
{ 81, 100 }, /* PARTIAL LOCK 20% */
};
static struct hammer_mode lck_mixed[] = {
{ 1, 50 }, /* FULL LOCK 50% */
{ 51, 100 }, /* PARTIAL LOCK 50% */
};
static struct hammer_mode lck_partial[] = {
{ 1, 20 }, /* FULL LOCK 20% */
{ 21, 100 }, /* PARTIAL LOCK 80% */
};
/*
* Hammer Specific Functions:
*/
int hammer_doio(dinfo_t *dip);
void hammer_startup(dinfo_t *dip);
char *getmodename(dinfo_t *dip);
char *makefullpath(dinfo_t *dip, char *path);
void update_dname(dinfo_t *dip, char *file);
int init_datablock(dinfo_t *dip);
void setmem(dinfo_t *dip, char *buf, size_t nbytes, Offset_t offset, uint32_t fileid, uint32_t timestamp);
unsigned char *corruption(dinfo_t *dip, unsigned char *bad, unsigned char *good, size_t nbytes);
char *mkcorruptmsg(unsigned char *bad, unsigned char *good, size_t nbytes);
void hammer_report_miscompare_information(
dinfo_t *dip, char *file, HANDLE *fdp, char *base, size_t iosize, size_t nbytes, Offset_t buffer_index);
void dumpfilercore(dinfo_t *dip);
void *chkmem(dinfo_t *dip, char *file, HANDLE *fdp, char *buf, size_t nbytes, Offset_t offset, uint32_t fileid, uint32_t timestamp);
HANDLE start_copy(dinfo_t *dip, hammer_file_t *f, int64_t fsize);
int choose_action(dinfo_t *dip, hammer_parameters_t *hmrp);
int cleanup_files(dinfo_t *dip);
void cleanup_hammer(dinfo_t *dip);
hammer_file_t *allocfile(dinfo_t *dip, char *path);
int freefile(dinfo_t *dip, hammer_file_t *file);
int freefiles(dinfo_t *dip);
hammer_file_t *getrndfile(dinfo_t *dip);
hammer_file_t *findfile(hammer_thread_info_t *tip, char *path);
static hammer_file_t *findotherstream(dinfo_t *dip, hammer_thread_info_t *tip, hammer_file_t *f);
uint64_t newrndfilenum(hammer_thread_info_t *tip);
hammer_file_t *newrndfile(dinfo_t *dip);
int updatesize(dinfo_t *dip, hammer_file_t *f);
int writefile(dinfo_t *dip, hammer_file_t **fp);
int api_writefile(dinfo_t *dip, hammer_file_t *f, int bsize, int64_t fsize, int do_overwrite);
int readfile(dinfo_t *dip, hammer_file_t *f);
int api_readfile(dinfo_t *dip, hammer_file_t *f, int bsize);
int truncatefile(dinfo_t *dip, hammer_file_t *f);
int renamefile(dinfo_t *dip, hammer_file_t *f);
int deletefile(dinfo_t *dip, hammer_file_t *f, hbool_t cleanup_flag);
int removepath(dinfo_t *dip, char *path);
static int inode_add(dinfo_t *dip, hammer_thread_info_t *tip, os_ino_t ino);
static void inode_remove(hammer_thread_info_t *tip, os_ino_t ino);
static hbool_t inode_exists(hammer_thread_info_t *tip, os_ino_t ino);
static os_ino_t inode_lookup(char *path, HANDLE fd);
static double timer(dinfo_t *dip, hbool_t dostart);
char *mklogtime(hammer_thread_info_t *tip);
char *mktimezone(hammer_thread_info_t *tip);
static __inline void setfileid(hammer_file_t *f);
static __inline void setfiletimestamp(hammer_file_t *f);
static __inline void setdiskisfull(hammer_parameters_t *hmrp, hammer_thread_info_t *tip);
hbool_t test_lock_mode(dinfo_t *dip, hammer_parameters_t *hmrp, int lck_mode);
hbool_t unlock_file_chance(dinfo_t *dip, hammer_parameters_t *hmrp);
/* Temorary API's: */
int api_lockfile(dinfo_t *dip, HANDLE *fd, hammer_file_t *f, char lock_type, Offset_t offset, Offset_t length);
/* ---------------------------------------------------------------------- */
/*
* Forward References:
*/
void hammer_help(dinfo_t *dip);
int hammer_thread_setup(dinfo_t *dip);
/* I/O Behavior Support Functions */
int hammer_initialize(dinfo_t *dip);
int hammer_parser(dinfo_t *dip, char *option);
void hammer_cleanup_information(dinfo_t *dip);
int hammer_clone_information(dinfo_t *dip, dinfo_t *cdip, hbool_t new_context);
int hammer_job_finish(dinfo_t *mdip, job_info_t *job);
void hammer_show_parameters(dinfo_t *dip);
void *hammer_thread(void *arg);
int hammer_validate_parameters(dinfo_t *dip);
/*
* Declare the I/O behavior functions:
*/
iobehavior_funcs_t hammer_iobehavior_funcs = {
"hammer", /* iob_name */
HAMMER_IO, /* iob_iobehavior */
&hammer_map_options, /* iob_map_options */
NULL, /* iob_maptodt_name */
NULL, /* iob_dtmap_options */
&hammer_initialize, /* iob_initialize */
NULL, /* iob_initiate_job */
&hammer_parser, /* iob_parser */
&hammer_cleanup_information,/* iob_cleanup */
&hammer_clone_information, /* iob_clone */
&hammer_thread, /* iob_thread */
NULL, /* iob_thread1 */
NULL, /* iob_job_init */
NULL, /* iob_job_cleanup */
&hammer_job_finish, /* iob_job_finish */
NULL, /* iob_job_modify */
&hammer_job_finish, /* iob_job_query */
NULL, /* iob_job_keepalive */
NULL, /* iob_thread_keepalive */
&hammer_show_parameters, /* iob_show_parameters */
&hammer_validate_parameters /* iob_validate_parameters */
};
void
hammer_set_iobehavior_funcs(dinfo_t *dip)
{
dip->di_iobf = &hammer_iobehavior_funcs;
/* Match hammers' UUID directory style! */
dip->di_uuid_dashes = False;
return;
}
/* ---------------------------------------------------------------------- */
#if _BIG_ENDIAN_
# define tobigendian2(n) (n)
# define tobigendian4(n) (n)
# define tobigendian8(n) (n)
#else /* _BIG_ENDIAN_ == 0 */
/* Forward Declarations: */
uint16_t tobigendian2(uint16_t little_n);
uint32_t tobigendian4(uint32_t little_n);
uint64_t tobigendian8(uint64_t little_n);
uint16_t
tobigendian2(uint16_t little_n)
{
unsigned char *little_p, *big_p;
uint16_t big_n;
little_p = (unsigned char *)&little_n;
big_p = (unsigned char *)&big_n;
big_p[0] = little_p[1];
big_p[1] = little_p[0];
return big_n;
}
uint32_t
tobigendian4(uint32_t little_n)
{
unsigned char *little_p, *big_p;
uint32_t big_n;
little_p = (unsigned char *)&little_n;
big_p = (unsigned char *)&big_n;
big_p[0] = little_p[3];
big_p[1] = little_p[2];
big_p[2] = little_p[1];
big_p[3] = little_p[0];
return big_n;
}
uint64_t
tobigendian8(uint64_t little_n)
{
unsigned char *little_p, *big_p;
uint64_t big_n;
little_p = (unsigned char *)&little_n;
big_p = (unsigned char *)&big_n;
big_p[0] = little_p[7];
big_p[1] = little_p[6];
big_p[2] = little_p[5];
big_p[3] = little_p[4];
big_p[4] = little_p[3];
big_p[5] = little_p[2];
big_p[6] = little_p[1];
big_p[7] = little_p[0];
return big_n;
}
#endif /* _BIG_ENDIAN_ */
/* Note: BSD random API's are NOT being ported! */
/* Note: Moved to dt.h and dtutil.c for sharing! */
#if 0
/* lower <= rnd(lower,upper) <= upper */
static __inline int64_t
rnd64(dinfo_t *dip, int64_t lower, int64_t upper)
{
return( lower + (int64_t)( ((double)(upper - lower + 1) * genrand64_int64(dip)) / (UINT64_MAX + 1.0)) );
}
/* lower <= rnd(lower,upper) <= upper */
static __inline long
rnd(dinfo_t *dip, long lower, long upper)
{
return( lower + (long)( ((double)(upper - lower + 1) * get_random(dip)) / (UINT32_MAX + 1.0)) );
}
#endif /* 0 */
/* ---------------------------------------------------------------------- */
int
hammer_map_options(dinfo_t *dip, int argc, char **argv)
{
int i;
char *cmdp, *option, *optp, *param;
int status = SUCCESS;
status = SetupCommandBuffers(dip);
if (status == FAILURE) return(status);
cmdp = dip->cmdbufptr;
/* Set the I/O behavior firstr!. */
cmdp += sprintf(cmdp, "iobehavior=hammer");
for (i = 0; i < argc; i++) {
option = optp = argv[i];
/* Note: Allowing dt style options too! */
if ( (*option != '-') && NES(option, "=") ) {
cmdp += sprintf(cmdp, " dir=%s", option);
continue;
}
/* Remember, match() updates 1st arg (string parsed)! */
if ( match(&optp, "-api") || match(&optp, "-filercore") ||
match(&optp, "-iterations") || match(&optp, "-mode") ||
match(&optp, "-logfile") ||
match(&optp, "-blocksize") || match(&option, "-bsize") ||
match(&optp, "-minfsize") || match(&optp, "-maxfsize") ||
match(&optp, "-minbsize") || match(&optp, "-maxbsize") ||
match(&optp, "-runtime") || match(&optp, "-seed") ||
match(&optp, "-lockmode") || match(&optp, "-unlockchance") ||
match(&optp, "-trigger") || match(&optp, "-ontap_cserver") ||
match(&optp, "-ontap_nodes") || match(&optp, "-ontap_username") ||
match(&optp, "-optap_password") || match(&optp, "-ontapi_path") ) {
/* Get next parameter (if any). */
if (++i < argc) {
param = argv[i];
cmdp += sprintf(cmdp, " %s=%s", option, param);
}
continue;
}
/* Pass option through to I/O behavior parser. */
cmdp += sprintf(cmdp, " %s", option);
}
if (status == SUCCESS) {
dip->argc = MakeArgList(dip->argv, dip->cmdbufptr);
if (dip->argc == FAILURE) status = FAILURE;
}
return(status);
}
int
hammer_parser(dinfo_t *dip, char *option)
{
hammer_information_t *hip = dip->di_opaque;
hammer_parameters_t *hmrp = &hip->hammer_parameters;
char *optp = option;
int status = PARSE_MATCH;
if (match(&option, "-")) { /* Optional "-" to match hammer options! */
;
}
if (match(&option, "help")) {
hammer_help(dip);
return(STOP_PARSING);
}
/* Hammer specific options. */
if (match(&option, "version")) {
Printf(dip, "%s\n", COPYRIGHT);
Printf(dip, "hammer version %s\n", VERSION);
return(STOP_PARSING);
}
if (match(&option, "api=") ) {
#if !defined(WIN32)
if ( NE(option, "posix") ) {
Eprintf(dip, "Unix only supports the POSIX API!\n");
return(FAILURE);
}
#else /* defined(WIN32) */
if ( NE(option, "win32") ) {
Eprintf(dip, "Windows only supports the WIN32 API!\n");
return(FAILURE);
}
#endif /* !defined(WIN32) */
return(status);
}
if (match(&option, "bg")) {
hmrp->background = True;
return(status);
}
if (match(&option, "interactive")) {
Wprintf(dip, "Hammers' interactive mode is NOT supported!\n");
return(status);
}
if (match(&option, "iterations=")) {
hmrp->max_iterations = large_number(dip, option, ANY_RADIX, &status, True);
if (status == SUCCESS) dip->di_record_limit = hmrp->max_iterations;
return(status);
}
if (match(&option, "mode=")) {
if (match(&option, "creates")) {
hmrp->mode = creates;
} else if (match(&option, "mixed")) {
hmrp->mode = mixed;
} else if (match(&option, "overwrites")) {
hmrp->mode = overwrites;
} else {
Eprintf(dip, "Valid modes are: creates, mixed, or overwrites\n");
status = FAILURE;
}
return(status);
}
if (match(&option, "blocksize=") || match(&option, "bsize=")) {
if (match(&option, "random")) {
hmrp->randombsize = True;
} else {
hmrp->randombsize = False;
hmrp->maxbsize = (int)number(dip, option, ANY_RADIX, &status, True);
}
return(status);
}
if (match(&option, "minfsize=")) {
hmrp->minfsize = large_number(dip, option, ANY_RADIX, &status, True);
return(status);
}
if (match(&option, "maxfsize=")) {
hmrp->maxfsize = large_number(dip, option, ANY_RADIX, &status, True);
dip->di_data_limit = hmrp->maxfsize;
return(status);
}
if (match(&option, "minbsize=")) {
hmrp->minbsize = number(dip, option, ANY_RADIX, &status, True);
return(status);
}
if (match(&option, "maxbsize=")) {
hmrp->maxbsize = number(dip, option, ANY_RADIX, &status, True);
return(status);
}
if (match(&option, "logfile=")) {
if (dip->di_log_file) {
FreeStr(dip, dip->di_log_file);
dip->di_log_file = NULL;
}
if (*option) {
dip->di_log_file = strdup(option);
/* Note: hammer does not display the command line. */
//dip->di_logheader_flag = True;
}
return(status);
}
if (match(&option, "runtime=")) {
dip->di_runtime = time_value(dip, option);
return(status);
}
if (match(&option, "threads=")) {
dip->di_threads = (int)number(dip, option, ANY_RADIX, &status, True);
return(status);
}
if (match(&option, "seed=")) {
dip->di_random_seed = large_number(dip, option, ANY_RADIX, &status, True);
if (status == SUCCESS) dip->di_user_rseed = True;
return(status);
}
if (match(&option, "direct")) {
dip->di_open_flags |= O_DIRECT;
dip->di_dio_flag = True;
return(status);
}
if (match(&option, "fill")) {
hmrp->keep_disk_full = True;
hmrp->mode = creates;
return(status);
}
if (match(&option, "nocleanup")) {
hmrp->nocleanup = True;
return(status);
}
if (match(&option, "noflush")) {
hmrp->noflush = True;
return(status);
}
if (match(&option, "onlydelete")) {
hmrp->onlydelete = True;
return(status);
}
if (match(&option, "onlyflush")) {
hmrp->onlyflush = True;
return(status);
}
if (match(&option, "streams")) {
hmrp->nostreams = False;
return(status);
}
#if defined(WIN32)
if (match(&option, "nostreams")) {
hmrp->nostreams = True;
return(status);
}
if (match(&option, "noretrydisc")) {
dip->di_retry_disconnects = False;
dip->di_retry_entries = 0;
return(status);
}
if (match(&option, "retrydisc")) {
dip->di_retry_disconnects = True;
os_set_disconnect_errors(dip);
return(status);
}
#endif /* defined(WIN32) */
if (match(&option, "checkinodes")) {
hmrp->inode_check = True;
return(status);
}
#if defined(NETAPP)
/* Convert this to dts' equivalent for c-mode! */
if (match(&option, "filercore=")) {
trigger_data_t *tdp = &dip->di_triggers[dip->di_num_triggers];
dip->di_ontap_cserver = strdup(option);
if (dip->di_num_triggers == NUM_TRIGGERS) {
Eprintf(dip, "Maximum number of triggers is %d.\n", NUM_TRIGGERS);
status = FAILURE;
} else {
if ((tdp->td_trigger = check_trigger_type(dip, "zapipanic")) == TRIGGER_INVALID) {
status = FAILURE;
} else {
dip->di_num_triggers++;
}
}
return(status);
}
if (match(&option, "nofilercore")) {
return(status);
}
#endif /* defined(NETAPP) */
if (match(&option, "lockdebug")) {
dip->di_lDebugFlag = True;
return(status);
}
if (match(&option, "nolockdebug")) {
dip->di_lDebugFlag = False;
return(status);
}
if (match(&option, "lockfiles")) {
hmrp->lock_files = True;
return(status);
}
if (match(&option, "lockmode=")) {
if (match(&option, "full")) {
hmrp->lock_mode = lck_full;
} else if (match(&option, "mixed")) {
hmrp->lock_mode = lck_mixed;
} else if (match(&option, "partial")) {
hmrp->lock_mode = lck_partial;
} else {
Eprintf(dip, "Valid lock modes are: full, mixed, or partial\n");
status = FAILURE;
}
if (status == SUCCESS) hmrp->lock_files = True;
return(status);
}
if (match(&option, "ignoreallerrors")) {
hmrp->halt_on_all_errors = False;
return(status);
}
if (match(&option, "ignorefileerrors")) {
hmrp->halt_on_file_errors = False;
return(status);
}
if (match(&option, "ignorelockerrors")) {
hmrp->halt_on_lock_errors = False;
return(status);
}
if (match(&option, "ignoredatacorruption")) {
hmrp->halt_on_data_corruption = False;
return(status);
}
if (match(&option, "unlockchance=")) {
hmrp->unlock_chance = (int)number(dip, option, ANY_RADIX, &status, True);
if ( (status == SUCCESS) &&
( (hmrp->unlock_chance < 0) || (hmrp->unlock_chance > 100) ) ) {
Eprintf(dip, "invalid value [%d] for '-unlockchance' option. Valid values are in the range: 0-100\n", hmrp->unlock_chance);
status = FAILURE;
}
hmrp->lock_files = True;
return(status);
}
if ( match(&optp, "-bg") ||
match(&optp, "-interactive") ||
match(&optp, "-nofilercore") ) {
Wprintf(dip, "Option %s is NOT supported in dts' hammer, so ignored!\n", option);
return(status);
}
return(PARSE_NOMATCH);
}
/* ---------------------------------------------------------------------- */
int
hammer_job_finish(dinfo_t *dip, job_info_t *job)
{
hammer_information_t *hip;
hammer_thread_info_t *thread_info;
threads_info_t *tip = job->ji_tinfo;
dinfo_t *tdip;
int thread;
/*
* Accumulate the total statistics.
*/
for (thread = 0; (thread < tip->ti_threads); thread++) {
tdip = tip->ti_dts[thread];
hip = tdip->di_opaque;
thread_info = &hip->hammer_thread_info;
/* Accumulate thread statistics here...*/
}
//hammer_report_stats(dip, total_info, "Total", hip->hammer_style);
return(SUCCESS);
}
int
hammer_doio(dinfo_t *dip)
{
hammer_information_t *hip = dip->di_opaque;
hammer_thread_info_t *tip = &hip->hammer_thread_info;
hammer_parameters_t *hmrp = &hip->hammer_parameters;
hammer_file_t *f = NULL;
int action;
int status = SUCCESS;
do {
PAUSE_THREAD(dip);
if (hmrp->disk_filled && (tip->nfiles <= (tip->nfiles_when_full / 4)) ) {
/* Max data also sets the disk full flag. */
if (dip->di_maxdata_reached) {
Printf(dip, "Resume filling disk, max data written is "LUF" bytes...\n",
dip->di_maxdata_written);
} else {
Printf(dip, "disk is no longer full...\n");
}
hmrp->disk_filled = False;
dip->di_maxdata_reached = False;
}
action = choose_action(dip, hmrp);
if (hmrp->disk_filled) {
/*
* The disk has been filled.
*/
if (hmrp->keep_disk_full) {
/*
* Overwrite when we would have created,
* and create when we would have overwritten.
*/
if (action == CREATEFILE) {
action = OWRITEFILE;
} else if (action == OWRITEFILE) {
action = CREATEFILE;
}
} else {
/*
* Delete when we would have created,
* and we create when we would have
* deleted.
*/
if (action == CREATEFILE) {
action = DELETEFILE;
} else if (action == DELETEFILE) {
if (dip->di_max_files && (tip->nfiles >= dip->di_max_files) ) {
action = OWRITEFILE;
} else {
action = CREATEFILE;
}
}
}
}
/*
* If max files specified, don't create more files than requested!
*/
if ( (action == CREATEFILE) &&
(dip->di_max_files && (tip->nfiles >= dip->di_max_files)) ) {
action = OWRITEFILE;
}
switch (action) {
case CREATEFILE: {
f = NULL;
status = writefile(dip, &f);
if (status == SUCCESS) hmrp->num_iterations++;
break;
}
case OWRITEFILE: {
if ( (f = getrndfile(dip)) != NULL) {
status = writefile(dip, &f);
if (status == SUCCESS) hmrp->num_iterations++;
}
break;
}
case RENAMEFILE: {
if ( (f = getrndfile(dip)) != NULL) {
status = renamefile(dip, f);
if (status == SUCCESS) hmrp->num_iterations++;
}
break;
}
case TRUNCFILE: {
if ( (f = getrndfile(dip)) != NULL) {
status = truncatefile(dip, f);
if (status == SUCCESS) hmrp->num_iterations++;
}
break;
}
case DELETEFILE: {
hbool_t cleanup_flag = False;
if ( (f = getrndfile(dip)) != NULL) {
status = deletefile(dip, f, cleanup_flag);
if (status == SUCCESS) hmrp->num_iterations++;
f = NULL; /* Already freed, skip below! */
}