Loading...
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 | /* * Copyright (c) 2009 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <kern/kalloc.h> #include <kern/kern_types.h> #include <kern/locks.h> #include <kern/misc_protos.h> #include <kern/task.h> #include <kern/thread.h> #include <kern/zalloc.h> #include <machine/machine_cpu.h> #include <pmc/pmc.h> #include <libkern/OSAtomic.h> #if defined(__i386__) || defined(__x86_64__) #include <i386/mp.h> #endif #if CONFIG_COUNTERS /* various debug logging enable */ #undef DEBUG_COUNTERS typedef uint8_t pmc_state_event_t; #define PMC_STATE_EVENT_START 0 #define PMC_STATE_EVENT_STOP 1 #define PMC_STATE_EVENT_FREE 2 #define PMC_STATE_EVENT_INTERRUPT 3 #define PMC_STATE_EVENT_END_OF_INTERRUPT 4 #define PMC_STATE_EVENT_CONTEXT_IN 5 #define PMC_STATE_EVENT_CONTEXT_OUT 6 #define PMC_STATE_EVENT_LOAD_FINISHED 7 #define PMC_STATE_EVENT_STORE_FINISHED 8 /* PMC spin timeouts */ #define PMC_SPIN_THRESHOLD 10 /* Number of spins to allow before checking mach_absolute_time() */ #define PMC_SPIN_TIMEOUT_US 10 /* Time in microseconds before the spin causes an assert */ uint64_t pmc_spin_timeout_count = 0; /* Number of times where a PMC spin loop causes a timeout */ #ifdef DEBUG_COUNTERS # include <pexpert/pexpert.h> # define COUNTER_DEBUG(...) \ do { \ kprintf("[%s:%s][%u] ", __FILE__, __PRETTY_FUNCTION__, cpu_number()); \ kprintf(__VA_ARGS__); \ } while(0) # define PRINT_PERF_MON(x) \ do { \ kprintf("perfmon: %p (obj: %p refCt: %u switchable: %u)\n", \ x, x->object, x->useCount, \ (x->methods.flags & PERFMON_FLAG_SUPPORTS_CONTEXT_SWITCHING) ? \ 1 : 0); \ } while(0) static const char const * pmc_state_state_name(pmc_state_t state) { switch (PMC_STATE_STATE(state)) { case PMC_STATE_STATE_INVALID: return "INVALID"; case PMC_STATE_STATE_STOP: return "STOP"; case PMC_STATE_STATE_CAN_RUN: return "CAN_RUN"; case PMC_STATE_STATE_LOAD: return "LOAD"; case PMC_STATE_STATE_RUN: return "RUN"; case PMC_STATE_STATE_STORE: return "STORE"; case PMC_STATE_STATE_INTERRUPT: return "INTERRUPT"; case PMC_STATE_STATE_DEALLOC: return "DEALLOC"; default: return "UNKNOWN"; } } static const char const * pmc_state_event_name(pmc_state_event_t event) { switch (event) { case PMC_STATE_EVENT_START: return "START"; case PMC_STATE_EVENT_STOP: return "STOP"; case PMC_STATE_EVENT_FREE: return "FREE"; case PMC_STATE_EVENT_INTERRUPT: return "INTERRUPT"; case PMC_STATE_EVENT_END_OF_INTERRUPT: return "END OF INTERRUPT"; case PMC_STATE_EVENT_CONTEXT_IN: return "CONTEXT IN"; case PMC_STATE_EVENT_CONTEXT_OUT: return "CONTEXT OUT"; case PMC_STATE_EVENT_LOAD_FINISHED: return "LOAD_FINISHED"; case PMC_STATE_EVENT_STORE_FINISHED: return "STORE_FINISHED"; default: return "UNKNOWN"; } } # define PMC_STATE_FORMAT "<%s, %u, %s%s%s>" # define PMC_STATE_ARGS(x) pmc_state_state_name(x), PMC_STATE_CONTEXT_COUNT(x), ((PMC_STATE_FLAGS(x) & PMC_STATE_FLAGS_INTERRUPTING) ? "I" : ""), \ ((PMC_STATE_FLAGS(x) & PMC_STATE_FLAGS_STOPPING) ? "S" : ""), ((PMC_STATE_FLAGS(x) & PMC_STATE_FLAGS_DEALLOCING) ? "D" : "") #else # define COUNTER_DEBUG(...) # define PRINT_PERF_MON(x) # define PMC_STATE_FORMAT # define PMC_STATE_ARGS(x) #endif /*!struct * pmc_config is the data behind a pmc_config_t. * @member object A pointer to an instance of IOPerformanceCounterConfiguration * @member method A pointer to a method to call to handle PMI. * @member interrupt_after_value Cause a PMI after the counter counts this many * events. * @member refCon Passed to the @method method as the refCon argument. */ struct pmc_config { pmc_config_object_t object; volatile pmc_interrupt_method_t method; uint64_t interrupt_after_value; void *refCon; }; /* * Allocation Zones * * Two allocation zones - Perf zone small and Perf zone big. * Each zone has associated maximums, defined below. * The small zone is the max of the smallest allocation objects (all sizes on * K64): * perf_monitor_t - 48 bytes * perf_monitor_methods_t - 28 bytes * pmc_reservation_t - 48 bytes * pmc_config_t - 32 bytes * perf_small_zone unit size is (on K64) 48 bytes * perf_small_zone max count must be max number of perf monitors, plus (max * number of reservations * 2). The "*2" is because each reservation has a * pmc_config_t within. * * Big zone is max of the larger allocation units * pmc_t - 144 bytes * pmc_methods_t - 116 bytes * perf_big_zone unit size is (on K64) 144 bytes * perf_big_zone max count is the max number of PMCs we support. */ static zone_t perf_small_zone = NULL; #define MAX_PERF_SMALLS (256 + 8196 + 8196) #define PERF_SMALL_UNIT_SZ (MAX(MAX(sizeof(struct perf_monitor), \ sizeof(struct pmc_reservation)), sizeof(struct pmc_config))) static zone_t perf_big_zone = NULL; #define MAX_PERF_BIGS (1024) #define PERF_BIG_UNIT_SZ (sizeof(struct pmc)) /* * Locks and Lock groups */ static lck_grp_t *pmc_lock_grp = LCK_GRP_NULL; static lck_grp_attr_t *pmc_lock_grp_attr; static lck_attr_t *pmc_lock_attr; /* PMC tracking queue locks */ static lck_mtx_t cpu_monitor_queue_mutex; /* protects per-cpu queues at initialisation time */ static lck_spin_t perf_monitor_queue_spin; /* protects adding and removing from queue */ static lck_spin_t perf_counters_queue_spin; /* protects adding and removing from queue */ /* Reservation tracking queues lock */ static lck_spin_t reservations_spin; /* * Tracking queues * * Keeps track of registered perf monitors and perf counters */ static queue_head_t **cpu_monitor_queues = NULL; static queue_head_t *perf_monitors_queue = NULL; static volatile uint32_t perf_monitors_count = 0U; static queue_head_t *perf_counters_queue = NULL; static volatile uint32_t perf_counters_count = 0U; /* * Reservation queues * * Keeps track of all system, task, and thread-level reservations (both active and * inactive). * * We track them all here (rather than in their respective task or thread only) * so that we can inspect our tracking data directly (rather than peeking at * every task and thread) to determine if/when a new reservation would * constitute a conflict. */ static queue_head_t *system_reservations = NULL; static volatile uint32_t system_reservation_count = 0U; static queue_head_t *task_reservations = NULL; static volatile uint32_t task_reservation_count = 0U; static queue_head_t *thread_reservations = NULL; static volatile uint32_t thread_reservation_count = 0U; #if XNU_KERNEL_PRIVATE /* * init_pmc_locks creates and initializes all the locks and lock groups and lock * attributes required for the pmc sub-system. */ static void init_pmc_locks(void) { pmc_lock_attr = lck_attr_alloc_init(); assert(pmc_lock_attr); pmc_lock_grp_attr = lck_grp_attr_alloc_init(); assert(pmc_lock_grp_attr); pmc_lock_grp = lck_grp_alloc_init("pmc", pmc_lock_grp_attr); assert(pmc_lock_grp); lck_spin_init(&perf_monitor_queue_spin, pmc_lock_grp, pmc_lock_attr); lck_spin_init(&perf_counters_queue_spin, pmc_lock_grp, pmc_lock_attr); lck_spin_init(&reservations_spin, pmc_lock_grp, pmc_lock_attr); lck_mtx_init(&cpu_monitor_queue_mutex, pmc_lock_grp, pmc_lock_attr); } /* * init_pmc_zones initializes the allocation zones used by the pmc subsystem */ static void init_pmc_zones(void) { perf_small_zone = zinit(PERF_SMALL_UNIT_SZ, MAX_PERF_SMALLS * PERF_SMALL_UNIT_SZ, MAX_PERF_SMALLS, "pmc.small zone"); assert(perf_small_zone); perf_big_zone = zinit(PERF_BIG_UNIT_SZ, MAX_PERF_BIGS * PERF_BIG_UNIT_SZ, MAX_PERF_BIGS, "pmc.big zone"); assert(perf_big_zone); } /* * init_pmc_queues allocates and initializes the tracking queues for * registering and reserving individual pmcs and perf monitors. */ static void init_pmc_queues(void) { perf_monitors_queue = (queue_head_t*)kalloc(sizeof(queue_head_t)); assert(perf_monitors_queue); queue_init(perf_monitors_queue); perf_counters_queue = (queue_head_t*)kalloc(sizeof(queue_head_t)); assert(perf_counters_queue); queue_init(perf_counters_queue); system_reservations = (queue_head_t*)kalloc(sizeof(queue_t)); assert(system_reservations); queue_init(system_reservations); task_reservations = (queue_head_t*)kalloc(sizeof(queue_head_t)); assert(task_reservations); queue_init(task_reservations); thread_reservations = (queue_head_t*)kalloc(sizeof(queue_head_t)); assert(thread_reservations); queue_init(thread_reservations); } /* * pmc_bootstrap brings up all the necessary infrastructure required to use the * pmc sub-system. */ __private_extern__ void pmc_bootstrap(void) { /* build our alloc zones */ init_pmc_zones(); /* build the locks */ init_pmc_locks(); /* build our tracking queues */ init_pmc_queues(); } #endif /* XNU_KERNEL_PRIVATE */ /* * Perf Monitor Internals */ static perf_monitor_t perf_monitor_alloc(void) { /* perf monitors come from the perf small zone */ return (perf_monitor_t)zalloc(perf_small_zone); } static void perf_monitor_free(void *pm) { zfree(perf_small_zone, pm); } static void perf_monitor_init(perf_monitor_t pm, int cpu) { assert(pm); pm->object = NULL; bzero(&(pm->methods), sizeof(perf_monitor_methods_t)); pm->useCount = 1; /* initial retain count of 1, for caller */ pm->reservedCounters = 0; pm->cpu = cpu; pm->link.next = pm->link.prev = (queue_entry_t)NULL; pm->cpu_link.next = pm->cpu_link.prev = (queue_entry_t)NULL; } /* * perf_monitor_dequeue removes the given perf_monitor_t from the * perf_monitor_queue, thereby unregistering it with the system. */ static void perf_monitor_dequeue(perf_monitor_t pm) { lck_spin_lock(&perf_monitor_queue_spin); if (pm->methods.flags & PERFMON_FLAG_REQUIRES_IDLE_NOTIFICATIONS) { /* If this flag is set, the monitor is already validated to be * accessible from a single cpu only. */ queue_remove(cpu_monitor_queues[pm->cpu], pm, perf_monitor_t, cpu_link); } /* * remove the @pm object from the @perf_monitor_queue queue (it is of type * <perf_monitor_t> and has a field called @link that is the queue_link_t */ queue_remove(perf_monitors_queue, pm, perf_monitor_t, link); perf_monitors_count--; lck_spin_unlock(&perf_monitor_queue_spin); } /* * perf_monitor_enqueue adds the given perf_monitor_t to the perf_monitor_queue, * thereby registering it for use with the system. */ static void perf_monitor_enqueue(perf_monitor_t pm) { lck_mtx_lock(&cpu_monitor_queue_mutex); lck_spin_lock(&perf_monitor_queue_spin); if (pm->cpu >= 0) { /* Deferred initialisation; saves memory and permits ml_get_max_cpus() * to block until cpu initialisation is complete. */ if (!cpu_monitor_queues) { uint32_t max_cpus; queue_head_t **queues; uint32_t i; lck_spin_unlock(&perf_monitor_queue_spin); max_cpus = ml_get_max_cpus(); queues = (queue_head_t**)kalloc(sizeof(queue_head_t*) * max_cpus); assert(queues); for (i = 0; i < max_cpus; i++) { queue_head_t *queue = (queue_head_t*)kalloc(sizeof(queue_head_t)); assert(queue); queue_init(queue); queues[i] = queue; } lck_spin_lock(&perf_monitor_queue_spin); cpu_monitor_queues = queues; } queue_enter(cpu_monitor_queues[pm->cpu], pm, perf_monitor_t, cpu_link); } queue_enter(perf_monitors_queue, pm, perf_monitor_t, link); perf_monitors_count++; lck_spin_unlock(&perf_monitor_queue_spin); lck_mtx_unlock(&cpu_monitor_queue_mutex); } /* * perf_monitor_reference increments the reference count for the given * perf_monitor_t. */ static void perf_monitor_reference(perf_monitor_t pm) { assert(pm); OSIncrementAtomic(&(pm->useCount)); } /* * perf_monitor_deallocate decrements the reference count for the given * perf_monitor_t. If the reference count hits 0, the object is released back * to the perf_small_zone via a call to perf_monitor_free(). */ static void perf_monitor_deallocate(perf_monitor_t pm) { assert(pm); /* If we just removed the last reference count */ if(1 == OSDecrementAtomic(&(pm->useCount))) { /* Free the object */ perf_monitor_free(pm); } } /* * perf_monitor_find attempts to find a perf_monitor_t that corresponds to the * given C++ object pointer that was used when registering with the subsystem. * * If found, the method returns the perf_monitor_t with an extra reference * placed on the object (or NULL if not * found). * * NOTE: Caller must use perf_monitor_deallocate to remove the extra reference after * calling perf_monitor_find. */ static perf_monitor_t perf_monitor_find(perf_monitor_object_t monitor) { assert(monitor); perf_monitor_t element = NULL; perf_monitor_t found = NULL; lck_spin_lock(&perf_monitor_queue_spin); queue_iterate(perf_monitors_queue, element, perf_monitor_t, link) { if(element->object == monitor) { perf_monitor_reference(element); found = element; break; } } lck_spin_unlock(&perf_monitor_queue_spin); return found; } /* * perf_monitor_add_pmc adds a newly registered PMC to the perf monitor it is * associated with. */ static void perf_monitor_add_pmc(perf_monitor_t pm, pmc_t pmc __unused) { assert(pm); assert(pmc); /* Today, we merely add a reference count now that a new pmc is attached */ perf_monitor_reference(pm); } /* * perf_monitor_remove_pmc removes a newly *un*registered PMC from the perf * monitor it is associated with. */ static void perf_monitor_remove_pmc(perf_monitor_t pm, pmc_t pmc __unused) { assert(pm); assert(pmc); /* Today, we merely remove a reference count now that the pmc is detached */ perf_monitor_deallocate(pm); } /* * Perf Counter internals */ static pmc_t pmc_alloc(void) { return (pmc_t)zalloc(perf_big_zone); } static void pmc_free(void *pmc) { zfree(perf_big_zone, pmc); } /* * pmc_init initializes a newly allocated pmc_t */ static void pmc_init(pmc_t pmc) { assert(pmc); pmc->object = NULL; pmc->monitor = NULL; bzero(&pmc->methods, sizeof(pmc_methods_t)); /* One reference for the caller */ pmc->useCount = 1; } /* * pmc_reference increments the reference count of the given pmc_t */ static void pmc_reference(pmc_t pmc) { assert(pmc); OSIncrementAtomic(&(pmc->useCount)); } /* * pmc_deallocate decrements the reference count of the given pmc_t. If the * reference count hits zero, the given pmc_t is deallocated and released back * to the allocation zone. */ static void pmc_deallocate(pmc_t pmc) { assert(pmc); /* If we just removed the last reference count */ if(1 == OSDecrementAtomic(&(pmc->useCount))) { /* Free the pmc */ pmc_free(pmc); } } /* * pmc_dequeue removes the given, newly *un*registered pmc from the * perf_counters_queue. */ static void pmc_dequeue(pmc_t pmc) { lck_spin_lock(&perf_counters_queue_spin); queue_remove(perf_counters_queue, pmc, pmc_t, link); perf_counters_count--; lck_spin_unlock(&perf_counters_queue_spin); } /* * pmc_enqueue adds the given, newly registered pmc to the perf_counters_queue */ static void pmc_enqueue(pmc_t pmc) { lck_spin_lock(&perf_counters_queue_spin); queue_enter(perf_counters_queue, pmc, pmc_t, link); perf_counters_count++; lck_spin_unlock(&perf_counters_queue_spin); } /* * pmc_find attempts to locate a pmc_t that was registered with the given * pmc_object_t pointer. If found, it returns the pmc_t with an extra reference * which must be dropped by the caller by calling pmc_deallocate(). */ static pmc_t pmc_find(pmc_object_t object) { assert(object); lck_spin_lock(&perf_counters_queue_spin); pmc_t element = NULL; pmc_t found = NULL; queue_iterate(perf_counters_queue, element, pmc_t, link) { if(element->object == object) { pmc_reference(element); found = element; break; } } lck_spin_unlock(&perf_counters_queue_spin); return found; } /* * Config internals */ /* Allocate a pmc_config_t */ static pmc_config_t pmc_config_alloc(pmc_t pmc __unused) { return (pmc_config_t)zalloc(perf_small_zone); } /* Free a pmc_config_t, and underlying pmc_config_object_t (if needed) */ static void pmc_config_free(pmc_t pmc, pmc_config_t config) { assert(pmc); assert(config); if(config->object) { pmc->methods.free_config(pmc->object, config->object); config->object = NULL; } zfree(perf_small_zone, config); } static kern_return_t pmc_open(pmc_t pmc) { assert(pmc); assert(pmc->object); assert(pmc->open_object); return pmc->methods.open(pmc->object, pmc->open_object); } static kern_return_t pmc_close(pmc_t pmc) { assert(pmc); assert(pmc->object); assert(pmc->open_object); return pmc->methods.close(pmc->object, pmc->open_object); } /* * Reservation Internals */ static kern_return_t pmc_internal_reservation_set_pmc(pmc_reservation_t resv, pmc_t pmc); static void pmc_internal_reservation_store(pmc_reservation_t reservation); static void pmc_internal_reservation_load(pmc_reservation_t reservation); static pmc_reservation_t reservation_alloc(void) { /* pmc reservations come from the perf small zone */ return (pmc_reservation_t)zalloc(perf_small_zone); } /* * reservation_free deallocates and releases all resources associated with the * given pmc_reservation_t. This includes freeing the config used to create the * reservation, decrementing the reference count for the pmc used to create the * reservation, and deallocating the reservation's memory. */ static void reservation_free(pmc_reservation_t resv) { /* Free config */ if(resv->config) { assert(resv->pmc); pmc_free_config(resv->pmc, resv->config); resv->config = NULL; } /* release PMC */ (void)pmc_internal_reservation_set_pmc(resv, NULL); /* Free reservation */ zfree(perf_small_zone, resv); } /* * reservation_init initializes a newly created reservation. */ static void reservation_init(pmc_reservation_t resv) { assert(resv); resv->pmc = NULL; resv->config = NULL; resv->value = 0ULL; resv->flags = 0U; resv->state = PMC_STATE(PMC_STATE_STATE_STOP, 0, 0); resv->active_last_context_in = 0U; /* * Since this member is a union, we only need to set either the task * or thread to NULL. */ resv->task = TASK_NULL; } /* * pmc_internal_reservation_set_pmc sets the pmc associated with the reservation object. If * there was one set already, it is deallocated (reference is dropped) before * the new one is set. This methods increases the reference count of the given * pmc_t. * * NOTE: It is okay to pass NULL as the pmc_t - this will have the effect of * dropping the reference on any previously set pmc, and setting the reservation * to having no pmc set. */ static kern_return_t pmc_internal_reservation_set_pmc(pmc_reservation_t resv, pmc_t pmc) { assert(resv); if(resv->pmc) { (void)pmc_close(resv->pmc); pmc_deallocate(resv->pmc); resv->pmc = NULL; } resv->pmc = pmc; if(resv->pmc) { pmc_reference(resv->pmc); if(KERN_SUCCESS != pmc_open(resv->pmc)) { pmc_deallocate(resv->pmc); resv->pmc = NULL; return KERN_FAILURE; } } return KERN_SUCCESS; } /* * Used to place reservation into one of the system, task, and thread queues * Assumes the queue's spin lock is already held. */ static void pmc_internal_reservation_enqueue(queue_t queue, pmc_reservation_t resv) { assert(queue); assert(resv); queue_enter(queue, resv, pmc_reservation_t, link); } static void pmc_internal_reservation_dequeue(queue_t queue, pmc_reservation_t resv) { assert(queue); assert(resv); queue_remove(queue, resv, pmc_reservation_t, link); } /* Returns TRUE if the reservation applies to the current execution context */ static boolean_t pmc_internal_reservation_matches_context(pmc_reservation_t resv) { boolean_t ret = FALSE; assert(resv); if(PMC_FLAG_IS_SYSTEM_SCOPE(resv->flags)) { ret = TRUE; } else if(PMC_FLAG_IS_TASK_SCOPE(resv->flags)) { if(current_task() == resv->task) { ret = TRUE; } } else if(PMC_FLAG_IS_THREAD_SCOPE(resv->flags)) { if(current_thread() == resv->thread) { ret = TRUE; } } return ret; } /* * pmc_accessible_core_count returns the number of logical cores that can access * a given @pmc. 0 means every core in the system. */ static uint32_t pmc_accessible_core_count(pmc_t pmc) { assert(pmc); uint32_t *cores = NULL; size_t coreCt = 0UL; if(KERN_SUCCESS != pmc->methods.accessible_cores(pmc->object, &cores, &coreCt)) { coreCt = 0U; } return (uint32_t)coreCt; } /* spin lock for the queue must already be held */ /* * This method will inspect the task/thread of the reservation to see if it * matches the new incoming one (for thread/task reservations only). Will only * return TRUE if the task/thread matches. */ static boolean_t pmc_internal_reservation_queue_contains_pmc(queue_t queue, pmc_reservation_t resv) { assert(queue); assert(resv); boolean_t ret = FALSE; pmc_reservation_t tmp = NULL; queue_iterate(queue, tmp, pmc_reservation_t, link) { if(tmp->pmc == resv->pmc) { /* PMC matches - make sure scope matches first */ switch(PMC_FLAG_SCOPE(tmp->flags)) { case PMC_FLAG_SCOPE_SYSTEM: /* * Found a reservation in system queue with same pmc - always a * conflict. */ ret = TRUE; break; case PMC_FLAG_SCOPE_THREAD: /* * Found one in thread queue with the same PMC as the * argument. Only a conflict if argument scope isn't * thread or system, or the threads match. */ ret = (PMC_FLAG_SCOPE(resv->flags) != PMC_FLAG_SCOPE_THREAD) || (tmp->thread == resv->thread); if(!ret) { /* * so far, no conflict - check that the pmc that is * being reserved isn't accessible from more than * one core, if it is, we need to say it's already * taken. */ if(1 != pmc_accessible_core_count(tmp->pmc)) { ret = TRUE; } } break; case PMC_FLAG_SCOPE_TASK: /* * Follow similar semantics for task scope. */ ret = (PMC_FLAG_SCOPE(resv->flags) != PMC_FLAG_SCOPE_TASK) || (tmp->task == resv->task); if(!ret) { /* * so far, no conflict - check that the pmc that is * being reserved isn't accessible from more than * one core, if it is, we need to say it's already * taken. */ if(1 != pmc_accessible_core_count(tmp->pmc)) { ret = TRUE; } } break; } if(ret) break; } } return ret; } /* * pmc_internal_reservation_validate_for_pmc returns TRUE if the given reservation can be * added to its target queue without creating conflicts (target queue is * determined by the reservation's scope flags). Further, this method returns * FALSE if any level contains a reservation for a PMC that can be accessed from * more than just 1 core, and the given reservation also wants the same PMC. */ static boolean_t pmc_internal_reservation_validate_for_pmc(pmc_reservation_t resv) { assert(resv); boolean_t ret = TRUE; if(pmc_internal_reservation_queue_contains_pmc(system_reservations, resv) || pmc_internal_reservation_queue_contains_pmc(task_reservations, resv) || pmc_internal_reservation_queue_contains_pmc(thread_reservations, resv)) { ret = FALSE; } return ret; } static void pmc_internal_update_thread_flag(thread_t thread, boolean_t newFlag) { assert(thread); /* See if this thread needs it's PMC flag set */ pmc_reservation_t tmp = NULL; if(!newFlag) { /* * If the parent task just dropped its reservation, iterate the thread * reservations to see if we need to keep the pmc flag set for the given * thread or not. */ lck_spin_lock(&reservations_spin); queue_iterate(thread_reservations, tmp, pmc_reservation_t, link) { if(tmp->thread == thread) { newFlag = TRUE; break; } } lck_spin_unlock(&reservations_spin); } if(newFlag) { OSBitOrAtomic(THREAD_PMC_FLAG, &thread->t_chud); } else { OSBitAndAtomic(~(THREAD_PMC_FLAG), &thread->t_chud); } } /* * This operation is (worst case) O(N*M) where N is number of threads in the * given task, and M is the number of thread reservations in our system. */ static void pmc_internal_update_task_flag(task_t task, boolean_t newFlag) { assert(task); thread_t thread = NULL; if(newFlag) { OSBitOrAtomic(TASK_PMC_FLAG, &task->t_chud); } else { OSBitAndAtomic(~(TASK_PMC_FLAG), &task->t_chud); } task_lock(task); queue_iterate(&task->threads, thread, thread_t, task_threads) { /* propagate the task's mask down to each thread */ pmc_internal_update_thread_flag(thread, newFlag); } task_unlock(task); } /* * pmc_internal_reservation_add adds a reservation to the global tracking queues after * ensuring there are no reservation conflicts. To do this, it takes all the * spin locks for all the queue (to ensure no other core goes and adds a * reservation for the same pmc to a queue that has already been checked). */ static boolean_t pmc_internal_reservation_add(pmc_reservation_t resv) { assert(resv); boolean_t ret = FALSE; /* always lock all three in the same order */ lck_spin_lock(&reservations_spin); /* Check if the reservation can be added without conflicts */ if(pmc_internal_reservation_validate_for_pmc(resv)) { /* add reservation to appropriate scope */ switch(PMC_FLAG_SCOPE(resv->flags)) { case PMC_FLAG_SCOPE_SYSTEM: /* Simply add it to the system queue */ pmc_internal_reservation_enqueue(system_reservations, resv); system_reservation_count++; lck_spin_unlock(&reservations_spin); break; case PMC_FLAG_SCOPE_TASK: assert(resv->task); /* Not only do we enqueue it in our local queue for tracking */ pmc_internal_reservation_enqueue(task_reservations, resv); task_reservation_count++; lck_spin_unlock(&reservations_spin); /* update the task mask, and propagate it to existing threads */ pmc_internal_update_task_flag(resv->task, TRUE); break; /* Thread-switched counter */ case PMC_FLAG_SCOPE_THREAD: assert(resv->thread); /* * Works the same as a task-switched counter, only at * thread-scope */ pmc_internal_reservation_enqueue(thread_reservations, resv); thread_reservation_count++; lck_spin_unlock(&reservations_spin); pmc_internal_update_thread_flag(resv->thread, TRUE); break; } ret = TRUE; } else { lck_spin_unlock(&reservations_spin); } return ret; } static void pmc_internal_reservation_broadcast(pmc_reservation_t reservation, void (*action_func)(void *)) { uint32_t * cores; size_t core_cnt; /* Get the list of accessible cores */ if (KERN_SUCCESS == pmc_get_accessible_core_list(reservation->pmc, &cores, &core_cnt)) { boolean_t intrs_enabled = ml_set_interrupts_enabled(FALSE); /* Fast case: the PMC is only accessible from one core and we happen to be on it */ if (core_cnt == 1 && cores[0] == (uint32_t)cpu_number()) { action_func(reservation); } else { /* Call action_func on every accessible core */ #if defined(__i386__) || defined(__x86_64__) size_t ii; cpumask_t mask = 0; /* Build a mask for the accessible cores */ if (core_cnt > 0) { for (ii = 0; ii < core_cnt; ii++) { mask |= cpu_to_cpumask(cores[ii]); } } else { /* core_cnt = 0 really means all cpus */ mask = CPUMASK_ALL; } mp_cpus_call(mask, ASYNC, action_func, reservation); #else #error pmc_reservation_interrupt needs an inter-processor method invocation mechanism for this architecture #endif } ml_set_interrupts_enabled(intrs_enabled); } } /* * pmc_internal_reservation_remove removes the given reservation from the appropriate * reservation queue according to its scope. * * NOTE: The scope flag must have been set for this method to function. */ static void pmc_internal_reservation_remove(pmc_reservation_t resv) { assert(resv); /* * Due to the way the macros are written, we can't just blindly queue-remove * the reservation without knowing which queue it's in. We figure this out * using the reservation's scope flags. */ /* Lock the global spin lock */ lck_spin_lock(&reservations_spin); switch(PMC_FLAG_SCOPE(resv->flags)) { case PMC_FLAG_SCOPE_SYSTEM: pmc_internal_reservation_dequeue(system_reservations, resv); system_reservation_count--; lck_spin_unlock(&reservations_spin); break; case PMC_FLAG_SCOPE_TASK: /* remove from the global queue */ pmc_internal_reservation_dequeue(task_reservations, resv); task_reservation_count--; /* unlock the global */ lck_spin_unlock(&reservations_spin); /* Recalculate task's counter mask */ pmc_internal_update_task_flag(resv->task, FALSE); break; case PMC_FLAG_SCOPE_THREAD: pmc_internal_reservation_dequeue(thread_reservations, resv); thread_reservation_count--; lck_spin_unlock(&reservations_spin); /* recalculate the thread's counter mask */ pmc_internal_update_thread_flag(resv->thread, FALSE); break; } } /* Reservation State Machine * * The PMC subsystem uses a 3-tuple of state information packed into a 32-bit quantity and a * set of 9 events to provide MP-safe bookkeeping and control flow. The 3-tuple is comprised * of a state, a count of active contexts, and a set of modifier flags. A state machine defines * the possible transitions at each event point given the current 3-tuple. Atomicity is handled * by reading the current 3-tuple, applying the transformations indicated by the state machine * and then attempting to OSCompareAndSwap the transformed value. If the OSCompareAndSwap fails, * the process is repeated until either the OSCompareAndSwap succeeds or not valid transitions are * available. * * The state machine is described using tuple notation for the current state and a related notation * for describing the transformations. For concisness, the flag and state names are abbreviated as * follows: * * states: * S = STOP * CR = CAN_RUN * L = LOAD * R = RUN * ST = STORE * I = INTERRUPT * D = DEALLOC * * flags: * * S = STOPPING * D = DEALLOCING * I = INTERRUPTING * * The tuple notation is formed from the following pattern: * * tuple = < state, active-context-count, flags > * state = S | CR | L | R | ST | I | D * active-context-count = 0 | >0 | 1 | >1 * flags = flags flag | blank * flag = S | D | I * * The transform notation is similar, but only describes the modifications made to the current state. * The notation is formed from the following pattern: * * transform = < state, active-context-count, flags > * state = S | CR | L | R | ST | I | D * active-context-count = + | - | blank * flags = flags flag | flags !flag | blank * flag = S | D | I * * And now for the state machine: * State Start Stop Free Interrupt End Interrupt Context In Context Out Load Finished Store Finished * <CR, 0, > <S, , > <D, , > <L, +, > * <D, 0, > * <D, 1, D> < , -, !D> * <D, >1, D> < , -, > * <I, 0, D> <D, , !D> * <I, 0, S> < , , !S> < , , !SD> <S, , !S> * <I, 0, > < , , S> < , , D> <CR, , > * <L, 1, D> <ST, -, > * <L, 1, ID> <ST, -, > * <L, 1, IS> < , , !SD> <ST, -, > * <L, 1, S> < , , !S> < , , !SD> <ST, -, > * <L, 1, > < , , S> < , , D> < , , IS> < , +, > <R, , > * <L, >1, D> < , -, > <R, -, > * <L, >1, ID> < , -, > <R, -, > * <L, >1, IS> < , , !SD> < , -, > <R, -, > * <L, >1, S> < , , !S> < , , !SD> < , -, > <R, -, > * <L, >1, > < , , S> < , , D> < , , IS> < , +, > < , -, > <R, , > * <R, 1, D> <ST, -, > * <R, 1, ID> <ST, -, > * <R, 1, IS> < , , !SD> <ST, -, > * <R, 1, S> < , , !S> < , , !SD> <ST, -, > * <R, 1, > < , , S> < , , D> < , , IS> < , +, > <ST, -, > * <R, >1, D> < , -, > * <R, >1, ID> < , -, > * <R, >1, IS> < , , !SD> < , -, > * <R, >1, S> < , , !S> < , , !SD> < , -, > * <R, >1, > < , , S> < , , D> < , , IS> < , +, > < , -, > * <S, 0, > <CR, , > <D, , > * <S, 1, ID> <I, -, !I> * <S, 1, IS> < , , !SD> <I, -, !I> * <S, 1, S> < , , !S> <D, , !SD> < , -, !S> * <S, 1, > < , , S> <D, , D> <L, +, > <CR, -, > * <S, >1, ID> < , -, > * <S, >1, IS> < , , !SD> < , -, > * <S, >1, S> < , , !S> <D, , !SD> < , -, > * <S, >1, > < , , S> <D, , D> <L, +, > < , -, > * <ST, 0, D> <D, , !D> * <ST, 0, ID> <I, , !I> * <ST, 0, IS> < , , !SD> <I, , !I> * <ST, 0, S> < , , !S> < , , !SD> <S, , !S> * <ST, 0, > < , , S> < , , D> < , , IS> < , +, > <CR, , > * <ST, >0, D> < , -, > <D, , > * <ST, >0, ID> < , -, > <S, , > * <ST, >0, IS> < , , !SD> < , -, > <S, , > * <ST, >0, S> < , , !S> < , , !SD> < , -, > <S, , > * <ST, >0, > < , , S> < , , D> < , , IS> < , +, > < , -, > <L, , > */ static uint32_t pmc_internal_reservation_next_state(uint32_t current_state, pmc_state_event_t event) { uint32_t new_state = PMC_STATE(PMC_STATE_STATE_INVALID, 0, 0); switch (event) { case PMC_STATE_EVENT_START: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_INTERRUPT, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_RUN, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_STOP, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_STOPPING): new_state = PMC_STATE_MODIFY(current_state, 0, 0, PMC_STATE_FLAGS_STOPPING); break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) == 0) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_CAN_RUN, 0, 0, 0); } break; } break; case PMC_STATE_EVENT_STOP: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_CAN_RUN, 0, 0): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_STOP, 0, 0, 0); break; case PMC_STATE(PMC_STATE_STATE_INTERRUPT, 0, 0): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, 0): case PMC_STATE(PMC_STATE_STATE_RUN, 0, 0): case PMC_STATE(PMC_STATE_STATE_STORE, 0, 0): new_state = PMC_STATE_MODIFY(current_state, 0, PMC_STATE_FLAGS_STOPPING, 0); break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) > 0) { new_state = PMC_STATE_MODIFY(current_state, 0, PMC_STATE_FLAGS_STOPPING, 0); } break; } break; case PMC_STATE_EVENT_FREE: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_CAN_RUN, 0, 0): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_DEALLOC, 0, 0, 0); break; case PMC_STATE(PMC_STATE_STATE_INTERRUPT, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_RUN, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_RUN, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_STOP, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_STOPPING): new_state = PMC_STATE_MODIFY(current_state, 0, PMC_STATE_FLAGS_DEALLOCING, PMC_STATE_FLAGS_STOPPING); break; case PMC_STATE(PMC_STATE_STATE_INTERRUPT, 0, 0): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, 0): case PMC_STATE(PMC_STATE_STATE_RUN, 0, 0): case PMC_STATE(PMC_STATE_STATE_STORE, 0, 0): new_state = PMC_STATE_MODIFY(current_state, 0, PMC_STATE_FLAGS_DEALLOCING, 0); break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, PMC_STATE_FLAGS_STOPPING): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_DEALLOC, 0, PMC_STATE_FLAGS_DEALLOCING, PMC_STATE_FLAGS_STOPPING); break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) > 0) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_DEALLOC, 0, PMC_STATE_FLAGS_DEALLOCING, 0); } else { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_DEALLOC, 0, 0, 0); } break; } break; case PMC_STATE_EVENT_INTERRUPT: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_LOAD, 0, 0): case PMC_STATE(PMC_STATE_STATE_RUN, 0, 0): case PMC_STATE(PMC_STATE_STATE_STORE, 0, 0): new_state = PMC_STATE_MODIFY(current_state, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING, 0); break; } break; case PMC_STATE_EVENT_END_OF_INTERRUPT: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_INTERRUPT, 0, PMC_STATE_FLAGS_DEALLOCING): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_DEALLOC, 0, 0, PMC_STATE_FLAGS_DEALLOCING); break; case PMC_STATE(PMC_STATE_STATE_INTERRUPT, 0, PMC_STATE_FLAGS_STOPPING): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_STOP, 0, 0, PMC_STATE_FLAGS_STOPPING); break; case PMC_STATE(PMC_STATE_STATE_INTERRUPT, 0, 0): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_CAN_RUN, 0, 0, 0); break; } break; case PMC_STATE_EVENT_CONTEXT_IN: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_CAN_RUN, 0, 0): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_LOAD, 1, 0, 0); break; case PMC_STATE(PMC_STATE_STATE_LOAD, 0, 0): case PMC_STATE(PMC_STATE_STATE_RUN, 0, 0): case PMC_STATE(PMC_STATE_STATE_STORE, 0, 0): new_state = PMC_STATE_MODIFY(current_state, 1, 0, 0); break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) > 0) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_LOAD, 1, 0, 0); } break; } break; case PMC_STATE_EVENT_CONTEXT_OUT: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_DEALLOC, 0, PMC_STATE_FLAGS_DEALLOCING): if (PMC_STATE_CONTEXT_COUNT(current_state) > 1) { new_state = PMC_STATE_MODIFY(current_state, -1, 0, PMC_STATE_FLAGS_DEALLOCING); } else { new_state = PMC_STATE_MODIFY(current_state, -1, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) > 1) { new_state = PMC_STATE_MODIFY(current_state, -1, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_RUN, 0, PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_RUN, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_RUN, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_RUN, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_RUN, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) == 1) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_STORE, -1, 0, 0); } else { new_state = PMC_STATE_MODIFY(current_state, -1, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_STOP, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): if (PMC_STATE_CONTEXT_COUNT(current_state) == 1) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_INTERRUPT, -1, 0, PMC_STATE_FLAGS_INTERRUPTING); } else { new_state = PMC_STATE_MODIFY(current_state, -1, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, PMC_STATE_FLAGS_STOPPING): if (PMC_STATE_CONTEXT_COUNT(current_state) == 1) { new_state = PMC_STATE_MODIFY(current_state, -1, 0, PMC_STATE_FLAGS_STOPPING); } else { new_state = PMC_STATE_MODIFY(current_state, -1, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_STOP, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) > 0) { if (PMC_STATE_CONTEXT_COUNT(current_state) == 1) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_CAN_RUN, -1, 0, 0); } else { new_state = PMC_STATE_MODIFY(current_state, -1, 0, 0); } } break; case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) > 0) { new_state = PMC_STATE_MODIFY(current_state, -1, 0, 0); } break; } break; case PMC_STATE_EVENT_LOAD_FINISHED: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): case PMC_STATE(PMC_STATE_STATE_LOAD, 0, PMC_STATE_FLAGS_STOPPING): if (PMC_STATE_CONTEXT_COUNT(current_state) > 1) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_RUN, -1, 0, 0); } else { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_STORE, -1, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_LOAD, 0, 0): new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_RUN, 0, 0, 0); break; } break; case PMC_STATE_EVENT_STORE_FINISHED: switch (current_state & ~(PMC_STATE_CONTEXT_COUNT_MASK)) { case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_DEALLOCING): if (PMC_STATE_CONTEXT_COUNT(current_state) == 0) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_DEALLOC, 0, 0, PMC_STATE_FLAGS_DEALLOCING); } else { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_DEALLOC, 0, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_DEALLOCING): case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_INTERRUPTING | PMC_STATE_FLAGS_STOPPING): if (PMC_STATE_CONTEXT_COUNT(current_state) == 0) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_INTERRUPT, 0, 0, PMC_STATE_FLAGS_INTERRUPTING); } else { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_STOP, 0, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_STORE, 0, PMC_STATE_FLAGS_STOPPING): if (PMC_STATE_CONTEXT_COUNT(current_state) == 0) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_STOP, 0, 0, PMC_STATE_FLAGS_STOPPING); } else { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_STOP, 0, 0, 0); } break; case PMC_STATE(PMC_STATE_STATE_STORE, 0, 0): if (PMC_STATE_CONTEXT_COUNT(current_state) == 0) { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_CAN_RUN, 0, 0, 0); } else { new_state = PMC_STATE_MOVE(current_state, PMC_STATE_STATE_LOAD, 0, 0, 0); } break; } break; } return new_state; } static uint32_t pmc_internal_reservation_move_for_event(pmc_reservation_t reservation, pmc_state_event_t event, pmc_state_t *old_state_out) { pmc_state_t oldState; pmc_state_t newState; assert(reservation); /* Determine what state change, if any, we need to do. Keep trying until either we succeed doing a transition * or the there is no valid move. */ do { oldState = reservation->state; newState = pmc_internal_reservation_next_state(oldState, event); } while (newState != PMC_STATE_INVALID && !OSCompareAndSwap(oldState, newState, &(reservation->state))); if (newState != PMC_STATE_INVALID) { COUNTER_DEBUG("Moved reservation %p from state "PMC_STATE_FORMAT" to state "PMC_STATE_FORMAT" for event %s\n", reservation, PMC_STATE_ARGS(oldState), PMC_STATE_ARGS(newState), pmc_state_event_name(event)); } else { COUNTER_DEBUG("No valid moves for reservation %p in state "PMC_STATE_FORMAT" for event %s\n", reservation, PMC_STATE_ARGS(oldState), pmc_state_event_name(event)); } if (old_state_out != NULL) { *old_state_out = oldState; } return newState; } static void pmc_internal_reservation_context_out(pmc_reservation_t reservation) { assert(reservation); pmc_state_t newState; pmc_state_t oldState; /* Clear that the this reservation was active when this cpu did its last context in */ OSBitAndAtomic(~(1U << cpu_number()), &(reservation->active_last_context_in)); /* Move the state machine */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_CONTEXT_OUT, &oldState))) { return; } /* Do any actions required based on the state change */ if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_STORE && PMC_STATE_STATE(oldState) != PMC_STATE_STATE_STORE) { /* Just moved into STORE, so store the reservation. */ pmc_internal_reservation_store(reservation); } else if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_DEALLOC && PMC_STATE_CONTEXT_COUNT(newState) == 0 && PMC_STATE_FLAGS(newState) == 0) { /* Wakeup any thread blocking for this reservation to hit <DEALLOC, 0, > */ thread_wakeup((event_t)reservation); } } static void pmc_internal_reservation_context_in(pmc_reservation_t reservation) { assert(reservation); pmc_state_t oldState; pmc_state_t newState; /* Move the state machine */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_CONTEXT_IN, &oldState))) { return; } /* Mark that the reservation was active when this cpu did its last context in */ OSBitOrAtomic(1U << cpu_number(), &(reservation->active_last_context_in)); /* Do any actions required based on the state change */ if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_LOAD && PMC_STATE_STATE(oldState) != PMC_STATE_STATE_LOAD) { /* Just moved into LOAD, so load the reservation. */ pmc_internal_reservation_load(reservation); } } static void pmc_internal_reservation_store(pmc_reservation_t reservation) { assert(reservation); assert(PMC_STATE_STATE(reservation->state) == PMC_STATE_STATE_STORE); assert(reservation->pmc); assert(reservation->config); pmc_state_t newState; kern_return_t ret = KERN_SUCCESS; pmc_t store_pmc = reservation->pmc; pmc_object_t store_pmc_obj = store_pmc->object; perf_monitor_t store_pm = store_pmc->monitor; /* * Instruct the Perf Monitor that contains this counter to turn * off the global disable for this counter. */ ret = store_pm->methods.disable_counters(store_pm->object, &store_pmc_obj, 1); if(KERN_SUCCESS != ret) { COUNTER_DEBUG(" [error] disable_counters: 0x%x\n", ret); return; } /* Instruct the counter to disable itself */ ret = store_pmc->methods.disable(store_pmc_obj); if(KERN_SUCCESS != ret) { COUNTER_DEBUG(" [error] disable: 0x%x\n", ret); } /* store the counter value into the reservation's stored count */ ret = store_pmc->methods.get_count(store_pmc_obj, &reservation->value); if(KERN_SUCCESS != ret) { COUNTER_DEBUG(" [error] get_count: 0x%x\n", ret); return; } /* Advance the state machine now that the STORE is finished */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_STORE_FINISHED, NULL))) { return; } /* Do any actions required based on the state change */ if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_LOAD) { /* Just moved into LOAD, so load the reservation. */ pmc_internal_reservation_load(reservation); } else if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_DEALLOC && PMC_STATE_CONTEXT_COUNT(newState) == 0 && PMC_STATE_FLAGS(newState) == 0) { /* Wakeup any thread blocking for this reservation to hit <DEALLOC, 0, > */ thread_wakeup((event_t)reservation); } } static void pmc_internal_reservation_load(pmc_reservation_t reservation) { assert(reservation); assert(PMC_STATE_STATE(reservation->state) == PMC_STATE_STATE_LOAD); pmc_state_t newState; kern_return_t ret = KERN_SUCCESS; assert(reservation->pmc); assert(reservation->config); pmc_t load_pmc = reservation->pmc; pmc_object_t load_pmc_obj = load_pmc->object; perf_monitor_t load_pm = load_pmc->monitor; /* Set the control register up with the stored configuration */ ret = load_pmc->methods.set_config(load_pmc_obj, reservation->config->object); if(KERN_SUCCESS != ret) { COUNTER_DEBUG(" [error] set_config: 0x%x\n", ret); return; } /* load the counter value */ ret = load_pmc->methods.set_count(load_pmc_obj, reservation->value); if(KERN_SUCCESS != ret) { COUNTER_DEBUG(" [error] set_count: 0x%x\n", ret); return; } /* Locally enable the counter */ ret = load_pmc->methods.enable(load_pmc_obj); if(KERN_SUCCESS != ret) { COUNTER_DEBUG(" [error] enable: 0x%x\n", ret); return; } /* * Instruct the Perf Monitor containing the pmc to enable the * counter. */ ret = load_pm->methods.enable_counters(load_pm->object, &load_pmc_obj, 1); if(KERN_SUCCESS != ret) { COUNTER_DEBUG(" [error] enable_counters: 0x%x\n", ret); /* not on the hardware. */ return; } /* Advance the state machine now that the STORE is finished */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_LOAD_FINISHED, NULL))) { return; } /* Do any actions required based on the state change */ if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_STORE) { /* Just moved into STORE, so store the reservation. */ pmc_internal_reservation_store(reservation); } } /* * pmc_accessible_from_core will return TRUE if the given @pmc is directly * (e.g., hardware) readable from the given logical core. * * NOTE: This method is interrupt safe. */ static inline boolean_t pmc_accessible_from_core(pmc_t pmc, uint32_t logicalCore) { boolean_t ret = FALSE; assert(pmc); ret = pmc->methods.accessible_from_core(pmc->object, logicalCore); return ret; } static void pmc_internal_reservation_start_cpu(void * arg) { pmc_reservation_t reservation = (pmc_reservation_t)arg; assert(reservation); if (pmc_internal_reservation_matches_context(reservation)) { /* We are in context, but the reservation may have already had the context_in method run. Attempt * to set this cpu's bit in the active_last_context_in mask. If we set it, call context_in. */ uint32_t oldMask = OSBitOrAtomic(1U << cpu_number(), &(reservation->active_last_context_in)); if ((oldMask & (1U << cpu_number())) == 0) { COUNTER_DEBUG("Starting already in-context reservation %p for cpu %d\n", reservation, cpu_number()); pmc_internal_reservation_context_in(reservation); } } } static void pmc_internal_reservation_stop_cpu(void * arg) { pmc_reservation_t reservation = (pmc_reservation_t)arg; assert(reservation); if (pmc_internal_reservation_matches_context(reservation)) { COUNTER_DEBUG("Stopping in-context reservation %p for cpu %d\n", reservation, cpu_number()); pmc_internal_reservation_context_out(reservation); } } /*!fn * pmc_reservation_interrupt is called when a PMC reservation which was setup * with an interrupt threshold counts the requested number of events. When the * underlying counter hits the threshold, an interrupt is generated, and this * method is called. This method marks the reservation as stopped, and passes * control off to the user-registered callback method, along with the * reservation (so that the user can, for example, write a 0 to the counter, and * restart the reservation). * This method assumes the reservation has a valid pmc_config_t within. * * @param target The pmc_reservation_t that caused the interrupt. * @param refCon User specified reference constant. */ static void pmc_reservation_interrupt(void *target, void *refCon) { pmc_reservation_t reservation = (pmc_reservation_t)target; pmc_state_t newState; uint64_t timeout; uint32_t spins; assert(reservation); /* Move the state machine */ if (PMC_STATE_INVALID == pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_INTERRUPT, NULL)) { return; } /* A valid state move has been made, but won't be picked up until a context switch occurs. To cause matching * contexts that are currently running to update, we do an inter-processor message to run pmc_internal_reservation_stop_cpu * on every cpu that can access the PMC. */ pmc_internal_reservation_broadcast(reservation, pmc_internal_reservation_stop_cpu); /* Spin waiting for the state to turn to INTERRUPT */ nanoseconds_to_absolutetime(PMC_SPIN_TIMEOUT_US * 1000, &timeout); timeout += mach_absolute_time(); spins = 0; while (PMC_STATE_STATE(reservation->state) != PMC_STATE_STATE_INTERRUPT) { /* Assert if this takes longer than PMC_SPIN_TIMEOUT_US */ if (++spins > PMC_SPIN_THRESHOLD) { if (mach_absolute_time() > timeout) { pmc_spin_timeout_count++; assert(0); } } cpu_pause(); } assert(reservation->config); assert(reservation->config->method); /* Call the registered callback handler */ #if DEBUG_COUNTERS uint64_t start = mach_absolute_time(); #endif /* DEBUG */ (void)reservation->config->method(reservation, refCon); #if DEBUG_COUNTERS uint64_t end = mach_absolute_time(); if((end - start) > 5000ULL) { kprintf("%s - user method %p took %llu ns\n", __FUNCTION__, reservation->config->method, (end - start)); } #endif /* Move the state machine */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_END_OF_INTERRUPT, NULL))) { return; } /* Do any post-move actions necessary */ if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_CAN_RUN) { pmc_internal_reservation_broadcast(reservation, pmc_internal_reservation_start_cpu); } else if (PMC_STATE_STATE(newState) == PMC_STATE_STATE_DEALLOC && PMC_STATE_CONTEXT_COUNT(newState) == 0 && PMC_STATE_FLAGS(newState) == 0) { /* Wakeup any thread blocking for this reservation to hit <DEALLOC, 0, > */ thread_wakeup((event_t)reservation); } } /* * Apple-private KPI for Apple kext's (IOProfileFamily) only */ #if 0 #pragma mark - #pragma mark IOProfileFamily private KPI #endif /* * perf_monitor_register registers a new Performance Monitor, and its associated * callback methods. The given perf_monitor_object_t is the first argument to * each callback when they are called. */ kern_return_t perf_monitor_register(perf_monitor_object_t monitor, perf_monitor_methods_t *methods) { int cpu = -1; COUNTER_DEBUG("registering perf monitor %p\n", monitor); if(!monitor || !methods) { return KERN_INVALID_ARGUMENT; } /* Protect against out-of-date driver kexts */ if(MACH_PERFMON_METHODS_VERSION != methods->perf_monitor_methods_version) { return KERN_INVALID_ARGUMENT; } /* If the monitor requires idle notifications, ensure that it is * accessible from a single core only. */ if (methods->flags & PERFMON_FLAG_REQUIRES_IDLE_NOTIFICATIONS) { uint32_t *cores; size_t core_cnt; if (KERN_SUCCESS == methods->accessible_cores(monitor, &cores, &core_cnt)) { /* * Guard against disabled cores - monitors will always match and * attempt registration, irrespective of 'cpus=x' boot-arg. */ if ((core_cnt == 1) && (cores[0] < (uint32_t)ml_get_max_cpus())) { cpu = cores[0]; } else { return KERN_INVALID_ARGUMENT; } } } /* All methods are required */ if(!methods->accessible_cores | !methods->enable_counters || !methods->disable_counters || !methods->on_idle || !methods->on_idle_exit) { return KERN_INVALID_ARGUMENT; } /* prevent dupes. */ perf_monitor_t dupe = perf_monitor_find(monitor); if(dupe) { COUNTER_DEBUG("Duplicate registration for %p\n", monitor); perf_monitor_deallocate(dupe); return KERN_FAILURE; } perf_monitor_t pm = perf_monitor_alloc(); if(!pm) { return KERN_RESOURCE_SHORTAGE; } /* initialize the object */ perf_monitor_init(pm, cpu); /* copy in the registration info */ pm->object = monitor; memcpy(&(pm->methods), methods, sizeof(perf_monitor_methods_t)); /* place it in the tracking queues */ perf_monitor_enqueue(pm); /* debug it */ PRINT_PERF_MON(pm); return KERN_SUCCESS; } /* * perf_monitor_unregister unregisters a previously registered Perf Monitor, * looking it up by reference pointer (the same that was used in * perf_monitor_register()). */ kern_return_t perf_monitor_unregister(perf_monitor_object_t monitor) { kern_return_t ret = KERN_FAILURE; COUNTER_DEBUG("unregistering perf monitor %p\n", monitor); if(!monitor) { return KERN_INVALID_ARGUMENT; } perf_monitor_t pm = perf_monitor_find(monitor); if(pm) { /* Remove it from the queues. */ perf_monitor_dequeue(pm); /* drop extra retain from find */ perf_monitor_deallocate(pm); /* and release the object */ perf_monitor_deallocate(pm); ret = KERN_SUCCESS; } else { COUNTER_DEBUG("could not find a registered pm that matches!\n"); } return ret; } /* * pmc_register registers a new PMC for use with the pmc subsystem. Each PMC is * associated with a Perf Monitor. Perf Monitors are looked up by the reference * pointer that was used to previously register them. * * PMCs are registered with a reference pointer (@pmc_object), and a set of * callback methods. When the given callback methods are called from xnu, the * first argument will always be the reference pointer used to register the PMC. * * NOTE: @monitor must have been successfully registered via * perf_monitor_register before this method will succeed. */ kern_return_t pmc_register(perf_monitor_object_t monitor, pmc_object_t pmc_object, pmc_methods_t *methods, void *object) { COUNTER_DEBUG("%p %p\n", monitor, pmc_object); if(!monitor || !pmc_object || !methods || !object) { return KERN_INVALID_ARGUMENT; } /* Prevent version mismatches */ if(MACH_PMC_METHODS_VERSION != methods->pmc_methods_version) { COUNTER_DEBUG("version mismatch\n"); return KERN_INVALID_ARGUMENT; } /* All methods are required. */ if(!methods->create_config || !methods->free_config || !methods->config_set_value || !methods->config_set_threshold || !methods->config_set_handler || !methods->set_config || !methods->get_monitor || !methods->get_name || !methods->accessible_from_core || !methods->accessible_cores || !methods->get_count || !methods->set_count || !methods->disable || !methods->enable || !methods->open || !methods->close) { return KERN_INVALID_ARGUMENT; } /* make sure this perf monitor object is already registered */ /* * NOTE: this adds a reference to the parent, so we'll have to drop it in * any failure code paths from here on out. */ perf_monitor_t pm = perf_monitor_find(monitor); if(!pm) { COUNTER_DEBUG("Could not find perf monitor for %p\n", monitor); return KERN_INVALID_ARGUMENT; } /* make a new pmc */ pmc_t pmc = pmc_alloc(); if(!pmc) { /* drop the extra reference from perf_monitor_find() */ perf_monitor_deallocate(pm); return KERN_RESOURCE_SHORTAGE; } /* init it */ pmc_init(pmc); pmc->object = pmc_object; pmc->open_object = object; /* copy the callbacks in */ memcpy(&(pmc->methods), methods, sizeof(pmc_methods_t)); pmc->monitor = pm; perf_monitor_add_pmc(pmc->monitor, pmc); /* enqueue it in our tracking queue */ pmc_enqueue(pmc); /* drop extra reference from perf_monitor_find() */ perf_monitor_deallocate(pm); return KERN_SUCCESS; } /* * pmc_unregister unregisters a previously registered PMC, looking it up by * reference point to *both* the Perf Monitor it was created with, and the PMC's * reference pointer itself. */ kern_return_t pmc_unregister(perf_monitor_object_t monitor, pmc_object_t pmc_object) { COUNTER_DEBUG("%p %p\n", monitor, pmc_object); if(!monitor || !pmc_object) { return KERN_INVALID_ARGUMENT; } pmc_t pmc = pmc_find(pmc_object); if(!pmc) { COUNTER_DEBUG("Could not find a matching pmc.\n"); return KERN_FAILURE; } /* remove it from the global queue */ pmc_dequeue(pmc); perf_monitor_remove_pmc(pmc->monitor, pmc); /* remove extra reference count from pmc_find() */ pmc_deallocate(pmc); /* dealloc the pmc */ pmc_deallocate(pmc); return KERN_SUCCESS; } static void perf_monitor_reservation_add(perf_monitor_t monitor) { assert(monitor); OSIncrementAtomic(&(monitor->reservedCounters)); } static void perf_monitor_reservation_remove(perf_monitor_t monitor) { assert(monitor); OSDecrementAtomic(&(monitor->reservedCounters)); } #if 0 #pragma mark - #pragma mark KPI #endif /* * Begin in-kernel and in-kext KPI methods */ /* * pmc_create_config creates a new configuration area from a given @pmc. * * NOTE: This method is not interrupt safe. */ kern_return_t pmc_create_config(pmc_t pmc, pmc_config_t *config) { pmc_config_t tmp = NULL; if(!pmc || !config) { return KERN_INVALID_ARGUMENT; } pmc_reference(pmc); tmp = pmc_config_alloc(pmc); if(tmp) { tmp->object = pmc->methods.create_config(pmc->object); if(!tmp->object) { pmc_config_free(pmc, tmp); tmp = NULL; } else { tmp->interrupt_after_value = 0ULL; tmp->method = NULL; tmp->refCon = NULL; } } pmc_deallocate(pmc); if(!tmp) { return KERN_RESOURCE_SHORTAGE; } *config = tmp; return KERN_SUCCESS; } /* * pmc_free_config frees a configuration area created from a given @pmc * * NOTE: This method is not interrupt safe. */ void pmc_free_config(pmc_t pmc, pmc_config_t config) { assert(pmc); assert(config); pmc_reference(pmc); pmc_config_free(pmc, config); pmc_deallocate(pmc); } /* * pmc_config_set_value sets up configuration area key-value pairs. These pairs * are to be either pre-known, or looked up via CoreProfile.framework. * * NOTE: This method is not interrupt safe. */ kern_return_t pmc_config_set_value(pmc_t pmc, pmc_config_t config, uint8_t id, uint64_t value) { kern_return_t ret = KERN_INVALID_ARGUMENT; if(!pmc || !config) { return ret; } pmc_reference(pmc); ret = pmc->methods.config_set_value(config->object, id, value); pmc_deallocate(pmc); return ret; } /* * pmc_config_set_interrupt_threshold modifies a config object, instructing * the pmc that it should generate a call to the given pmc_interrupt_method_t * after the counter counts @threshold events. * * PMC Threshold handler methods will have the pmc_reservation_t that generated the interrupt * as the first argument when the interrupt handler is invoked, and the given * @refCon (which may be NULL) as the second. * * See pmc_interrupt_method_t. * * NOTE: This method is not interrupt safe. */ kern_return_t pmc_config_set_interrupt_threshold(pmc_t pmc, pmc_config_t config, uint64_t threshold, pmc_interrupt_method_t method, void *refCon) { kern_return_t ret = KERN_INVALID_ARGUMENT; if(!config || !pmc) { return ret; } assert(config); assert(pmc); pmc_reference(pmc); do { /* * We have a minor annoyance to side-step here. The driver layer expects * the config to never change once a reservation has been taken out with * it. However, in order to have the PMI method have the reservation as * the first argument (in order to allow the user-method to, for * example, write a 0 to it, and restart it), we need to create the * pmc_reservation_t before setting it up in the config object. * We overcome this by caching the method in the pmc_config_t stand-in, * and mutating the pmc_config_object_t just before returning a * reservation (in pmc_reserve() and friends, below). */ /* might as well stash this away too. */ config->interrupt_after_value = threshold; config->method = method; config->refCon = refCon; ret = KERN_SUCCESS; }while(0); pmc_deallocate(pmc); return ret; } /* * pmc_get_pmc_list returns an allocated list of pmc_t's, as well as the number * of pmc_t's returned. Callers should free this list with a call to * pmc_free_pmc_list(). * * NOTE: This method is not interrupt safe. */ kern_return_t pmc_get_pmc_list(pmc_t **pmcs, size_t *pmcCount) { pmc_t *array = NULL; pmc_t pmc = NULL; size_t count = 0UL; do { /* Copy down (to the stack) the count of perf counters */ vm_size_t size = perf_counters_count; /* Allocate that sized chunk */ array = (pmc_t *)kalloc(sizeof(pmc_t) * size); if(!array) { return KERN_RESOURCE_SHORTAGE; } /* Take the spin lock */ lck_spin_lock(&perf_counters_queue_spin); /* verify the size didn't change while we were allocating */ if(size != perf_counters_count) { /* * queue size has changed between alloc and now - go back and * make another pass. */ /* drop the lock */ lck_spin_unlock(&perf_counters_queue_spin); /* free the block */ kfree(array, sizeof(pmc_t) * size); array = NULL; } /* if we get here, and array is NULL, we try again. */ }while(!array); /* copy the bits out */ queue_iterate(perf_counters_queue, pmc, pmc_t, link) { /* copy out the pointer */ array[count++] = pmc; } lck_spin_unlock(&perf_counters_queue_spin); /* return the list and the size */ *pmcs = array; *pmcCount = count; return KERN_SUCCESS; } /* * pmc_free_pmc_list frees an array of pmc_t that has been returned from * pmc_get_pmc_list. * * NOTE: This method is not interrupt safe. */ void pmc_free_pmc_list(pmc_t *pmcs, size_t pmcCount) { if(pmcs && pmcCount) { COUNTER_DEBUG("pmcs: %p pmcCount: %lu\n", pmcs, pmcCount); kfree(pmcs, pmcCount * sizeof(pmc_t)); } } kern_return_t pmc_find_by_name(const char *name, pmc_t **pmcs, size_t *pmcCount) { kern_return_t ret = KERN_INVALID_ARGUMENT; if(!name || !pmcs || !pmcCount) { return ret; } pmc_t *list = NULL; size_t count = 0UL; if(KERN_SUCCESS == (ret = pmc_get_pmc_list(&list, &count))) { size_t matchCount = 0UL, ii = 0UL, swapPtr = 0UL; size_t len = strlen(name); for(ii = 0UL; ii < count; ii++) { const char *pmcName = pmc_get_name(list[ii]); if(strlen(pmcName) < len) { /* * If the pmc name is shorter than the requested match, it's no * match, as we're looking for the most specific match(es). */ continue; } if(0 == strncmp(name, pmcName, len)) { pmc_t temp = list[ii]; // move matches to the head of the array. list[ii] = list[swapPtr]; list[swapPtr] = temp; swapPtr++; // keep a count of the matches matchCount++; } } if(matchCount) { /* * If we have matches, they are all at the head of the array, so * just allocate enough space for @matchCount pmc_t's, and copy the * head of the array to the new allocation. Then free the old * allocation. */ pmc_t *result = (pmc_t *)kalloc(sizeof(pmc_t) * matchCount); if(result) { // copy the matches memcpy(result, list, sizeof(pmc_t) * matchCount); ret = KERN_SUCCESS; } pmc_free_pmc_list(list, count); if(!result) { *pmcs = NULL; *pmcCount = 0UL; return KERN_RESOURCE_SHORTAGE; } *pmcs = result; *pmcCount = matchCount; } else { *pmcs = NULL; *pmcCount = 0UL; } } return ret; } /* * pmc_get_name returns a pointer (not copied) to the human-readable name of the * given pmc. * * NOTE: Driver authors must take care to not allocate during this method, as * this method *IS* interrupt safe. */ const char *pmc_get_name(pmc_t pmc) { assert(pmc); const char *name = pmc->methods.get_name(pmc->object); return name; } /* * pmc_get_accessible_core_list returns a pointer to an array of logical core * numbers (as well as the size of that array) that represent the local cores * (hardware threads) from which the given @pmc can be accessed directly. * * NOTE: This method is interrupt safe. */ kern_return_t pmc_get_accessible_core_list(pmc_t pmc, uint32_t **logicalCores, size_t *logicalCoreCt) { kern_return_t ret = KERN_INVALID_ARGUMENT; if(!pmc || !logicalCores || !logicalCoreCt) { return ret; } ret = pmc->methods.accessible_cores(pmc->object, logicalCores, logicalCoreCt); return ret; } static boolean_t pmc_reservation_setup_pmi(pmc_reservation_t resv, pmc_config_t config) { assert(resv); assert(resv->pmc); assert(config); assert(config->object); /* If there's no PMI to setup, return success */ if(config->interrupt_after_value && config->method) { /* set the threshold */ kern_return_t ret = resv->pmc->methods.config_set_threshold(config->object, config->interrupt_after_value); if(KERN_SUCCESS != ret) { /* * This is the most useful error message here, as this only happens * as a result of pmc_reserve*() */ COUNTER_DEBUG("Failed to set threshold for pmc %p\n", resv->pmc); return FALSE; } if(KERN_SUCCESS != resv->pmc->methods.config_set_handler(config->object, (void *)resv, &pmc_reservation_interrupt, config->refCon)) { COUNTER_DEBUG("Failed to set handler for pmc %p\n", resv->pmc); return FALSE; } } return TRUE; } /* * pmc_reserve will attempt to reserve the given @pmc, with a given * configuration object, for counting system-wide. This method will fail with * KERN_FAILURE if the given pmc is already reserved at any scope. * * This method consumes the given configuration object if it returns * KERN_SUCCESS. Any other return value indicates the caller * must free the config object via pmc_free_config(). * * NOTE: This method is NOT interrupt safe. */ kern_return_t pmc_reserve(pmc_t pmc, pmc_config_t config, pmc_reservation_t *reservation) { if(!pmc || !config || !reservation) { return KERN_INVALID_ARGUMENT; } pmc_reservation_t resv = reservation_alloc(); if(!resv) { return KERN_RESOURCE_SHORTAGE; } reservation_init(resv); resv->flags |= PMC_FLAG_SCOPE_SYSTEM; resv->config = config; if(KERN_SUCCESS != pmc_internal_reservation_set_pmc(resv, pmc)) { resv->config = NULL; return KERN_FAILURE; } /* enqueue reservation in proper place */ if(!pmc_internal_reservation_add(resv) || !pmc_reservation_setup_pmi(resv, config)) { /* Prevent free of config object */ resv->config = NULL; reservation_free(resv); return KERN_FAILURE; } perf_monitor_reservation_add(pmc->monitor); *reservation = resv; return KERN_SUCCESS; } /* * pmc_reserve_task will attempt to reserve the given @pmc with a given * configuration object, for counting when the given @task is running on any * logical core that can directly access the given @pmc. This method will fail * with KERN_FAILURE if the given pmc is already reserved at either system or * thread scope. * * This method consumes the given configuration object if it returns * KERN_SUCCESS. Any other return value indicates the caller * must free the config object via pmc_free_config(). * * NOTE: You can reserve the same pmc for N different tasks concurrently. * NOTE: This method is NOT interrupt safe. */ kern_return_t pmc_reserve_task(pmc_t pmc, pmc_config_t config, task_t task, pmc_reservation_t *reservation) { if(!pmc || !config || !reservation || !task) { return KERN_INVALID_ARGUMENT; } if (!(pmc->monitor->methods.flags & PERFMON_FLAG_SUPPORTS_CONTEXT_SWITCHING)) { COUNTER_DEBUG("pmc %p cannot be context switched!\n", pmc); return KERN_INVALID_ARGUMENT; } pmc_reservation_t resv = reservation_alloc(); if(!resv) { return KERN_RESOURCE_SHORTAGE; } reservation_init(resv); resv->flags |= PMC_FLAG_SCOPE_TASK; resv->task = task; resv->config = config; if(KERN_SUCCESS != pmc_internal_reservation_set_pmc(resv, pmc)) { resv->config = NULL; return KERN_FAILURE; } /* enqueue reservation in proper place */ if(!pmc_internal_reservation_add(resv) || !pmc_reservation_setup_pmi(resv, config)) { /* Prevent free of config object */ resv->config = NULL; reservation_free(resv); return KERN_FAILURE; } perf_monitor_reservation_add(pmc->monitor); *reservation = resv; return KERN_SUCCESS; } /* * pmc_reserve_thread will attempt to reserve the given @pmc with a given * configuration object, for counting when the given @thread is running on any * logical core that can directly access the given @pmc. This method will fail * with KERN_FAILURE if the given pmc is already reserved at either system or * task scope. * * This method consumes the given configuration object if it returns * KERN_SUCCESS. Any other return value indicates the caller * must free the config object via pmc_free_config(). * * NOTE: You can reserve the same pmc for N different threads concurrently. * NOTE: This method is NOT interrupt safe. */ kern_return_t pmc_reserve_thread(pmc_t pmc, pmc_config_t config, thread_t thread, pmc_reservation_t *reservation) { if(!pmc || !config || !reservation || !thread) { return KERN_INVALID_ARGUMENT; } if (!(pmc->monitor->methods.flags & PERFMON_FLAG_SUPPORTS_CONTEXT_SWITCHING)) { COUNTER_DEBUG("pmc %p cannot be context switched!\n", pmc); return KERN_INVALID_ARGUMENT; } pmc_reservation_t resv = reservation_alloc(); if(!resv) { return KERN_RESOURCE_SHORTAGE; } reservation_init(resv); resv->flags |= PMC_FLAG_SCOPE_THREAD; resv->thread = thread; resv->config = config; if(KERN_SUCCESS != pmc_internal_reservation_set_pmc(resv, pmc)) { resv->config = NULL; return KERN_FAILURE; } /* enqueue reservation in proper place */ if(!pmc_internal_reservation_add(resv) || !pmc_reservation_setup_pmi(resv, config)) { /* Prevent free of config object */ resv->config = NULL; reservation_free(resv); return KERN_FAILURE; } perf_monitor_reservation_add(pmc->monitor); *reservation = resv; return KERN_SUCCESS; } /* * pmc_reservation_start instructs the given reservation to start counting as * soon as possible. * * NOTE: This method is interrupt safe. */ kern_return_t pmc_reservation_start(pmc_reservation_t reservation) { pmc_state_t newState; if(!reservation) { return KERN_INVALID_ARGUMENT; } /* Move the state machine */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_START, NULL))) { return KERN_FAILURE; } /* If we are currently in an interrupt, don't bother to broadcast since it won't do anything now and the interrupt will * broadcast right before it leaves */ if (PMC_STATE_STATE(newState) != PMC_STATE_STATE_INTERRUPT) { /* A valid state move has been made, but won't be picked up until a context switch occurs. To cause matching * contexts that are currently running to update, we do an inter-processor message to run pmc_internal_reservation_start_cpu * on every cpu that can access the PMC. */ pmc_internal_reservation_broadcast(reservation, pmc_internal_reservation_start_cpu); } return KERN_SUCCESS; } /* * pmc_reservation_stop instructs the given reservation to stop counting as * soon as possible. When this method returns, the pmc will be marked as stopping * and subsequent calls to pmc_reservation_start will succeed. This does not mean * that the pmc hardware has _actually_ stopped running. Assuming no other changes * to the reservation state, the pmc hardware _will_ stop shortly. * */ kern_return_t pmc_reservation_stop(pmc_reservation_t reservation) { pmc_state_t newState; if(!reservation) { return KERN_INVALID_ARGUMENT; } /* Move the state machine */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_STOP, NULL))) { return KERN_FAILURE; } /* If we are currently in an interrupt, don't bother to broadcast since it won't do anything now and the interrupt will * broadcast right before it leaves. Similarly, if we just moved directly to STOP, don't bother broadcasting. */ if (PMC_STATE_STATE(newState) != PMC_STATE_STATE_INTERRUPT && PMC_STATE_STATE(newState) != PMC_STATE_STATE_STOP) { /* A valid state move has been made, but won't be picked up until a context switch occurs. To cause matching * contexts that are currently running to update, we do an inter-processor message to run pmc_internal_reservation_stop_cpu * on every cpu that can access the PMC. */ pmc_internal_reservation_broadcast(reservation, pmc_internal_reservation_stop_cpu); } return KERN_SUCCESS; } /* * pmc_reservation_read will read the event count associated with a reservation. * If the caller is current executing in a context that both a) matches the * reservation's context, and b) can access the reservation's pmc directly, the * value will be read from hardware. Otherwise, this returns the reservation's * stored value. * * NOTE: This method is interrupt safe. * NOTE: When not on the interrupt stack, this method may block. */ kern_return_t pmc_reservation_read(pmc_reservation_t reservation, uint64_t *value) { kern_return_t ret = KERN_FAILURE; uint64_t timeout; uint32_t spins; if(!reservation || !value) { return KERN_INVALID_ARGUMENT; } nanoseconds_to_absolutetime(PMC_SPIN_TIMEOUT_US * 1000, &timeout); timeout += mach_absolute_time(); spins = 0; do { uint32_t state = reservation->state; if((PMC_STATE_STATE(state) == PMC_STATE_STATE_RUN)) { /* Attempt read from hardware via drivers. */ assert(reservation->pmc); ret = reservation->pmc->methods.get_count(reservation->pmc->object, value); break; } else if ((PMC_STATE_STATE(state) == PMC_STATE_STATE_STORE) || (PMC_STATE_STATE(state) == PMC_STATE_STATE_LOAD)) { /* Spin */ /* Assert if this takes longer than PMC_SPIN_TIMEOUT_US */ if (++spins > PMC_SPIN_THRESHOLD) { if (mach_absolute_time() > timeout) { pmc_spin_timeout_count++; assert(0); } } cpu_pause(); } else { break; } } while (1); /* If the direct hardware read failed (for whatever reason) */ if(KERN_SUCCESS != ret) { /* Read stored value */ *value = reservation->value; } return KERN_SUCCESS; } /* * pmc_reservation_write will write the event count associated with a reservation. * If the caller is current executing in a context that both a) matches the * reservation's context, and b) can access the reservation's pmc directly, the * value will be written to hardware. Otherwise, this writes the reservation's * stored value. * * NOTE: This method is interrupt safe. * NOTE: When not on the interrupt stack, this method may block. */ kern_return_t pmc_reservation_write(pmc_reservation_t reservation, uint64_t value) { kern_return_t ret = KERN_FAILURE; uint64_t timeout; uint32_t spins; if(!reservation) { return KERN_INVALID_ARGUMENT; } nanoseconds_to_absolutetime(PMC_SPIN_TIMEOUT_US * 1000, &timeout); timeout += mach_absolute_time(); spins = 0; do { uint32_t state = reservation->state; if((PMC_STATE_STATE(state) == PMC_STATE_STATE_RUN)) { /* Write to hardware via drivers. */ assert(reservation->pmc); ret = reservation->pmc->methods.set_count(reservation->pmc->object, value); break; } else if ((PMC_STATE_STATE(state) == PMC_STATE_STATE_STORE) || (PMC_STATE_STATE(state) == PMC_STATE_STATE_LOAD)) { /* Spin */ /* Assert if this takes longer than PMC_SPIN_TIMEOUT_US */ if (++spins > PMC_SPIN_THRESHOLD) { if (mach_absolute_time() > timeout) { pmc_spin_timeout_count++; assert(0); } } cpu_pause(); } else { break; } } while (1); if(KERN_SUCCESS != ret) { /* Write stored value */ reservation->value = value; } return KERN_SUCCESS; } /* * pmc_reservation_free releases a reservation and all associated resources. * * NOTE: This method is NOT interrupt safe. */ kern_return_t pmc_reservation_free(pmc_reservation_t reservation) { pmc_state_t newState; if(!reservation) { return KERN_INVALID_ARGUMENT; } perf_monitor_reservation_remove(reservation->pmc->monitor); /* Move the state machine */ if (PMC_STATE_INVALID == (newState = pmc_internal_reservation_move_for_event(reservation, PMC_STATE_EVENT_FREE, NULL))) { return KERN_FAILURE; } /* If we didn't move directly to DEALLOC, help things along */ if (PMC_STATE_STATE(newState) != PMC_STATE_STATE_DEALLOC) { /* A valid state move has been made, but won't be picked up until a context switch occurs. To cause matching * contexts that are currently running to update, we do an inter-processor message to run pmc_internal_reservation_stop_cpu * on every cpu that can access the PMC. */ pmc_internal_reservation_broadcast(reservation, pmc_internal_reservation_stop_cpu); } /* Block until the reservation hits the <DEALLOC, 0, > state */ while (!(PMC_STATE_STATE(reservation->state) == PMC_STATE_STATE_DEALLOC && PMC_STATE_CONTEXT_COUNT(reservation->state) == 0 && PMC_STATE_FLAGS(reservation->state) == 0)) { assert_wait((event_t)reservation, THREAD_UNINT); thread_block(THREAD_CONTINUE_NULL); } /* remove from queues */ pmc_internal_reservation_remove(reservation); /* free reservation */ reservation_free(reservation); return KERN_SUCCESS; } /* * pmc_idle notifies eligible monitors of impending per-CPU idle, and can be used to save state. */ boolean_t pmc_idle(void) { perf_monitor_t monitor = NULL; queue_head_t *cpu_queue; lck_spin_lock(&perf_monitor_queue_spin); if (cpu_monitor_queues) { cpu_queue = cpu_monitor_queues[cpu_number()]; queue_iterate(cpu_queue, monitor, perf_monitor_t, cpu_link) { perf_monitor_methods_t *methods = &(monitor->methods); if ((methods->flags & PERFMON_FLAG_ALWAYS_ACTIVE) || (monitor->reservedCounters)) { methods->on_idle(monitor->object); } } } lck_spin_unlock(&perf_monitor_queue_spin); return TRUE; } /* * pmc_idle_exit notifies eligible monitors of wake from idle; it can be used to restore state. */ boolean_t pmc_idle_exit(void) { perf_monitor_t monitor = NULL; queue_head_t *cpu_queue; lck_spin_lock(&perf_monitor_queue_spin); if (cpu_monitor_queues) { cpu_queue = cpu_monitor_queues[cpu_number()]; queue_iterate(cpu_queue, monitor, perf_monitor_t, cpu_link) { perf_monitor_methods_t *methods = &(monitor->methods); if ((methods->flags & PERFMON_FLAG_ALWAYS_ACTIVE) || (monitor->reservedCounters)) { methods->on_idle_exit(monitor->object); } } } lck_spin_unlock(&perf_monitor_queue_spin); return TRUE; } /* * pmc_context_switch performs all context switching necessary to save all pmc * state associated with @oldThread (and the task to which @oldThread belongs), * as well as to restore all pmc state associated with @newThread (and the task * to which @newThread belongs). * * NOTE: This method IS interrupt safe. */ boolean_t pmc_context_switch(thread_t oldThread, thread_t newThread) { pmc_reservation_t resv = NULL; uint32_t cpuNum = cpu_number(); lck_spin_lock(&reservations_spin); /* Save pmc states */ if (thread_reservation_count) { queue_iterate(thread_reservations, resv, pmc_reservation_t, link) { if ((oldThread == resv->thread) && pmc_accessible_from_core(resv->pmc, cpuNum)) { (void)pmc_internal_reservation_context_out(resv); } } } if (task_reservation_count) { queue_iterate(task_reservations, resv, pmc_reservation_t, link) { if ((resv->task == oldThread->task) && pmc_accessible_from_core(resv->pmc, cpuNum)) { (void)pmc_internal_reservation_context_out(resv); } } } /* Restore */ if (thread_reservation_count) { queue_iterate(thread_reservations, resv, pmc_reservation_t, link) { if ((resv->thread == newThread) && pmc_accessible_from_core(resv->pmc, cpuNum)) { (void)pmc_internal_reservation_context_in(resv); } } } if (task_reservation_count) { queue_iterate(task_reservations, resv, pmc_reservation_t, link) { if ((resv->task == newThread->task) && pmc_accessible_from_core(resv->pmc, cpuNum)) { (void)pmc_internal_reservation_context_in(resv); } } } lck_spin_unlock(&reservations_spin); return TRUE; } #else /* !CONFIG_COUNTERS */ #if 0 #pragma mark - #pragma mark Dummy functions #endif /* * In the case that someone has chosen not to include the PMC KPI in some * configuration, we still have exports for kexts, so we'll need to define stub * methods that return failures. */ kern_return_t perf_monitor_register(perf_monitor_object_t monitor __unused, perf_monitor_methods_t *methods __unused) { return KERN_FAILURE; } kern_return_t perf_monitor_unregister(perf_monitor_object_t monitor __unused) { return KERN_FAILURE; } kern_return_t pmc_register(perf_monitor_object_t monitor __unused, pmc_object_t pmc __unused, pmc_methods_t *methods __unused, void *object __unused) { return KERN_FAILURE; } kern_return_t pmc_unregister(perf_monitor_object_t monitor __unused, pmc_object_t pmc __unused) { return KERN_FAILURE; } kern_return_t pmc_create_config(pmc_t pmc __unused, pmc_config_t *config __unused) { return KERN_FAILURE; } void pmc_free_config(pmc_t pmc __unused, pmc_config_t config __unused) { } kern_return_t pmc_config_set_value(pmc_t pmc __unused, pmc_config_t config __unused, uint8_t id __unused, uint64_t value __unused) { return KERN_FAILURE; } kern_return_t pmc_config_set_interrupt_threshold(pmc_t pmc __unused, pmc_config_t config __unused, uint64_t threshold __unused, pmc_interrupt_method_t method __unused, void *refCon __unused) { return KERN_FAILURE; } kern_return_t pmc_get_pmc_list(pmc_t **pmcs __unused, size_t *pmcCount __unused) { return KERN_FAILURE; } void pmc_free_pmc_list(pmc_t *pmcs __unused, size_t pmcCount __unused) { } kern_return_t pmc_find_by_name(const char *name __unused, pmc_t **pmcs __unused, size_t *pmcCount __unused) { return KERN_FAILURE; } const char *pmc_get_name(pmc_t pmc __unused) { return ""; } kern_return_t pmc_get_accessible_core_list(pmc_t pmc __unused, uint32_t **logicalCores __unused, size_t *logicalCoreCt __unused) { return KERN_FAILURE; } kern_return_t pmc_reserve(pmc_t pmc __unused, pmc_config_t config __unused, pmc_reservation_t *reservation __unused) { return KERN_FAILURE; } kern_return_t pmc_reserve_task(pmc_t pmc __unused, pmc_config_t config __unused, task_t task __unused, pmc_reservation_t *reservation __unused) { return KERN_FAILURE; } kern_return_t pmc_reserve_thread(pmc_t pmc __unused, pmc_config_t config __unused, thread_t thread __unused, pmc_reservation_t *reservation __unused) { return KERN_FAILURE; } kern_return_t pmc_reservation_start(pmc_reservation_t reservation __unused) { return KERN_FAILURE; } kern_return_t pmc_reservation_stop(pmc_reservation_t reservation __unused) { return KERN_FAILURE; } kern_return_t pmc_reservation_read(pmc_reservation_t reservation __unused, uint64_t *value __unused) { return KERN_FAILURE; } kern_return_t pmc_reservation_write(pmc_reservation_t reservation __unused, uint64_t value __unused) { return KERN_FAILURE; } kern_return_t pmc_reservation_free(pmc_reservation_t reservation __unused) { return KERN_FAILURE; } #endif /* !CONFIG_COUNTERS */ |