|
root / base / usr / src / cmd / cpc / common
common Plain Text 3041 lines 72.9 KB
   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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#define	 __EXTENSIONS__	/* header bug! strtok_r is overly hidden */
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <libintl.h>

#include <libcpc.h>

#include "cpucmds.h"

struct args {
	FILE *fp;
	int colnum;
	int margin;
};

struct evlist {
	char *list;
	int size;
};

#define	MAX_RHS_COLUMN	76
#define	EVENT_MARGIN	17
#define	ATTR_MARGIN	20

/*ARGSUSED*/
static void
list_cap(void *arg, uint_t regno, const char *name)
{
	struct args *args = arg;
	int i;

	if ((args->colnum + strlen(name) + 1) > MAX_RHS_COLUMN) {
		(void) fprintf(args->fp, "\n");
		for (i = 0; i < args->margin; i++)
			(void) fprintf(args->fp, " ");
		args->colnum = args->margin;
	}
	args->colnum += fprintf(args->fp, "%s ", name);
}

static void
list_attr(void *arg, const char *name)
{
	/*
	 * The following attributes are used by the commands but should not be
	 * reported to the user, since they may not be specified directly.
	 */
	if (strncmp(name, "picnum", 7) == 0 ||
	    strncmp(name, "count_sibling_usr", 18) == 0 ||
	    strncmp(name, "count_sibling_sys", 18) == 0)
		return;

	list_cap(arg, 0, name);
}

static void *
emalloc(size_t size)
{
	void *ptr;

	if ((ptr = malloc(size)) == NULL) {
		(void) fprintf(stderr, gettext("no memory available\n"));
		exit(1);
	}

	return (ptr);
}

/*
 * Used by allpics_equal().
 */
/*ARGSUSED*/
static void
cap_walker(void *arg, uint_t regno, const char *name)
{
	struct evlist *list = arg;

	list->size += strlen(name);
	if ((list->list = realloc(list->list, list->size + 1)) == NULL) {
		(void) fprintf(stderr, gettext("no memory available\n"));
		exit(1);
	}

	(void) strcat(list->list, name);
}

/*
 * Returns 1 if all counters on this chip can count all possible events.
 */
static int
allpics_equal(cpc_t *cpc)
{
	int	npics = cpc_npic(cpc);
	int	i;
	struct	evlist **lists;
	int	ret = 1;

	lists = emalloc(npics * sizeof (struct evlist *));

	for (i = 0; i < npics; i++) {
		lists[i] = emalloc(sizeof (struct evlist));
		lists[i]->size = 0;
		lists[i]->list = emalloc(1);
		lists[i]->list[0] = '\0';
		cpc_walk_events_pic(cpc, i, lists[i], cap_walker);
	}

	for (i = 1; i < npics; i++)
		if (lists[i]->size != lists[0]->size ||
		    strncmp(lists[i]->list, lists[0]->list,
		    lists[0]->size) != 0) {
			ret = 0;
			break;
		}

	for (i = 0; i < npics; i++) {
		free(lists[i]->list);
		free(lists[i]);
	}
	free(lists);

	return (ret);
}

int
capabilities(cpc_t *cpc, FILE *fp)
{
	struct args _args, *args = &_args;
	char *text, *tok, *cp;
	const char *ccp;
	int npic = cpc_npic(cpc);
	int i, pics_equal = allpics_equal(cpc);

	args->fp = fp;

	if ((ccp = cpc_cciname(cpc)) == NULL)
		ccp = "No information available";
	(void) fprintf(args->fp, "\t%s: %s\n\n",
	    gettext("CPU performance counter interface"), ccp);

	(void) fprintf(args->fp, gettext("\tevent specification syntax:\n"));

	(void) fprintf(args->fp, "\t[picn=]<eventn>[,attr[n][=<val>]]"
	    "[,[picn=]<eventn>[,attr[n][=<val>]],...]\n");

	(void) fprintf(args->fp, gettext("\n\tGeneric Events:\n"));

	if (pics_equal) {
		args->margin = args->colnum = EVENT_MARGIN;
		(void) fprintf(args->fp, "\n\tevent[0-%d]: ", npic - 1);
		cpc_walk_generic_events_pic(cpc, 0, args, list_cap);
		(void) fprintf(args->fp, "\n");
	} else {
		args->margin = EVENT_MARGIN;
		for (i = 0; i < npic; i++) {
			(void) fprintf(args->fp, "\n\tevent%d: ", i);
			if (i < 10) (void) fprintf(args->fp, " ");
			args->colnum = EVENT_MARGIN;
			cpc_walk_generic_events_pic(cpc, i, args, list_cap);
			(void) fprintf(args->fp, "\n");
		}
	}

	(void) fprintf(args->fp, gettext("\n\tSee generic_events(3CPC) for"
	    " descriptions of these events\n\n"));

	(void) fprintf(args->fp, gettext("\tPlatform Specific Events:\n"));

	if (pics_equal) {
		args->margin = args->colnum = EVENT_MARGIN;
		(void) fprintf(args->fp, "\n\tevent[0-%d]: ", npic - 1);
		cpc_walk_events_pic(cpc, 0, args, list_cap);
		(void) fprintf(args->fp, "\n");
	} else {
		args->margin = EVENT_MARGIN;
		for (i = 0; i < npic; i++) {
			(void) fprintf(args->fp, "\n\tevent%d: ", i);
			if (i < 10) (void) fprintf(args->fp, " ");
			args->colnum = EVENT_MARGIN;
			cpc_walk_events_pic(cpc, i, args, list_cap);
			(void) fprintf(args->fp, "\n");
		}
	}

	(void) fprintf(args->fp, "\n\tattributes: ");
	args->colnum = args->margin = ATTR_MARGIN;
	cpc_walk_attrs(cpc, args, list_attr);
	/*
	 * In addition to the attributes published by the kernel, we allow the
	 * user to specify two additional tokens on all platforms. List them
	 * here.
	 */
	list_cap(args, 0, "nouser");
	list_cap(args, 0, "sys");
	(void) fprintf(args->fp, "\n\n\t");
	args->colnum = 8;

	if ((ccp = cpc_cpuref(cpc)) == NULL)
		ccp = "No information available";
	if ((text = strdup(ccp)) == NULL) {
		(void) fprintf(stderr, gettext("no memory available.\n"));
		exit(1);
	}
	for (cp = strtok_r(text, " ", &tok);
	    cp != NULL; cp = strtok_r(NULL, " ", &tok)) {
		if ((args->colnum + strlen(cp) + 1) > MAX_RHS_COLUMN) {
			(void) fprintf(args->fp, "\n\t");
			args->colnum = 8;
		}
		args->colnum += fprintf(args->fp, "%s ", cp);
	}
	(void) fprintf(args->fp, "\n");
	free(text);

	return (0);
}

/*
 * Returns 1 on SMT processors which do not have full CPC hardware for each
 * logical processor.
 */
int
smt_limited_cpc_hw(cpc_t *cpc)
{
	if (strcmp(cpc_cciname(cpc), "Pentium 4 with HyperThreading") == 0)
		return (1);
	return (0);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_CPUCMDS_H
#define	_CPUCMDS_H

#include <sys/types.h>
#include <stdio.h>
#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

#include "libcpc_impl.h"

extern cpc_set_t *cpc_strtoset(cpc_t *cpc, const char *spec, int smt);
extern cpc_errhndlr_t *strtoset_errfn;
extern int capabilities(cpc_t *cpc, FILE *);
extern int smt_limited_cpc_hw(cpc_t *cpc);
extern void zerotime(void);
extern float mstimestamp(hrtime_t hrt);

/*
 * Request sets can be manipulated in collections called setgroups.
 */
typedef struct __cpc_setgrp cpc_setgrp_t;

extern cpc_setgrp_t *cpc_setgrp_new(cpc_t *cpc, int smt);
extern cpc_setgrp_t *cpc_setgrp_newset(cpc_setgrp_t *sgrp,
    const char *spec, int *errcnt);
extern int cpc_setgrp_getbufs(cpc_setgrp_t *sgrp, cpc_buf_t ***data1,
    cpc_buf_t ***data2, cpc_buf_t ***scratch);
extern cpc_setgrp_t *cpc_setgrp_clone(cpc_setgrp_t *sgrp);
extern void cpc_setgrp_free(cpc_setgrp_t *sgrp);

extern cpc_set_t *cpc_setgrp_getset(cpc_setgrp_t *sgrp);
extern const char *cpc_setgrp_getname(cpc_setgrp_t *sgrp);
extern const char *cpc_setgrp_gethdr(cpc_setgrp_t *sgrp);
extern int cpc_setgrp_numsets(cpc_setgrp_t *sgrp);
extern cpc_set_t *cpc_setgrp_nextset(cpc_setgrp_t *sgrp);
extern void cpc_setgrp_reset(cpc_setgrp_t *to);
extern void cpc_setgrp_accum(cpc_setgrp_t *accum, cpc_setgrp_t *sgrp);
extern int cpc_setgrp_sysonly(cpc_setgrp_t *sgrp);
extern int cpc_setgrp_has_sysonly(cpc_setgrp_t *sgrp);

#ifdef __cplusplus
}
#endif

#endif	/* _CPUCMDS_H */
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 2019 Joyent, Inc.
 */

#include <sys/types.h>
#include <sys/processor.h>
#include <sys/pset.h>
#include <sys/lwp.h>
#include <sys/priocntl.h>
#include <sys/fxpriocntl.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <thread.h>
#include <errno.h>
#include <libintl.h>
#include <locale.h>
#include <kstat.h>
#include <synch.h>
#include <libcpc.h>
#include <sys/resource.h>

#include "cpucmds.h"
#include "statcommon.h"

static struct options {
	int debug;
	int dotitle;
	int dohelp;
	int dotick;
	int dosoaker;
	int doperiod;
	char *pgmname;
	uint_t mseconds;
	uint_t nsamples;
	uint_t nsets;
	uint_t mseconds_rest;
	cpc_setgrp_t *master;
} __options;

/*
 * States for soaker threads.
 */
#define	SOAK_PAUSE	0
#define	SOAK_RUN	1

struct tstate {
	processorid_t	cpuid;
	int		chip_id;
	cpc_setgrp_t	*sgrp;
	int		status;
	thread_t	tid;
	int		soak_state;
	mutex_t		soak_lock;
	cond_t		soak_cv;
};

static const struct options *opts = (const struct options *)&__options;

static cpc_t *cpc;

struct tstate	*gstate;
static int	ncpus;
static int	max_chip_id;
static int	*chip_designees;    /* cpuid of CPU which counts for phs chip */
static int	smt = 0;	    /* If set, cpustat needs to be SMT-aware. */
static pcinfo_t	fxinfo = { 0, "FX", 0 }; /* FX scheduler class info */

static uint_t timestamp_fmt = NODATE;

