summaryrefslogtreecommitdiff
path: root/usr/src/cmd/filebench/common/vars.c
blob: 3be0891bd2555f8583ef7853989b72dffcc61f97 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
/*
 * 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;

	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;

	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);
	}

	/* allocate pointer to var and return */
	return (avd_alloc_var_ptr(var));
}

/*
 * Converts the contents of a var to a string
 */
static char *
var_get_string(var_t *var)
{

	if ((var->var_type & VAR_TYPE_MASK) == VAR_TYPE_RANDOM) {
		switch (var->var_val.randptr->rnd_type & RAND_TYPE_MASK) {
		case RAND_TYPE_UNIFORM:
			return (fb_stralloc("uniform random var"));
		case RAND_TYPE_GAMMA:
			return (fb_stralloc("gamma random var"));
		case RAND_TYPE_TABLE:
			return (fb_stralloc("tabular random var"));
		default:
			return (fb_stralloc("unitialized random var"));
		}
	}

	if (VAR_HAS_STRING(var) && var->var_val.string)
		return (fb_stralloc(var->var_val.string));

	if (VAR_HAS_BOOLEAN(var)) {
		if (var->var_val.boolean)
			return (fb_stralloc("true"));
		else
			return (fb_stralloc("false"));
	}

	if (VAR_HAS_INTEGER(var)) {
		char tmp[128];

		(void) snprintf(tmp, sizeof (tmp), "%llu",
		    (u_longlong_t)var->var_val.integer);
		return (fb_stralloc(tmp));
	}

	if (VAR_HAS_INDVAR(var)) {
		var_t *ivp;

		if ((ivp = var->var_varptr1) != NULL) {
			return (var_get_string(ivp));
		}
	}

	if (VAR_HAS_BINOP(var)) {
		char tmp[128];

		(void) snprintf(tmp, sizeof (tmp), "%llu",
		    var_binary_integer_op(var));
		return (fb_stralloc(tmp));
	}

	return (fb_stralloc("No default"));
}

/*
 * Searches for the named var, and if found copies the var_val.string,
 * if it exists, a decimal number string representation of
 * var_val.integer, the state of var_val.boolean, or the type of random
 * distribution employed, into a malloc'd bit of memory using fb_stralloc().
 * Returns a pointer to the created string, or NULL on failure.
 */
char *
var_to_string(char *name)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_find_dynamic(name);

	if (var == NULL)
		return (NULL);

	return (var_get_string(var));
}

/*
 * Returns the boolean from the supplied var_t "var".
 */
static boolean_t
var_get_bool(var_t *var)
{
	if (var == NULL)
		return (0);

	if (VAR_HAS_BOOLEAN(var))
		return (var->var_val.boolean);

	if (VAR_HAS_INTEGER(var)) {
		if (var->var_val.integer == 0)
			return (FALSE);
		else
			return (TRUE);
	}

	filebench_log(LOG_ERROR,
	    "Attempt to get boolean from %s var $%s",
	    var_get_type_string(var), var->var_name);
	return (FALSE);
}

/*
 * Returns the fbint_t from the supplied var_t "var".
 */
static fbint_t
var_get_int(var_t *var)
{
	randdist_t *rndp;

	if (var == NULL)
		return (0);

	if (VAR_HAS_INTEGER(var))
		return (var->var_val.integer);

	if (VAR_HAS_RANDDIST(var)) {
		if ((rndp = var->var_val.randptr) != NULL)
			return ((fbint_t)rndp->rnd_get(rndp));
	}

	if (VAR_HAS_BINOP(var))
		return (var_binary_integer_op(var));

	filebench_log(LOG_ERROR,
	    "Attempt to get integer from %s var $%s",
	    var_get_type_string(var), var->var_name);
	return (0);
}

/*
 * Returns the floating point value of a variable pointed to by the
 * supplied var_t "var". Intended to get the actual (double) value
 * supplied by the random variable.
 */
