aboutsummaryrefslogtreecommitdiff
path: root/sys/kern/subr_stats.c
blob: 9dd874fcbcf89b1f6cd3cc63b41efb0d5f534202 (plain) (blame)
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
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
/*-
 * Copyright (c) 2014-2018 Netflix, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * Author: Lawrence Stewart <lstewart@netflix.com>
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/arb.h>
#include <sys/ctype.h>
#include <sys/errno.h>
#include <sys/hash.h>
#include <sys/limits.h>
#include <sys/malloc.h>
#include <sys/qmath.h>
#include <sys/sbuf.h>
#if defined(DIAGNOSTIC)
#include <sys/tree.h>
#endif
#include <sys/stats.h> /* Must come after qmath.h and arb.h */
#include <sys/stddef.h>
#include <sys/stdint.h>
#include <sys/time.h>

#ifdef _KERNEL
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/rwlock.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#else /* ! _KERNEL */
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif /* _KERNEL */

struct voistatdata_voistate {
	/* Previous VOI value for diff calculation. */
	struct voistatdata_numeric prev;
};

#define	VS_VSDVALID	0x0001	/* Stat's voistatdata updated at least once. */
struct voistat {
	int8_t		stype;		/* Type of stat e.g. VS_STYPE_SUM. */
	enum vsd_dtype	dtype : 8;	/* Data type of this stat's data. */
	uint16_t	data_off;	/* Blob offset for this stat's data. */
	uint16_t	dsz;		/* Size of stat's data. */
#define	VS_EBITS 8
	uint16_t	errs : VS_EBITS;/* Non-wrapping error count. */
	uint16_t	flags : 16 - VS_EBITS;
};
/* The voistat error count is capped to avoid wrapping. */
#define	VS_INCERRS(vs) do {						\
	if ((vs)->errs < (1U << VS_EBITS) - 1)				\
		(vs)->errs++;						\
} while (0)

/*
 * Ideas for flags:
 *   - Global or entity specific (global would imply use of counter(9)?)
 *   - Whether to reset stats on read or not
 *   - Signal an overflow?
 *   - Compressed voistat array
 */
#define	VOI_REQSTATE	0x0001	/* VOI requires VS_STYPE_VOISTATE. */
struct voi {
	int16_t		id;		/* VOI id. */
	enum vsd_dtype	dtype : 8;	/* Data type of the VOI itself. */
	int8_t		voistatmaxid;	/* Largest allocated voistat index. */
	uint16_t	stats_off;	/* Blob offset for this VOIs stats. */
	uint16_t	flags;
};

/*
 * Memory for the entire blob is allocated as a slab and then offsets are
 * maintained to carve up the slab into sections holding different data types.
 *
 * Ideas for flags:
 * - Compressed voi array (trade off memory usage vs search time)
 * - Units of offsets (default bytes, flag for e.g. vm_page/KiB/Mib)
 */
struct statsblobv1 {
	uint8_t		abi;
	uint8_t		endian;
	uint16_t	flags;
	uint16_t	maxsz;
	uint16_t	cursz;
	/* Fields from here down are opaque to consumers. */
	uint32_t	tplhash;	/* Base template hash ID. */
	uint16_t	stats_off;	/* voistat array blob offset. */
	uint16_t	statsdata_off;	/* voistatdata array blob offset. */
	sbintime_t	created;	/* Blob creation time. */
	sbintime_t	lastrst;	/* Time of last reset. */
	struct voi	vois[];		/* Array indexed by [voi_id]. */
} __aligned(sizeof(void *));
_Static_assert(offsetof(struct statsblobv1, cursz) +
    SIZEOF_MEMBER(struct statsblobv1, cursz) ==
    offsetof(struct statsblob, opaque),
    "statsblobv1 ABI mismatch");

struct statsblobv1_tpl {
	struct metablob		*mb;
	struct statsblobv1	*sb;
};

/* Context passed to iterator callbacks. */
struct sb_iter_ctx {
	void		*usrctx;	/* Caller supplied context. */
	uint32_t	flags;		/* Flags for current iteration. */
	int16_t		vslot;		/* struct voi slot index. */
	int8_t		vsslot;		/* struct voistat slot index. */
};

struct sb_tostrcb_ctx {
	struct sbuf		*buf;
	struct statsblob_tpl	*tpl;
	enum sb_str_fmt	fmt;
	uint32_t		flags;
};

struct sb_visitcb_ctx {
	stats_blob_visitcb_t	cb;
	void			*usrctx;
};

/* Stats blob iterator callback. */
typedef int (*stats_v1_blob_itercb_t)(struct statsblobv1 *sb, struct voi *v,
    struct voistat *vs, struct sb_iter_ctx *ctx);

#ifdef _KERNEL
static struct rwlock tpllistlock;
RW_SYSINIT(stats_tpl_list, &tpllistlock, "Stat template list lock");
#define	TPL_LIST_RLOCK() rw_rlock(&tpllistlock)
#define	TPL_LIST_RUNLOCK() rw_runlock(&tpllistlock)
#define	TPL_LIST_WLOCK() rw_wlock(&tpllistlock)
#define	TPL_LIST_WUNLOCK() rw_wunlock(&tpllistlock)
#define	TPL_LIST_LOCK_ASSERT() rw_assert(&tpllistlock, RA_LOCKED)
#define	TPL_LIST_RLOCK_ASSERT() rw_assert(&tpllistlock, RA_RLOCKED)
#define	TPL_LIST_WLOCK_ASSERT() rw_assert(&tpllistlock, RA_WLOCKED)
MALLOC_DEFINE(M_STATS, "stats(9) related memory", "stats(9) related memory");
#define	stats_free(ptr) free((ptr), M_STATS)
#else /* ! _KERNEL */
static void stats_constructor(void);
static void stats_destructor(void);
static pthread_rwlock_t tpllistlock;
#define	TPL_LIST_UNLOCK() pthread_rwlock_unlock(&tpllistlock)
#define	TPL_LIST_RLOCK() pthread_rwlock_rdlock(&tpllistlock)
#define	TPL_LIST_RUNLOCK() TPL_LIST_UNLOCK()
#define	TPL_LIST_WLOCK() pthread_rwlock_wrlock(&tpllistlock)
#define	TPL_LIST_WUNLOCK() TPL_LIST_UNLOCK()
#define	TPL_LIST_LOCK_ASSERT() do { } while (0)
#define	TPL_LIST_RLOCK_ASSERT() do { } while (0)
#define	TPL_LIST_WLOCK_ASSERT() do { } while (0)
#ifdef NDEBUG
#define	KASSERT(cond, msg) do {} while (0)
#define	stats_abort() do {} while (0)
#else /* ! NDEBUG */
#define	KASSERT(cond, msg) do { \
	if (!(cond)) { \
		panic msg; \
	} \
} while (0)
#define	stats_abort() abort()
#endif /* NDEBUG */
#define	stats_free(ptr) free(ptr)
#define	panic(fmt, ...) do { \
	fprintf(stderr, (fmt), ##__VA_ARGS__); \
	stats_abort(); \
} while (0)
#endif /* _KERNEL */

#define	SB_V1_MAXSZ 65535

/* Obtain a blob offset pointer. */
#define	BLOB_OFFSET(sb, off) ((void *)(((uint8_t *)(sb)) + (off)))

/*
 * Number of VOIs in the blob's vois[] array. By virtue of struct voi being a
 * power of 2 size, we can shift instead of divide. The shift amount must be
 * updated if sizeof(struct voi) ever changes, which the assert should catch.
 */
#define	NVOIS(sb) ((int32_t)((((struct statsblobv1 *)(sb))->stats_off - \
    sizeof(struct statsblobv1)) >> 3))
_Static_assert(sizeof(struct voi) == 8, "statsblobv1 voi ABI mismatch");

/* Try restrict names to alphanumeric and underscore to simplify JSON compat. */
const char *vs_stype2name[VS_NUM_STYPES] = {
	[VS_STYPE_VOISTATE] = "VOISTATE",
	[VS_STYPE_SUM] = "SUM",
	[VS_STYPE_MAX] = "MAX",
	[VS_STYPE_MIN] = "MIN",
	[VS_STYPE_HIST] = "HIST",
	[VS_STYPE_TDGST] = "TDGST",
};

const char *vs_stype2desc[VS_NUM_STYPES] = {
	[VS_STYPE_VOISTATE] = "VOI related state data (not a real stat)",
	[VS_STYPE_SUM] = "Simple arithmetic accumulator",
	[VS_STYPE_MAX] = "Maximum observed VOI value",
	[VS_STYPE_MIN] = "Minimum observed VOI value",
	[VS_STYPE_HIST] = "Histogram of observed VOI values",
	[VS_STYPE_TDGST] = "t-digest of observed VOI values",
};

const char *vsd_dtype2name[VSD_NUM_DTYPES] = {
	[VSD_DTYPE_VOISTATE] = "VOISTATE",
	[VSD_DTYPE_INT_S32] = "INT_S32",
	[VSD_DTYPE_INT_U32] = "INT_U32",
	[VSD_DTYPE_INT_S64] = "INT_S64",
	[VSD_DTYPE_INT_U64] = "INT_U64",
	[VSD_DTYPE_INT_SLONG] = "INT_SLONG",
	[VSD_DTYPE_INT_ULONG] = "INT_ULONG",
	[VSD_DTYPE_Q_S32] = "Q_S32",
	[VSD_DTYPE_Q_U32] = "Q_U32",
	[VSD_DTYPE_Q_S64] = "Q_S64",
	[VSD_DTYPE_Q_U64] = "Q_U64",
	[VSD_DTYPE_CRHIST32] = "CRHIST32",
	[VSD_DTYPE_DRHIST32] = "DRHIST32",
	[VSD_DTYPE_DVHIST32] = "DVHIST32",
	[VSD_DTYPE_CRHIST64] = "CRHIST64",
	[VSD_DTYPE_DRHIST64] = "DRHIST64",
	[VSD_DTYPE_DVHIST64] = "DVHIST64",
	[VSD_DTYPE_TDGSTCLUST32] = "TDGSTCLUST32",
	[VSD_DTYPE_TDGSTCLUST64] = "TDGSTCLUST64",
};

const size_t vsd_dtype2size[VSD_NUM_DTYPES] = {
	[VSD_DTYPE_VOISTATE] = sizeof(struct voistatdata_voistate),
	[VSD_DTYPE_INT_S32] = sizeof(struct voistatdata_int32),
	[VSD_DTYPE_INT_U32] = sizeof(struct voistatdata_int32),
	[VSD_DTYPE_INT_S64] = sizeof(struct voistatdata_int64),
	[VSD_DTYPE_INT_U64] = sizeof(struct voistatdata_int64),
	[VSD_DTYPE_INT_SLONG] = sizeof(struct voistatdata_intlong),
	[VSD_DTYPE_INT_ULONG] = sizeof(struct voistatdata_intlong),
	[VSD_DTYPE_Q_S32] = sizeof(struct voistatdata_q32),
	[VSD_DTYPE_Q_U32] = sizeof(struct voistatdata_q32),
	[VSD_DTYPE_Q_S64] = sizeof(struct voistatdata_q64),
	[VSD_DTYPE_Q_U64] = sizeof(struct voistatdata_q64),
	[VSD_DTYPE_CRHIST32] = sizeof(struct voistatdata_crhist32),
	[VSD_DTYPE_DRHIST32] = sizeof(struct voistatdata_drhist32),
	[VSD_DTYPE_DVHIST32] = sizeof(struct voistatdata_dvhist32),
	[VSD_DTYPE_CRHIST64] = sizeof(struct voistatdata_crhist64),
	[VSD_DTYPE_DRHIST64] = sizeof(struct voistatdata_drhist64),
	[VSD_DTYPE_DVHIST64] = sizeof(struct voistatdata_dvhist64),
	[VSD_DTYPE_TDGSTCLUST32] = sizeof(struct voistatdata_tdgstclust32),
	[VSD_DTYPE_TDGSTCLUST64] = sizeof(struct voistatdata_tdgstclust64),
};

static const bool vsd_compoundtype[VSD_NUM_DTYPES] = {
	[VSD_DTYPE_VOISTATE] = true,
	[VSD_DTYPE_INT_S32] = false,
	[VSD_DTYPE_INT_U32] = false,
	[VSD_DTYPE_INT_S64] = false,
	[VSD_DTYPE_INT_U64] = false,
	[VSD_DTYPE_INT_SLONG] = false,
	[VSD_DTYPE_INT_ULONG] = false,
	[VSD_DTYPE_Q_S32] = false,
	[VSD_DTYPE_Q_U32] = false,
	[VSD_DTYPE_Q_S64] = false,
	[VSD_DTYPE_Q_U64] = false,
	[VSD_DTYPE_CRHIST32] = true,
	[VSD_DTYPE_DRHIST32] = true,
	[VSD_DTYPE_DVHIST32] = true,
	[VSD_DTYPE_CRHIST64] = true,
	[VSD_DTYPE_DRHIST64] = true,
	[VSD_DTYPE_DVHIST64] = true,
	[VSD_DTYPE_TDGSTCLUST32] = true,
	[VSD_DTYPE_TDGSTCLUST64] = true,
};

const struct voistatdata_numeric numeric_limits[2][VSD_DTYPE_Q_U64 + 1] = {
	[LIM_MIN] = {
		[VSD_DTYPE_VOISTATE] = {0},
		[VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MIN}},
		[VSD_DTYPE_INT_U32] = {.int32 = {.u32 = 0}},
		[VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MIN}},
		[VSD_DTYPE_INT_U64] = {.int64 = {.u64 = 0}},
		[VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MIN}},
		[VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = 0}},
		[VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMINVAL(INT32_MIN)}},
		[VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = 0}},
		[VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMINVAL(INT64_MIN)}},
		[VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = 0}},
	},
	[LIM_MAX] = {
		[VSD_DTYPE_VOISTATE] = {0},
		[VSD_DTYPE_INT_S32] = {.int32 = {.s32 = INT32_MAX}},
		[VSD_DTYPE_INT_U32] = {.int32 = {.u32 = UINT32_MAX}},
		[VSD_DTYPE_INT_S64] = {.int64 = {.s64 = INT64_MAX}},
		[VSD_DTYPE_INT_U64] = {.int64 = {.u64 = UINT64_MAX}},
		[VSD_DTYPE_INT_SLONG] = {.intlong = {.slong = LONG_MAX}},
		[VSD_DTYPE_INT_ULONG] = {.intlong = {.ulong = ULONG_MAX}},
		[VSD_DTYPE_Q_S32] = {.q32 = {.sq32 = Q_IFMAXVAL(INT32_MAX)}},
		[VSD_DTYPE_Q_U32] = {.q32 = {.uq32 = Q_IFMAXVAL(UINT32_MAX)}},
		[VSD_DTYPE_Q_S64] = {.q64 = {.sq64 = Q_IFMAXVAL(INT64_MAX)}},
		[VSD_DTYPE_Q_U64] = {.q64 = {.uq64 = Q_IFMAXVAL(UINT64_MAX)}},
	}
};

/* tpllistlock protects tpllist and ntpl */
static uint32_t ntpl;
static struct statsblob_tpl **tpllist;

static inline void * stats_realloc(void *ptr, size_t oldsz, size_t newsz,
    int flags);
//static void stats_v1_blob_finalise(struct statsblobv1 *sb);
static int stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id,
    uint32_t flags);
static int stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes,
    int newvoistatbytes, int newvoistatdatabytes);
static void stats_v1_blob_iter(struct statsblobv1 *sb,
    stats_v1_blob_itercb_t icb, void *usrctx, uint32_t flags);
static inline int stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype,
    struct voistatdata_tdgst *tdgst, s64q_t x, uint64_t weight, int attempt);

static inline int
ctd32cmp(const struct voistatdata_tdgstctd32 *c1, const struct voistatdata_tdgstctd32 *c2)
{

	KASSERT(Q_PRECEQ(c1->mu, c2->mu),
	    ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__,
	    Q_RELPREC(c1->mu, c2->mu)));

       return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1);
}
ARB_GENERATE_STATIC(ctdth32, voistatdata_tdgstctd32, ctdlnk, ctd32cmp);

static inline int
ctd64cmp(const struct voistatdata_tdgstctd64 *c1, const struct voistatdata_tdgstctd64 *c2)
{

	KASSERT(Q_PRECEQ(c1->mu, c2->mu),
	    ("%s: Q_RELPREC(c1->mu,c2->mu)=%d", __func__,
	    Q_RELPREC(c1->mu, c2->mu)));

       return (Q_QLTQ(c1->mu, c2->mu) ? -1 : 1);
}
ARB_GENERATE_STATIC(ctdth64, voistatdata_tdgstctd64, ctdlnk, ctd64cmp);

#ifdef DIAGNOSTIC
RB_GENERATE_STATIC(rbctdth32, voistatdata_tdgstctd32, rblnk, ctd32cmp);
RB_GENERATE_STATIC(rbctdth64, voistatdata_tdgstctd64, rblnk, ctd64cmp);
#endif

static inline sbintime_t
stats_sbinuptime(void)
{
	sbintime_t sbt;
#ifdef _KERNEL

	sbt = sbinuptime();
#else /* ! _KERNEL */
	struct timespec tp;

	clock_gettime(CLOCK_MONOTONIC_FAST, &tp);
	sbt = tstosbt(tp);
#endif /* _KERNEL */

	return (sbt);
}

static inline void *
stats_realloc(void *ptr, size_t oldsz, size_t newsz, int flags)
{

#ifdef _KERNEL
	/* Default to M_NOWAIT if neither M_NOWAIT or M_WAITOK are set. */
	if (!(flags & (M_WAITOK | M_NOWAIT)))
		flags |= M_NOWAIT;
	ptr = realloc(ptr, newsz, M_STATS, flags);
#else /* ! _KERNEL */
	ptr = realloc(ptr, newsz);
	if ((flags & M_ZERO) && ptr != NULL) {
		if (oldsz == 0)
			memset(ptr, '\0', newsz);
		else if (newsz > oldsz)
			memset(BLOB_OFFSET(ptr, oldsz), '\0', newsz - oldsz);
	}
#endif /* _KERNEL */

	return (ptr);
}