/*ARGSUSED*/
static void
cpustat_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
{
	(void) fprintf(stderr, "%s: ", opts->pgmname);
	if (opts->debug)
		(void) fprintf(stderr, "%s: ", fn);
	(void) vfprintf(stderr, fmt, ap);
}

static int cpustat(void);
static int get_chipid(kstat_ctl_t *kc, processorid_t cpuid);
static void *soaker(void *arg);


#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif

int
main(int argc, char *argv[])
{
	struct options	*opts = &__options;
	int		c, errcnt = 0, ret;
	cpc_setgrp_t	*sgrp;
	char		*errstr;
	double		period;
	char		*endp;
	struct rlimit	rl;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
		opts->pgmname = argv[0];
	else
		opts->pgmname++;

	/* Make sure we can open enough files */
	rl.rlim_max = rl.rlim_cur = RLIM_INFINITY;
	if (setrlimit(RLIMIT_NOFILE, &rl) != 0) {
		errstr = strerror(errno);
		(void) fprintf(stderr,
		    gettext("%s: setrlimit failed - %s\n"),
		    opts->pgmname, errstr);
	}

	if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
		errstr = strerror(errno);
		(void) fprintf(stderr, gettext("%s: cannot access performance "
		    "counters - %s\n"), opts->pgmname, errstr);
		return (1);
	}

	(void) cpc_seterrhndlr(cpc, cpustat_errfn);
	strtoset_errfn = cpustat_errfn;

	/*
	 * Check to see if cpustat needs to be SMT-aware.
	 */
	smt = smt_limited_cpc_hw(cpc);

	/*
	 * Establish some defaults
	 */
	opts->mseconds = 5000;
	opts->nsamples = UINT_MAX;
	opts->dotitle = 1;
	if ((opts->master = cpc_setgrp_new(cpc, smt)) == NULL) {
		(void) fprintf(stderr, gettext("%s: out of heap\n"),
		    opts->pgmname);
		return (1);
	}

	while ((c = getopt(argc, argv, "Dc:hntT:sp:")) != EOF && errcnt == 0)
		switch (c) {
		case 'D':			/* enable debugging */
			opts->debug++;
			break;
		case 'c':			/* specify statistics */
			if ((sgrp = cpc_setgrp_newset(opts->master,
			    optarg, &errcnt)) != NULL)
				opts->master = sgrp;
			break;
		case 'n':			/* no titles */
			opts->dotitle = 0;
			break;
		case 'p':			/* periodic behavior */
			opts->doperiod = 1;
			period = strtod(optarg, &endp);
			if (*endp != '\0') {
				(void) fprintf(stderr, gettext("%s: invalid "
				    "parameter \"%s\"\n"), opts->pgmname,
				    optarg);
				errcnt++;
			}
			break;
		case 's':			/* run soaker thread */
			opts->dosoaker = 1;
			break;
		case 't':			/* print %tick */
			opts->dotick = 1;
			break;
		case 'T':
			if (optarg) {
				if (*optarg == 'u')
					timestamp_fmt = UDATE;
				else if (*optarg == 'd')
					timestamp_fmt = DDATE;
				else
					errcnt++;
			} else {
				errcnt++;
			}
			break;
		case 'h':			/* help */
			opts->dohelp = 1;
			break;
		case '?':
		default:
			errcnt++;
			break;
		}

	switch (argc - optind) {
	case 0:
		break;
	case 2:
		opts->nsamples = strtol(argv[optind + 1], &endp, 10);
		if (*endp != '\0') {
			(void) fprintf(stderr,
			    gettext("%s: invalid argument \"%s\"\n"),
			    opts->pgmname, argv[optind + 1]);
			errcnt++;
			break;
		}
		/*FALLTHROUGH*/
	case 1:
		opts->mseconds = (uint_t)(strtod(argv[optind], &endp) * 1000.0);
		if (*endp != '\0') {
			(void) fprintf(stderr,
			    gettext("%s: invalid argument \"%s\"\n"),
			    opts->pgmname, argv[optind]);
			errcnt++;
		}
		break;
	default:
		errcnt++;
		break;
	}

	if (opts->nsamples == 0 || opts->mseconds == 0)
		errcnt++;

	if (errcnt != 0 || opts->dohelp ||
	    (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
		(void) fprintf(opts->dohelp ? stdout : stderr, gettext(
		    "Usage:\n\t%s -c spec [-c spec]... [-p period] [-T u|d]\n"
		    "\t\t[-sntD] [interval [count]]\n\n"
		    "\t-c spec\t  specify processor events to be monitored\n"
		    "\t-n\t  suppress titles\n"
		    "\t-p period cycle through event list periodically\n"
		    "\t-s\t  run user soaker thread for system-only events\n"
		    "\t-t\t  include %s register\n"
		    "\t-T d|u\t  Display a timestamp in date (d) or unix "
		    "time_t (u)\n"
		    "\t-D\t  enable debug mode\n"
		    "\t-h\t  print extended usage information\n\n"
		    "\tUse cputrack(1) to monitor per-process statistics.\n"),
		    opts->pgmname, CPC_TICKREG_NAME);
		if (opts->dohelp) {
			(void) putchar('\n');
			(void) capabilities(cpc, stdout);
			exit(0);
		}
		exit(2);
	}

	/*
	 * If the user requested periodic behavior, calculate the rest time
	 * between cycles.
	 */
	if (opts->doperiod) {
		opts->mseconds_rest = (uint_t)((period * 1000.0) -
		    (opts->mseconds * opts->nsets));
		if ((int)opts->mseconds_rest < 0)
			opts->mseconds_rest = 0;
		if (opts->nsamples != UINT_MAX)
			opts->nsamples *= opts->nsets;
	}

	cpc_setgrp_reset(opts->master);
	(void) setvbuf(stdout, NULL, _IOLBF, 0);

	/*
	 * By design, cpustat (regrettably) has multiple threads racing in
	 * write(2) to generate output.  As there are no guarantees made with
	 * respect to the atomicity of concurrent writes on non-O_APPEND file
	 * descriptors, we must set O_APPEND on stdout to assure that no output
	 * is lost.  If cpustat is rearchitected such that only one thread is
	 * generating output (which would also assure that the output is always
	 * in a consistent order), this code should be removed.
	 */
	if (fcntl(1, F_SETFL, fcntl(1, F_GETFL) | O_APPEND) == -1) {
		(void) fprintf(stderr, gettext("%s: cannot set output to be "
		    "append-only - %s\n"), opts->pgmname, strerror(errno));
		return (1);
	}

	/*
	 * If no system-mode only sets were created, no soaker threads will be
	 * needed.
	 */
	if (opts->dosoaker == 1 && cpc_setgrp_has_sysonly(opts->master) == 0)
		opts->dosoaker = 0;

	ret = cpustat();

	(void) cpc_close(cpc);

	return (ret);
}

static void
print_title(cpc_setgrp_t *sgrp)
{
	(void) printf("%7s %3s %5s ", "time", "cpu", "event");
	if (opts->dotick)
		(void) printf("%9s ", CPC_TICKREG_NAME);
	(void) printf("%s\n", cpc_setgrp_gethdr(sgrp));
}

static void
print_sample(processorid_t cpuid, cpc_buf_t *buf, int nreq, const char *setname,
    int sibling)
{
	char		line[1024];
	int		ccnt;
	int		i;
	uint64_t	val;
	uint64_t	tick;
	hrtime_t	hrtime;

	hrtime = cpc_buf_hrtime(cpc, buf);
	tick = cpc_buf_tick(cpc, buf);

	ccnt = snprintf(line, sizeof (line), "%7.3f %3d %5s ",
	    mstimestamp(hrtime), (int)cpuid, "tick");
	if (opts->dotick)
		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
		    "%9" PRId64 " ", tick);
	for (i = 0; i < nreq; i++) {
		(void) cpc_buf_get(cpc, buf, i, &val);
		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
		    "%9" PRId64 " ", val);
	}
	if (opts->nsets > 1)
		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
		    " # %s\n", setname);
	else
		ccnt += snprintf(line + ccnt, sizeof (line) - ccnt, "\n");

	if (sibling) {
		/*
		 * This sample is being printed for a "sibling" CPU -- that is,
		 * a CPU which does not have its own CPC set bound. It is being
		 * measured via a set bound to another CPU sharing its physical
		 * processor.
		 */
		int designee = chip_designees[gstate[cpuid].chip_id];
		char *p;

		if ((p = strrchr(line, '#')) == NULL)
			p = strrchr(line, '\n');

		if (p != NULL) {
			*p = '\0';
			ccnt = strlen(line);
			ccnt += snprintf(line + ccnt, sizeof (line) - ccnt,
			    "# counter shared with CPU %d\n", designee);
		}
	}

	if (timestamp_fmt != NODATE)
		print_timestamp(timestamp_fmt);
	if (ccnt > sizeof (line))
		ccnt = sizeof (line);
	if (ccnt > 0)
		(void) write(1, line, ccnt);

	/*
	 * If this CPU is the chip designee for any other CPUs, print a line for
	 * them here.
	 */
	if (smt && (sibling == 0)) {
		for (i = 0; i < ncpus; i++) {
			if ((i != cpuid) && (gstate[i].cpuid != -1) &&
			    (chip_designees[gstate[i].chip_id] == cpuid))
				print_sample(i, buf, nreq, setname, 1);
		}
	}
}

static void
print_total(int ncpus, cpc_buf_t *buf, int nreq, const char *setname)
{
	int		i;
	uint64_t	val;

	(void) printf("%7.3f %3d %5s ", mstimestamp(cpc_buf_hrtime(cpc, buf)),
	    ncpus, "total");
	if (opts->dotick)
		(void) printf("%9" PRId64 " ", cpc_buf_tick(cpc, buf));
	for (i = 0; i < nreq; i++) {
		(void) cpc_buf_get(cpc, buf, i, &val);
		(void) printf("%9" PRId64 " ", val);
	}
	if (opts->nsets > 1)
		(void) printf(" # %s", setname);
	(void) fputc('\n', stdout);
}

#define	NSECS_PER_MSEC	1000000ll
#define	NSECS_PER_SEC	1000000000ll