static double
var_get_dbl(var_t *var)
{
	randdist_t *rndp;

	if (var == NULL)
		return (0.0);

	if (VAR_HAS_INTEGER(var))
		return ((double)var->var_val.integer);

	if (VAR_HAS_DOUBLE(var))
		return (var->var_val.dbl_flt);

	if (VAR_HAS_RANDDIST(var)) {
		if ((rndp = var->var_val.randptr) != NULL)
			return (rndp->rnd_get(rndp));
	}

	if (VAR_HAS_BINOP(var))
		return (var_binary_dbl_flt_op(var));

	filebench_log(LOG_ERROR,
	    "Attempt to get double float from %s var $%s",
	    var_get_type_string(var), var->var_name);
	return (0.0);
}

/*
 * Searches for the named var, and if found returns the value,
 * of var_val.boolean. If the var is not found, or a boolean
 * value has not been set, logs an error and returns 0.
 */
boolean_t
var_to_boolean(char *name)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_find_dynamic(name);

	if (var != NULL)
		return (var_get_bool(var));

	filebench_log(LOG_ERROR,
	    "Variable %s referenced before set", name);

	return (FALSE);
}

/*
 * Searches for the named var, and if found returns the value,
 * of var_val.integer. If the var is not found, or the an
 * integer value has not been set, logs an error and returns 0.
 */
fbint_t
var_to_integer(char *name)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_find_dynamic(name);

	if (var != NULL)
		return (var_get_int(var));

	filebench_log(LOG_ERROR,
	    "Variable %s referenced before set", name);

	return (0);
}

/*
 * Searches for the named var, and if found returns the value,
 * of var_val.dbl_flt. If the var is not found, or the
 * floating value has not been set, logs an error and returns 0.0.
 */
double
var_to_double(char *name)
{
	var_t *var;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_find_dynamic(name);

	if (var != NULL)
		return (var_get_dbl(var));

	filebench_log(LOG_ERROR,
	    "Variable %s referenced before set", name);

	return (0.0);
}

/*
 * Searches for the named random var, and if found, converts the
 * requested parameter into a string or a decimal number string
 * representation, into a malloc'd bit of memory using fb_stralloc().
 * Returns a pointer to the created string, or calls var_to_string()
 * if a random variable isn't found.
 */
char *
var_randvar_to_string(char *name, int param_name)
{
	var_t *var;
	fbint_t value;

	if ((var = var_find(name + 1)) == NULL)
		return (var_to_string(name));

	if (((var->var_type & VAR_TYPE_MASK) != VAR_TYPE_RANDOM) ||
	    !VAR_HAS_RANDDIST(var))
		return (var_to_string(name));

	switch (param_name) {
	case RAND_PARAM_TYPE:
		switch (var->var_val.randptr->rnd_type & RAND_TYPE_MASK) {
		case RAND_TYPE_UNIFORM:
			return (fb_stralloc("uniform"));
		case RAND_TYPE_GAMMA:
			return (fb_stralloc("gamma"));
		case RAND_TYPE_TABLE:
			return (fb_stralloc("tabular"));
		default:
			return (fb_stralloc("uninitialized"));
		}

	case RAND_PARAM_SRC:
		if (var->var_val.randptr->rnd_type & RAND_SRC_GENERATOR)
			return (fb_stralloc("rand48"));
		else
			return (fb_stralloc("urandom"));

	case RAND_PARAM_SEED:
		value = avd_get_int(var->var_val.randptr->rnd_seed);
		break;

	case RAND_PARAM_MIN:
		value = avd_get_int(var->var_val.randptr->rnd_min);
		break;

	case RAND_PARAM_MEAN:
		value = avd_get_int(var->var_val.randptr->rnd_mean);
		break;

	case RAND_PARAM_GAMMA:
		value = avd_get_int(var->var_val.randptr->rnd_gamma);
		break;

	case RAND_PARAM_ROUND:
		value = avd_get_int(var->var_val.randptr->rnd_round);
		break;

	default:
		return (NULL);

	}

	/* just an integer value if we got here */
	{
		char tmp[128];

		(void) snprintf(tmp, sizeof (tmp), "%llu",
		    (u_longlong_t)value);
		return (fb_stralloc(tmp));
	}
}