static inline char *
stats_strdup(const char *s,
#ifdef _KERNEL
    int flags)
{
	char *copy;
	size_t len;

	if (!(flags & (M_WAITOK | M_NOWAIT)))
		flags |= M_NOWAIT;

	len = strlen(s) + 1;
	if ((copy = malloc(len, M_STATS, flags)) != NULL)
		bcopy(s, copy, len);

	return (copy);
#else
    int flags __unused)
{
	return (strdup(s));
#endif
}

static inline void
stats_tpl_update_hash(struct statsblob_tpl *tpl)
{

	TPL_LIST_WLOCK_ASSERT();
	tpl->mb->tplhash = hash32_str(tpl->mb->tplname, 0);
	for (int voi_id = 0; voi_id < NVOIS(tpl->sb); voi_id++) {
		if (tpl->mb->voi_meta[voi_id].name != NULL)
			tpl->mb->tplhash = hash32_str(
			    tpl->mb->voi_meta[voi_id].name, tpl->mb->tplhash);
	}
	tpl->mb->tplhash = hash32_buf(tpl->sb, tpl->sb->cursz,
	    tpl->mb->tplhash);
}

static inline uint64_t
stats_pow_u64(uint64_t base, uint64_t exp)
{
	uint64_t result = 1;

	while (exp) {
		if (exp & 1)
			result *= base;
		exp >>= 1;
		base *= base;
	}

	return (result);
}

static inline int
stats_vss_hist_bkt_hlpr(struct vss_hist_hlpr_info *info, uint32_t curbkt,
    struct voistatdata_numeric *bkt_lb, struct voistatdata_numeric *bkt_ub)
{
	uint64_t step = 0;
	int error = 0;