static void *
gtick(void *arg)
{
	struct tstate		*state = arg;
	char			*errstr;
	uint_t			nsamples;
	uint_t			sample_cnt = 1;
	hrtime_t		ht, htdelta, restdelta;
	cpc_setgrp_t		*sgrp = state->sgrp;
	cpc_set_t		*this = cpc_setgrp_getset(sgrp);
	const char		*name = cpc_setgrp_getname(sgrp);
	cpc_buf_t		**data1, **data2, **scratch;
	cpc_buf_t		*tmp;
	int			nreqs;
	thread_t		tid;

	htdelta = NSECS_PER_MSEC * opts->mseconds;
	restdelta = NSECS_PER_MSEC * opts->mseconds_rest;
	ht = gethrtime();

	/*
	 * If this CPU is SMT, we run one gtick() thread per _physical_ CPU,
	 * instead of per cpu_t. The following check returns if it detects that
	 * this cpu_t has not been designated to do the counting for this
	 * physical CPU.
	 */
	if (smt && chip_designees[state->chip_id] != state->cpuid)
		return (NULL);

	/*
	 * If we need to run a soaker thread on this CPU, start it here.
	 */
	if (opts->dosoaker) {
		if (cond_init(&state->soak_cv, USYNC_THREAD, NULL) != 0)
			goto bad;
		if (mutex_init(&state->soak_lock, USYNC_THREAD,
		    NULL) != 0)
			goto bad;
		(void) mutex_lock(&state->soak_lock);
		state->soak_state = SOAK_PAUSE;
		if (thr_create(NULL, 0, soaker, state, 0, &tid) != 0)
			goto bad;

		while (state->soak_state == SOAK_PAUSE)
			(void) cond_wait(&state->soak_cv,
			    &state->soak_lock);
		(void) mutex_unlock(&state->soak_lock);

		/*
		 * If the soaker needs to pause for the first set, stop it now.
		 */
		if (cpc_setgrp_sysonly(sgrp) == 0) {
			(void) mutex_lock(&state->soak_lock);
			state->soak_state = SOAK_PAUSE;
			(void) mutex_unlock(&state->soak_lock);
		}
	}
	if (cpc_bind_cpu(cpc, state->cpuid, this, 0) == -1)
		goto bad;

	for (nsamples = opts->nsamples; nsamples; nsamples--, sample_cnt++) {
		hrtime_t htnow;
		struct timespec ts;

		nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);

		ht += htdelta;
		htnow = gethrtime();
		if (ht <= htnow)
			continue;
		ts.tv_sec = (time_t)((ht - htnow) / NSECS_PER_SEC);
		ts.tv_nsec = (suseconds_t)((ht - htnow) % NSECS_PER_SEC);

		(void) nanosleep(&ts, NULL);

		if (opts->nsets == 1) {
			/*
			 * If we're dealing with one set, buffer usage is:
			 *
			 * data1 = most recent data snapshot
			 * data2 = previous data snapshot
			 * scratch = used for diffing data1 and data2
			 *
			 * Save the snapshot from the previous sample in data2
			 * before putting the current sample in data1.
			 */
			tmp = *data1;
			*data1 = *data2;
			*data2 = tmp;
			if (cpc_set_sample(cpc, this, *data1) != 0)
				goto bad;
			cpc_buf_sub(cpc, *scratch, *data1, *data2);

			print_sample(state->cpuid, *scratch, nreqs, name, 0);
		} else {
			/*
			 * More than one set is in use (multiple -c options
			 * given). Buffer usage in this case is:
			 *
			 * data1 = total counts for this set since program began
			 * data2 = unused
			 * scratch = most recent data snapshot
			 */
			name = cpc_setgrp_getname(sgrp);
			nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2,
			    &scratch);

			if (cpc_set_sample(cpc, this, *scratch) != 0)
				goto bad;

			cpc_buf_add(cpc, *data1, *data1, *scratch);

			if (cpc_unbind(cpc, this) != 0)
				(void) fprintf(stderr, gettext("%s: error "
				    "unbinding on cpu %d - %s\n"),
				    opts->pgmname, state->cpuid,
				    strerror(errno));

			this = cpc_setgrp_nextset(sgrp);

			print_sample(state->cpuid, *scratch, nreqs, name, 0);

			/*
			 * If periodic behavior was requested, rest here.
			 */
			if (opts->doperiod && opts->mseconds_rest > 0 &&
			    (sample_cnt % opts->nsets) == 0) {
				/*
				 * Stop the soaker while the tool rests.
				 */
				if (opts->dosoaker) {
					(void) mutex_lock(&state->soak_lock);
					if (state->soak_state == SOAK_RUN)
						state->soak_state = SOAK_PAUSE;
					(void) mutex_unlock(&state->soak_lock);
				}

				htnow = gethrtime();
				ht += restdelta;
				ts.tv_sec = (time_t)((ht - htnow) /
				    NSECS_PER_SEC);
				ts.tv_nsec = (suseconds_t)((ht - htnow) %
				    NSECS_PER_SEC);

				(void) nanosleep(&ts, NULL);
			}

			/*
			 * Start or stop the soaker if needed.
			 */
			if (opts->dosoaker) {
				(void) mutex_lock(&state->soak_lock);
				if (cpc_setgrp_sysonly(sgrp) &&
				    state->soak_state == SOAK_PAUSE) {
					/*
					 * Soaker is paused but the next set is
					 * sysonly: start the soaker.
					 */
					state->soak_state = SOAK_RUN;
					(void) cond_signal(&state->soak_cv);
				} else if (cpc_setgrp_sysonly(sgrp) == 0 &&
				    state->soak_state == SOAK_RUN)
					/*
					 * Soaker is running but the next set
					 * counts user events: stop the soaker.
					 */
					state->soak_state = SOAK_PAUSE;
				(void) mutex_unlock(&state->soak_lock);
			}

			if (cpc_bind_cpu(cpc, state->cpuid, this, 0) != 0)
				goto bad;
		}
	}

	if (cpc_unbind(cpc, this) != 0)
		(void) fprintf(stderr, gettext("%s: error unbinding on"
		    " cpu %d - %s\n"), opts->pgmname,
		    state->cpuid, strerror(errno));

	/*
	 * We're done, so stop the soaker if needed.
	 */
	if (opts->dosoaker) {
		(void) mutex_lock(&state->soak_lock);
		if (state->soak_state == SOAK_RUN)
			state->soak_state = SOAK_PAUSE;
		(void) mutex_unlock(&state->soak_lock);
	}

	return (NULL);
bad:
	state->status = 3;
	errstr = strerror(errno);
	(void) fprintf(stderr, gettext("%s: cpu%d - %s\n"),
	    opts->pgmname, state->cpuid, errstr);
	return (NULL);
}

static int
cpustat(void)
{
	cpc_setgrp_t	*accum;
	cpc_set_t	*start;
	int		c, i, retval;
	int		lwps = 0;
	psetid_t	mypset, cpupset;
	char		*errstr;
	cpc_buf_t	**data1, **data2, **scratch;
	int		nreqs;
	kstat_ctl_t	*kc;

	ncpus = (int)sysconf(_SC_NPROCESSORS_CONF);
	if ((gstate = calloc(ncpus, sizeof (*gstate))) == NULL) {
		(void) fprintf(stderr, gettext(
		    "%s: out of heap\n"), opts->pgmname);
		return (1);
	}

	max_chip_id = sysconf(_SC_CPUID_MAX);
	if ((chip_designees = malloc(max_chip_id * sizeof (int))) == NULL) {
		(void) fprintf(stderr, gettext(
		    "%s: out of heap\n"), opts->pgmname);
		return (1);
	}
	for (i = 0; i < max_chip_id; i++)
		chip_designees[i] = -1;

	if (smt) {
		if ((kc = kstat_open()) == NULL) {
			(void) fprintf(stderr, gettext(
			    "%s: kstat_open() failed: %s\n"), opts->pgmname,
			    strerror(errno));
			return (1);
		}
	}

	if (opts->dosoaker)
		if (priocntl(0, 0, PC_GETCID, &fxinfo) == -1) {
			(void) fprintf(stderr, gettext(
			    "%s: couldn't get FX scheduler class: %s\n"),
			    opts->pgmname, strerror(errno));
			return (1);
		}

	/*
	 * Only include processors that are participating in the system
	 */
	for (c = 0, i = 0; i < ncpus; c++) {
		switch (p_online(c, P_STATUS)) {
		case P_ONLINE:
		case P_NOINTR:
			if (smt) {

				gstate[i].chip_id = get_chipid(kc, c);
				if (gstate[i].chip_id != -1 &&
				    chip_designees[gstate[i].chip_id] == -1)
					chip_designees[gstate[i].chip_id] = c;
			}

			gstate[i++].cpuid = c;
			break;
		case P_OFFLINE:
		case P_POWEROFF:
		case P_FAULTED:
		case P_SPARE:
		case P_DISABLED:
			gstate[i++].cpuid = -1;
			break;
		default:
			gstate[i++].cpuid = -1;
			(void) fprintf(stderr,
			    gettext("%s: cpu%d in unknown state\n"),
			    opts->pgmname, c);
			break;
		case -1:
			break;
		}
	}

	/*
	 * Examine the processor sets; if we're in one, only attempt
	 * to report on the set we're in.
	 */
	if (pset_bind(PS_QUERY, P_PID, P_MYID, &mypset) == -1) {
		errstr = strerror(errno);
		(void) fprintf(stderr, gettext("%s: pset_bind - %s\n"),
		    opts->pgmname, errstr);
	} else {
		for (i = 0; i < ncpus; i++) {
			struct tstate *this = &gstate[i];

			if (this->cpuid == -1)
				continue;

			if (pset_assign(PS_QUERY,
			    this->cpuid, &cpupset) == -1) {
				errstr = strerror(errno);
				(void) fprintf(stderr,
				    gettext("%s: pset_assign - %s\n"),
				    opts->pgmname, errstr);
				continue;
			}

			if (mypset != cpupset)
				this->cpuid = -1;
		}
	}

	if (opts->dotitle)
		print_title(opts->master);
	zerotime();

	for (i = 0; i < ncpus; i++) {
		struct tstate *this = &gstate[i];

		if (this->cpuid == -1)
			continue;
		this->sgrp = cpc_setgrp_clone(opts->master);
		if (this->sgrp == NULL) {
			this->cpuid = -1;
			continue;
		}
		if (thr_create(NULL, 0, gtick, this,
		    THR_BOUND|THR_NEW_LWP, &this->tid) == 0)
			lwps++;
		else {
			(void) fprintf(stderr,
			    gettext("%s: cannot create thread for cpu%d\n"),
			    opts->pgmname, this->cpuid);
			this->status = 4;
		}
	}

	if (lwps != 0)
		for (i = 0; i < ncpus; i++)
			(void) thr_join(gstate[i].tid, NULL, NULL);

	if ((accum = cpc_setgrp_clone(opts->master)) == NULL) {
		(void) fprintf(stderr, gettext("%s: out of heap\n"),
		    opts->pgmname);
		return (1);
	}

	retval = 0;
	for (i = 0; i < ncpus; i++) {
		struct tstate *this = &gstate[i];

		if (this->cpuid == -1)
			continue;
		cpc_setgrp_accum(accum, this->sgrp);
		cpc_setgrp_free(this->sgrp);
		this->sgrp = NULL;
		if (this->status != 0)
			retval = 1;
	}

	cpc_setgrp_reset(accum);
	start = cpc_setgrp_getset(accum);
	do {
		nreqs = cpc_setgrp_getbufs(accum, &data1, &data2, &scratch);
		print_total(lwps, *data1, nreqs, cpc_setgrp_getname(accum));
	} while (cpc_setgrp_nextset(accum) != start);

	cpc_setgrp_free(accum);
	accum = NULL;

	free(gstate);
	return (retval);
}