/*
 * Copies the value stored in the source string into the destination
 * string. Returns -1 if any problems encountered, 0 otherwise.
 */
static int
var_copy(var_t *dst_var, var_t *src_var) {

	if (VAR_HAS_BOOLEAN(src_var)) {
		VAR_SET_BOOL(dst_var, src_var->var_val.boolean);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var %s=%s", dst_var->var_name,
		    dst_var->var_val.boolean?"true":"false");
	}

	if (VAR_HAS_INTEGER(src_var)) {
		VAR_SET_INT(dst_var, src_var->var_val.integer);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var %s=%llu", dst_var->var_name,
		    (u_longlong_t)dst_var->var_val.integer);
	}

	if (VAR_HAS_DOUBLE(src_var)) {
		VAR_SET_DBL(dst_var, src_var->var_val.dbl_flt);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var %s=%lf", dst_var->var_name,
		    dst_var->var_val.dbl_flt);
	}

	if (VAR_HAS_STRING(src_var)) {
		char *strptr;

		if ((strptr =
		    ipc_stralloc(src_var->var_val.string)) == NULL) {
			filebench_log(LOG_ERROR,
			    "Cannot assign string for variable %s",
			    dst_var->var_name);
			return (-1);
		}
		VAR_SET_STR(dst_var, strptr);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var %s=%s", dst_var->var_name,
		    dst_var->var_val.string);
	}

	if (VAR_HAS_INDVAR(src_var)) {
		VAR_SET_INDVAR(dst_var, src_var->var_varptr1);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var %s to var %s", dst_var->var_name,
		    src_var->var_name);
	}
	return (0);
}

/*
 * Searches for the var named "name", and if not found
 * allocates it. The then copies the value from
 * the src_var into the destination var "name"
 * If the var "name" cannot be found or allocated, or the var "src_name"
 * cannot be found, the routine returns -1, otherwise it returns 0.
 */
int
var_assign_var(char *name, char *src_name)
{
	var_t *dst_var, *src_var;

	name += 1;
	src_name += 1;

	if ((src_var = var_find(src_name)) == NULL) {
		filebench_log(LOG_ERROR,
		    "Cannot find source variable %s", src_name);
		return (-1);
	}

	if ((dst_var = var_find(name)) == NULL)
		dst_var = var_alloc(name);

	if (dst_var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (-1);
	}

	if ((dst_var->var_type & VAR_TYPE_MASK) == VAR_TYPE_RANDOM) {
		filebench_log(LOG_ERROR,
		    "Cannot assign var to Random variable %s", name);
		return (-1);
	}

	return (var_copy(dst_var, src_var));
}

/*
 * Like var_assign_integer, only this routine copies the
 * supplied "string" into the var named "name". If the var
 * named "name" cannot be found then it is first allocated
 * before the copy. Space for the string in the var comes
 * from interprocess shared memory. If the var "name"
 * cannot be found or allocated, or the memory for the
 * var_val.string copy of "string" cannot be allocated, the
 * routine returns -1, otherwise it returns 0.
 */
int
var_assign_string(char *name, char *string)
{
	var_t *var;
	char *strptr;

	name += 1;

	if ((var = var_find(name)) == NULL)
		var = var_alloc(name);

	if (var == 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 string to random variable %s", name);
		return (-1);
	}

	if ((strptr = ipc_stralloc(string)) == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (-1);
	}
	VAR_SET_STR(var, strptr);

	filebench_log(LOG_DEBUG_SCRIPT,
	    "Var assign string $%s=%s", name, string);

	return (0);
}

