-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvars.c
1889 lines (1548 loc) · 42 KB
/
vars.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "filebench.h"
#include "vars.h"
#include "misc.h"
#include "utils.h"
#include "stats.h"
#include "eventgen.h"
#include "fb_random.h"
static var_t *var_find_dynamic(char *name);
static boolean_t var_get_bool(var_t *var);
static fbint_t var_get_int(var_t *var);
static double var_get_dbl(var_t *var);
/*
* The filebench variables system has attribute value descriptors (avd_t)
* where an avd contains a boolean, integer, double, string, random
* distribution object ptr, boolean ptr, integer ptr, double ptr,
* string ptr, or variable ptr. The system also has the variables
* themselves, (var_t), which are named, typed entities which can be
* allocated, selected and changed using the "set" command and used in
* attribute assignments. The variables contain either a boolean, an
* integer, a double, a string or pointer to an associated random
* distribution object. Both avd_t and var_t entities are allocated
* from interprocess shared memory space.
*
* The attribute descriptors implement delayed binding to variable values,
* which is necessary because the values of variables may be changed
* between the time the workload file is loaded and it is actually run,
* either by further "set" commands in the file or from the command line
* interface. For random variables, they actually point to the random
* distribution object, allowing Filebench to invoke the appropriate
* random distribution function on each access to the attribute. However,
* for static attributes, the value is just loaded in the descriptor
* directly, avoiding the need to allocate a variable to hold the static
* value.
*
* The routines in this module are used to allocate, locate, and
* manipulate the attribute descriptors, and vars. Routines are
* also included to convert between the component strings, doubles
* and integers of vars, and said components of avd_t.
*/
/*
* returns a pointer to a string indicating the type of data contained
* in the supplied attribute variable descriptor.
*/
static char *
avd_get_type_string(avd_t avd)
{
switch (avd->avd_type) {
case AVD_INVALID:
return ("uninitialized");
case AVD_VAL_BOOL:
return ("boolean value");
case AVD_VARVAL_BOOL:
return ("points to boolean in var_t");
case AVD_VAL_INT:
return ("integer value");
case AVD_VARVAL_INT:
return ("points to integer in var_t");
case AVD_VAL_STR:
return ("string");
case AVD_VARVAL_STR:
return ("points to string in var_t");
case AVD_VAL_DBL:
return ("double float value");
case AVD_VARVAL_DBL:
return ("points to double float in var_t");
case AVD_IND_VAR:
return ("points to a var_t");
case AVD_IND_RANDVAR:
return ("points to var_t's random distribution object");
default:
return ("illegal avd type");
}
}
/*
* returns a pointer to a string indicating the type of data contained
* in the supplied variable.
*/
static char *
var_get_type_string(var_t *ivp)
{
switch (ivp->var_type & VAR_TYPE_SET_MASK) {
case VAR_TYPE_BOOL_SET:
return ("boolean");
case VAR_TYPE_INT_SET:
return ("integer");
case VAR_TYPE_STR_SET:
return ("string");
case VAR_TYPE_DBL_SET:
return ("double float");
case VAR_TYPE_RAND_SET:
return ("random");
default:
return ("empty");
}
}
/*
* Returns the fbint_t pointed to by the supplied avd_t "avd".
*/
fbint_t
avd_get_int(avd_t avd)
{
randdist_t *rndp;
if (avd == NULL)
return (0);
switch (avd->avd_type) {
case AVD_VAL_INT:
return (avd->avd_val.intval);
case AVD_VARVAL_INT:
if (avd->avd_val.intptr)
return (*(avd->avd_val.intptr));
else
return (0);
case AVD_IND_VAR:
return (var_get_int(avd->avd_val.varptr));
case AVD_IND_RANDVAR:
if ((rndp = avd->avd_val.randptr) == NULL)
return (0);
else
return ((fbint_t)rndp->rnd_get(rndp));
default:
filebench_log(LOG_ERROR,
"Attempt to get integer from %s avd",
avd_get_type_string(avd));
return (0);
}
}
/*
* Returns the floating point value of a variable pointed to by the
* supplied avd_t "avd". Intended to get the actual (double) value
* supplied by the random variable.
*/
double
avd_get_dbl(avd_t avd)
{
randdist_t *rndp;
if (avd == NULL)
return (0.0);
switch (avd->avd_type) {
case AVD_VAL_INT:
return ((double)avd->avd_val.intval);
case AVD_VAL_DBL:
return (avd->avd_val.dblval);
case AVD_VARVAL_INT:
if (avd->avd_val.intptr)
return ((double)(*(avd->avd_val.intptr)));
else
return (0.0);
case AVD_VARVAL_DBL:
if (avd->avd_val.dblptr)
return (*(avd->avd_val.dblptr));
else
return (0.0);
case AVD_IND_VAR:
return (var_get_dbl(avd->avd_val.varptr));
case AVD_IND_RANDVAR:
if ((rndp = avd->avd_val.randptr) == NULL) {
return (0.0);
} else
return (rndp->rnd_get(rndp));
default:
filebench_log(LOG_ERROR,
"Attempt to get floating point from %s avd",
avd_get_type_string(avd));
return (0.0);
}
}
/*
* Returns the boolean pointed to by the supplied avd_t "avd".
*/
boolean_t
avd_get_bool(avd_t avd)
{
if (avd == NULL)
return (0);
switch (avd->avd_type) {
case AVD_VAL_BOOL:
return (avd->avd_val.boolval);
case AVD_VARVAL_BOOL:
if (avd->avd_val.boolptr)
return (*(avd->avd_val.boolptr));
else
return (FALSE);
/* for backwards compatibility with old workloads */
case AVD_VAL_INT:
if (avd->avd_val.intval != 0)
return (TRUE);
else
return (FALSE);
case AVD_VARVAL_INT:
if (avd->avd_val.intptr)
if (*(avd->avd_val.intptr) != 0)
return (TRUE);
return (FALSE);
case AVD_IND_VAR:
return (var_get_bool(avd->avd_val.varptr));
default:
filebench_log(LOG_ERROR,
"Attempt to get boolean from %s avd",
avd_get_type_string(avd));
return (FALSE);
}
}
/*
* Returns the string pointed to by the supplied avd_t "avd".
*/
char *
avd_get_str(avd_t avd)
{
var_t *ivp;
if (avd == NULL)
return (NULL);
switch (avd->avd_type) {
case AVD_VAL_STR:
return (avd->avd_val.strval);
case AVD_VARVAL_STR:
if (avd->avd_val.strptr)
return (*avd->avd_val.strptr);
else
return (NULL);
case AVD_IND_VAR:
ivp = avd->avd_val.varptr;
if (ivp && VAR_HAS_STRING(ivp))
return (ivp->var_val.string);
filebench_log(LOG_ERROR,
"Attempt to get string from %s var $%s",
var_get_type_string(ivp), ivp->var_name);
return (NULL);
default:
filebench_log(LOG_ERROR,
"Attempt to get string from %s avd",
avd_get_type_string(avd));
return (NULL);
}
}
/*
* Allocates a avd_t from ipc memory space.
* logs an error and returns NULL on failure.
*/
static avd_t
avd_alloc_cmn(void)
{
avd_t rtn;
if ((rtn = (avd_t)ipc_malloc(FILEBENCH_AVD)) == NULL)
filebench_log(LOG_ERROR, "Avd alloc failed");
return (rtn);
}
/*
* pre-loads the allocated avd_t with the boolean_t "bool".
* Returns the avd_t on success, NULL on failure.
*/
avd_t
avd_bool_alloc(boolean_t bool)
{
avd_t avd;
if ((avd = avd_alloc_cmn()) == NULL)
return (NULL);
avd->avd_type = AVD_VAL_BOOL;
avd->avd_val.boolval = bool;
filebench_log(LOG_DEBUG_IMPL, "Alloc boolean %d", bool);
return (avd);
}
/*
* pre-loads the allocated avd_t with the fbint_t "integer".
* Returns the avd_t on success, NULL on failure.
*/
avd_t
avd_int_alloc(fbint_t integer)
{
avd_t avd;
if ((avd = avd_alloc_cmn()) == NULL)
return (NULL);
avd->avd_type = AVD_VAL_INT;
avd->avd_val.intval = integer;
filebench_log(LOG_DEBUG_IMPL, "Alloc integer %llu",
(u_longlong_t)integer);
return (avd);
}
/*
* Gets a avd_t and points it to the var that
* it will eventually be filled from
*/
static avd_t
avd_alloc_var_ptr(var_t *var)
{
avd_t avd;
if (var == NULL)
return (NULL);
if ((avd = avd_alloc_cmn()) == NULL)
return (NULL);
switch (var->var_type & VAR_TYPE_SET_MASK) {
case VAR_TYPE_BOOL_SET:
avd->avd_type = AVD_VARVAL_BOOL;
avd->avd_val.boolptr = (&var->var_val.boolean);
break;
case VAR_TYPE_INT_SET:
avd->avd_type = AVD_VARVAL_INT;
avd->avd_val.intptr = (&var->var_val.integer);
break;
case VAR_TYPE_STR_SET:
avd->avd_type = AVD_VARVAL_STR;
avd->avd_val.strptr = &(var->var_val.string);
break;
case VAR_TYPE_DBL_SET:
avd->avd_type = AVD_VARVAL_DBL;
avd->avd_val.dblptr = &(var->var_val.dbl_flt);
break;
case VAR_TYPE_RAND_SET:
avd->avd_type = AVD_IND_RANDVAR;
avd->avd_val.randptr = var->var_val.randptr;
break;
case VAR_TYPE_INDVAR_SET:
avd->avd_type = AVD_IND_VAR;
if ((var->var_type & VAR_INDVAR_MASK) == VAR_IND_ASSIGN)
avd->avd_val.varptr = var->var_varptr1;
else
avd->avd_val.varptr = var;
break;
default:
avd->avd_type = AVD_IND_VAR;
avd->avd_val.varptr = var;
break;
}
return (avd);
}
/*
* Gets a avd_t, then allocates and initializes a piece of
* shared string memory, putting the pointer to it into the just
* allocated string pointer location. The routine returns a pointer
* to the string pointer location or returns NULL on error.
*/
avd_t
avd_str_alloc(char *string)
{
avd_t avd;
if (string == NULL) {
filebench_log(LOG_ERROR, "No string supplied\n");
return (NULL);
}
if ((avd = avd_alloc_cmn()) == NULL)
return (NULL);
avd->avd_type = AVD_VAL_STR;
avd->avd_val.strval = ipc_stralloc(string);
filebench_log(LOG_DEBUG_IMPL,
"Alloc string %s ptr %zx",
string, avd);
return (avd);
}
/*
* Allocates a var (var_t) from interprocess shared memory.
* Places the allocated var on the end of the globally shared
* shm_var_list. Finally, the routine allocates a string containing
* a copy of the supplied "name" string. If any allocations
* fails, returns NULL, otherwise it returns a pointer to the
* newly allocated var.
*/
static var_t *
var_alloc_cmn(char *name, int var_type)
{
var_t **var_listp;
var_t *var = NULL;
var_t *prev = NULL;
var_t *newvar;
if ((newvar = (var_t *)ipc_malloc(FILEBENCH_VARIABLE)) == NULL) {
filebench_log(LOG_ERROR, "Out of memory for variables");
return (NULL);
}
(void) memset(newvar, 0, sizeof (newvar));
newvar->var_type = var_type;
if ((newvar->var_name = ipc_stralloc(name)) == NULL) {
filebench_log(LOG_ERROR, "Out of memory for variables");
return (NULL);
}
switch (var_type & VAR_TYPE_MASK) {
case VAR_TYPE_RANDOM:
case VAR_TYPE_GLOBAL:
var_listp = &filebench_shm->shm_var_list;
break;
case VAR_TYPE_DYNAMIC:
var_listp = &filebench_shm->shm_var_dyn_list;
break;
case VAR_TYPE_LOCAL:
/* place on head of shared local list */
newvar->var_next = filebench_shm->shm_var_loc_list;
filebench_shm->shm_var_loc_list = newvar;
return (newvar);
default:
var_listp = &filebench_shm->shm_var_list;
break;
}
/* add to the end of list */
for (var = *var_listp; var != NULL; var = var->var_next)
prev = var; /* Find end of list */
if (prev != NULL)
prev->var_next = newvar;
else
*var_listp = newvar;
return (newvar);
}
/*
* Allocates a var (var_t) from interprocess shared memory after
* first adjusting the name to elminate the leading $. Places the
* allocated var temporarily on the end of the globally
* shared var_loc_list. If the allocation fails, returns NULL,
* otherwise it returns a pointer to the newly allocated var.
*/
var_t *
var_lvar_alloc_local(char *name)
{
if (name[0] == '$')
name += 1;
return (var_alloc_cmn(name, VAR_TYPE_LOCAL));
}
/*
* Allocates a var (var_t) from interprocess shared memory and
* places the allocated var on the end of the globally shared
* shm_var_list. If the allocation fails, returns NULL, otherwise
* it returns a pointer to the newly allocated var.
*/
static var_t *
var_alloc(char *name)
{
return (var_alloc_cmn(name, VAR_TYPE_GLOBAL));
}
/*
* Allocates a var (var_t) from interprocess shared memory.
* Places the allocated var on the end of the globally shared
* shm_var_dyn_list. If the allocation fails, returns NULL, otherwise
* it returns a pointer to the newly allocated var.
*/
static var_t *
var_alloc_dynamic(char *name)
{
return (var_alloc_cmn(name, VAR_TYPE_DYNAMIC));
}
/*
* Searches for var_t with name "name" in the shm_var_loc_list,
* then, if not found, in the global shm_var_list. If a matching
* local or global var is found, returns a pointer to the var_t,
* otherwise returns NULL.
*/
static var_t *
var_find(char *name)
{
var_t *var;
for (var = filebench_shm->shm_var_loc_list; var != NULL;
var = var->var_next) {
if (strcmp(var->var_name, name) == 0)
return (var);
}
for (var = filebench_shm->shm_var_list; var != NULL;
var = var->var_next) {
if (strcmp(var->var_name, name) == 0)
return (var);
}
return (NULL);
}
/*
* Searches for var_t with name "name" in the supplied shm_var_list.
* If not found there, checks the global list. If still
* unsuccessful, returns NULL. Otherwise returns a pointer to the var_t.
*/
static var_t *
var_find_list_only(char *name, var_t *var_list)
{
var_t *var;
for (var = var_list; var != NULL; var = var->var_next) {
if (strcmp(var->var_name, name) == 0)
return (var);
}
return (NULL);
}
/*
* Searches for var_t with name "name" in the supplied shm_var_list.
* If not found there, checks the global list. If still
* unsuccessful, returns NULL. Otherwise returns a pointer to the var_t.
*/
static var_t *
var_find_list(char *name, var_t *var_list)
{
var_t *var;
if ((var = var_find_list_only(name, var_list)) != NULL)
return (var);
else
return (var_find(name));
}
/*
* Searches for the named var and returns it if found. If not
* found it allocates a new variable
*/
static var_t *
var_find_alloc(char *name)
{
var_t *var;
if (name == NULL) {
filebench_log(LOG_ERROR,
"var_find_alloc: Var name not supplied");
return (NULL);
}
name += 1;
if ((var = var_find(name)) == NULL) {
var = var_alloc(name);
}
return (var);
}
/*
* Searches for the named var, and, if found, sets its
* var_val.boolean's value to that of the supplied boolean.
* If not found, the routine allocates a new var and sets
* its var_val.boolean's value to that of the supplied
* boolean. If the named var cannot be found or allocated
* the routine returns -1, otherwise it returns 0.
*/
int
var_assign_boolean(char *name, boolean_t bool)
{
var_t *var;
if ((var = var_find_alloc(name)) == NULL) {
filebench_log(LOG_ERROR, "Cannot assign variable %s",
name);
return (-1);
}
if ((var->var_type & VAR_TYPE_MASK) == VAR_TYPE_RANDOM) {
filebench_log(LOG_ERROR,
"Cannot assign integer to random variable %s", name);
return (-1);
}
VAR_SET_BOOL(var, bool);
filebench_log(LOG_DEBUG_SCRIPT, "Assign boolean %s=%d",
name, bool);
return (0);
}
/*
* Searches for the named var, and, if found, sets its
* var_integer's value to that of the supplied integer.
* If not found, the routine allocates a new var and sets
* its var_integers's value to that of the supplied
* integer. If the named var cannot be found or allocated
* the routine returns -1, otherwise it returns 0.
*/
int
var_assign_integer(char *name, fbint_t integer)
{
var_t *var;
if ((var = var_find_alloc(name)) == NULL) {
filebench_log(LOG_ERROR, "Cannot assign variable %s",
name);
return (-1);
}
if ((var->var_type & VAR_TYPE_MASK) == VAR_TYPE_RANDOM) {
filebench_log(LOG_ERROR,
"Cannot assign integer to random variable %s", name);
return (-1);
}
VAR_SET_INT(var, integer);
filebench_log(LOG_DEBUG_SCRIPT, "Assign integer %s=%llu",
name, (u_longlong_t)integer);
return (0);
}
/*
* Add, subtract, multiply or divide two integers based on optype
* passed from caller.
*/
static fbint_t
var_binary_integer_op(var_t *var)
{
fbint_t result;
fbint_t src1, src2 = 0;
if (var == NULL)
return (0);
switch (var->var_type & VAR_INDBINOP_MASK) {
case VAR_IND_BINOP_INT:
src2 = var->var_val.integer;
break;
case VAR_IND_BINOP_DBL:
src2 = (fbint_t)var->var_val.dbl_flt;
break;
case VAR_IND_BINOP_VAR:
if (var->var_val.varptr2 != NULL)
src2 = var_get_int(var->var_val.varptr2);
else
src2 = 0;
break;
}
if (var->var_varptr1 != NULL)
src1 = var_get_int(var->var_varptr1);
else
src1 = 0;
switch (var->var_type & VAR_INDVAR_MASK) {
case VAR_IND_VAR_SUM_VC:
result = src1 + src2;
break;
case VAR_IND_VAR_DIF_VC:
result = src1 - src2;
break;
case VAR_IND_C_DIF_VAR:
result = src2 - src1;
break;
case VAR_IND_VAR_MUL_VC:
result = src1 * src2;
break;
case VAR_IND_VAR_DIV_VC:
result = src1 / src2;
break;
case VAR_IND_C_DIV_VAR:
result = src2 / src1;
break;
default:
filebench_log(LOG_DEBUG_IMPL,
"var_binary_integer_op: Called with unknown IND_TYPE");
result = 0;
break;
}
return (result);
}
/*
* Add, subtract, multiply or divide two double precision floating point
* numbers based on optype passed from caller.
*/
static double
var_binary_dbl_flt_op(var_t *var)
{
double result;
double src1, src2 = 0;
if (var == NULL)
return (0.0);
switch (var->var_type & VAR_INDBINOP_MASK) {
case VAR_IND_BINOP_INT:
src2 = (double)var->var_val.integer;
break;
case VAR_IND_BINOP_DBL:
src2 = var->var_val.dbl_flt;
break;
case VAR_IND_BINOP_VAR:
if (var->var_val.varptr2 != NULL)
src2 = var_get_dbl(var->var_val.varptr2);
else
src2 = 0;
break;
}
if (var->var_varptr1 != NULL)
src1 = var_get_dbl(var->var_varptr1);
else
src1 = 0;
switch (var->var_type & VAR_INDVAR_MASK) {
case VAR_IND_VAR_SUM_VC:
result = src1 + src2;
break;
case VAR_IND_VAR_DIF_VC:
result = src1 - src2;
break;
case VAR_IND_C_DIF_VAR:
result = src2 - src1;
break;
case VAR_IND_VAR_MUL_VC:
result = src1 * src2;
break;
case VAR_IND_C_DIV_VAR:
result = src2 / src1;
break;
case VAR_IND_VAR_DIV_VC:
result = src1 / src2;
break;
default:
filebench_log(LOG_DEBUG_IMPL,
"var_binary_dbl_flt_op: Called with unknown IND_TYPE");
result = 0;
break;
}
return (result);
}
/*
* Perform a binary operation on a variable and an integer
*/
int
var_assign_op_var_int(char *name, int optype, char *src1, fbint_t src2)
{
var_t *var;
var_t *var_src1;
if ((var_src1 = var_find(src1+1)) == NULL)
return (FILEBENCH_ERROR);
if ((var = var_find_alloc(name)) == NULL)
return (FILEBENCH_ERROR);
if ((var->var_type & VAR_TYPE_MASK) == VAR_TYPE_RANDOM) {
filebench_log(LOG_ERROR,
"Cannot assign integer to random variable %s", name);
return (FILEBENCH_ERROR);
}
VAR_SET_BINOP_INDVAR(var, var_src1, optype);
var->var_val.integer = src2;
return (FILEBENCH_OK);
}
int
var_assign_op_var_var(char *name, int optype, char *src1, char *src2)
{
var_t *var;
var_t *var_src1;
var_t *var_src2;
if ((var_src1 = var_find(src1+1)) == NULL)
return (FILEBENCH_ERROR);
if ((var_src2 = var_find(src2+1)) == NULL)
return (FILEBENCH_ERROR);
if ((var = var_find_alloc(name)) == NULL)
return (FILEBENCH_ERROR);
if ((var->var_type & VAR_TYPE_MASK) == VAR_TYPE_RANDOM) {
filebench_log(LOG_ERROR,
"Cannot assign integer to random variable %s", name);
return (FILEBENCH_ERROR);
}
VAR_SET_BINOP_INDVAR(var, var_src1, optype);
var->var_val.varptr2 = var_src2;
return (FILEBENCH_OK);
}
/*
* Find a variable, and set it to random type.
* If it does not have a random extension, allocate one
*/
var_t *
var_find_randvar(char *name)
{
var_t *newvar;
name += 1;
if ((newvar = var_find(name)) == NULL) {
filebench_log(LOG_ERROR,
"failed to locate random variable $%s\n", name);
return (NULL);
}
/* set randdist pointer unless it is already set */
if (((newvar->var_type & VAR_TYPE_MASK) != VAR_TYPE_RANDOM) ||
!VAR_HAS_RANDDIST(newvar)) {
filebench_log(LOG_ERROR,
"Found variable $%s not random\n", name);
return (NULL);
}
return (newvar);
}
/*
* Allocate a variable, and set it to random type. Then
* allocate a random extension.
*/
var_t *
var_define_randvar(char *name)
{
var_t *newvar;
randdist_t *rndp = NULL;
name += 1;
/* make sure variable doesn't already exist */
if (var_find(name) != NULL) {
filebench_log(LOG_ERROR,
"variable name already in use\n");
return (NULL);
}
/* allocate a random variable */
if ((newvar = var_alloc_cmn(name, VAR_TYPE_RANDOM)) == NULL) {
filebench_log(LOG_ERROR,
"failed to alloc random variable\n");
return (NULL);
}
/* set randdist pointer */
if ((rndp = randdist_alloc()) == NULL) {
filebench_log(LOG_ERROR,
"failed to alloc random distribution object\n");
return (NULL);
}
rndp->rnd_var = newvar;
VAR_SET_RAND(newvar, rndp);
return (newvar);
}
/*
* Searches for the named var, and if found returns an avd_t
* pointing to the var's var_integer, var_string or var_double
* as appropriate. If not found, attempts to allocate
* a var named "name" and returns an avd_t to it with
* no value set. If the var cannot be found or allocated, an
* error is logged and the run is terminated.
*/
avd_t
var_ref_attr(char *name)
{
var_t *var;
name += 1;
if ((var = var_find(name)) == NULL)
var = var_find_dynamic(name);
if (var == NULL)
var = var_alloc(name);
if (var == NULL) {
filebench_log(LOG_ERROR, "Invalid variable $%s",
name);
filebench_shutdown(1);
}