static int
get_chipid(kstat_ctl_t *kc, processorid_t cpuid)
{
	kstat_t		*ksp;
	kstat_named_t	*k;

	if ((ksp = kstat_lookup(kc, "cpu_info", cpuid, NULL)) == NULL)
		return (-1);

	if (kstat_read(kc, ksp, NULL) == -1) {
		(void) fprintf(stderr,
		    gettext("%s: kstat_read() failed for cpu %d: %s\n"),
		    opts->pgmname, cpuid, strerror(errno));
		return (-1);
	}

	if ((k = (kstat_named_t *)kstat_data_lookup(ksp, "chip_id")) == NULL) {
		(void) fprintf(stderr,
		    gettext("%s: chip_id not found for cpu %d: %s\n"),
		    opts->pgmname, cpuid, strerror(errno));
		return (-1);
	}

	return (k->value.i32);
}

static void *
soaker(void *arg)
{
	struct tstate	*state = arg;
	pcparms_t	pcparms;
	fxparms_t	*fx = (fxparms_t *)pcparms.pc_clparms;

	if (processor_bind(P_LWPID, P_MYID, state->cpuid, NULL) != 0)
		(void) fprintf(stderr, gettext("%s: couldn't bind soaker "
		    "thread to cpu%d: %s\n"), opts->pgmname, state->cpuid,
		    strerror(errno));

	/*
	 * Put the soaker thread in the fixed priority (FX) class so it runs
	 * at the lowest possible global priority.
	 */
	pcparms.pc_cid = fxinfo.pc_cid;
	fx->fx_upri = 0;
	fx->fx_uprilim = 0;
	fx->fx_tqsecs = fx->fx_tqnsecs = FX_TQDEF;

	if (priocntl(P_LWPID, P_MYID, PC_SETPARMS, &pcparms) != 0)
		(void) fprintf(stderr, gettext("%s: couldn't put soaker "
		    "thread in FX sched class: %s\n"), opts->pgmname,
		    strerror(errno));

	/*
	 * Let the parent thread know we're ready to roll.
	 */
	(void) mutex_lock(&state->soak_lock);
	state->soak_state = SOAK_RUN;
	(void) cond_signal(&state->soak_cv);
	(void) mutex_unlock(&state->soak_lock);

	for (;;) {
spin:
		(void) mutex_lock(&state->soak_lock);
		if (state->soak_state == SOAK_RUN) {
			(void) mutex_unlock(&state->soak_lock);
			goto spin;
		}

		while (state->soak_state == SOAK_PAUSE)
			(void) cond_wait(&state->soak_cv,
			    &state->soak_lock);
		(void) mutex_unlock(&state->soak_lock);
	}

	/*NOTREACHED*/
	return (NULL);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <libintl.h>
#include <locale.h>
#include <errno.h>
#include <kstat.h>
#include <libcpc.h>

#include "cpucmds.h"

static struct options {
	int debug;
	int verbose;
	int dotitle;
	int dohelp;
	int dotick;
	int cpuver;
	char *pgmname;
	uint_t mseconds;
	uint_t nsamples;
	uint_t nsets;
	cpc_setgrp_t *master;
	int followfork;
	int followexec;
	pid_t pid;
	FILE *log;
} __options;

static const struct options *opts = (const struct options *)&__options;

static cpc_t *cpc;

/*
 * How many signals caught from terminal
 * We bail out as soon as possible when interrupt is set
 */
static int	interrupt = 0;

/*ARGSUSED*/
static void
cputrack_errfn(const char *fn, int subcode, const char *fmt, va_list ap)
{
	(void) fprintf(stderr, "%s: ", opts->pgmname);
	if (opts->debug)
		(void) fprintf(stderr, "%s: ", fn);
	(void) vfprintf(stderr, fmt, ap);
}

static void
cputrack_pctx_errfn(const char *fn, const char *fmt, va_list ap)
{
	cputrack_errfn(fn, -1, fmt, ap);
}

static int cputrack(int argc, char *argv[], int optind);
static void intr(int);

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif

int
main(int argc, char *argv[])
{
	struct options *opts = &__options;
	int c, errcnt = 0;
	int nsamples;
	cpc_setgrp_t *sgrp;
	char *errstr;
	int ret;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	if ((opts->pgmname = strrchr(argv[0], '/')) == NULL)
		opts->pgmname = argv[0];
	else
		opts->pgmname++;

	if ((cpc = cpc_open(CPC_VER_CURRENT)) == NULL) {
		errstr = strerror(errno);
		(void) fprintf(stderr, gettext("%s: cannot access performance "
		    "counter library - %s\n"), opts->pgmname, errstr);
		return (1);
	}

	(void) cpc_seterrhndlr(cpc, cputrack_errfn);
	strtoset_errfn = cputrack_errfn;

	/*
	 * Establish (non-zero) defaults
	 */
	opts->mseconds = 1000;
	opts->dotitle = 1;
	opts->log = stdout;
	if ((opts->master = cpc_setgrp_new(cpc, 0)) == NULL) {
		(void) fprintf(stderr, gettext("%s: no memory available\n"),
		    opts->pgmname);
		exit(1);
	}

	while ((c = getopt(argc, argv, "T:N:Defhntvo:r:c:p:")) != EOF)
		switch (c) {
		case 'T':			/* sample time,	seconds */
			opts->mseconds = (uint_t)(atof(optarg) * 1000.0);
			break;
		case 'N':			/* number of samples */
			nsamples = atoi(optarg);
			if (nsamples < 0)
				errcnt++;
			else
				opts->nsamples = (uint_t)nsamples;
			break;
		case 'D':			/* enable debugging */
			opts->debug++;
			break;
		case 'f':			/* follow fork */
			opts->followfork++;
			break;
		case 'e':			/* follow exec */
			opts->followexec++;
			break;
		case 'n':			/* no titles */
			opts->dotitle = 0;
			break;
		case 't':			/* print %tick */
			opts->dotick = 1;
			break;
		case 'v':
			opts->verbose = 1;	/* more chatty */
			break;
		case 'o':
			if (optarg == NULL) {
				errcnt++;
				break;
			}
			if ((opts->log = fopen(optarg, "w")) == NULL) {
				(void) fprintf(stderr, gettext(
				    "%s: cannot open '%s' for writing\n"),
				    opts->pgmname, optarg);
				return (1);
			}
			break;
		case 'c':			/* specify statistics */
			if ((sgrp = cpc_setgrp_newset(opts->master,
			    optarg, &errcnt)) != NULL)
				opts->master = sgrp;
			break;
		case 'p':			/* grab given pid */
			if ((opts->pid = atoi(optarg)) <= 0)
				errcnt++;
			break;
		case 'h':
			opts->dohelp = 1;
			break;
		case '?':
		default:
			errcnt++;
			break;
		}

	if (opts->nsamples == 0)
		opts->nsamples = UINT_MAX;

	if (errcnt != 0 ||
	    opts->dohelp ||
	    (argc == optind && opts->pid == 0) ||
	    (argc > optind && opts->pid != 0) ||
	    (opts->nsets = cpc_setgrp_numsets(opts->master)) == 0) {
		(void) fprintf(opts->dohelp ? stdout : stderr, gettext(
		    "Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
		    "\t\t-c events [command [args] | -p pid]\n\n"
		    "\t-T secs\t  seconds between samples, default 1\n"
		    "\t-N count  number of samples, default unlimited\n"
		    "\t-D\t  enable debug mode\n"
		    "\t-e\t  follow exec(2), and execve(2)\n"
		    "\t-f\t  follow fork(2), fork1(2), and vfork(2)\n"
		    "\t-h\t  print extended usage information\n"
		    "\t-n\t  suppress titles\n"
		    "\t-t\t  include virtualized %s register\n"
		    "\t-v\t  verbose mode\n"
		    "\t-o file\t  write cpu statistics to this file\n"
		    "\t-c events specify processor events to be monitored\n"
		    "\t-p pid\t  pid of existing process to capture\n\n"
		    "\tUse cpustat(8) to monitor system-wide statistics.\n"),
		    opts->pgmname, CPC_TICKREG_NAME);
		if (opts->dohelp) {
			(void) putchar('\n');
			(void) capabilities(cpc, stdout);
			exit(0);
		}
		exit(2);
	}

	/*
	 * Catch signals from terminal, so they can be handled asynchronously
	 * when we're ready instead of when we're not (;-)
	 */
	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
		(void) sigset(SIGHUP, intr);
	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
		(void) sigset(SIGINT, intr);
	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
		(void) sigset(SIGQUIT, intr);
	(void) sigset(SIGPIPE, intr);
	(void) sigset(SIGTERM, intr);

	cpc_setgrp_reset(opts->master);
	(void) setvbuf(opts->log, NULL, _IOLBF, 0);
	ret = cputrack(argc, argv, optind);
	(void) cpc_close(cpc);
	return (ret);
}

static void
print_title(cpc_setgrp_t *sgrp)
{
	(void) fprintf(opts->log, "%7s ", "time");
	if (opts->followfork)
		(void) fprintf(opts->log, "%6s ", "pid");
	(void) fprintf(opts->log, "%3s %10s ", "lwp", "event");
	if (opts->dotick)
		(void) fprintf(opts->log, "%9s ", CPC_TICKREG_NAME);
	(void) fprintf(opts->log, "%s\n", cpc_setgrp_gethdr(sgrp));
	(void) fflush(opts->log);
}

static void
print_exec(float now, pid_t pid, char *name)
{
	if (name == NULL)
		name = "(unknown)";

	(void) fprintf(opts->log, "%7.3f ", now);
	if (opts->followfork)
		(void) fprintf(opts->log, "%6d ", (int)pid);
	(void) fprintf(opts->log, "%3d %10s ", 1, "exec");
	if (opts->dotick)
		(void) fprintf(opts->log, "%9s ", "");
	(void) fprintf(opts->log, "%9s %9s # '%s'\n", "", "", name);
	(void) fflush(opts->log);
}

static void
print_fork(float now, pid_t newpid, id_t lwpid, pid_t oldpid)
{
	(void) fprintf(opts->log, "%7.3f ", now);
	if (opts->followfork)
		(void) fprintf(opts->log, "%6d ", (int)oldpid);
	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, "fork");
	if (opts->dotick)
		(void) fprintf(opts->log, "%9s ", "");
	(void) fprintf(opts->log, "%9s %9s # %d\n", "", "", (int)newpid);
	(void) fflush(opts->log);
}

