forked from filebench/filebench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileset.c
1948 lines (1655 loc) · 52.5 KB
/
fileset.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Portions Copyright 2008 Denis Cheng
*/
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <math.h>
#include <libgen.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include "filebench.h"
#include "fileset.h"
#include "gamma_dist.h"
#include "utils.h"
#include "fsplug.h"
static int filecreate_done;
/*
* File sets, of type fileset_t, are entities which contain
* information about collections of files and subdirectories in Filebench.
* The fileset, once populated, consists of a tree of fileset entries of
* type filesetentry_t which specify files and directories. The fileset
* is rooted in a directory specified by fileset_path, and once the populated
* fileset has been created, has a tree of directories and files
* corresponding to the fileset's filesetentry tree.
*
* Fileset entities are allocated by fileset_define() which is called from
* parser_gram.y: parser_fileset_define(). The filesetentry tree corrseponding
* to the eventual directory and file tree to be instantiated on the storage
* medium is built by fileset_populate(), which is This routine is called
* from fileset_createset(), which is in turn called by fileset_createset().
* After calling fileset_populate(), fileset_createset() will call
* fileset_create() to pre-allocate designated files and directories.
*
* Fileset_createset() is called from parser_gram.y: parser_create_fileset()
* when a "create fileset" or "run" command is encountered. When the
* "create fileset" command is used, it is generally paired with
* a "create processes" command, and must appear first, in order to
* instantiate all the files in the fileset before trying to use them.
*/
/* maximum parallel allocation control */
#define MAX_PARALLOC_THREADS 32
/*
* returns pointer to file or fileset
* string, as appropriate
*/
static char *
fileset_entity_name(fileset_t *fileset)
{
if (fileset->fs_attrs & FILESET_IS_FILE)
return ("file");
else
return ("fileset");
}
/*
* Removes the last file or directory name from a pathname.
* Basically removes characters from the end of the path by
* setting them to \0 until a forward slash '/' is
* encountered. It also removes the forward slash.
*/
static char *
trunc_dirname(char *dir)
{
char *s = dir + strlen(dir);
while (s != dir) {
int c = *s;
*s = 0;
if (c == '/')
break;
s--;
}
return (dir);
}
/*
* Creates a path string from the filesetentry_t "*entry"
* and all of its parent's path names. The resulting path
* is a concatination of all the individual parent paths.
* Allocates memory for the path string and returns a
* pointer to it.
*/
char *
fileset_resolvepath(filesetentry_t *entry)
{
filesetentry_t *fsep = entry;
char path[MAXPATHLEN];
char pathtmp[MAXPATHLEN];
char *s;
path[0] = '\0';
while (fsep->fse_parent) {
(void) strcpy(pathtmp, "/");
(void) fb_strlcat(pathtmp, fsep->fse_path, MAXPATHLEN);
(void) fb_strlcat(pathtmp, path, MAXPATHLEN);
(void) fb_strlcpy(path, pathtmp, MAXPATHLEN);
fsep = fsep->fse_parent;
}
s = malloc(strlen(path) + 1);
(void) fb_strlcpy(s, path, MAXPATHLEN);
return (s);
}
/*
* Creates multiple nested directories as required by the
* supplied path. Starts at the end of the path, creating
* a list of directories to mkdir, up to the root of the
* path, then mkdirs them one at a time from the root on down.
*/
static int
fileset_mkdir(char *path, int mode)
{
char *p;
char *dirs[65536];
int i = 0;
if ((p = strdup(path)) == NULL)
goto null_str;
/*
* Fill an array of subdirectory path names until either we
* reach the root or encounter an already existing subdirectory
*/
/* CONSTCOND */
while (1) {
struct stat64 sb;
if (stat64(p, &sb) == 0)
break;
if (strlen(p) < 3)
break;
if ((dirs[i] = strdup(p)) == NULL) {
free(p);
goto null_str;
}
(void) trunc_dirname(p);
i++;
}
/* Make the directories, from closest to root downwards. */
for (--i; i >= 0; i--) {
(void) FB_MKDIR(dirs[i], mode);
free(dirs[i]);
}
free(p);
return (FILEBENCH_OK);
null_str:
/* clean up */
for (--i; i >= 0; i--)
free(dirs[i]);
filebench_log(LOG_ERROR,
"Failed to create directory path %s: Out of memory", path);
return (FILEBENCH_ERROR);
}
/*
* creates the subdirectory tree for a fileset.
*/
static int
fileset_create_subdirs(fileset_t *fileset, char *filesetpath)
{
filesetentry_t *direntry;
char full_path[MAXPATHLEN];
char *part_path;
/* walk the subdirectory list, enstanciating subdirs */
direntry = fileset->fs_dirlist;
while (direntry) {
(void) fb_strlcpy(full_path, filesetpath, MAXPATHLEN);
part_path = fileset_resolvepath(direntry);
(void) fb_strlcat(full_path, part_path, MAXPATHLEN);
free(part_path);
/* now create this portion of the subdirectory tree */
if (fileset_mkdir(full_path, 0755) == FILEBENCH_ERROR)
return (FILEBENCH_ERROR);
direntry = direntry->fse_nextoftype;
}
return (FILEBENCH_OK);
}
/*
* move filesetentry between exist tree and non-exist tree, source_tree
* to destination tree.
*/
static void
fileset_move_entry(avl_tree_t *src_tree, avl_tree_t *dst_tree,
filesetentry_t *entry)
{
avl_remove(src_tree, entry);
avl_add(dst_tree, entry);
}
/*
* given a fileset entry, determines if the associated leaf directory
* needs to be made or not, and if so does the mkdir.
*/
static int
fileset_alloc_leafdir(filesetentry_t *entry)
{
fileset_t *fileset;
char path[MAXPATHLEN];
struct stat64 sb;
char *pathtmp;
fileset = entry->fse_fileset;
(void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN);
(void) fb_strlcat(path, "/", MAXPATHLEN);
(void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN);
pathtmp = fileset_resolvepath(entry);
(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
free(pathtmp);
filebench_log(LOG_DEBUG_IMPL, "Populated %s", entry->fse_path);
/* see if not reusing and this directory does not exist */
if (!((entry->fse_flags & FSE_REUSING) && (stat64(path, &sb) == 0))) {
/* No file or not reusing, so create */
if (FB_MKDIR(path, 0755) < 0) {
filebench_log(LOG_ERROR,
"Failed to pre-allocate leaf directory %s: %s",
path, strerror(errno));
fileset_unbusy(entry, TRUE, FALSE, 0);
return (FILEBENCH_ERROR);
}
}
/* unbusy the allocated entry */
fileset_unbusy(entry, TRUE, TRUE, 0);
return (FILEBENCH_OK);
}
/*
* given a fileset entry, determines if the associated file
* needs to be allocated or not, and if so does the allocation.
*/
static int
fileset_alloc_file(filesetentry_t *entry)
{
fileset_t *fileset;
char path[MAXPATHLEN];
char *buf;
struct stat64 sb;
char *pathtmp;
off64_t seek;
fb_fdesc_t fdesc;
int trust_tree;
int fs_readonly;
fileset = entry->fse_fileset;
(void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN);
(void) fb_strlcat(path, "/", MAXPATHLEN);
(void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN);
pathtmp = fileset_resolvepath(entry);
(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
free(pathtmp);
filebench_log(LOG_DEBUG_IMPL, "Populated %s", entry->fse_path);
/* see if fileset is readonly */
fs_readonly = avd_get_bool(fileset->fs_readonly) == TRUE;
/* see if reusing and this file exists */
trust_tree = avd_get_bool(fileset->fs_trust_tree);
if ((entry->fse_flags & FSE_REUSING) && (trust_tree ||
(FB_STAT(path, &sb) == 0))) {
if (FB_OPEN(&fdesc, path, fs_readonly ? O_RDONLY : O_RDWR, 0) == FILEBENCH_ERROR) {
filebench_log(LOG_INFO,
"Attempted but failed to Re-use file %s",
path);
fileset_unbusy(entry, TRUE, FALSE, 0);
return (FILEBENCH_ERROR);
}
if (trust_tree || (sb.st_size == (off64_t)entry->fse_size)) {
filebench_log(LOG_DEBUG_IMPL,
"Reusing file %s", path);
(void) FB_CLOSE(&fdesc);
/* unbusy the allocated entry */
fileset_unbusy(entry, TRUE, TRUE, 0);
return (FILEBENCH_OK);
} else if (sb.st_size > (off64_t)entry->fse_size) {
/* reuse, but too large */
filebench_log(LOG_DEBUG_IMPL,
"Truncating & re-using file %s", path);
(void) FB_FTRUNC(&fdesc, (off64_t)entry->fse_size);
(void) FB_CLOSE(&fdesc);
/* unbusy the allocated entry */
fileset_unbusy(entry, TRUE, TRUE, 0);
return (FILEBENCH_OK);
}
} else {
/* No file or not reusing, so create */
if (FB_OPEN(&fdesc, path, O_RDWR | O_CREAT, 0644) ==
FILEBENCH_ERROR) {
filebench_log(LOG_ERROR,
"Failed to pre-allocate file %s: %s",
path, strerror(errno));
/* unbusy the unallocated entry */
fileset_unbusy(entry, TRUE, FALSE, 0);
return (FILEBENCH_ERROR);
}
}
if ((buf = (char *)malloc(FILE_ALLOC_BLOCK)) == NULL) {
/* unbusy the unallocated entry */
fileset_unbusy(entry, TRUE, FALSE, 0);
return (FILEBENCH_ERROR);
}
for (seek = 0; seek < entry->fse_size; ) {
off64_t wsize;
int ret = 0;
/*
* Write FILE_ALLOC_BLOCK's worth,
* except on last write
*/
wsize = MIN(entry->fse_size - seek, FILE_ALLOC_BLOCK);
ret = FB_WRITE(&fdesc, buf, wsize);
if (ret != wsize) {
filebench_log(LOG_ERROR,
"Failed to pre-allocate file %s: %s",
path, strerror(errno));
(void) FB_CLOSE(&fdesc);
free(buf);
fileset_unbusy(entry, TRUE, FALSE, 0);
return (FILEBENCH_ERROR);
}
seek += wsize;
}
(void) FB_CLOSE(&fdesc);
free(buf);
/* unbusy the allocated entry */
fileset_unbusy(entry, TRUE, TRUE, 0);
filebench_log(LOG_DEBUG_IMPL,
"Pre-allocated file %s size %llu",
path, (u_longlong_t)entry->fse_size);
return (FILEBENCH_OK);
}
/*
* given a fileset entry, determines if the associated file
* needs to be allocated or not, and if so does the allocation.
* Sets shm_fsparalloc_count to -1 on error.
*/
static void *
fileset_alloc_thread(filesetentry_t *entry)
{
if (fileset_alloc_file(entry) == FILEBENCH_ERROR) {
(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
filebench_shm->shm_fsparalloc_count = -1;
} else {
(void) pthread_mutex_lock(&filebench_shm->shm_fsparalloc_lock);
filebench_shm->shm_fsparalloc_count--;
}
(void) pthread_cond_signal(&filebench_shm->shm_fsparalloc_cv);
(void) pthread_mutex_unlock(&filebench_shm->shm_fsparalloc_lock);
pthread_exit(NULL);
return (NULL);
}
/*
* First creates the parent directories of the file using
* fileset_mkdir(). Then Optionally sets the O_DSYNC flag
* and opens the file with open64(). It unlocks the fileset
* entry lock, sets the DIRECTIO_ON or DIRECTIO_OFF flags
* as requested, and returns the file descriptor integer
* for the opened file in the supplied filebench file descriptor.
* Returns FILEBENCH_ERROR on error, and FILEBENCH_OK on success.
*/
int
fileset_openfile(fb_fdesc_t *fdesc, fileset_t *fileset,
filesetentry_t *entry, int flag, int filemode, int attrs)
{
char path[MAXPATHLEN];
char dir[MAXPATHLEN];
char *pathtmp;
struct stat64 sb;
int open_attrs = 0;
(void) fb_strlcpy(path, avd_get_str(fileset->fs_path), MAXPATHLEN);
(void) fb_strlcat(path, "/", MAXPATHLEN);
(void) fb_strlcat(path, avd_get_str(fileset->fs_name), MAXPATHLEN);
pathtmp = fileset_resolvepath(entry);
(void) fb_strlcat(path, pathtmp, MAXPATHLEN);
(void) fb_strlcpy(dir, path, MAXPATHLEN);
free(pathtmp);
(void) trunc_dirname(dir);
/* If we are going to create a file, create the parent dirs */
if ((flag & O_CREAT) && (stat64(dir, &sb) != 0)) {
if (fileset_mkdir(dir, 0755) == FILEBENCH_ERROR)
return (FILEBENCH_ERROR);
}
if (attrs & FLOW_ATTR_DSYNC)
open_attrs |= O_SYNC;
#ifdef HAVE_O_DIRECT
if (attrs & FLOW_ATTR_DIRECTIO)
open_attrs |= O_DIRECT;
#endif /* HAVE_O_DIRECT */
if (FB_OPEN(fdesc, path, flag | open_attrs, filemode)
== FILEBENCH_ERROR) {
filebench_log(LOG_ERROR,
"Failed to open file %d, %s, with status %x: %s",
entry->fse_index, path, entry->fse_flags, strerror(errno));
fileset_unbusy(entry, FALSE, FALSE, 0);
return (FILEBENCH_ERROR);
}
#ifdef HAVE_DIRECTIO
if (attrs & FLOW_ATTR_DIRECTIO)
(void)directio(fdesc->fd_num, DIRECTIO_ON);
#endif /* HAVE_DIRECTIO */
#ifdef HAVE_NOCACHE_FCNTL
if (attrs & FLOW_ATTR_DIRECTIO)
(void)fcntl(fdesc->fd_num, F_NOCACHE, 1);
#endif /* HAVE_NOCACHE_FCNTL */
/* Disable read ahead with the help of fadvise, if asked for */
if (attrs & FLOW_ATTR_FADV_RANDOM) {
#ifdef HAVE_FADVISE
if (posix_fadvise(fdesc->fd_num, 0, 0, POSIX_FADV_RANDOM)
!= FILEBENCH_OK) {
filebench_log(LOG_ERROR,
"Failed to disable read ahead for file %s, with status %s",
path, strerror(errno));
fileset_unbusy(entry, FALSE, FALSE, 0);
return (FILEBENCH_ERROR);
}
filebench_log(LOG_INFO, "** Read ahead disabled **");
#else
filebench_log(LOG_INFO, "** Read ahead was NOT disabled: not supported on this platform! **");
#endif
}
if (flag & O_CREAT)
fileset_unbusy(entry, TRUE, TRUE, 1);
else
fileset_unbusy(entry, FALSE, FALSE, 1);
return (FILEBENCH_OK);
}
/*
* removes all filesetentries from their respective btrees, and puts them
* on the free list. The supplied argument indicates which free list to
* use.
*/
static void
fileset_pickreset(fileset_t *fileset, int entry_type)
{
filesetentry_t *entry;
switch (entry_type & FILESET_PICKMASK) {
case FILESET_PICKFILE:
entry = (filesetentry_t *)avl_first(&fileset->fs_noex_files);
/* make sure non-existing files are marked free */
while (entry) {
entry->fse_flags |= FSE_FREE;
entry->fse_open_cnt = 0;
fileset_move_entry(&fileset->fs_noex_files,
&fileset->fs_free_files, entry);
entry = AVL_NEXT(&fileset->fs_noex_files, entry);
}
/* free up any existing files */
entry = (filesetentry_t *)avl_first(&fileset->fs_exist_files);
while (entry) {
entry->fse_flags |= FSE_FREE;
entry->fse_open_cnt = 0;
fileset_move_entry(&fileset->fs_exist_files,
&fileset->fs_free_files, entry);
entry = AVL_NEXT(&fileset->fs_exist_files, entry);
}
break;
case FILESET_PICKDIR:
/* nothing to reset, as all (sub)dirs always exist */
break;
case FILESET_PICKLEAFDIR:
entry = (filesetentry_t *)
avl_first(&fileset->fs_noex_leaf_dirs);
/* make sure non-existing leaf dirs are marked free */
while (entry) {
entry->fse_flags |= FSE_FREE;
entry->fse_open_cnt = 0;
fileset_move_entry(&fileset->fs_noex_leaf_dirs,
&fileset->fs_free_leaf_dirs, entry);
entry = AVL_NEXT(&fileset->fs_noex_leaf_dirs, entry);
}
/* free up any existing leaf dirs */
entry = (filesetentry_t *)
avl_first(&fileset->fs_exist_leaf_dirs);
while (entry) {
entry->fse_flags |= FSE_FREE;
entry->fse_open_cnt = 0;
fileset_move_entry(&fileset->fs_exist_leaf_dirs,
&fileset->fs_free_leaf_dirs, entry);
entry = AVL_NEXT(&fileset->fs_exist_leaf_dirs, entry);
}
break;
}
}
/*
* find a filesetentry from the fileset using the supplied index
*/
static filesetentry_t *
fileset_find_entry(avl_tree_t *atp, uint_t index)
{
avl_index_t found_loc;
filesetentry_t desired_fse, *found_fse;
/* find the file with the desired index, if it is in the tree */
desired_fse.fse_index = index;
found_fse = avl_find(atp, (void *)(&desired_fse), &found_loc);
if (found_fse != NULL)
return (found_fse);
/* if requested node not found, find next higher node */
found_fse = avl_nearest(atp, found_loc, AVL_AFTER);
if (found_fse != NULL)
return (found_fse);
/* might have hit the end, return lowest available index node */
found_fse = avl_first(atp);
return (found_fse);
}
/*
* Selects a fileset entry from a fileset. If the
* FILESET_PICKLEAFDIR flag is set it will pick a leaf directory entry,
* if the FILESET_PICKDIR flag is set it will pick a non leaf directory
* entry, otherwise a file entry. The FILESET_PICKUNIQUE
* flag will take an entry off of one of the free (unused)
* lists (file or directory), otherwise the entry will be
* picked off of one of the rotor lists (file or directory).
* The FILESET_PICKEXISTS will insure that only extant
* (FSE_EXISTS) state files are selected, while
* FILESET_PICKNOEXIST insures that only non extant
* (not FSE_EXISTS) state files are selected.
* Note that the selected fileset entry (file) is returned
* with its FSE_BUSY flag (in fse_flags) set.
*/
filesetentry_t *
fileset_pick(fileset_t *fileset, int flags, int tid, int index)
{
filesetentry_t *entry = NULL;
filesetentry_t *start_point;
avl_tree_t *atp = NULL;
fbint_t max_entries = 0;
(void) ipc_mutex_lock(&fileset->fs_pick_lock);
/* see if we have to wait for available files or directories */
switch (flags & FILESET_PICKMASK) {
case FILESET_PICKFILE:
filebench_log(LOG_DEBUG_SCRIPT, "Picking file");
if (fileset->fs_filelist == NULL)
goto empty;
while (fileset->fs_idle_files == 0) {
(void) pthread_cond_wait(&fileset->fs_idle_files_cv,
&fileset->fs_pick_lock);
}
max_entries = fileset->fs_constentries;
if (flags & FILESET_PICKUNIQUE) {
filebench_log(LOG_DEBUG_SCRIPT, "Picking unique");
atp = &fileset->fs_free_files;
} else if (flags & FILESET_PICKNOEXIST) {
filebench_log(LOG_DEBUG_SCRIPT, "Picking not existing");
atp = &fileset->fs_noex_files;
} else {
filebench_log(LOG_DEBUG_SCRIPT, "Picking existing");
atp = &fileset->fs_exist_files;
}
break;
case FILESET_PICKDIR:
filebench_log(LOG_DEBUG_SCRIPT, "Picking directory");
if (fileset->fs_dirlist == NULL)
goto empty;
while (fileset->fs_idle_dirs == 0) {
(void) pthread_cond_wait(&fileset->fs_idle_dirs_cv,
&fileset->fs_pick_lock);
}
max_entries = 1;
atp = &fileset->fs_dirs;
break;
case FILESET_PICKLEAFDIR:
filebench_log(LOG_DEBUG_SCRIPT, "Picking leaf directory");
if (fileset->fs_leafdirlist == NULL)
goto empty;
while (fileset->fs_idle_leafdirs == 0) {
(void) pthread_cond_wait(&fileset->fs_idle_leafdirs_cv,
&fileset->fs_pick_lock);
}
max_entries = fileset->fs_constleafdirs;
if (flags & FILESET_PICKUNIQUE) {
atp = &fileset->fs_free_leaf_dirs;
} else if (flags & FILESET_PICKNOEXIST) {
atp = &fileset->fs_noex_leaf_dirs;
} else {
atp = &fileset->fs_exist_leaf_dirs;
}
break;
}
/* see if asking for impossible */
if (avl_is_empty(atp)) {
filebench_log(LOG_DEBUG_SCRIPT, "atp is empty");
goto empty;
}
if (flags & FILESET_PICKUNIQUE) {
uint64_t index64;
/*
* pick at random from free list in order to
* distribute initially allocated files more
* randomly on storage media. Use uniform
* random number generator to select index
* if it is not supplied with pick call.
*/
if (index) {
index64 = index;
} else {
fb_random64(&index64, max_entries, 0, NULL);
}
entry = fileset_find_entry(atp, (int)index64);
if (entry == NULL)
goto empty;
} else if (flags & FILESET_PICKBYINDEX) {
/* pick by supplied index */
entry = fileset_find_entry(atp, index);
} else {
/* pick in rotation */
switch (flags & FILESET_PICKMASK) {
case FILESET_PICKFILE:
if (flags & FILESET_PICKNOEXIST) {
entry = fileset_find_entry(atp,
fileset->fs_file_nerotor);
fileset->fs_file_nerotor =
entry->fse_index + 1;
} else {
entry = fileset_find_entry(atp,
fileset->fs_file_exrotor[tid]);
fileset->fs_file_exrotor[tid] =
entry->fse_index + 1;
}
break;
case FILESET_PICKDIR:
entry = fileset_find_entry(atp, fileset->fs_dirrotor);
fileset->fs_dirrotor = entry->fse_index + 1;
break;
case FILESET_PICKLEAFDIR:
if (flags & FILESET_PICKNOEXIST) {
entry = fileset_find_entry(atp,
fileset->fs_leafdir_nerotor);
fileset->fs_leafdir_nerotor =
entry->fse_index + 1;
} else {
entry = fileset_find_entry(atp,
fileset->fs_leafdir_exrotor);
fileset->fs_leafdir_exrotor =
entry->fse_index + 1;
}
break;
}
}
if (entry == NULL)
goto empty;
/* see if entry in use */
start_point = entry;
while (entry->fse_flags & FSE_BUSY) {
/* it is, so try next */
entry = AVL_NEXT(atp, entry);
if (entry == NULL)
entry = avl_first(atp);
/* see if we have wrapped around */
if ((entry == NULL) || (entry == start_point)) {
filebench_log(LOG_DEBUG_SCRIPT,
"All %d files are busy", avl_numnodes(atp));
goto empty;
}
}
/* update file or directory idle counts */
switch (flags & FILESET_PICKMASK) {
case FILESET_PICKFILE:
fileset->fs_idle_files--;
break;
case FILESET_PICKDIR:
fileset->fs_idle_dirs--;
break;
case FILESET_PICKLEAFDIR:
fileset->fs_idle_leafdirs--;
break;
}
/* Indicate that file or directory is now busy */
entry->fse_flags |= FSE_BUSY;
(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
filebench_log(LOG_DEBUG_SCRIPT, "Picked file %s", entry->fse_path);
return (entry);
empty:
filebench_log(LOG_DEBUG_SCRIPT, "No file found");
(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
return (NULL);
}
/*
* Removes a filesetentry from the "FSE_BUSY" state, signaling any threads
* that are waiting for a NOT BUSY filesetentry. Also sets whether it is
* existant or not, or leaves that designation alone.
*/
void
fileset_unbusy(filesetentry_t *entry, int update_exist,
int new_exist_val, int open_cnt_incr)
{
fileset_t *fileset = NULL;
if (entry)
fileset = entry->fse_fileset;
if (fileset == NULL) {
filebench_log(LOG_ERROR, "fileset_unbusy: NO FILESET!");
return;
}
(void) ipc_mutex_lock(&fileset->fs_pick_lock);
/* modify FSE_EXIST flag and actual dirs/files count, if requested */
if (update_exist) {
if (new_exist_val == TRUE) {
if (entry->fse_flags & FSE_FREE) {
/* asked to set and it was free */
entry->fse_flags |= FSE_EXISTS;
entry->fse_flags &= (~FSE_FREE);
switch (entry->fse_flags & FSE_TYPE_MASK) {
case FSE_TYPE_FILE:
fileset_move_entry(
&fileset->fs_free_files,
&fileset->fs_exist_files, entry);
break;
case FSE_TYPE_DIR:
break;
case FSE_TYPE_LEAFDIR:
fileset_move_entry(
&fileset->fs_free_leaf_dirs,
&fileset->fs_exist_leaf_dirs,
entry);
break;
}
} else if (!(entry->fse_flags & FSE_EXISTS)) {
/* asked to set, and it was clear */
entry->fse_flags |= FSE_EXISTS;
switch (entry->fse_flags & FSE_TYPE_MASK) {
case FSE_TYPE_FILE:
fileset_move_entry(
&fileset->fs_noex_files,
&fileset->fs_exist_files, entry);
break;
case FSE_TYPE_DIR:
break;
case FSE_TYPE_LEAFDIR:
fileset_move_entry(
&fileset->fs_noex_leaf_dirs,
&fileset->fs_exist_leaf_dirs,
entry);
break;
}
}
} else {
if (entry->fse_flags & FSE_FREE) {
/* asked to clear, and it was free */
entry->fse_flags &= (~(FSE_FREE | FSE_EXISTS));
switch (entry->fse_flags & FSE_TYPE_MASK) {
case FSE_TYPE_FILE:
fileset_move_entry(
&fileset->fs_free_files,
&fileset->fs_noex_files, entry);
break;
case FSE_TYPE_DIR:
break;
case FSE_TYPE_LEAFDIR:
fileset_move_entry(
&fileset->fs_free_leaf_dirs,
&fileset->fs_noex_leaf_dirs,
entry);
break;
}
} else if (entry->fse_flags & FSE_EXISTS) {
/* asked to clear, and it was set */
entry->fse_flags &= (~FSE_EXISTS);
switch (entry->fse_flags & FSE_TYPE_MASK) {
case FSE_TYPE_FILE:
fileset_move_entry(
&fileset->fs_exist_files,
&fileset->fs_noex_files, entry);
break;
case FSE_TYPE_DIR:
break;
case FSE_TYPE_LEAFDIR:
fileset_move_entry(
&fileset->fs_exist_leaf_dirs,
&fileset->fs_noex_leaf_dirs,
entry);
break;
}
}
}
}
/* update open count */
entry->fse_open_cnt += open_cnt_incr;
/* increment idle count, clear FSE_BUSY and signal IF it was busy */
if (entry->fse_flags & FSE_BUSY) {
/* unbusy it */
entry->fse_flags &= (~FSE_BUSY);
/* release any threads waiting for unbusy */
if (entry->fse_flags & FSE_THRD_WAITNG) {
entry->fse_flags &= (~FSE_THRD_WAITNG);
(void) pthread_cond_broadcast(
&fileset->fs_thrd_wait_cv);
}
/* increment idle count and signal waiting threads */
switch (entry->fse_flags & FSE_TYPE_MASK) {
case FSE_TYPE_FILE:
fileset->fs_idle_files++;
if (fileset->fs_idle_files >= 1) {
(void) pthread_cond_signal(
&fileset->fs_idle_files_cv);
}
break;
case FSE_TYPE_DIR:
fileset->fs_idle_dirs++;
if (fileset->fs_idle_dirs >= 1) {
(void) pthread_cond_signal(
&fileset->fs_idle_dirs_cv);
}
break;
case FSE_TYPE_LEAFDIR:
fileset->fs_idle_leafdirs++;
if (fileset->fs_idle_leafdirs >= 1) {
(void) pthread_cond_signal(
&fileset->fs_idle_leafdirs_cv);
}
break;
}
}
(void) ipc_mutex_unlock(&fileset->fs_pick_lock);
}
/*
* Given a fileset "fileset", create the associated files as specified in the
* attributes of the fileset. The fileset is rooted in a directory whose
* pathname is in fileset_path. If the directory exists, meaning that there is
* already a fileset, and the fileset_reuse attribute is false, then remove it
* and all its contained files and subdirectories. Next, the routine creates a
* root directory for the fileset. All the file type filesetentries are cycled
* through creating as needed their containing subdirectory trees in the
* filesystem and creating actual files for fileset_preallocpercent of them.
* The created files are filled with fse_size bytes of unitialized data. The
* routine returns FILEBENCH_ERROR on errors, FILEBENCH_OK on success.
*/
static int
fileset_create(fileset_t *fileset)
{
filesetentry_t *entry;
char path[MAXPATHLEN];
struct stat64 sb;
hrtime_t start = gethrtime();
char *fileset_path;
char *fileset_name;
int randno;
int preallocated = 0;
int reusing;
uint64_t preallocpercent;
fileset_path = avd_get_str(fileset->fs_path);
if (!fileset_path) {
filebench_log(LOG_ERROR, "%s path not set",
fileset_entity_name(fileset));
return FILEBENCH_ERROR;
}
fileset_name = avd_get_str(fileset->fs_name);
if (!fileset_name) {