	switch (info->scheme) {
	case BKT_LIN:
		step = info->lin.stepinc;
		break;
	case BKT_EXP:
		step = stats_pow_u64(info->exp.stepbase,
		    info->exp.stepexp + curbkt);
		break;
	case BKT_LINEXP:
		{
		uint64_t curstepexp = 1;

		switch (info->voi_dtype) {
		case VSD_DTYPE_INT_S32:
			while ((int32_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= bkt_lb->int32.s32)
				curstepexp++;
			break;
		case VSD_DTYPE_INT_U32:
			while ((uint32_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= bkt_lb->int32.u32)
				curstepexp++;
			break;
		case VSD_DTYPE_INT_S64:
			while ((int64_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= bkt_lb->int64.s64)
				curstepexp++;
			break;
		case VSD_DTYPE_INT_U64:
			while ((uint64_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= bkt_lb->int64.u64)
				curstepexp++;
			break;
		case VSD_DTYPE_INT_SLONG:
			while ((long)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= bkt_lb->intlong.slong)
				curstepexp++;
			break;
		case VSD_DTYPE_INT_ULONG:
			while ((unsigned long)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= bkt_lb->intlong.ulong)
				curstepexp++;
			break;
		case VSD_DTYPE_Q_S32:
			while ((s32q_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= Q_GIVAL(bkt_lb->q32.sq32))
			break;
		case VSD_DTYPE_Q_U32:
			while ((u32q_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= Q_GIVAL(bkt_lb->q32.uq32))
			break;
		case VSD_DTYPE_Q_S64:
			while ((s64q_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= Q_GIVAL(bkt_lb->q64.sq64))
				curstepexp++;
			break;
		case VSD_DTYPE_Q_U64:
			while ((u64q_t)stats_pow_u64(info->linexp.stepbase,
			    curstepexp) <= Q_GIVAL(bkt_lb->q64.uq64))
				curstepexp++;
			break;
		default:
			break;
		}

		step = stats_pow_u64(info->linexp.stepbase, curstepexp) /
		    info->linexp.linstepdiv;
		if (step == 0)
			step = 1;
		break;
		}
	default:
		break;
	}

	if (info->scheme == BKT_USR) {
		*bkt_lb = info->usr.bkts[curbkt].lb;
		*bkt_ub = info->usr.bkts[curbkt].ub;
	} else if (step != 0) {
		switch (info->voi_dtype) {
		case VSD_DTYPE_INT_S32:
			bkt_ub->int32.s32 += (int32_t)step;
			break;
		case VSD_DTYPE_INT_U32:
			bkt_ub->int32.u32 += (uint32_t)step;
			break;
		case VSD_DTYPE_INT_S64:
			bkt_ub->int64.s64 += (int64_t)step;
			break;
		case VSD_DTYPE_INT_U64:
			bkt_ub->int64.u64 += (uint64_t)step;
			break;
		case VSD_DTYPE_INT_SLONG:
			bkt_ub->intlong.slong += (long)step;
			break;
		case VSD_DTYPE_INT_ULONG:
			bkt_ub->intlong.ulong += (unsigned long)step;
			break;
		case VSD_DTYPE_Q_S32:
			error = Q_QADDI(&bkt_ub->q32.sq32, step);
			break;
		case VSD_DTYPE_Q_U32:
			error = Q_QADDI(&bkt_ub->q32.uq32, step);
			break;
		case VSD_DTYPE_Q_S64:
			error = Q_QADDI(&bkt_ub->q64.sq64, step);
			break;
		case VSD_DTYPE_Q_U64:
			error = Q_QADDI(&bkt_ub->q64.uq64, step);
			break;
		default:
			break;
		}
	} else { /* info->scheme != BKT_USR && step == 0 */
		return (EINVAL);
	}

	return (error);
}

static uint32_t
stats_vss_hist_nbkts_hlpr(struct vss_hist_hlpr_info *info)
{
	struct voistatdata_numeric bkt_lb, bkt_ub;
	uint32_t nbkts;
	int done;

	if (info->scheme == BKT_USR) {
		/* XXXLAS: Setting info->{lb,ub} from macro is tricky. */
		info->lb = info->usr.bkts[0].lb;
		info->ub = info->usr.bkts[info->usr.nbkts - 1].lb;
	}

	nbkts = 0;
	done = 0;
	bkt_ub = info->lb;

	do {
		bkt_lb = bkt_ub;
		if (stats_vss_hist_bkt_hlpr(info, nbkts++, &bkt_lb, &bkt_ub))
			return (0);

		if (info->scheme == BKT_USR)
			done = (nbkts == info->usr.nbkts);
		else {
			switch (info->voi_dtype) {
			case VSD_DTYPE_INT_S32:
				done = (bkt_ub.int32.s32 > info->ub.int32.s32);
				break;
			case VSD_DTYPE_INT_U32:
				done = (bkt_ub.int32.u32 > info->ub.int32.u32);
				break;
			case VSD_DTYPE_INT_S64:
				done = (bkt_ub.int64.s64 > info->ub.int64.s64);
				break;
			case VSD_DTYPE_INT_U64:
				done = (bkt_ub.int64.u64 > info->ub.int64.u64);
				break;
			case VSD_DTYPE_INT_SLONG:
				done = (bkt_ub.intlong.slong >
				    info->ub.intlong.slong);
				break;
			case VSD_DTYPE_INT_ULONG:
				done = (bkt_ub.intlong.ulong >
				    info->ub.intlong.ulong);
				break;
			case VSD_DTYPE_Q_S32:
				done = Q_QGTQ(bkt_ub.q32.sq32,
				    info->ub.q32.sq32);
				break;
			case VSD_DTYPE_Q_U32:
				done = Q_QGTQ(bkt_ub.q32.uq32,
				    info->ub.q32.uq32);
				break;
			case VSD_DTYPE_Q_S64:
				done = Q_QGTQ(bkt_ub.q64.sq64,
				    info->ub.q64.sq64);
				break;
			case VSD_DTYPE_Q_U64:
				done = Q_QGTQ(bkt_ub.q64.uq64,
				    info->ub.q64.uq64);
				break;
			default:
				return (0);
			}
		}
	} while (!done);

	if (info->flags & VSD_HIST_LBOUND_INF)
		nbkts++;
	if (info->flags & VSD_HIST_UBOUND_INF)
		nbkts++;

	return (nbkts);
}

int
stats_vss_hist_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
    struct vss_hist_hlpr_info *info)
{
	struct voistatdata_hist *hist;
	struct voistatdata_numeric bkt_lb, bkt_ub, *lbinfbktlb, *lbinfbktub,
	    *ubinfbktlb, *ubinfbktub;
	uint32_t bkt, nbkts, nloop;

	if (vss == NULL || info == NULL || (info->flags &
	(VSD_HIST_LBOUND_INF|VSD_HIST_UBOUND_INF) && (info->hist_dtype ==
	VSD_DTYPE_DVHIST32 || info->hist_dtype == VSD_DTYPE_DVHIST64)))
		return (EINVAL);

	info->voi_dtype = voi_dtype;

	if ((nbkts = stats_vss_hist_nbkts_hlpr(info)) == 0)
		return (EINVAL);

	switch (info->hist_dtype) {
	case VSD_DTYPE_CRHIST32:
		vss->vsdsz = HIST_NBKTS2VSDSZ(crhist32, nbkts);
		break;
	case VSD_DTYPE_DRHIST32:
		vss->vsdsz = HIST_NBKTS2VSDSZ(drhist32, nbkts);
		break;
	case VSD_DTYPE_DVHIST32:
		vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist32, nbkts);
		break;
	case VSD_DTYPE_CRHIST64:
		vss->vsdsz = HIST_NBKTS2VSDSZ(crhist64, nbkts);
		break;
	case VSD_DTYPE_DRHIST64:
		vss->vsdsz = HIST_NBKTS2VSDSZ(drhist64, nbkts);
		break;
	case VSD_DTYPE_DVHIST64:
		vss->vsdsz = HIST_NBKTS2VSDSZ(dvhist64, nbkts);
		break;
	default:
		return (EINVAL);
	}

	vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO);
	if (vss->iv == NULL)
		return (ENOMEM);

	hist = (struct voistatdata_hist *)vss->iv;
	bkt_ub = info->lb;

	for (bkt = (info->flags & VSD_HIST_LBOUND_INF), nloop = 0;
	    bkt < nbkts;
	    bkt++, nloop++) {
		bkt_lb = bkt_ub;
		if (stats_vss_hist_bkt_hlpr(info, nloop, &bkt_lb, &bkt_ub))
			return (EINVAL);

		switch (info->hist_dtype) {
		case VSD_DTYPE_CRHIST32:
			VSD(crhist32, hist)->bkts[bkt].lb = bkt_lb;
			break;
		case VSD_DTYPE_DRHIST32:
			VSD(drhist32, hist)->bkts[bkt].lb = bkt_lb;
			VSD(drhist32, hist)->bkts[bkt].ub = bkt_ub;
			break;
		case VSD_DTYPE_DVHIST32:
			VSD(dvhist32, hist)->bkts[bkt].val = bkt_lb;
			break;
		case VSD_DTYPE_CRHIST64:
			VSD(crhist64, hist)->bkts[bkt].lb = bkt_lb;
			break;
		case VSD_DTYPE_DRHIST64:
			VSD(drhist64, hist)->bkts[bkt].lb = bkt_lb;
			VSD(drhist64, hist)->bkts[bkt].ub = bkt_ub;
			break;
		case VSD_DTYPE_DVHIST64:
			VSD(dvhist64, hist)->bkts[bkt].val = bkt_lb;
			break;
		default:
			return (EINVAL);
		}
	}

	lbinfbktlb = lbinfbktub = ubinfbktlb = ubinfbktub = NULL;

	switch (info->hist_dtype) {
	case VSD_DTYPE_CRHIST32:
		lbinfbktlb = &VSD(crhist32, hist)->bkts[0].lb;
		ubinfbktlb = &VSD(crhist32, hist)->bkts[nbkts - 1].lb;
		break;
	case VSD_DTYPE_DRHIST32:
		lbinfbktlb = &VSD(drhist32, hist)->bkts[0].lb;
		lbinfbktub = &VSD(drhist32, hist)->bkts[0].ub;
		ubinfbktlb = &VSD(drhist32, hist)->bkts[nbkts - 1].lb;
		ubinfbktub = &VSD(drhist32, hist)->bkts[nbkts - 1].ub;
		break;
	case VSD_DTYPE_CRHIST64:
		lbinfbktlb = &VSD(crhist64, hist)->bkts[0].lb;
		ubinfbktlb = &VSD(crhist64, hist)->bkts[nbkts - 1].lb;
		break;
	case VSD_DTYPE_DRHIST64:
		lbinfbktlb = &VSD(drhist64, hist)->bkts[0].lb;
		lbinfbktub = &VSD(drhist64, hist)->bkts[0].ub;
		ubinfbktlb = &VSD(drhist64, hist)->bkts[nbkts - 1].lb;
		ubinfbktub = &VSD(drhist64, hist)->bkts[nbkts - 1].ub;
		break;
	case VSD_DTYPE_DVHIST32:
	case VSD_DTYPE_DVHIST64:
		break;
	default:
		return (EINVAL);
	}

	if ((info->flags & VSD_HIST_LBOUND_INF) && lbinfbktlb) {
		*lbinfbktlb = numeric_limits[LIM_MIN][info->voi_dtype];
		/*
		 * Assignment from numeric_limit array for Q types assigns max
		 * possible integral/fractional value for underlying data type,
		 * but we must set control bits for this specific histogram per
		 * the user's choice of fractional bits, which we extract from
		 * info->lb.
		 */
		if (info->voi_dtype == VSD_DTYPE_Q_S32 ||
		    info->voi_dtype == VSD_DTYPE_Q_U32) {
			/* Signedness doesn't matter for setting control bits. */
			Q_SCVAL(lbinfbktlb->q32.sq32,
			    Q_GCVAL(info->lb.q32.sq32));
		} else if (info->voi_dtype == VSD_DTYPE_Q_S64 ||
		    info->voi_dtype == VSD_DTYPE_Q_U64) {
			/* Signedness doesn't matter for setting control bits. */
			Q_SCVAL(lbinfbktlb->q64.sq64,
			    Q_GCVAL(info->lb.q64.sq64));
		}
		if (lbinfbktub)
			*lbinfbktub = info->lb;
	}
	if ((info->flags & VSD_HIST_UBOUND_INF) && ubinfbktlb) {
		*ubinfbktlb = bkt_lb;
		if (ubinfbktub) {
			*ubinfbktub = numeric_limits[LIM_MAX][info->voi_dtype];
			if (info->voi_dtype == VSD_DTYPE_Q_S32 ||
			    info->voi_dtype == VSD_DTYPE_Q_U32) {
				Q_SCVAL(ubinfbktub->q32.sq32,
				    Q_GCVAL(info->lb.q32.sq32));
			} else if (info->voi_dtype == VSD_DTYPE_Q_S64 ||
			    info->voi_dtype == VSD_DTYPE_Q_U64) {
				Q_SCVAL(ubinfbktub->q64.sq64,
				    Q_GCVAL(info->lb.q64.sq64));
			}
		}
	}

	return (0);
}

int
stats_vss_tdgst_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
    struct vss_tdgst_hlpr_info *info)
{
	struct voistatdata_tdgst *tdgst;
	struct ctdth32 *ctd32tree;
	struct ctdth64 *ctd64tree;
	struct voistatdata_tdgstctd32 *ctd32;
	struct voistatdata_tdgstctd64 *ctd64;

	info->voi_dtype = voi_dtype;

	switch (info->tdgst_dtype) {
	case VSD_DTYPE_TDGSTCLUST32:
		vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust32, info->nctds);
		break;
	case VSD_DTYPE_TDGSTCLUST64:
		vss->vsdsz = TDGST_NCTRS2VSDSZ(tdgstclust64, info->nctds);
		break;
	default:
		return (EINVAL);
	}

	vss->iv = stats_realloc(NULL, 0, vss->vsdsz, M_ZERO);
	if (vss->iv == NULL)
		return (ENOMEM);

	tdgst = (struct voistatdata_tdgst *)vss->iv;

	switch (info->tdgst_dtype) {
	case VSD_DTYPE_TDGSTCLUST32:
		ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
		ARB_INIT(ctd32, ctdlnk, ctd32tree, info->nctds) {
			Q_INI(&ctd32->mu, 0, 0, info->prec);
		}
		break;
	case VSD_DTYPE_TDGSTCLUST64:
		ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
		ARB_INIT(ctd64, ctdlnk, ctd64tree, info->nctds) {
			Q_INI(&ctd64->mu, 0, 0, info->prec);
		}
		break;
	default:
		return (EINVAL);
	}

	return (0);
}

int
stats_vss_numeric_hlpr(enum vsd_dtype voi_dtype, struct voistatspec *vss,
    struct vss_numeric_hlpr_info *info)
{
	struct voistatdata_numeric iv;

	switch (vss->stype) {
	case VS_STYPE_SUM:
		iv = stats_ctor_vsd_numeric(0);
		break;
	case VS_STYPE_MIN:
		iv = numeric_limits[LIM_MAX][voi_dtype];
		break;
	case VS_STYPE_MAX:
		iv = numeric_limits[LIM_MIN][voi_dtype];
		break;
	default:
		return (EINVAL);
	}

	vss->iv = stats_realloc(NULL, 0, vsd_dtype2size[voi_dtype], 0);
	if (vss->iv == NULL)
		return (ENOMEM);

	vss->vs_dtype = voi_dtype;
	vss->vsdsz = vsd_dtype2size[voi_dtype];
	switch (voi_dtype) {
	case VSD_DTYPE_INT_S32:
		*((int32_t *)vss->iv) = iv.int32.s32;
		break;
	case VSD_DTYPE_INT_U32:
		*((uint32_t *)vss->iv) = iv.int32.u32;
		break;
	case VSD_DTYPE_INT_S64:
		*((int64_t *)vss->iv) = iv.int64.s64;
		break;
	case VSD_DTYPE_INT_U64:
		*((uint64_t *)vss->iv) = iv.int64.u64;
		break;
	case VSD_DTYPE_INT_SLONG:
		*((long *)vss->iv) = iv.intlong.slong;
		break;
	case VSD_DTYPE_INT_ULONG:
		*((unsigned long *)vss->iv) = iv.intlong.ulong;
		break;
	case VSD_DTYPE_Q_S32:
		*((s32q_t *)vss->iv) = Q_SCVAL(iv.q32.sq32,
		    Q_CTRLINI(info->prec));
		break;
	case VSD_DTYPE_Q_U32:
		*((u32q_t *)vss->iv) = Q_SCVAL(iv.q32.uq32,
		    Q_CTRLINI(info->prec));
		break;
	case VSD_DTYPE_Q_S64:
		*((s64q_t *)vss->iv) = Q_SCVAL(iv.q64.sq64,
		    Q_CTRLINI(info->prec));
		break;
	case VSD_DTYPE_Q_U64:
		*((u64q_t *)vss->iv) = Q_SCVAL(iv.q64.uq64,
		    Q_CTRLINI(info->prec));
		break;
	default:
		break;
	}

	return (0);
}

int
stats_vss_hlpr_init(enum vsd_dtype voi_dtype, uint32_t nvss,
    struct voistatspec *vss)
{
	int i, ret;

	for (i = nvss - 1; i >= 0; i--) {
		if (vss[i].hlpr && (ret = vss[i].hlpr(voi_dtype, &vss[i],
		    vss[i].hlprinfo)) != 0)
			return (ret);
	}

	return (0);
}

void
stats_vss_hlpr_cleanup(uint32_t nvss, struct voistatspec *vss)
{
	int i;

	for (i = nvss - 1; i >= 0; i--) {
		if (vss[i].hlpr) {
			stats_free((void *)vss[i].iv);
			vss[i].iv = NULL;
		}
	}
}

int
stats_tpl_fetch(int tpl_id, struct statsblob_tpl **tpl)
{
	int error;

	error = 0;

	TPL_LIST_WLOCK();
	if (tpl_id < 0 || tpl_id >= (int)ntpl) {
		error = ENOENT;
	} else {
		*tpl = tpllist[tpl_id];
		/* XXXLAS: Acquire refcount on tpl. */
	}
	TPL_LIST_WUNLOCK();

	return (error);
}

int
stats_tpl_fetch_allocid(const char *name, uint32_t hash)
{
	int i, tpl_id;

	tpl_id = -ESRCH;

	TPL_LIST_RLOCK();
	for (i = ntpl - 1; i >= 0; i--) {
		if (name != NULL) {
			if (strlen(name) == strlen(tpllist[i]->mb->tplname) &&
			    strncmp(name, tpllist[i]->mb->tplname,
			    TPL_MAX_NAME_LEN) == 0 && (!hash || hash ==
			    tpllist[i]->mb->tplhash)) {
				tpl_id = i;
				break;
			}
		} else if (hash == tpllist[i]->mb->tplhash) {
			tpl_id = i;
			break;
		}
	}
	TPL_LIST_RUNLOCK();

	return (tpl_id);
}

int
stats_tpl_id2name(uint32_t tpl_id, char *buf, size_t len)
{
	int error;

	error = 0;

	TPL_LIST_RLOCK();
	if (tpl_id < ntpl) {
		if (buf != NULL && len > strlen(tpllist[tpl_id]->mb->tplname))
			strlcpy(buf, tpllist[tpl_id]->mb->tplname, len);
		else
			error = EOVERFLOW;
	} else
		error = ENOENT;
	TPL_LIST_RUNLOCK();

	return (error);
}

int
stats_tpl_sample_rollthedice(struct stats_tpl_sample_rate *rates, int nrates,
    void *seed_bytes, size_t seed_len)
{
	uint32_t cum_pct, rnd_pct;
	int i;

	cum_pct = 0;

	/*
	 * Choose a pseudorandom or seeded number in range [0,100] and use
	 * it to make a sampling decision and template selection where required.
	 * If no seed is supplied, a PRNG is used to generate a pseudorandom
	 * number so that every selection is independent. If a seed is supplied,
	 * the caller desires random selection across different seeds, but
	 * deterministic selection given the same seed. This is achieved by
	 * hashing the seed and using the hash as the random number source.
	 *
	 * XXXLAS: Characterise hash function output distribution.
	 */
	if (seed_bytes == NULL)
		rnd_pct = random() / (INT32_MAX / 100);
	else
		rnd_pct = hash32_buf(seed_bytes, seed_len, 0) /
		    (UINT32_MAX / 100U);

	/*
	 * We map the randomly selected percentage on to the interval [0,100]
	 * consisting of the cumulatively summed template sampling percentages.
	 * The difference between the cumulative sum of all template sampling
	 * percentages and 100 is treated as a NULL assignment i.e. no stats
	 * template will be assigned, and -1 returned instead.
	 */
	for (i = 0; i < nrates; i++) {
		cum_pct += rates[i].tpl_sample_pct;

		KASSERT(cum_pct <= 100, ("%s cum_pct %u > 100", __func__,
		    cum_pct));
		if (rnd_pct > cum_pct || rates[i].tpl_sample_pct == 0)
			continue;

		return (rates[i].tpl_slot_id);
	}

	return (-1);
}

int
stats_v1_blob_clone(struct statsblobv1 **dst, size_t dstmaxsz,
    struct statsblobv1 *src, uint32_t flags)
{
	int error;

	error = 0;

	if (src == NULL || dst == NULL ||
	    src->cursz < sizeof(struct statsblob) ||
	    ((flags & SB_CLONE_ALLOCDST) &&
	    (flags & (SB_CLONE_USRDSTNOFAULT | SB_CLONE_USRDST)))) {
		error = EINVAL;
	} else if (flags & SB_CLONE_ALLOCDST) {
		*dst = stats_realloc(NULL, 0, src->cursz, 0);
		if (*dst)
			(*dst)->maxsz = dstmaxsz = src->cursz;
		else
			error = ENOMEM;
	} else if (*dst == NULL || dstmaxsz < sizeof(struct statsblob)) {
		error = EINVAL;
	}

	if (!error) {
		size_t postcurszlen;

		/*
		 * Clone src into dst except for the maxsz field. If dst is too
		 * small to hold all of src, only copy src's header and return
		 * EOVERFLOW.
		 */
#ifdef _KERNEL
		if (flags & SB_CLONE_USRDSTNOFAULT)
			copyout_nofault(src, *dst,
			    offsetof(struct statsblob, maxsz));
		else if (flags & SB_CLONE_USRDST)
			copyout(src, *dst, offsetof(struct statsblob, maxsz));
		else
#endif
			memcpy(*dst, src, offsetof(struct statsblob, maxsz));

		if (dstmaxsz >= src->cursz) {
			postcurszlen = src->cursz -
			    offsetof(struct statsblob, cursz);
		} else {
			error = EOVERFLOW;
			postcurszlen = sizeof(struct statsblob) -
			    offsetof(struct statsblob, cursz);
		}
#ifdef _KERNEL
		if (flags & SB_CLONE_USRDSTNOFAULT)
			copyout_nofault(&(src->cursz), &((*dst)->cursz),
			    postcurszlen);
		else if (flags & SB_CLONE_USRDST)
			copyout(&(src->cursz), &((*dst)->cursz), postcurszlen);
		else
#endif
			memcpy(&((*dst)->cursz), &(src->cursz), postcurszlen);
	}

	return (error);
}

int
stats_v1_tpl_alloc(const char *name, uint32_t flags __unused)
{
	struct statsblobv1_tpl *tpl, **newtpllist;
	struct statsblobv1 *tpl_sb;
	struct metablob *tpl_mb;
	int tpl_id;

	if (name != NULL && strlen(name) > TPL_MAX_NAME_LEN)
		return (-EINVAL);

	if (name != NULL && stats_tpl_fetch_allocid(name, 0) >= 0)
		return (-EEXIST);

	tpl = stats_realloc(NULL, 0, sizeof(struct statsblobv1_tpl), M_ZERO);
	tpl_mb = stats_realloc(NULL, 0, sizeof(struct metablob), M_ZERO);
	tpl_sb = stats_realloc(NULL, 0, sizeof(struct statsblobv1), M_ZERO);

	if (tpl_mb != NULL && name != NULL)
		tpl_mb->tplname = stats_strdup(name, 0);

	if (tpl == NULL || tpl_sb == NULL || tpl_mb == NULL ||
	    tpl_mb->tplname == NULL) {
		stats_free(tpl);
		stats_free(tpl_sb);
		if (tpl_mb != NULL) {
			stats_free(tpl_mb->tplname);
			stats_free(tpl_mb);
		}
		return (-ENOMEM);
	}

	tpl->mb = tpl_mb;
	tpl->sb = tpl_sb;

	tpl_sb->abi = STATS_ABI_V1;
	tpl_sb->endian =
#if BYTE_ORDER == LITTLE_ENDIAN
	    SB_LE;
#elif BYTE_ORDER == BIG_ENDIAN
	    SB_BE;
#else
	    SB_UE;
#endif
	tpl_sb->cursz = tpl_sb->maxsz = sizeof(struct statsblobv1);
	tpl_sb->stats_off = tpl_sb->statsdata_off = sizeof(struct statsblobv1);

	TPL_LIST_WLOCK();
	newtpllist = stats_realloc(tpllist, ntpl * sizeof(void *),
	    (ntpl + 1) * sizeof(void *), 0);
	if (newtpllist != NULL) {
		tpl_id = ntpl++;
		tpllist = (struct statsblob_tpl **)newtpllist;
		tpllist[tpl_id] = (struct statsblob_tpl *)tpl;
		stats_tpl_update_hash(tpllist[tpl_id]);
	} else {
		stats_free(tpl);
		stats_free(tpl_sb);
		if (tpl_mb != NULL) {
			stats_free(tpl_mb->tplname);
			stats_free(tpl_mb);
		}
		tpl_id = -ENOMEM;
	}
	TPL_LIST_WUNLOCK();

	return (tpl_id);
}

int
stats_v1_tpl_add_voistats(uint32_t tpl_id, int32_t voi_id, const char *voi_name,
    enum vsd_dtype voi_dtype, uint32_t nvss, struct voistatspec *vss,
    uint32_t flags)
{
	struct voi *voi;
	struct voistat *tmpstat;
	struct statsblobv1 *tpl_sb;
	struct metablob *tpl_mb;
	int error, i, newstatdataidx, newvoibytes, newvoistatbytes,
	    newvoistatdatabytes, newvoistatmaxid;
	uint32_t nbytes;

	if (voi_id < 0 || voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES ||
	    nvss == 0 || vss == NULL)
		return (EINVAL);

	error = nbytes = newvoibytes = newvoistatbytes =
	    newvoistatdatabytes = 0;
	newvoistatmaxid = -1;

	/* Calculate the number of bytes required for the new voistats. */
	for (i = nvss - 1; i >= 0; i--) {
		if (vss[i].stype == 0 || vss[i].stype >= VS_NUM_STYPES ||
		    vss[i].vs_dtype == 0 || vss[i].vs_dtype >= VSD_NUM_DTYPES ||
		    vss[i].iv == NULL || vss[i].vsdsz == 0)
			return (EINVAL);
		if ((int)vss[i].stype > newvoistatmaxid)
			newvoistatmaxid = vss[i].stype;
		newvoistatdatabytes += vss[i].vsdsz;
	}

	if (flags & SB_VOI_RELUPDATE) {
		/* XXXLAS: VOI state bytes may need to vary based on stat types. */
		newvoistatdatabytes += sizeof(struct voistatdata_voistate);
	}
	nbytes += newvoistatdatabytes;

	TPL_LIST_WLOCK();
	if (tpl_id < ntpl) {
		tpl_sb = (struct statsblobv1 *)tpllist[tpl_id]->sb;
		tpl_mb = tpllist[tpl_id]->mb;

		if (voi_id >= NVOIS(tpl_sb) || tpl_sb->vois[voi_id].id == -1) {
			/* Adding a new VOI and associated stats. */
			if (voi_id >= NVOIS(tpl_sb)) {
				/* We need to grow the tpl_sb->vois array. */
				newvoibytes = (voi_id - (NVOIS(tpl_sb) - 1)) *
				    sizeof(struct voi);
				nbytes += newvoibytes;
			}
			newvoistatbytes =
			    (newvoistatmaxid + 1) * sizeof(struct voistat);
		} else {
			/* Adding stats to an existing VOI. */
			if (newvoistatmaxid >
			    tpl_sb->vois[voi_id].voistatmaxid) {
				newvoistatbytes = (newvoistatmaxid -
				    tpl_sb->vois[voi_id].voistatmaxid) *
				    sizeof(struct voistat);
			}
			/* XXXLAS: KPI does not yet support expanding VOIs. */
			error = EOPNOTSUPP;
		}
		nbytes += newvoistatbytes;

		if (!error && newvoibytes > 0) {
			struct voi_meta *voi_meta = tpl_mb->voi_meta;

			voi_meta = stats_realloc(voi_meta, voi_meta == NULL ?
			    0 : NVOIS(tpl_sb) * sizeof(struct voi_meta),
			    (1 + voi_id) * sizeof(struct voi_meta),
			    M_ZERO);

			if (voi_meta == NULL)
				error = ENOMEM;
			else
				tpl_mb->voi_meta = voi_meta;
		}

		if (!error) {
			/* NB: Resizing can change where tpl_sb points. */
			error = stats_v1_blob_expand(&tpl_sb, newvoibytes,
			    newvoistatbytes, newvoistatdatabytes);
		}

		if (!error) {
			tpl_mb->voi_meta[voi_id].name = stats_strdup(voi_name,
			    0);
			if (tpl_mb->voi_meta[voi_id].name == NULL)
				error = ENOMEM;
		}

		if (!error) {
			/* Update the template list with the resized pointer. */
			tpllist[tpl_id]->sb = (struct statsblob *)tpl_sb;

			/* Update the template. */
			voi = &tpl_sb->vois[voi_id];

			if (voi->id < 0) {
				/* VOI is new and needs to be initialised. */
				voi->id = voi_id;
				voi->dtype = voi_dtype;
				voi->stats_off = tpl_sb->stats_off;
				if (flags & SB_VOI_RELUPDATE)
					voi->flags |= VOI_REQSTATE;
			} else {
				/*
				 * XXXLAS: When this else block is written, the
				 * "KPI does not yet support expanding VOIs"
				 * error earlier in this function can be
				 * removed. What is required here is to shuffle
				 * the voistat array such that the new stats for
				 * the voi are contiguous, which will displace
				 * stats for other vois that reside after the
				 * voi being updated. The other vois then need
				 * to have their stats_off adjusted post
				 * shuffle.
				 */
			}

			voi->voistatmaxid = newvoistatmaxid;
			newstatdataidx = 0;

			if (voi->flags & VOI_REQSTATE) {
				/* Initialise the voistate stat in slot 0. */
				tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off);
				tmpstat->stype = VS_STYPE_VOISTATE;
				tmpstat->flags = 0;
				tmpstat->dtype = VSD_DTYPE_VOISTATE;
				newstatdataidx = tmpstat->dsz =
				    sizeof(struct voistatdata_numeric);
				tmpstat->data_off = tpl_sb->statsdata_off;
			}

			for (i = 0; (uint32_t)i < nvss; i++) {
				tmpstat = BLOB_OFFSET(tpl_sb, voi->stats_off +
				    (vss[i].stype * sizeof(struct voistat)));
				KASSERT(tmpstat->stype < 0, ("voistat %p "
				    "already initialised", tmpstat));
				tmpstat->stype = vss[i].stype;
				tmpstat->flags = vss[i].flags;
				tmpstat->dtype = vss[i].vs_dtype;
				tmpstat->dsz = vss[i].vsdsz;
				tmpstat->data_off = tpl_sb->statsdata_off +
				    newstatdataidx;
				memcpy(BLOB_OFFSET(tpl_sb, tmpstat->data_off),
				    vss[i].iv, vss[i].vsdsz);
				newstatdataidx += vss[i].vsdsz;
			}

			/* Update the template version hash. */
			stats_tpl_update_hash(tpllist[tpl_id]);
			/* XXXLAS: Confirm tpl name/hash pair remains unique. */
		}
	} else
		error = EINVAL;
	TPL_LIST_WUNLOCK();

	return (error);
}

struct statsblobv1 *
stats_v1_blob_alloc(uint32_t tpl_id, uint32_t flags __unused)
{
	struct statsblobv1 *sb;
	int error;

	sb = NULL;

	TPL_LIST_RLOCK();
	if (tpl_id < ntpl) {
		sb = stats_realloc(NULL, 0, tpllist[tpl_id]->sb->maxsz, 0);
		if (sb != NULL) {
			sb->maxsz = tpllist[tpl_id]->sb->maxsz;
			error = stats_v1_blob_init_locked(sb, tpl_id, 0);
		} else
			error = ENOMEM;

		if (error) {
			stats_free(sb);
			sb = NULL;
		}
	}
	TPL_LIST_RUNLOCK();

	return (sb);
}

void
stats_v1_blob_destroy(struct statsblobv1 *sb)
{

	stats_free(sb);
}

int
stats_v1_voistat_fetch_dptr(struct statsblobv1 *sb, int32_t voi_id,
    enum voi_stype stype, enum vsd_dtype *retdtype, struct voistatdata **retvsd,
    size_t *retvsdsz)
{
	struct voi *v;
	struct voistat *vs;

	if (retvsd == NULL || sb == NULL || sb->abi != STATS_ABI_V1 ||
	    voi_id >= NVOIS(sb))
		return (EINVAL);

	v = &sb->vois[voi_id];
	if ((__typeof(v->voistatmaxid))stype > v->voistatmaxid)
		return (EINVAL);

	vs = BLOB_OFFSET(sb, v->stats_off + (stype * sizeof(struct voistat)));
	*retvsd = BLOB_OFFSET(sb, vs->data_off);
	if (retdtype != NULL)
		*retdtype = vs->dtype;
	if (retvsdsz != NULL)
		*retvsdsz = vs->dsz;

	return (0);
}

int
stats_v1_blob_init(struct statsblobv1 *sb, uint32_t tpl_id, uint32_t flags)
{
	int error;

	error = 0;

	TPL_LIST_RLOCK();
	if (sb == NULL || tpl_id >= ntpl) {
		error = EINVAL;
	} else {
		error = stats_v1_blob_init_locked(sb, tpl_id, flags);
	}
	TPL_LIST_RUNLOCK();

	return (error);
}

static inline int
stats_v1_blob_init_locked(struct statsblobv1 *sb, uint32_t tpl_id,
    uint32_t flags __unused)
{
	int error;

	TPL_LIST_RLOCK_ASSERT();
	error = (sb->maxsz >= tpllist[tpl_id]->sb->cursz) ? 0 : EOVERFLOW;
	KASSERT(!error,
	    ("sb %d instead of %d bytes", sb->maxsz, tpllist[tpl_id]->sb->cursz));

	if (!error) {
		memcpy(sb, tpllist[tpl_id]->sb, tpllist[tpl_id]->sb->cursz);
		sb->created = sb->lastrst = stats_sbinuptime();
		sb->tplhash = tpllist[tpl_id]->mb->tplhash;
	}

	return (error);
}

static int
stats_v1_blob_expand(struct statsblobv1 **sbpp, int newvoibytes,
    int newvoistatbytes, int newvoistatdatabytes)
{
	struct statsblobv1 *sb;
	struct voi *tmpvoi;
	struct voistat *tmpvoistat, *voistat_array;
	int error, i, idxnewvois, idxnewvoistats, nbytes, nvoistats;

	KASSERT(newvoibytes % sizeof(struct voi) == 0,
	    ("Bad newvoibytes %d", newvoibytes));
	KASSERT(newvoistatbytes % sizeof(struct voistat) == 0,
	    ("Bad newvoistatbytes %d", newvoistatbytes));

	error = ((newvoibytes % sizeof(struct voi) == 0) &&
	    (newvoistatbytes % sizeof(struct voistat) == 0)) ? 0 : EINVAL;
	sb = *sbpp;
	nbytes = newvoibytes + newvoistatbytes + newvoistatdatabytes;

	/*
	 * XXXLAS: Required until we gain support for flags which alter the
	 * units of size/offset fields in key structs.
	 */
	if (!error && ((((int)sb->cursz) + nbytes) > SB_V1_MAXSZ))
		error = EFBIG;

	if (!error && (sb->cursz + nbytes > sb->maxsz)) {
		/* Need to expand our blob. */
		sb = stats_realloc(sb, sb->maxsz, sb->cursz + nbytes, M_ZERO);
		if (sb != NULL) {
			sb->maxsz = sb->cursz + nbytes;
			*sbpp = sb;
		} else
		    error = ENOMEM;
	}

	if (!error) {
		/*
		 * Shuffle memory within the expanded blob working from the end
		 * backwards, leaving gaps for the new voistat and voistatdata
		 * structs at the beginning of their respective blob regions,
		 * and for the new voi structs at the end of their blob region.
		 */
		memmove(BLOB_OFFSET(sb, sb->statsdata_off + nbytes),
		    BLOB_OFFSET(sb, sb->statsdata_off),
		    sb->cursz - sb->statsdata_off);
		memmove(BLOB_OFFSET(sb, sb->stats_off + newvoibytes +
		    newvoistatbytes), BLOB_OFFSET(sb, sb->stats_off),
		    sb->statsdata_off - sb->stats_off);

		/* First index of new voi/voistat structs to be initialised. */
		idxnewvois = NVOIS(sb);
		idxnewvoistats = (newvoistatbytes / sizeof(struct voistat)) - 1;

		/* Update housekeeping variables and offsets. */
		sb->cursz += nbytes;
		sb->stats_off += newvoibytes;
		sb->statsdata_off += newvoibytes + newvoistatbytes;

		/* XXXLAS: Zeroing not strictly needed but aids debugging. */
		memset(&sb->vois[idxnewvois], '\0', newvoibytes);
		memset(BLOB_OFFSET(sb, sb->stats_off), '\0',
		    newvoistatbytes);
		memset(BLOB_OFFSET(sb, sb->statsdata_off), '\0',
		    newvoistatdatabytes);

		/* Initialise new voi array members and update offsets. */
		for (i = 0; i < NVOIS(sb); i++) {
			tmpvoi = &sb->vois[i];
			if (i >= idxnewvois) {
				tmpvoi->id = tmpvoi->voistatmaxid = -1;
			} else if (tmpvoi->id > -1) {
				tmpvoi->stats_off += newvoibytes +
				    newvoistatbytes;
			}
		}

		/* Initialise new voistat array members and update offsets. */
		nvoistats = (sb->statsdata_off - sb->stats_off) /
		    sizeof(struct voistat);
		voistat_array = BLOB_OFFSET(sb, sb->stats_off);
		for (i = 0; i < nvoistats; i++) {
			tmpvoistat = &voistat_array[i];
			if (i <= idxnewvoistats) {
				tmpvoistat->stype = -1;
			} else if (tmpvoistat->stype > -1) {
				tmpvoistat->data_off += nbytes;
			}
		}
	}

	return (error);
}

static void
stats_v1_blob_finalise(struct statsblobv1 *sb __unused)
{

	/* XXXLAS: Fill this in. */
}

static void
stats_v1_blob_iter(struct statsblobv1 *sb, stats_v1_blob_itercb_t icb,
    void *usrctx, uint32_t flags)
{
	struct voi *v;
	struct voistat *vs;
	struct sb_iter_ctx ctx;
	int i, j, firstvoi;

	ctx.usrctx = usrctx;
	ctx.flags = SB_IT_FIRST_CB;
	firstvoi = 1;

	for (i = 0; i < NVOIS(sb); i++) {
		v = &sb->vois[i];
		ctx.vslot = i;
		ctx.vsslot = -1;
		ctx.flags |= SB_IT_FIRST_VOISTAT;

		if (firstvoi)
			ctx.flags |= SB_IT_FIRST_VOI;
		else if (i == (NVOIS(sb) - 1))
			ctx.flags |= SB_IT_LAST_VOI | SB_IT_LAST_CB;

		if (v->id < 0 && (flags & SB_IT_NULLVOI)) {
			if (icb(sb, v, NULL, &ctx))
				return;
			firstvoi = 0;
			ctx.flags &= ~SB_IT_FIRST_CB;
		}

		/* If NULL voi, v->voistatmaxid == -1 */
		for (j = 0; j <= v->voistatmaxid; j++) {
			vs = &((struct voistat *)BLOB_OFFSET(sb,
			    v->stats_off))[j];
			if (vs->stype < 0 &&
			    !(flags & SB_IT_NULLVOISTAT))
				continue;

			if (j == v->voistatmaxid) {
				ctx.flags |= SB_IT_LAST_VOISTAT;
				if (i == (NVOIS(sb) - 1))
					ctx.flags |=
					    SB_IT_LAST_CB;
			} else
				ctx.flags &= ~SB_IT_LAST_CB;

			ctx.vsslot = j;
			if (icb(sb, v, vs, &ctx))
				return;

			ctx.flags &= ~(SB_IT_FIRST_CB | SB_IT_FIRST_VOISTAT |
			    SB_IT_LAST_VOISTAT);
		}
		ctx.flags &= ~(SB_IT_FIRST_VOI | SB_IT_LAST_VOI);
	}
}

static inline void
stats_voistatdata_tdgst_tostr(enum vsd_dtype voi_dtype __unused,
    const struct voistatdata_tdgst *tdgst, enum vsd_dtype tdgst_dtype,
    size_t tdgst_dsz __unused, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
{
	const struct ctdth32 *ctd32tree;
	const struct ctdth64 *ctd64tree;
	const struct voistatdata_tdgstctd32 *ctd32;
	const struct voistatdata_tdgstctd64 *ctd64;
	const char *fmtstr;
	uint64_t smplcnt, compcnt;
	int is32bit, qmaxstrlen;
	uint16_t maxctds, curctds;

	switch (tdgst_dtype) {
	case VSD_DTYPE_TDGSTCLUST32:
		smplcnt = CONSTVSD(tdgstclust32, tdgst)->smplcnt;
		compcnt = CONSTVSD(tdgstclust32, tdgst)->compcnt;
		maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
		curctds = ARB_CURNODES(&CONSTVSD(tdgstclust32, tdgst)->ctdtree);
		ctd32tree = &CONSTVSD(tdgstclust32, tdgst)->ctdtree;
		ctd32 = (objdump ? ARB_CNODE(ctd32tree, 0) :
		    ARB_CMIN(ctdth32, ctd32tree));
		qmaxstrlen = (ctd32 == NULL) ? 1 : Q_MAXSTRLEN(ctd32->mu, 10);
		is32bit = 1;
		ctd64tree = NULL;
		ctd64 = NULL;
		break;
	case VSD_DTYPE_TDGSTCLUST64:
		smplcnt = CONSTVSD(tdgstclust64, tdgst)->smplcnt;
		compcnt = CONSTVSD(tdgstclust64, tdgst)->compcnt;
		maxctds = ARB_MAXNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
		curctds = ARB_CURNODES(&CONSTVSD(tdgstclust64, tdgst)->ctdtree);
		ctd64tree = &CONSTVSD(tdgstclust64, tdgst)->ctdtree;
		ctd64 = (objdump ? ARB_CNODE(ctd64tree, 0) :
		    ARB_CMIN(ctdth64, ctd64tree));
		qmaxstrlen = (ctd64 == NULL) ? 1 : Q_MAXSTRLEN(ctd64->mu, 10);
		is32bit = 0;
		ctd32tree = NULL;
		ctd32 = NULL;
		break;
	default:
		return;
	}

	switch (fmt) {
	case SB_STRFMT_FREEFORM:
		fmtstr = "smplcnt=%ju, compcnt=%ju, maxctds=%hu, nctds=%hu";
		break;
	case SB_STRFMT_JSON:
	default:
		fmtstr =
		    "\"smplcnt\":%ju,\"compcnt\":%ju,\"maxctds\":%hu,"
		    "\"nctds\":%hu,\"ctds\":[";
		break;
	}
	sbuf_printf(buf, fmtstr, (uintmax_t)smplcnt, (uintmax_t)compcnt,
	    maxctds, curctds);

	while ((is32bit ? NULL != ctd32 : NULL != ctd64)) {
		char qstr[qmaxstrlen];

		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = "\n\t\t\t\t";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = "{";
			break;
		}
		sbuf_cat(buf, fmtstr);

		if (objdump) {
			switch (fmt) {
			case SB_STRFMT_FREEFORM:
				fmtstr = "ctd[%hu].";
				break;
			case SB_STRFMT_JSON:
			default:
				fmtstr = "\"ctd\":%hu,";
				break;
			}
			sbuf_printf(buf, fmtstr, is32bit ?
			    ARB_SELFIDX(ctd32tree, ctd32) :
			    ARB_SELFIDX(ctd64tree, ctd64));
		}

		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = "{mu=";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = "\"mu\":";
			break;
		}
		sbuf_cat(buf, fmtstr);
		Q_TOSTR((is32bit ? ctd32->mu : ctd64->mu), -1, 10, qstr,
		    sizeof(qstr));
		sbuf_cat(buf, qstr);

		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
			break;
		}
		sbuf_printf(buf, fmtstr,
		    is32bit ? ctd32->cnt : (uintmax_t)ctd64->cnt);