static void
print_sample(pid_t pid, id_t lwpid,
    char *pevent, cpc_buf_t *buf, int nreq, const char *evname)
{
	uint64_t	val;
	int		i;

	(void) fprintf(opts->log, "%7.3f ",
	    mstimestamp(cpc_buf_hrtime(cpc, buf)));
	if (opts->followfork)
		(void) fprintf(opts->log, "%6d ", (int)pid);
	(void) fprintf(opts->log, "%3d %10s ", (int)lwpid, pevent);
	if (opts->dotick)
		(void) fprintf(opts->log, "%9" PRId64 " ",
		    cpc_buf_tick(cpc, buf));
	for (i = 0; i < nreq; i++) {
		(void) cpc_buf_get(cpc, buf, i, &val);
		(void) fprintf(opts->log, "%9" PRId64 " ", val);
	}
	if (opts->nsets > 1)
		(void) fprintf(opts->log, " # %s\n", evname);
	else
		(void) fputc('\n', opts->log);
}

struct pstate {
	cpc_setgrp_t *accum;
	cpc_setgrp_t **sgrps;
	int maxlwpid;
};

static int
pinit_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
{
	struct pstate *state = arg;
	cpc_setgrp_t *sgrp;
	cpc_set_t *set;
	cpc_buf_t **data1, **data2, **scratch;
	char *errstr;
	int nreq;

	if (interrupt)
		return (0);

	if (state->maxlwpid < lwpid) {
		state->sgrps = realloc(state->sgrps,
		    lwpid * sizeof (state->sgrps));
		if (state->sgrps == NULL) {
			(void) fprintf(stderr, gettext(
			    "%6d: init_lwp: out of memory\n"), (int)pid);
			return (-1);
		}
		while (state->maxlwpid < lwpid) {
			state->sgrps[state->maxlwpid] = NULL;
			state->maxlwpid++;
		}
	}

	if ((sgrp = state->sgrps[lwpid-1]) == NULL) {
		if ((sgrp = cpc_setgrp_clone(opts->master)) == NULL) {
			(void) fprintf(stderr, gettext(
			    "%6d: init_lwp: out of memory\n"), (int)pid);
			return (-1);
		}
		state->sgrps[lwpid-1] = sgrp;
		set = cpc_setgrp_getset(sgrp);
	} else {
		cpc_setgrp_reset(sgrp);
		set = cpc_setgrp_getset(sgrp);
	}

	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);

	if (cpc_bind_pctx(cpc, pctx, lwpid, set, 0) != 0 ||
	    cpc_set_sample(cpc, set, *data2) != 0) {
		errstr = strerror(errno);
		if (errno == EAGAIN) {
			(void) cpc_unbind(cpc, set);
		}

		(void) fprintf(stderr, gettext(
		    "%6d: init_lwp: can't bind perf counters "
		    "to lwp%d - %s\n"), (int)pid, (int)lwpid, errstr);
		return (-1);
	}

	if (opts->verbose)
		print_sample(pid, lwpid, "init_lwp",
		    *data2, nreq, cpc_setgrp_getname(sgrp));
	return (0);
}

/*ARGSUSED*/
static int
pfini_lwp(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
{
	struct pstate *state = arg;
	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
	cpc_set_t *set;
	char *errstr;
	cpc_buf_t **data1, **data2, **scratch;
	int nreq;

	if (interrupt)
		return (0);

	set = cpc_setgrp_getset(sgrp);
	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
	if (cpc_set_sample(cpc, set, *scratch) == 0) {
		if (opts->nsets == 1) {
			/*
			 * When we only have one set of counts, the sample
			 * gives us the accumulated count.
			 */
			*data1 = *scratch;
		} else {
			/*
			 * When we have more than one set of counts, the
			 * sample gives us the count for the latest sample
			 * period. *data1 contains the accumulated count but
			 * does not include the count for the latest sample
			 * period for this set of counters.
			 */
			cpc_buf_add(cpc, *data1, *data1, *scratch);
		}
		if (opts->verbose)
			print_sample(pid, lwpid, "fini_lwp",
			    *data1, nreq, cpc_setgrp_getname(sgrp));
		cpc_setgrp_accum(state->accum, sgrp);
		if (cpc_unbind(cpc, set) == 0)
			return (0);
	}

	switch (errno) {
	case EAGAIN:
		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
		    "lwp%d: perf counter contents invalidated\n"),
		    (int)pid, (int)lwpid);
		break;
	default:
		errstr = strerror(errno);
		(void) fprintf(stderr, gettext("%6d: fini_lwp: "
		    "lwp%d: can't access perf counters - %s\n"),
		    (int)pid, (int)lwpid, errstr);
		break;
	}
	return (-1);
}

/*ARGSUSED*/
static int
plwp_create(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
{
	cpc_setgrp_t	*sgrp = opts->master;
	cpc_buf_t	**data1, **data2, **scratch;
	int		nreq;

	if (interrupt)
		return (0);

	nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);

	print_sample(pid, lwpid, "lwp_create",
	    *data1, nreq, cpc_setgrp_getname(sgrp));

	return (0);
}

/*ARGSUSED*/
static int
plwp_exit(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
{
	struct pstate	*state = arg;
	cpc_setgrp_t	*sgrp = state->sgrps[lwpid-1];
	cpc_set_t	*start;
	int		nreq;
	cpc_buf_t	**data1, **data2, **scratch;

	if (interrupt)
		return (0);

	start = cpc_setgrp_getset(sgrp);
	do {
		nreq = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);
		if (cpc_buf_hrtime(cpc, *data1) == 0)
			continue;
		print_sample(pid, lwpid, "lwp_exit",
		    *data1, nreq, cpc_setgrp_getname(sgrp));
	} while (cpc_setgrp_nextset(sgrp) != start);

	return (0);
}

/*ARGSUSED*/
static int
pexec(pctx_t *pctx, pid_t pid, id_t lwpid, char *name, void *arg)
{
	struct pstate	*state = arg;
	float		now = 0.0;
	cpc_set_t	*start;
	int		nreq;
	cpc_buf_t	**data1, **data2, **scratch;
	hrtime_t	hrt;

	if (interrupt)
		return (0);

	/*
	 * Print the accumulated results from the previous program image
	 */
	cpc_setgrp_reset(state->accum);
	start = cpc_setgrp_getset(state->accum);
	do {
		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
		    &scratch);
		hrt = cpc_buf_hrtime(cpc, *data1);
		if (hrt == 0)
			continue;
		print_sample(pid, lwpid, "exec",
		    *data1, nreq, cpc_setgrp_getname(state->accum));
		if (now < mstimestamp(hrt))
			now = mstimestamp(hrt);
	} while (cpc_setgrp_nextset(state->accum) != start);

	print_exec(now, pid, name);

	if (state->accum != NULL) {
		cpc_setgrp_free(state->accum);
		state->accum = NULL;
	}

	if (opts->followexec) {
		state->accum = cpc_setgrp_clone(opts->master);
		return (0);
	}
	return (-1);
}

/*ARGSUSED*/
static void
pexit(pctx_t *pctx, pid_t pid, id_t lwpid, int status, void *arg)
{
	struct pstate	*state = arg;
	cpc_set_t	*start;
	int		nreq;
	cpc_buf_t	**data1, **data2, **scratch;

	if (interrupt)
		return;

	cpc_setgrp_reset(state->accum);
	start = cpc_setgrp_getset(state->accum);
	do {
		nreq = cpc_setgrp_getbufs(state->accum, &data1, &data2,
		    &scratch);
		if (cpc_buf_hrtime(cpc, *data1) == 0)
			continue;
		print_sample(pid, lwpid, "exit",
		    *data1, nreq, cpc_setgrp_getname(state->accum));
	} while (cpc_setgrp_nextset(state->accum) != start);

	cpc_setgrp_free(state->accum);
	state->accum = NULL;

	for (lwpid = 1; lwpid < state->maxlwpid; lwpid++)
		if (state->sgrps[lwpid-1] != NULL) {
			cpc_setgrp_free(state->sgrps[lwpid-1]);
			state->sgrps[lwpid-1] = NULL;
		}
	free(state->sgrps);
	state->sgrps = NULL;
}

static int
ptick(pctx_t *pctx, pid_t pid, id_t lwpid, void *arg)
{
	struct pstate *state = arg;
	cpc_setgrp_t *sgrp = state->sgrps[lwpid-1];
	cpc_set_t *this = cpc_setgrp_getset(sgrp);
	const char *name = cpc_setgrp_getname(sgrp);
	cpc_buf_t **data1, **data2, **scratch, *tmp;
	char *errstr;
	int nreqs;

	if (interrupt)
		return (0);

	nreqs = cpc_setgrp_getbufs(sgrp, &data1, &data2, &scratch);

	if (opts->nsets == 1) {
		/*
		 * If we're dealing with one set, buffer usage is:
		 *
		 * data1 = most recent data snapshot
		 * data2 = previous data snapshot
		 * scratch = used for diffing data1 and data2
		 *
		 * Save the snapshot from the previous sample in data2
		 * before putting the current sample in data1.
		 */
		tmp = *data1;
		*data1 = *data2;
		*data2 = tmp;
		if (cpc_set_sample(cpc, this, *data1) != 0)
			goto broken;
		cpc_buf_sub(cpc, *scratch, *data1, *data2);
	} else {
		cpc_set_t *next = cpc_setgrp_nextset(sgrp);
		/*
		 * If there is more than set in use, we will need to
		 * unbind and re-bind on each go-around because each
		 * time a counter is bound, it is preset to 0 (as it was
		 * specified when the requests were added to the set).
		 *
		 * Buffer usage in this case is:
		 *
		 * data1 = total counts for this set since program began
		 * data2 = unused
		 * scratch = most recent data snapshot
		 */

		if (cpc_set_sample(cpc, this, *scratch) != 0)
			goto broken;
		cpc_buf_add(cpc, *data1, *data1, *scratch);

		/*
		 * No need to unbind the previous set, as binding another set
		 * automatically unbinds the most recently bound set.
		 */
		if (cpc_bind_pctx(cpc, pctx, lwpid, next, 0) != 0)
			goto broken;
	}
	print_sample(pid, lwpid, "tick", *scratch, nreqs, name);

	return (0);

broken:
	switch (errno) {
	case EAGAIN:
		(void) fprintf(stderr, gettext(
		    "%6d: tick: lwp%d: perf counter contents invalidated\n"),
		    (int)pid, (int)lwpid);
		break;
	default:
		errstr = strerror(errno);
		(void) fprintf(stderr, gettext(
		    "%6d: tick: lwp%d: can't access perf counter - %s\n"),
		    (int)pid, (int)lwpid, errstr);
		break;
	}
	(void) cpc_unbind(cpc, this);
	return (-1);
}

