forked from auristor/filebench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowop_library.c
2627 lines (2237 loc) · 76.7 KB
/
flowop_library.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 "config.h"
#include <sys/types.h>
#include <stddef.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <inttypes.h>
#include <fcntl.h>
#include <math.h>
#ifndef HAVE_SYSV_SEM
#include <semaphore.h>
#endif /* HAVE_SYSV_SEM */
#include "filebench.h"
#include "flowop.h"
#include "fileset.h"
#include "fb_random.h"
#include "utils.h"
#include "fsplug.h"
/*
* These routines implement the flowops from the f language. Each
* flowop has has a name such as "read", and a set of function pointers
* to call for initialization, execution and destruction of the flowop.
* The table flowoplib_funcs[] contains a flowoplib struct for each
* implemented flowop. Most flowops use a generic initialization function
* and all currently use a generic destruction function. All flowop
* functions referenced from the table are in this file, though, of
* course, they often call functions from other files.
*
* The flowop_init() routine uses the flowoplib_funcs[] table to
* create an initial set of "instance 0" flowops, one for each type of
* flowop, from which all other flowops are derived. These "instance 0"
* flowops are initialized with information from the table including
* pointers for their fo_init, fo_func and fo_destroy functions. When
* a flowop definition is encountered in an f language script, the
* "type" of flowop, such as "read" is used to search for the
* "instance 0" flowop named "read", then a new flowop is allocated
* which inherits its function pointers and other initial properties
* from the instance 0 flowop, and is given a new name as specified
* by the "name=" attribute.
*/
static void flowoplib_destruct_noop(flowop_t *flowop);
static int flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_print(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_write(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_read(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_block_init(flowop_t *flowop);
static int flowoplib_block(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_sempost(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_sempost_init(flowop_t *flowop);
static int flowoplib_semblock(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_semblock_init(flowop_t *flowop);
static void flowoplib_semblock_destruct(flowop_t *flowop);
static int flowoplib_eventlimit(threadflow_t *, flowop_t *flowop);
static int flowoplib_bwlimit(threadflow_t *, flowop_t *flowop);
static int flowoplib_iopslimit(threadflow_t *, flowop_t *flowop);
static int flowoplib_opslimit(threadflow_t *, flowop_t *flowop);
static int flowoplib_openfile(threadflow_t *, flowop_t *flowop);
static int flowoplib_openfile_common(threadflow_t *, flowop_t *flowop, int fd);
static int flowoplib_createfile(threadflow_t *, flowop_t *flowop);
static int flowoplib_closefile(threadflow_t *, flowop_t *flowop);
static int flowoplib_makedir(threadflow_t *, flowop_t *flowop);
static int flowoplib_removedir(threadflow_t *, flowop_t *flowop);
static int flowoplib_listdir(threadflow_t *, flowop_t *flowop);
static int flowoplib_fsync(threadflow_t *, flowop_t *flowop);
static int flowoplib_readwholefile(threadflow_t *, flowop_t *flowop);
static int flowoplib_writewholefile(threadflow_t *, flowop_t *flowop);
static int flowoplib_appendfile(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_appendfilerand(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_deletefile(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_statfile(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_finishoncount(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_finishonbytes(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_fsyncset(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_testrandvar(threadflow_t *threadflow, flowop_t *flowop);
static int flowoplib_testrandvar_init(flowop_t *flowop);
static void flowoplib_testrandvar_destruct(flowop_t *flowop);
static flowop_proto_t flowoplib_funcs[] = {
{FLOW_TYPE_IO, FLOW_ATTR_WRITE, "write", flowop_init_generic,
flowoplib_write, flowop_destruct_generic},
{FLOW_TYPE_IO, FLOW_ATTR_READ, "read", flowop_init_generic,
flowoplib_read, flowop_destruct_generic},
{FLOW_TYPE_SYNC, 0, "block", flowoplib_block_init,
flowoplib_block, flowop_destruct_generic},
{FLOW_TYPE_SYNC, 0, "wakeup", flowop_init_generic,
flowoplib_wakeup, flowop_destruct_generic},
{FLOW_TYPE_SYNC, 0, "semblock", flowoplib_semblock_init,
flowoplib_semblock, flowoplib_semblock_destruct},
{FLOW_TYPE_SYNC, 0, "sempost", flowoplib_sempost_init,
flowoplib_sempost, flowoplib_destruct_noop},
{FLOW_TYPE_OTHER, 0, "hog", flowop_init_generic,
flowoplib_hog, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "delay", flowop_init_generic,
flowoplib_delay, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "eventlimit", flowop_init_generic,
flowoplib_eventlimit, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "bwlimit", flowop_init_generic,
flowoplib_bwlimit, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "iopslimit", flowop_init_generic,
flowoplib_iopslimit, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "opslimit", flowop_init_generic,
flowoplib_opslimit, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "finishoncount", flowop_init_generic,
flowoplib_finishoncount, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "finishonbytes", flowop_init_generic,
flowoplib_finishonbytes, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "openfile", flowop_init_generic,
flowoplib_openfile, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "createfile", flowop_init_generic,
flowoplib_createfile, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "closefile", flowop_init_generic,
flowoplib_closefile, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "makedir", flowop_init_generic,
flowoplib_makedir, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "removedir", flowop_init_generic,
flowoplib_removedir, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "listdir", flowop_init_generic,
flowoplib_listdir, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "fsync", flowop_init_generic,
flowoplib_fsync, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "fsyncset", flowop_init_generic,
flowoplib_fsyncset, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "statfile", flowop_init_generic,
flowoplib_statfile, flowop_destruct_generic},
{FLOW_TYPE_IO, FLOW_ATTR_READ, "readwholefile", flowop_init_generic,
flowoplib_readwholefile, flowop_destruct_generic},
{FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfile", flowop_init_generic,
flowoplib_appendfile, flowop_destruct_generic},
{FLOW_TYPE_IO, FLOW_ATTR_WRITE, "appendfilerand", flowop_init_generic,
flowoplib_appendfilerand, flowop_destruct_generic},
{FLOW_TYPE_IO, 0, "deletefile", flowop_init_generic,
flowoplib_deletefile, flowop_destruct_generic},
{FLOW_TYPE_IO, FLOW_ATTR_WRITE, "writewholefile", flowop_init_generic,
flowoplib_writewholefile, flowop_destruct_generic},
{FLOW_TYPE_OTHER, 0, "print", flowop_init_generic,
flowoplib_print, flowop_destruct_generic},
/* routine to calculate mean and stddev for output from a randvar */
{FLOW_TYPE_OTHER, 0, "testrandvar", flowoplib_testrandvar_init,
flowoplib_testrandvar, flowoplib_testrandvar_destruct}
};
/*
* Loops through the list of flowops defined in this
* module, and creates and initializes a flowop for each one
* by calling flowop_flow_init. As a side effect of calling
* flowop_flow_init, the created flowops are placed on the
* master flowop list. All created flowops are set to
* instance "0".
*/
void
flowoplib_flowinit(void)
{
int nops = sizeof (flowoplib_funcs) / sizeof (flowop_proto_t);
flowop_add_from_proto(flowoplib_funcs, nops);
}
/*
* Special total noop destruct
*/
/* ARGSUSED */
static void
flowoplib_destruct_noop(flowop_t *flowop)
{
}
/*
* Generates a file attribute from flags in the supplied flowop.
* Sets FLOW_ATTR_DIRECTIO and/or FLOW_ATTR_DSYNC and advise for
* no random read (POSIX_FADV_RANDOM) as needed.
*/
static int
flowoplib_fileattrs(flowop_t *flowop)
{
int attrs = 0;
if (avd_get_bool(flowop->fo_directio))
attrs |= FLOW_ATTR_DIRECTIO;
if (avd_get_bool(flowop->fo_dsync))
attrs |= FLOW_ATTR_DSYNC;
if (avd_get_bool(flowop->fo_noreadahead))
attrs |= FLOW_ATTR_FADV_RANDOM;
return (attrs);
}
/*
* Obtain a filesetentry for a file. Result placed where filep points.
* Supply with a flowop and a flag to indicate whether an existent or
* non-existent file is required. Returns FILEBENCH_NORSC if all out
* of the appropriate type of directories, FILEBENCH_ERROR if the
* flowop does not point to a fileset, and FILEBENCH_OK otherwise.
*/
static int
flowoplib_pickfile(filesetentry_t **filep, flowop_t *flowop, int flags, int tid)
{
fileset_t *fileset;
int fileindex;
if ((fileset = flowop->fo_fileset) == NULL) {
filebench_log(LOG_ERROR, "flowop NO fileset");
return (FILEBENCH_ERROR);
}
if (flowop->fo_fileindex) {
fileindex = (int)(avd_get_dbl(flowop->fo_fileindex));
fileindex = fileindex % fileset->fs_constentries;
flags |= FILESET_PICKBYINDEX;
} else {
fileindex = 0;
}
if ((*filep = fileset_pick(fileset, FILESET_PICKFILE | flags,
tid, fileindex)) == NULL) {
filebench_log(LOG_DEBUG_SCRIPT,
"flowop %s failed to pick file from fileset %s",
flowop->fo_name,
avd_get_str(fileset->fs_name));
return (FILEBENCH_NORSC);
}
return (FILEBENCH_OK);
}
/*
* Obtain a filesetentry for a leaf directory. Result placed where dirp
* points. Supply with flowop and a flag to indicate whether an existent
* or non-existent leaf directory is required. Returns FILEBENCH_NORSC
* if all out of the appropriate type of directories, FILEBENCH_ERROR
* if the flowop does not point to a fileset, and FILEBENCH_OK otherwise.
*/
static int
flowoplib_pickleafdir(filesetentry_t **dirp, flowop_t *flowop, int flags)
{
fileset_t *fileset;
int dirindex;
if ((fileset = flowop->fo_fileset) == NULL) {
filebench_log(LOG_ERROR, "flowop NO fileset");
return (FILEBENCH_ERROR);
}
if (flowop->fo_fileindex) {
dirindex = (int)(avd_get_dbl(flowop->fo_fileindex) *
((double)(fileset->fs_constleafdirs / 2)));
dirindex = dirindex % fileset->fs_constleafdirs;
flags |= FILESET_PICKBYINDEX;
} else {
dirindex = 0;
}
if ((*dirp = fileset_pick(fileset,
FILESET_PICKLEAFDIR | flags, 0, dirindex)) == NULL) {
filebench_log(LOG_DEBUG_SCRIPT,
"flowop %s failed to pick directory from fileset %s",
flowop->fo_name,
avd_get_str(fileset->fs_name));
return (FILEBENCH_NORSC);
}
return (FILEBENCH_OK);
}
/*
* Searches for a file descriptor. Tries the flowop's fo_fdnumber first and
* returns with it if it has been explicitly set (greater than 0). It next
* checks to see if a rotating file descriptor policy is in effect, and if not
* returns the fdnumber regardless of what it is. (note that if it is 0, it
* just selects to the default file descriptor in the threadflow's tf_fd
* array). If the rotating fd policy is in effect, it cycles from the end of
* the tf_fd array to 0 and then starts over from the end.
*
* The routine returns an index into the threadflow's tf_fd table where the
* actual file descriptor will be found.
*/
static int
flowoplib_fdnum(threadflow_t *threadflow, flowop_t *flowop)
{
int fd = flowop->fo_fdnumber;
if (fd > 0) {
filebench_log(LOG_DEBUG_IMPL, "picking explicitly set fd");
goto retfd;
}
if (!avd_get_bool(flowop->fo_rotatefd)) {
filebench_log(LOG_DEBUG_IMPL, "picking default fd");
goto retfd;
}
filebench_log(LOG_DEBUG_IMPL, "picking rotor fd");
/* first time or we wraped around */
if (!threadflow->tf_fdrotor)
threadflow->tf_fdrotor = THREADFLOW_MAXFD;
threadflow->tf_fdrotor--;
fd = threadflow->tf_fdrotor;
retfd:
filebench_log(LOG_DEBUG_IMPL, "picked fd = %d", fd);
return fd;
}
/*
* Determines the file descriptor to use, and attempts to open
* the file if it is not already open. Also determines the wss
* value. Returns FILEBENCH_ERROR on errors, FILESET_NORSC if
* if flowop_openfile_common couldn't obtain an appropriate file
* from a the fileset, and FILEBENCH_OK otherwise.
*/
static int
flowoplib_filesetup(threadflow_t *threadflow, flowop_t *flowop,
fbint_t *wssp, fb_fdesc_t **fdescp)
{
int fd = flowoplib_fdnum(threadflow, flowop);
if (fd == -1)
return (FILEBENCH_ERROR);
/* check for conflicting fdnumber and file name */
if ((fd > 0) && (threadflow->tf_fse[fd] != NULL)) {
char *fd_based_name;
fd_based_name =
avd_get_str(threadflow->tf_fse[fd]->fse_fileset->fs_name);
if (flowop->fo_filename != NULL) {
char *fo_based_name;
fo_based_name = avd_get_str(flowop->fo_filename);
if (strcmp(fd_based_name, fo_based_name) != 0) {
filebench_log(LOG_ERROR, "Name of fd refer"
"enced fileset name (%s) CONFLICTS with"
" flowop supplied fileset name (%s)",
fd_based_name, fo_based_name);
filebench_shutdown(1);
return (FILEBENCH_ERROR);
}
}
}
if (threadflow->tf_fd[fd].fd_ptr == NULL) {
int ret;
if ((ret = flowoplib_openfile_common(
threadflow, flowop, fd)) != FILEBENCH_OK)
return (ret);
if (threadflow->tf_fse[fd]) {
filebench_log(LOG_DEBUG_IMPL, "opened file %s",
threadflow->tf_fse[fd]->fse_path);
} else {
filebench_log(LOG_DEBUG_IMPL,
"opened device %s/%s",
avd_get_str(flowop->fo_fileset->fs_path),
avd_get_str(flowop->fo_fileset->fs_name));
}
}
*fdescp = &(threadflow->tf_fd[fd]);
if ((*wssp = flowop->fo_constwss) == 0) {
if (threadflow->tf_fse[fd])
*wssp = threadflow->tf_fse[fd]->fse_size;
else
*wssp = avd_get_int(flowop->fo_fileset->fs_size);
}
return (FILEBENCH_OK);
}
/*
* Determines the io buffer or random offset into tf_mem for
* the IO operation. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
*/
static int
flowoplib_iobufsetup(threadflow_t *threadflow, flowop_t *flowop,
caddr_t *iobufp, fbint_t iosize)
{
long memsize;
#if defined(_LP64) || (__WORDSIZE == 64)
uint64_t memoffset;
#else
size_t memoffset;
#endif
if (iosize == 0) {
filebench_log(LOG_ERROR, "zero iosize for thread %s",
flowop->fo_name);
return (FILEBENCH_ERROR);
}
/* If directio, we need to align buffer address by sector */
if (flowoplib_fileattrs(flowop) & FLOW_ATTR_DIRECTIO)
iosize = iosize + 512;
if ((memsize = threadflow->tf_constmemsize) != 0) {
/* use tf_mem for I/O with random offset */
if (memsize < iosize) {
filebench_log(LOG_ERROR,
"tf_memsize smaller than IO size for thread %s",
flowop->fo_name);
return (FILEBENCH_ERROR);
}
fb_random(&memoffset, memsize, iosize, NULL);
*iobufp = threadflow->tf_mem + memoffset;
} else {
/* use private I/O buffer */
if ((flowop->fo_buf != NULL) &&
(flowop->fo_buf_size < iosize)) {
/* too small, so free up and re-allocate */
free(flowop->fo_buf);
flowop->fo_buf = NULL;
}
/*
* Allocate memory for the buffer. The memory is freed
* by flowop_destruct_generic() or by this routine if more
* memory is needed for the buffer.
*/
if ((flowop->fo_buf == NULL) && ((flowop->fo_buf
= (char *)malloc(iosize)) == NULL))
return (FILEBENCH_ERROR);
flowop->fo_buf_size = iosize;
*iobufp = flowop->fo_buf;
}
if (flowoplib_fileattrs(flowop) & FLOW_ATTR_DIRECTIO)
*iobufp = (caddr_t)((((unsigned long)(*iobufp) + 512) / 512) * 512);
return (FILEBENCH_OK);
}
/*
* Determines the file descriptor to use, opens it if necessary, the
* io buffer or random offset into tf_mem for IO operation and the wss
* value. Returns FILEBENCH_ERROR on errors, FILEBENCH_OK otherwise.
*/
int
flowoplib_iosetup(threadflow_t *threadflow, flowop_t *flowop,
fbint_t *wssp, caddr_t *iobufp, fb_fdesc_t **filedescp, fbint_t iosize)
{
int ret;
if ((ret = flowoplib_filesetup(threadflow, flowop, wssp, filedescp)) !=
FILEBENCH_OK)
return (ret);
if ((ret = flowoplib_iobufsetup(threadflow, flowop, iobufp, iosize)) !=
FILEBENCH_OK)
return (ret);
return (FILEBENCH_OK);
}
/*
* Emulate posix read / pread. If the flowop has a fileset,
* a file descriptor number index is fetched, otherwise a
* supplied fileobj file is used. In either case the specified
* file will be opened if not already open. If the flowop has
* neither a fileset or fileobj, an error is logged and FILEBENCH_ERROR
* returned.
*
* The actual read is done to a random offset in the
* threadflow's thread memory (tf_mem), with a size set by
* fo_iosize and at either a random disk offset within the
* working set size, or at the next sequential location. If
* any errors are encountered, FILEBENCH_ERROR is returned,
* if no appropriate file can be obtained from the fileset then
* FILEBENCH_NORSC is returned, otherise FILEBENCH_OK is returned.
*/
static int
flowoplib_read(threadflow_t *threadflow, flowop_t *flowop)
{
caddr_t iobuf;
fbint_t wss;
fbint_t iosize;
fb_fdesc_t *fdesc;
int ret;
iosize = avd_get_int(flowop->fo_iosize);
if ((ret = flowoplib_iosetup(threadflow, flowop, &wss, &iobuf,
&fdesc, iosize)) != FILEBENCH_OK)
return (ret);
if (avd_get_bool(flowop->fo_random)) {
uint64_t fileoffset;
if (iosize > wss) {
filebench_log(LOG_ERROR,
"file size smaller than IO size for thread %s",
flowop->fo_name);
return (FILEBENCH_ERROR);
}
/* select randomly */
fb_random64(&fileoffset, wss, iosize, NULL);
(void) flowop_beginop(threadflow, flowop);
if ((ret = FB_PREAD(fdesc, iobuf,
iosize, (off64_t)fileoffset)) == -1) {
(void) flowop_endop(threadflow, flowop, 0);
filebench_log(LOG_ERROR,
"read file %s failed, offset %llu "
"io buffer %zd: %s",
avd_get_str(flowop->fo_fileset->fs_name),
(u_longlong_t)fileoffset, iobuf, strerror(errno));
flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_ERROR);
}
(void) flowop_endop(threadflow, flowop, ret);
if (ret == 0)
(void) FB_LSEEK(fdesc, 0, SEEK_SET);
} else {
(void) flowop_beginop(threadflow, flowop);
if ((ret = FB_READ(fdesc, iobuf, iosize)) == -1) {
(void) flowop_endop(threadflow, flowop, 0);
filebench_log(LOG_ERROR,
"read file %s failed, io buffer %zd: %s",
avd_get_str(flowop->fo_fileset->fs_name),
iobuf, strerror(errno));
(void) flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_ERROR);
}
(void) flowop_endop(threadflow, flowop, ret);
if (ret == 0)
(void) FB_LSEEK(fdesc, 0, SEEK_SET);
}
return (FILEBENCH_OK);
}
/*
* Initializes a "flowop_block" flowop. Specifically, it
* initializes the flowop's fo_cv and unlocks the fo_lock.
*/
static int
flowoplib_block_init(flowop_t *flowop)
{
filebench_log(LOG_DEBUG_IMPL, "flow %s-%d block init address %zx",
flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
(void) pthread_cond_init(&flowop->fo_cv, ipc_condattr());
(void) ipc_mutex_unlock(&flowop->fo_lock);
return (FILEBENCH_OK);
}
/*
* Blocks the threadflow until woken up by flowoplib_wakeup.
* The routine blocks on the flowop's fo_cv condition variable.
*/
static int
flowoplib_block(threadflow_t *threadflow, flowop_t *flowop)
{
filebench_log(LOG_DEBUG_IMPL, "flow %s-%d blocking at address %zx",
flowop->fo_name, flowop->fo_instance, &flowop->fo_cv);
(void) ipc_mutex_lock(&flowop->fo_lock);
flowop_beginop(threadflow, flowop);
(void) pthread_cond_wait(&flowop->fo_cv, &flowop->fo_lock);
flowop_endop(threadflow, flowop, 0);
filebench_log(LOG_DEBUG_IMPL, "flow %s-%d unblocking",
flowop->fo_name, flowop->fo_instance);
(void) ipc_mutex_unlock(&flowop->fo_lock);
return (FILEBENCH_OK);
}
/*
* Wakes up one or more target blocking flowops.
* Sends broadcasts on the fo_cv condition variables of all
* flowops on the target list, except those that are
* FLOW_MASTER flowops. The target list consists of all
* flowops whose name matches this flowop's "fo_targetname"
* attribute. The target list is generated on the first
* invocation, and the run will be shutdown if no targets
* are found. Otherwise the routine always returns FILEBENCH_OK.
*/
static int
flowoplib_wakeup(threadflow_t *threadflow, flowop_t *flowop)
{
flowop_t *target;
/* if this is the first wakeup, create the wakeup list */
if (flowop->fo_targets == NULL) {
flowop_t *result = flowop_find(flowop->fo_targetname);
flowop->fo_targets = result;
if (result == NULL) {
filebench_log(LOG_ERROR,
"wakeup: could not find op %s for thread %s",
flowop->fo_targetname,
threadflow->tf_name);
filebench_shutdown(1);
}
while (result) {
result->fo_targetnext =
result->fo_resultnext;
result = result->fo_resultnext;
}
}
target = flowop->fo_targets;
/* wakeup the targets */
while (target) {
if (target->fo_instance == FLOW_MASTER) {
target = target->fo_targetnext;
continue;
}
filebench_log(LOG_DEBUG_IMPL,
"wakeup flow %s-%d at address %zx",
target->fo_name,
target->fo_instance,
&target->fo_cv);
flowop_beginop(threadflow, flowop);
(void) ipc_mutex_lock(&target->fo_lock);
(void) pthread_cond_broadcast(&target->fo_cv);
(void) ipc_mutex_unlock(&target->fo_lock);
flowop_endop(threadflow, flowop, 0);
target = target->fo_targetnext;
}
return (FILEBENCH_OK);
}
/*
* "think time" routines. the "hog" routine consumes cpu cycles as
* it "thinks", while the "delay" flowop simply calls sleep() to delay
* for a given number of seconds without consuming cpu cycles.
*/
/*
* Consumes CPU cycles and memory bandwidth by looping for
* flowop->fo_value times. With each loop sets memory location
* threadflow->tf_mem to 1.
*/
static int
flowoplib_hog(threadflow_t *threadflow, flowop_t *flowop)
{
uint64_t value = avd_get_int(flowop->fo_value);
int i;
filebench_log(LOG_DEBUG_IMPL, "hog enter");
flowop_beginop(threadflow, flowop);
if (threadflow->tf_mem != NULL) {
for (i = 0; i < value; i++)
*(threadflow->tf_mem) = 1;
}
flowop_endop(threadflow, flowop, 0);
filebench_log(LOG_DEBUG_IMPL, "hog exit");
return (FILEBENCH_OK);
}
/*
* Delays for fo_value seconds.
*/
static int
flowoplib_delay(threadflow_t *threadflow, flowop_t *flowop)
{
int value = avd_get_int(flowop->fo_value);
flowop_beginop(threadflow, flowop);
(void) sleep(value);
flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_OK);
}
/*
* Rate limiting routines. This is the event consuming half of the
* event system. Each of the four following routines will limit the rate
* to one unit of either calls, issued I/O operations, issued filebench
* operations, or I/O bandwidth. Since there is only one event generator,
* the events will be divided amoung multiple instances of an event
* consumer, and further divided among different consumers if more than
* one has been defined. There is no mechanism to enforce equal sharing
* of events.
*/
/*
* Completes one invocation per posted event. If eventgen_q
* has an event count greater than zero, one will be removed
* (count decremented), otherwise the calling thread will
* block until another event has been posted. Always returns 0
*/
static int
flowoplib_eventlimit(threadflow_t *threadflow, flowop_t *flowop)
{
/* Immediately bail if not set/enabled */
if (!filebench_shm->shm_eventgen_enabled)
return (FILEBENCH_OK);
if (flowop->fo_initted == 0) {
filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
flowop, threadflow->tf_name, threadflow->tf_instance);
flowop->fo_initted = 1;
}
flowop_beginop(threadflow, flowop);
while (filebench_shm->shm_eventgen_enabled) {
(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
if (filebench_shm->shm_eventgen_q > 0) {
filebench_shm->shm_eventgen_q--;
(void) ipc_mutex_unlock(
&filebench_shm->shm_eventgen_lock);
break;
}
(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
&filebench_shm->shm_eventgen_lock);
(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
}
flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_OK);
}
static int
flowoplib_event_find_target(threadflow_t *threadflow, flowop_t *flowop)
{
if (flowop->fo_targetname[0] != '\0') {
/* Try to use statistics from specific flowop */
flowop->fo_targets =
flowop_find_from_list(flowop->fo_targetname,
threadflow->tf_thrd_fops);
if (flowop->fo_targets == NULL) {
filebench_log(LOG_ERROR,
"limit target: could not find flowop %s",
flowop->fo_targetname);
filebench_shutdown(1);
return (FILEBENCH_ERROR);
}
} else {
/* use total workload statistics */
flowop->fo_targets = NULL;
}
return (FILEBENCH_OK);
}
/*
* Blocks the calling thread if the number of issued I/O
* operations exceeds the number of posted events, thus
* limiting the average I/O operation rate to the rate
* specified by eventgen_hz. Always returns FILEBENCH_OK.
*/
static int
flowoplib_iopslimit(threadflow_t *threadflow, flowop_t *flowop)
{
uint64_t iops;
uint64_t delta;
uint64_t events;
/* Immediately bail if not set/enabled */
if (!filebench_shm->shm_eventgen_enabled)
return (FILEBENCH_OK);
if (flowop->fo_initted == 0) {
filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
flowop, threadflow->tf_name, threadflow->tf_instance);
flowop->fo_initted = 1;
if (flowoplib_event_find_target(threadflow, flowop)
== FILEBENCH_ERROR)
return (FILEBENCH_ERROR);
if (flowop->fo_targets && ((flowop->fo_targets->fo_attrs &
(FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) {
filebench_log(LOG_ERROR,
"WARNING: Flowop %s does no IO",
flowop->fo_targets->fo_name);
filebench_shutdown(1);
return (FILEBENCH_ERROR);
}
}
if (flowop->fo_targets) {
/*
* Note that fs_count is already the sum of fs_rcount
* and fs_wcount if looking at a single flowop.
*/
iops = flowop->fo_targets->fo_stats.fs_count;
} else {
(void) ipc_mutex_lock(&controlstats_lock);
iops = (controlstats.fs_rcount +
controlstats.fs_wcount);
(void) ipc_mutex_unlock(&controlstats_lock);
}
/* Is this the first time around */
if (flowop->fo_tputlast == 0) {
flowop->fo_tputlast = iops;
return (FILEBENCH_OK);
}
delta = iops - flowop->fo_tputlast;
flowop->fo_tputbucket -= delta;
flowop->fo_tputlast = iops;
/* No need to block if the q isn't empty */
if (flowop->fo_tputbucket >= 0LL) {
flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_OK);
}
iops = flowop->fo_tputbucket * -1;
events = iops;
flowop_beginop(threadflow, flowop);
while (filebench_shm->shm_eventgen_enabled) {
(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
if (filebench_shm->shm_eventgen_q >= events) {
filebench_shm->shm_eventgen_q -= events;
(void) ipc_mutex_unlock(
&filebench_shm->shm_eventgen_lock);
flowop->fo_tputbucket += events;
break;
}
(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
&filebench_shm->shm_eventgen_lock);
(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
}
flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_OK);
}
/*
* Blocks the calling thread if the number of issued filebench
* operations exceeds the number of posted events, thus limiting
* the average filebench operation rate to the rate specified by
* eventgen_hz. Always returns FILEBENCH_OK.
*/
static int
flowoplib_opslimit(threadflow_t *threadflow, flowop_t *flowop)
{
uint64_t ops;
uint64_t delta;
uint64_t events;
/* Immediately bail if not set/enabled */
if (!filebench_shm->shm_eventgen_enabled)
return (FILEBENCH_OK);
if (flowop->fo_initted == 0) {
filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
flowop, threadflow->tf_name, threadflow->tf_instance);
flowop->fo_initted = 1;
if (flowoplib_event_find_target(threadflow, flowop)
== FILEBENCH_ERROR)
return (FILEBENCH_ERROR);
}
if (flowop->fo_targets) {
ops = flowop->fo_targets->fo_stats.fs_count;
} else {
(void) ipc_mutex_lock(&controlstats_lock);
ops = controlstats.fs_count;
(void) ipc_mutex_unlock(&controlstats_lock);
}
/* Is this the first time around */
if (flowop->fo_tputlast == 0) {
flowop->fo_tputlast = ops;
return (FILEBENCH_OK);
}
delta = ops - flowop->fo_tputlast;
flowop->fo_tputbucket -= delta;
flowop->fo_tputlast = ops;
/* No need to block if the q isn't empty */
if (flowop->fo_tputbucket >= 0LL) {
flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_OK);
}
ops = flowop->fo_tputbucket * -1;
events = ops;
flowop_beginop(threadflow, flowop);
while (filebench_shm->shm_eventgen_enabled) {
(void) ipc_mutex_lock(&filebench_shm->shm_eventgen_lock);
if (filebench_shm->shm_eventgen_q >= events) {
filebench_shm->shm_eventgen_q -= events;
(void) ipc_mutex_unlock(
&filebench_shm->shm_eventgen_lock);
flowop->fo_tputbucket += events;
break;
}
(void) pthread_cond_wait(&filebench_shm->shm_eventgen_cv,
&filebench_shm->shm_eventgen_lock);
(void) ipc_mutex_unlock(&filebench_shm->shm_eventgen_lock);
}
flowop_endop(threadflow, flowop, 0);
return (FILEBENCH_OK);
}
/*
* Blocks the calling thread if the number of bytes of I/O
* issued exceeds one megabyte times the number of posted
* events, thus limiting the average I/O byte rate to one
* megabyte times the event rate as set by eventgen_hz.
* Always retuns FILEBENCH_OK.
*/
static int
flowoplib_bwlimit(threadflow_t *threadflow, flowop_t *flowop)
{
uint64_t bytes;
uint64_t delta;
uint64_t events;
/* Immediately bail if not set/enabled */
if (!filebench_shm->shm_eventgen_enabled)
return (FILEBENCH_OK);
if (flowop->fo_initted == 0) {
filebench_log(LOG_DEBUG_IMPL, "rate %zx %s-%d locking",
flowop, threadflow->tf_name, threadflow->tf_instance);
flowop->fo_initted = 1;
if (flowoplib_event_find_target(threadflow, flowop)
== FILEBENCH_ERROR)
return (FILEBENCH_ERROR);
if ((flowop->fo_targets) &&
((flowop->fo_targets->fo_attrs &
(FLOW_ATTR_READ | FLOW_ATTR_WRITE)) == 0)) {
filebench_log(LOG_ERROR,
"WARNING: Flowop %s does no Reads or Writes",
flowop->fo_targets->fo_name);
filebench_shutdown(1);
return (FILEBENCH_ERROR);
}
}
if (flowop->fo_targets) {
/*
* Note that fs_bytes is already the sum of fs_rbytes
* and fs_wbytes if looking at a single flowop.
*/
bytes = flowop->fo_targets->fo_stats.fs_bytes;
} else {
(void) ipc_mutex_lock(&controlstats_lock);
bytes = (controlstats.fs_rbytes +