		if (is32bit)
			ctd32 = (objdump ? ARB_CNODE(ctd32tree,
			    ARB_SELFIDX(ctd32tree, ctd32) + 1) :
			    ARB_CNEXT(ctdth32, ctd32tree, ctd32));
		else
			ctd64 = (objdump ? ARB_CNODE(ctd64tree,
			    ARB_SELFIDX(ctd64tree, ctd64) + 1) :
			    ARB_CNEXT(ctdth64, ctd64tree, ctd64));

		if (fmt == SB_STRFMT_JSON &&
		    (is32bit ? NULL != ctd32 : NULL != ctd64))
			sbuf_putc(buf, ',');
	}
	if (fmt == SB_STRFMT_JSON)
		sbuf_cat(buf, "]");
}

static inline void
stats_voistatdata_hist_tostr(enum vsd_dtype voi_dtype,
    const struct voistatdata_hist *hist, enum vsd_dtype hist_dtype,
    size_t hist_dsz, enum sb_str_fmt fmt, struct sbuf *buf, int objdump)
{
	const struct voistatdata_numeric *bkt_lb, *bkt_ub;
	const char *fmtstr;
	int is32bit;
	uint16_t i, nbkts;

	switch (hist_dtype) {
	case VSD_DTYPE_CRHIST32:
		nbkts = HIST_VSDSZ2NBKTS(crhist32, hist_dsz);
		is32bit = 1;
		break;
	case VSD_DTYPE_DRHIST32:
		nbkts = HIST_VSDSZ2NBKTS(drhist32, hist_dsz);
		is32bit = 1;
		break;
	case VSD_DTYPE_DVHIST32:
		nbkts = HIST_VSDSZ2NBKTS(dvhist32, hist_dsz);
		is32bit = 1;
		break;
	case VSD_DTYPE_CRHIST64:
		nbkts = HIST_VSDSZ2NBKTS(crhist64, hist_dsz);
		is32bit = 0;
		break;
	case VSD_DTYPE_DRHIST64:
		nbkts = HIST_VSDSZ2NBKTS(drhist64, hist_dsz);
		is32bit = 0;
		break;
	case VSD_DTYPE_DVHIST64:
		nbkts = HIST_VSDSZ2NBKTS(dvhist64, hist_dsz);
		is32bit = 0;
		break;
	default:
		return;
	}

	switch (fmt) {
	case SB_STRFMT_FREEFORM:
		fmtstr = "nbkts=%hu, ";
		break;
	case SB_STRFMT_JSON:
	default:
		fmtstr = "\"nbkts\":%hu,";
		break;
	}
	sbuf_printf(buf, fmtstr, nbkts);

	switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = (is32bit ? "oob=%u" : "oob=%ju");
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = (is32bit ? "\"oob\":%u,\"bkts\":[" :
			    "\"oob\":%ju,\"bkts\":[");
			break;
	}
	sbuf_printf(buf, fmtstr, is32bit ? VSD_CONSTHIST_FIELDVAL(hist,
	    hist_dtype, oob) : (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist,
	    hist_dtype, oob));

	for (i = 0; i < nbkts; i++) {
		switch (hist_dtype) {
		case VSD_DTYPE_CRHIST32:
		case VSD_DTYPE_CRHIST64:
			bkt_lb = VSD_CONSTCRHIST_FIELDPTR(hist, hist_dtype,
			    bkts[i].lb);
			if (i < nbkts - 1)
				bkt_ub = VSD_CONSTCRHIST_FIELDPTR(hist,
				    hist_dtype, bkts[i + 1].lb);
			else
				bkt_ub = &numeric_limits[LIM_MAX][voi_dtype];
			break;
		case VSD_DTYPE_DRHIST32:
		case VSD_DTYPE_DRHIST64:
			bkt_lb = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
			    bkts[i].lb);
			bkt_ub = VSD_CONSTDRHIST_FIELDPTR(hist, hist_dtype,
			    bkts[i].ub);
			break;
		case VSD_DTYPE_DVHIST32:
		case VSD_DTYPE_DVHIST64:
			bkt_lb = bkt_ub = VSD_CONSTDVHIST_FIELDPTR(hist,
			    hist_dtype, bkts[i].val);
			break;
		default:
			break;
		}

		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = "\n\t\t\t\t";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = "{";
			break;
		}
		sbuf_cat(buf, fmtstr);

		if (objdump) {
			switch (fmt) {
			case SB_STRFMT_FREEFORM:
				fmtstr = "bkt[%hu].";
				break;
			case SB_STRFMT_JSON:
			default:
				fmtstr = "\"bkt\":%hu,";
				break;
			}
			sbuf_printf(buf, fmtstr, i);
		}

		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = "{lb=";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = "\"lb\":";
			break;
		}
		sbuf_cat(buf, fmtstr);
		stats_voistatdata_tostr((const struct voistatdata *)bkt_lb,
		    voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
		    fmt, buf, objdump);

		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = ",ub=";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = ",\"ub\":";
			break;
		}
		sbuf_cat(buf, fmtstr);
		stats_voistatdata_tostr((const struct voistatdata *)bkt_ub,
		    voi_dtype, voi_dtype, sizeof(struct voistatdata_numeric),
		    fmt, buf, objdump);

		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = is32bit ? ",cnt=%u}" : ",cnt=%ju}";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = is32bit ? ",\"cnt\":%u}" : ",\"cnt\":%ju}";
			break;
		}
		sbuf_printf(buf, fmtstr, is32bit ?
		    VSD_CONSTHIST_FIELDVAL(hist, hist_dtype, bkts[i].cnt) :
		    (uintmax_t)VSD_CONSTHIST_FIELDVAL(hist, hist_dtype,
		    bkts[i].cnt));

		if (fmt == SB_STRFMT_JSON && i < nbkts - 1)
			sbuf_putc(buf, ',');
	}
	if (fmt == SB_STRFMT_JSON)
		sbuf_cat(buf, "]");
}

int
stats_voistatdata_tostr(const struct voistatdata *vsd, enum vsd_dtype voi_dtype,
    enum vsd_dtype vsd_dtype, size_t vsd_sz, enum sb_str_fmt fmt,
    struct sbuf *buf, int objdump)
{
	const char *fmtstr;

	if (vsd == NULL || buf == NULL || voi_dtype >= VSD_NUM_DTYPES ||
	    vsd_dtype >= VSD_NUM_DTYPES || fmt >= SB_STRFMT_NUM_FMTS)
		return (EINVAL);