/*
 * The system has just created a new address space that has a new pid.
 * We're running in a child of the controlling process, with a new
 * pctx handle already opened on the child of the original controlled process.
 */
static void
pfork(pctx_t *pctx, pid_t oldpid, pid_t pid, id_t lwpid, void *arg)
{
	struct pstate *state = arg;

	print_fork(mstimestamp(0), pid, lwpid, oldpid);

	if (!opts->followfork)
		return;

	if (pctx_set_events(pctx,
	    PCTX_SYSC_EXEC_EVENT, pexec,
	    PCTX_SYSC_FORK_EVENT, pfork,
	    PCTX_SYSC_EXIT_EVENT, pexit,
	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
	    PCTX_INIT_LWP_EVENT, pinit_lwp,
	    PCTX_FINI_LWP_EVENT, pfini_lwp,
	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
	    PCTX_NULL_EVENT) == 0) {
		state->accum = cpc_setgrp_clone(opts->master);
		(void) pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
		if (state->accum) {
			free(state->accum);
			state->accum = NULL;
		}
	}
}

/*
 * Translate the incoming options into actions, and get the
 * tool and the process to control running.
 */
static int
cputrack(int argc, char *argv[], int optind)
{
	struct pstate __state, *state = &__state;
	pctx_t *pctx;
	int err;

	bzero(state, sizeof (*state));

	if (opts->pid == 0) {
		if (argc <= optind) {
			(void) fprintf(stderr, "%s: %s\n",
			    opts->pgmname,
			    gettext("no program to start"));
			return (1);
		}
		pctx = pctx_create(argv[optind],
		    &argv[optind], state, 1, cputrack_pctx_errfn);
		if (pctx == NULL) {
			(void) fprintf(stderr, "%s: %s '%s'\n",
			    opts->pgmname,
			    gettext("failed to start program"),
			    argv[optind]);
			return (1);
		}
	} else {
		pctx = pctx_capture(opts->pid, state, 1, cputrack_pctx_errfn);
		if (pctx == NULL) {
			(void) fprintf(stderr, "%s: %s %d\n",
			    opts->pgmname,
			    gettext("failed to capture pid"),
			    (int)opts->pid);
			return (1);
		}
	}

	err = pctx_set_events(pctx,
	    PCTX_SYSC_EXEC_EVENT, pexec,
	    PCTX_SYSC_FORK_EVENT, pfork,
	    PCTX_SYSC_EXIT_EVENT, pexit,
	    PCTX_SYSC_LWP_CREATE_EVENT, plwp_create,
	    PCTX_INIT_LWP_EVENT, pinit_lwp,
	    PCTX_FINI_LWP_EVENT, pfini_lwp,
	    PCTX_SYSC_LWP_EXIT_EVENT, plwp_exit,
	    PCTX_NULL_EVENT);

	if (err != 0) {
		(void) fprintf(stderr, "%s: %s\n",
		    opts->pgmname,
		    gettext("can't bind process context ops to process"));
	} else {
		if (opts->dotitle)
			print_title(opts->master);
		state->accum = cpc_setgrp_clone(opts->master);
		zerotime();
		err = pctx_run(pctx, opts->mseconds, opts->nsamples, ptick);
		if (state->accum) {
			cpc_setgrp_free(state->accum);
			state->accum = NULL;
		}
	}

	return (err != 0 ? 1 : 0);
}