/*
 * Allocates a local var. The then extracts the var_string from
 * the var named "string" and copies it into the var_string
 * of the var "name", after first allocating a piece of
 * interprocess shared string memory. Returns a pointer to the
 * newly allocated local var or NULL on error.
 */
var_t *
var_lvar_assign_var(char *name, char *src_name)
{
	var_t *dst_var, *src_var;

	src_name += 1;

	if ((src_var = var_find(src_name)) == NULL) {
		filebench_log(LOG_ERROR,
		    "Cannot find source variable %s", src_name);
		return (NULL);
	}

	dst_var = var_lvar_alloc_local(name);

	if (dst_var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (NULL);
	}

	/*
	 * if referencing another local var which is currently
	 * empty, indirect to it
	 */
	if ((src_var->var_type & VAR_TYPE_MASK) == VAR_TYPE_LOCAL) {
		VAR_SET_INDVAR(dst_var, src_var);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign local var %s to %s", name, src_name);
		return (dst_var);
	}

	if (VAR_HAS_BOOLEAN(src_var)) {
		VAR_SET_BOOL(dst_var, src_var->var_val.boolean);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var (%s, %p)=%s", name,
		    dst_var, src_var->var_val.boolean?"true":"false");
	} else if (VAR_HAS_INTEGER(src_var)) {
		VAR_SET_INT(dst_var, src_var->var_val.integer);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var (%s, %p)=%llu", name,
		    dst_var, (u_longlong_t)src_var->var_val.integer);
	} else if (VAR_HAS_STRING(src_var)) {
		char *strptr;

		if ((strptr = ipc_stralloc(src_var->var_val.string)) == NULL) {
			filebench_log(LOG_ERROR,
			    "Cannot assign variable %s",
			    name);
			return (NULL);
		}
		VAR_SET_STR(dst_var, strptr);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var (%s, %p)=%s", name,
		    dst_var, src_var->var_val.string);
	} else if (VAR_HAS_DOUBLE(src_var)) {
		/* LINTED E_ASSIGMENT_CAUSE_LOSS_PREC */
		VAR_SET_INT(dst_var, src_var->var_val.dbl_flt);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var (%s, %p)=%8.2f", name,
		    dst_var, src_var->var_val.dbl_flt);
	} else if (VAR_HAS_RANDDIST(src_var)) {
		VAR_SET_RAND(dst_var, src_var->var_val.randptr);
		filebench_log(LOG_DEBUG_SCRIPT,
		    "Assign var (%s, %p)=%llu", name,
		    dst_var, (u_longlong_t)src_var->var_val.integer);
	}

	return (dst_var);
}

/*
 * the routine allocates a new local var and sets
 * its var_boolean's value to that of the supplied
 * boolean. It returns a pointer to the new local var
 */
var_t *
var_lvar_assign_boolean(char *name, boolean_t bool)
{
	var_t *var;

	var = var_lvar_alloc_local(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (NULL);
	}

	VAR_SET_BOOL(var, bool);

	filebench_log(LOG_DEBUG_SCRIPT, "Assign integer %s=%s",
	    name, bool ? "true" : "false");

	return (var);
}

/*
 * the routine allocates a new local var and sets
 * its var_integers's value to that of the supplied
 * integer. It returns a pointer to the new local var
 */
var_t *
var_lvar_assign_integer(char *name, fbint_t integer)
{
	var_t *var;

	var = var_lvar_alloc_local(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (NULL);
	}

	VAR_SET_INT(var, integer);

	filebench_log(LOG_DEBUG_SCRIPT, "Assign integer %s=%llu",
	    name, (u_longlong_t)integer);

	return (var);
}

/*
 * the routine allocates a new local var and sets
 * its var_dbl_flt value to that of the supplied
 * double precission floating point number. It returns
 * a pointer to the new local var
 */