	switch (vsd_dtype) {
	case VSD_DTYPE_VOISTATE:
		switch (fmt) {
		case SB_STRFMT_FREEFORM:
			fmtstr = "prev=";
			break;
		case SB_STRFMT_JSON:
		default:
			fmtstr = "\"prev\":";
			break;
		}
		sbuf_cat(buf, fmtstr);
		/*
		 * Render prev by passing it as *vsd and voi_dtype as vsd_dtype.
		 */
		stats_voistatdata_tostr(
		    (const struct voistatdata *)&CONSTVSD(voistate, vsd)->prev,
		    voi_dtype, voi_dtype, vsd_sz, fmt, buf, objdump);
		break;
	case VSD_DTYPE_INT_S32:
		sbuf_printf(buf, "%d", vsd->int32.s32);
		break;
	case VSD_DTYPE_INT_U32:
		sbuf_printf(buf, "%u", vsd->int32.u32);
		break;
	case VSD_DTYPE_INT_S64:
		sbuf_printf(buf, "%jd", (intmax_t)vsd->int64.s64);
		break;
	case VSD_DTYPE_INT_U64:
		sbuf_printf(buf, "%ju", (uintmax_t)vsd->int64.u64);
		break;
	case VSD_DTYPE_INT_SLONG:
		sbuf_printf(buf, "%ld", vsd->intlong.slong);
		break;
	case VSD_DTYPE_INT_ULONG:
		sbuf_printf(buf, "%lu", vsd->intlong.ulong);
		break;
	case VSD_DTYPE_Q_S32:
		{
		char qstr[Q_MAXSTRLEN(vsd->q32.sq32, 10)];
		Q_TOSTR((s32q_t)vsd->q32.sq32, -1, 10, qstr, sizeof(qstr));
		sbuf_cat(buf, qstr);
		}
		break;
	case VSD_DTYPE_Q_U32:
		{
		char qstr[Q_MAXSTRLEN(vsd->q32.uq32, 10)];
		Q_TOSTR((u32q_t)vsd->q32.uq32, -1, 10, qstr, sizeof(qstr));
		sbuf_cat(buf, qstr);
		}
		break;
	case VSD_DTYPE_Q_S64:
		{
		char qstr[Q_MAXSTRLEN(vsd->q64.sq64, 10)];
		Q_TOSTR((s64q_t)vsd->q64.sq64, -1, 10, qstr, sizeof(qstr));
		sbuf_cat(buf, qstr);
		}
		break;
	case VSD_DTYPE_Q_U64:
		{
		char qstr[Q_MAXSTRLEN(vsd->q64.uq64, 10)];
		Q_TOSTR((u64q_t)vsd->q64.uq64, -1, 10, qstr, sizeof(qstr));
		sbuf_cat(buf, qstr);
		}
		break;
	case VSD_DTYPE_CRHIST32:
	case VSD_DTYPE_DRHIST32:
	case VSD_DTYPE_DVHIST32:
	case VSD_DTYPE_CRHIST64:
	case VSD_DTYPE_DRHIST64:
	case VSD_DTYPE_DVHIST64:
		stats_voistatdata_hist_tostr(voi_dtype, CONSTVSD(hist, vsd),
		    vsd_dtype, vsd_sz, fmt, buf, objdump);
		break;
	case VSD_DTYPE_TDGSTCLUST32:
	case VSD_DTYPE_TDGSTCLUST64:
		stats_voistatdata_tdgst_tostr(voi_dtype,
		    CONSTVSD(tdgst, vsd), vsd_dtype, vsd_sz, fmt, buf,
		    objdump);
		break;
	default:
		break;
	}

	return (sbuf_error(buf));
}

static void
stats_v1_itercb_tostr_freeform(struct statsblobv1 *sb, struct voi *v,
    struct voistat *vs, struct sb_iter_ctx *ctx)
{
	struct sb_tostrcb_ctx *sctx;
	struct metablob *tpl_mb;
	struct sbuf *buf;
	void *vsd;
	uint8_t dump;

	sctx = ctx->usrctx;
	buf = sctx->buf;
	tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
	dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);

	if (ctx->flags & SB_IT_FIRST_CB) {
		sbuf_printf(buf, "struct statsblobv1@%p", sb);
		if (dump) {
			sbuf_printf(buf, ", abi=%hhu, endian=%hhu, maxsz=%hu, "
			    "cursz=%hu, created=%jd, lastrst=%jd, flags=0x%04hx, "
			    "stats_off=%hu, statsdata_off=%hu",
			    sb->abi, sb->endian, sb->maxsz, sb->cursz,
			    sb->created, sb->lastrst, sb->flags, sb->stats_off,
			    sb->statsdata_off);
		}
		sbuf_printf(buf, ", tplhash=%u", sb->tplhash);
	}

	if (ctx->flags & SB_IT_FIRST_VOISTAT) {
		sbuf_printf(buf, "\n\tvois[%hd]: id=%hd", ctx->vslot, v->id);
		if (v->id < 0)
			return;
		sbuf_printf(buf, ", name=\"%s\"", (tpl_mb == NULL) ? "" :
		    tpl_mb->voi_meta[v->id].name);
		if (dump)
		    sbuf_printf(buf, ", flags=0x%04hx, dtype=%s, "
		    "voistatmaxid=%hhd, stats_off=%hu", v->flags,
		    vsd_dtype2name[v->dtype], v->voistatmaxid, v->stats_off);
	}

	if (!dump && vs->stype <= 0)
		return;

	sbuf_printf(buf, "\n\t\tvois[%hd]stat[%hhd]: stype=", v->id, ctx->vsslot);
	if (vs->stype < 0) {
		sbuf_printf(buf, "%hhd", vs->stype);
		return;
	} else
		sbuf_printf(buf, "%s, errs=%hu", vs_stype2name[vs->stype],
		    vs->errs);
	vsd = BLOB_OFFSET(sb, vs->data_off);
	if (dump)
		sbuf_printf(buf, ", flags=0x%04x, dtype=%s, dsz=%hu, "
		    "data_off=%hu", vs->flags, vsd_dtype2name[vs->dtype],
		    vs->dsz, vs->data_off);

	sbuf_printf(buf, "\n\t\t\tvoistatdata: ");
	stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
	    sctx->fmt, buf, dump);
}

static void
stats_v1_itercb_tostr_json(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
    struct sb_iter_ctx *ctx)
{
	struct sb_tostrcb_ctx *sctx;
	struct metablob *tpl_mb;
	struct sbuf *buf;
	const char *fmtstr;
	void *vsd;
	uint8_t dump;

	sctx = ctx->usrctx;
	buf = sctx->buf;
	tpl_mb = sctx->tpl ? sctx->tpl->mb : NULL;
	dump = ((sctx->flags & SB_TOSTR_OBJDUMP) != 0);

	if (ctx->flags & SB_IT_FIRST_CB) {
		sbuf_putc(buf, '{');
		if (dump) {
			sbuf_printf(buf, "\"abi\":%hhu,\"endian\":%hhu,"
			    "\"maxsz\":%hu,\"cursz\":%hu,\"created\":%jd,"
			    "\"lastrst\":%jd,\"flags\":%hu,\"stats_off\":%hu,"
			    "\"statsdata_off\":%hu,", sb->abi,
			    sb->endian, sb->maxsz, sb->cursz, sb->created,
			    sb->lastrst, sb->flags, sb->stats_off,
			    sb->statsdata_off);
		}

		if (tpl_mb == NULL)
			fmtstr = "\"tplname\":%s,\"tplhash\":%u,\"vois\":{";
		else
			fmtstr = "\"tplname\":\"%s\",\"tplhash\":%u,\"vois\":{";

		sbuf_printf(buf, fmtstr, tpl_mb ? tpl_mb->tplname : "null",
		    sb->tplhash);
	}

	if (ctx->flags & SB_IT_FIRST_VOISTAT) {
		if (dump) {
			sbuf_printf(buf, "\"[%d]\":{\"id\":%d", ctx->vslot,
			    v->id);
			if (v->id < 0) {
				sbuf_printf(buf, "},");
				return;
			}
			
			if (tpl_mb == NULL)
				fmtstr = ",\"name\":%s,\"flags\":%hu,"
				    "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
				    "\"stats_off\":%hu,";
			else
				fmtstr = ",\"name\":\"%s\",\"flags\":%hu,"
				    "\"dtype\":\"%s\",\"voistatmaxid\":%hhd,"
				    "\"stats_off\":%hu,";

			sbuf_printf(buf, fmtstr, tpl_mb ?
			    tpl_mb->voi_meta[v->id].name : "null", v->flags,
			    vsd_dtype2name[v->dtype], v->voistatmaxid,
			    v->stats_off);
		} else {
			if (tpl_mb == NULL) {
				sbuf_printf(buf, "\"[%hd]\":{", v->id);
			} else {
				sbuf_printf(buf, "\"%s\":{",
				    tpl_mb->voi_meta[v->id].name);
			}
		}
		sbuf_cat(buf, "\"stats\":{");
	}

	vsd = BLOB_OFFSET(sb, vs->data_off);
	if (dump) {
		sbuf_printf(buf, "\"[%hhd]\":", ctx->vsslot);
		if (vs->stype < 0) {
			sbuf_printf(buf, "{\"stype\":-1},");
			return;
		}
		sbuf_printf(buf, "{\"stype\":\"%s\",\"errs\":%hu,\"flags\":%hu,"
		    "\"dtype\":\"%s\",\"data_off\":%hu,\"voistatdata\":{",
		    vs_stype2name[vs->stype], vs->errs, vs->flags,
		    vsd_dtype2name[vs->dtype], vs->data_off);
	} else if (vs->stype > 0) {
		if (tpl_mb == NULL)
			sbuf_printf(buf, "\"[%hhd]\":", vs->stype);
		else
			sbuf_printf(buf, "\"%s\":", vs_stype2name[vs->stype]);
	} else
		return;

	if ((vs->flags & VS_VSDVALID) || dump) {
		if (!dump)
			sbuf_printf(buf, "{\"errs\":%hu,", vs->errs);
		/* Simple non-compound VSD types need a key. */
		if (!vsd_compoundtype[vs->dtype])
			sbuf_cat(buf, "\"val\":");
		stats_voistatdata_tostr(vsd, v->dtype, vs->dtype, vs->dsz,
		    sctx->fmt, buf, dump);
		sbuf_cat(buf, dump ? "}}" : "}");
	} else
		sbuf_cat(buf, dump ? "null}" : "null");

	if (ctx->flags & SB_IT_LAST_VOISTAT)
		sbuf_cat(buf, "}}");

	if (ctx->flags & SB_IT_LAST_CB)
		sbuf_cat(buf, "}}");
	else
		sbuf_putc(buf, ',');
}

static int
stats_v1_itercb_tostr(struct statsblobv1 *sb, struct voi *v, struct voistat *vs,
    struct sb_iter_ctx *ctx)
{
	struct sb_tostrcb_ctx *sctx;

	sctx = ctx->usrctx;

	switch (sctx->fmt) {
	case SB_STRFMT_FREEFORM:
		stats_v1_itercb_tostr_freeform(sb, v, vs, ctx);
		break;
	case SB_STRFMT_JSON:
		stats_v1_itercb_tostr_json(sb, v, vs, ctx);
		break;
	default:
		break;
	}

	return (sbuf_error(sctx->buf));
}

int
stats_v1_blob_tostr(struct statsblobv1 *sb, struct sbuf *buf,
    enum sb_str_fmt fmt, uint32_t flags)
{
	struct sb_tostrcb_ctx sctx;
	uint32_t iflags;

	if (sb == NULL || sb->abi != STATS_ABI_V1 || buf == NULL ||
	    fmt >= SB_STRFMT_NUM_FMTS)
		return (EINVAL);

	sctx.buf = buf;
	sctx.fmt = fmt;
	sctx.flags = flags;

	if (flags & SB_TOSTR_META) {
		if (stats_tpl_fetch(stats_tpl_fetch_allocid(NULL, sb->tplhash),
		    &sctx.tpl))
			return (EINVAL);
	} else
		sctx.tpl = NULL;

	iflags = 0;
	if (flags & SB_TOSTR_OBJDUMP)
		iflags |= (SB_IT_NULLVOI | SB_IT_NULLVOISTAT);
	stats_v1_blob_iter(sb, stats_v1_itercb_tostr, &sctx, iflags);

	return (sbuf_error(buf));
}

static int
stats_v1_itercb_visit(struct statsblobv1 *sb, struct voi *v,
    struct voistat *vs, struct sb_iter_ctx *ctx)
{
	struct sb_visitcb_ctx *vctx;
	struct sb_visit sbv;

	vctx = ctx->usrctx;

	sbv.tplhash = sb->tplhash;
	sbv.voi_id = v->id;
	sbv.voi_dtype = v->dtype;
	sbv.vs_stype = vs->stype;
	sbv.vs_dtype = vs->dtype;
	sbv.vs_dsz = vs->dsz;
	sbv.vs_data = BLOB_OFFSET(sb, vs->data_off);
	sbv.vs_errs = vs->errs;
	sbv.flags = ctx->flags & (SB_IT_FIRST_CB | SB_IT_LAST_CB |
	    SB_IT_FIRST_VOI | SB_IT_LAST_VOI | SB_IT_FIRST_VOISTAT |
	    SB_IT_LAST_VOISTAT);

	return (vctx->cb(&sbv, vctx->usrctx));
}

int
stats_v1_blob_visit(struct statsblobv1 *sb, stats_blob_visitcb_t func,
    void *usrctx)
{
	struct sb_visitcb_ctx vctx;

	if (sb == NULL || sb->abi != STATS_ABI_V1 || func == NULL)
		return (EINVAL);

	vctx.cb = func;
	vctx.usrctx = usrctx;

	stats_v1_blob_iter(sb, stats_v1_itercb_visit, &vctx, 0);

	return (0);
}

static int
stats_v1_icb_reset_voistat(struct statsblobv1 *sb, struct voi *v __unused,
    struct voistat *vs, struct sb_iter_ctx *ctx __unused)
{
	void *vsd;

	if (vs->stype == VS_STYPE_VOISTATE)
		return (0);

	vsd = BLOB_OFFSET(sb, vs->data_off);

	/* Perform the stat type's default reset action. */
	switch (vs->stype) {
	case VS_STYPE_SUM:
		switch (vs->dtype) {
		case VSD_DTYPE_Q_S32:
			Q_SIFVAL(VSD(q32, vsd)->sq32, 0);
			break;
		case VSD_DTYPE_Q_U32:
			Q_SIFVAL(VSD(q32, vsd)->uq32, 0);
			break;
		case VSD_DTYPE_Q_S64:
			Q_SIFVAL(VSD(q64, vsd)->sq64, 0);
			break;
		case VSD_DTYPE_Q_U64:
			Q_SIFVAL(VSD(q64, vsd)->uq64, 0);
			break;
		default:
			bzero(vsd, vs->dsz);
			break;
		}
		break;
	case VS_STYPE_MAX:
		switch (vs->dtype) {
		case VSD_DTYPE_Q_S32:
			Q_SIFVAL(VSD(q32, vsd)->sq32,
			    Q_IFMINVAL(VSD(q32, vsd)->sq32));
			break;
		case VSD_DTYPE_Q_U32:
			Q_SIFVAL(VSD(q32, vsd)->uq32,
			    Q_IFMINVAL(VSD(q32, vsd)->uq32));
			break;
		case VSD_DTYPE_Q_S64:
			Q_SIFVAL(VSD(q64, vsd)->sq64,
			    Q_IFMINVAL(VSD(q64, vsd)->sq64));
			break;
		case VSD_DTYPE_Q_U64:
			Q_SIFVAL(VSD(q64, vsd)->uq64,
			    Q_IFMINVAL(VSD(q64, vsd)->uq64));
			break;
		default:
			memcpy(vsd, &numeric_limits[LIM_MIN][vs->dtype],
			    vs->dsz);
			break;
		}
		break;
	case VS_STYPE_MIN:
		switch (vs->dtype) {
		case VSD_DTYPE_Q_S32:
			Q_SIFVAL(VSD(q32, vsd)->sq32,
			    Q_IFMAXVAL(VSD(q32, vsd)->sq32));
			break;
		case VSD_DTYPE_Q_U32:
			Q_SIFVAL(VSD(q32, vsd)->uq32,
			    Q_IFMAXVAL(VSD(q32, vsd)->uq32));
			break;
		case VSD_DTYPE_Q_S64:
			Q_SIFVAL(VSD(q64, vsd)->sq64,
			    Q_IFMAXVAL(VSD(q64, vsd)->sq64));
			break;
		case VSD_DTYPE_Q_U64:
			Q_SIFVAL(VSD(q64, vsd)->uq64,
			    Q_IFMAXVAL(VSD(q64, vsd)->uq64));
			break;
		default:
			memcpy(vsd, &numeric_limits[LIM_MAX][vs->dtype],
			    vs->dsz);
			break;
		}
		break;
	case VS_STYPE_HIST:
		{
		/* Reset bucket counts. */
		struct voistatdata_hist *hist;
		int i, is32bit;
		uint16_t nbkts;

		hist = VSD(hist, vsd);
		switch (vs->dtype) {
		case VSD_DTYPE_CRHIST32:
			nbkts = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
			is32bit = 1;
			break;
		case VSD_DTYPE_DRHIST32:
			nbkts = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
			is32bit = 1;
			break;
		case VSD_DTYPE_DVHIST32:
			nbkts = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
			is32bit = 1;
			break;
		case VSD_DTYPE_CRHIST64:
			nbkts = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
			is32bit = 0;
			break;
		case VSD_DTYPE_DRHIST64:
			nbkts = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
			is32bit = 0;
			break;
		case VSD_DTYPE_DVHIST64:
			nbkts = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
			is32bit = 0;
			break;
		default:
			return (0);
		}

		bzero(VSD_HIST_FIELDPTR(hist, vs->dtype, oob),
		    is32bit ? sizeof(uint32_t) : sizeof(uint64_t));
		for (i = nbkts - 1; i >= 0; i--) {
			bzero(VSD_HIST_FIELDPTR(hist, vs->dtype,
			    bkts[i].cnt), is32bit ? sizeof(uint32_t) :
			    sizeof(uint64_t));
		}
		break;
		}
	case VS_STYPE_TDGST:
		{
		/* Reset sample count centroids array/tree. */
		struct voistatdata_tdgst *tdgst;
		struct ctdth32 *ctd32tree;
		struct ctdth64 *ctd64tree;
		struct voistatdata_tdgstctd32 *ctd32;
		struct voistatdata_tdgstctd64 *ctd64;

		tdgst = VSD(tdgst, vsd);
		switch (vs->dtype) {
		case VSD_DTYPE_TDGSTCLUST32:
			VSD(tdgstclust32, tdgst)->smplcnt = 0;
			VSD(tdgstclust32, tdgst)->compcnt = 0;
			ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
			ARB_INIT(ctd32, ctdlnk, ctd32tree,
			    ARB_MAXNODES(ctd32tree)) {
				ctd32->cnt = 0;
				Q_SIFVAL(ctd32->mu, 0);
			}
#ifdef DIAGNOSTIC
			RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
#endif
		break;
		case VSD_DTYPE_TDGSTCLUST64:
			VSD(tdgstclust64, tdgst)->smplcnt = 0;
			VSD(tdgstclust64, tdgst)->compcnt = 0;
			ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
			ARB_INIT(ctd64, ctdlnk, ctd64tree,
			    ARB_MAXNODES(ctd64tree)) {
				ctd64->cnt = 0;
				Q_SIFVAL(ctd64->mu, 0);
			}
#ifdef DIAGNOSTIC
			RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
#endif
		break;
		default:
			return (0);
		}
		break;
		}
	default:
		KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
		break;
	}