/*ARGSUSED*/
static void
intr(int sig)
{
	interrupt++;
	if (cpc != NULL)
		cpc_terminate(cpc);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <libintl.h>
#include <sys/types.h>


#include <libcpc.h>
#include "cpucmds.h"

#define	CHARS_PER_REQ 11	/* space required for printing column headers */

/*
 * These routines are solely used to manage a list of request sets.
 */

struct __cpc_setgrp {
	struct setgrp_elem {
		cpc_set_t	*set;
		uint8_t		sysonly;	/* All reqs sys-mode only ? */
		int		nreqs;
		int		*picnums;	/* picnum used per req */
		cpc_buf_t	*data1;
		cpc_buf_t	*data2;
		cpc_buf_t	*scratch;
		char *name;
		char *hdr;
	} *sets;		/* array of events and names */
	int nelem;		/* size of array */
	int current;		/* currently bound event in eventset */
	int smt;		/* Measures physical events on SMT CPU */
	int has_sysonly_set;	/* Does this group have a system-only set? */
	cpc_t *cpc;		/* library handle */
};

static void *emalloc(size_t n);

cpc_setgrp_t *
cpc_setgrp_new(cpc_t *cpc, int smt)
{
	cpc_setgrp_t *sgrp;

	sgrp = emalloc(sizeof (*sgrp));
	sgrp->current = -1;
	sgrp->cpc = cpc;
	sgrp->smt = smt;
	sgrp->has_sysonly_set = 0;
	return (sgrp);
}

/*
 * Walker to count the number of requests in a set, and check if any requests
 * count user-mode events.
 */
/*ARGSUSED*/
static void
cpc_setgrp_walker(void *arg, int index, const char *event, uint64_t preset,
    uint_t flags, int nattrs, const cpc_attr_t *attrs)
{
	struct setgrp_elem *se = arg;

	se->nreqs++;
	if (flags & CPC_COUNT_USER)
		se->sysonly = 0;
}

/*
 * Walker to discover the picnums used by the requests in a set.
 */
/*ARGSUSED*/
static void
cpc_setgrp_picwalker(void *arg, int index, const char *event, uint64_t preset,
    uint_t flags, int nattrs, const cpc_attr_t *attrs)
{
	int *picnums = arg;
	int i;

	for (i = 0; i < nattrs; i++) {
		if (strncmp(attrs[i].ca_name, "picnum", 7) == 0)
			break;
	}
	if (i == nattrs)
		picnums[index] = -1;

	picnums[index] = (int)attrs[i].ca_val;
}

cpc_setgrp_t *
cpc_setgrp_newset(cpc_setgrp_t *sgrp, const char *spec, int *errcnt)
{
	cpc_set_t		*set;
	struct setgrp_elem	*new;
	char			hdr[CHARS_PER_REQ+1];
	int			i;

	if ((set = cpc_strtoset(sgrp->cpc, spec, sgrp->smt)) == NULL) {
		*errcnt += 1;
		return (NULL);
	}

	if ((new = realloc(sgrp->sets, (1 + sgrp->nelem) * sizeof (*new)))
	    == NULL) {
		(void) fprintf(stderr,
		    gettext("cpc_setgrp: no re memory available\n"));
		exit(0);
	}

	sgrp->sets = new;
	sgrp->sets[sgrp->nelem].set = set;
	/*
	 * Count the number of requests in the set we just made. If any requests
	 * in the set have CPC_COUNT_USER in the flags, the sysonly flag will
	 * be cleared.
	 */
	sgrp->sets[sgrp->nelem].nreqs = 0;
	sgrp->sets[sgrp->nelem].sysonly = 1;
	cpc_walk_requests(sgrp->cpc, set, &(sgrp->sets[sgrp->nelem]),
	    cpc_setgrp_walker);

	if (sgrp->sets[sgrp->nelem].sysonly == 1)
		sgrp->has_sysonly_set = 1;

	sgrp->sets[sgrp->nelem].picnums = emalloc(sgrp->sets[sgrp->nelem].nreqs
	    * sizeof (int));

	sgrp->sets[sgrp->nelem].hdr = emalloc((sgrp->sets[sgrp->nelem].nreqs *
	    CHARS_PER_REQ) + 1);

	/*
	 * Find out which picnums the requests are using.
	 */
	cpc_walk_requests(sgrp->cpc, set, sgrp->sets[sgrp->nelem].picnums,
	    cpc_setgrp_picwalker);
	/*
	 * Use the picnums we discovered to build a printable header for this
	 * set.
	 */
	sgrp->sets[sgrp->nelem].hdr[0] = '\0';
	for (i = 0; i < sgrp->sets[sgrp->nelem].nreqs; i++) {
		(void) snprintf(hdr, CHARS_PER_REQ, "%8s%-2d ", "pic",
		    sgrp->sets[sgrp->nelem].picnums[i]);
		(void) strncat(sgrp->sets[sgrp->nelem].hdr, hdr,
		    sgrp->sets[sgrp->nelem].nreqs * CHARS_PER_REQ);
	}
	sgrp->sets[sgrp->nelem].hdr[strlen(sgrp->sets[sgrp->nelem].hdr)] = '\0';

	if ((sgrp->sets[sgrp->nelem].name = strdup(spec)) == NULL) {
		(void) fprintf(stderr,
		    gettext("cpc_setgrp: no memory available\n"));
		exit(0);
	}

	if ((sgrp->sets[sgrp->nelem].data1 = cpc_buf_create(sgrp->cpc, set))
	    == NULL ||
	    (sgrp->sets[sgrp->nelem].data2 = cpc_buf_create(sgrp->cpc, set))
	    == NULL ||
	    (sgrp->sets[sgrp->nelem].scratch = cpc_buf_create(sgrp->cpc, set))
	    == NULL) {
		(void) fprintf(stderr,
		    gettext("cpc_setgrp: no memory available\n"));
		exit(0);
	}

	if (sgrp->current < 0)
		sgrp->current = 0;
	sgrp->nelem++;
	return (sgrp);
}

int
cpc_setgrp_getbufs(cpc_setgrp_t *sgrp, cpc_buf_t ***data1, cpc_buf_t ***data2,
    cpc_buf_t ***scratch)
{
	if ((uint_t)sgrp->current >= sgrp->nelem)
		return (-1);

	*data1   = &(sgrp->sets[sgrp->current].data1);
	*data2   = &(sgrp->sets[sgrp->current].data2);
	*scratch = &(sgrp->sets[sgrp->current].scratch);

	return (sgrp->sets[sgrp->current].nreqs);
}

cpc_setgrp_t *
cpc_setgrp_clone(cpc_setgrp_t *old)
{
	int			i;
	cpc_setgrp_t		*new;
	struct setgrp_elem	*newa;

	new = emalloc(sizeof (*new));
	newa = emalloc(old->nelem * sizeof (*newa));

	new->nelem = old->nelem;
	new->current = old->current;
	new->cpc = old->cpc;
	new->sets = newa;
	new->smt = old->smt;
	new->has_sysonly_set = old->has_sysonly_set;
	for (i = 0; i < old->nelem; i++) {
		if ((newa[i].set = cpc_strtoset(old->cpc, old->sets[i].name,
		    old->smt)) == NULL) {
			(void) fprintf(stderr,
			    gettext("cpc_setgrp: cpc_strtoset() failed\n"));
			exit(0);
		}
		if ((newa[i].name = strdup(old->sets[i].name)) == NULL) {
			(void) fprintf(stderr,
			    gettext("cpc_setgrp: no memory available\n"));
			exit(0);
		}
		newa[i].sysonly = old->sets[i].sysonly;
		newa[i].nreqs = old->sets[i].nreqs;
		newa[i].data1 = cpc_buf_create(old->cpc, newa[i].set);
		newa[i].data2 = cpc_buf_create(old->cpc, newa[i].set);
		newa[i].scratch = cpc_buf_create(old->cpc, newa[i].set);
		if (newa[i].data1 == NULL || newa[i].data2 == NULL ||
		    newa[i].scratch == NULL) {
			(void) fprintf(stderr,
			    gettext("cpc_setgrp: no memory available\n"));
			exit(0);
		}
		cpc_buf_copy(old->cpc, newa[i].data1, old->sets[i].data1);
		cpc_buf_copy(old->cpc, newa[i].data2, old->sets[i].data2);
		cpc_buf_copy(old->cpc, newa[i].scratch, old->sets[i].scratch);
	}
	return (new);
}

static void
cpc_setgrp_delset(cpc_setgrp_t *sgrp)
{
	int l;

	if ((uint_t)sgrp->current >= sgrp->nelem)
		sgrp->current = sgrp->nelem - 1;
	if (sgrp->current < 0)
		return;
	free(sgrp->sets[sgrp->current].name);
	free(sgrp->sets[sgrp->current].hdr);
	free(sgrp->sets[sgrp->current].picnums);
	(void) cpc_buf_destroy(sgrp->cpc, sgrp->sets[sgrp->current].data1);
	(void) cpc_buf_destroy(sgrp->cpc, sgrp->sets[sgrp->current].data2);
	(void) cpc_buf_destroy(sgrp->cpc, sgrp->sets[sgrp->current].scratch);
	for (l = sgrp->current; l < sgrp->nelem - 1; l++)
		sgrp->sets[l] = sgrp->sets[l + 1];
	sgrp->nelem--;
}

void
cpc_setgrp_free(cpc_setgrp_t *sgrp)
{
	if (sgrp->sets) {
		while (sgrp->nelem)
			cpc_setgrp_delset(sgrp);
		free(sgrp->sets);
	}
	free(sgrp);
}

cpc_set_t *
cpc_setgrp_getset(cpc_setgrp_t *sgrp)
{
	if ((uint_t)sgrp->current >= sgrp->nelem)
		return (NULL);
	return (sgrp->sets[sgrp->current].set);
}

const char *
cpc_setgrp_getname(cpc_setgrp_t *sgrp)
{
	if ((uint_t)sgrp->current >= sgrp->nelem)
		return (NULL);
	return (sgrp->sets[sgrp->current].name);
}

const char *
cpc_setgrp_gethdr(cpc_setgrp_t *sgrp)
{
	if ((uint_t)sgrp->current >= sgrp->nelem)
		return (NULL);
	return (sgrp->sets[sgrp->current].hdr);
}

int
cpc_setgrp_numsets(cpc_setgrp_t *sgrp)
{
	return (sgrp->nelem);
}

cpc_set_t *
cpc_setgrp_nextset(cpc_setgrp_t *sgrp)
{
	if (sgrp->current < 0)
		return (NULL);

	if (++sgrp->current >= sgrp->nelem)
		sgrp->current = 0;

	return (cpc_setgrp_getset(sgrp));
}

/*
 * Put the setgrp pointer back to the beginning of the set
 */
void
cpc_setgrp_reset(cpc_setgrp_t *sgrp)
{
	if (sgrp->current > 0)
		sgrp->current = 0;
}

/*
 * Adds the data from the 'data1' buf into the accum setgrp.
 */
void
cpc_setgrp_accum(cpc_setgrp_t *accum, cpc_setgrp_t *sgrp)
{
	int i;

	cpc_setgrp_reset(accum);
	cpc_setgrp_reset(sgrp);
	if (accum->nelem != sgrp->nelem)
		return;

	for (i = 0; i < sgrp->nelem; i++) {
		if (accum->sets[i].nreqs != sgrp->sets[i].nreqs)
			return;
		cpc_buf_add(sgrp->cpc, accum->sets[i].data1,
		    accum->sets[i].data1, sgrp->sets[i].data1);
	}
}

/*
 * Returns 1 if all requests in the current set count only system-mode events.
 */
int
cpc_setgrp_sysonly(cpc_setgrp_t *sgrp)
{
	return ((int)sgrp->sets[sgrp->current].sysonly);
}

/*
 * Returns 1 if any set in the group is a system-mode-only set.
 */
int
cpc_setgrp_has_sysonly(cpc_setgrp_t *sgrp)
{
	return (sgrp->has_sysonly_set);
}

/*
 * If we ever fail to get memory, we print an error message and exit.
 */
static void *
emalloc(size_t n)
{
	/*
	 * Several callers of this routine need zero-filled buffers.
	 */
	void *p = calloc(1, n);

	if (p == NULL) {
		(void) fprintf(stderr,
		    gettext("cpc_setgrp: no memory available\n"));
		exit(0);
	}

	return (p);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <libintl.h>

#include "libcpc.h"

/*
 * Takes a string and converts it to a cpc_set_t.
 *
 * While processing the string using getsubopt(), we will use an array of
 * requests to hold the data, and a proprietary representation of attributes
 * which allow us to avoid a realloc()/bcopy() dance every time we come across
 * a new attribute.
 *
 * Not until after the string has been processed in its entirety do we
 * allocate and specify a request set properly.
 */

/*
 * Leave enough room in token strings for picn, nousern, or sysn where n is
 * picnum.
 */
#define	TOK_SIZE	10

typedef struct __tmp_attr {
	char			*name;
	uint64_t		val;
	struct __tmp_attr	*next;
} tmp_attr_t;

typedef struct __tok_info {
	char			*name;
	int			picnum;
} tok_info_t;

typedef struct __request_t {
	char			cr_event[CPC_MAX_EVENT_LEN];
	uint_t			cr_flags;
	uint_t			cr_nattrs;	/* # CPU-specific attrs */
} request_t;

static void strtoset_cleanup(void);
static void smt_special(int picnum);
static void *emalloc(size_t n);

/*
 * Clients of cpc_strtoset may set this to specify an error handler during
 * string parsing.
 */
cpc_errhndlr_t		*strtoset_errfn = NULL;

static request_t	*reqs;
static int		nreqs;
static int		ncounters;

static tmp_attr_t	**attrs;
static int		ntoks;
static char		**toks;
static tok_info_t	*tok_info;
static int		(*(*tok_funcs))(int, char *);
static char		**attrlist;	/* array of ptrs to toks in attrlistp */
static int		nattrs;
static cpc_t		*cpc;
static int		found;

static void
strtoset_err(const char *fmt, ...)
{
	va_list ap;

	if (strtoset_errfn == NULL)
		return;

	va_start(ap, fmt);
	(*strtoset_errfn)("cpc_strtoset", -1, fmt, ap);
	va_end(ap);
}

/*ARGSUSED*/
static void
event_walker(void *arg, uint_t picno, const char *event)
{
	if (strncmp(arg, event, CPC_MAX_EVENT_LEN) == 0)
		found = 1;
}

static int
event_valid(int picnum, char *event)
{
	char *end_event;
	int err;

	found = 0;

	cpc_walk_events_pic(cpc, picnum, event, event_walker);

	if (found)
		return (1);

	cpc_walk_generic_events_pic(cpc, picnum, event, event_walker);

	if (found)
		return (1);

	/*
	 * Before assuming this is an invalid event, see if we have been given
	 * a raw event code.
	 * Check the second argument of strtol() to ensure invalid events
	 * beginning with number do not go through.
	 */
	err = errno;
	errno = 0;
	(void) strtol(event, &end_event, 0);
	if ((errno == 0) && (*end_event == '\0')) {
		/*
		 * Success - this is a valid raw code in hex, decimal, or octal.
		 */
		errno = err;
		return (1);
	}

	errno = err;
	return (0);
}

/*
 * An unknown token was encountered; check here if it is an implicit event
 * name. We allow users to omit the "picn=" portion of the event spec, and
 * assign such events to available pics in order they are returned from
 * getsubopt(3C). We start our search for an available pic _after_ the highest
 * picnum to be assigned. This ensures that the event spec can never be out of
 * order; i.e. if the event string is "eventa,eventb" we must ensure that the
 * picnum counting eventa is less than the picnum counting eventb.
 */
static int
find_event(char *event)
{
	int i;

	/*
	 * Event names cannot have '=' in them. If present here, it means we
	 * have encountered an unknown token (foo=bar, for example).
	 */
	if (strchr(event, '=') != NULL)
		return (0);

	/*
	 * Find the first unavailable pic, after which we must start our search.
	 */
	for (i = ncounters - 1; i >= 0; i--) {
		if (reqs[i].cr_event[0] != '\0')
			break;
	}
	/*
	 * If the last counter has been assigned, we cannot place this event.
	 */
	if (i == ncounters - 1)
		return (0);

	/*
	 * If none of the counters have been assigned yet, i is -1 and we will
	 * begin our search at 0. Else we begin our search at the counter after
	 * the last one currently assigned.
	 */
	i++;

	for (; i < ncounters; i++) {
		if (event_valid(i, event) == 0)
			continue;

		nreqs++;
		(void) strncpy(reqs[i].cr_event, event, CPC_MAX_EVENT_LEN);
		return (1);
	}

	return (0);
}

static int
pic(int tok, char *val)
{
	int picnum = tok_info[tok].picnum;
	/*
	 * Make sure the each pic only appears in the spec once.
	 */
	if (reqs[picnum].cr_event[0] != '\0') {
		strtoset_err(gettext("repeated 'pic%d' token\n"), picnum);
		return (-1);
	}

	if (val == NULL || val[0] == '\0') {
		strtoset_err(gettext("missing 'pic%d' value\n"), picnum);
		return (-1);
	}

	if (event_valid(picnum, val) == 0) {
		strtoset_err(gettext("pic%d cannot measure event '%s' on this "
		    "cpu\n"), picnum, val);
		return (-1);
	}

	nreqs++;
	(void) strncpy(reqs[picnum].cr_event, val, CPC_MAX_EVENT_LEN);
	return (0);
}

/*
 * We explicitly ignore any value provided for these tokens, as their
 * mere presence signals us to turn on or off the relevant flags.
 */
/*ARGSUSED*/
static int
flag(int tok, char *val)
{
	int i;
	int picnum = tok_info[tok].picnum;

	/*
	 * If picnum is -1, this flag should be applied to all reqs.
	 */
	for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) {
		if (strcmp(tok_info[tok].name, "nouser") == 0)
			reqs[i].cr_flags &= ~CPC_COUNT_USER;
		else if (strcmp(tok_info[tok].name, "sys") == 0)
			reqs[i].cr_flags |= CPC_COUNT_SYSTEM;
		else
			return (-1);

		if (picnum != -1)
			break;
	}

	return (0);
}

static int
doattr(int tok, char *val)
{
	int		i;
	int		picnum = tok_info[tok].picnum;
	tmp_attr_t	*tmp;
	char		*endptr;

	/*
	 * If picnum is -1, this attribute should be applied to all reqs.
	 */
	for (i = (picnum == -1) ? 0 : picnum; i < ncounters; i++) {
		tmp = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
		tmp->name = tok_info[tok].name;
		if (val != NULL) {
			tmp->val = strtoll(val, &endptr, 0);
			if (endptr == val) {
				strtoset_err(gettext("invalid value '%s' for "
				    "attribute '%s'\n"), val, tmp->name);
				free(tmp);
				return (-1);
			}
		} else
			/*
			 * No value was provided for this attribute,
			 * so specify a default value of 1.
			 */
			tmp->val = 1;

		tmp->next = attrs[i];
		attrs[i] = tmp;
		reqs[i].cr_nattrs++;

		if (picnum != -1)
			break;
	}

	return (0);
}

/*ARGSUSED*/
static void
attr_count_walker(void *arg, const char *attr)
{
	/*
	 * We don't allow picnum to be specified by the user.
	 */
	if (strncmp(attr, "picnum", 7) == 0)
		return;
	(*(int *)arg)++;
}

static int
cpc_count_attrs(cpc_t *cpc)
{
	int nattrs = 0;

	cpc_walk_attrs(cpc, &nattrs, attr_count_walker);

	return (nattrs);
}

static void
attr_walker(void *arg, const char *attr)
{
	int *i = arg;

	if (strncmp(attr, "picnum", 7) == 0)
		return;

	if ((attrlist[(*i)++] = strdup(attr)) == NULL) {
		strtoset_err(gettext("no memory available\n"));
		exit(0);
	}
}

cpc_set_t *
cpc_strtoset(cpc_t *cpcin, const char *spec, int smt)
{
	cpc_set_t		*set;
	cpc_attr_t		*req_attrs;
	tmp_attr_t		*tmp;
	size_t			toklen;
	int			i;
	int			j;
	int			x;
	char			*opts;
	char			*val;

	cpc = cpcin;
	nattrs = 0;

	ncounters = cpc_npic(cpc);

	reqs = (request_t *)emalloc(ncounters * sizeof (request_t));

	attrs = (tmp_attr_t **)emalloc(ncounters * sizeof (tmp_attr_t *));

	for (i = 0; i < ncounters; i++) {
		reqs[i].cr_event[0] = '\0';
		reqs[i].cr_flags = CPC_COUNT_USER;
		/*
		 * Each pic will have at least one attribute: the physical pic
		 * assignment via the "picnum" attribute. Set that up here for
		 * each request.
		 */
		reqs[i].cr_nattrs = 1;
		attrs[i] = emalloc(sizeof (tmp_attr_t));
		attrs[i]->name = "picnum";
		attrs[i]->val = i;
		attrs[i]->next = NULL;
	}

	/*
	 * Build up a list of acceptable tokens.
	 *
	 * Permitted tokens are
	 * picn=event
	 * nousern
	 * sysn
	 * attrn=val
	 * nouser
	 * sys
	 * attr=val
	 *
	 * Where n is a counter number, and attr is any attribute supported by
	 * the current processor.
	 *
	 * If a token appears without a counter number, it applies to all
	 * counters in the request set.
	 *
	 * The number of tokens is:
	 *
	 * picn: ncounters
	 * generic flags: 2 * ncounters (nouser, sys)
	 * attrs: nattrs * ncounters
	 * attrs with no picnum: nattrs
	 * generic flags with no picnum: 2 (nouser, sys)
	 * NULL token to signify end of list to getsubopt(3C).
	 *
	 * Matching each token's index in the token table is a function which
	 * process that token; these are in tok_funcs.
	 */

	/*
	 * Count the number of valid attributes.
	 * Set up the attrlist array to point to the attributes in attrlistp.
	 */
	nattrs = cpc_count_attrs(cpc);
	attrlist = (char **)emalloc(nattrs * sizeof (char *));

	i = 0;
	cpc_walk_attrs(cpc, &i, attr_walker);

	ntoks = ncounters + (2 * ncounters) + (nattrs * ncounters) + nattrs + 3;
	toks = (char **)emalloc(ntoks * sizeof (char *));
	tok_info = (tok_info_t *)emalloc(ntoks * sizeof (tok_info_t));

	tok_funcs = (int (**)(int, char *))emalloc(ntoks *
	    sizeof (int (*)(char *)));

	for (i = 0; i < ntoks; i++) {
		toks[i] = NULL;
		tok_funcs[i] = NULL;
	}

	x = 0;
	for (i = 0; i < ncounters; i++) {
		toks[x] = (char *)emalloc(TOK_SIZE);
		(void) snprintf(toks[x], TOK_SIZE, "pic%d", i);
		tok_info[x].name = "pic";
		tok_info[i].picnum = i;
		tok_funcs[x] = pic;
		x++;
	}

	for (i = 0; i < ncounters; i++) {
		toks[x] = (char *)emalloc(TOK_SIZE);
		(void) snprintf(toks[x], TOK_SIZE, "nouser%d", i);
		tok_info[x].name = "nouser";
		tok_info[x].picnum = i;
		tok_funcs[x] = flag;
		x++;
	}

	for (i = 0; i < ncounters; i++) {
		toks[x] = (char *)emalloc(TOK_SIZE);
		(void) snprintf(toks[x], TOK_SIZE, "sys%d", i);
		tok_info[x].name = "sys";
		tok_info[x].picnum = i;
		tok_funcs[x] = flag;
		x++;
	}
	for (j = 0; j < nattrs; j++) {
		toklen = strlen(attrlist[j]) + 3;
		for (i = 0; i < ncounters; i++) {
			toks[x] = (char *)emalloc(toklen);
			(void) snprintf(toks[x], toklen, "%s%d", attrlist[j],
			    i);
			tok_info[x].name = attrlist[j];
			tok_info[x].picnum = i;
			tok_funcs[x] = doattr;
			x++;
		}

		/*
		 * Now create a token for this attribute with no picnum; if used
		 * it will be applied to all reqs.
		 */
		toks[x] = (char *)emalloc(toklen);
		(void) snprintf(toks[x], toklen, "%s", attrlist[j]);
		tok_info[x].name = attrlist[j];
		tok_info[x].picnum = -1;
		tok_funcs[x] = doattr;
		x++;
	}

	toks[x] = "nouser";
	tok_info[x].name = "nouser";
	tok_info[x].picnum = -1;
	tok_funcs[x] = flag;
	x++;

	toks[x] = "sys";
	tok_info[x].name = "sys";
	tok_info[x].picnum = -1;
	tok_funcs[x] = flag;
	x++;

	toks[x] = NULL;

	opts = strdupa(spec);
	while (*opts != '\0') {
		int idx = getsubopt(&opts, toks, &val);

		if (idx == -1) {
			if (find_event(val) == 0) {
				strtoset_err(gettext("bad token '%s'\n"), val);
				goto inval;
			} else
				continue;
		}

		if (tok_funcs[idx](idx, val) == -1)
			goto inval;
	}

	/*
	 * The string has been processed. Now count how many PICs were used,
	 * create a request set, and specify each request properly.
	 */

	if ((set = cpc_set_create(cpc)) == NULL) {
		strtoset_err(gettext("no memory available\n"));
		exit(0);
	}

	for (i = 0; i < ncounters; i++) {
		if (reqs[i].cr_event[0] == '\0')
			continue;

		/*
		 * If the caller wishes to measure events on the physical CPU,
		 * we need to add SMT attributes to each request.
		 */
		if (smt)
			smt_special(i);

		req_attrs = (cpc_attr_t *)emalloc(reqs[i].cr_nattrs *
		    sizeof (cpc_attr_t));

		j = 0;
		for (tmp = attrs[i]; tmp != NULL; tmp = tmp->next) {
			req_attrs[j].ca_name = tmp->name;
			req_attrs[j].ca_val = tmp->val;
			j++;
		}

		if (cpc_set_add_request(cpc, set, reqs[i].cr_event, 0,
		    reqs[i].cr_flags, reqs[i].cr_nattrs, req_attrs) == -1) {
			free(req_attrs);
			(void) cpc_set_destroy(cpc, set);
			strtoset_err(
			    gettext("cpc_set_add_request() failed: %s\n"),
			    strerror(errno));
			goto inval;
		}

		free(req_attrs);
	}

	strtoset_cleanup();

	return (set);

inval:
	strtoset_cleanup();
	errno = EINVAL;
	return (NULL);
}

static void
strtoset_cleanup(void)
{
	int		i;
	tmp_attr_t	*tmp, *p;

	for (i = 0; i < nattrs; i++)
		free(attrlist[i]);
	free(attrlist);

	for (i = 0; i < ncounters; i++) {
		for (tmp = attrs[i]; tmp != NULL; tmp = p) {
			p = tmp->next;
			free(tmp);
		}
	}
	free(attrs);

	for (i = 0; i < ntoks - 3; i++)
		/*
		 * We free all but the last three tokens: "nouser", "sys", NULL
		 */
		free(toks[i]);
	free(toks);
	free(reqs);
	free(tok_info);
	free(tok_funcs);
}

/*
 * The following is called to modify requests so that they count events on
 * behalf of a physical processor, instead of a logical processor. It duplicates
 * the request flags for the sibling processor (i.e. if the request counts user
 * events, add an attribute to count user events on the sibling processor also).
 */
static void
smt_special(int picnum)
{
	tmp_attr_t *attr;

	if (reqs[picnum].cr_flags & CPC_COUNT_USER) {
		attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
		attr->name = "count_sibling_usr";
		attr->val = 1;
		attr->next = attrs[picnum];
		attrs[picnum] = attr;
		reqs[picnum].cr_nattrs++;
	}

	if (reqs[picnum].cr_flags & CPC_COUNT_SYSTEM) {
		attr = (tmp_attr_t *)emalloc(sizeof (tmp_attr_t));
		attr->name = "count_sibling_sys";
		attr->val = 1;
		attr->next = attrs[picnum];
		attrs[picnum] = attr;
		reqs[picnum].cr_nattrs++;
	}
}

/*
 * If we ever fail to get memory, we print an error message and exit.
 */
static void *
emalloc(size_t n)
{
	void *p = malloc(n);

	if (p == NULL) {
		strtoset_err(gettext("no memory available\n"));
		exit(0);
	}

	return (p);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <libcpc.h>

#include "cpucmds.h"

static hrtime_t timebase;

void
zerotime(void)
{
	timebase = gethrtime();
}

#define	NSECPERSEC	1000000000.0

float
mstimestamp(hrtime_t hrt)
{
	if (hrt == 0)
		hrt = gethrtime();
	return ((float)(hrt - timebase) / NSECPERSEC);
}