1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166
|
/*
* Copyright © 2016 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <poll.h>
#include <math.h>
#include "i915/gem.h"
#include "i915/gem_engine_topology.h"
#include "i915/perf.h"
#include "igt.h"
#include "igt_perf.h"
#include "igt_sysfs.h"
#include "drm.h"
/**
* TEST: perf
* Description: Test the i915 perf metrics streaming interface
* Category: Core
* Mega feature: Performance interface
* Sub-category: Performance tests
* Functionality: oa
* Feature: i915 streaming interface, oa
* Test category: Perf
*
* SUBTEST: blocking
* Description: Test blocking read with default hrtimer frequency
*
* SUBTEST: blocking-parameterized
* Description: Test blocking read with different hrtimer frequencies
*
* SUBTEST: buffer-fill
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: create-destroy-userspace-config
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: disabled-read-error
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: enable-disable
* Feature: oa perf
*
* SUBTEST: gen12-group-concurrent-oa-buffer-read
* Description: Verify concurrent reads from OA buffers in different groups
* Feature: oa perf
*
* SUBTEST: gen12-group-exclusive-stream-ctx-handle
* Description: Verify exclusivity of perf streams with ctx handle
* Feature: oa perf
*
* SUBTEST: gen12-group-exclusive-stream-sample-oa
* Description: Verify exclusivity of perf streams with sample oa option
* Feature: oa perf
*
* SUBTEST: gen12-invalid-class-instance
* Description: Verify invalid class instance
*
* SUBTEST: gen12-mi-rpc
* Description: Test MI REPORT PERF COUNT for Gen 12
* Feature: oa perf
*
* SUBTEST: gen12-oa-tlb-invalidate
* Description: Test OA TLB invalidate
*
* SUBTEST: gen12-unprivileged-single-ctx-counters
* Description: Measure performance for a specific context using OAR in Gen 12
* Feature: oa perf
*
* SUBTEST: gen8-unprivileged-single-ctx-counters
* Feature: oa perf, obsolete test
*
* SUBTEST: global-sseu-config
* Description: Verify specifying SSEU opening parameters
* Feature: oa perf
*
* SUBTEST: global-sseu-config-invalid
* Description: Verify invalid SSEU opening parameters
* Feature: oa perf
*
* SUBTEST: i915-ref-count
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: invalid-create-userspace-config
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: invalid-oa-exponent
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: invalid-oa-format-id
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: invalid-oa-metric-set-id
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: invalid-open-flags
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: invalid-remove-userspace-config
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: low-oa-exponent-permissions
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: mi-rpc
* Feature: oa perf
*
* SUBTEST: missing-sample-flags
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: non-sampling-read-error
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: non-system-wide-paranoid
* Feature: oa perf
*
* SUBTEST: non-zero-reason
* Description: Test that reason field in OA reports is never 0 on Gen8+
*
* SUBTEST: oa-exponents
* Feature: oa perf
*
* SUBTEST: oa-formats
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: per-context-mode-unprivileged
* Feature: oa perf, obsolete test
*
* SUBTEST: polling
* Description: Test polled read with default hrtimer frequency
*
* SUBTEST: polling-parameterized
* Description: Test polled read with different hrtimer frequencies
*
* SUBTEST: polling-small-buf
* Description: Test polled read with buffer size smaller than available data
*
* SUBTEST: rc6-disable
* Feature: oa perf
*
* SUBTEST: short-reads
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: stress-open-close
* Description: Stress tests opening & closing the i915-perf stream in a busy loop
* Feature: oa perf
*
* SUBTEST: sysctl-defaults
* Description: Test the i915 perf metrics streaming interface
*
* SUBTEST: unprivileged-single-ctx-counters
* Feature: oa perf, obsolete test
*
* SUBTEST: whitelisted-registers-userspace-config
* Description: Test the i915 perf metrics streaming interface
*/
IGT_TEST_DESCRIPTION("Test the i915 perf metrics streaming interface");
#define GEN6_MI_REPORT_PERF_COUNT ((0x28 << 23) | (3 - 2))
#define GEN8_MI_REPORT_PERF_COUNT ((0x28 << 23) | (4 - 2))
#define OAREPORT_REASON_MASK 0x3f
#define OAREPORT_REASON_SHIFT 19
#define OAREPORT_REASON_TIMER (1<<0)
#define OAREPORT_REASON_INTERNAL (3<<1)
#define OAREPORT_REASON_CTX_SWITCH (1<<3)
#define OAREPORT_REASON_GO (1<<4)
#define OAREPORT_REASON_CLK_RATIO (1<<5)
#define PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET (1 << 19)
#define PIPE_CONTROL_SYNC_GFDT (1 << 17)
#define PIPE_CONTROL_NO_WRITE (0 << 14)
#define PIPE_CONTROL_WRITE_IMMEDIATE (1 << 14)
#define PIPE_CONTROL_WRITE_DEPTH_COUNT (2 << 14)
#define PIPE_CONTROL_RENDER_TARGET_FLUSH (1 << 12)
#define PIPE_CONTROL_INSTRUCTION_INVALIDATE (1 << 11)
#define PIPE_CONTROL_ISP_DIS (1 << 9)
#define PIPE_CONTROL_INTERRUPT_ENABLE (1 << 8)
/* GT */
#define PIPE_CONTROL_DATA_CACHE_INVALIDATE (1 << 5)
#define PIPE_CONTROL_PPGTT_WRITE (0 << 2)
#define PIPE_CONTROL_GLOBAL_GTT_WRITE (1 << 2)
#define MAX_OA_BUF_SIZE (16 * 1024 * 1024)
#define ADD_PROPS(_head, _tail, _key, _value) \
do { \
igt_assert((_tail - _head) < (DRM_I915_PERF_PROP_MAX * 2)); \
*_tail++ = DRM_I915_PERF_PROP_##_key; \
*_tail++ = _value; \
} while (0)
struct accumulator {
#define MAX_RAW_OA_COUNTERS 62
enum drm_i915_oa_format format;
uint64_t deltas[MAX_RAW_OA_COUNTERS];
};
enum {
OAG,
OAR,
OAM,
MAX_OA_TYPE,
};
struct oa_format {
const char *name;
size_t size;
int a40_high_off; /* bytes */
int a40_low_off;
int n_a40;
int a64_off;
int n_a64;
int a_off;
int n_a;
int first_a;
int first_a40;
int b_off;
int n_b;
int c_off;
int n_c;
int oa_type;
bool report_hdr_64bit;
};
static struct oa_format hsw_oa_formats[I915_OA_FORMAT_MAX] = {
[I915_OA_FORMAT_A13] = { /* HSW only */
"A13", .size = 64,
.a_off = 12, .n_a = 13, },
[I915_OA_FORMAT_A29] = { /* HSW only */
"A29", .size = 128,
.a_off = 12, .n_a = 29, },
[I915_OA_FORMAT_A13_B8_C8] = { /* HSW only */
"A13_B8_C8", .size = 128,
.a_off = 12, .n_a = 13,
.b_off = 64, .n_b = 8,
.c_off = 96, .n_c = 8, },
[I915_OA_FORMAT_A45_B8_C8] = { /* HSW only */
"A45_B8_C8", .size = 256,
.a_off = 12, .n_a = 45,
.b_off = 192, .n_b = 8,
.c_off = 224, .n_c = 8, },
[I915_OA_FORMAT_B4_C8] = { /* HSW only */
"B4_C8", .size = 64,
.b_off = 16, .n_b = 4,
.c_off = 32, .n_c = 8, },
[I915_OA_FORMAT_B4_C8_A16] = { /* HSW only */
"B4_C8_A16", .size = 128,
.b_off = 16, .n_b = 4,
.c_off = 32, .n_c = 8,
.a_off = 60, .n_a = 16, .first_a = 29, },
[I915_OA_FORMAT_C4_B8] = { /* HSW+ (header differs from HSW-Gen8+) */
"C4_B8", .size = 64,
.c_off = 16, .n_c = 4,
.b_off = 28, .n_b = 8 },
};
static struct oa_format gen8_oa_formats[I915_OA_FORMAT_MAX] = {
[I915_OA_FORMAT_A12] = {
"A12", .size = 64,
.a_off = 12, .n_a = 12, .first_a = 7, },
[I915_OA_FORMAT_A12_B8_C8] = {
"A12_B8_C8", .size = 128,
.a_off = 12, .n_a = 12,
.b_off = 64, .n_b = 8,
.c_off = 96, .n_c = 8, .first_a = 7, },
[I915_OA_FORMAT_A32u40_A4u32_B8_C8] = {
"A32u40_A4u32_B8_C8", .size = 256,
.a40_high_off = 160, .a40_low_off = 16, .n_a40 = 32,
.a_off = 144, .n_a = 4, .first_a = 32,
.b_off = 192, .n_b = 8,
.c_off = 224, .n_c = 8, },
[I915_OA_FORMAT_C4_B8] = {
"C4_B8", .size = 64,
.c_off = 16, .n_c = 4,
.b_off = 32, .n_b = 8, },
};
static struct oa_format gen12_oa_formats[I915_OA_FORMAT_MAX] = {
[I915_OA_FORMAT_A32u40_A4u32_B8_C8] = {
"A32u40_A4u32_B8_C8", .size = 256,
.a40_high_off = 160, .a40_low_off = 16, .n_a40 = 32,
.a_off = 144, .n_a = 4, .first_a = 32,
.b_off = 192, .n_b = 8,
.c_off = 224, .n_c = 8, },
};
static struct oa_format dg2_oa_formats[I915_OA_FORMAT_MAX] = {
[I915_OAR_FORMAT_A32u40_A4u32_B8_C8] = {
"A32u40_A4u32_B8_C8", .size = 256,
.a40_high_off = 160, .a40_low_off = 16, .n_a40 = 32,
.a_off = 144, .n_a = 4, .first_a = 32,
.b_off = 192, .n_b = 8,
.c_off = 224, .n_c = 8, .oa_type = OAR, },
/* This format has A36 and A37 interleaved with high bytes of some A
* counters, so we will accumulate only subset of counters.
*/
[I915_OA_FORMAT_A24u40_A14u32_B8_C8] = {
"A24u40_A14u32_B8_C8", .size = 256,
/* u40: A4 - A23 */
.a40_high_off = 160, .a40_low_off = 16, .n_a40 = 20, .first_a40 = 4,
/* u32: A0 - A3 */
.a_off = 16, .n_a = 4,
.b_off = 192, .n_b = 8,
.c_off = 224, .n_c = 8, .oa_type = OAG, },
};
static struct oa_format mtl_oa_formats[I915_OA_FORMAT_MAX] = {
[I915_OAR_FORMAT_A32u40_A4u32_B8_C8] = {
"A32u40_A4u32_B8_C8", .size = 256,
.a40_high_off = 160, .a40_low_off = 16, .n_a40 = 32,
.a_off = 144, .n_a = 4, .first_a = 32,
.b_off = 192, .n_b = 8,
.c_off = 224, .n_c = 8, .oa_type = OAR, },
/* This format has A36 and A37 interleaved with high bytes of some A
* counters, so we will accumulate only subset of counters.
*/
[I915_OA_FORMAT_A24u40_A14u32_B8_C8] = {
"A24u40_A14u32_B8_C8", .size = 256,
/* u40: A4 - A23 */
.a40_high_off = 160, .a40_low_off = 16, .n_a40 = 20, .first_a40 = 4,
/* u32: A0 - A3 */
.a_off = 16, .n_a = 4,
.b_off = 192, .n_b = 8,
.c_off = 224, .n_c = 8, .oa_type = OAG, },
/* Treat MPEC countes as A counters for now */
[I915_OAM_FORMAT_MPEC8u64_B8_C8] = {
"MPEC8u64_B8_C8", .size = 192,
.a64_off = 32, .n_a64 = 8,
.b_off = 96, .n_b = 8,
.c_off = 128, .n_c = 8, .oa_type = OAM,
.report_hdr_64bit = true, },
[I915_OAM_FORMAT_MPEC8u32_B8_C8] = {
"MPEC8u32_B8_C8", .size = 128,
.a_off = 32, .n_a = 8,
.b_off = 64, .n_b = 8,
.c_off = 96, .n_c = 8, .oa_type = OAM,
.report_hdr_64bit = true, },
};
static bool hsw_undefined_a_counters[45] = {
[4] = true,
[6] = true,
[9] = true,
[11] = true,
[14] = true,
[16] = true,
[19] = true,
[21] = true,
[24] = true,
[26] = true,
[29] = true,
[31] = true,
[34] = true,
[43] = true,
[44] = true,
};
/* No A counters currently reserved/undefined for gen8+ so far */
static bool gen8_undefined_a_counters[45];
static int drm_fd = -1;
static int sysfs = -1;
static int pm_fd = -1;
static int stream_fd = -1;
static uint32_t devid;
static struct intel_execution_engine2 default_e2;
static struct perf_engine_group *perf_oa_groups;
static uint32_t num_perf_oa_groups;
static uint64_t gt_max_freq_mhz = 0;
static struct intel_perf *intel_perf = NULL;
static bool *undefined_a_counters;
static uint64_t oa_exp_1_millisec;
static igt_render_copyfunc_t render_copy = NULL;
static uint64_t (*read_report_ticks)(const uint32_t *report,
enum drm_i915_oa_format format);
static void (*sanity_check_reports)(const uint32_t *oa_report0,
const uint32_t *oa_report1,
enum drm_i915_oa_format format);
static struct intel_perf_metric_set *metric_set(const struct intel_execution_engine2 *e2);
#define default_test_set metric_set(&default_e2)
static void
dump_report(const uint32_t *report, uint32_t size, const char *message) {
uint32_t i;
igt_debug("%s\n", message);
for (i = 0; i < size; i += 4) {
igt_debug("%08x %08x %08x %08x\n",
report[i],
report[i + 1],
report[i + 2],
report[i + 3]);
}
}
static struct oa_format
get_oa_format(enum drm_i915_oa_format format)
{
if (IS_HASWELL(devid))
return hsw_oa_formats[format];
else if (IS_DG2(devid))
return dg2_oa_formats[format];
else if (IS_METEORLAKE(devid))
return mtl_oa_formats[format];
else if (IS_GEN12(devid))
return gen12_oa_formats[format];
else
return gen8_oa_formats[format];
}
static char *
pretty_print_oa_period(uint64_t oa_period_ns)
{
static char result[100];
static const char *units[4] = { "ns", "us", "ms", "s" };
double val = oa_period_ns;
int iter = 0;
while (iter < (ARRAY_SIZE(units) - 1) &&
val >= 1000.0f) {
val /= 1000.0f;
iter++;
}
snprintf(result, sizeof(result), "%.3f%s", val, units[iter]);
return result;
}
static void
__perf_close(int fd)
{
close(fd);
stream_fd = -1;
if (pm_fd >= 0) {
close(pm_fd);
pm_fd = -1;
}
}
static int
__perf_open(int fd, struct drm_i915_perf_open_param *param, bool prevent_pm)
{
int ret;
int32_t pm_value = 0;
if (stream_fd >= 0)
__perf_close(stream_fd);
if (pm_fd >= 0) {
close(pm_fd);
pm_fd = -1;
}
ret = igt_ioctl(fd, DRM_IOCTL_I915_PERF_OPEN, param);
igt_assert_lte(0, ret);
errno = 0;
if (prevent_pm) {
pm_fd = open("/dev/cpu_dma_latency", O_RDWR);
igt_assert_lte(0, pm_fd);
igt_assert_eq(write(pm_fd, &pm_value, sizeof(pm_value)), sizeof(pm_value));
}
return ret;
}
static int i915_perf_revision(int fd)
{
drm_i915_getparam_t gp;
int value = 1, ret;
gp.param = I915_PARAM_PERF_REVISION;
gp.value = &value;
ret = igt_ioctl(drm_fd, DRM_IOCTL_I915_GETPARAM, &gp);
if (ret == -1) {
/* If the param is missing, consider version 1. */
igt_assert_eq(errno, EINVAL);
return 1;
}
return value;
}
static bool
has_param_class_instance(void)
{
return i915_perf_revision(drm_fd) >= 6;
}
static bool
has_param_poll_period(void)
{
return i915_perf_revision(drm_fd) >= 5;
}
static int
lookup_format(int i915_perf_fmt_id)
{
igt_assert(i915_perf_fmt_id < I915_OA_FORMAT_MAX);
igt_assert(get_oa_format(i915_perf_fmt_id).name);
return i915_perf_fmt_id;
}
static uint64_t
read_u64_file(const char *path)
{
FILE *f;
uint64_t val;
f = fopen(path, "r");
igt_assert(f);
igt_assert_eq(fscanf(f, "%"PRIu64, &val), 1);
fclose(f);
return val;
}
static void
write_u64_file(const char *path, uint64_t val)
{
FILE *f;
f = fopen(path, "w");
igt_assert(f);
igt_assert(fprintf(f, "%"PRIu64, val) > 0);
fclose(f);
}
static bool
try_sysfs_read_u64(const char *path, uint64_t *val)
{
return igt_sysfs_scanf(sysfs, path, "%"PRIu64, val) == 1;
}
static unsigned long
sysfs_read(enum i915_attr_id id)
{
unsigned long value;
igt_assert(igt_sysfs_rps_scanf(sysfs, id, "%lu", &value) == 1);
return value;
}
/* XXX: For Haswell this utility is only applicable to the render basic
* metric set.
*
* C2 corresponds to a clock counter for the Haswell render basic metric set
* but it's not included in all of the formats.
*/
static uint64_t
hsw_read_report_ticks(const uint32_t *report, enum drm_i915_oa_format format)
{
uint32_t *c = (uint32_t *)(((uint8_t *)report) + get_oa_format(format).c_off);
igt_assert_neq(get_oa_format(format).n_c, 0);
return c[2];
}
static uint64_t
gen8_read_report_ticks(const uint32_t *report, enum drm_i915_oa_format format)
{
struct oa_format fmt = get_oa_format(format);
return fmt.report_hdr_64bit ? *(uint64_t *)&report[6] : report[3];
}
/*
* t0 is a value sampled before t1. width is number of bits used to represent
* t0/t1. Normally t1 is greater than t0. In cases where t1 < t0 use this
* helper. Since the size of t1/t0 is already 64 bits, no special handling is
* needed for width = 64.
*/
static uint64_t
elapsed_delta(uint64_t t1, uint64_t t0, uint32_t width)
{
uint32_t max_bits = sizeof(t1) * 8;
igt_assert_lte_u32(width, max_bits);
if (t1 < t0 && width != max_bits)
return ((1ULL << width) - t0) + t1;
return t1 - t0;
}
static uint64_t
oa_tick_delta(const uint32_t *report1,
const uint32_t *report0,
enum drm_i915_oa_format format)
{
return elapsed_delta(read_report_ticks(report1, format),
read_report_ticks(report0, format), 32);
}
static void
gen8_read_report_clock_ratios(const uint32_t *report,
uint32_t *slice_freq_mhz,
uint32_t *unslice_freq_mhz)
{
uint32_t unslice_freq = report[0] & 0x1ff;
uint32_t slice_freq_low = (report[0] >> 25) & 0x7f;
uint32_t slice_freq_high = (report[0] >> 9) & 0x3;
uint32_t slice_freq = slice_freq_low | (slice_freq_high << 7);
*slice_freq_mhz = (slice_freq * 16666) / 1000;
*unslice_freq_mhz = (unslice_freq * 16666) / 1000;
}
static uint32_t
gen8_report_reason(const uint32_t *report)
{
return ((report[0] >> OAREPORT_REASON_SHIFT) &
OAREPORT_REASON_MASK);
}
static const char *
gen8_read_report_reason(const uint32_t *report)
{
uint32_t reason = gen8_report_reason(report);
if (reason & (1<<0))
return "timer";
else if (reason & (1<<1))
return "internal trigger 1";
else if (reason & (1<<2))
return "internal trigger 2";
else if (reason & (1<<3))
return "context switch";
else if (reason & (1<<4))
return "GO 1->0 transition (enter RC6)";
else if (reason & (1<<5))
return "[un]slice clock ratio change";
else
return "unknown";
}
static uint32_t
cs_timestamp_frequency(int fd)
{
struct drm_i915_getparam gp = {};
static uint32_t value = 0;
if (value)
return value;
gp.param = I915_PARAM_CS_TIMESTAMP_FREQUENCY;
gp.value = (int *)(&value);
igt_assert_eq(igt_ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp), 0);
return value;
}
static uint64_t
cs_timebase_scale(uint32_t u32_delta)
{
return ((uint64_t)u32_delta * NSEC_PER_SEC) / cs_timestamp_frequency(drm_fd);
}
static uint64_t
oa_timestamp(const uint32_t *report, enum drm_i915_oa_format format)
{
struct oa_format fmt = get_oa_format(format);
return fmt.report_hdr_64bit ? *(uint64_t *)&report[2] : report[1];
}
static uint64_t
oa_timestamp_delta(const uint32_t *report1,
const uint32_t *report0,
enum drm_i915_oa_format format)
{
uint32_t width = intel_graphics_ver(devid) >= IP_VER(12, 55) ? 56 : 32;
return elapsed_delta(oa_timestamp(report1, format),
oa_timestamp(report0, format), width);
}
static uint64_t
timebase_scale(uint64_t delta)
{
return (delta * NSEC_PER_SEC) / intel_perf->devinfo.timestamp_frequency;
}
/* Returns: the largest OA exponent that will still result in a sampling period
* less than or equal to the given @period.
*/
static int
max_oa_exponent_for_period_lte(uint64_t period)
{
/* NB: timebase_scale() takes a uint64_t and an exponent of 30
* would already represent a period of ~3 minutes so there's
* really no need to consider higher exponents.
*/
for (int i = 0; i < 30; i++) {
uint64_t oa_period = timebase_scale(2 << i);
if (oa_period > period)
return max(0, i - 1);
}
igt_assert(!"reached");
return -1;
}
/* Return: the largest OA exponent that will still result in a sampling
* frequency greater than the given @frequency.
*/
static int
max_oa_exponent_for_freq_gt(uint64_t frequency)
{
uint64_t period = NSEC_PER_SEC / frequency;
igt_assert_neq(period, 0);
return max_oa_exponent_for_period_lte(period - 1);
}
static uint64_t
oa_exponent_to_ns(int exponent)
{
return 1000000000ULL * (2ULL << exponent) / intel_perf->devinfo.timestamp_frequency;
}
static bool
oa_report_is_periodic(uint32_t oa_exponent, const uint32_t *report)
{
if (IS_HASWELL(devid)) {
/* For Haswell we don't have a documented report reason field
* (though empirically report[0] bit 10 does seem to correlate
* with a timer trigger reason) so we instead infer which
* reports are timer triggered by checking if the least
* significant bits are zero and the exponent bit is set.
*/
uint32_t oa_exponent_mask = (1 << (oa_exponent + 1)) - 1;
if ((report[1] & oa_exponent_mask) == (1 << oa_exponent))
return true;
} else {
if (gen8_report_reason(report) & OAREPORT_REASON_TIMER)
return true;
}
return false;
}
static bool
oa_report_ctx_is_valid(uint32_t *report)
{
if (IS_HASWELL(devid)) {
return false; /* TODO */
} else if (IS_GEN8(devid)) {
return report[0] & (1ul << 25);
} else if (intel_gen(devid) >= 9) {
return report[0] & (1ul << 16);
}
igt_assert(!"Please update this function for newer Gen");
}
static uint32_t
oa_report_get_ctx_id(uint32_t *report)
{
if (!oa_report_ctx_is_valid(report))
return 0xffffffff;
return report[2];
}
static int
oar_unit_default_format(void)
{
if (IS_DG2(devid) || IS_METEORLAKE(devid))
return I915_OAR_FORMAT_A32u40_A4u32_B8_C8;
return default_test_set->perf_oa_format;
}
/*
* Temporary wrapper to distinguish mappings on !llc platforms,
* where it seems cache over GEM_MMAP_OFFSET is not flushed before execution.
*/
static void *buf_map(int i915, struct intel_buf *buf, bool write)
{
void *p;
if (gem_has_llc(i915))
p = intel_buf_cpu_map(buf, write);
else
p = intel_buf_device_map(buf, write);
return p;
}
static void
scratch_buf_memset(struct intel_buf *buf, int width, int height, uint32_t color)
{
buf_map(buf_ops_get_fd(buf->bops), buf, true);
for (int i = 0; i < width * height; i++)
buf->ptr[i] = color;
intel_buf_unmap(buf);
}
static void
scratch_buf_init(struct buf_ops *bops,
struct intel_buf *buf,
int width, int height,
uint32_t color)
{
intel_buf_init(bops, buf, width, height, 32, 0,
I915_TILING_NONE, I915_COMPRESSION_NONE);
scratch_buf_memset(buf, width, height, color);
}
static void
emit_report_perf_count(struct intel_bb *ibb,
struct intel_buf *dst,
int dst_offset,
uint32_t report_id)
{
intel_bb_add_intel_buf(ibb, dst, true);
if (IS_HASWELL(devid))
intel_bb_out(ibb, GEN6_MI_REPORT_PERF_COUNT);
else
intel_bb_out(ibb, GEN8_MI_REPORT_PERF_COUNT);
intel_bb_emit_reloc(ibb, dst->handle,
I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
dst_offset, dst->addr.offset);
intel_bb_out(ibb, report_id);
}
static void
hsw_sanity_check_render_basic_reports(const uint32_t *oa_report0,
const uint32_t *oa_report1,
enum drm_i915_oa_format fmt)
{
uint64_t time_delta = timebase_scale(oa_timestamp_delta(oa_report1,
oa_report0,
fmt));
uint64_t clock_delta;
uint64_t max_delta;
struct oa_format format = get_oa_format(fmt);
igt_assert_neq_u64(time_delta, 0);
/* As a special case we have to consider that on Haswell we
* can't explicitly derive a clock delta for all OA report
* formats...
*/
if (format.n_c == 0) {
/* Assume running at max freq for sake of
* below sanity check on counters... */
clock_delta = (gt_max_freq_mhz * time_delta) / 1000;
} else {
uint64_t freq;
clock_delta = oa_tick_delta(oa_report1, oa_report0, fmt);
igt_assert_neq_u64(clock_delta, 0);
freq = (clock_delta * 1000) / time_delta;
igt_debug("freq = %"PRIu64"\n", freq);
igt_assert(freq <= gt_max_freq_mhz);
}
igt_debug("clock delta = %"PRIu64"\n", clock_delta);
/* The maximum rate for any HSW counter =
* clock_delta * N EUs
*
* Sanity check that no counters exceed this delta.
*/
max_delta = clock_delta * intel_perf->devinfo.n_eus;
/* 40bit A counters were only introduced for Gen8+ */
igt_assert_eq(format.n_a40, 0);
for (int j = 0; j < format.n_a; j++) {
uint32_t *a0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.a_off);
uint32_t *a1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.a_off);
int a_id = format.first_a + j;
uint32_t delta = a1[j] - a0[j];
if (undefined_a_counters[a_id])
continue;
igt_debug("A%d: delta = %"PRIu32"\n", a_id, delta);
igt_assert(delta <= max_delta);
}
for (int j = 0; j < format.n_b; j++) {
uint32_t *b0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.b_off);
uint32_t *b1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.b_off);
uint32_t delta = b1[j] - b0[j];
igt_debug("B%d: delta = %"PRIu32"\n", j, delta);
igt_assert(delta <= max_delta);
}
for (int j = 0; j < format.n_c; j++) {
uint32_t *c0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.c_off);
uint32_t *c1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.c_off);
uint32_t delta = c1[j] - c0[j];
igt_debug("C%d: delta = %"PRIu32"\n", j, delta);
igt_assert(delta <= max_delta);
}
}
static uint64_t
gen8_read_40bit_a_counter(const uint32_t *report,
enum drm_i915_oa_format fmt, int a_id)
{
struct oa_format format = get_oa_format(fmt);
uint8_t *a40_high = (((uint8_t *)report) + format.a40_high_off);
uint32_t *a40_low = (uint32_t *)(((uint8_t *)report) +
format.a40_low_off);
uint64_t high = (uint64_t)(a40_high[a_id]) << 32;
return a40_low[a_id] | high;
}
static uint64_t
xehpsdv_read_64bit_a_counter(const uint32_t *report, enum drm_i915_oa_format fmt, int a_id)
{
struct oa_format format = get_oa_format(fmt);
uint64_t *a64 = (uint64_t *)(((uint8_t *)report) + format.a64_off);
return a64[a_id];
}
static uint64_t
gen8_40bit_a_delta(uint64_t value0, uint64_t value1)
{
if (value0 > value1)
return (1ULL << 40) + value1 - value0;
else
return value1 - value0;
}
static void
accumulate_uint32(size_t offset,
uint32_t *report0,
uint32_t *report1,
uint64_t *delta)
{
uint32_t value0 = *(uint32_t *)(((uint8_t *)report0) + offset);
uint32_t value1 = *(uint32_t *)(((uint8_t *)report1) + offset);
*delta += (uint32_t)(value1 - value0);
}
static void
accumulate_uint40(int a_index,
uint32_t *report0,
uint32_t *report1,
enum drm_i915_oa_format format,
uint64_t *delta)
{
uint64_t value0 = gen8_read_40bit_a_counter(report0, format, a_index),
value1 = gen8_read_40bit_a_counter(report1, format, a_index);
*delta += gen8_40bit_a_delta(value0, value1);
}
static void
accumulate_uint64(int a_index,
const uint32_t *report0,
const uint32_t *report1,
enum drm_i915_oa_format format,
uint64_t *delta)
{
uint64_t value0 = xehpsdv_read_64bit_a_counter(report0, format, a_index),
value1 = xehpsdv_read_64bit_a_counter(report1, format, a_index);
*delta += (value1 - value0);
}
static void
accumulate_reports(struct accumulator *accumulator,
uint32_t *start,
uint32_t *end)
{
struct oa_format format = get_oa_format(accumulator->format);
uint64_t *deltas = accumulator->deltas;
int idx = 0;
if (intel_gen(devid) >= 8) {
/* timestamp */
deltas[idx] += oa_timestamp_delta(end, start, accumulator->format);
idx++;
/* clock cycles */
deltas[idx] += oa_tick_delta(end, start, accumulator->format);
idx++;
} else {
/* timestamp */
accumulate_uint32(4, start, end, deltas + idx++);
}
for (int i = 0; i < format.n_a40; i++) {
accumulate_uint40(i, start, end, accumulator->format,
deltas + idx++);
}
for (int i = 0; i < format.n_a64; i++) {
accumulate_uint64(i, start, end, accumulator->format,
deltas + idx++);
}
for (int i = 0; i < format.n_a; i++) {
accumulate_uint32(format.a_off + 4 * i,
start, end, deltas + idx++);
}
for (int i = 0; i < format.n_b; i++) {
accumulate_uint32(format.b_off + 4 * i,
start, end, deltas + idx++);
}
for (int i = 0; i < format.n_c; i++) {
accumulate_uint32(format.c_off + 4 * i,
start, end, deltas + idx++);
}
}
static void
accumulator_print(struct accumulator *accumulator, const char *title)
{
struct oa_format format = get_oa_format(accumulator->format);
uint64_t *deltas = accumulator->deltas;
int idx = 0;
igt_debug("%s:\n", title);
if (intel_gen(devid) >= 8) {
igt_debug("\ttime delta = %"PRIu64"\n", deltas[idx++]);
igt_debug("\tclock cycle delta = %"PRIu64"\n", deltas[idx++]);
for (int i = 0; i < format.n_a40; i++)
igt_debug("\tA%u = %"PRIu64"\n", i, deltas[idx++]);
for (int i = 0; i < format.n_a64; i++)
igt_debug("\tA64_%u = %"PRIu64"\n", i, deltas[idx++]);
} else {
igt_debug("\ttime delta = %"PRIu64"\n", deltas[idx++]);
}
for (int i = 0; i < format.n_a; i++) {
int a_id = format.first_a + i;
igt_debug("\tA%u = %"PRIu64"\n", a_id, deltas[idx++]);
}
for (int i = 0; i < format.n_a; i++)
igt_debug("\tB%u = %"PRIu64"\n", i, deltas[idx++]);
for (int i = 0; i < format.n_c; i++)
igt_debug("\tC%u = %"PRIu64"\n", i, deltas[idx++]);
}
/* The TestOa metric set is designed so */
static void
gen8_sanity_check_test_oa_reports(const uint32_t *oa_report0,
const uint32_t *oa_report1,
enum drm_i915_oa_format fmt)
{
struct oa_format format = get_oa_format(fmt);
uint64_t time_delta = timebase_scale(oa_timestamp_delta(oa_report1,
oa_report0,
fmt));
uint64_t clock_delta = oa_tick_delta(oa_report1, oa_report0, fmt);
uint64_t max_delta;
uint64_t freq;
uint32_t *rpt0_b = (uint32_t *)(((uint8_t *)oa_report0) +
format.b_off);
uint32_t *rpt1_b = (uint32_t *)(((uint8_t *)oa_report1) +
format.b_off);
uint32_t b;
uint32_t ref;
igt_debug("report type: %s->%s\n",
gen8_read_report_reason(oa_report0),
gen8_read_report_reason(oa_report1));
freq = time_delta ? (clock_delta * 1000) / time_delta : 0;
igt_debug("freq = %"PRIu64"\n", freq);
igt_debug("clock delta = %"PRIu64"\n", clock_delta);
max_delta = clock_delta * intel_perf->devinfo.n_eus;
/* Gen8+ has some 40bit A counters... */
for (int j = format.first_a40; j < format.n_a40 + format.first_a40; j++) {
uint64_t value0 = gen8_read_40bit_a_counter(oa_report0, fmt, j);
uint64_t value1 = gen8_read_40bit_a_counter(oa_report1, fmt, j);
uint64_t delta = gen8_40bit_a_delta(value0, value1);
if (undefined_a_counters[j])
continue;
igt_debug("A40_%d: delta = %"PRIu64"\n", j, delta);
igt_assert_f(delta <= max_delta,
"A40_%d: delta = %"PRIu64", max_delta = %"PRIu64"\n",
j, delta, max_delta);
}
for (int j = 0; j < format.n_a64; j++) {
uint64_t delta = 0;
accumulate_uint64(j, oa_report0, oa_report1, fmt, &delta);
if (undefined_a_counters[j])
continue;
igt_debug("A64_%d: delta = %"PRIu64"\n", format.first_a + j, delta);
igt_assert_f(delta <= max_delta,
"A64_%d: delta = %"PRIu64", max_delta = %"PRIu64"\n",
format.first_a + j, delta, max_delta);
}
for (int j = 0; j < format.n_a; j++) {
uint32_t *a0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.a_off);
uint32_t *a1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.a_off);
int a_id = format.first_a + j;
uint32_t delta = a1[j] - a0[j];
if (undefined_a_counters[a_id])
continue;
igt_debug("A%d: delta = %"PRIu32"\n", a_id, delta);
igt_assert_f(delta <= max_delta,
"A%d: delta = %"PRIu32", max_delta = %"PRIu64"\n",
a_id, delta, max_delta);
}
/* The TestOa metric set defines all B counters to be a
* multiple of the gpu clock
*/
if (format.n_b && (format.oa_type == OAG || format.oa_type == OAR)) {
if (clock_delta > 0) {
b = rpt1_b[0] - rpt0_b[0];
igt_debug("B0: delta = %"PRIu32"\n", b);
igt_assert_eq(b, 0);
b = rpt1_b[1] - rpt0_b[1];
igt_debug("B1: delta = %"PRIu32"\n", b);
igt_assert_eq(b, clock_delta);
b = rpt1_b[2] - rpt0_b[2];
igt_debug("B2: delta = %"PRIu32"\n", b);
igt_assert_eq(b, clock_delta);
b = rpt1_b[3] - rpt0_b[3];
ref = clock_delta / 2;
igt_debug("B3: delta = %"PRIu32"\n", b);
igt_assert(b >= ref - 1 && b <= ref + 1);
b = rpt1_b[4] - rpt0_b[4];
ref = clock_delta / 3;
igt_debug("B4: delta = %"PRIu32"\n", b);
igt_assert(b >= ref - 1 && b <= ref + 1);
b = rpt1_b[5] - rpt0_b[5];
ref = clock_delta / 3;
igt_debug("B5: delta = %"PRIu32"\n", b);
igt_assert(b >= ref - 1 && b <= ref + 1);
b = rpt1_b[6] - rpt0_b[6];
ref = clock_delta / 6;
igt_debug("B6: delta = %"PRIu32"\n", b);
igt_assert(b >= ref - 1 && b <= ref + 1);
b = rpt1_b[7] - rpt0_b[7];
ref = clock_delta * 2 / 3;
igt_debug("B7: delta = %"PRIu32"\n", b);
igt_assert(b >= ref - 1 && b <= ref + 1);
} else {
for (int j = 0; j < format.n_b; j++) {
b = rpt1_b[j] - rpt0_b[j];
igt_debug("B%i: delta = %"PRIu32"\n", j, b);
igt_assert_eq(b, 0);
}
}
}
for (int j = 0; j < format.n_c; j++) {
uint32_t *c0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.c_off);
uint32_t *c1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.c_off);
uint32_t delta = c1[j] - c0[j];
igt_debug("C%d: delta = %"PRIu32", max_delta=%"PRIu64"\n",
j, delta, max_delta);
igt_assert_f(delta <= max_delta,
"C%d: delta = %"PRIu32", max_delta = %"PRIu64"\n",
j, delta, max_delta);
}
}
static bool
init_sys_info(void)
{
igt_assert_neq(devid, 0);
intel_perf = intel_perf_for_fd(drm_fd, 0);
igt_require(intel_perf);
igt_debug("n_eu_slices: %"PRIu64"\n", intel_perf->devinfo.n_eu_slices);
igt_debug("n_eu_sub_slices: %"PRIu64"\n", intel_perf->devinfo.n_eu_sub_slices);
igt_debug("n_eus: %"PRIu64"\n", intel_perf->devinfo.n_eus);
igt_debug("timestamp_frequency = %"PRIu64"\n",
intel_perf->devinfo.timestamp_frequency);
igt_assert_neq(intel_perf->devinfo.timestamp_frequency, 0);
/* We don't have a TestOa metric set for Haswell so use
* RenderBasic
*/
if (IS_HASWELL(devid)) {
read_report_ticks = hsw_read_report_ticks;
sanity_check_reports = hsw_sanity_check_render_basic_reports;
undefined_a_counters = hsw_undefined_a_counters;
} else {
read_report_ticks = gen8_read_report_ticks;
sanity_check_reports = gen8_sanity_check_test_oa_reports;
undefined_a_counters = gen8_undefined_a_counters;
}
intel_perf_load_perf_configs(intel_perf, drm_fd);
oa_exp_1_millisec = max_oa_exponent_for_period_lte(1000000);
return true;
}
static int
i915_read_reports_until_timestamp(enum drm_i915_oa_format oa_format,
uint8_t *buf,
uint32_t max_size,
uint64_t start_timestamp,
uint64_t end_timestamp)
{
size_t format_size = get_oa_format(oa_format).size;
uint64_t last_seen_timestamp = start_timestamp;
int total_len = 0;
while (last_seen_timestamp < end_timestamp) {
int offset, len;
/* Running out of space. */
if ((max_size - total_len) < format_size) {
igt_warn("run out of space before reaching "
"end timestamp (%"PRIu64"/%"PRIu64")\n",
last_seen_timestamp, end_timestamp);
return -1;
}
while ((len = read(stream_fd, &buf[total_len],
max_size - total_len)) < 0 &&
errno == EINTR)
;
/* Intentionally return an error. */
if (len <= 0) {
if (errno == EAGAIN)
return total_len;
else {
igt_warn("error read OA stream : %i\n", errno);
return -1;
}
}
offset = total_len;
total_len += len;
while (offset < total_len) {
const struct drm_i915_perf_record_header *header =
(const struct drm_i915_perf_record_header *) &buf[offset];
uint32_t *report = (uint32_t *) (header + 1);
if (header->type == DRM_I915_PERF_RECORD_SAMPLE)
last_seen_timestamp = oa_timestamp(report, oa_format);
offset += header->size;
}
}
return total_len;
}
/* CAP_SYS_ADMIN is required to open system wide metrics, unless the system
* control parameter dev.i915.perf_stream_paranoid == 0 */
static void
test_system_wide_paranoid(void)
{
igt_fork(child, 1) {
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_FD_NONBLOCK,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
igt_drop_root();
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EACCES);
}
igt_waitchildren();
igt_fork(child, 1) {
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_FD_NONBLOCK,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 0);
igt_drop_root();
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
}
igt_waitchildren();
/* leave in paranoid state */
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
}
static void
test_invalid_open_flags(void)
{
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
};
struct drm_i915_perf_open_param param = {
.flags = ~0, /* Undefined flag bits set! */
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
}
static void
test_invalid_class_instance(void)
{
uint64_t properties[] = {
DRM_I915_PERF_PROP_SAMPLE_OA, true,
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, 0,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, 0,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
#define OA_E_CLASS 9
#define OA_E_INSTANCE 11
properties[OA_E_CLASS] = I915_ENGINE_CLASS_COPY;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
properties[OA_E_CLASS] = 10;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
properties[OA_E_CLASS] = I915_ENGINE_CLASS_RENDER;
properties[OA_E_INSTANCE] = 100;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
properties[OA_E_INSTANCE] = 248;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
properties[OA_E_CLASS] = default_e2.class;
properties[OA_E_INSTANCE] = default_e2.instance;
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
}
static void
test_invalid_oa_metric_set_id(void)
{
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
DRM_I915_PERF_PROP_OA_METRICS_SET, UINT64_MAX,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_FD_NONBLOCK,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
properties[ARRAY_SIZE(properties) - 1] = 0; /* ID 0 is also be reserved as invalid */
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
/* Check that we aren't just seeing false positives... */
properties[ARRAY_SIZE(properties) - 1] = default_test_set->perf_oa_metrics_set;
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
/* There's no valid default OA metric set ID... */
param.num_properties--;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
}
static void
test_invalid_oa_format_id(void)
{
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
DRM_I915_PERF_PROP_OA_FORMAT, UINT64_MAX,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_FD_NONBLOCK,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
properties[ARRAY_SIZE(properties) - 1] = 0; /* ID 0 is also be reserved as invalid */
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
/* Check that we aren't just seeing false positives... */
properties[ARRAY_SIZE(properties) - 1] = default_test_set->perf_oa_format;
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
/* There's no valid default OA format... */
param.num_properties--;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
}
static void
test_missing_sample_flags(void)
{
uint64_t properties[] = {
/* No _PROP_SAMPLE_xyz flags */
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
}
static void
read_2_oa_reports(int format_id,
int exponent,
uint32_t *oa_report0,
uint32_t *oa_report1,
bool timer_only)
{
size_t format_size = get_oa_format(format_id).size;
size_t sample_size = (sizeof(struct drm_i915_perf_record_header) +
format_size);
const struct drm_i915_perf_record_header *header;
uint32_t exponent_mask = (1 << (exponent + 1)) - 1;
/* Note: we allocate a large buffer so that each read() iteration
* should scrape *all* pending records.
*
* The largest buffer the OA unit supports is 16MB.
*
* Being sure we are fetching all buffered reports allows us to
* potentially throw away / skip all reports whenever we see
* a _REPORT_LOST notification as a way of being sure are
* measurements aren't skewed by a lost report.
*
* Note: that is is useful for some tests but also not something
* applications would be expected to resort to. Lost reports are
* somewhat unpredictable but typically don't pose a problem - except
* to indicate that the OA unit may be over taxed if lots of reports
* are being lost.
*/
int max_reports = MAX_OA_BUF_SIZE / format_size;
int buf_size = sample_size * max_reports * 1.5;
uint8_t *buf = malloc(buf_size);
int n = 0;
for (int i = 0; i < 1000; i++) {
ssize_t len;
while ((len = read(stream_fd, buf, buf_size)) < 0 &&
errno == EINTR)
;
igt_assert(len > 0);
igt_debug("read %d bytes\n", (int)len);
for (size_t offset = 0; offset < len; offset += header->size) {
const uint32_t *report;
header = (void *)(buf + offset);
igt_assert_eq(header->pad, 0); /* Reserved */
/* Currently the only test that should ever expect to
* see a _BUFFER_LOST error is the buffer_fill test,
* otherwise something bad has probably happened...
*/
igt_assert_neq(header->type, DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
/* At high sampling frequencies the OA HW might not be
* able to cope with all write requests and will notify
* us that a report was lost. We restart our read of
* two sequential reports due to the timeline blip this
* implies
*/
if (header->type == DRM_I915_PERF_RECORD_OA_REPORT_LOST) {
igt_debug("read restart: OA trigger collision / report lost\n");
n = 0;
/* XXX: break, because we don't know where
* within the series of already read reports
* there could be a blip from the lost report.
*/
break;
}
/* Currently the only other record type expected is a
* _SAMPLE. Notably this test will need updating if
* i915-perf is extended in the future with additional
* record types.
*/
igt_assert_eq(header->type, DRM_I915_PERF_RECORD_SAMPLE);
igt_assert_eq(header->size, sample_size);
report = (const void *)(header + 1);
dump_report(report, format_size / 4, "oa-formats");
igt_debug("read report: reason = %x, timestamp = %"PRIx64", exponent mask=%x\n",
report[0], oa_timestamp(report, format_id), exponent_mask);
/* Don't expect zero for timestamps */
igt_assert_neq_u64(oa_timestamp(report, format_id), 0);
if (timer_only) {
if (!oa_report_is_periodic(exponent, report)) {
igt_debug("skipping non timer report\n");
continue;
}
}
if (n++ == 0)
memcpy(oa_report0, report, format_size);
else {
memcpy(oa_report1, report, format_size);
free(buf);
return;
}
}
}
free(buf);
igt_assert(!"reached");
}
static void
open_and_read_2_oa_reports(int format_id,
int exponent,
uint32_t *oa_report0,
uint32_t *oa_report1,
bool timer_only,
const struct intel_execution_engine2 *e)
{
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, format_id,
DRM_I915_PERF_PROP_OA_EXPONENT, exponent,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
stream_fd = __perf_open(drm_fd, ¶m, false);
read_2_oa_reports(format_id, exponent,
oa_report0, oa_report1, timer_only);
__perf_close(stream_fd);
}
static void
print_reports(uint32_t *oa_report0, uint32_t *oa_report1, int fmt)
{
struct oa_format format = get_oa_format(fmt);
uint64_t ts0 = oa_timestamp(oa_report0, fmt);
uint64_t ts1 = oa_timestamp(oa_report1, fmt);
igt_debug("TIMESTAMP: 1st = %"PRIu64", 2nd = %"PRIu64", delta = %"PRIu64"\n",
ts0, ts1, ts1 - ts0);
if (IS_HASWELL(devid) && format.n_c == 0) {
igt_debug("CLOCK = N/A\n");
} else {
uint64_t clock0 = read_report_ticks(oa_report0, fmt);
uint64_t clock1 = read_report_ticks(oa_report1, fmt);
igt_debug("CLOCK: 1st = %"PRIu64", 2nd = %"PRIu64", delta = %"PRIu64"\n",
clock0, clock1, clock1 - clock0);
}
if (intel_gen(devid) >= 8) {
uint32_t slice_freq0, slice_freq1, unslice_freq0, unslice_freq1;
const char *reason0 = gen8_read_report_reason(oa_report0);
const char *reason1 = gen8_read_report_reason(oa_report1);
igt_debug("CTX ID: 1st = %"PRIu32", 2nd = %"PRIu32"\n",
oa_report0[2], oa_report1[2]);
gen8_read_report_clock_ratios(oa_report0,
&slice_freq0, &unslice_freq0);
gen8_read_report_clock_ratios(oa_report1,
&slice_freq1, &unslice_freq1);
igt_debug("SLICE CLK: 1st = %umhz, 2nd = %umhz, delta = %d\n",
slice_freq0, slice_freq1,
((int)slice_freq1 - (int)slice_freq0));
igt_debug("UNSLICE CLK: 1st = %umhz, 2nd = %umhz, delta = %d\n",
unslice_freq0, unslice_freq1,
((int)unslice_freq1 - (int)unslice_freq0));
igt_debug("REASONS: 1st = \"%s\", 2nd = \"%s\"\n", reason0, reason1);
}
/* Gen8+ has some 40bit A counters... */
for (int j = 0; j < format.n_a40; j++) {
uint64_t value0 = gen8_read_40bit_a_counter(oa_report0, fmt, j);
uint64_t value1 = gen8_read_40bit_a_counter(oa_report1, fmt, j);
uint64_t delta = gen8_40bit_a_delta(value0, value1);
if (undefined_a_counters[j])
continue;
igt_debug("A%d: 1st = %"PRIu64", 2nd = %"PRIu64", delta = %"PRIu64"\n",
j, value0, value1, delta);
}
for (int j = 0; j < format.n_a64; j++) {
uint64_t value0 = xehpsdv_read_64bit_a_counter(oa_report0, fmt, j);
uint64_t value1 = xehpsdv_read_64bit_a_counter(oa_report1, fmt, j);
uint64_t delta = value1 - value0;
if (undefined_a_counters[j])
continue;
igt_debug("A_64%d: 1st = %"PRIu64", 2nd = %"PRIu64", delta = %"PRIu64"\n",
format.first_a + j, value0, value1, delta);
}
for (int j = 0; j < format.n_a; j++) {
uint32_t *a0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.a_off);
uint32_t *a1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.a_off);
int a_id = format.first_a + j;
uint32_t delta = a1[j] - a0[j];
if (undefined_a_counters[a_id])
continue;
igt_debug("A%d: 1st = %"PRIu32", 2nd = %"PRIu32", delta = %"PRIu32"\n",
a_id, a0[j], a1[j], delta);
}
for (int j = 0; j < format.n_b; j++) {
uint32_t *b0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.b_off);
uint32_t *b1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.b_off);
uint32_t delta = b1[j] - b0[j];
igt_debug("B%d: 1st = %"PRIu32", 2nd = %"PRIu32", delta = %"PRIu32"\n",
j, b0[j], b1[j], delta);
}
for (int j = 0; j < format.n_c; j++) {
uint32_t *c0 = (uint32_t *)(((uint8_t *)oa_report0) +
format.c_off);
uint32_t *c1 = (uint32_t *)(((uint8_t *)oa_report1) +
format.c_off);
uint32_t delta = c1[j] - c0[j];
igt_debug("C%d: 1st = %"PRIu32", 2nd = %"PRIu32", delta = %"PRIu32"\n",
j, c0[j], c1[j], delta);
}
}
/* Debug function, only useful when reports don't make sense. */
#if 0
static void
print_report(uint32_t *report, int fmt)
{
struct oa_format format = get_oa_format(fmt);
igt_debug("TIMESTAMP: %"PRIu64"\n", oa_timestamp(report, fmt));
if (IS_HASWELL(devid) && format.n_c == 0) {
igt_debug("CLOCK = N/A\n");
} else {
uint64_t clock = read_report_ticks(report, fmt);
igt_debug("CLOCK: %"PRIu64"\n", clock);
}
if (intel_gen(devid) >= 8) {
uint32_t slice_freq, unslice_freq;
const char *reason = gen8_read_report_reason(report);
gen8_read_report_clock_ratios(report, &slice_freq, &unslice_freq);
igt_debug("SLICE CLK: %umhz\n", slice_freq);
igt_debug("UNSLICE CLK: %umhz\n", unslice_freq);
igt_debug("REASON: \"%s\"\n", reason);
igt_debug("CTX ID: %"PRIu32"/%"PRIx32"\n", report[2], report[2]);
}
/* Gen8+ has some 40bit A counters... */
for (int j = 0; j < format.n_a40; j++) {
uint64_t value = gen8_read_40bit_a_counter(report, fmt, j);
if (undefined_a_counters[j])
continue;
igt_debug("A%d: %"PRIu64"\n", j, value);
}
for (int j = 0; j < format.n_a; j++) {
uint32_t *a = (uint32_t *)(((uint8_t *)report) +
format.a_off);
int a_id = format.first_a + j;
if (undefined_a_counters[a_id])
continue;
igt_debug("A%d: %"PRIu32"\n", a_id, a[j]);
}
for (int j = 0; j < format.n_b; j++) {
uint32_t *b = (uint32_t *)(((uint8_t *)report) +
format.b_off);
igt_debug("B%d: %"PRIu32"\n", j, b[j]);
}
for (int j = 0; j < format.n_c; j++) {
uint32_t *c = (uint32_t *)(((uint8_t *)report) +
format.c_off);
igt_debug("C%d: %"PRIu32"\n", j, c[j]);
}
}
#endif
static bool
oa_unit_supports_engine(int oa_unit, const struct intel_execution_engine2 *e)
{
switch (oa_unit) {
case OAM:
return e->class == I915_ENGINE_CLASS_VIDEO ||
e->class == I915_ENGINE_CLASS_VIDEO_ENHANCE;
case OAG:
return e->class == I915_ENGINE_CLASS_RENDER;
}
return false;
}
static void
test_oa_formats(const struct intel_execution_engine2 *e)
{
for (int i = 0; i < I915_OA_FORMAT_MAX; i++) {
struct oa_format format = get_oa_format(i);
uint32_t oa_report0[format.size / 4];
uint32_t oa_report1[format.size / 4];
if (!format.name) /* sparse, indexed by ID */
continue;
if (!oa_unit_supports_engine(format.oa_type, e))
continue;
igt_debug("Checking OA format %s\n", format.name);
open_and_read_2_oa_reports(i,
oa_exp_1_millisec,
oa_report0,
oa_report1,
false, /* timer reports only */
e);
print_reports(oa_report0, oa_report1, i);
sanity_check_reports(oa_report0, oa_report1, i);
}
}
enum load {
LOW,
HIGH
};
#define LOAD_HELPER_PAUSE_USEC 500
static struct load_helper {
int devid;
struct buf_ops *bops;
uint32_t context_id;
struct intel_bb *ibb;
enum load load;
bool exit;
struct igt_helper_process igt_proc;
struct intel_buf src, dst;
} lh = { 0, };
static void load_helper_signal_handler(int sig)
{
if (sig == SIGUSR2)
lh.load = lh.load == LOW ? HIGH : LOW;
else
lh.exit = true;
}
static void load_helper_set_load(enum load load)
{
igt_assert(lh.igt_proc.running);
if (lh.load == load)
return;
lh.load = load;
kill(lh.igt_proc.pid, SIGUSR2);
}
static void load_helper_run(enum load load)
{
if (!render_copy)
return;
/*
* FIXME fork helpers won't get cleaned up when started from within a
* subtest, so handle the case where it sticks around a bit too long.
*/
if (lh.igt_proc.running) {
load_helper_set_load(load);
return;
}
lh.load = load;
igt_fork_helper(&lh.igt_proc) {
signal(SIGUSR1, load_helper_signal_handler);
signal(SIGUSR2, load_helper_signal_handler);
while (!lh.exit) {
render_copy(lh.ibb,
&lh.src, 0, 0, 1920, 1080,
&lh.dst, 0, 0);
intel_bb_sync(lh.ibb);
/* Lower the load by pausing after every submitted
* write. */
if (lh.load == LOW)
usleep(LOAD_HELPER_PAUSE_USEC);
}
}
}
static void load_helper_stop(void)
{
if (!render_copy)
return;
kill(lh.igt_proc.pid, SIGUSR1);
igt_assert(igt_wait_helper(&lh.igt_proc) == 0);
}
static void load_helper_init(void)
{
if (!render_copy) {
igt_info("Running test without render_copy\n");
return;
}
lh.devid = intel_get_drm_devid(drm_fd);
/* MI_STORE_DATA can only use GTT address on gen4+/g33 and needs
* snoopable mem on pre-gen6. Hence load-helper only works on gen6+, but
* that's also all we care about for the rps testcase*/
igt_assert(intel_gen(lh.devid) >= 6);
lh.bops = buf_ops_create(drm_fd);
lh.context_id = gem_context_create(drm_fd);
igt_assert_neq(lh.context_id, 0xffffffff);
lh.ibb = intel_bb_create_with_context(drm_fd, lh.context_id, 0, NULL, BATCH_SZ);
scratch_buf_init(lh.bops, &lh.dst, 1920, 1080, 0);
scratch_buf_init(lh.bops, &lh.src, 1920, 1080, 0);
}
static void load_helper_fini(void)
{
int i915;
if (!render_copy)
return;
i915 = buf_ops_get_fd(lh.bops);
if (lh.igt_proc.running)
load_helper_stop();
intel_buf_close(lh.bops, &lh.src);
intel_buf_close(lh.bops, &lh.dst);
intel_bb_destroy(lh.ibb);
gem_context_destroy(i915, lh.context_id);
buf_ops_destroy(lh.bops);
}
static bool expected_report_timing_delta(uint32_t delta, uint32_t expected_delta)
{
/*
* On ICL, the OA unit appears to be a bit more relaxed about
* its timing for emitting OA reports (often missing the
* deadline by 1 timestamp).
*/
if (IS_ICELAKE(devid))
return delta <= (expected_delta + 3);
else
return delta <= expected_delta;
}
static void
test_oa_exponents(const struct intel_execution_engine2 *e)
{
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t fmt = test_set->perf_oa_format;
load_helper_init();
load_helper_run(HIGH);
/* It's asking a lot to sample with a 160 nanosecond period and the
* test can fail due to buffer overflows if it wasn't possible to
* keep up, so we don't start from an exponent of zero...
*/
for (int exponent = 5; exponent < 20; exponent++) {
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, fmt,
DRM_I915_PERF_PROP_OA_EXPONENT, exponent,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
uint64_t expected_timestamp_delta = 2ULL << exponent;
size_t format_size = get_oa_format(fmt).size;
size_t sample_size = (sizeof(struct drm_i915_perf_record_header) +
format_size);
int max_reports = MAX_OA_BUF_SIZE / format_size;
int buf_size = sample_size * max_reports * 1.5;
uint8_t *buf = calloc(1, buf_size);
int ret, n_timer_reports = 0;
uint32_t matches = 0;
#define NUM_TIMER_REPORTS 30
uint32_t *reports = malloc(NUM_TIMER_REPORTS * format_size);
uint32_t *timer_reports = reports;
void *this, *prev;
igt_debug("testing OA exponent %d,"
" expected ts delta = %"PRIu64" (%"PRIu64"ns/%.2fus/%.2fms)\n",
exponent, expected_timestamp_delta,
oa_exponent_to_ns(exponent),
oa_exponent_to_ns(exponent) / 1000.0,
oa_exponent_to_ns(exponent) / (1000.0 * 1000.0));
stream_fd = __perf_open(drm_fd, ¶m, true /* prevent_pm */);
while (n_timer_reports < NUM_TIMER_REPORTS) {
struct drm_i915_perf_record_header *header;
while ((ret = read(stream_fd, buf, buf_size)) < 0 &&
errno == EINTR)
;
/* igt_debug(" > read %i bytes\n", ret); */
/* We should never have no data. */
igt_assert_lt(0, ret);
for (int offset = 0;
offset < ret && n_timer_reports < NUM_TIMER_REPORTS;
offset += header->size) {
uint32_t *report;
header = (void *)(buf + offset);
if (header->type == DRM_I915_PERF_RECORD_OA_BUFFER_LOST) {
igt_assert(!"reached");
break;
}
if (header->type == DRM_I915_PERF_RECORD_OA_REPORT_LOST)
igt_debug("report loss\n");
if (header->type != DRM_I915_PERF_RECORD_SAMPLE)
continue;
report = (void *)(header + 1);
if (!oa_report_is_periodic(exponent, report))
continue;
memcpy(timer_reports, report, format_size);
n_timer_reports++;
timer_reports += (format_size / 4);
}
}
__perf_close(stream_fd);
this = reports + format_size / 4;
prev = reports;
igt_debug("report%04i ts=%"PRIx64" hw_id=0x%08x\n", 0,
oa_timestamp(prev, fmt),
oa_report_get_ctx_id(prev));
for (int i = 1; i < n_timer_reports; i++) {
uint64_t delta = oa_timestamp_delta(this, prev, fmt);
igt_debug("report%04i ts=%"PRIx64" hw_id=0x%08x delta=%"PRIu64" %s\n", i,
oa_timestamp(this, fmt),
oa_report_get_ctx_id(this),
delta, expected_report_timing_delta(delta,
expected_timestamp_delta) ? "" : "******");
matches += expected_report_timing_delta(delta,expected_timestamp_delta);
this += format_size;
prev += format_size;
}
igt_debug("matches=%u/%u\n", matches, n_timer_reports - 1);
/*
* Expect half the reports to match the timing
* expectation. The results are quite erratic because
* the condition under which the HW reaches
* expectations depends on memory controller pressure
* etc...
*/
igt_assert_lte(n_timer_reports / 2, matches);
free(reports);
}
load_helper_stop();
load_helper_fini();
}
/* The OA exponent selects a timestamp counter bit to trigger reports on.
*
* With a 64bit timestamp and least significant bit approx == 80ns then the MSB
* equates to > 40 thousand years and isn't exposed via the i915 perf interface.
*
* The max exponent exposed is expected to be 31, which is still a fairly
* ridiculous period (>5min) but is the maximum exponent where it's still
* possible to use periodic sampling as a means for tracking the overflow of
* 32bit OA report timestamps.
*/
static void
test_invalid_oa_exponent(void)
{
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, 31, /* maximum exponent expected
to be accepted */
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
for (int i = 32; i < 65; i++) {
properties[7] = i;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
}
}
/* The lowest periodic sampling exponent equates to a period of 160 nanoseconds
* or a frequency of 6.25MHz which is only possible to request as root by
* default. By default the maximum OA sampling rate is 100KHz
*/
static void
test_low_oa_exponent_permissions(void)
{
int max_freq = read_u64_file("/proc/sys/dev/i915/oa_max_sample_rate");
int bad_exponent = max_oa_exponent_for_freq_gt(max_freq);
int ok_exponent = bad_exponent + 1;
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, bad_exponent,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
uint64_t oa_period, oa_freq;
igt_assert_eq(max_freq, 100000);
/* Avoid EACCES errors opening a stream without CAP_SYS_ADMIN */
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 0);
igt_fork(child, 1) {
igt_drop_root();
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EACCES);
}
igt_waitchildren();
properties[7] = ok_exponent;
igt_fork(child, 1) {
igt_drop_root();
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
}
igt_waitchildren();
oa_period = timebase_scale(2 << ok_exponent);
oa_freq = NSEC_PER_SEC / oa_period;
write_u64_file("/proc/sys/dev/i915/oa_max_sample_rate", oa_freq - 100);
igt_fork(child, 1) {
igt_drop_root();
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EACCES);
}
igt_waitchildren();
/* restore the defaults */
write_u64_file("/proc/sys/dev/i915/oa_max_sample_rate", 100000);
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
}
static void
test_per_context_mode_unprivileged(void)
{
uint64_t properties[] = {
/* Single context sampling */
DRM_I915_PERF_PROP_CTX_HANDLE, UINT64_MAX, /* updated below */
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
/* should be default, but just to be sure... */
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
igt_fork(child, 1) {
uint32_t ctx_id = 0xffffffff; /* invalid id */
igt_drop_root();
ctx_id = gem_context_create(drm_fd);
igt_assert_neq(ctx_id, 0xffffffff);
properties[1] = ctx_id;
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
gem_context_destroy(drm_fd, ctx_id);
}
igt_waitchildren();
}
static int64_t
get_time(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000 + ts.tv_nsec;
}
/* Note: The interface doesn't currently provide strict guarantees or control
* over the upper bound for how long it might take for a POLLIN event after
* some OA report is written by the OA unit.
*
* The plan is to add a property later that gives some control over the maximum
* latency, but for now we expect it is tuned for a fairly low latency
* suitable for applications wanting to provide live feedback for captured
* metrics.
*
* At the time of writing this test the driver was using a fixed 200Hz hrtimer
* regardless of the OA sampling exponent.
*
* There is no lower bound since a stream configured for periodic sampling may
* still contain other automatically triggered reports.
*
* What we try and check for here is that blocking reads don't return EAGAIN
* and that we aren't spending any significant time burning the cpu in
* kernelspace.
*/
static void
test_blocking(uint64_t requested_oa_period,
bool set_kernel_hrtimer,
uint64_t kernel_hrtimer,
const struct intel_execution_engine2 *e)
{
int oa_exponent = max_oa_exponent_for_period_lte(requested_oa_period);
uint64_t oa_period = oa_exponent_to_ns(oa_exponent);
uint64_t props[DRM_I915_PERF_PROP_MAX * 2];
uint64_t *idx = props;
struct drm_i915_perf_open_param param;
uint8_t buf[1024 * 1024];
struct tms start_times;
struct tms end_times;
int64_t user_ns, kernel_ns;
int64_t tick_ns = 1000000000 / sysconf(_SC_CLK_TCK);
int64_t test_duration_ns = tick_ns * 1000;
int max_iterations = (test_duration_ns / oa_period) + 2;
int n_extra_iterations = 0;
int perf_fd;
/* It's a bit tricky to put a lower limit here, but we expect a
* relatively low latency for seeing reports, while we don't currently
* give any control over this in the api.
*
* We assume a maximum latency of 6 millisecond to deliver a POLLIN and
* read() after a new sample is written (46ms per iteration) considering
* the knowledge that that the driver uses a 200Hz hrtimer (5ms period)
* to check for data and giving some time to read().
*/
int min_iterations = (test_duration_ns / (oa_period + kernel_hrtimer + kernel_hrtimer / 5));
int64_t start, end;
int n = 0;
struct intel_perf_metric_set *test_set = metric_set(e);
ADD_PROPS(props, idx, SAMPLE_OA, true);
ADD_PROPS(props, idx, OA_METRICS_SET, test_set->perf_oa_metrics_set);
ADD_PROPS(props, idx, OA_FORMAT, test_set->perf_oa_format);
ADD_PROPS(props, idx, OA_EXPONENT, oa_exponent);
if (has_param_poll_period() && set_kernel_hrtimer)
ADD_PROPS(props, idx, POLL_OA_PERIOD, kernel_hrtimer);
if (has_param_class_instance()) {
ADD_PROPS(props, idx, OA_ENGINE_CLASS, e->class);
ADD_PROPS(props, idx, OA_ENGINE_INSTANCE, e->instance);
}
param.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED;
param.num_properties = (idx - props) / 2;
param.properties_ptr = to_user_pointer(props);
perf_fd = __perf_open(drm_fd, ¶m, true /* prevent_pm */);
times(&start_times);
igt_debug("tick length = %dns, test duration = %"PRIu64"ns, min iter. = %d,"
" estimated max iter. = %d, oa_period = %s\n",
(int)tick_ns, test_duration_ns,
min_iterations, max_iterations,
pretty_print_oa_period(oa_period));
/* In the loop we perform blocking polls while the HW is sampling at
* ~25Hz, with the expectation that we spend most of our time blocked
* in the kernel, and shouldn't be burning cpu cycles in the kernel in
* association with this process (verified by looking at stime before
* and after loop).
*
* We're looking to assert that less than 1% of the test duration is
* spent in the kernel dealing with polling and read()ing.
*
* The test runs for a relatively long time considering the very low
* resolution of stime in ticks of typically 10 milliseconds. Since we
* don't know the fractional part of tick values we read from userspace
* so our minimum threshold needs to be >= one tick since any
* measurement might really be +- tick_ns (assuming we effectively get
* floor(real_stime)).
*
* We Loop for 1000 x tick_ns so one tick corresponds to 0.1%
*
* Also enable the stream just before poll/read to minimize
* the error delta.
*/
start = get_time();
do_ioctl(perf_fd, I915_PERF_IOCTL_ENABLE, 0);
for (/* nop */; ((end = get_time()) - start) < test_duration_ns; /* nop */) {
struct drm_i915_perf_record_header *header;
bool timer_report_read = false;
bool non_timer_report_read = false;
int ret;
while ((ret = read(perf_fd, buf, sizeof(buf))) < 0 &&
errno == EINTR)
;
igt_assert_lt(0, ret);
/* For Haswell reports don't contain a well defined reason
* field we so assume all reports to be 'periodic'. For gen8+
* we want to to consider that the HW automatically writes some
* non periodic reports (e.g. on context switch) which might
* lead to more successful read()s than expected due to
* periodic sampling and we don't want these extra reads to
* cause the test to fail...
*/
if (intel_gen(devid) >= 8) {
for (int offset = 0; offset < ret; offset += header->size) {
header = (void *)(buf + offset);
if (header->type == DRM_I915_PERF_RECORD_SAMPLE) {
uint32_t *report = (void *)(header + 1);
if (oa_report_is_periodic(oa_exponent,
report))
timer_report_read = true;
else
non_timer_report_read = true;
}
}
}
if (non_timer_report_read && !timer_report_read)
n_extra_iterations++;
n++;
}
times(&end_times);
/* Using nanosecond units is fairly silly here, given the tick in-
* precision - ah well, it's consistent with the get_time() units.
*/
user_ns = (end_times.tms_utime - start_times.tms_utime) * tick_ns;
kernel_ns = (end_times.tms_stime - start_times.tms_stime) * tick_ns;
igt_debug("%d blocking reads during test with %"PRIu64" Hz OA sampling (expect no more than %d)\n",
n, NSEC_PER_SEC / oa_period, max_iterations);
igt_debug("%d extra iterations seen, not related to periodic sampling (e.g. context switches)\n",
n_extra_iterations);
igt_debug("time in userspace = %"PRIu64"ns (+-%dns) (start utime = %d, end = %d)\n",
user_ns, (int)tick_ns,
(int)start_times.tms_utime, (int)end_times.tms_utime);
igt_debug("time in kernelspace = %"PRIu64"ns (+-%dns) (start stime = %d, end = %d)\n",
kernel_ns, (int)tick_ns,
(int)start_times.tms_stime, (int)end_times.tms_stime);
/* With completely broken blocking (but also not returning an error) we
* could end up with an open loop,
*/
igt_assert_lte(n, (max_iterations + n_extra_iterations));
/* Make sure the driver is reporting new samples with a reasonably
* low latency...
*/
igt_assert_lt((min_iterations + n_extra_iterations), n);
if (!set_kernel_hrtimer)
igt_assert(kernel_ns <= (test_duration_ns / 100ull));
__perf_close(perf_fd);
}
static void
test_polling(uint64_t requested_oa_period,
bool set_kernel_hrtimer,
uint64_t kernel_hrtimer,
const struct intel_execution_engine2 *e)
{
int oa_exponent = max_oa_exponent_for_period_lte(requested_oa_period);
uint64_t oa_period = oa_exponent_to_ns(oa_exponent);
uint64_t props[DRM_I915_PERF_PROP_MAX * 2];
uint64_t *idx = props;
struct drm_i915_perf_open_param param;
uint8_t buf[1024 * 1024];
struct tms start_times;
struct tms end_times;
int64_t user_ns, kernel_ns;
int64_t tick_ns = 1000000000 / sysconf(_SC_CLK_TCK);
int64_t test_duration_ns = tick_ns * 1000;
int max_iterations = (test_duration_ns / oa_period) + 2;
int n_extra_iterations = 0;
/* It's a bit tricky to put a lower limit here, but we expect a
* relatively low latency for seeing reports.
*
* We assume a maximum latency of kernel_hrtimer + some margin
* to deliver a POLLIN and read() after a new sample is
* written (40ms + hrtimer + margin per iteration) considering
* the knowledge that that the driver uses a 200Hz hrtimer
* (5ms period) to check for data and giving some time to
* read().
*/
int min_iterations = (test_duration_ns / (oa_period + (kernel_hrtimer + kernel_hrtimer / 5)));
int64_t start, end;
int n = 0;
struct intel_perf_metric_set *test_set = metric_set(e);
ADD_PROPS(props, idx, SAMPLE_OA, true);
ADD_PROPS(props, idx, OA_METRICS_SET, test_set->perf_oa_metrics_set);
ADD_PROPS(props, idx, OA_FORMAT, test_set->perf_oa_format);
ADD_PROPS(props, idx, OA_EXPONENT, oa_exponent);
if (has_param_poll_period() && set_kernel_hrtimer)
ADD_PROPS(props, idx, POLL_OA_PERIOD, kernel_hrtimer);
if (has_param_class_instance()) {
ADD_PROPS(props, idx, OA_ENGINE_CLASS, e->class);
ADD_PROPS(props, idx, OA_ENGINE_INSTANCE, e->instance);
}
param.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED |
I915_PERF_FLAG_FD_NONBLOCK;
param.num_properties = (idx - props) / 2;
param.properties_ptr = to_user_pointer(props);
stream_fd = __perf_open(drm_fd, ¶m, true /* prevent_pm */);
times(&start_times);
igt_debug("tick length = %dns, oa period = %s, "
"test duration = %"PRIu64"ns, min iter. = %d, max iter. = %d\n",
(int)tick_ns, pretty_print_oa_period(oa_period), test_duration_ns,
min_iterations, max_iterations);
/* In the loop we perform blocking polls while the HW is sampling at
* ~25Hz, with the expectation that we spend most of our time blocked
* in the kernel, and shouldn't be burning cpu cycles in the kernel in
* association with this process (verified by looking at stime before
* and after loop).
*
* We're looking to assert that less than 1% of the test duration is
* spent in the kernel dealing with polling and read()ing.
*
* The test runs for a relatively long time considering the very low
* resolution of stime in ticks of typically 10 milliseconds. Since we
* don't know the fractional part of tick values we read from userspace
* so our minimum threshold needs to be >= one tick since any
* measurement might really be +- tick_ns (assuming we effectively get
* floor(real_stime)).
*
* We Loop for 1000 x tick_ns so one tick corresponds to 0.1%
*
* Also enable the stream just before poll/read to minimize
* the error delta.
*/
start = get_time();
do_ioctl(stream_fd, I915_PERF_IOCTL_ENABLE, 0);
for (/* nop */; ((end = get_time()) - start) < test_duration_ns; /* nop */) {
struct pollfd pollfd = { .fd = stream_fd, .events = POLLIN };
struct drm_i915_perf_record_header *header;
bool timer_report_read = false;
bool non_timer_report_read = false;
int ret;
while ((ret = poll(&pollfd, 1, -1)) < 0 &&
errno == EINTR)
;
igt_assert_eq(ret, 1);
igt_assert(pollfd.revents & POLLIN);
while ((ret = read(stream_fd, buf, sizeof(buf))) < 0 &&
errno == EINTR)
;
/* Don't expect to see EAGAIN if we've had a POLLIN event
*
* XXX: actually this is technically overly strict since we do
* knowingly allow false positive POLLIN events. At least in
* the future when supporting context filtering of metrics for
* Gen8+ handled in the kernel then POLLIN events may be
* delivered when we know there are pending reports to process
* but before we've done any filtering to know for certain that
* any reports are destined to be copied to userspace.
*
* Still, for now it's a reasonable sanity check.
*/
if (ret < 0)
igt_debug("Unexpected error when reading after poll = %d\n", errno);
igt_assert_neq(ret, -1);
/* For Haswell reports don't contain a well defined reason
* field we so assume all reports to be 'periodic'. For gen8+
* we want to to consider that the HW automatically writes some
* non periodic reports (e.g. on context switch) which might
* lead to more successful read()s than expected due to
* periodic sampling and we don't want these extra reads to
* cause the test to fail...
*/
if (intel_gen(devid) >= 8) {
for (int offset = 0; offset < ret; offset += header->size) {
header = (void *)(buf + offset);
if (header->type == DRM_I915_PERF_RECORD_SAMPLE) {
uint32_t *report = (void *)(header + 1);
if (oa_report_is_periodic(oa_exponent, report))
timer_report_read = true;
else
non_timer_report_read = true;
}
}
}
if (non_timer_report_read && !timer_report_read)
n_extra_iterations++;
/* At this point, after consuming pending reports (and hoping
* the scheduler hasn't stopped us for too long) we now expect
* EAGAIN on read. While this works most of the times, there are
* some rare failures when the OA period passed to this test is
* very small (say 500 us) and that results in some valid
* reports here. To weed out those rare occurences we assert
* only if the OA period is >= 40 ms because 40 ms has withstood
* the test of time on most platforms (ref: subtest: polling).
*/
while ((ret = read(stream_fd, buf, sizeof(buf))) < 0 &&
errno == EINTR)
;
if (requested_oa_period >= 40000000) {
igt_assert_eq(ret, -1);
igt_assert_eq(errno, EAGAIN);
}
n++;
}
times(&end_times);
/* Using nanosecond units is fairly silly here, given the tick in-
* precision - ah well, it's consistent with the get_time() units.
*/
user_ns = (end_times.tms_utime - start_times.tms_utime) * tick_ns;
kernel_ns = (end_times.tms_stime - start_times.tms_stime) * tick_ns;
igt_debug("%d non-blocking reads during test with %"PRIu64" Hz OA sampling (expect no more than %d)\n",
n, NSEC_PER_SEC / oa_period, max_iterations);
igt_debug("%d extra iterations seen, not related to periodic sampling (e.g. context switches)\n",
n_extra_iterations);
igt_debug("time in userspace = %"PRIu64"ns (+-%dns) (start utime = %d, end = %d)\n",
user_ns, (int)tick_ns,
(int)start_times.tms_utime, (int)end_times.tms_utime);
igt_debug("time in kernelspace = %"PRIu64"ns (+-%dns) (start stime = %d, end = %d)\n",
kernel_ns, (int)tick_ns,
(int)start_times.tms_stime, (int)end_times.tms_stime);
/* With completely broken blocking while polling (but still somehow
* reporting a POLLIN event) we could end up with an open loop.
*/
igt_assert_lte(n, (max_iterations + n_extra_iterations));
/* Make sure the driver is reporting new samples with a reasonably
* low latency...
*/
igt_assert_lt((min_iterations + n_extra_iterations), n);
if (!set_kernel_hrtimer)
igt_assert(kernel_ns <= (test_duration_ns / 100ull));
__perf_close(stream_fd);
}
static void test_polling_small_buf(void)
{
int oa_exponent = max_oa_exponent_for_period_lte(40 * 1000); /* 40us */
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED |
I915_PERF_FLAG_FD_NONBLOCK,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
uint32_t test_duration = 80 * 1000 * 1000;
int sample_size = (sizeof(struct drm_i915_perf_record_header) +
get_oa_format(default_test_set->perf_oa_format).size);
int n_expected_reports = test_duration / oa_exponent_to_ns(oa_exponent);
int n_expect_read_bytes = n_expected_reports * sample_size;
struct timespec ts = {};
int n_bytes_read = 0;
uint32_t n_polls = 0;
stream_fd = __perf_open(drm_fd, ¶m, true /* prevent_pm */);
do_ioctl(stream_fd, I915_PERF_IOCTL_ENABLE, 0);
while (igt_nsec_elapsed(&ts) < test_duration) {
struct pollfd pollfd = { .fd = stream_fd, .events = POLLIN };
ppoll(&pollfd, 1, NULL, NULL);
if (pollfd.revents & POLLIN) {
uint8_t buf[1024];
int ret;
ret = read(stream_fd, buf, sizeof(buf));
if (ret >= 0)
n_bytes_read += ret;
}
n_polls++;
}
igt_info("Read %d expected %d (%.2f%% of the expected number), polls=%u\n",
n_bytes_read, n_expect_read_bytes,
n_bytes_read * 100.0f / n_expect_read_bytes,
n_polls);
__perf_close(stream_fd);
igt_assert(abs(n_expect_read_bytes - n_bytes_read) <
0.20 * n_expect_read_bytes);
}
static int
num_valid_reports_captured(struct drm_i915_perf_open_param *param,
int64_t *duration_ns)
{
uint8_t buf[1024 * 1024];
int64_t start, end;
int num_reports = 0;
igt_debug("Expected duration = %"PRId64"\n", *duration_ns);
stream_fd = __perf_open(drm_fd, param, true);
start = get_time();
do_ioctl(stream_fd, I915_PERF_IOCTL_ENABLE, 0);
for (/* nop */; ((end = get_time()) - start) < *duration_ns; /* nop */) {
struct drm_i915_perf_record_header *header;
int ret;
while ((ret = read(stream_fd, buf, sizeof(buf))) < 0 &&
errno == EINTR)
;
igt_assert_lt(0, ret);
for (int offset = 0; offset < ret; offset += header->size) {
header = (void *)(buf + offset);
if (header->type == DRM_I915_PERF_RECORD_SAMPLE) {
uint32_t *report = (void *)(header + 1);
if (gen8_report_reason(report) & OAREPORT_REASON_TIMER)
num_reports++;
}
}
}
__perf_close(stream_fd);
*duration_ns = end - start;
igt_debug("Actual duration = %"PRIu64"\n", *duration_ns);
return num_reports;
}
static void
gen12_test_oa_tlb_invalidate(const struct intel_execution_engine2 *e)
{
int oa_exponent = max_oa_exponent_for_period_lte(30000000);
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t properties[] = {
DRM_I915_PERF_PROP_SAMPLE_OA, true,
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED,
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
int num_reports1, num_reports2, num_expected_reports;
int64_t duration;
/* Capture reports for 5 seconds twice and then make sure you get around
* the same number of reports. In the case of failure, the number of
* reports will vary largely since the beginning of the OA buffer
* will have invalid entries.
*/
duration = 5LL * NSEC_PER_SEC;
num_reports1 = num_valid_reports_captured(¶m, &duration);
num_expected_reports = duration / oa_exponent_to_ns(oa_exponent);
igt_debug("expected num reports = %d\n", num_expected_reports);
igt_debug("actual num reports = %d\n", num_reports1);
igt_assert(num_reports1 > 0.95 * num_expected_reports);
duration = 5LL * NSEC_PER_SEC;
num_reports2 = num_valid_reports_captured(¶m, &duration);
num_expected_reports = duration / oa_exponent_to_ns(oa_exponent);
igt_debug("expected num reports = %d\n", num_expected_reports);
igt_debug("actual num reports = %d\n", num_reports2);
igt_assert(num_reports2 > 0.95 * num_expected_reports);
}
static void
test_buffer_fill(const struct intel_execution_engine2 *e)
{
/* ~5 micro second period */
int oa_exponent = max_oa_exponent_for_period_lte(5000);
uint64_t oa_period = oa_exponent_to_ns(oa_exponent);
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t fmt = test_set->perf_oa_format;
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, fmt,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
struct drm_i915_perf_record_header *header;
size_t report_size = get_oa_format(fmt).size;
int buf_size = 65536 * (report_size + sizeof(struct drm_i915_perf_record_header));
uint8_t *buf = malloc(buf_size);
int len;
size_t oa_buf_size = MAX_OA_BUF_SIZE;
int n_full_oa_reports = oa_buf_size / report_size;
uint64_t fill_duration = n_full_oa_reports * oa_period;
uint32_t *last_periodic_report = malloc(report_size);
igt_assert(fill_duration < 1000000000);
stream_fd = __perf_open(drm_fd, ¶m, true /* prevent_pm */);
for (int i = 0; i < 5; i++) {
bool overflow_seen;
uint32_t n_periodic_reports;
uint32_t first_timestamp = 0, last_timestamp = 0;
do_ioctl(stream_fd, I915_PERF_IOCTL_ENABLE, 0);
nanosleep(&(struct timespec){ .tv_sec = 0,
.tv_nsec = fill_duration * 1.25 },
NULL);
while ((len = read(stream_fd, buf, buf_size)) == -1 && errno == EINTR)
;
igt_assert_neq(len, -1);
overflow_seen = false;
for (int offset = 0; offset < len; offset += header->size) {
header = (void *)(buf + offset);
if (header->type == DRM_I915_PERF_RECORD_OA_BUFFER_LOST)
overflow_seen = true;
}
igt_assert_eq(overflow_seen, true);
do_ioctl(stream_fd, I915_PERF_IOCTL_DISABLE, 0);
igt_debug("fill_duration = %"PRIu64"ns, oa_exponent = %u\n",
fill_duration, oa_exponent);
do_ioctl(stream_fd, I915_PERF_IOCTL_ENABLE, 0);
nanosleep(&(struct timespec){ .tv_sec = 0,
.tv_nsec = fill_duration / 2 },
NULL);
n_periodic_reports = 0;
/* Because of the race condition between notification of new
* reports and reports landing in memory, we need to rely on
* timestamps to figure whether we've read enough of them.
*/
while (((last_timestamp - first_timestamp) * oa_period) < (fill_duration / 2)) {
igt_debug("dts=%u elapsed=%"PRIu64" duration=%"PRIu64"\n",
last_timestamp - first_timestamp,
(last_timestamp - first_timestamp) * oa_period,
fill_duration / 2);
while ((len = read(stream_fd, buf, buf_size)) == -1 && errno == EINTR)
;
igt_assert_neq(len, -1);
for (int offset = 0; offset < len; offset += header->size) {
uint32_t *report;
header = (void *) (buf + offset);
report = (void *) (header + 1);
switch (header->type) {
case DRM_I915_PERF_RECORD_OA_REPORT_LOST:
igt_debug("report loss, trying again\n");
break;
case DRM_I915_PERF_RECORD_SAMPLE:
igt_debug(" > report ts=%"PRIu64""
" ts_delta_last_periodic=%"PRIu64" is_timer=%i ctx_id=%8x nb_periodic=%u\n",
oa_timestamp(report, fmt),
n_periodic_reports > 0 ? oa_timestamp_delta(report, last_periodic_report, fmt) : 0,
oa_report_is_periodic(oa_exponent, report),
oa_report_get_ctx_id(report),
n_periodic_reports);
if (first_timestamp == 0)
first_timestamp = oa_timestamp(report, fmt);
last_timestamp = oa_timestamp(report, fmt);
if (((last_timestamp - first_timestamp) * oa_period) < (fill_duration / 2))
break;
if (oa_report_is_periodic(oa_exponent, report)) {
memcpy(last_periodic_report, report, report_size);
n_periodic_reports++;
}
break;
case DRM_I915_PERF_RECORD_OA_BUFFER_LOST:
igt_assert(!"unexpected overflow");
break;
}
}
}
do_ioctl(stream_fd, I915_PERF_IOCTL_DISABLE, 0);
igt_debug("first ts = %u, last ts = %u\n", first_timestamp, last_timestamp);
igt_debug("%f < %zu < %f\n",
report_size * n_full_oa_reports * 0.45,
n_periodic_reports * report_size,
report_size * n_full_oa_reports * 0.55);
igt_assert(n_periodic_reports * report_size >
report_size * n_full_oa_reports * 0.45);
igt_assert(n_periodic_reports * report_size <
report_size * n_full_oa_reports * 0.55);
}
free(last_periodic_report);
free(buf);
__perf_close(stream_fd);
}
static void
test_non_zero_reason(const struct intel_execution_engine2 *e)
{
/* ~20 micro second period */
int oa_exponent = max_oa_exponent_for_period_lte(20000);
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t fmt = test_set->perf_oa_format;
size_t report_size = get_oa_format(fmt).size;
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, fmt,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
struct drm_i915_perf_record_header *header;
uint32_t buf_size = 3 * 65536 * (report_size + sizeof(struct drm_i915_perf_record_header));
uint8_t *buf = malloc(buf_size);
uint32_t total_len = 0, reports_lost;
const uint32_t *last_report;
int len;
igt_assert(buf);
igt_debug("Ready to read about %u bytes\n", buf_size);
load_helper_init();
load_helper_run(HIGH);
stream_fd = __perf_open(drm_fd, ¶m, true /* prevent_pm */);
while (total_len < (buf_size - sizeof(struct drm_i915_perf_record_header)) &&
((len = read(stream_fd, &buf[total_len], buf_size - total_len)) > 0 ||
(len == -1 && errno == EINTR))) {
if (len > 0)
total_len += len;
}
__perf_close(stream_fd);
load_helper_stop();
load_helper_fini();
igt_debug("Got %u bytes\n", total_len);
last_report = NULL;
reports_lost = 0;
for (uint32_t offset = 0; offset < total_len; offset += header->size) {
header = (void *) (buf + offset);
switch (header->type) {
case DRM_I915_PERF_RECORD_OA_REPORT_LOST:
reports_lost++;
break;
case DRM_I915_PERF_RECORD_SAMPLE: {
const uint32_t *report = (void *) (header + 1);
uint32_t reason = (report[0] >> OAREPORT_REASON_SHIFT) &
OAREPORT_REASON_MASK;
igt_assert_neq(reason, 0);
if (last_report)
sanity_check_reports(last_report, report, fmt);
last_report = report;
break;
}
case DRM_I915_PERF_RECORD_OA_BUFFER_LOST:
igt_assert(!"unexpected overflow");
break;
}
}
igt_debug("Got %u report lost events\n", reports_lost);
free(buf);
}
static void
test_enable_disable(const struct intel_execution_engine2 *e)
{
/* ~5 micro second period */
int oa_exponent = max_oa_exponent_for_period_lte(5000);
uint64_t oa_period = oa_exponent_to_ns(oa_exponent);
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t fmt = test_set->perf_oa_format;
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, fmt,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED, /* Verify we start disabled */
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
size_t report_size = get_oa_format(fmt).size;
int buf_size = 65536 * (report_size + sizeof(struct drm_i915_perf_record_header));
uint8_t *buf = malloc(buf_size);
size_t oa_buf_size = MAX_OA_BUF_SIZE;
int n_full_oa_reports = oa_buf_size / report_size;
uint64_t fill_duration = n_full_oa_reports * oa_period;
uint32_t *last_periodic_report = malloc(report_size);
load_helper_init();
load_helper_run(HIGH);
stream_fd = __perf_open(drm_fd, ¶m, true /* prevent_pm */);
for (int i = 0; i < 5; i++) {
int len;
uint32_t n_periodic_reports;
struct drm_i915_perf_record_header *header;
uint64_t first_timestamp = 0, last_timestamp = 0;
/* Giving enough time for an overflow might help catch whether
* the OA unit has been enabled even if the driver might at
* least avoid copying reports while disabled.
*/
nanosleep(&(struct timespec){ .tv_sec = 0,
.tv_nsec = fill_duration * 1.25 },
NULL);
while ((len = read(stream_fd, buf, buf_size)) == -1 && errno == EINTR)
;
igt_assert_eq(len, -1);
igt_assert_eq(errno, EIO);
do_ioctl(stream_fd, I915_PERF_IOCTL_ENABLE, 0);
nanosleep(&(struct timespec){ .tv_sec = 0,
.tv_nsec = fill_duration / 2 },
NULL);
n_periodic_reports = 0;
/* Because of the race condition between notification of new
* reports and reports landing in memory, we need to rely on
* timestamps to figure whether we've read enough of them.
*/
while (((last_timestamp - first_timestamp) * oa_period) < (fill_duration / 2)) {
while ((len = read(stream_fd, buf, buf_size)) == -1 && errno == EINTR)
;
igt_assert_neq(len, -1);
for (int offset = 0; offset < len; offset += header->size) {
uint32_t *report;
header = (void *) (buf + offset);
report = (void *) (header + 1);
switch (header->type) {
case DRM_I915_PERF_RECORD_OA_REPORT_LOST:
break;
case DRM_I915_PERF_RECORD_SAMPLE:
if (first_timestamp == 0)
first_timestamp = oa_timestamp(report, fmt);
last_timestamp = oa_timestamp(report, fmt);
igt_debug(" > report ts=%"PRIx64""
" ts_delta_last_periodic=%s%"PRIu64""
" is_timer=%i ctx_id=0x%8x\n",
oa_timestamp(report, fmt),
oa_report_is_periodic(oa_exponent, report) ? " " : "*",
n_periodic_reports > 0 ? oa_timestamp_delta(report, last_periodic_report, fmt) : 0,
oa_report_is_periodic(oa_exponent, report),
oa_report_get_ctx_id(report));
if (((last_timestamp - first_timestamp) * oa_period) < (fill_duration / 2))
break;
if (oa_report_is_periodic(oa_exponent, report)) {
memcpy(last_periodic_report, report, report_size);
/* We want to measure only the
* periodic reports, ctx-switch
* might inflate the content of
* the buffer and skew or
* measurement.
*/
n_periodic_reports++;
}
break;
case DRM_I915_PERF_RECORD_OA_BUFFER_LOST:
igt_assert(!"unexpected overflow");
break;
}
}
}
do_ioctl(stream_fd, I915_PERF_IOCTL_DISABLE, 0);
igt_debug("first ts = %"PRIu64", last ts = %"PRIu64"\n", first_timestamp, last_timestamp);
igt_debug("%f < %zu < %f\n",
report_size * n_full_oa_reports * 0.45,
n_periodic_reports * report_size,
report_size * n_full_oa_reports * 0.55);
igt_assert((n_periodic_reports * report_size) >
(report_size * n_full_oa_reports * 0.45));
igt_assert((n_periodic_reports * report_size) <
report_size * n_full_oa_reports * 0.55);
/* It's considered an error to read a stream while it's disabled
* since it would block indefinitely...
*/
len = read(stream_fd, buf, buf_size);
igt_assert_eq(len, -1);
igt_assert_eq(errno, EIO);
}
free(last_periodic_report);
free(buf);
__perf_close(stream_fd);
load_helper_stop();
load_helper_fini();
}
static void
test_short_reads(void)
{
int oa_exponent = max_oa_exponent_for_period_lte(5000);
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
size_t record_size = 256 + sizeof(struct drm_i915_perf_record_header);
size_t page_size = sysconf(_SC_PAGE_SIZE);
int zero_fd = open("/dev/zero", O_RDWR|O_CLOEXEC);
uint8_t *pages = mmap(NULL, page_size * 2,
PROT_READ|PROT_WRITE, MAP_PRIVATE, zero_fd, 0);
struct drm_i915_perf_record_header *header;
int ret;
igt_assert_neq(zero_fd, -1);
close(zero_fd);
zero_fd = -1;
igt_assert(pages);
ret = mprotect(pages + page_size, page_size, PROT_NONE);
igt_assert_eq(ret, 0);
stream_fd = __perf_open(drm_fd, ¶m, false);
nanosleep(&(struct timespec){ .tv_sec = 0, .tv_nsec = 5000000 }, NULL);
/* At this point there should be lots of pending reports to read */
/* A read that can return at least one record should result in a short
* read not an EFAULT if the buffer is smaller than the requested read
* size...
*
* Expect to see a sample record here, but at least skip over any
* _RECORD_LOST notifications.
*/
do {
header = (void *)(pages + page_size - record_size);
ret = read(stream_fd,
header,
page_size);
igt_assert_lt(0, ret);
} while (header->type == DRM_I915_PERF_RECORD_OA_REPORT_LOST);
igt_assert_eq(ret, record_size);
/* A read that can't return a single record because it would result
* in a fault on buffer overrun should result in an EFAULT error...
*
* Make sure to weed out all report lost errors before verifying EFAULT.
*/
header = (void *)(pages + page_size - 16);
do {
ret = read(stream_fd, header, page_size);
} while (ret > 0 && header->type == DRM_I915_PERF_RECORD_OA_REPORT_LOST);
igt_assert_eq(ret, -1);
igt_assert_eq(errno, EFAULT);
/* A read that can't return a single record because the buffer is too
* small should result in an ENOSPC error..
*
* Again, skip over _RECORD_LOST records (smaller than record_size/2)
*/
do {
header = (void *)(pages + page_size - record_size / 2);
ret = read(stream_fd,
header,
record_size / 2);
} while (ret > 0 && header->type == DRM_I915_PERF_RECORD_OA_REPORT_LOST);
igt_assert_eq(ret, -1);
igt_assert_eq(errno, ENOSPC);
__perf_close(stream_fd);
munmap(pages, page_size * 2);
}
static void
test_non_sampling_read_error(void)
{
uint64_t properties[] = {
/* XXX: even without periodic sampling we have to
* specify at least one sample layout property...
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
/* XXX: no sampling exponent */
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
int ret;
uint8_t buf[1024];
stream_fd = __perf_open(drm_fd, ¶m, false);
ret = read(stream_fd, buf, sizeof(buf));
igt_assert_eq(ret, -1);
igt_assert_eq(errno, EIO);
__perf_close(stream_fd);
}
/* Check that attempts to read from a stream while it is disable will return
* EIO instead of blocking indefinitely.
*/
static void
test_disabled_read_error(void)
{
int oa_exponent = 5; /* 5 micro seconds */
uint64_t properties[] = {
/* XXX: even without periodic sampling we have to
* specify at least one sample layout property...
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED, /* XXX: open disabled */
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
uint32_t oa_report0[64];
uint32_t oa_report1[64];
uint32_t buf[128] = { 0 };
int ret;
stream_fd = __perf_open(drm_fd, ¶m, false);
ret = read(stream_fd, buf, sizeof(buf));
igt_assert_eq(ret, -1);
igt_assert_eq(errno, EIO);
__perf_close(stream_fd);
param.flags &= ~I915_PERF_FLAG_DISABLED;
stream_fd = __perf_open(drm_fd, ¶m, false);
read_2_oa_reports(default_test_set->perf_oa_format,
oa_exponent,
oa_report0,
oa_report1,
false); /* not just timer reports */
do_ioctl(stream_fd, I915_PERF_IOCTL_DISABLE, 0);
ret = read(stream_fd, buf, sizeof(buf));
igt_assert_eq(ret, -1);
igt_assert_eq(errno, EIO);
do_ioctl(stream_fd, I915_PERF_IOCTL_ENABLE, 0);
read_2_oa_reports(default_test_set->perf_oa_format,
oa_exponent,
oa_report0,
oa_report1,
false); /* not just timer reports */
__perf_close(stream_fd);
}
static void
gen12_test_mi_rpc(const struct intel_execution_engine2 *e)
{
uint64_t fmt = oar_unit_default_format();
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t properties[] = {
/* On Gen12, MI RPC uses OAR. OAR is configured only for the
* render context that wants to measure the performance. Hence a
* context must be specified in the gen12 MI RPC when compared
* to previous gens.
*
* Have a random value here for the context id, but initialize
* it once you figure out the context ID for the work to be
* measured
*/
DRM_I915_PERF_PROP_CTX_HANDLE, UINT64_MAX,
/* OA unit configuration:
* DRM_I915_PERF_PROP_SAMPLE_OA is no longer required for Gen12
* because the OAR unit increments counters only for the
* relevant context. No other parameters are needed since we do
* not rely on the OA buffer anymore to normalize the counter
* values.
*/
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, fmt,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
struct buf_ops *bops;
struct intel_bb *ibb;
struct intel_buf *buf;
#define INVALID_CTX_ID 0xffffffff
uint32_t ctx_id = INVALID_CTX_ID;
uint32_t *report32;
size_t format_size_32;
struct oa_format format = get_oa_format(fmt);
/* Ensure perf_stream_paranoid is set to 1 by default */
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
bops = buf_ops_create(drm_fd);
ctx_id = gem_context_create(drm_fd);
igt_assert_neq(ctx_id, INVALID_CTX_ID);
properties[1] = ctx_id;
ibb = intel_bb_create_with_context(drm_fd, ctx_id, 0, NULL, BATCH_SZ);
buf = intel_buf_create(bops, 4096, 1, 8, 64,
I915_TILING_NONE, I915_COMPRESSION_NONE);
buf_map(drm_fd, buf, true);
memset(buf->ptr, 0x80, 4096);
intel_buf_unmap(buf);
stream_fd = __perf_open(drm_fd, ¶m, false);
#define REPORT_ID 0xdeadbeef
#define REPORT_OFFSET 0
emit_report_perf_count(ibb,
buf,
REPORT_OFFSET,
REPORT_ID);
intel_bb_flush_render(ibb);
intel_bb_sync(ibb);
intel_buf_cpu_map(buf, false);
report32 = buf->ptr;
format_size_32 = format.size >> 2;
dump_report(report32, format_size_32, "mi-rpc");
/* Sanity check reports
* reportX_32[0]: report id passed with mi-rpc
* reportX_32[1]: timestamp. NOTE: wraps around in ~6 minutes.
*
* reportX_32[format.b_off]: check if the entire report was filled.
* B0 counter falls in the last 64 bytes of this report format.
* Since reports are filled in 64 byte blocks, we should be able to
* assure that the report was filled by checking the B0 counter. B0
* counter is defined to be zero, so we can easily validate it.
*
* reportX_32[format_size_32]: outside report, make sure only the report
* size amount of data was written.
*/
igt_assert_eq(report32[0], REPORT_ID);
igt_assert(oa_timestamp(report32, test_set->perf_oa_format));
igt_assert_neq(report32[format.b_off >> 2], 0x80808080);
igt_assert_eq(report32[format_size_32], 0x80808080);
intel_buf_unmap(buf);
intel_buf_destroy(buf);
intel_bb_destroy(ibb);
gem_context_destroy(drm_fd, ctx_id);
buf_ops_destroy(bops);
__perf_close(stream_fd);
}
static void
test_mi_rpc(void)
{
uint64_t properties[] = {
/* Note: we have to specify at least one sample property even
* though we aren't interested in samples in this case.
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
/* Note: no OA exponent specified in this case */
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
struct buf_ops *bops = buf_ops_create(drm_fd);
struct intel_bb *ibb;
struct intel_buf *buf;
uint32_t *report32, ctx_id;
stream_fd = __perf_open(drm_fd, ¶m, false);
ctx_id = gem_context_create(drm_fd);
ibb = intel_bb_create_with_context(drm_fd, ctx_id, 0, NULL, BATCH_SZ);
buf = intel_buf_create(bops, 4096, 1, 8, 64,
I915_TILING_NONE, I915_COMPRESSION_NONE);
buf_map(drm_fd, buf, true);
memset(buf->ptr, 0x80, 4096);
intel_buf_unmap(buf);
emit_report_perf_count(ibb,
buf, /* dst */
0, /* dst offset in bytes */
0xdeadbeef); /* report ID */
intel_bb_flush_render(ibb);
intel_bb_sync(ibb);
intel_buf_cpu_map(buf, false);
report32 = buf->ptr;
dump_report(report32, 64, "mi-rpc");
igt_assert_eq(report32[0], 0xdeadbeef); /* report ID */
igt_assert(oa_timestamp(report32, default_test_set->perf_oa_format)); /* timestamp */
igt_assert_neq(report32[63], 0x80808080); /* end of report */
igt_assert_eq(report32[64], 0x80808080); /* after 256 byte report */
intel_buf_unmap(buf);
intel_buf_destroy(buf);
intel_bb_destroy(ibb);
gem_context_destroy(drm_fd, ctx_id);
buf_ops_destroy(bops);
__perf_close(stream_fd);
}
static void
emit_stall_timestamp_and_rpc(struct intel_bb *ibb,
struct intel_buf *dst,
int timestamp_offset,
int report_dst_offset,
uint32_t report_id)
{
uint32_t pipe_ctl_flags = (PIPE_CONTROL_CS_STALL |
PIPE_CONTROL_RENDER_TARGET_FLUSH |
PIPE_CONTROL_WRITE_TIMESTAMP);
intel_bb_add_intel_buf(ibb, dst, true);
if (intel_gen(devid) >= 8)
intel_bb_out(ibb, GFX_OP_PIPE_CONTROL(6));
else
intel_bb_out(ibb, GFX_OP_PIPE_CONTROL(5));
intel_bb_out(ibb, pipe_ctl_flags);
intel_bb_emit_reloc(ibb, dst->handle,
I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
timestamp_offset, dst->addr.offset);
intel_bb_out(ibb, 0); /* imm lower */
intel_bb_out(ibb, 0); /* imm upper */
emit_report_perf_count(ibb, dst, report_dst_offset, report_id);
}
/* Tests the INTEL_performance_query use case where an unprivileged process
* should be able to configure the OA unit for per-context metrics (for a
* context associated with that process' drm file descriptor) and the counters
* should only relate to that specific context.
*
* Unfortunately only Haswell limits the progression of OA counters for a
* single context and so this unit test is Haswell specific. For Gen8+ although
* reports read via i915 perf can be filtered for a single context the counters
* themselves always progress as global/system-wide counters affected by all
* contexts.
*/
static void
hsw_test_single_ctx_counters(void)
{
uint64_t properties[] = {
DRM_I915_PERF_PROP_CTX_HANDLE, UINT64_MAX, /* updated below */
/* Note: we have to specify at least one sample property even
* though we aren't interested in samples in this case
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
/* Note: no OA exponent specified in this case */
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
/* should be default, but just to be sure... */
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
igt_fork(child, 1) {
struct buf_ops *bops;
struct intel_buf src[3], dst[3], *dst_buf;
struct intel_bb *ibb0, *ibb1;
uint32_t context0_id, context1_id;
uint32_t *report0_32, *report1_32;
uint64_t timestamp0_64, timestamp1_64;
uint32_t delta_ts64, delta_oa32;
uint64_t delta_ts64_ns, delta_oa32_ns;
uint32_t delta_delta;
int n_samples_written;
int width = 800;
int height = 600;
igt_drop_root();
bops = buf_ops_create(drm_fd);
for (int i = 0; i < ARRAY_SIZE(src); i++) {
scratch_buf_init(bops, &src[i], width, height, 0xff0000ff);
scratch_buf_init(bops, &dst[i], width, height, 0x00ff00ff);
}
/*
* We currently cache addresses for buffers within
* intel_bb, so use separate batches for different contexts
*/
context0_id = gem_context_create(drm_fd);
context1_id = gem_context_create(drm_fd);
ibb0 = intel_bb_create_with_context(drm_fd, context0_id, 0, NULL, BATCH_SZ);
ibb1 = intel_bb_create_with_context(drm_fd, context1_id, 0, NULL, BATCH_SZ);
igt_debug("submitting warm up render_copy\n");
/* Submit some early, unmeasured, work to the context we want
* to measure to try and catch issues with i915-perf
* initializing the HW context ID for filtering.
*
* We do this because i915-perf single context filtering had
* previously only relied on a hook into context pinning to
* initialize the HW context ID, instead of also trying to
* determine the HW ID while opening the stream, in case it
* has already been pinned.
*
* This wasn't noticed by the previous unit test because we
* were opening the stream while the context hadn't been
* touched or pinned yet and so it worked out correctly to wait
* for the pinning hook.
*
* Now a buggy version of i915-perf will fail to measure
* anything for context0 once this initial render_copy() ends
* up pinning the context since there won't ever be a pinning
* hook callback.
*/
render_copy(ibb0,
&src[0], 0, 0, width, height,
&dst[0], 0, 0);
properties[1] = context0_id;
intel_bb_flush_render(ibb0);
intel_bb_sync(ibb0);
scratch_buf_memset(&src[0], width, height, 0xff0000ff);
scratch_buf_memset(&dst[0], width, height, 0x00ff00ff);
igt_debug("opening i915-perf stream\n");
stream_fd = __perf_open(drm_fd, ¶m, false);
dst_buf = intel_buf_create(bops, 4096, 1, 8, 64,
I915_TILING_NONE,
I915_COMPRESSION_NONE);
buf_map(drm_fd, dst_buf, true /* write enable */);
memset(dst_buf->ptr, 0x80, 4096);
intel_buf_unmap(dst_buf);
emit_stall_timestamp_and_rpc(ibb0,
dst_buf,
512 /* timestamp offset */,
0, /* report dst offset */
0xdeadbeef); /* report id */
/* Explicitly flush here (even though the render_copy() call
* will itself flush before/after the copy) to clarify that
* that the PIPE_CONTROL + MI_RPC commands will be in a
* separate batch from the copy.
*/
intel_bb_flush_render(ibb0);
render_copy(ibb0,
&src[0], 0, 0, width, height,
&dst[0], 0, 0);
/* Another redundant flush to clarify batch bo is free to reuse */
intel_bb_flush_render(ibb0);
/* submit two copies on the other context to avoid a false
* positive in case the driver somehow ended up filtering for
* context1
*/
render_copy(ibb1,
&src[1], 0, 0, width, height,
&dst[1], 0, 0);
render_copy(ibb1,
&src[2], 0, 0, width, height,
&dst[2], 0, 0);
/* And another */
intel_bb_flush_render(ibb1);
emit_stall_timestamp_and_rpc(ibb0,
dst_buf,
520 /* timestamp offset */,
256, /* report dst offset */
0xbeefbeef); /* report id */
intel_bb_flush_render(ibb0);
intel_bb_sync(ibb0);
intel_buf_cpu_map(dst_buf, false /* write enable */);
report0_32 = dst_buf->ptr;
igt_assert_eq(report0_32[0], 0xdeadbeef); /* report ID */
igt_assert_neq(report0_32[1], 0); /* timestamp */
report1_32 = report0_32 + 64;
igt_assert_eq(report1_32[0], 0xbeefbeef); /* report ID */
igt_assert_neq(report1_32[1], 0); /* timestamp */
print_reports(report0_32, report1_32,
lookup_format(default_test_set->perf_oa_format));
/* A40 == N samples written to all render targets */
n_samples_written = report1_32[43] - report0_32[43];
igt_debug("n samples written = %d\n", n_samples_written);
igt_assert_eq(n_samples_written, width * height);
igt_debug("timestamp32 0 = %u\n", report0_32[1]);
igt_debug("timestamp32 1 = %u\n", report1_32[1]);
timestamp0_64 = *(uint64_t *)(((uint8_t *)dst_buf->ptr) + 512);
timestamp1_64 = *(uint64_t *)(((uint8_t *)dst_buf->ptr) + 520);
igt_debug("timestamp64 0 = %"PRIu64"\n", timestamp0_64);
igt_debug("timestamp64 1 = %"PRIu64"\n", timestamp1_64);
delta_ts64 = timestamp1_64 - timestamp0_64;
delta_oa32 = report1_32[1] - report0_32[1];
/* sanity check that we can pass the delta to timebase_scale */
igt_assert(delta_ts64 < UINT32_MAX);
delta_oa32_ns = timebase_scale(delta_oa32);
delta_ts64_ns = timebase_scale(delta_ts64);
igt_debug("ts32 delta = %u, = %uns\n",
delta_oa32, (unsigned)delta_oa32_ns);
igt_debug("ts64 delta = %u, = %uns\n",
delta_ts64, (unsigned)delta_ts64_ns);
/* The delta as calculated via the PIPE_CONTROL timestamp or
* the OA report timestamps should be almost identical but
* allow a 320 nanoseconds margin.
*/
delta_delta = delta_ts64_ns > delta_oa32_ns ?
(delta_ts64_ns - delta_oa32_ns) :
(delta_oa32_ns - delta_ts64_ns);
igt_assert(delta_delta <= 320);
for (int i = 0; i < ARRAY_SIZE(src); i++) {
intel_buf_close(bops, &src[i]);
intel_buf_close(bops, &dst[i]);
}
intel_buf_unmap(dst_buf);
intel_buf_destroy(dst_buf);
intel_bb_destroy(ibb0);
intel_bb_destroy(ibb1);
gem_context_destroy(drm_fd, context0_id);
gem_context_destroy(drm_fd, context1_id);
buf_ops_destroy(bops);
__perf_close(stream_fd);
}
igt_waitchildren();
}
/* Tests the INTEL_performance_query use case where an unprivileged process
* should be able to configure the OA unit for per-context metrics (for a
* context associated with that process' drm file descriptor) and the counters
* should only relate to that specific context.
*
* For Gen8+ although reports read via i915 perf can be filtered for a single
* context the counters themselves always progress as global/system-wide
* counters affected by all contexts. To support the INTEL_performance_query
* use case on Gen8+ it's necessary to combine OABUFFER and
* MI_REPORT_PERF_COUNT reports so that counter normalisation can take into
* account context-switch reports and factor out any counter progression not
* associated with the current context.
*/
static void
gen8_test_single_ctx_render_target_writes_a_counter(void)
{
int oa_exponent = max_oa_exponent_for_period_lte(1000000);
uint64_t properties[] = {
DRM_I915_PERF_PROP_CTX_HANDLE, UINT64_MAX, /* updated below */
/* Note: we have to specify at least one sample property even
* though we aren't interested in samples in this case
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
/* Note: no OA exponent specified in this case */
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
size_t format_size = get_oa_format(default_test_set->perf_oa_format).size;
size_t sample_size = (sizeof(struct drm_i915_perf_record_header) +
format_size);
int max_reports = MAX_OA_BUF_SIZE / format_size;
int buf_size = sample_size * max_reports * 1.5;
int child_ret;
uint8_t *buf = malloc(buf_size);
ssize_t len;
struct igt_helper_process child = {};
/* should be default, but just to be sure... */
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
do {
igt_fork_helper(&child) {
struct drm_i915_perf_record_header *header;
struct buf_ops *bops;
struct intel_bb *ibb0, *ibb1;
struct intel_buf src[3], dst[3], *dst_buf;
uint32_t context0_id, context1_id;
uint32_t *report0_32, *report1_32;
uint32_t *prev, *lprev = NULL;
uint64_t timestamp0_64, timestamp1_64;
uint32_t delta_ts64, delta_oa32;
uint64_t delta_ts64_ns, delta_oa32_ns;
int width = 800;
int height = 600;
uint32_t ctx_id = 0xffffffff; /* invalid handle */
uint32_t ctx1_id = 0xffffffff; /* invalid handle */
uint32_t current_ctx_id = 0xffffffff;
uint32_t n_invalid_ctx = 0;
int delta_delta;
int ret;
struct accumulator accumulator = {
.format = default_test_set->perf_oa_format
};
bops = buf_ops_create(drm_fd);
for (int i = 0; i < ARRAY_SIZE(src); i++) {
scratch_buf_init(bops, &src[i], width, height, 0xff0000ff);
scratch_buf_init(bops, &dst[i], width, height, 0x00ff00ff);
}
context0_id = gem_context_create(drm_fd);
context1_id = gem_context_create(drm_fd);
ibb0 = intel_bb_create_with_context(drm_fd, context0_id, 0, NULL, BATCH_SZ);
ibb1 = intel_bb_create_with_context(drm_fd, context1_id, 0, NULL, BATCH_SZ);
igt_debug("submitting warm up render_copy\n");
/* Submit some early, unmeasured, work to the context we want
* to measure to try and catch issues with i915-perf
* initializing the HW context ID for filtering.
*
* We do this because i915-perf single context filtering had
* previously only relied on a hook into context pinning to
* initialize the HW context ID, instead of also trying to
* determine the HW ID while opening the stream, in case it
* has already been pinned.
*
* This wasn't noticed by the previous unit test because we
* were opening the stream while the context hadn't been
* touched or pinned yet and so it worked out correctly to wait
* for the pinning hook.
*
* Now a buggy version of i915-perf will fail to measure
* anything for context0 once this initial render_copy() ends
* up pinning the context since there won't ever be a pinning
* hook callback.
*/
render_copy(ibb0,
&src[0], 0, 0, width, height,
&dst[0], 0, 0);
intel_bb_sync(ibb0);
properties[1] = context0_id;
scratch_buf_memset(&src[0], width, height, 0xff0000ff);
scratch_buf_memset(&dst[0], width, height, 0x00ff00ff);
igt_debug("opening i915-perf stream\n");
stream_fd = __perf_open(drm_fd, ¶m, false);
dst_buf = intel_buf_create(bops, 4096, 1, 8, 64,
I915_TILING_NONE,
I915_COMPRESSION_NONE);
buf_map(drm_fd, dst_buf, true);
memset(dst_buf->ptr, 0x80, 4096);
intel_buf_unmap(dst_buf);
emit_stall_timestamp_and_rpc(ibb0,
dst_buf,
512 /* timestamp offset */,
0, /* report dst offset */
0xdeadbeef); /* report id */
/* Explicitly flush here (even though the render_copy() call
* will itself flush before/after the copy) to clarify that
* that the PIPE_CONTROL + MI_RPC commands will be in a
* separate batch from the copy.
*/
intel_bb_flush_render(ibb0);
render_copy(ibb0,
&src[0], 0, 0, width, height,
&dst[0], 0, 0);
/* Another redundant flush to clarify batch bo is free to reuse */
intel_bb_flush_render(ibb0);
/* Remove intel_buf from ibb0 added implicitly in rendercopy */
intel_bb_remove_intel_buf(ibb0, dst_buf);
/* submit two copies on the other context to avoid a false
* positive in case the driver somehow ended up filtering for
* context1
*/
render_copy(ibb1,
&src[1], 0, 0, width, height,
&dst[1], 0, 0);
render_copy(ibb1,
&src[2], 0, 0, width, height,
&dst[2], 0, 0);
/* And another */
intel_bb_flush_render(ibb1);
emit_stall_timestamp_and_rpc(ibb1,
dst_buf,
520 /* timestamp offset */,
256, /* report dst offset */
0xbeefbeef); /* report id */
intel_bb_flush_render(ibb1);
intel_bb_sync(ibb1);
intel_bb_sync(ibb0);
intel_buf_cpu_map(dst_buf, false /* write enable */);
report0_32 = dst_buf->ptr;
igt_assert_eq(report0_32[0], 0xdeadbeef); /* report ID */
igt_assert_neq(report0_32[1], 0); /* timestamp */
prev = report0_32;
ctx_id = prev[2];
igt_debug("MI_RPC(start) CTX ID: %u\n", ctx_id);
dump_report(report0_32, 64, "report0_32");
report1_32 = report0_32 + 64; /* 64 uint32_t = 256bytes offset */
igt_assert_eq(report1_32[0], 0xbeefbeef); /* report ID */
igt_assert_neq(report1_32[1], 0); /* timestamp */
ctx1_id = report1_32[2];
igt_debug("CTX1 ID: %u\n", ctx1_id);
dump_report(report1_32, 64, "report1_32");
memset(accumulator.deltas, 0, sizeof(accumulator.deltas));
accumulate_reports(&accumulator, report0_32, report1_32);
igt_debug("total: A0 = %"PRIu64", A21 = %"PRIu64", A26 = %"PRIu64"\n",
accumulator.deltas[2 + 0], /* skip timestamp + clock cycles */
accumulator.deltas[2 + 21],
accumulator.deltas[2 + 26]);
igt_debug("oa_timestamp32 0 = %u\n", report0_32[1]);
igt_debug("oa_timestamp32 1 = %u\n", report1_32[1]);
igt_debug("ctx_id 0 = %u\n", report0_32[2]);
igt_debug("ctx_id 1 = %u\n", report1_32[2]);
timestamp0_64 = *(uint64_t *)(((uint8_t *)dst_buf->ptr) + 512);
timestamp1_64 = *(uint64_t *)(((uint8_t *)dst_buf->ptr) + 520);
igt_debug("ts_timestamp64 0 = %"PRIu64"\n", timestamp0_64);
igt_debug("ts_timestamp64 1 = %"PRIu64"\n", timestamp1_64);
delta_ts64 = timestamp1_64 - timestamp0_64;
delta_oa32 = report1_32[1] - report0_32[1];
/* sanity check that we can pass the delta to timebase_scale */
igt_assert(delta_ts64 < UINT32_MAX);
delta_oa32_ns = timebase_scale(delta_oa32);
delta_ts64_ns = timebase_scale(delta_ts64);
igt_debug("oa32 delta = %u, = %uns\n",
delta_oa32, (unsigned)delta_oa32_ns);
igt_debug("ts64 delta = %u, = %uns\n",
delta_ts64, (unsigned)delta_ts64_ns);
/* The delta as calculated via the PIPE_CONTROL timestamp or
* the OA report timestamps should be almost identical but
* allow a 2 microsecond margin.
*/
delta_delta = delta_ts64_ns - delta_oa32_ns;
if (abs(delta_delta) > 2000) {
igt_debug("Too slow %d; skipping\n",
delta_delta);
ret = EAGAIN;
goto again;
}
len = i915_read_reports_until_timestamp(default_test_set->perf_oa_format,
buf, buf_size,
report0_32[1],
report1_32[1]);
igt_assert(len > 0);
igt_debug("read %d bytes\n", (int)len);
memset(accumulator.deltas, 0, sizeof(accumulator.deltas));
for (size_t offset = 0; offset < len; offset += header->size) {
uint32_t *report;
uint32_t reason;
const char *skip_reason = NULL, *report_reason = NULL;
struct accumulator laccumulator = {
.format = default_test_set->perf_oa_format
};
header = (void *)(buf + offset);
igt_assert_eq(header->pad, 0); /* Reserved */
/* Currently the only test that should ever expect to
* see a _BUFFER_LOST error is the buffer_fill test,
* otherwise something bad has probably happened...
*/
igt_assert_neq(header->type, DRM_I915_PERF_RECORD_OA_BUFFER_LOST);
/* At high sampling frequencies the OA HW might not be
* able to cope with all write requests and will notify
* us that a report was lost.
*
* XXX: we should maybe restart the test in this case?
*/
if (header->type == DRM_I915_PERF_RECORD_OA_REPORT_LOST) {
igt_debug("OA trigger collision / report lost\n");
ret = EAGAIN;
goto again;
}
/* Currently the only other record type expected is a
* _SAMPLE. Notably this test will need updating if
* i915-perf is extended in the future with additional
* record types.
*/
igt_assert_eq(header->type, DRM_I915_PERF_RECORD_SAMPLE);
igt_assert_eq(header->size, sample_size);
report = (void *)(header + 1);
dump_report(report, 64, "OA report");
/* Don't expect zero for timestamps */
igt_assert_neq(report[1], 0);
igt_debug("report %p:\n", report);
/* Discard reports not contained in between the
* timestamps we're looking at. */
{
uint32_t time_delta = report[1] - report0_32[1];
if (timebase_scale(time_delta) > 1000000000) {
skip_reason = "prior first mi-rpc";
}
}
{
uint32_t time_delta = report[1] - report1_32[1];
if (timebase_scale(time_delta) <= 1000000000) {
igt_debug(" comes after last MI_RPC (%u)\n",
report1_32[1]);
report = report1_32;
}
}
/* Print out deltas for a few significant
* counters for each report. */
if (lprev) {
memset(laccumulator.deltas, 0, sizeof(laccumulator.deltas));
accumulate_reports(&laccumulator, lprev, report);
igt_debug(" deltas: A0=%"PRIu64" A21=%"PRIu64", A26=%"PRIu64"\n",
laccumulator.deltas[2 + 0], /* skip timestamp + clock cycles */
laccumulator.deltas[2 + 21],
laccumulator.deltas[2 + 26]);
}
lprev = report;
/* Print out reason for the report. */
reason = gen8_report_reason(report);
if (reason & OAREPORT_REASON_CTX_SWITCH) {
report_reason = "ctx-load";
} else if (reason & OAREPORT_REASON_TIMER) {
report_reason = "timer";
} else if (reason & OAREPORT_REASON_INTERNAL ||
reason & OAREPORT_REASON_GO ||
reason & OAREPORT_REASON_CLK_RATIO) {
report_reason = "internal/go/clk-ratio";
} else {
report_reason = "end-mi-rpc";
}
igt_debug(" ctx_id=%u/%x reason=%s oa_timestamp32=%u\n",
report[2], report[2], report_reason, report[1]);
/* Should we skip this report?
*
* Only if the current context id of
* the stream is not the one we want
* to measure.
*/
if (current_ctx_id != ctx_id) {
skip_reason = "not our context";
}
if (n_invalid_ctx > 1) {
skip_reason = "too many invalid context events";
}
if (!skip_reason) {
accumulate_reports(&accumulator, prev, report);
igt_debug(" -> Accumulated deltas A0=%"PRIu64" A21=%"PRIu64", A26=%"PRIu64"\n",
accumulator.deltas[2 + 0], /* skip timestamp + clock cycles */
accumulator.deltas[2 + 21],
accumulator.deltas[2 + 26]);
} else {
igt_debug(" -> Skipping: %s\n", skip_reason);
}
/* Finally update current-ctx_id, only possible
* with a valid context id. */
if (oa_report_ctx_is_valid(report)) {
current_ctx_id = report[2];
n_invalid_ctx = 0;
} else {
n_invalid_ctx++;
}
prev = report;
if (report == report1_32) {
igt_debug("Breaking on end of report\n");
print_reports(report0_32, report1_32,
lookup_format(default_test_set->perf_oa_format));
break;
}
}
igt_debug("n samples written = %"PRIu64"/%"PRIu64" (%ix%i)\n",
accumulator.deltas[2 + 21],/* skip timestamp + clock cycles */
accumulator.deltas[2 + 26],
width, height);
accumulator_print(&accumulator, "filtered");
intel_buf_cpu_map(&src[0], false /* write enable */);
intel_buf_cpu_map(&dst[0], false /* write enable */);
ret = memcmp(src[0].ptr, dst[0].ptr, 4 * width * height);
intel_buf_unmap(&src[0]);
intel_buf_unmap(&dst[0]);
again:
for (int i = 0; i < ARRAY_SIZE(src); i++) {
intel_buf_close(bops, &src[i]);
intel_buf_close(bops, &dst[i]);
}
intel_buf_unmap(dst_buf);
intel_buf_destroy(dst_buf);
intel_bb_destroy(ibb0);
intel_bb_destroy(ibb1);
gem_context_destroy(drm_fd, context0_id);
gem_context_destroy(drm_fd, context1_id);
buf_ops_destroy(bops);
__perf_close(stream_fd);
gem_quiescent_gpu(drm_fd);
if (ret != 0) {
accumulator_print(&accumulator, "total");
/* This needs to be investigated... From time
* to time, the work we kick off doesn't seem
* to happen. WTH?? */
exit(EAGAIN);
}
igt_assert_eq(accumulator.deltas[2 + 26], width * height);
}
child_ret = igt_wait_helper(&child);
igt_assert(WEXITSTATUS(child_ret) == EAGAIN ||
WEXITSTATUS(child_ret) == 0);
} while (WEXITSTATUS(child_ret) == EAGAIN);
}
static void gen12_single_ctx_helper(const struct intel_execution_engine2 *e)
{
struct intel_perf_metric_set *test_set = metric_set(e);
uint64_t fmt = oar_unit_default_format();
uint64_t properties[] = {
/* Have a random value here for the context id, but initialize
* it once you figure out the context ID for the work to be
* measured
*/
DRM_I915_PERF_PROP_CTX_HANDLE, UINT64_MAX,
/* OA unit configuration:
* DRM_I915_PERF_PROP_SAMPLE_OA is no longer required for Gen12
* because the OAR unit increments counters only for the
* relevant context. No other parameters are needed since we do
* not rely on the OA buffer anymore to normalize the counter
* values.
*/
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, fmt,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
struct buf_ops *bops;
struct intel_bb *ibb0, *ibb1;
struct intel_buf src[3], dst[3], *dst_buf;
uint32_t context0_id, context1_id;
uint32_t *report0_32, *report1_32, *report2_32, *report3_32;
uint64_t timestamp0_64, timestamp1_64;
uint64_t delta_ts64, delta_oa32;
uint64_t delta_ts64_ns, delta_oa32_ns;
uint64_t delta_delta;
int width = 800;
int height = 600;
#define INVALID_CTX_ID 0xffffffff
uint32_t ctx0_id = INVALID_CTX_ID;
uint32_t ctx1_id = INVALID_CTX_ID;
int ret;
struct accumulator accumulator = {
.format = fmt
};
bops = buf_ops_create(drm_fd);
for (int i = 0; i < ARRAY_SIZE(src); i++) {
scratch_buf_init(bops, &src[i], width, height, 0xff0000ff);
scratch_buf_init(bops, &dst[i], width, height, 0x00ff00ff);
}
context0_id = gem_context_create(drm_fd);
context1_id = gem_context_create(drm_fd);
ibb0 = intel_bb_create_with_context(drm_fd, context0_id, 0, NULL, BATCH_SZ);
ibb1 = intel_bb_create_with_context(drm_fd, context1_id, 0, NULL, BATCH_SZ);
igt_debug("submitting warm up render_copy\n");
/* Submit some early, unmeasured, work to the context we want
* to measure to try and catch issues with i915-perf
* initializing the HW context ID for filtering.
*
* We do this because i915-perf single context filtering had
* previously only relied on a hook into context pinning to
* initialize the HW context ID, instead of also trying to
* determine the HW ID while opening the stream, in case it
* has already been pinned.
*
* This wasn't noticed by the previous unit test because we
* were opening the stream while the context hadn't been
* touched or pinned yet and so it worked out correctly to wait
* for the pinning hook.
*
* Now a buggy version of i915-perf will fail to measure
* anything for context0 once this initial render_copy() ends
* up pinning the context since there won't ever be a pinning
* hook callback.
*/
render_copy(ibb0,
&src[0], 0, 0, width, height,
&dst[0], 0, 0);
/* Initialize the context parameter to the perf open ioctl here */
properties[1] = context0_id;
igt_debug("opening i915-perf stream\n");
stream_fd = __perf_open(drm_fd, ¶m, false);
dst_buf = intel_buf_create(bops, 4096, 1, 8, 64,
I915_TILING_NONE,
I915_COMPRESSION_NONE);
/* Set write domain to cpu briefly to fill the buffer with 80s */
buf_map(drm_fd, dst_buf, true /* write enable */);
memset(dst_buf->ptr, 0x80, 2048);
memset((uint8_t *) dst_buf->ptr + 2048, 0, 2048);
intel_buf_unmap(dst_buf);
/* Submit an mi-rpc to context0 before measurable work */
#define BO_TIMESTAMP_OFFSET0 1024
#define BO_REPORT_OFFSET0 0
#define BO_REPORT_ID0 0xdeadbeef
emit_stall_timestamp_and_rpc(ibb0,
dst_buf,
BO_TIMESTAMP_OFFSET0,
BO_REPORT_OFFSET0,
BO_REPORT_ID0);
intel_bb_flush_render(ibb0);
/* Remove intel_buf from ibb0 added implicitly in rendercopy */
intel_bb_remove_intel_buf(ibb0, dst_buf);
/* This is the work/context that is measured for counter increments */
render_copy(ibb0,
&src[0], 0, 0, width, height,
&dst[0], 0, 0);
intel_bb_flush_render(ibb0);
/* Submit an mi-rpc to context1 before work
*
* On gen12, this measurement should just yield counters that are
* all zeroes, since the counters will only increment for the
* context passed to perf open ioctl
*/
#define BO_TIMESTAMP_OFFSET2 1040
#define BO_REPORT_OFFSET2 512
#define BO_REPORT_ID2 0x00c0ffee
emit_stall_timestamp_and_rpc(ibb1,
dst_buf,
BO_TIMESTAMP_OFFSET2,
BO_REPORT_OFFSET2,
BO_REPORT_ID2);
intel_bb_flush_render(ibb1);
/* Submit two copies on the other context to avoid a false
* positive in case the driver somehow ended up filtering for
* context1
*/
render_copy(ibb1,
&src[1], 0, 0, width, height,
&dst[1], 0, 0);
render_copy(ibb1,
&src[2], 0, 0, width, height,
&dst[2], 0, 0);
intel_bb_flush_render(ibb1);
/* Submit an mi-rpc to context1 after all work */
#define BO_TIMESTAMP_OFFSET3 1048
#define BO_REPORT_OFFSET3 768
#define BO_REPORT_ID3 0x01c0ffee
emit_stall_timestamp_and_rpc(ibb1,
dst_buf,
BO_TIMESTAMP_OFFSET3,
BO_REPORT_OFFSET3,
BO_REPORT_ID3);
intel_bb_flush_render(ibb1);
/* Remove intel_buf from ibb1 added implicitly in rendercopy */
intel_bb_remove_intel_buf(ibb1, dst_buf);
/* Submit an mi-rpc to context0 after all measurable work */
#define BO_TIMESTAMP_OFFSET1 1032
#define BO_REPORT_OFFSET1 256
#define BO_REPORT_ID1 0xbeefbeef
emit_stall_timestamp_and_rpc(ibb0,
dst_buf,
BO_TIMESTAMP_OFFSET1,
BO_REPORT_OFFSET1,
BO_REPORT_ID1);
intel_bb_flush_render(ibb0);
intel_bb_sync(ibb0);
intel_bb_sync(ibb1);
intel_buf_cpu_map(dst_buf, false);
/* Sanity check reports
* reportX_32[0]: report id passed with mi-rpc
* reportX_32[1]: timestamp
* reportX_32[2]: context id
*
* report0_32: start of measurable work
* report1_32: end of measurable work
* report2_32: start of other work
* report3_32: end of other work
*/
report0_32 = dst_buf->ptr;
igt_assert_eq(report0_32[0], 0xdeadbeef);
igt_assert(oa_timestamp(report0_32, fmt));
ctx0_id = report0_32[2];
igt_debug("MI_RPC(start) CTX ID: %u\n", ctx0_id);
dump_report(report0_32, 64, "report0_32");
report1_32 = report0_32 + 64;
igt_assert_eq(report1_32[0], 0xbeefbeef);
igt_assert(oa_timestamp(report1_32, fmt));
ctx1_id = report1_32[2];
igt_debug("CTX ID1: %u\n", ctx1_id);
dump_report(report1_32, 64, "report1_32");
/* Verify that counters in context1 are all zeroes */
report2_32 = report0_32 + 128;
igt_assert_eq(report2_32[0], 0x00c0ffee);
igt_assert(oa_timestamp(report2_32, fmt));
dump_report(report2_32, 64, "report2_32");
igt_assert_eq(0, memcmp(&report2_32[4],
(uint8_t *) dst_buf->ptr + 2048,
240));
report3_32 = report0_32 + 192;
igt_assert_eq(report3_32[0], 0x01c0ffee);
igt_assert(oa_timestamp(report3_32, fmt));
dump_report(report3_32, 64, "report3_32");
igt_assert_eq(0, memcmp(&report3_32[4],
(uint8_t *) dst_buf->ptr + 2048,
240));
/* Accumulate deltas for counters - A0, A21 and A26 */
memset(accumulator.deltas, 0, sizeof(accumulator.deltas));
accumulate_reports(&accumulator, report0_32, report1_32);
igt_debug("total: A0 = %"PRIu64", A21 = %"PRIu64", A26 = %"PRIu64"\n",
accumulator.deltas[2 + 0],
accumulator.deltas[2 + 21],
accumulator.deltas[2 + 26]);
igt_debug("oa_timestamp32 0 = %"PRIu64"\n", oa_timestamp(report0_32, fmt));
igt_debug("oa_timestamp32 1 = %"PRIu64"\n", oa_timestamp(report1_32, fmt));
igt_debug("ctx_id 0 = %u\n", report0_32[2]);
igt_debug("ctx_id 1 = %u\n", report1_32[2]);
/* The delta as calculated via the PIPE_CONTROL timestamp or
* the OA report timestamps should be almost identical but
* allow a 500 nanoseconds margin.
*/
timestamp0_64 = *(uint64_t *)(((uint8_t *)dst_buf->ptr) + BO_TIMESTAMP_OFFSET0);
timestamp1_64 = *(uint64_t *)(((uint8_t *)dst_buf->ptr) + BO_TIMESTAMP_OFFSET1);
igt_debug("ts_timestamp64 0 = %"PRIu64"\n", timestamp0_64);
igt_debug("ts_timestamp64 1 = %"PRIu64"\n", timestamp1_64);
delta_ts64 = timestamp1_64 - timestamp0_64;
delta_oa32 = oa_timestamp_delta(report1_32, report0_32, fmt);
/* Sanity check that we can pass the delta to timebase_scale */
delta_oa32_ns = timebase_scale(delta_oa32);
delta_ts64_ns = cs_timebase_scale(delta_ts64);
igt_debug("oa32 delta = %"PRIu64", = %"PRIu64"ns\n",
delta_oa32, delta_oa32_ns);
igt_debug("ts64 delta = %"PRIu64", = %"PRIu64"ns\n",
delta_ts64, delta_ts64_ns);
delta_delta = delta_ts64_ns > delta_oa32_ns ?
(delta_ts64_ns - delta_oa32_ns) :
(delta_oa32_ns - delta_ts64_ns);
if (delta_delta > 500) {
igt_debug("delta_delta = %"PRIu64". exceeds margin, skipping..\n",
delta_delta);
exit(EAGAIN);
}
igt_debug("n samples written = %"PRIu64"/%"PRIu64" (%ix%i)\n",
accumulator.deltas[2 + 21],
accumulator.deltas[2 + 26],
width, height);
accumulator_print(&accumulator, "filtered");
/* Verify that the work actually happened by comparing the src
* and dst buffers
*/
intel_buf_cpu_map(&src[0], false);
intel_buf_cpu_map(&dst[0], false);
ret = memcmp(src[0].ptr, dst[0].ptr, 4 * width * height);
intel_buf_unmap(&src[0]);
intel_buf_unmap(&dst[0]);
if (ret != 0) {
accumulator_print(&accumulator, "total");
exit(EAGAIN);
}
/* Check that this test passed. The test measures the number of 2x2
* samples written to the render target using the counter A26. For
* OAR, this counter will only have increments relevant to this specific
* context. The value equals the width * height of the rendered work.
*/
igt_assert_eq(accumulator.deltas[2 + 26], width * height);
/* Clean up */
for (int i = 0; i < ARRAY_SIZE(src); i++) {
intel_buf_close(bops, &src[i]);
intel_buf_close(bops, &dst[i]);
}
intel_buf_unmap(dst_buf);
intel_buf_destroy(dst_buf);
intel_bb_destroy(ibb0);
intel_bb_destroy(ibb1);
gem_context_destroy(drm_fd, context0_id);
gem_context_destroy(drm_fd, context1_id);
buf_ops_destroy(bops);
__perf_close(stream_fd);
}
static void
gen12_test_single_ctx_render_target_writes_a_counter(const struct intel_execution_engine2 *e)
{
int child_ret;
struct igt_helper_process child = {};
/* Ensure perf_stream_paranoid is set to 1 by default */
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
do {
igt_fork_helper(&child) {
/* A local device for local resources. */
drm_fd = drm_reopen_driver(drm_fd);
igt_drop_root();
gen12_single_ctx_helper(e);
drm_close_driver(drm_fd);
}
child_ret = igt_wait_helper(&child);
igt_assert(WEXITSTATUS(child_ret) == EAGAIN ||
WEXITSTATUS(child_ret) == 0);
} while (WEXITSTATUS(child_ret) == EAGAIN);
}
static unsigned long rc6_residency_ms(void)
{
return sysfs_read(RC6_RESIDENCY_MS);
}
static void
test_rc6_disable(void)
{
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, default_test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
unsigned long rc6_start, rc6_end, rc6_enabled;
rc6_enabled = 0;
igt_sysfs_rps_scanf(sysfs, RC6_ENABLE, "%lu", &rc6_enabled);
igt_require(rc6_enabled);
/* Verify rc6 is functional by measuring residency while idle */
gem_quiescent_gpu(drm_fd);
rc6_start = rc6_residency_ms();
usleep(50000);
rc6_end = rc6_residency_ms();
igt_require(rc6_end != rc6_start);
/* While OA is active, we keep rc6 disabled so we don't lose metrics */
stream_fd = __perf_open(drm_fd, ¶m, false);
rc6_start = rc6_residency_ms();
usleep(50000);
rc6_end = rc6_residency_ms();
igt_assert_eq(rc6_end - rc6_start, 0);
__perf_close(stream_fd);
gem_quiescent_gpu(drm_fd);
/* But once OA is closed, we expect the device to sleep again */
rc6_start = rc6_residency_ms();
usleep(50000);
rc6_end = rc6_residency_ms();
igt_assert_neq(rc6_end - rc6_start, 0);
}
static void
test_stress_open_close(const struct intel_execution_engine2 *e)
{
struct intel_perf_metric_set *test_set = metric_set(e);
load_helper_init();
load_helper_run(HIGH);
igt_until_timeout(2) {
int oa_exponent = 5; /* 5 micro seconds */
uint64_t properties[] = {
/* XXX: even without periodic sampling we have to
* specify at least one sample layout property...
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exponent,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED, /* XXX: open disabled */
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
}
load_helper_stop();
load_helper_fini();
}
static uint64_t mask_minus_one(uint64_t mask)
{
unsigned int i;
for (i = 0; i < (sizeof(mask) * 8 - 1); i++) {
if ((1ULL << i) & mask)
return mask & ~(1ULL << i);
}
igt_assert(0);
return 0;
}
static uint64_t mask_plus_one(uint64_t mask)
{
unsigned int i;
for (i = 0; i < (sizeof(mask) * 8 - 1); i++) {
if (((1ULL << i) & mask) == 0)
return mask | (1ULL << i);
}
igt_assert(0);
return 0;
}
static void print_sseu_config(struct drm_i915_gem_context_param_sseu *sseu)
{
igt_debug(" engine class/instance=%hu:%hu\n",
sseu->engine.engine_class,
sseu->engine.engine_instance);
igt_debug(" slice_mask=0x%llx\n", sseu->slice_mask);
igt_debug(" subslice_mask=0x%llx\n", sseu->subslice_mask);
igt_debug(" eu min/max=%hu/%hu\n",
sseu->min_eus_per_subslice,
sseu->max_eus_per_subslice);
}
static struct drm_i915_gem_context_param_sseu
make_valid_reduced_sseu_config(struct drm_i915_gem_context_param_sseu default_sseu,
uint16_t class, uint16_t instance)
{
struct drm_i915_gem_context_param_sseu sseu = default_sseu;
if (intel_gen(devid) == 11) {
/*
* On Gen11 there are restrictions on what subslices
* can be disabled, notably we're not able to enable
* more than half the subslice. So disable half
* subslices only.
*/
for (int i = 0; i < DIV_ROUND_UP(__builtin_popcount(default_sseu.subslice_mask), 2); i++)
sseu.subslice_mask = mask_minus_one(sseu.subslice_mask);
} else {
sseu.subslice_mask = mask_minus_one(sseu.subslice_mask);
}
sseu.engine.engine_class = class;
sseu.engine.engine_instance = instance;
return sseu;
}
static void
test_global_sseu_config_invalid(const intel_ctx_t *ctx, const struct intel_execution_engine2 *e)
{
struct intel_perf_metric_set *test_set = metric_set(e);
struct drm_i915_gem_context_param_sseu default_sseu;
struct drm_i915_gem_context_param_sseu sseu_param;
struct drm_i915_gem_context_param ctx_gp = {
.ctx_id = ctx->id,
.param = I915_CONTEXT_PARAM_SSEU,
.size = sizeof(default_sseu),
.value = to_user_pointer(&default_sseu),
};
uint64_t properties[] = {
/* XXX: even without periodic sampling we have to
* specify at least one sample layout property...
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
DRM_I915_PERF_PROP_GLOBAL_SSEU, to_user_pointer(&sseu_param),
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED, /* XXX: open disabled */
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
memset(&default_sseu, 0, sizeof(default_sseu));
default_sseu.flags = I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX;
if (has_param_class_instance()) {
default_sseu.engine.engine_class = e->class;
default_sseu.engine.engine_instance = e->flags;
} else {
default_sseu.engine.engine_class = default_e2.class;
default_sseu.engine.engine_instance = default_e2.flags;
}
igt_require(__gem_context_get_param(drm_fd, &ctx_gp) == 0);
igt_debug("Default context sseu:\n");
print_sseu_config(&default_sseu);
/* Invalid engine class */
sseu_param = default_sseu;
sseu_param.engine.engine_class = -1;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
/* Invalid engine instance */
sseu_param = default_sseu;
sseu_param.engine.engine_instance = -1;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
/* Invalid slice mask */
sseu_param = default_sseu;
sseu_param.slice_mask = 0;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
sseu_param = default_sseu;
sseu_param.slice_mask = mask_plus_one(sseu_param.slice_mask);
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
/* Invalid subslice mask */
sseu_param = default_sseu;
sseu_param.subslice_mask = 0;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
sseu_param = default_sseu;
sseu_param.subslice_mask = mask_plus_one(sseu_param.subslice_mask);
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EINVAL);
/* Privileged operation */
if (__builtin_popcount(default_sseu.subslice_mask) > 1) {
igt_fork(child, 1) {
igt_drop_root();
sseu_param = make_valid_reduced_sseu_config(default_sseu,
e->class,
e->instance);
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m, EACCES);
}
igt_waitchildren();
}
}
static void
test_global_sseu_config(const intel_ctx_t *ctx, const struct intel_execution_engine2 *e)
{
struct intel_perf_metric_set *test_set = metric_set(e);
struct drm_i915_gem_context_param_sseu default_sseu;
struct drm_i915_gem_context_param_sseu sseu_param;
struct drm_i915_gem_context_param ctx_gp = {
.ctx_id = ctx->id,
.param = I915_CONTEXT_PARAM_SSEU,
.size = sizeof(default_sseu),
.value = to_user_pointer(&default_sseu),
};
uint64_t properties[] = {
/* XXX: even without periodic sampling we have to
* specify at least one sample layout property...
*/
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, test_set->perf_oa_metrics_set,
DRM_I915_PERF_PROP_OA_FORMAT, test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
DRM_I915_PERF_PROP_GLOBAL_SSEU, to_user_pointer(&sseu_param),
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, e->class,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, e->instance,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_DISABLED, /* XXX: open disabled */
.num_properties = has_param_class_instance() ?
ARRAY_SIZE(properties) / 2 :
(ARRAY_SIZE(properties) / 2) - 2,
.properties_ptr = to_user_pointer(properties),
};
memset(&default_sseu, 0, sizeof(default_sseu));
default_sseu.flags = I915_CONTEXT_SSEU_FLAG_ENGINE_INDEX;
if (has_param_class_instance()) {
default_sseu.engine.engine_class = e->class;
default_sseu.engine.engine_instance = e->flags;
} else {
default_sseu.engine.engine_class = default_e2.class;
default_sseu.engine.engine_instance = default_e2.flags;
}
igt_require(__gem_context_get_param(drm_fd, &ctx_gp) == 0);
igt_debug("Default context sseu:\n");
print_sseu_config(&default_sseu);
igt_require(__builtin_popcount(default_sseu.subslice_mask) > 1);
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 0);
sseu_param = make_valid_reduced_sseu_config(default_sseu,
e->class,
e->instance);
igt_debug("Selected context sseu:\n");
print_sseu_config(&sseu_param);
igt_fork(child, 1) {
igt_drop_root();
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
}
igt_waitchildren();
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
stream_fd = __perf_open(drm_fd, ¶m, false);
__perf_close(stream_fd);
}
static int __i915_perf_add_config(int fd, struct drm_i915_perf_oa_config *config)
{
int ret = igt_ioctl(fd, DRM_IOCTL_I915_PERF_ADD_CONFIG, config);
if (ret < 0)
ret = -errno;
return ret;
}
static int i915_perf_add_config(int fd, struct drm_i915_perf_oa_config *config)
{
int config_id = __i915_perf_add_config(fd, config);
igt_debug("config_id=%i\n", config_id);
igt_assert_lt(0, config_id);
return config_id;
}
static void i915_perf_remove_config(int fd, uint64_t config_id)
{
igt_assert_eq(igt_ioctl(fd, DRM_IOCTL_I915_PERF_REMOVE_CONFIG,
&config_id), 0);
}
static bool has_i915_perf_userspace_config(int fd)
{
uint64_t config = 0;
int ret = igt_ioctl(fd, DRM_IOCTL_I915_PERF_REMOVE_CONFIG, &config);
igt_assert_eq(ret, -1);
igt_debug("errno=%i\n", errno);
return errno != EINVAL;
}
static void
test_invalid_create_userspace_config(void)
{
struct drm_i915_perf_oa_config config;
const char *uuid = "01234567-0123-0123-0123-0123456789ab";
const char *invalid_uuid = "blablabla-wrong";
uint32_t mux_regs[] = { 0x9888 /* NOA_WRITE */, 0x0 };
uint32_t invalid_mux_regs[] = { 0x12345678 /* invalid register */, 0x0 };
igt_require(has_i915_perf_userspace_config(drm_fd));
memset(&config, 0, sizeof(config));
/* invalid uuid */
strncpy(config.uuid, invalid_uuid, sizeof(config.uuid));
config.n_mux_regs = 1;
config.mux_regs_ptr = to_user_pointer(mux_regs);
config.n_boolean_regs = 0;
config.n_flex_regs = 0;
igt_assert_eq(__i915_perf_add_config(drm_fd, &config), -EINVAL);
/* invalid mux_regs */
memcpy(config.uuid, uuid, sizeof(config.uuid));
config.n_mux_regs = 1;
config.mux_regs_ptr = to_user_pointer(invalid_mux_regs);
config.n_boolean_regs = 0;
config.n_flex_regs = 0;
igt_assert_eq(__i915_perf_add_config(drm_fd, &config), -EINVAL);
/* empty config */
memcpy(config.uuid, uuid, sizeof(config.uuid));
config.n_mux_regs = 0;
config.mux_regs_ptr = to_user_pointer(mux_regs);
config.n_boolean_regs = 0;
config.n_flex_regs = 0;
igt_assert_eq(__i915_perf_add_config(drm_fd, &config), -EINVAL);
/* empty config with null pointers */
memcpy(config.uuid, uuid, sizeof(config.uuid));
config.n_mux_regs = 1;
config.mux_regs_ptr = to_user_pointer(NULL);
config.n_boolean_regs = 2;
config.boolean_regs_ptr = to_user_pointer(NULL);
config.n_flex_regs = 3;
config.flex_regs_ptr = to_user_pointer(NULL);
igt_assert_eq(__i915_perf_add_config(drm_fd, &config), -EINVAL);
/* invalid pointers */
memcpy(config.uuid, uuid, sizeof(config.uuid));
config.n_mux_regs = 42;
config.mux_regs_ptr = to_user_pointer((void *) 0xDEADBEEF);
config.n_boolean_regs = 0;
config.n_flex_regs = 0;
igt_assert_eq(__i915_perf_add_config(drm_fd, &config), -EFAULT);
}
static void
test_invalid_remove_userspace_config(void)
{
struct drm_i915_perf_oa_config config;
const char *uuid = "01234567-0123-0123-0123-0123456789ab";
uint32_t mux_regs[] = { 0x9888 /* NOA_WRITE */, 0x0 };
uint64_t config_id, wrong_config_id = 999999999;
char path[512];
igt_require(has_i915_perf_userspace_config(drm_fd));
snprintf(path, sizeof(path), "metrics/%s/id", uuid);
/* Destroy previous configuration if present */
if (try_sysfs_read_u64(path, &config_id))
i915_perf_remove_config(drm_fd, config_id);
memset(&config, 0, sizeof(config));
memcpy(config.uuid, uuid, sizeof(config.uuid));
config.n_mux_regs = 1;
config.mux_regs_ptr = to_user_pointer(mux_regs);
config.n_boolean_regs = 0;
config.n_flex_regs = 0;
config_id = i915_perf_add_config(drm_fd, &config);
/* Removing configs without permissions should fail. */
igt_fork(child, 1) {
igt_drop_root();
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_REMOVE_CONFIG, &config_id, EACCES);
}
igt_waitchildren();
/* Removing invalid config ID should fail. */
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_REMOVE_CONFIG, &wrong_config_id, ENOENT);
i915_perf_remove_config(drm_fd, config_id);
}
static void
test_create_destroy_userspace_config(void)
{
struct drm_i915_perf_oa_config config;
const char *uuid = "01234567-0123-0123-0123-0123456789ab";
uint32_t mux_regs[] = { 0x9888 /* NOA_WRITE */, 0x0 };
uint32_t flex_regs[100];
int i;
uint64_t config_id;
uint64_t properties[] = {
DRM_I915_PERF_PROP_OA_METRICS_SET, 0, /* Filled later */
/* OA unit configuration */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
DRM_I915_PERF_PROP_OA_FORMAT, default_test_set->perf_oa_format,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
DRM_I915_PERF_PROP_OA_METRICS_SET
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC |
I915_PERF_FLAG_FD_NONBLOCK |
I915_PERF_FLAG_DISABLED,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
char path[512];
igt_require(has_i915_perf_userspace_config(drm_fd));
snprintf(path, sizeof(path), "metrics/%s/id", uuid);
/* Destroy previous configuration if present */
if (try_sysfs_read_u64(path, &config_id))
i915_perf_remove_config(drm_fd, config_id);
memset(&config, 0, sizeof(config));
memcpy(config.uuid, uuid, sizeof(config.uuid));
config.n_mux_regs = 1;
config.mux_regs_ptr = to_user_pointer(mux_regs);
/* Flex EU counters are only available on gen8+ */
if (intel_gen(devid) >= 8) {
for (i = 0; i < ARRAY_SIZE(flex_regs) / 2; i++) {
flex_regs[i * 2] = 0xe458; /* EU_PERF_CNTL0 */
flex_regs[i * 2 + 1] = 0x0;
}
config.flex_regs_ptr = to_user_pointer(flex_regs);
config.n_flex_regs = ARRAY_SIZE(flex_regs) / 2;
}
config.n_boolean_regs = 0;
/* Creating configs without permissions shouldn't work. */
igt_fork(child, 1) {
igt_drop_root();
igt_assert_eq(__i915_perf_add_config(drm_fd, &config), -EACCES);
}
igt_waitchildren();
/* Create a new config */
config_id = i915_perf_add_config(drm_fd, &config);
/* Verify that adding the another config with the same uuid fails. */
igt_assert_eq(__i915_perf_add_config(drm_fd, &config), -EADDRINUSE);
/* Try to use the new config */
properties[1] = config_id;
stream_fd = __perf_open(drm_fd, ¶m, false);
/* Verify that destroying the config doesn't yield any error. */
i915_perf_remove_config(drm_fd, config_id);
/* Read the config to verify shouldn't raise any issue. */
config_id = i915_perf_add_config(drm_fd, &config);
__perf_close(stream_fd);
i915_perf_remove_config(drm_fd, config_id);
}
/* Registers required by userspace. This list should be maintained by
* the OA configs developers and agreed upon with kernel developers as
* some of the registers have bits used by the kernel (for workarounds
* for instance) and other bits that need to be set by the OA configs.
*/
static void
test_whitelisted_registers_userspace_config(void)
{
struct drm_i915_perf_oa_config config;
const char *uuid = "01234567-0123-0123-0123-0123456789ab";
uint32_t mux_regs[200];
uint32_t b_counters_regs[200];
uint32_t flex_regs[200];
uint32_t i;
uint32_t oa_start_trig1, oa_start_trig8;
uint32_t oa_report_trig1, oa_report_trig8;
uint64_t config_id;
char path[512];
int ret;
const uint32_t flex[] = {
0xe458,
0xe558,
0xe658,
0xe758,
0xe45c,
0xe55c,
0xe65c
};
igt_require(has_i915_perf_userspace_config(drm_fd));
snprintf(path, sizeof(path), "metrics/%s/id", uuid);
if (try_sysfs_read_u64(path, &config_id))
i915_perf_remove_config(drm_fd, config_id);
memset(&config, 0, sizeof(config));
memcpy(config.uuid, uuid, sizeof(config.uuid));
if (intel_gen(devid) >= 12) {
oa_start_trig1 = 0xd900;
oa_start_trig8 = 0xd91c;
oa_report_trig1 = 0xd920;
oa_report_trig8 = 0xd93c;
} else {
oa_start_trig1 = 0x2710;
oa_start_trig8 = 0x272c;
oa_report_trig1 = 0x2740;
oa_report_trig8 = 0x275c;
}
/* OASTARTTRIG[1-8] */
for (i = oa_start_trig1; i <= oa_start_trig8; i += 4) {
b_counters_regs[config.n_boolean_regs * 2] = i;
b_counters_regs[config.n_boolean_regs * 2 + 1] = 0;
config.n_boolean_regs++;
}
/* OAREPORTTRIG[1-8] */
for (i = oa_report_trig1; i <= oa_report_trig8; i += 4) {
b_counters_regs[config.n_boolean_regs * 2] = i;
b_counters_regs[config.n_boolean_regs * 2 + 1] = 0;
config.n_boolean_regs++;
}
config.boolean_regs_ptr = (uintptr_t) b_counters_regs;
if (intel_gen(devid) >= 8) {
/* Flex EU registers, only from Gen8+. */
for (i = 0; i < ARRAY_SIZE(flex); i++) {
flex_regs[config.n_flex_regs * 2] = flex[i];
flex_regs[config.n_flex_regs * 2 + 1] = 0;
config.n_flex_regs++;
}
config.flex_regs_ptr = (uintptr_t) flex_regs;
}
/* Mux registers (too many of them, just checking bounds) */
i = 0;
/* NOA_WRITE */
mux_regs[i++] = 0x9888;
mux_regs[i++] = 0;
if (IS_HASWELL(devid)) {
/* Haswell specific. undocumented... */
mux_regs[i++] = 0x9ec0;
mux_regs[i++] = 0;
mux_regs[i++] = 0x25100;
mux_regs[i++] = 0;
mux_regs[i++] = 0x2ff90;
mux_regs[i++] = 0;
}
if (intel_gen(devid) >= 8 && !IS_CHERRYVIEW(devid)) {
/* NOA_CONFIG */
mux_regs[i++] = 0xD04;
mux_regs[i++] = 0;
mux_regs[i++] = 0xD2C;
mux_regs[i++] = 0;
if (!IS_METEORLAKE(devid)) {
/* WAIT_FOR_RC6_EXIT */
mux_regs[i++] = 0x20CC;
mux_regs[i++] = 0;
}
}
if (IS_CHERRYVIEW(devid)) {
/* Cherryview specific. undocumented... */
mux_regs[i++] = 0x182300;
mux_regs[i++] = 0;
mux_regs[i++] = 0x1823A4;
mux_regs[i++] = 0;
}
if (intel_gen(devid) <= 11) {
/* HALF_SLICE_CHICKEN2 (shared with kernel workaround) */
mux_regs[i++] = 0xE180;
mux_regs[i++] = 0;
/* PERFCNT[12] */
mux_regs[i++] = 0x91B8;
mux_regs[i++] = 0;
/* PERFMATRIX */
mux_regs[i++] = 0x91C8;
mux_regs[i++] = 0;
}
config.mux_regs_ptr = (uintptr_t) mux_regs;
config.n_mux_regs = i / 2;
/* Create a new config */
ret = igt_ioctl(drm_fd, DRM_IOCTL_I915_PERF_ADD_CONFIG, &config);
igt_assert_lt(0, ret); /* Config 0 should be used by the kernel */
config_id = ret;
i915_perf_remove_config(drm_fd, config_id);
}
static unsigned
read_i915_module_ref(void)
{
FILE *fp = fopen("/proc/modules", "r");
char *line = NULL;
size_t line_buf_size = 0;
int len = 0;
unsigned ref_count;
igt_assert(fp);
while ((len = getline(&line, &line_buf_size, fp)) > 0) {
if (strncmp(line, "i915 ", 5) == 0) {
unsigned long mem;
int ret = sscanf(line + 5, "%lu %u", &mem, &ref_count);
igt_assert_eq(ret, 2);
goto done;
}
}
igt_assert(!"reached");
done:
free(line);
fclose(fp);
return ref_count;
}
static int perf_sysfs_open(int i915)
{
int dirfd, gt;
/* use the first available sysfs interface */
for_each_sysfs_gt_dirfd(i915, dirfd, gt)
break;
igt_assert_neq(dirfd, -1);
return dirfd;
}
/* check that an open i915 perf stream holds a reference on the drm i915 module
* including in the corner case where the original drm fd has been closed.
*/
static void
test_i915_ref_count(void)
{
uint64_t properties[] = {
/* Include OA reports in samples */
DRM_I915_PERF_PROP_SAMPLE_OA, true,
/* OA unit configuration */
DRM_I915_PERF_PROP_OA_METRICS_SET, 0 /* updated below */,
DRM_I915_PERF_PROP_OA_FORMAT, 0, /* update below */
DRM_I915_PERF_PROP_OA_EXPONENT, 0, /* update below */
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
.num_properties = ARRAY_SIZE(properties) / 2,
.properties_ptr = to_user_pointer(properties),
};
unsigned baseline, ref_count0, ref_count1;
uint32_t oa_report0[64];
uint32_t oa_report1[64];
/* This should be the first test before the first fixture so no drm_fd
* should have been opened so far...
*/
igt_assert_eq(drm_fd, -1);
baseline = read_i915_module_ref();
igt_debug("baseline ref count (drm fd closed) = %u\n", baseline);
drm_fd = __drm_open_driver(DRIVER_INTEL);
igt_require_i915(drm_fd);
devid = intel_get_drm_devid(drm_fd);
sysfs = perf_sysfs_open(drm_fd);
/* Note: these global variables are only initialized after calling
* init_sys_info()...
*/
igt_require(init_sys_info());
properties[3] = default_test_set->perf_oa_metrics_set;
properties[5] = default_test_set->perf_oa_format;
properties[7] = oa_exp_1_millisec;
ref_count0 = read_i915_module_ref();
igt_debug("initial ref count with drm_fd open = %u\n", ref_count0);
igt_assert(ref_count0 > baseline);
stream_fd = __perf_open(drm_fd, ¶m, false);
ref_count1 = read_i915_module_ref();
igt_debug("ref count after opening i915 perf stream = %u\n", ref_count1);
igt_assert(ref_count1 > ref_count0);
drm_close_driver(drm_fd);
close(sysfs);
drm_fd = -1;
sysfs = -1;
ref_count0 = read_i915_module_ref();
igt_debug("ref count after closing drm fd = %u\n", ref_count0);
igt_assert(ref_count0 > baseline);
read_2_oa_reports(default_test_set->perf_oa_format,
oa_exp_1_millisec,
oa_report0,
oa_report1,
false); /* not just timer reports */
__perf_close(stream_fd);
ref_count0 = read_i915_module_ref();
igt_debug("ref count after closing i915 perf stream fd = %u\n", ref_count0);
igt_assert_eq(ref_count0, baseline);
}
static void
test_sysctl_defaults(void)
{
int paranoid = read_u64_file("/proc/sys/dev/i915/perf_stream_paranoid");
int max_freq = read_u64_file("/proc/sys/dev/i915/oa_max_sample_rate");
igt_assert_eq(paranoid, 1);
igt_assert_eq(max_freq, 100000);
}
static struct intel_execution_engine2 *
__ci_to_e2(const intel_ctx_t *ctx, struct i915_engine_class_instance *ci)
{
static struct intel_execution_engine2 e2;
struct intel_execution_engine2 *e;
for_each_ctx_engine(drm_fd, ctx, e) {
if (e->class == ci->engine_class && e->instance == ci->engine_instance) {
e2 = *e;
break;
}
}
return &e2;
}
#define __for_random_engine_in_each_group(groups_, ctx_, e_) \
for (int i_ = 0; \
i_ < num_perf_oa_groups && !!(e_ = __ci_to_e2(ctx_, random_engine(&groups_[i_]))); \
i_++) \
igt_dynamic_f("%d-%s", i_, e_->name)
#define __for_each_render_engine(fd__, e__) \
for_each_physical_engine(fd__, e__) \
if (e__->class == I915_ENGINE_CLASS_RENDER) \
igt_dynamic_f("%s", e__->name)
struct perf_engine_group {
/* exclusive perf fd per engine group */
int perf_fd;
/* gem context id passed to perf */
uint32_t ctx_id;
uint32_t oa_unit_id;
/* perf engines in a group */
int num_engines;
struct i915_engine_class_instance *ci;
int gt;
struct intel_perf_metric_set *test_set;
};
static struct drm_i915_query_engine_info *query_engine_info(int i915)
{
struct drm_i915_query_engine_info *qinfo;
#define QUERY_SIZE (0x4000)
qinfo = malloc(QUERY_SIZE);
igt_assert(qinfo);
memset(qinfo, 0, QUERY_SIZE);
igt_assert(!__gem_query_engines(i915, qinfo, QUERY_SIZE));
#undef QUERY_SIZE
return qinfo;
}
static struct intel_perf_metric_set *metric_set(const struct intel_execution_engine2 *e2)
{
const char *test_set_name = NULL;
struct intel_perf_metric_set *metric_set_iter;
struct intel_perf_metric_set *test_set = NULL;
if (IS_HASWELL(devid))
test_set_name = "RenderBasic";
else if (e2->class == I915_ENGINE_CLASS_RENDER)
test_set_name = "TestOa";
else if ((e2->class == I915_ENGINE_CLASS_VIDEO ||
e2->class == I915_ENGINE_CLASS_VIDEO_ENHANCE) &&
HAS_OAM(devid))
test_set_name = "MediaSet1";
else
igt_assert(!"reached");
igt_list_for_each_entry(metric_set_iter, &intel_perf->metric_sets, link) {
if (strcmp(metric_set_iter->symbol_name, test_set_name) == 0) {
test_set = metric_set_iter;
break;
}
}
igt_assert(test_set);
/*
* configuration was loaded in init_sys_info() ->
* intel_perf_load_perf_configs(), and test_set->perf_oa_metrics_set
* should point to metric id returned by the config add ioctl. 0 is
* invalid.
*/
igt_assert_neq_u64(test_set->perf_oa_metrics_set, 0);
igt_debug("engine %d:%d - %s metric set UUID = %s\n",
e2->class,
e2->instance,
test_set->symbol_name,
test_set->hw_config_guid);
return test_set;
}
static int compare_engine_oa_unit_id(const void *e1, const void *e2)
{
const struct drm_i915_engine_info *_e1 = e1;
const struct drm_i915_engine_info *_e2 = e2;
return (int)_e1->rsvd0 - (int)_e2->rsvd0;
}
static struct perf_engine_group *default_engine_group(uint32_t *num_groups)
{
struct perf_engine_group *groups = malloc(sizeof(*groups));
igt_debug("using default engine group\n");
groups->perf_fd = -1,
groups->ctx_id = 0xffffffff,
groups->oa_unit_id = 0,
groups->num_engines = 1,
groups->ci = malloc(sizeof(*groups->ci));
groups->ci->engine_class = default_e2.class;
groups->ci->engine_instance = default_e2.instance;
*num_groups = 1;
return groups;
}
/* Until oa_unit_id is exposed from uapi, work around it */
static void populate_mtl_oa_unit_ids(struct drm_i915_query_engine_info *qinfo)
{
struct i915_engine_class_instance ci;
int i;
for (i = 0; i < qinfo->num_engines; i++) {
ci = qinfo->engines[i].engine;
switch (ci.engine_class) {
case I915_ENGINE_CLASS_RENDER:
qinfo->engines[i].rsvd0 = 0;
break;
case I915_ENGINE_CLASS_VIDEO:
case I915_ENGINE_CLASS_VIDEO_ENHANCE:
if (i915_perf_revision(drm_fd) >= 7)
qinfo->engines[i].rsvd0 = 1;
else
qinfo->engines[i].rsvd0 = UINT32_MAX;
break;
default:
qinfo->engines[i].rsvd0 = UINT32_MAX;
break;
}
igt_debug("class:instance = %d:%d, id = %d\n",
ci.engine_class, ci.engine_instance,
qinfo->engines[i].rsvd0);
}
}
static struct perf_engine_group *get_engine_groups(int i915, uint32_t *num_groups)
{
struct drm_i915_query_engine_info *qinfo;
struct perf_engine_group *groups = NULL;
uint32_t id = UINT32_MAX, num_grps = 0, i = 0, j;
qinfo = query_engine_info(i915);
if (!qinfo)
return default_engine_group(num_groups);
igt_assert(qinfo->num_engines);
/* Currently only meteorlake is supported with engine groups */
if (IS_METEORLAKE(devid)) {
populate_mtl_oa_unit_ids(qinfo);
} else {
free(qinfo);
return default_engine_group(num_groups);
}
/* sort so that engines with same oa id are together */
qsort(qinfo->engines, qinfo->num_engines, sizeof(qinfo->engines[0]),
compare_engine_oa_unit_id);
/* create groups */
for (i = 0; i < qinfo->num_engines; i++) {
struct i915_engine_class_instance ci = qinfo->engines[i].engine;
igt_debug("class:instance = %d:%d, id = %d\n",
ci.engine_class, ci.engine_instance,
qinfo->engines[i].rsvd0);
if (qinfo->engines[i].rsvd0 == UINT32_MAX)
continue;
if (qinfo->engines[i].rsvd0 != id) {
id = qinfo->engines[i].rsvd0;
groups = realloc(groups, ++num_grps * sizeof(*groups));
j = num_grps - 1;
groups[j].perf_fd = -1;
groups[j].ctx_id = 0xffffffff;
groups[j].oa_unit_id = id;
groups[j].num_engines = 0;
/* alloc max engines, trim later */
groups[j].ci = malloc(qinfo->num_engines * sizeof(ci));
}
groups[j].ci[groups[j].num_engines++] = ci;
}
igt_assert(num_grps);
/* trim engines */
for (i = 0; i < num_grps; i++) {
struct i915_engine_class_instance *ci = groups[i].ci;
ci = realloc(ci, groups[i].num_engines * sizeof(*ci));
groups[i].ci = ci;
}
*num_groups = num_grps;
free(qinfo);
return groups;
}
static void put_engine_groups(struct perf_engine_group *groups,
unsigned int num_groups)
{
int i;
for (i = 0; i < num_groups; i++)
free(groups[i].ci);
free(groups);
}
static struct i915_engine_class_instance *
random_engine(struct perf_engine_group *group)
{
srandom(time(NULL));
return &group->ci[random() % group->num_engines];
}
static bool has_class_instance(int i915, uint16_t class, uint16_t instance)
{
int fd;
fd = perf_i915_open(i915, I915_PMU_ENGINE_BUSY(class, instance));
if (fd >= 0) {
close(fd);
return true;
}
return false;
}
static void set_default_engine(const intel_ctx_t *ctx)
{
const struct intel_execution_engine2 *e;
for_each_ctx_engine(drm_fd, ctx, e)
if (e->class == I915_ENGINE_CLASS_RENDER && e->instance == 0)
default_e2 = *e;
}
/*
* Test if OA buffer streams can be independently opened on each group. Once a user
* opens a stream, that group is exclusive to the user, other users get -EBUSY on
* trying to open a stream.
*/
static void
test_group_exclusive_stream(const intel_ctx_t *ctx, bool exponent)
{
uint64_t properties[] = {
DRM_I915_PERF_PROP_SAMPLE_OA, true,
DRM_I915_PERF_PROP_OA_METRICS_SET, 0,
DRM_I915_PERF_PROP_OA_FORMAT, 0,
DRM_I915_PERF_PROP_OA_ENGINE_CLASS, 0,
DRM_I915_PERF_PROP_OA_ENGINE_INSTANCE, 0,
DRM_I915_PERF_PROP_OA_EXPONENT, oa_exp_1_millisec,
};
struct drm_i915_perf_open_param param = {
.flags = I915_PERF_FLAG_FD_CLOEXEC,
/* for gem_context use case, we do no pass exponent */
.num_properties = exponent ?
ARRAY_SIZE(properties) / 2 :
ARRAY_SIZE(properties) / 2 - 1,
.properties_ptr = to_user_pointer(properties),
};
uint32_t i, j;
/* for each group, open one random perf stream with sample OA */
for (i = 0; i < num_perf_oa_groups; i++) {
struct perf_engine_group *grp = &perf_oa_groups[i];
struct i915_engine_class_instance *ci = random_engine(grp);
struct intel_execution_engine2 *e2 = __ci_to_e2(ctx, ci);
struct intel_perf_metric_set *test_set = metric_set(e2);
if (!exponent) {
properties[0] = DRM_I915_PERF_PROP_CTX_HANDLE;
properties[1] = ctx->id;
}
properties[3] = test_set->perf_oa_metrics_set;
properties[5] = test_set->perf_oa_format;
properties[7] = ci->engine_class;
properties[9] = ci->engine_instance;
grp->perf_fd = igt_ioctl(drm_fd,
DRM_IOCTL_I915_PERF_OPEN,
¶m);
igt_assert_lte(0, grp->perf_fd);
igt_debug("opened OA buffer with c:i %d:%d\n",
ci->engine_class, ci->engine_instance);
}
/* for each group make sure no other streams can be opened */
for (i = 0; i < num_perf_oa_groups; i++) {
struct perf_engine_group *grp = &perf_oa_groups[i];
int err;
for (j = 0; j < grp->num_engines; j++) {
struct i915_engine_class_instance *ci = grp->ci + j;
struct intel_execution_engine2 *e2 = __ci_to_e2(ctx, ci);
struct intel_perf_metric_set *test_set = metric_set(e2);
/*
* case 1:
* concurrent access to OAG should fail
*/
properties[0] = DRM_I915_PERF_PROP_SAMPLE_OA;
properties[1] = true;
properties[3] = test_set->perf_oa_metrics_set;
properties[5] = test_set->perf_oa_format;
properties[7] = ci->engine_class;
properties[9] = ci->engine_instance;
/* for SAMPLE OA use case, we must pass exponent */
param.num_properties = ARRAY_SIZE(properties) / 2;
do_ioctl_err(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m,
EBUSY);
igt_debug("try OA buffer with c:i %d:%d\n",
ci->engine_class, ci->engine_instance);
/*
* case 2:
* concurrent access to non-OAG unit should fail
*/
properties[0] = DRM_I915_PERF_PROP_CTX_HANDLE;
properties[1] = gem_context_create(drm_fd);
/* for gem_context use case, we do no pass exponent */
param.num_properties = ARRAY_SIZE(properties) / 2 - 1;
errno = 0;
err = igt_ioctl(drm_fd, DRM_IOCTL_I915_PERF_OPEN, ¶m);
igt_assert_lt(err, 0);
igt_assert(errno == EBUSY || errno == ENODEV);
igt_debug("try OA ci unit with c:i %d:%d\n",
ci->engine_class, ci->engine_instance);
gem_context_destroy(drm_fd, properties[1]);
}
if (grp->perf_fd >= 0)
close(grp->perf_fd);
}
}
static void
test_group_concurrent_oa_buffer_read(void)
{
igt_fork(child, num_perf_oa_groups) {
struct intel_execution_engine2 e;
e.class = perf_oa_groups[child].ci->engine_class;
e.instance = perf_oa_groups[child].ci->engine_instance;
test_blocking(40 * 1000 * 1000, false, 5 * 1000 * 1000, &e);
}
igt_waitchildren();
}
igt_main
{
const intel_ctx_t *ctx;
const struct intel_execution_engine2 *e;
igt_fixture {
struct stat sb;
/*
* Prior tests may have unloaded i915 or failed while
* loading/unloading i915. Load i915 here before we stat the
* files.
*/
drm_load_module(DRIVER_INTEL);
igt_require(stat("/proc/sys/dev/i915/perf_stream_paranoid", &sb)
== 0);
igt_require(stat("/proc/sys/dev/i915/oa_max_sample_rate", &sb)
== 0);
}
igt_subtest("i915-ref-count")
test_i915_ref_count();
igt_subtest("sysctl-defaults")
test_sysctl_defaults();
igt_fixture {
/* We expect that the ref count test before these fixtures
* should have closed drm_fd...
*/
igt_assert_eq(drm_fd, -1);
/* Avoid the normal exithandler, our perf-fd interferes */
drm_fd = __drm_open_driver(DRIVER_INTEL);
igt_require_gem(drm_fd);
devid = intel_get_drm_devid(drm_fd);
sysfs = perf_sysfs_open(drm_fd);
igt_require(init_sys_info());
ctx = intel_ctx_create_all_physical(drm_fd);
set_default_engine(ctx);
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
write_u64_file("/proc/sys/dev/i915/oa_max_sample_rate", 100000);
gt_max_freq_mhz = sysfs_read(RPS_RP0_FREQ_MHZ);
perf_oa_groups = get_engine_groups(drm_fd, &num_perf_oa_groups);
igt_assert(perf_oa_groups && num_perf_oa_groups);
if (has_class_instance(drm_fd, I915_ENGINE_CLASS_RENDER, 0))
render_copy = igt_get_render_copyfunc(drm_fd);
}
igt_subtest("non-system-wide-paranoid")
test_system_wide_paranoid();
igt_subtest("invalid-open-flags")
test_invalid_open_flags();
igt_subtest("invalid-oa-metric-set-id")
test_invalid_oa_metric_set_id();
igt_subtest("invalid-oa-format-id")
test_invalid_oa_format_id();
igt_subtest("missing-sample-flags")
test_missing_sample_flags();
igt_subtest_with_dynamic("oa-formats")
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_oa_formats(e);
igt_subtest("invalid-oa-exponent")
test_invalid_oa_exponent();
igt_subtest("low-oa-exponent-permissions")
test_low_oa_exponent_permissions();
igt_subtest_with_dynamic("oa-exponents")
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_oa_exponents(e);
igt_subtest("per-context-mode-unprivileged") {
igt_require(IS_HASWELL(devid));
test_per_context_mode_unprivileged();
}
igt_subtest_with_dynamic("buffer-fill")
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_buffer_fill(e);
igt_describe("Test that reason field in OA reports is never 0 on Gen8+");
igt_subtest_with_dynamic("non-zero-reason") {
/* Reason field is only available on Gen8+ */
igt_require(intel_gen(devid) >= 8);
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_non_zero_reason(e);
}
igt_subtest("disabled-read-error")
test_disabled_read_error();
igt_subtest("non-sampling-read-error")
test_non_sampling_read_error();
igt_subtest_with_dynamic("enable-disable")
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_enable_disable(e);
igt_describe("Test blocking read with default hrtimer frequency");
igt_subtest_with_dynamic("blocking") {
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_blocking(40 * 1000 * 1000 /* 40ms oa period */,
false /* set_kernel_hrtimer */,
5 * 1000 * 1000 /* default 5ms/200Hz hrtimer */,
e);
}
igt_describe("Test blocking read with different hrtimer frequencies");
igt_subtest("blocking-parameterized") {
const struct intel_execution_engine2 _e = {
.class = default_e2.class,
.instance = default_e2.instance,
};
igt_require(i915_perf_revision(drm_fd) >= 5);
test_blocking(10 * 1000 * 1000 /* 10ms oa period */,
true /* set_kernel_hrtimer */,
40 * 1000 * 1000 /* default 40ms hrtimer */,
&_e);
test_blocking(500 * 1000 /* 500us oa period */,
true /* set_kernel_hrtimer */,
2 * 1000 * 1000 /* default 2ms hrtimer */,
&_e);
}
igt_describe("Test polled read with default hrtimer frequency");
igt_subtest_with_dynamic("polling") {
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_polling(40 * 1000 * 1000 /* 40ms oa period */,
false /* set_kernel_hrtimer */,
5 * 1000 * 1000 /* default 5ms/200Hz hrtimer */,
e);
}
igt_describe("Test polled read with different hrtimer frequencies");
igt_subtest("polling-parameterized") {
const struct intel_execution_engine2 _e = {
.class = default_e2.class,
.instance = default_e2.instance,
};
igt_require(i915_perf_revision(drm_fd) >= 5);
test_polling(10 * 1000 * 1000 /* 10ms oa period */,
true /* set_kernel_hrtimer */,
40 * 1000 * 1000 /* default 40ms hrtimer */,
&_e);
test_polling(500 * 1000 /* 500us oa period */,
true /* set_kernel_hrtimer */,
2 * 1000 * 1000 /* default 2ms hrtimer */,
&_e);
}
igt_describe("Test polled read with buffer size smaller than available data");
igt_subtest("polling-small-buf")
test_polling_small_buf();
igt_subtest("short-reads")
test_short_reads();
igt_subtest("mi-rpc") {
igt_require(intel_gen(devid) < 12);
test_mi_rpc();
}
igt_subtest("unprivileged-single-ctx-counters") {
igt_require(IS_HASWELL(devid));
igt_require_f(render_copy, "no render-copy function\n");
hsw_test_single_ctx_counters();
}
igt_subtest("gen8-unprivileged-single-ctx-counters") {
/* For Gen8+ the OA unit can no longer be made to clock gate
* for a specific context. Additionally the partial-replacement
* functionality to HW filter timer reports for a specific
* context (SKL+) can't stop multiple applications viewing
* system-wide data via MI_REPORT_PERF_COUNT commands.
*
* For gen12 implement a separate test that uses only OAR
*/
igt_require(intel_gen(devid) >= 8 && intel_gen(devid) < 12);
igt_require_f(render_copy, "no render-copy function\n");
gen8_test_single_ctx_render_target_writes_a_counter();
}
igt_subtest_group {
igt_describe("Test MI REPORT PERF COUNT for Gen 12");
igt_subtest_with_dynamic("gen12-mi-rpc") {
igt_require(intel_gen(devid) >= 12);
igt_require(has_class_instance(drm_fd, I915_ENGINE_CLASS_RENDER, 0));
__for_each_render_engine(drm_fd, e)
gen12_test_mi_rpc(e);
}
igt_describe("Test OA TLB invalidate");
igt_subtest_with_dynamic("gen12-oa-tlb-invalidate") {
igt_require(intel_gen(devid) >= 12);
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
gen12_test_oa_tlb_invalidate(e);
}
igt_describe("Measure performance for a specific context using OAR in Gen 12");
igt_subtest_with_dynamic("gen12-unprivileged-single-ctx-counters") {
igt_require(intel_gen(devid) >= 12);
igt_require(has_class_instance(drm_fd, I915_ENGINE_CLASS_RENDER, 0));
igt_require_f(render_copy, "no render-copy function\n");
__for_each_render_engine(drm_fd, e)
gen12_test_single_ctx_render_target_writes_a_counter(e);
}
}
igt_subtest_group {
igt_describe("Verify invalid class instance");
igt_subtest("gen12-invalid-class-instance") {
igt_require(i915_perf_revision(drm_fd) >= 6);
test_invalid_class_instance();
}
/*
* OAR and OAG use cases can be separately opened only on gen12
* and later, so group-exclusive-stream tests require >= 12.
*/
igt_describe("Verify exclusivity of perf streams with sample oa option");
igt_subtest("gen12-group-exclusive-stream-sample-oa") {
igt_require(intel_gen(devid) >= 12);
test_group_exclusive_stream(ctx, true);
}
igt_describe("Verify exclusivity of perf streams with ctx handle");
igt_subtest("gen12-group-exclusive-stream-ctx-handle") {
igt_require(intel_gen(devid) >= 12);
test_group_exclusive_stream(ctx, false);
}
igt_describe("Verify concurrent reads from OA buffers in different groups");
igt_subtest("gen12-group-concurrent-oa-buffer-read") {
igt_require(i915_perf_revision(drm_fd) >= 6);
test_group_concurrent_oa_buffer_read();
}
}
igt_subtest("rc6-disable")
test_rc6_disable();
igt_describe("Stress tests opening & closing the i915-perf stream in a busy loop");
igt_subtest_with_dynamic("stress-open-close")
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_stress_open_close(e);
igt_subtest_group {
igt_describe("Verify invalid SSEU opening parameters");
igt_subtest_with_dynamic("global-sseu-config-invalid") {
igt_require(i915_perf_revision(drm_fd) >= 4);
igt_require(intel_graphics_ver(devid) < IP_VER(12, 50));
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_global_sseu_config_invalid(ctx, e);
}
igt_describe("Verify specifying SSEU opening parameters");
igt_subtest_with_dynamic("global-sseu-config") {
igt_require(i915_perf_revision(drm_fd) >= 4);
igt_require(intel_graphics_ver(devid) < IP_VER(12, 50));
__for_random_engine_in_each_group(perf_oa_groups, ctx, e)
test_global_sseu_config(ctx, e);
}
}
igt_subtest("invalid-create-userspace-config")
test_invalid_create_userspace_config();
igt_subtest("invalid-remove-userspace-config")
test_invalid_remove_userspace_config();
igt_subtest("create-destroy-userspace-config")
test_create_destroy_userspace_config();
igt_subtest("whitelisted-registers-userspace-config")
test_whitelisted_registers_userspace_config();
igt_fixture {
/* leave sysctl options in their default state... */
write_u64_file("/proc/sys/dev/i915/oa_max_sample_rate", 100000);
write_u64_file("/proc/sys/dev/i915/perf_stream_paranoid", 1);
if (intel_perf)
intel_perf_free(intel_perf);
if (perf_oa_groups)
put_engine_groups(perf_oa_groups, num_perf_oa_groups);
intel_ctx_destroy(drm_fd, ctx);
drm_close_driver(drm_fd);
}
}
|