	vs->errs = 0;
	vs->flags &= ~VS_VSDVALID;

	return (0);
}

int
stats_v1_blob_snapshot(struct statsblobv1 **dst, size_t dstmaxsz,
    struct statsblobv1 *src, uint32_t flags)
{
	int error;

	if (src != NULL && src->abi == STATS_ABI_V1) {
		error = stats_v1_blob_clone(dst, dstmaxsz, src, flags);
		if (!error) {
			if (flags & SB_CLONE_RSTSRC) {
				stats_v1_blob_iter(src,
				    stats_v1_icb_reset_voistat, NULL, 0);
				src->lastrst = stats_sbinuptime();
			}
			stats_v1_blob_finalise(*dst);
		}
	} else
		error = EINVAL;

	return (error);
}

static inline int
stats_v1_voi_update_max(enum vsd_dtype voi_dtype __unused,
    struct voistatdata *voival, struct voistat *vs, void *vsd)
{
	int error;

	KASSERT(vs->dtype < VSD_NUM_DTYPES,
	    ("Unknown VSD dtype %d", vs->dtype));

	error = 0;

	switch (vs->dtype) {
	case VSD_DTYPE_INT_S32:
		if (VSD(int32, vsd)->s32 < voival->int32.s32) {
			VSD(int32, vsd)->s32 = voival->int32.s32;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_U32:
		if (VSD(int32, vsd)->u32 < voival->int32.u32) {
			VSD(int32, vsd)->u32 = voival->int32.u32;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_S64:
		if (VSD(int64, vsd)->s64 < voival->int64.s64) {
			VSD(int64, vsd)->s64 = voival->int64.s64;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_U64:
		if (VSD(int64, vsd)->u64 < voival->int64.u64) {
			VSD(int64, vsd)->u64 = voival->int64.u64;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_SLONG:
		if (VSD(intlong, vsd)->slong < voival->intlong.slong) {
			VSD(intlong, vsd)->slong = voival->intlong.slong;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_ULONG:
		if (VSD(intlong, vsd)->ulong < voival->intlong.ulong) {
			VSD(intlong, vsd)->ulong = voival->intlong.ulong;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_S32:
		if (Q_QLTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
		    voival->q32.sq32)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_U32:
		if (Q_QLTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
		    voival->q32.uq32)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_S64:
		if (Q_QLTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
		    voival->q64.sq64)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_U64:
		if (Q_QLTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
		    voival->q64.uq64)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	default:
		error = EINVAL;
		break;
	}

	return (error);
}

static inline int
stats_v1_voi_update_min(enum vsd_dtype voi_dtype __unused,
    struct voistatdata *voival, struct voistat *vs, void *vsd)
{
	int error;

	KASSERT(vs->dtype < VSD_NUM_DTYPES,
	    ("Unknown VSD dtype %d", vs->dtype));

	error = 0;

	switch (vs->dtype) {
	case VSD_DTYPE_INT_S32:
		if (VSD(int32, vsd)->s32 > voival->int32.s32) {
			VSD(int32, vsd)->s32 = voival->int32.s32;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_U32:
		if (VSD(int32, vsd)->u32 > voival->int32.u32) {
			VSD(int32, vsd)->u32 = voival->int32.u32;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_S64:
		if (VSD(int64, vsd)->s64 > voival->int64.s64) {
			VSD(int64, vsd)->s64 = voival->int64.s64;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_U64:
		if (VSD(int64, vsd)->u64 > voival->int64.u64) {
			VSD(int64, vsd)->u64 = voival->int64.u64;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_SLONG:
		if (VSD(intlong, vsd)->slong > voival->intlong.slong) {
			VSD(intlong, vsd)->slong = voival->intlong.slong;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_INT_ULONG:
		if (VSD(intlong, vsd)->ulong > voival->intlong.ulong) {
			VSD(intlong, vsd)->ulong = voival->intlong.ulong;
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_S32:
		if (Q_QGTQ(VSD(q32, vsd)->sq32, voival->q32.sq32) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->sq32,
		    voival->q32.sq32)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_U32:
		if (Q_QGTQ(VSD(q32, vsd)->uq32, voival->q32.uq32) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q32, vsd)->uq32,
		    voival->q32.uq32)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_S64:
		if (Q_QGTQ(VSD(q64, vsd)->sq64, voival->q64.sq64) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->sq64,
		    voival->q64.sq64)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	case VSD_DTYPE_Q_U64:
		if (Q_QGTQ(VSD(q64, vsd)->uq64, voival->q64.uq64) &&
		    (0 == (error = Q_QCPYVALQ(&VSD(q64, vsd)->uq64,
		    voival->q64.uq64)))) {
			vs->flags |= VS_VSDVALID;
		}
		break;
	default:
		error = EINVAL;
		break;
	}

	return (error);
}

static inline int
stats_v1_voi_update_sum(enum vsd_dtype voi_dtype __unused,
    struct voistatdata *voival, struct voistat *vs, void *vsd)
{
	int error;

	KASSERT(vs->dtype < VSD_NUM_DTYPES,
	    ("Unknown VSD dtype %d", vs->dtype));

	error = 0;

	switch (vs->dtype) {
	case VSD_DTYPE_INT_S32:
		VSD(int32, vsd)->s32 += voival->int32.s32;
		break;
	case VSD_DTYPE_INT_U32:
		VSD(int32, vsd)->u32 += voival->int32.u32;
		break;
	case VSD_DTYPE_INT_S64:
		VSD(int64, vsd)->s64 += voival->int64.s64;
		break;
	case VSD_DTYPE_INT_U64:
		VSD(int64, vsd)->u64 += voival->int64.u64;
		break;
	case VSD_DTYPE_INT_SLONG:
		VSD(intlong, vsd)->slong += voival->intlong.slong;
		break;
	case VSD_DTYPE_INT_ULONG:
		VSD(intlong, vsd)->ulong += voival->intlong.ulong;
		break;
	case VSD_DTYPE_Q_S32:
		error = Q_QADDQ(&VSD(q32, vsd)->sq32, voival->q32.sq32);
		break;
	case VSD_DTYPE_Q_U32:
		error = Q_QADDQ(&VSD(q32, vsd)->uq32, voival->q32.uq32);
		break;
	case VSD_DTYPE_Q_S64:
		error = Q_QADDQ(&VSD(q64, vsd)->sq64, voival->q64.sq64);
		break;
	case VSD_DTYPE_Q_U64:
		error = Q_QADDQ(&VSD(q64, vsd)->uq64, voival->q64.uq64);
		break;
	default:
		error = EINVAL;
		break;
	}

	if (!error)
		vs->flags |= VS_VSDVALID;

	return (error);
}

static inline int
stats_v1_voi_update_hist(enum vsd_dtype voi_dtype, struct voistatdata *voival,
    struct voistat *vs, struct voistatdata_hist *hist)
{
	struct voistatdata_numeric *bkt_lb, *bkt_ub;
	uint64_t *oob64, *cnt64;
	uint32_t *oob32, *cnt32;
	int error, i, found, is32bit, has_ub, eq_only;

	error = 0;

	switch (vs->dtype) {
	case VSD_DTYPE_CRHIST32:
		i = HIST_VSDSZ2NBKTS(crhist32, vs->dsz);
		is32bit = 1;
		has_ub = eq_only = 0;
		oob32 = &VSD(crhist32, hist)->oob;
		break;
	case VSD_DTYPE_DRHIST32:
		i = HIST_VSDSZ2NBKTS(drhist32, vs->dsz);
		is32bit = has_ub = 1;
		eq_only = 0;
		oob32 = &VSD(drhist32, hist)->oob;
		break;
	case VSD_DTYPE_DVHIST32:
		i = HIST_VSDSZ2NBKTS(dvhist32, vs->dsz);
		is32bit = eq_only = 1;
		has_ub = 0;
		oob32 = &VSD(dvhist32, hist)->oob;
		break;
	case VSD_DTYPE_CRHIST64:
		i = HIST_VSDSZ2NBKTS(crhist64, vs->dsz);
		is32bit = has_ub = eq_only = 0;
		oob64 = &VSD(crhist64, hist)->oob;
		break;
	case VSD_DTYPE_DRHIST64:
		i = HIST_VSDSZ2NBKTS(drhist64, vs->dsz);
		is32bit = eq_only = 0;
		has_ub = 1;
		oob64 = &VSD(drhist64, hist)->oob;
		break;
	case VSD_DTYPE_DVHIST64:
		i = HIST_VSDSZ2NBKTS(dvhist64, vs->dsz);
		is32bit = has_ub = 0;
		eq_only = 1;
		oob64 = &VSD(dvhist64, hist)->oob;
		break;
	default:
		return (EINVAL);
	}
	i--; /* Adjust for 0-based array index. */

	/* XXXLAS: Should probably use a better bucket search algorithm. ARB? */
	for (found = 0; i >= 0 && !found; i--) {
		switch (vs->dtype) {
		case VSD_DTYPE_CRHIST32:
			bkt_lb = &VSD(crhist32, hist)->bkts[i].lb;
			cnt32 = &VSD(crhist32, hist)->bkts[i].cnt;
			break;
		case VSD_DTYPE_DRHIST32:
			bkt_lb = &VSD(drhist32, hist)->bkts[i].lb;
			bkt_ub = &VSD(drhist32, hist)->bkts[i].ub;
			cnt32 = &VSD(drhist32, hist)->bkts[i].cnt;
			break;
		case VSD_DTYPE_DVHIST32:
			bkt_lb = &VSD(dvhist32, hist)->bkts[i].val;
			cnt32 = &VSD(dvhist32, hist)->bkts[i].cnt;
			break;
		case VSD_DTYPE_CRHIST64:
			bkt_lb = &VSD(crhist64, hist)->bkts[i].lb;
			cnt64 = &VSD(crhist64, hist)->bkts[i].cnt;
			break;
		case VSD_DTYPE_DRHIST64:
			bkt_lb = &VSD(drhist64, hist)->bkts[i].lb;
			bkt_ub = &VSD(drhist64, hist)->bkts[i].ub;
			cnt64 = &VSD(drhist64, hist)->bkts[i].cnt;
			break;
		case VSD_DTYPE_DVHIST64:
			bkt_lb = &VSD(dvhist64, hist)->bkts[i].val;
			cnt64 = &VSD(dvhist64, hist)->bkts[i].cnt;
			break;
		default:
			return (EINVAL);
		}

		switch (voi_dtype) {
		case VSD_DTYPE_INT_S32:
			if (voival->int32.s32 >= bkt_lb->int32.s32) {
				if ((eq_only && voival->int32.s32 ==
				    bkt_lb->int32.s32) ||
				    (!eq_only && (!has_ub ||
				    voival->int32.s32 < bkt_ub->int32.s32)))
					found = 1;
			}
			break;
		case VSD_DTYPE_INT_U32:
			if (voival->int32.u32 >= bkt_lb->int32.u32) {
				if ((eq_only && voival->int32.u32 ==
				    bkt_lb->int32.u32) ||
				    (!eq_only && (!has_ub ||
				    voival->int32.u32 < bkt_ub->int32.u32)))
					found = 1;
			}
			break;
		case VSD_DTYPE_INT_S64:
			if (voival->int64.s64 >= bkt_lb->int64.s64)
				if ((eq_only && voival->int64.s64 ==
				    bkt_lb->int64.s64) ||
				    (!eq_only && (!has_ub ||
				    voival->int64.s64 < bkt_ub->int64.s64)))
					found = 1;
			break;
		case VSD_DTYPE_INT_U64:
			if (voival->int64.u64 >= bkt_lb->int64.u64)
				if ((eq_only && voival->int64.u64 ==
				    bkt_lb->int64.u64) ||
				    (!eq_only && (!has_ub ||
				    voival->int64.u64 < bkt_ub->int64.u64)))
					found = 1;
			break;
		case VSD_DTYPE_INT_SLONG:
			if (voival->intlong.slong >= bkt_lb->intlong.slong)
				if ((eq_only && voival->intlong.slong ==
				    bkt_lb->intlong.slong) ||
				    (!eq_only && (!has_ub ||
				    voival->intlong.slong <
				    bkt_ub->intlong.slong)))
					found = 1;
			break;
		case VSD_DTYPE_INT_ULONG:
			if (voival->intlong.ulong >= bkt_lb->intlong.ulong)
				if ((eq_only && voival->intlong.ulong ==
				    bkt_lb->intlong.ulong) ||
				    (!eq_only && (!has_ub ||
				    voival->intlong.ulong <
				    bkt_ub->intlong.ulong)))
					found = 1;
			break;
		case VSD_DTYPE_Q_S32:
			if (Q_QGEQ(voival->q32.sq32, bkt_lb->q32.sq32))
				if ((eq_only && Q_QEQ(voival->q32.sq32,
				    bkt_lb->q32.sq32)) ||
				    (!eq_only && (!has_ub ||
				    Q_QLTQ(voival->q32.sq32,
				    bkt_ub->q32.sq32))))
					found = 1;
			break;
		case VSD_DTYPE_Q_U32:
			if (Q_QGEQ(voival->q32.uq32, bkt_lb->q32.uq32))
				if ((eq_only && Q_QEQ(voival->q32.uq32,
				    bkt_lb->q32.uq32)) ||
				    (!eq_only && (!has_ub ||
				    Q_QLTQ(voival->q32.uq32,
				    bkt_ub->q32.uq32))))
					found = 1;
			break;
		case VSD_DTYPE_Q_S64:
			if (Q_QGEQ(voival->q64.sq64, bkt_lb->q64.sq64))
				if ((eq_only && Q_QEQ(voival->q64.sq64,
				    bkt_lb->q64.sq64)) ||
				    (!eq_only && (!has_ub ||
				    Q_QLTQ(voival->q64.sq64,
				    bkt_ub->q64.sq64))))
					found = 1;
			break;
		case VSD_DTYPE_Q_U64:
			if (Q_QGEQ(voival->q64.uq64, bkt_lb->q64.uq64))
				if ((eq_only && Q_QEQ(voival->q64.uq64,
				    bkt_lb->q64.uq64)) ||
				    (!eq_only && (!has_ub ||
				    Q_QLTQ(voival->q64.uq64,
				    bkt_ub->q64.uq64))))
					found = 1;
			break;
		default:
			break;
		}
	}

	if (found) {
		if (is32bit)
			*cnt32 += 1;
		else
			*cnt64 += 1;
	} else {
		if (is32bit)
			*oob32 += 1;
		else
			*oob64 += 1;
	}

	vs->flags |= VS_VSDVALID;
	return (error);
}

static inline int
stats_v1_vsd_tdgst_compress(enum vsd_dtype vs_dtype,
    struct voistatdata_tdgst *tdgst, int attempt)
{
	struct ctdth32 *ctd32tree;
	struct ctdth64 *ctd64tree;
	struct voistatdata_tdgstctd32 *ctd32;
	struct voistatdata_tdgstctd64 *ctd64;
	uint64_t ebits, idxmask;
	uint32_t bitsperidx, nebits;
	int error, idx, is32bit, maxctds, remctds, tmperr;

	error = 0;

	switch (vs_dtype) {
	case VSD_DTYPE_TDGSTCLUST32:
		ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
		if (!ARB_FULL(ctd32tree))
			return (0);
		VSD(tdgstclust32, tdgst)->compcnt++;
		maxctds = remctds = ARB_MAXNODES(ctd32tree);
		ARB_RESET_TREE(ctd32tree, ctdth32, maxctds);
		VSD(tdgstclust32, tdgst)->smplcnt = 0;
		is32bit = 1;
		ctd64tree = NULL;
		ctd64 = NULL;
#ifdef DIAGNOSTIC
		RB_INIT(&VSD(tdgstclust32, tdgst)->rbctdtree);
#endif
		break;
	case VSD_DTYPE_TDGSTCLUST64:
		ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
		if (!ARB_FULL(ctd64tree))
			return (0);
		VSD(tdgstclust64, tdgst)->compcnt++;
		maxctds = remctds = ARB_MAXNODES(ctd64tree);
		ARB_RESET_TREE(ctd64tree, ctdth64, maxctds);
		VSD(tdgstclust64, tdgst)->smplcnt = 0;
		is32bit = 0;
		ctd32tree = NULL;
		ctd32 = NULL;
#ifdef DIAGNOSTIC
		RB_INIT(&VSD(tdgstclust64, tdgst)->rbctdtree);
#endif
		break;
	default:
		return (EINVAL);
	}

	/*
	 * Rebuild the t-digest ARB by pseudorandomly selecting centroids and
	 * re-inserting the mu/cnt of each as a value and corresponding weight.
	 */

	/*
	 * XXXCEM: random(9) is currently rand(3), not random(3).  rand(3)
	 * RAND_MAX happens to be approximately 31 bits (range [0,
	 * 0x7ffffffd]), so the math kinda works out.  When/if this portion of
	 * the code is compiled in userspace, it gets the random(3) behavior,
	 * which has expected range [0, 0x7fffffff].
	 */
#define	bitsperrand 31
	ebits = 0;
	nebits = 0;
	bitsperidx = fls(maxctds);
	KASSERT(bitsperidx <= sizeof(ebits) << 3,
	    ("%s: bitsperidx=%d, ebits=%d",
	    __func__, bitsperidx, (int)(sizeof(ebits) << 3)));
	idxmask = (UINT64_C(1) << bitsperidx) - 1;

	/* Initialise the free list with randomised centroid indices. */
	for (; remctds > 0; remctds--) {
		while (nebits < bitsperidx) {
			ebits |= ((uint64_t)random()) << nebits;
			nebits += bitsperrand;
			if (nebits > (sizeof(ebits) << 3))
				nebits = sizeof(ebits) << 3;
		}
		idx = ebits & idxmask;
		nebits -= bitsperidx;
		ebits >>= bitsperidx;

		/*
		 * Select the next centroid to put on the ARB free list. We
		 * start with the centroid at our randomly selected array index,
		 * and work our way forwards until finding one (the latter
		 * aspect reduces re-insertion randomness, but is good enough).
		 */
		do {
			if (idx >= maxctds)
				idx %= maxctds;

			if (is32bit)
				ctd32 = ARB_NODE(ctd32tree, idx);
			else
				ctd64 = ARB_NODE(ctd64tree, idx);
		} while ((is32bit ? ARB_ISFREE(ctd32, ctdlnk) :
		    ARB_ISFREE(ctd64, ctdlnk)) && ++idx);

		/* Put the centroid on the ARB free list. */
		if (is32bit)
			ARB_RETURNFREE(ctd32tree, ctd32, ctdlnk);
		else
			ARB_RETURNFREE(ctd64tree, ctd64, ctdlnk);
	}

	/*
	 * The free list now contains the randomised indices of every centroid.
	 * Walk the free list from start to end, re-inserting each centroid's
	 * mu/cnt. The tdgst_add() call may or may not consume the free centroid
	 * we re-insert values from during each loop iteration, so we must latch
	 * the index of the next free list centroid before the re-insertion
	 * call. The previous loop above should have left the centroid pointer
	 * pointing to the element at the head of the free list.
	 */
	KASSERT((is32bit ?
	    ARB_FREEIDX(ctd32tree) == ARB_SELFIDX(ctd32tree, ctd32) :
	    ARB_FREEIDX(ctd64tree) == ARB_SELFIDX(ctd64tree, ctd64)),
	    ("%s: t-digest ARB@%p free list bug", __func__,
	    (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));
	remctds = maxctds;
	while ((is32bit ? ctd32 != NULL : ctd64 != NULL)) {
		tmperr = 0;
		if (is32bit) {
			s64q_t x;

			idx = ARB_NEXTFREEIDX(ctd32, ctdlnk);
			/* Cloning a s32q_t into a s64q_t should never fail. */
			tmperr = Q_QCLONEQ(&x, ctd32->mu);
			tmperr = tmperr ? tmperr : stats_v1_vsd_tdgst_add(
			    vs_dtype, tdgst, x, ctd32->cnt, attempt);
			ctd32 = ARB_NODE(ctd32tree, idx);
			KASSERT(ctd32 == NULL || ARB_ISFREE(ctd32, ctdlnk),
			    ("%s: t-digest ARB@%p free list bug", __func__,
			    ctd32tree));
		} else {
			idx = ARB_NEXTFREEIDX(ctd64, ctdlnk);
			tmperr = stats_v1_vsd_tdgst_add(vs_dtype, tdgst,
			    ctd64->mu, ctd64->cnt, attempt);
			ctd64 = ARB_NODE(ctd64tree, idx);
			KASSERT(ctd64 == NULL || ARB_ISFREE(ctd64, ctdlnk),
			    ("%s: t-digest ARB@%p free list bug", __func__,
			    ctd64tree));
		}
		/*
		 * This process should not produce errors, bugs notwithstanding.
		 * Just in case, latch any errors and attempt all re-insertions.
		 */
		error = tmperr ? tmperr : error;
		remctds--;
	}

	KASSERT(remctds == 0, ("%s: t-digest ARB@%p free list bug", __func__,
	    (is32bit ? (void *)ctd32tree : (void *)ctd64tree)));

	return (error);
}

static inline int
stats_v1_vsd_tdgst_add(enum vsd_dtype vs_dtype, struct voistatdata_tdgst *tdgst,
    s64q_t x, uint64_t weight, int attempt)
{
#ifdef DIAGNOSTIC
	char qstr[Q_MAXSTRLEN(x, 10)];
#endif
	struct ctdth32 *ctd32tree;
	struct ctdth64 *ctd64tree;
	void *closest, *cur, *lb, *ub;
	struct voistatdata_tdgstctd32 *ctd32;
	struct voistatdata_tdgstctd64 *ctd64;
	uint64_t cnt, smplcnt, sum, tmpsum;
	s64q_t k, minz, q, z;
	int error, is32bit, n;

	error = 0;
	minz = Q_INI(&z, 0, 0, Q_NFBITS(x));

	switch (vs_dtype) {
	case VSD_DTYPE_TDGSTCLUST32:
		if ((UINT32_MAX - weight) < VSD(tdgstclust32, tdgst)->smplcnt)
			error = EOVERFLOW;
		smplcnt = VSD(tdgstclust32, tdgst)->smplcnt;
		ctd32tree = &VSD(tdgstclust32, tdgst)->ctdtree;
		is32bit = 1;
		ctd64tree = NULL;
		ctd64 = NULL;
		break;
	case VSD_DTYPE_TDGSTCLUST64:
		if ((UINT64_MAX - weight) < VSD(tdgstclust64, tdgst)->smplcnt)
			error = EOVERFLOW;
		smplcnt = VSD(tdgstclust64, tdgst)->smplcnt;
		ctd64tree = &VSD(tdgstclust64, tdgst)->ctdtree;
		is32bit = 0;
		ctd32tree = NULL;
		ctd32 = NULL;
		break;
	default:
		error = EINVAL;
		break;
	}

	if (error)
		return (error);

	/*
	 * Inspired by Ted Dunning's AVLTreeDigest.java
	 */
	do {
#if defined(DIAGNOSTIC)
		KASSERT(attempt < 5,
		    ("%s: Too many attempts", __func__));
#endif
		if (attempt >= 5)
			return (EAGAIN);

		Q_SIFVAL(minz, Q_IFMAXVAL(minz));
		closest = ub = NULL;
		sum = tmpsum = 0;

		if (is32bit)
			lb = cur = (void *)(ctd32 = ARB_MIN(ctdth32, ctd32tree));
		else
			lb = cur = (void *)(ctd64 = ARB_MIN(ctdth64, ctd64tree));

		if (lb == NULL) /* Empty tree. */
			lb = (is32bit ? (void *)ARB_ROOT(ctd32tree) :
			    (void *)ARB_ROOT(ctd64tree));

		/*
		 * Find the set of centroids with minimum distance to x and
		 * compute the sum of counts for all centroids with mean less
		 * than the first centroid in the set.
		 */
		for (; cur != NULL;
		    cur = (is32bit ?
		    (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
		    (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
			if (is32bit) {
				cnt = ctd32->cnt;
				KASSERT(Q_PRECEQ(ctd32->mu, x),
				    ("%s: Q_RELPREC(mu,x)=%d", __func__,
				    Q_RELPREC(ctd32->mu, x)));
				/* Ok to assign as both have same precision. */
				z = ctd32->mu;
			} else {
				cnt = ctd64->cnt;
				KASSERT(Q_PRECEQ(ctd64->mu, x),
				    ("%s: Q_RELPREC(mu,x)=%d", __func__,
				    Q_RELPREC(ctd64->mu, x)));
				/* Ok to assign as both have same precision. */
				z = ctd64->mu;
			}

			error = Q_QSUBQ(&z, x);
#if defined(DIAGNOSTIC)
			KASSERT(!error, ("%s: unexpected error %d", __func__,
			    error));
#endif
			if (error)
				return (error);

			z = Q_QABS(z);
			if (Q_QLTQ(z, minz)) {
				minz = z;
				lb = cur;
				sum = tmpsum;
				tmpsum += cnt;
			} else if (Q_QGTQ(z, minz)) {
				ub = cur;
				break;
			}
		}

		cur = (is32bit ?
		    (void *)(ctd32 = (struct voistatdata_tdgstctd32 *)lb) :
		    (void *)(ctd64 = (struct voistatdata_tdgstctd64 *)lb));

		for (n = 0; cur != ub; cur = (is32bit ?
		    (void *)(ctd32 = ARB_NEXT(ctdth32, ctd32tree, ctd32)) :
		    (void *)(ctd64 = ARB_NEXT(ctdth64, ctd64tree, ctd64)))) {
			if (is32bit)
				cnt = ctd32->cnt;
			else
				cnt = ctd64->cnt;

			q = Q_CTRLINI(16);
			if (smplcnt == 1)
				error = Q_QFRACI(&q, 1, 2);
			else
				/* [ sum + ((cnt - 1) / 2) ] / (smplcnt - 1) */
				error = Q_QFRACI(&q, (sum << 1) + cnt - 1,
				    (smplcnt - 1) << 1);
			k = q;
			/* k = q x 4 x samplcnt x attempt */
			error |= Q_QMULI(&k, 4 * smplcnt * attempt);
			/* k = k x (1 - q) */
			error |= Q_QSUBI(&q, 1);
			q = Q_QABS(q);
			error |= Q_QMULQ(&k, q);
#if defined(DIAGNOSTIC)
#if !defined(_KERNEL)
			double q_dbl, k_dbl, q2d, k2d;
			q2d = Q_Q2D(q);
			k2d = Q_Q2D(k);
			q_dbl = smplcnt == 1 ? 0.5 :
			    (sum + ((cnt - 1)  / 2.0)) / (double)(smplcnt - 1);
			k_dbl = 4 * smplcnt * q_dbl * (1.0 - q_dbl) * attempt;
			/*
			 * If the difference between q and q_dbl is greater than
			 * the fractional precision of q, something is off.
			 * NB: q is holding the value of 1 - q
			 */
			q_dbl = 1.0 - q_dbl;
			KASSERT((q_dbl > q2d ? q_dbl - q2d : q2d - q_dbl) <
			    (1.05 * ((double)1 / (double)(1ULL << Q_NFBITS(q)))),
			    ("Q-type q bad precision"));
			KASSERT((k_dbl > k2d ? k_dbl - k2d : k2d - k_dbl) <
			    1.0 + (0.01 * smplcnt),
			    ("Q-type k bad precision"));
#endif /* !_KERNEL */
			KASSERT(!error, ("%s: unexpected error %d", __func__,
			    error));
#endif /* DIAGNOSTIC */
			if (error)
				return (error);
			if ((is32bit && ((ctd32->cnt + weight) <=
			    (uint64_t)Q_GIVAL(k))) ||
			    (!is32bit && ((ctd64->cnt + weight) <=
			    (uint64_t)Q_GIVAL(k)))) {
				n++;
				/* random() produces 31 bits. */
				if (random() < (INT32_MAX / n))
					closest = cur;
			}
			sum += cnt;
		}
	} while (closest == NULL &&
	    (is32bit ? ARB_FULL(ctd32tree) : ARB_FULL(ctd64tree)) &&
	    (error = stats_v1_vsd_tdgst_compress(vs_dtype, tdgst,
	    attempt++)) == 0);

	if (error)
		return (error);

	if (closest != NULL) {
		/* Merge with an existing centroid. */
		if (is32bit) {
			ctd32 = (struct voistatdata_tdgstctd32 *)closest;
			error = Q_QSUBQ(&x, ctd32->mu);
			error = error ? error :
			    Q_QDIVI(&x, ctd32->cnt + weight);
			if (error || (error = Q_QADDQ(&ctd32->mu, x))) {
#ifdef DIAGNOSTIC
				KASSERT(!error, ("%s: unexpected error %d",
				    __func__, error));
#endif
				return (error);
			}
			ctd32->cnt += weight;
			error = ARB_REINSERT(ctdth32, ctd32tree, ctd32) ==
			    NULL ? 0 : EALREADY;
#ifdef DIAGNOSTIC
			RB_REINSERT(rbctdth32,
			    &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
#endif
		} else {
			ctd64 = (struct voistatdata_tdgstctd64 *)closest;
			error = Q_QSUBQ(&x, ctd64->mu);
			error = error ? error :
			    Q_QDIVI(&x, ctd64->cnt + weight);
			if (error || (error = Q_QADDQ(&ctd64->mu, x))) {
				KASSERT(!error, ("%s: unexpected error %d",
				    __func__, error));
				return (error);
			}
			ctd64->cnt += weight;
			error = ARB_REINSERT(ctdth64, ctd64tree, ctd64) ==
			    NULL ? 0 : EALREADY;
#ifdef DIAGNOSTIC
			RB_REINSERT(rbctdth64,
			    &VSD(tdgstclust64, tdgst)->rbctdtree, ctd64);
#endif
		}
	} else {
		/*
		 * Add a new centroid. If digest compression is working
		 * correctly, there should always be at least one free.
		 */
		if (is32bit) {
			ctd32 = ARB_GETFREE(ctd32tree, ctdlnk);
#ifdef DIAGNOSTIC
			KASSERT(ctd32 != NULL,
			    ("%s: t-digest@%p has no free centroids",
			    __func__, tdgst));
#endif
			if (ctd32 == NULL)
				return (EAGAIN);
			if ((error = Q_QCPYVALQ(&ctd32->mu, x)))
				return (error);
			ctd32->cnt = weight;
			error = ARB_INSERT(ctdth32, ctd32tree, ctd32) == NULL ?
			    0 : EALREADY;
#ifdef DIAGNOSTIC
			RB_INSERT(rbctdth32,
			    &VSD(tdgstclust32, tdgst)->rbctdtree, ctd32);
#endif
		} else {
			ctd64 = ARB_GETFREE(ctd64tree, ctdlnk);
#ifdef DIAGNOSTIC
			KASSERT(ctd64 != NULL,
			    ("%s: t-digest@%p has no free centroids",
			    __func__, tdgst));
#endif
			if (ctd64 == NULL) /* Should not happen. */
				return (EAGAIN);
			/* Direct assignment ok as both have same type/prec. */
			ctd64->mu = x;
			ctd64->cnt = weight;
			error = ARB_INSERT(ctdth64, ctd64tree, ctd64) == NULL ?
			    0 : EALREADY;
#ifdef DIAGNOSTIC
			RB_INSERT(rbctdth64, &VSD(tdgstclust64,
			    tdgst)->rbctdtree, ctd64);
#endif
		}
	}

	if (is32bit)
		VSD(tdgstclust32, tdgst)->smplcnt += weight;
	else {
		VSD(tdgstclust64, tdgst)->smplcnt += weight;

#ifdef DIAGNOSTIC
		struct rbctdth64 *rbctdtree =
		    &VSD(tdgstclust64, tdgst)->rbctdtree;
		struct voistatdata_tdgstctd64 *rbctd64;
		int i = 0;
		ARB_FOREACH(ctd64, ctdth64, ctd64tree) {
			rbctd64 = (i == 0 ? RB_MIN(rbctdth64, rbctdtree) :
			    RB_NEXT(rbctdth64, rbctdtree, rbctd64));

			if (i >= ARB_CURNODES(ctd64tree)
			    || ctd64 != rbctd64
			    || ARB_MIN(ctdth64, ctd64tree) !=
			       RB_MIN(rbctdth64, rbctdtree)
			    || ARB_MAX(ctdth64, ctd64tree) !=
			       RB_MAX(rbctdth64, rbctdtree)
			    || ARB_LEFTIDX(ctd64, ctdlnk) !=
			       ARB_SELFIDX(ctd64tree, RB_LEFT(rbctd64, rblnk))
			    || ARB_RIGHTIDX(ctd64, ctdlnk) !=
			       ARB_SELFIDX(ctd64tree, RB_RIGHT(rbctd64, rblnk))
			    || ARB_PARENTIDX(ctd64, ctdlnk) !=
			       ARB_SELFIDX(ctd64tree,
			       RB_PARENT(rbctd64, rblnk))) {
				Q_TOSTR(ctd64->mu, -1, 10, qstr, sizeof(qstr));
				printf("ARB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
				    "mu=%s\n",
				    (int)ARB_SELFIDX(ctd64tree, ctd64),
				    ARB_PARENTIDX(ctd64, ctdlnk),
				    ARB_LEFTIDX(ctd64, ctdlnk),
				    ARB_RIGHTIDX(ctd64, ctdlnk),
				    ARB_COLOR(ctd64, ctdlnk),
				    qstr);

				Q_TOSTR(rbctd64->mu, -1, 10, qstr,
				    sizeof(qstr));
				printf(" RB ctd=%3d p=%3d l=%3d r=%3d c=%2d "
				    "mu=%s\n",
				    (int)ARB_SELFIDX(ctd64tree, rbctd64),
				    (int)ARB_SELFIDX(ctd64tree,
				      RB_PARENT(rbctd64, rblnk)),
				    (int)ARB_SELFIDX(ctd64tree,
				      RB_LEFT(rbctd64, rblnk)),
				    (int)ARB_SELFIDX(ctd64tree,
				      RB_RIGHT(rbctd64, rblnk)),
				    RB_COLOR(rbctd64, rblnk),
				    qstr);

				panic("RB@%p and ARB@%p trees differ\n",
				    rbctdtree, ctd64tree);
			}
			i++;
		}
#endif /* DIAGNOSTIC */
	}

	return (error);
}

static inline int
stats_v1_voi_update_tdgst(enum vsd_dtype voi_dtype, struct voistatdata *voival,
    struct voistat *vs, struct voistatdata_tdgst *tdgst)
{
	s64q_t x;
	int error;

	error = 0;

	switch (vs->dtype) {
	case VSD_DTYPE_TDGSTCLUST32:
		/* Use same precision as the user's centroids. */
		Q_INI(&x, 0, 0, Q_NFBITS(
		    ARB_CNODE(&VSD(tdgstclust32, tdgst)->ctdtree, 0)->mu));
		break;
	case VSD_DTYPE_TDGSTCLUST64:
		/* Use same precision as the user's centroids. */
		Q_INI(&x, 0, 0, Q_NFBITS(
		    ARB_CNODE(&VSD(tdgstclust64, tdgst)->ctdtree, 0)->mu));
		break;
	default:
		KASSERT(vs->dtype == VSD_DTYPE_TDGSTCLUST32 ||
		    vs->dtype == VSD_DTYPE_TDGSTCLUST64,
		    ("%s: vs->dtype(%d) != VSD_DTYPE_TDGSTCLUST<32|64>",
		    __func__, vs->dtype));
		return (EINVAL);
	}

	/*
	 * XXXLAS: Should have both a signed and unsigned 'x' variable to avoid
	 * returning EOVERFLOW if the voival would have fit in a u64q_t.
	 */
	switch (voi_dtype) {
	case VSD_DTYPE_INT_S32:
		error = Q_QCPYVALI(&x, voival->int32.s32);
		break;
	case VSD_DTYPE_INT_U32:
		error = Q_QCPYVALI(&x, voival->int32.u32);
		break;
	case VSD_DTYPE_INT_S64:
		error = Q_QCPYVALI(&x, voival->int64.s64);
		break;
	case VSD_DTYPE_INT_U64:
		error = Q_QCPYVALI(&x, voival->int64.u64);
		break;
	case VSD_DTYPE_INT_SLONG:
		error = Q_QCPYVALI(&x, voival->intlong.slong);
		break;
	case VSD_DTYPE_INT_ULONG:
		error = Q_QCPYVALI(&x, voival->intlong.ulong);
		break;
	case VSD_DTYPE_Q_S32:
		error = Q_QCPYVALQ(&x, voival->q32.sq32);
		break;
	case VSD_DTYPE_Q_U32:
		error = Q_QCPYVALQ(&x, voival->q32.uq32);
		break;
	case VSD_DTYPE_Q_S64:
		error = Q_QCPYVALQ(&x, voival->q64.sq64);
		break;
	case VSD_DTYPE_Q_U64:
		error = Q_QCPYVALQ(&x, voival->q64.uq64);
		break;
	default:
		error = EINVAL;
		break;
	}

	if (error ||
	    (error = stats_v1_vsd_tdgst_add(vs->dtype, tdgst, x, 1, 1)))
		return (error);

	vs->flags |= VS_VSDVALID;
	return (0);
}

int
stats_v1_voi_update(struct statsblobv1 *sb, int32_t voi_id,
    enum vsd_dtype voi_dtype, struct voistatdata *voival, uint32_t flags)
{
	struct voi *v;
	struct voistat *vs;
	void *statevsd, *vsd;
	int error, i, tmperr;

	error = 0;

	if (sb == NULL || sb->abi != STATS_ABI_V1 || voi_id >= NVOIS(sb) ||
	    voi_dtype == 0 || voi_dtype >= VSD_NUM_DTYPES || voival == NULL)
		return (EINVAL);
	v = &sb->vois[voi_id];
	if (voi_dtype != v->dtype || v->id < 0 ||
	    ((flags & SB_VOI_RELUPDATE) && !(v->flags & VOI_REQSTATE)))
		return (EINVAL);

	vs = BLOB_OFFSET(sb, v->stats_off);
	if (v->flags & VOI_REQSTATE)
		statevsd = BLOB_OFFSET(sb, vs->data_off);
	else
		statevsd = NULL;

	if (flags & SB_VOI_RELUPDATE) {
		switch (voi_dtype) {
		case VSD_DTYPE_INT_S32:
			voival->int32.s32 +=
			    VSD(voistate, statevsd)->prev.int32.s32;
			break;
		case VSD_DTYPE_INT_U32:
			voival->int32.u32 +=
			    VSD(voistate, statevsd)->prev.int32.u32;
			break;
		case VSD_DTYPE_INT_S64:
			voival->int64.s64 +=
			    VSD(voistate, statevsd)->prev.int64.s64;
			break;
		case VSD_DTYPE_INT_U64:
			voival->int64.u64 +=
			    VSD(voistate, statevsd)->prev.int64.u64;
			break;
		case VSD_DTYPE_INT_SLONG:
			voival->intlong.slong +=
			    VSD(voistate, statevsd)->prev.intlong.slong;
			break;
		case VSD_DTYPE_INT_ULONG:
			voival->intlong.ulong +=
			    VSD(voistate, statevsd)->prev.intlong.ulong;
			break;
		case VSD_DTYPE_Q_S32:
			error = Q_QADDQ(&voival->q32.sq32,
			    VSD(voistate, statevsd)->prev.q32.sq32);
			break;
		case VSD_DTYPE_Q_U32:
			error = Q_QADDQ(&voival->q32.uq32,
			    VSD(voistate, statevsd)->prev.q32.uq32);
			break;
		case VSD_DTYPE_Q_S64:
			error = Q_QADDQ(&voival->q64.sq64,
			    VSD(voistate, statevsd)->prev.q64.sq64);
			break;
		case VSD_DTYPE_Q_U64:
			error = Q_QADDQ(&voival->q64.uq64,
			    VSD(voistate, statevsd)->prev.q64.uq64);
			break;
		default:
			KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
			break;
		}
	}

	if (error)
		return (error);

	for (i = v->voistatmaxid; i > 0; i--) {
		vs = &((struct voistat *)BLOB_OFFSET(sb, v->stats_off))[i];
		if (vs->stype < 0)
			continue;

		vsd = BLOB_OFFSET(sb, vs->data_off);

		switch (vs->stype) {
		case VS_STYPE_MAX:
			tmperr = stats_v1_voi_update_max(voi_dtype, voival,
			    vs, vsd);
			break;
		case VS_STYPE_MIN:
			tmperr = stats_v1_voi_update_min(voi_dtype, voival,
			    vs, vsd);
			break;
		case VS_STYPE_SUM:
			tmperr = stats_v1_voi_update_sum(voi_dtype, voival,
			    vs, vsd);
			break;
		case VS_STYPE_HIST:
			tmperr = stats_v1_voi_update_hist(voi_dtype, voival,
			    vs, vsd);
			break;
		case VS_STYPE_TDGST:
			tmperr = stats_v1_voi_update_tdgst(voi_dtype, voival,
			    vs, vsd);
			break;
		default:
			KASSERT(0, ("Unknown VOI stat type %d", vs->stype));
			break;
		}

		if (tmperr) {
			error = tmperr;
			VS_INCERRS(vs);
		}
	}

	if (statevsd) {
		switch (voi_dtype) {
		case VSD_DTYPE_INT_S32:
			VSD(voistate, statevsd)->prev.int32.s32 =
			    voival->int32.s32;
			break;
		case VSD_DTYPE_INT_U32:
			VSD(voistate, statevsd)->prev.int32.u32 =
			    voival->int32.u32;
			break;
		case VSD_DTYPE_INT_S64:
			VSD(voistate, statevsd)->prev.int64.s64 =
			    voival->int64.s64;
			break;
		case VSD_DTYPE_INT_U64:
			VSD(voistate, statevsd)->prev.int64.u64 =
			    voival->int64.u64;
			break;
		case VSD_DTYPE_INT_SLONG:
			VSD(voistate, statevsd)->prev.intlong.slong =
			    voival->intlong.slong;
			break;
		case VSD_DTYPE_INT_ULONG:
			VSD(voistate, statevsd)->prev.intlong.ulong =
			    voival->intlong.ulong;
			break;
		case VSD_DTYPE_Q_S32:
			error = Q_QCPYVALQ(
			    &VSD(voistate, statevsd)->prev.q32.sq32,
			    voival->q32.sq32);
			break;
		case VSD_DTYPE_Q_U32:
			error = Q_QCPYVALQ(
			    &VSD(voistate, statevsd)->prev.q32.uq32,
			    voival->q32.uq32);
			break;
		case VSD_DTYPE_Q_S64:
			error = Q_QCPYVALQ(
			    &VSD(voistate, statevsd)->prev.q64.sq64,
			    voival->q64.sq64);
			break;
		case VSD_DTYPE_Q_U64:
			error = Q_QCPYVALQ(
			    &VSD(voistate, statevsd)->prev.q64.uq64,
			    voival->q64.uq64);
			break;
		default:
			KASSERT(0, ("Unknown VOI data type %d", voi_dtype));
			break;
		}
	}

	return (error);
}

#ifdef _KERNEL

static void
stats_init(void *arg)
{

}
SYSINIT(stats, SI_SUB_KDTRACE, SI_ORDER_FIRST, stats_init, NULL);

/*
 * Sysctl handler to display the list of available stats templates.
 */
static int
stats_tpl_list_available(SYSCTL_HANDLER_ARGS)
{
	struct sbuf *s;
	int err, i;

	err = 0;

	/* We can tolerate ntpl being stale, so do not take the lock. */
	s = sbuf_new(NULL, NULL, /* +1 per tpl for , */
	    ntpl * (STATS_TPL_MAX_STR_SPEC_LEN + 1), SBUF_FIXEDLEN);
	if (s == NULL)
		return (ENOMEM);

	TPL_LIST_RLOCK();
	for (i = 0; i < ntpl; i++) {
		err = sbuf_printf(s, "%s\"%s\":%u", i ? "," : "",
		    tpllist[i]->mb->tplname, tpllist[i]->mb->tplhash);
		if (err) {
			/* Sbuf overflow condition. */
			err = EOVERFLOW;
			break;
		}
	}
	TPL_LIST_RUNLOCK();

	if (!err) {
		sbuf_finish(s);
		err = sysctl_handle_string(oidp, sbuf_data(s), 0, req);
	}

	sbuf_delete(s);
	return (err);
}

/*
 * Called by subsystem-specific sysctls to report and/or parse the list of
 * templates being sampled and their sampling rates. A stats_tpl_sr_cb_t
 * conformant function pointer must be passed in as arg1, which is used to
 * interact with the subsystem's stats template sample rates list. If arg2 > 0,
 * a zero-initialised allocation of arg2-sized contextual memory is
 * heap-allocated and passed in to all subsystem callbacks made during the
 * operation of stats_tpl_sample_rates().
 *
 * XXXLAS: Assumes templates are never removed, which is currently true but may
 * need to be reworked in future if dynamic template management becomes a
 * requirement e.g. to support kernel module based templates.
 */
int
stats_tpl_sample_rates(SYSCTL_HANDLER_ARGS)
{
	char kvpair_fmt[16], tplspec_fmt[16];
	char tpl_spec[STATS_TPL_MAX_STR_SPEC_LEN];
	char tpl_name[TPL_MAX_NAME_LEN + 2]; /* +2 for "" */
	stats_tpl_sr_cb_t subsys_cb;
	void *subsys_ctx;
	char *buf, *new_rates_usr_str, *tpl_name_p;
	struct stats_tpl_sample_rate *rates;
	struct sbuf *s, _s;
	uint32_t cum_pct, pct, tpl_hash;
	int err, i, off, len, newlen, nrates;

	buf = NULL;
	rates = NULL;
	err = nrates = 0;
	subsys_cb = (stats_tpl_sr_cb_t)arg1;
	KASSERT(subsys_cb != NULL, ("%s: subsys_cb == arg1 == NULL", __func__));
	if (arg2 > 0)
		subsys_ctx = malloc(arg2, M_TEMP, M_WAITOK | M_ZERO);
	else
		subsys_ctx = NULL;

	/* Grab current count of subsystem rates. */
	err = subsys_cb(TPL_SR_UNLOCKED_GET, NULL, &nrates, subsys_ctx);
	if (err)
		goto done;

	/* +1 to ensure we can append '\0' post copyin, +5 per rate for =nnn, */
	len = max(req->newlen + 1, nrates * (STATS_TPL_MAX_STR_SPEC_LEN + 5));

	if (req->oldptr != NULL || req->newptr != NULL)
		buf = malloc(len, M_TEMP, M_WAITOK);

	if (req->oldptr != NULL) {
		if (nrates == 0) {
			/* No rates, so return an empty string via oldptr. */
			err = SYSCTL_OUT(req, "", 1);
			if (err)
				goto done;
			goto process_new;
		}

		s = sbuf_new(&_s, buf, len, SBUF_FIXEDLEN | SBUF_INCLUDENUL);

		/* Grab locked count of, and ptr to, subsystem rates. */
		err = subsys_cb(TPL_SR_RLOCKED_GET, &rates, &nrates,
		    subsys_ctx);
		if (err)
			goto done;
		TPL_LIST_RLOCK();
		for (i = 0; i < nrates && !err; i++) {
			err = sbuf_printf(s, "%s\"%s\":%u=%u", i ? "," : "",
			    tpllist[rates[i].tpl_slot_id]->mb->tplname,
			    tpllist[rates[i].tpl_slot_id]->mb->tplhash,
			    rates[i].tpl_sample_pct);
		}
		TPL_LIST_RUNLOCK();
		/* Tell subsystem that we're done with its rates list. */
		err = subsys_cb(TPL_SR_RUNLOCK, &rates, &nrates, subsys_ctx);
		if (err)
			goto done;

		err = sbuf_finish(s);
		if (err)
			goto done; /* We lost a race for buf to be too small. */

		/* Return the rendered string data via oldptr. */
		err = SYSCTL_OUT(req, sbuf_data(s), sbuf_len(s));
	} else {
		/* Return the upper bound size for buffer sizing requests. */
		err = SYSCTL_OUT(req, NULL, len);
	}

process_new:
	if (err || req->newptr == NULL)
		goto done;

	newlen = req->newlen - req->newidx;
	err = SYSCTL_IN(req, buf, newlen);
	if (err)
		goto done;

	/*
	 * Initialise format strings at run time.
	 *
	 * Write the max template spec string length into the
	 * template_spec=percent key-value pair parsing format string as:
	 *     " %<width>[^=]=%u %n"
	 *
	 * Write the max template name string length into the tplname:tplhash
	 * parsing format string as:
	 *     "%<width>[^:]:%u"
	 *
	 * Subtract 1 for \0 appended by sscanf().
	 */
	sprintf(kvpair_fmt, " %%%zu[^=]=%%u %%n", sizeof(tpl_spec) - 1);
	sprintf(tplspec_fmt, "%%%zu[^:]:%%u", sizeof(tpl_name) - 1);

	/*
	 * Parse each CSV key-value pair specifying a template and its sample
	 * percentage. Whitespace either side of a key-value pair is ignored.
	 * Templates can be specified by name, hash, or name and hash per the
	 * following formats (chars in [] are optional):
	 *    ["]<tplname>["]=<percent>
	 *    :hash=pct
	 *    ["]<tplname>["]:hash=<percent>
	 */
	cum_pct = nrates = 0;
	rates = NULL;
	buf[newlen] = '\0'; /* buf is at least newlen+1 in size. */
	new_rates_usr_str = buf;
	while (isspace(*new_rates_usr_str))
		new_rates_usr_str++; /* Skip leading whitespace. */
	while (*new_rates_usr_str != '\0') {
		tpl_name_p = tpl_name;
		tpl_name[0] = '\0';
		tpl_hash = 0;
		off = 0;

		/*
		 * Parse key-value pair which must perform 2 conversions, then
		 * parse the template spec to extract either name, hash, or name
		 * and hash depending on the three possible spec formats. The
		 * tplspec_fmt format specifier parses name or name and hash
		 * template specs, while the ":%u" format specifier parses
		 * hash-only template specs. If parsing is successfull, ensure
		 * the cumulative sampling percentage does not exceed 100.
		 */
		err = EINVAL;
		if (2 != sscanf(new_rates_usr_str, kvpair_fmt, tpl_spec, &pct,
		    &off))
			break;
		if ((1 > sscanf(tpl_spec, tplspec_fmt, tpl_name, &tpl_hash)) &&
		    (1 != sscanf(tpl_spec, ":%u", &tpl_hash)))
			break;
		if ((cum_pct += pct) > 100)
			break;
		err = 0;

		/* Strip surrounding "" from template name if present. */
		len = strlen(tpl_name);
		if (len > 0) {
			if (tpl_name[len - 1] == '"')
				tpl_name[--len] = '\0';
			if (tpl_name[0] == '"') {
				tpl_name_p++;
				len--;
			}
		}

		rates = stats_realloc(rates, 0, /* oldsz is unused in kernel. */
		    (nrates + 1) * sizeof(*rates), M_WAITOK);
		rates[nrates].tpl_slot_id =
		    stats_tpl_fetch_allocid(len ? tpl_name_p : NULL, tpl_hash);
		if (rates[nrates].tpl_slot_id < 0) {
			err = -rates[nrates].tpl_slot_id;
			break;
		}
		rates[nrates].tpl_sample_pct = pct;
		nrates++;
		new_rates_usr_str += off;
		if (*new_rates_usr_str != ',')
			break; /* End-of-input or malformed. */
		new_rates_usr_str++; /* Move past comma to next pair. */
	}

	if (!err) {
		if ((new_rates_usr_str - buf) < newlen) {
			/* Entire input has not been consumed. */
			err = EINVAL;
		} else {
			/*
			 * Give subsystem the new rates. They'll return the
			 * appropriate rates pointer for us to garbage collect.
			 */
			err = subsys_cb(TPL_SR_PUT, &rates, &nrates,
			    subsys_ctx);
		}
	}
	stats_free(rates);

done:
	free(buf, M_TEMP);
	free(subsys_ctx, M_TEMP);
	return (err);
}

SYSCTL_NODE(_kern, OID_AUTO, stats, CTLFLAG_RW | CTLFLAG_MPSAFE, NULL,
    "stats(9) MIB");

SYSCTL_PROC(_kern_stats, OID_AUTO, templates,
    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
    stats_tpl_list_available, "A",
    "list the name/hash of all available stats(9) templates");

#else /* ! _KERNEL */

static void __attribute__ ((constructor))
stats_constructor(void)
{

	pthread_rwlock_init(&tpllistlock, NULL);
}

static void __attribute__ ((destructor))
stats_destructor(void)
{

	pthread_rwlock_destroy(&tpllistlock);
}

#endif /* _KERNEL */