var_t *
var_lvar_assign_double(char *name, double dbl)
{
	var_t *var;

	var = var_lvar_alloc_local(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (NULL);
	}

	VAR_SET_DBL(var, dbl);

	filebench_log(LOG_DEBUG_SCRIPT, "Assign integer %s=%8.2f", name, dbl);

	return (var);
}

/*
 * Like var_lvar_assign_integer, only this routine copies the
 * supplied "string" into the var named "name". If the var
 * named "name" cannot be found then it is first allocated
 * before the copy. Space for the string in the var comes
 * from interprocess shared memory. The allocated local var
 * is returned at as a char *, or NULL on error.
 */
var_t *
var_lvar_assign_string(char *name, char *string)
{
	var_t *var;
	char *strptr;

	var = var_lvar_alloc_local(name);

	if (var == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (NULL);
	}

	if ((strptr = ipc_stralloc(string)) == NULL) {
		filebench_log(LOG_ERROR, "Cannot assign variable %s",
		    name);
		return (NULL);
	}
	VAR_SET_STR(var, strptr);

	filebench_log(LOG_DEBUG_SCRIPT,
	    "Lvar_assign_string (%s, %p)=%s", name, var, string);

	return (var);
}

/*
 * Tests to see if the supplied variable name without the portion after
 * the last period is that of a random variable. If it is, it returns
 * the number of characters to backspace to skip the period and field
 * name. Otherwise it returns 0.
 */
int
var_is_set4_randvar(char *name)
{
	var_t *var;
	char varname[128];
	int namelength;
	char *sp;

	(void) strncpy(varname, name, 128);
	namelength = strlen(varname);
	sp = varname + namelength;

	while (sp != varname) {
		int c = *sp;

		*sp = 0;
		if (c == '.')
			break;

		sp--;
	}

	/* not a variable name + field? */
	if (sp == varname)
		return (0);

	/* first part not a variable name? */
	if ((var = var_find(varname+1)) == NULL)
		return (0);

	/* Make sure it is a random variable */
	if ((var->var_type & VAR_TYPE_MASK) != VAR_TYPE_RANDOM)
		return (0);

	/* calculate offset from end of random variable name */
	return (namelength - (sp - varname));
}

/*
 * Implements a simple path name like scheme for finding values
 * to place in certain specially named vars. The first part of
 * the name is interpreted as a category of either: stats,
 * eventgen, date, script, or host var. If a match is found,
 * the appropriate routine is called to fill in the requested
 * value in the provided var_t, and a pointer to the supplied
 * var_t is returned. If the requested value is not found, NULL
 * is returned.
 */
static var_t *
var_find_internal(var_t *var)
{
	char *n = fb_stralloc(var->var_name);
	char *name = n;
	var_t *rtn = NULL;

	name++;
	if (name[strlen(name) - 1] != '}')
		return (NULL);
	name[strlen(name) - 1] = 0;

	if (strncmp(name, STATS_VAR, strlen(STATS_VAR)) == 0)
		rtn = stats_findvar(var, name + strlen(STATS_VAR));

	if (strcmp(name, EVENTGEN_VAR) == 0)
		rtn = eventgen_ratevar(var);

	if (strcmp(name, DATE_VAR) == 0)
		rtn = date_var(var);

	if (strcmp(name, SCRIPT_VAR) == 0)
		rtn = script_var(var);

	if (strcmp(name, HOST_VAR) == 0)
		rtn = host_var(var);

	free(n);

	return (rtn);
}

/*
 * Calls the C library routine getenv() to obtain the value
 * for the environment variable specified by var->var_name.
 * If found, the value string is returned in var->var_val.string.
 * If the requested value is not found, NULL is returned.
 */
static var_t *
var_find_environment(var_t *var)
{
	char *n = fb_stralloc(var->var_name);
	char *name = n;
	char *strptr;

	name++;
	if (name[strlen(name) - 1] != ')') {
		free(n);
		return (NULL);
	}
	name[strlen(name) - 1] = 0;

	if ((strptr = getenv(name)) != NULL) {
		free(n);
		VAR_SET_STR(var, strptr);
		return (var);
	} else {
		free(n);
		return (NULL);
	}
}

/*
 * Look up special variables. The "name" argument is used to find
 * the desired special var and fill it with an appropriate string
 * value. Looks for an already allocated var of the same name on
 * the shm_var_dyn_list. If not found a new dynamic var is allocated.
 * if the name begins with '{', it is an internal variable, and
 * var_find_internal() is called. If the name begins with '(' it
 * is an environment varable, and var_find_environment() is
 * called. On success, a pointer to the var_t is returned,
 * otherwise, NULL is returned.
 */
static var_t *
var_find_dynamic(char *name)
{
	var_t *var = NULL;
	var_t *v = filebench_shm->shm_var_dyn_list;
	var_t *rtn;

	/*
	 * Lookup a reference to the var handle for this
	 * special var
	 */
	for (v = filebench_shm->shm_var_dyn_list; v != NULL; v = v->var_next) {
		if (strcmp(v->var_name, name) == 0) {
			var = v;
			break;
		}
	}

	if (var == NULL)
		var = var_alloc_dynamic(name);

	/* Internal system control variable */
	if (*name == '{') {
		rtn = var_find_internal(var);
		if (rtn == NULL)
			filebench_log(LOG_ERROR,
			    "Cannot find internal variable %s",
			    var->var_name);
		return (rtn);
	}

	/* Lookup variable in environment */
	if (*name == '(') {
		rtn = var_find_environment(var);
		if (rtn == NULL)
			filebench_log(LOG_ERROR,
			    "Cannot find environment variable %s",
			    var->var_name);
		return (rtn);
	}

	return (NULL);
}

/*
 * replace the avd_t attribute value descriptor in the new FLOW_MASTER flowop
 * that points to a local variable with a new avd_t containing
 * the actual value from the local variable.
 */
void
avd_update(avd_t *avdp, var_t *lvar_list)
{
	var_t *old_lvar, *new_lvar;

	if ((*avdp)->avd_type == AVD_IND_VAR) {

		/* Make sure there is a local var */
		if ((old_lvar = (*avdp)->avd_val.varptr) == NULL) {
			filebench_log(LOG_ERROR,
			    "avd_update: local var not found");
			return;
		}
	} else {
		/* Empty or not indirect, so no update needed */
		return;
	}

	/*  allocate a new avd using the new or old lvar contents */
	if ((new_lvar =
	    var_find_list(old_lvar->var_name, lvar_list)) != NULL)
		(*avdp) = avd_alloc_var_ptr(new_lvar);
	else
		(*avdp) = avd_alloc_var_ptr(old_lvar);
}

void
var_update_comp_lvars(var_t *newlvar, var_t *proto_comp_vars,
    var_t *mstr_lvars)
{
	var_t *proto_lvar;

	/* find the prototype lvar from the inherited list */
	proto_lvar = var_find_list_only(newlvar->var_name, proto_comp_vars);

	if (proto_lvar == NULL)
		return;

	/*
	 * if the new local variable has not already been assigned
	 * a value, try to copy a value from the prototype local variable
	 */
	if ((newlvar->var_type & VAR_TYPE_SET_MASK) == 0) {

		/* copy value from prototype lvar to new lvar */
		(void) var_copy(newlvar, proto_lvar);
	}

	/* If proto lvar is indirect, see if we can colapse indirection */
	if (VAR_HAS_INDVAR(proto_lvar)) {
		var_t *uplvp;

		uplvp = (var_t *)proto_lvar->var_varptr1;

		/* search for more current uplvar on comp master list */
		if (mstr_lvars) {
			uplvp = var_find_list_only(
			    uplvp->var_name, mstr_lvars);
			VAR_SET_INDVAR(newlvar, uplvp);
		}

		if (VAR_HAS_INDVAR(uplvp))
			VAR_SET_INDVAR(newlvar, uplvp->var_varptr1);
	}
}