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
|
/*
* Copyright © 2007-2023 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 "igt_device_scan.h"
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include <sys/sysmacros.h>
#include "igt_perf.h"
#include "igt_drm_clients.h"
#include "igt_drm_fdinfo.h"
#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
struct pmu_pair {
uint64_t cur;
uint64_t prev;
};
struct pmu_counter {
uint64_t type;
uint64_t config;
unsigned int idx;
struct pmu_pair val;
double scale;
const char *units;
bool present;
};
struct engine_class {
unsigned int engine_class;
const char *name;
unsigned int num_engines;
};
struct engine {
const char *name;
char *display_name;
char *short_name;
unsigned int class;
unsigned int instance;
unsigned int num_counters;
struct pmu_counter busy;
struct pmu_counter wait;
struct pmu_counter sema;
};
#define MAX_GTS 4
struct engines {
unsigned int num_engines;
unsigned int num_classes;
struct engine_class *class;
unsigned int num_counters;
DIR *root;
int fd;
struct pmu_pair ts;
int rapl_fd;
struct pmu_counter r_gpu, r_pkg;
unsigned int num_rapl;
int imc_fd;
struct pmu_counter imc_reads;
struct pmu_counter imc_writes;
unsigned int num_imc;
struct pmu_counter freq_req;
struct pmu_counter freq_req_gt[MAX_GTS];
struct pmu_counter freq_act;
struct pmu_counter freq_act_gt[MAX_GTS];
struct pmu_counter irq;
struct pmu_counter rc6;
struct pmu_counter rc6_gt[MAX_GTS];
bool discrete;
char *device;
int num_gts;
/* Do not edit below this line.
* This structure is reallocated every time a new engine is
* found and size is increased by sizeof (engine).
*/
struct engine engine;
};
struct intel_clients {
const char *pci_slot;
struct igt_drm_client_engines classes;
struct igt_drm_clients *clients;
struct igt_drm_client_regions *regions; /* Borrowed from first client */
};
static struct termios termios_orig;
static bool class_view;
static bool aggregate_regions;
/* Maps i915 fdinfo names to indices */
static const char *memory_region_map[] = {
"system0",
"local0",
};
/* For JSON, 1:1 with indices above. */
static const char *json_memory_region_names[] = {
"system",
"local",
};
__attribute__((format(scanf,3,4)))
static int igt_sysfs_scanf(int dir, const char *attr, const char *fmt, ...)
{
FILE *file;
int fd;
int ret = -1;
fd = openat(dir, attr, O_RDONLY);
if (fd < 0)
return -1;
file = fdopen(fd, "r");
if (file) {
va_list ap;
va_start(ap, fmt);
ret = vfscanf(file, fmt, ap);
va_end(ap);
fclose(file);
} else {
close(fd);
}
return ret;
}
static int pmu_parse(struct pmu_counter *pmu, const char *path, const char *str)
{
locale_t locale, oldlocale;
bool result = true;
char buf[128] = {};
int dir;
dir = open(path, O_RDONLY);
if (dir < 0)
return -errno;
/* Replace user environment with plain C to match kernel format */
locale = newlocale(LC_ALL, "C", 0);
oldlocale = uselocale(locale);
result &= igt_sysfs_scanf(dir, "type", "%"PRIu64, &pmu->type) == 1;
snprintf(buf, sizeof(buf) - 1, "events/%s", str);
result &= igt_sysfs_scanf(dir, buf, "event=%"PRIx64, &pmu->config) == 1;
snprintf(buf, sizeof(buf) - 1, "events/%s.scale", str);
result &= igt_sysfs_scanf(dir, buf, "%lf", &pmu->scale) == 1;
snprintf(buf, sizeof(buf) - 1, "events/%s.unit", str);
result &= igt_sysfs_scanf(dir, buf, "%127s", buf) == 1;
pmu->units = strdup(buf);
uselocale(oldlocale);
freelocale(locale);
close(dir);
if (!result)
return -EINVAL;
if (isnan(pmu->scale) || !pmu->scale)
return -ERANGE;
return 0;
}
static int rapl_parse(struct pmu_counter *pmu, const char *str)
{
const char *expected_units = "Joules";
int err;
err = pmu_parse(pmu, "/sys/devices/power", str);
if (err < 0)
return err;
if (!pmu->units || strcmp(pmu->units, expected_units)) {
fprintf(stderr,
"Unexpected units for RAPL %s: found '%s', expected '%s'\n",
str, pmu->units, expected_units);
}
return 0;
}
static void
rapl_open(struct pmu_counter *pmu,
const char *domain,
struct engines *engines)
{
int fd;
if (rapl_parse(pmu, domain) < 0)
return;
fd = igt_perf_open_group(pmu->type, pmu->config, engines->rapl_fd);
if (fd < 0)
return;
if (engines->rapl_fd == -1)
engines->rapl_fd = fd;
pmu->idx = engines->num_rapl++;
pmu->present = true;
}
static void gpu_power_open(struct pmu_counter *pmu,
struct engines *engines)
{
rapl_open(pmu, "energy-gpu", engines);
}
static void pkg_power_open(struct pmu_counter *pmu,
struct engines *engines)
{
rapl_open(pmu, "energy-pkg", engines);
}
static uint64_t
get_pmu_config(int dirfd, const char *name, const char *counter)
{
char buf[128], *p;
int fd, ret;
ret = snprintf(buf, sizeof(buf), "%s-%s", name, counter);
if (ret < 0 || ret == sizeof(buf))
return -1;
fd = openat(dirfd, buf, O_RDONLY);
if (fd < 0)
return -1;
ret = read(fd, buf, sizeof(buf));
close(fd);
if (ret <= 0)
return -1;
p = strchr(buf, '0');
if (!p)
return -1;
return strtoul(p, NULL, 0);
}
#define engine_ptr(engines, n) (&engines->engine + (n))
static const char *class_display_name(unsigned int class)
{
switch (class) {
case I915_ENGINE_CLASS_RENDER:
return "Render/3D";
case I915_ENGINE_CLASS_COPY:
return "Blitter";
case I915_ENGINE_CLASS_VIDEO:
return "Video";
case I915_ENGINE_CLASS_VIDEO_ENHANCE:
return "VideoEnhance";
case I915_ENGINE_CLASS_COMPUTE:
return "Compute";
default:
return "[unknown]";
}
}
static const char *class_short_name(unsigned int class)
{
switch (class) {
case I915_ENGINE_CLASS_RENDER:
return "RCS";
case I915_ENGINE_CLASS_COPY:
return "BCS";
case I915_ENGINE_CLASS_VIDEO:
return "VCS";
case I915_ENGINE_CLASS_VIDEO_ENHANCE:
return "VECS";
case I915_ENGINE_CLASS_COMPUTE:
return "CCS";
default:
return "UNKN";
}
}
static int engine_cmp(const void *__a, const void *__b)
{
const struct engine *a = (struct engine *)__a;
const struct engine *b = (struct engine *)__b;
if (a->class != b->class)
return a->class - b->class;
else
return a->instance - b->instance;
}
#define IGPU_PCI "0000:00:02.0"
#define is_igpu_pci(x) (strcmp(x, IGPU_PCI) == 0)
#define is_igpu(x) (strcmp(x, "i915") == 0)
static struct engines *discover_engines(char *device)
{
char sysfs_root[PATH_MAX];
struct engines *engines;
struct dirent *dent;
int ret = 0;
DIR *d;
snprintf(sysfs_root, sizeof(sysfs_root),
"/sys/devices/%s/events", device);
engines = malloc(sizeof(struct engines));
if (!engines)
return NULL;
memset(engines, 0, sizeof(*engines));
engines->num_engines = 0;
engines->device = device;
engines->discrete = !is_igpu(device);
d = opendir(sysfs_root);
if (!d)
goto err;
while ((dent = readdir(d)) != NULL) {
const char *endswith = "-busy";
const unsigned int endlen = strlen(endswith);
struct engine *engine =
engine_ptr(engines, engines->num_engines);
char buf[256];
if (dent->d_type != DT_REG)
continue;
if (strlen(dent->d_name) >= sizeof(buf)) {
ret = ENAMETOOLONG;
break;
}
strcpy(buf, dent->d_name);
/* xxxN-busy */
if (strlen(buf) < (endlen + 4))
continue;
if (strcmp(&buf[strlen(buf) - endlen], endswith))
continue;
memset(engine, 0, sizeof(*engine));
buf[strlen(buf) - endlen] = 0;
engine->name = strdup(buf);
if (!engine->name) {
ret = errno;
break;
}
engine->busy.config = get_pmu_config(dirfd(d), engine->name,
"busy");
if (engine->busy.config == -1) {
ret = ENOENT;
break;
}
/* Double check config is an engine config. */
if (engine->busy.config >= __I915_PMU_OTHER(0)) {
free((void *)engine->name);
continue;
}
engine->class = (engine->busy.config &
(__I915_PMU_OTHER(0) - 1)) >>
I915_PMU_CLASS_SHIFT;
engine->instance = (engine->busy.config >>
I915_PMU_SAMPLE_BITS) &
((1 << I915_PMU_SAMPLE_INSTANCE_BITS) - 1);
ret = asprintf(&engine->display_name, "%s/%u",
class_display_name(engine->class),
engine->instance);
if (ret <= 0) {
ret = errno;
break;
}
ret = asprintf(&engine->short_name, "%s/%u",
class_short_name(engine->class),
engine->instance);
if (ret <= 0) {
ret = errno;
break;
}
engines->num_engines++;
engines = realloc(engines, sizeof(struct engines) +
engines->num_engines * sizeof(struct engine));
if (!engines) {
ret = errno;
break;
}
ret = 0;
}
if (ret) {
errno = ret;
goto err;
}
qsort(engine_ptr(engines, 0), engines->num_engines,
sizeof(struct engine), engine_cmp);
engines->root = d;
return engines;
err:
free(engines);
return NULL;
}
static void free_engines(struct engines *engines)
{
struct pmu_counter **pmu, *free_list[] = {
&engines->r_gpu,
&engines->r_pkg,
&engines->imc_reads,
&engines->imc_writes,
NULL
};
unsigned int i;
if (!engines)
return;
for (pmu = &free_list[0]; *pmu; pmu++) {
if ((*pmu)->present)
free((char *)(*pmu)->units);
}
for (i = 0; i < engines->num_engines; i++) {
struct engine *engine = engine_ptr(engines, i);
free((char *)engine->name);
free((char *)engine->short_name);
free((char *)engine->display_name);
}
closedir(engines->root);
free(engines->class);
free(engines);
}
#define _open_pmu(type, cnt, pmu, fd) \
({ \
int fd__; \
\
fd__ = igt_perf_open_group((type), (pmu)->config, (fd)); \
if (fd__ >= 0) { \
if ((fd) == -1) \
(fd) = fd__; \
(pmu)->present = true; \
(pmu)->idx = (cnt)++; \
} \
\
fd__; \
})
static int imc_parse(struct pmu_counter *pmu, const char *str)
{
return pmu_parse(pmu, "/sys/devices/uncore_imc", str);
}
static void
imc_open(struct pmu_counter *pmu,
const char *domain,
struct engines *engines)
{
int fd;
if (imc_parse(pmu, domain) < 0)
return;
fd = igt_perf_open_group(pmu->type, pmu->config, engines->imc_fd);
if (fd < 0)
return;
if (engines->imc_fd == -1)
engines->imc_fd = fd;
pmu->idx = engines->num_imc++;
pmu->present = true;
}
static void imc_writes_open(struct pmu_counter *pmu, struct engines *engines)
{
imc_open(pmu, "data_writes", engines);
}
static void imc_reads_open(struct pmu_counter *pmu, struct engines *engines)
{
imc_open(pmu, "data_reads", engines);
}
static int get_num_gts(uint64_t type)
{
int fd, cnt;
errno = 0;
for (cnt = 0; cnt < MAX_GTS; cnt++) {
fd = igt_perf_open(type, __I915_PMU_INTERRUPTS(cnt));
if (fd < 0)
break;
close(fd);
}
if (!cnt || (errno && errno != ENOENT))
cnt = -errno;
return cnt;
}
static void init_aggregate_counters(struct engines *engines)
{
struct pmu_counter *pmu;
pmu = &engines->freq_req;
pmu->type = igt_perf_type_id(engines->device);
pmu->config = I915_PMU_REQUESTED_FREQUENCY;
pmu->present = true;
pmu = &engines->freq_act;
pmu->type = igt_perf_type_id(engines->device);
pmu->config = I915_PMU_ACTUAL_FREQUENCY;
pmu->present = true;
pmu = &engines->rc6;
pmu->type = igt_perf_type_id(engines->device);
pmu->config = I915_PMU_RC6_RESIDENCY;
pmu->present = true;
}
static int pmu_init(struct engines *engines)
{
unsigned int i;
int fd;
uint64_t type = igt_perf_type_id(engines->device);
engines->fd = -1;
engines->num_counters = 0;
engines->num_gts = get_num_gts(type);
if (engines->num_gts <= 0)
return -1;
engines->irq.config = I915_PMU_INTERRUPTS;
fd = _open_pmu(type, engines->num_counters, &engines->irq, engines->fd);
if (fd < 0)
return -1;
init_aggregate_counters(engines);
for (i = 0; i < engines->num_gts; i++) {
engines->freq_req_gt[i].config = __I915_PMU_REQUESTED_FREQUENCY(i);
_open_pmu(type, engines->num_counters, &engines->freq_req_gt[i], engines->fd);
engines->freq_act_gt[i].config = __I915_PMU_ACTUAL_FREQUENCY(i);
_open_pmu(type, engines->num_counters, &engines->freq_act_gt[i], engines->fd);
engines->rc6_gt[i].config = __I915_PMU_RC6_RESIDENCY(i);
_open_pmu(type, engines->num_counters, &engines->rc6_gt[i], engines->fd);
}
for (i = 0; i < engines->num_engines; i++) {
struct engine *engine = engine_ptr(engines, i);
struct {
struct pmu_counter *pmu;
const char *counter;
} *cnt, counters[] = {
{ .pmu = &engine->busy, .counter = "busy" },
{ .pmu = &engine->wait, .counter = "wait" },
{ .pmu = &engine->sema, .counter = "sema" },
{ .pmu = NULL, .counter = NULL },
};
for (cnt = counters; cnt->pmu; cnt++) {
if (!cnt->pmu->config)
cnt->pmu->config =
get_pmu_config(dirfd(engines->root),
engine->name,
cnt->counter);
fd = _open_pmu(type, engines->num_counters, cnt->pmu,
engines->fd);
if (fd >= 0)
engine->num_counters++;
}
}
engines->rapl_fd = -1;
if (!engines->discrete) {
gpu_power_open(&engines->r_gpu, engines);
pkg_power_open(&engines->r_pkg, engines);
}
engines->imc_fd = -1;
imc_reads_open(&engines->imc_reads, engines);
imc_writes_open(&engines->imc_writes, engines);
return 0;
}
static uint64_t pmu_read_multi(int fd, unsigned int num, uint64_t *val)
{
uint64_t buf[2 + num];
unsigned int i;
ssize_t len;
memset(buf, 0, sizeof(buf));
len = read(fd, buf, sizeof(buf));
assert(len == sizeof(buf));
for (i = 0; i < num; i++)
val[i] = buf[2 + i];
return buf[1];
}
static double pmu_calc(struct pmu_pair *p, double d, double t, double s)
{
double v;
v = p->cur - p->prev;
v /= d;
v /= t;
v *= s;
if (s == 100.0 && v > 100.0)
v = 100.0;
return v;
}
static void fill_str(char *buf, unsigned int bufsz, char c, unsigned int num)
{
unsigned int i;
for (i = 0; i < num && i < (bufsz - 1); i++)
*buf++ = c;
*buf = 0;
}
static void __update_sample(struct pmu_counter *counter, uint64_t val)
{
counter->val.prev = counter->val.cur;
counter->val.cur = val;
}
static void update_sample(struct pmu_counter *counter, uint64_t *val)
{
if (counter->present)
__update_sample(counter, val[counter->idx]);
}
static void pmu_sample(struct engines *engines)
{
const int num_val = engines->num_counters;
uint64_t val[2 + num_val];
unsigned int i;
engines->ts.prev = engines->ts.cur;
engines->ts.cur = pmu_read_multi(engines->fd, num_val, val);
engines->freq_req.val.cur = engines->freq_req.val.prev = 0;
engines->freq_act.val.cur = engines->freq_act.val.prev = 0;
engines->rc6.val.cur = engines->rc6.val.prev = 0;
for (i = 0; i < engines->num_gts; i++) {
update_sample(&engines->freq_req_gt[i], val);
engines->freq_req.val.cur += engines->freq_req_gt[i].val.cur;
engines->freq_req.val.prev += engines->freq_req_gt[i].val.prev;
update_sample(&engines->freq_act_gt[i], val);
engines->freq_act.val.cur += engines->freq_act_gt[i].val.cur;
engines->freq_act.val.prev += engines->freq_act_gt[i].val.prev;
update_sample(&engines->rc6_gt[i], val);
engines->rc6.val.cur += engines->rc6_gt[i].val.cur;
engines->rc6.val.prev += engines->rc6_gt[i].val.prev;
}
engines->freq_req.val.cur /= engines->num_gts;
engines->freq_req.val.prev /= engines->num_gts;
engines->freq_act.val.cur /= engines->num_gts;
engines->freq_act.val.prev /= engines->num_gts;
engines->rc6.val.cur /= engines->num_gts;
engines->rc6.val.prev /= engines->num_gts;
update_sample(&engines->irq, val);
for (i = 0; i < engines->num_engines; i++) {
struct engine *engine = engine_ptr(engines, i);
update_sample(&engine->busy, val);
update_sample(&engine->sema, val);
update_sample(&engine->wait, val);
}
if (engines->num_rapl) {
pmu_read_multi(engines->rapl_fd, engines->num_rapl, val);
update_sample(&engines->r_gpu, val);
update_sample(&engines->r_pkg, val);
}
if (engines->num_imc) {
pmu_read_multi(engines->imc_fd, engines->num_imc, val);
update_sample(&engines->imc_reads, val);
update_sample(&engines->imc_writes, val);
}
}
static int
__client_id_cmp(const struct igt_drm_client *a,
const struct igt_drm_client *b)
{
if (a->id > b->id)
return 1;
else if (a->id < b->id)
return -1;
else
return 0;
}
static int client_last_cmp(const void *_a, const void *_b, void *unused)
{
const struct igt_drm_client *a = _a;
const struct igt_drm_client *b = _b;
long val_a = a->agg_delta_engine_time, val_b = b->agg_delta_engine_time;
/*
* Sort clients in descending order of runtime in the previous sampling
* period. Tie-breaker is client id.
*/
if (val_a == val_b)
return __client_id_cmp(a, b);
else if (val_b > val_a)
return 1;
else
return -1;
}
static int client_total_cmp(const void *_a, const void *_b, void *unused)
{
const struct igt_drm_client *a = _a;
const struct igt_drm_client *b = _b;
long val_a = a->total_engine_time, val_b = b->total_engine_time;
if (val_a == val_b)
return __client_id_cmp(a, b);
else if (val_b > val_a)
return 1;
else
return -1;
}
static int client_id_cmp(const void *_a, const void *_b, void *unused)
{
const struct igt_drm_client *a = _a;
const struct igt_drm_client *b = _b;
return __client_id_cmp(a, b);
}
static int client_pid_cmp(const void *_a, const void *_b, void *unused)
{
const struct igt_drm_client *a = _a;
const struct igt_drm_client *b = _b;
int val_a = a->pid, val_b = b->pid;
if (val_a == val_b)
return __client_id_cmp(a, b);
else if (val_b > val_a)
return -1;
else
return 1;
}
static int (*client_cmp)(const void *, const void *, void *) = client_last_cmp;
static bool aggregate_pids = true;
static struct igt_drm_clients *display_clients(struct igt_drm_clients *clients)
{
struct igt_drm_client *ac, *c, *cp = NULL;
struct igt_drm_clients *aggregated;
int tmp, num = 0;
if (!clients)
return NULL;
if (!aggregate_pids)
goto out;
/* Sort by pid first to make it easy to aggregate while walking. */
igt_drm_clients_sort(clients, client_pid_cmp);
aggregated = calloc(1, sizeof(*clients));
assert(aggregated);
ac = calloc(clients->num_clients, sizeof(*c));
assert(ac);
aggregated->private_data = clients->private_data;
aggregated->client = ac;
igt_for_each_drm_client(clients, c, tmp) {
unsigned int i;
if (c->status == IGT_DRM_CLIENT_FREE)
break;
assert(c->status == IGT_DRM_CLIENT_ALIVE);
if (!cp || c->pid != cp->pid) {
ac = &aggregated->client[num++];
/* New pid. */
ac->clients = aggregated;
ac->status = IGT_DRM_CLIENT_ALIVE;
ac->id = -c->pid;
ac->pid = c->pid;
strcpy(ac->name, c->name);
strcpy(ac->pid_str, c->pid_str);
strcpy(ac->print_name, c->print_name);
ac->engines = c->engines;
ac->utilization = calloc(c->engines->max_engine_id + 1,
sizeof(*ac->utilization));
assert(ac->utilization);
ac->regions = c->regions;
ac->memory = calloc(c->regions->max_region_id + 1,
sizeof(ac->memory[0]));
ac->samples = 1;
}
cp = c;
if (c->samples < 2)
continue;
ac->samples = 2; /* All what matters for display. */
ac->total_engine_time += c->total_engine_time;
ac->agg_delta_engine_time += c->agg_delta_engine_time;
for (i = 0; i <= c->engines->max_engine_id; i++)
ac->utilization[i].delta_engine_time += c->utilization[i].delta_engine_time;
for (i = 0; i <= c->regions->max_region_id; i++) {
ac->memory[i].total += c->memory[i].total;
ac->memory[i].shared += c->memory[i].shared;
ac->memory[i].resident += c->memory[i].resident;
ac->memory[i].purgeable += c->memory[i].purgeable;
ac->memory[i].active += c->memory[i].active;
}
}
aggregated->num_clients = num;
aggregated->active_clients = num;
aggregated->max_pid_len = clients->max_pid_len;
aggregated->max_name_len = clients->max_name_len;
clients = aggregated;
out:
return igt_drm_clients_sort(clients, client_cmp);
}
static void free_display_clients(struct igt_drm_clients *clients)
{
struct igt_drm_client *c;
unsigned int tmp;
/*
* Don't call igt_drm_clients_free or igt_drm_client_free since
* "display" clients are not proper clients and have un-initialized
* or borrowed fields which we don't want the library to try and free.
*/
igt_for_each_drm_client(clients, c, tmp) {
free(c->utilization);
free(c->memory);
}
free(clients->client);
free(clients);
}
static int n_spaces(const int n)
{
static const char *spaces[] = {
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
};
int i, r = n;
while (r) {
if (r > ARRAY_SIZE(spaces))
i = ARRAY_SIZE(spaces) - 1;
else
i = r - 1;
fputs(spaces[i], stdout);
r -= i + 1;
}
return n;
}
static void
print_percentage_bar(double percent, double max, int max_len, bool numeric)
{
static const char *bars[] = {
" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"
};
int bar_len, i, len = max_len - 2;
const int w = 8;
if (len < 2) /* For edge lines '|' */
return;
bar_len = ceil(w * percent * len / max);
if (bar_len > w * len)
bar_len = w * len;
putchar('|');
for (i = bar_len; i >= w; i -= w)
printf("%s", bars[w]);
if (i)
printf("%s", bars[i]);
len -= (bar_len + (w - 1)) / w;
if (len >= 1)
n_spaces(len);
putchar('|');
if (numeric) {
/*
* TODO: Finer grained reverse control to better preserve
* bar under numerical percentage.
*/
printf("\033[%uD\033[7m", max_len - 1);
i = printf("%3.f%%", percent);
printf("\033[%uC\033[0m", max_len - i - 1);
}
}
#define DEFAULT_PERIOD_MS (1000)
static void
usage(const char *appname)
{
printf("intel_gpu_top - Display a top-like summary of Intel GPU usage\n"
"\n"
"Usage: %s [parameters]\n"
"\n"
"\tThe following parameters are optional:\n\n"
"\t[-h] Show this help text.\n"
"\t[-c] Output CSV formatted data.\n"
"\t[-J] Output JSON formatted data.\n"
"\t[-l] List plain text data.\n"
"\t[-o <file|->] Output to specified file or '-' for standard out.\n"
"\t[-s <ms>] Refresh period in milliseconds (default %ums).\n"
"\t[-L] List all cards.\n"
"\t[-d <device>] Device filter, please check manual page for more details.\n"
"\t[-p] Default to showing physical engines instead of classes.\n"
"\t[-m] Default to showing all memory regions.\n"
"\n",
appname, DEFAULT_PERIOD_MS);
igt_device_print_filter_types();
}
static enum {
INTERACTIVE,
TEXT,
CSV,
JSON
} output_mode;
struct cnt_item {
struct pmu_counter *pmu;
unsigned int fmt_width;
unsigned int fmt_precision;
double d;
double t;
double s;
const char *name;
const char *unit;
/* Internal fields. */
char buf[16];
};
struct cnt_group {
const char *name;
const char *display_name;
struct cnt_item *items;
};
static unsigned int json_indent_level;
static const char *json_indent[] = {
"",
"\t",
"\t\t",
"\t\t\t",
"\t\t\t\t",
"\t\t\t\t\t",
};
static unsigned int json_prev_struct_members;
static unsigned int json_struct_members;
FILE *out;
static void
json_open_struct(const char *name)
{
assert(json_indent_level < ARRAY_SIZE(json_indent));
json_prev_struct_members = json_struct_members;
json_struct_members = 0;
if (name)
fprintf(out, "%s%s\"%s\": {\n",
json_prev_struct_members ? ",\n" : "",
json_indent[json_indent_level],
name);
else
fprintf(out, "%s\n%s{\n",
json_prev_struct_members ? "," : "",
json_indent[json_indent_level]);
json_indent_level++;
}
static void
json_close_struct(void)
{
assert(json_indent_level > 0);
fprintf(out, "\n%s}", json_indent[--json_indent_level]);
if (json_indent_level == 0)
fflush(stdout);
}
static void
__json_add_member(const char *key, const char *val)
{
assert(json_indent_level < ARRAY_SIZE(json_indent));
fprintf(out, "%s%s\"%s\": \"%s\"",
json_struct_members ? ",\n" : "",
json_indent[json_indent_level], key, val);
json_struct_members++;
}
static unsigned int
json_add_member(const struct cnt_group *parent, struct cnt_item *item,
unsigned int headers)
{
assert(json_indent_level < ARRAY_SIZE(json_indent));
fprintf(out, "%s%s\"%s\": ",
json_struct_members ? ",\n" : "",
json_indent[json_indent_level], item->name);
json_struct_members++;
if (!strcmp(item->name, "unit"))
fprintf(out, "\"%s\"", item->unit);
else
fprintf(out, "%f",
pmu_calc(&item->pmu->val, item->d, item->t, item->s));
return 1;
}
static unsigned int text_level;
#define TEXT_HEADER_REPEAT 20
static unsigned int text_lines = TEXT_HEADER_REPEAT;
static bool text_header_repeat;
static void text_open_struct(const char *name)
{
text_level++;
assert(text_level > 0);
}
static void text_close_struct(void)
{
assert(text_level > 0);
if (--text_level == 0) {
text_lines++;
fputs("\n", out);
fflush(out);
}
}
static unsigned int
text_add_member(const struct cnt_group *parent, struct cnt_item *item,
unsigned int headers)
{
unsigned int fmt_tot = item->fmt_width + (item->fmt_precision ? 1 : 0);
char buf[fmt_tot + 1];
double val;
int len;
if (!item->pmu)
return 0;
else if (!item->pmu->present)
return 0;
if (headers == 1) {
unsigned int grp_tot = 0;
struct cnt_item *it;
if (item != parent->items)
return 0;
for (it = parent->items; it->pmu; it++) {
if (!it->pmu->present)
continue;
grp_tot += 1 + it->fmt_width +
(it->fmt_precision ? 1 : 0);
}
fprintf(out, "%*s ", grp_tot - 1, parent->display_name);
return 0;
} else if (headers == 2) {
fprintf(out, "%*s ", fmt_tot, item->unit ?: item->name);
return 0;
}
val = pmu_calc(&item->pmu->val, item->d, item->t, item->s);
len = snprintf(buf, sizeof(buf), "%*.*f",
fmt_tot, item->fmt_precision, val);
if (len < 0 || len == sizeof(buf))
fill_str(buf, sizeof(buf), 'X', fmt_tot);
len = fprintf(out, "%s ", buf);
return len > 0 ? len : 0;
}
static unsigned int
csv_add_member(const struct cnt_group *parent, struct cnt_item *item,
unsigned int headers)
{
int len = 0;
if (headers)
fprintf(out, "%s %s", parent->display_name, item->unit);
else
len = fprintf(out, "%f",
pmu_calc(&item->pmu->val, item->d, item->t,
item->s));
return len > 0 ? len : 0;
}
static void
term_open_struct(const char *name)
{
}
static void
term_close_struct(void)
{
}
static unsigned int
term_add_member(const struct cnt_group *parent, struct cnt_item *item,
unsigned int headers)
{
unsigned int fmt_tot = item->fmt_width + (item->fmt_precision ? 1 : 0);
double val;
int len;
if (!item->pmu)
return 0;
assert(fmt_tot <= sizeof(item->buf));
if (!item->pmu->present) {
fill_str(item->buf, sizeof(item->buf), '-', fmt_tot);
return 1;
}
val = pmu_calc(&item->pmu->val, item->d, item->t, item->s);
len = snprintf(item->buf, sizeof(item->buf),
"%*.*f",
fmt_tot, item->fmt_precision, val);
if (len < 0 || len == sizeof(item->buf))
fill_str(item->buf, sizeof(item->buf), 'X', fmt_tot);
return 1;
}
struct print_operations {
void (*open_struct)(const char *name);
void (*close_struct)(void);
unsigned int (*add_member)(const struct cnt_group *parent,
struct cnt_item *item,
unsigned int headers);
bool (*print_group)(struct cnt_group *group, unsigned int headers);
};
static const struct print_operations *pops;
static unsigned int
present_in_group(const struct cnt_group *grp)
{
unsigned int present = 0;
struct cnt_item *item;
for (item = grp->items; item->name; item++) {
if (item->pmu && item->pmu->present)
present++;
}
return present;
}
static bool
print_group(struct cnt_group *grp, unsigned int headers)
{
unsigned int consumed = 0;
struct cnt_item *item;
if (!present_in_group(grp))
return false;
pops->open_struct(grp->name);
for (item = grp->items; item->name; item++)
consumed += pops->add_member(grp, item, headers);
pops->close_struct();
return consumed;
}
static unsigned int csv_count, prev_csv_count;
static void csv_close_struct(void)
{
assert(text_level > 0);
if (--text_level == 0) {
csv_count = prev_csv_count = 0;
text_lines++;
fputs("\n", out);
fflush(out);
}
}
static bool
csv_print_group(struct cnt_group *grp, unsigned int headers)
{
unsigned int consumed = 0;
struct cnt_item *item;
if (!present_in_group(grp))
return false;
text_open_struct(grp->name);
for (item = grp->items; item->name; item++) {
if (!item->pmu || !item->pmu->present)
continue;
if (csv_count != prev_csv_count)
fprintf(out, ",");
prev_csv_count = csv_count++;
consumed += csv_add_member(grp, item, headers);
}
csv_close_struct();
return consumed;
}
static bool
term_print_group(struct cnt_group *grp, unsigned int headers)
{
unsigned int consumed = 0;
struct cnt_item *item;
pops->open_struct(grp->name);
for (item = grp->items; item->name; item++)
consumed += pops->add_member(grp, item, headers);
pops->close_struct();
return consumed;
}
static const struct print_operations json_pops = {
.open_struct = json_open_struct,
.close_struct = json_close_struct,
.add_member = json_add_member,
.print_group = print_group,
};
static const struct print_operations text_pops = {
.open_struct = text_open_struct,
.close_struct = text_close_struct,
.add_member = text_add_member,
.print_group = print_group,
};
static const struct print_operations csv_pops = {
.open_struct = text_open_struct,
.close_struct = csv_close_struct,
.add_member = csv_add_member,
.print_group = csv_print_group,
};
static const struct print_operations term_pops = {
.open_struct = term_open_struct,
.close_struct = term_close_struct,
.add_member = term_add_member,
.print_group = term_print_group,
};
static bool print_groups(struct cnt_group **groups)
{
static bool headers_printed;
bool print_data = true;
if ((output_mode == TEXT || output_mode == CSV) &&
(text_header_repeat || !headers_printed)) {
const unsigned int header_lines = output_mode == TEXT ? 2 : 1;
unsigned int headers = text_lines % TEXT_HEADER_REPEAT + 1;
if (headers > 0 && headers <= header_lines)
for (struct cnt_group **grp = groups; *grp; grp++)
print_data = pops->print_group(*grp, headers);
headers_printed = print_data;
}
for (struct cnt_group **grp = groups; print_data && *grp; grp++)
pops->print_group(*grp, 0);
return print_data;
}
static int __attribute__ ((format(__printf__, 6, 7)))
print_header_token(const char *cont, int lines, int con_w, int con_h, int *rem,
const char *fmt, ...)
{
const char *indent = "\n ";
char buf[256];
va_list args;
int ret;
if (lines >= con_h)
return lines;
va_start(args, fmt);
ret = vsnprintf(buf, sizeof(buf), fmt, args);
assert(ret < sizeof(buf));
va_end(args);
ret = (cont ? strlen(cont) : 0) + strlen(buf);
*rem -= ret;
if (*rem < 0) {
if (++lines >= con_h)
return lines;
*rem = con_w - ret - strlen(indent);
cont = indent;
}
if (cont)
ret = printf("%s%s", cont, buf);
else
ret = printf("%s", buf);
return lines;
}
static const char *header_msg;
static int
print_header(const struct igt_device_card *card,
const char *codename,
struct engines *engines, double t,
int lines, int con_w, int con_h, bool *consumed)
{
struct pmu_counter fake_pmu = {
.present = true,
.val.cur = 1,
};
struct cnt_item period_items[] = {
{ &fake_pmu, 0, 0, 1.0, 1.0, t * 1e3, "duration" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "ms" },
{ },
};
struct cnt_group period_group = {
.name = "period",
.items = period_items,
};
struct cnt_item freq_items[] = {
{ &engines->freq_req, 4, 0, 1.0, t, 1, "requested", "req" },
{ &engines->freq_act, 4, 0, 1.0, t, 1, "actual", "act" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "MHz" },
{ },
};
struct cnt_group freq_group = {
.name = "frequency",
.display_name = "Freq MHz",
.items = freq_items,
};
struct cnt_item freq_items_gt[] = {
{ &engines->freq_req_gt[0], 6, 0, 1.0, t, 1, "requested", "req" },
{ &engines->freq_act_gt[0], 6, 0, 1.0, t, 1, "actual", "act" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "MHz" },
{ },
{ &engines->freq_req_gt[1], 6, 0, 1.0, t, 1, "requested", "req" },
{ &engines->freq_act_gt[1], 6, 0, 1.0, t, 1, "actual", "act" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "MHz" },
{ },
{ &engines->freq_req_gt[2], 6, 0, 1.0, t, 1, "requested", "req" },
{ &engines->freq_act_gt[2], 6, 0, 1.0, t, 1, "actual", "act" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "MHz" },
{ },
{ &engines->freq_req_gt[3], 6, 0, 1.0, t, 1, "requested", "req" },
{ &engines->freq_act_gt[3], 6, 0, 1.0, t, 1, "actual", "act" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "MHz" },
{ },
};
struct cnt_group freq_group_gt[MAX_GTS] = {
{ .name = "frequency-gt0", .display_name = "Freq GT0 MHz", .items = &freq_items_gt[0] },
{ .name = "frequency-gt1", .display_name = "Freq GT1 MHz", .items = &freq_items_gt[4] },
{ .name = "frequency-gt2", .display_name = "Freq GT2 MHz", .items = &freq_items_gt[8] },
{ .name = "frequency-gt3", .display_name = "Freq GT3 MHz", .items = &freq_items_gt[12] },
};
struct cnt_item irq_items[] = {
{ &engines->irq, 8, 0, 1.0, t, 1, "count", "/s" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "irq/s" },
{ },
};
struct cnt_group irq_group = {
.name = "interrupts",
.display_name = "IRQ",
.items = irq_items,
};
struct cnt_item rc6_items[] = {
{ &engines->rc6, 3, 0, 1e9, t, 100, "value", "%" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "%" },
{ },
};
struct cnt_group rc6_group = {
.name = "rc6",
.display_name = "RC6",
.items = rc6_items,
};
struct cnt_item rc6_items_gt[] = {
{ &engines->rc6_gt[0], 8, 0, 1e9, t, 100, "value", "%" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "%" },
{ },
{ &engines->rc6_gt[1], 8, 0, 1e9, t, 100, "value", "%" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "%" },
{ },
{ &engines->rc6_gt[2], 8, 0, 1e9, t, 100, "value", "%" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "%" },
{ },
{ &engines->rc6_gt[3], 8, 0, 1e9, t, 100, "value", "%" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "%" },
{ },
};
struct cnt_group rc6_group_gt[MAX_GTS] = {
{ .name = "rc6-gt0", .display_name = "RC6 GT0", .items = &rc6_items_gt[0] },
{ .name = "rc6-gt1", .display_name = "RC6 GT1", .items = &rc6_items_gt[3] },
{ .name = "rc6-gt2", .display_name = "RC6 GT2", .items = &rc6_items_gt[6] },
{ .name = "rc6-gt3", .display_name = "RC6 GT3", .items = &rc6_items_gt[9] },
};
struct cnt_item power_items[] = {
{ &engines->r_gpu, 4, 2, 1.0, t, engines->r_gpu.scale, "GPU", "gpu" },
{ &engines->r_pkg, 4, 2, 1.0, t, engines->r_pkg.scale, "Package", "pkg" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "W" },
{ },
};
struct cnt_group power_group = {
.name = "power",
.display_name = "Power W",
.items = power_items,
};
/*
* Array size calculation:
* One group each for period, irq, power, NULL = 4
* One group per gt for freq = MAX_GTS
* One group per gt for rc6 = MAX_GTS
*/
struct cnt_group *groups[4 + MAX_GTS + MAX_GTS] = {
&period_group,
&freq_group,
&irq_group,
&rc6_group,
&power_group,
NULL
};
int rem, i;
/*
* If we have multi-gt and the user has specified -p options, show gt
* specific values.
*/
if (!class_view && engines->num_gts > 1) {
int j = 0;
groups[j++] = &period_group;
for (i = 0; i < engines->num_gts; i++)
groups[j++] = &freq_group_gt[i];
groups[j++] = &irq_group;
for (i = 0; i < engines->num_gts; i++)
groups[j++] = &rc6_group_gt[i];
groups[j++] = &power_group;
groups[j++] = NULL;
}
if (output_mode != JSON)
memmove(&groups[0], &groups[1],
sizeof(groups) - sizeof(groups[0]));
*consumed = print_groups(groups);
if (output_mode != INTERACTIVE)
return lines;
/* INTERACTIVE MODE */
rem = con_w;
printf("\033[H\033[J");
lines = print_header_token(NULL, lines, con_w, con_h, &rem,
"intel-gpu-top:");
lines = print_header_token(" ", lines, con_w, con_h, &rem,
"%s", codename);
lines = print_header_token(" @ ", lines, con_w, con_h, &rem,
"%s", card->card);
if (class_view || engines->num_gts == 1) {
lines = print_header_token(" - ", lines, con_w, con_h, &rem,
"%s/%s MHz",
freq_items[1].buf,
freq_items[0].buf);
lines = print_header_token("; ", lines, con_w, con_h, &rem,
"%s%% RC6",
rc6_items[0].buf);
} else {
for (i = 0; i < engines->num_gts; i++) {
const char *cont = !i ? " - ": "; ";
lines = print_header_token(cont, lines, con_w, con_h, &rem,
"%s/%s MHz GT%d",
freq_items_gt[i * 4 + 1].buf,
freq_items_gt[i * 4 + 0].buf,
i);
lines = print_header_token("; ", lines, con_w, con_h, &rem,
"%s%% RC6 GT%d",
rc6_items_gt[i * 3].buf,
i);
}
}
if (engines->r_gpu.present) {
lines = print_header_token("; ", lines, con_w, con_h,
&rem,
"%s/%s W",
power_items[0].buf,
power_items[1].buf);
}
lines = print_header_token("; ", lines, con_w, con_h, &rem,
"%s irqs/s",
irq_items[0].buf);
if (lines++ < con_h)
printf("\n");
if (lines++ < con_h) {
if (header_msg) {
printf(" >>> %s\n", header_msg);
header_msg = NULL;
} else {
printf("\n");
}
}
return lines;
}
static int
print_imc(struct engines *engines, double t, int lines, int con_w, int con_h)
{
struct cnt_item imc_items[] = {
{ &engines->imc_reads, 6, 0, 1.0, t, engines->imc_reads.scale,
"reads", "rd" },
{ &engines->imc_writes, 6, 0, 1.0, t, engines->imc_writes.scale,
"writes", "wr" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit" },
{ },
};
struct cnt_group imc_group = {
.name = "imc-bandwidth",
.items = imc_items,
};
struct cnt_group *groups[] = {
&imc_group,
NULL
};
int ret;
if (!engines->num_imc)
return lines;
ret = asprintf((char **)&imc_group.display_name, "IMC %s/s",
engines->imc_reads.units);
assert(ret >= 0);
ret = asprintf((char **)&imc_items[2].unit, "%s/s",
engines->imc_reads.units);
assert(ret >= 0);
print_groups(groups);
free((void *)imc_group.display_name);
free((void *)imc_items[2].unit);
if (output_mode == INTERACTIVE) {
if (lines++ < con_h)
printf(" IMC reads: %s %s/s\n",
imc_items[0].buf, engines->imc_reads.units);
if (lines++ < con_h)
printf(" IMC writes: %s %s/s\n",
imc_items[1].buf, engines->imc_writes.units);
if (lines++ < con_h)
printf("\n");
}
return lines;
}
static int
print_engines_header(struct engines *engines, double t,
int lines, int con_w, int con_h)
{
for (unsigned int i = 0;
i < engines->num_engines && lines < con_h;
i++) {
struct engine *engine = engine_ptr(engines, i);
if (!engine->num_counters)
continue;
pops->open_struct("engines");
if (output_mode == INTERACTIVE) {
const char *b = " MI_SEMA MI_WAIT";
const char *a;
if (class_view)
a = " ENGINES BUSY ";
else
a = " ENGINE BUSY ";
printf("\033[7m%s%*s%s\033[0m\n",
a, (int)(con_w - strlen(a) - strlen(b)), " ", b);
lines++;
}
break;
}
return lines;
}
static int
print_engine(struct engines *engines, unsigned int i, double t,
int lines, int con_w, int con_h)
{
struct engine *engine = engine_ptr(engines, i);
struct cnt_item engine_items[] = {
{ &engine->busy, 6, 2, 1e9, t, 100, "busy", "%" },
{ &engine->sema, 3, 0, 1e9, t, 100, "sema", "se" },
{ &engine->wait, 3, 0, 1e9, t, 100, "wait", "wa" },
{ NULL, 0, 0, 0.0, 0.0, 0.0, "unit", "%" },
{ },
};
struct cnt_group engine_group = {
.name = engine->display_name,
.display_name = engine->short_name,
.items = engine_items,
};
struct cnt_group *groups[] = {
&engine_group,
NULL
};
if (!engine->num_counters)
return lines;
print_groups(groups);
if (output_mode == INTERACTIVE) {
unsigned int len;
char buf[128];
double val;
len = snprintf(buf, sizeof(buf), " %s%% %s%%",
engine_items[1].buf, engine_items[2].buf);
len += printf("%16s %s%% ",
engine->display_name, engine_items[0].buf);
val = pmu_calc(&engine->busy.val, 1e9, t, 100);
print_percentage_bar(val, 100.0, con_w > len ? con_w - len : 0,
false);
printf("%s\n", buf);
lines++;
}
return lines;
}
static int
print_engines_footer(struct engines *engines, double t,
int lines, int con_w, int con_h)
{
pops->close_struct();
if (output_mode == INTERACTIVE) {
if (lines++ < con_h)
printf("\n");
}
return lines;
}
static int class_cmp(const void *_a, const void *_b)
{
const struct engine_class *a = _a;
const struct engine_class *b = _b;
return a->engine_class - b->engine_class;
}
static void init_engine_classes(struct engines *engines)
{
struct engine_class *classes;
unsigned int i, num;
int max = -1;
if (engines->num_classes)
return;
for (i = 0; i < engines->num_engines; i++) {
struct engine *engine = engine_ptr(engines, i);
if ((int)engine->class > max)
max = engine->class;
}
assert(max >= 0);
num = max + 1;
classes = calloc(num, sizeof(*classes));
assert(classes);
for (i = 0; i < engines->num_engines; i++) {
struct engine *engine = engine_ptr(engines, i);
classes[engine->class].num_engines++;
}
for (i = 0; i < num; i++) {
classes[i].engine_class = i;
classes[i].name = class_display_name(i);
}
qsort(classes, num, sizeof(*classes), class_cmp);
engines->num_classes = num;
engines->class = classes;
}
static void __pmu_sum(struct pmu_pair *dst, struct pmu_pair *src)
{
dst->prev += src->prev;
dst->cur += src->cur;
}
static void __pmu_normalize(struct pmu_pair *val, unsigned int n)
{
val->prev /= n;
val->cur /= n;
}
static struct engines *init_class_engines(struct engines *engines)
{
unsigned int num_present;
struct engines *classes;
unsigned int i, j, k;
init_engine_classes(engines);
num_present = 0; /* Classes with engines. */
for (i = 0; i < engines->num_classes; i++) {
if (engines->class[i].num_engines)
num_present++;
}
classes = calloc(1, sizeof(struct engines) +
num_present * sizeof(struct engine));
assert(classes);
classes->num_engines = num_present;
classes->num_classes = engines->num_classes;
classes->class = engines->class;
j = 0;
for (i = 0; i < engines->num_classes; i++) {
struct engine *engine = engine_ptr(classes, j);
/* Skip classes with no engines. */
if (!engines->class[i].num_engines)
continue;
assert(j < num_present);
engine->class = i;
engine->instance = -1;
engine->display_name = strdup(class_display_name(i));
assert(engine->display_name);
engine->short_name = strdup(class_short_name(i));
assert(engine->short_name);
/*
* Copy over pmu metadata from one real engine of the same
* class.
*/
for (k = 0; k < engines->num_engines; k++) {
struct engine *e = engine_ptr(engines, k);
if (e->class == i) {
engine->num_counters = e->num_counters;
engine->busy = e->busy;
engine->sema = e->sema;
engine->wait = e->wait;
break;
}
}
j++; /* Next "class engine" to populate. */
}
return classes;
}
static struct engines *update_class_engines(struct engines *engines)
{
static struct engines *classes;
unsigned int i, j;
if (!classes)
classes = init_class_engines(engines);
for (i = 0; i < classes->num_engines; i++) {
struct engine *engine = engine_ptr(classes, i);
unsigned int num_engines =
classes->class[engine->class].num_engines;
assert(num_engines);
memset(&engine->busy.val, 0, sizeof(engine->busy.val));
memset(&engine->sema.val, 0, sizeof(engine->sema.val));
memset(&engine->wait.val, 0, sizeof(engine->wait.val));
for (j = 0; j < engines->num_engines; j++) {
struct engine *e = engine_ptr(engines, j);
if (e->class == engine->class) {
__pmu_sum(&engine->busy.val, &e->busy.val);
__pmu_sum(&engine->sema.val, &e->sema.val);
__pmu_sum(&engine->wait.val, &e->wait.val);
}
}
__pmu_normalize(&engine->busy.val, num_engines);
__pmu_normalize(&engine->sema.val, num_engines);
__pmu_normalize(&engine->wait.val, num_engines);
}
return classes;
}
static int
print_engines(struct engines *engines, double t, int lines, int w, int h)
{
struct engines *show;
if (class_view)
show = update_class_engines(engines);
else
show = engines;
lines = print_engines_header(show, t, lines, w, h);
for (unsigned int i = 0; i < show->num_engines && lines < h; i++)
lines = print_engine(show, i, t, lines, w, h);
lines = print_engines_footer(show, t, lines, w, h);
return lines;
}
static int
print_clients_header(struct igt_drm_clients *clients, int lines,
int con_w, int con_h, int *class_w)
{
struct intel_clients *iclients = clients->private_data;
const int max_name_len = clients->max_name_len < 4 ?
4 : clients->max_name_len; /* At least "NAME" */
if (output_mode == INTERACTIVE) {
int len, num_active = 0;
unsigned int i;
if (lines++ >= con_h)
return lines;
printf("\033[7m");
len = printf("%*s ", clients->max_pid_len, "PID");
if (lines++ >= con_h || len >= con_w)
return lines;
if (iclients->regions) {
if (aggregate_regions) {
len += printf(" MEM RSS ");
} else {
len += printf(" RAM RSS ");
if (iclients->regions->num_regions > 1)
len += printf(" VRAM VRSS ");
}
}
if (iclients->classes.num_engines) {
int width;
for (i = 0; i <= iclients->classes.max_engine_id; i++) {
if (iclients->classes.capacity[i])
num_active++;
}
*class_w = width = (con_w - len - max_name_len - 1) /
num_active;
for (i = 0; i <= iclients->classes.max_engine_id; i++) {
const char *name = iclients->classes.names[i];
int name_len = strlen(name);
int pad = (width - name_len) / 2;
int spaces = width - pad - name_len;
if (!iclients->classes.capacity[i])
continue;
if (pad < 0 || spaces < 0)
continue;
n_spaces(pad);
printf("%s", name);
n_spaces(spaces);
len += pad + name_len + spaces;
}
}
printf(" %-*s\033[0m\n", con_w - len - 1, "NAME");
} else {
if (iclients->classes.num_engines)
pops->open_struct("clients");
}
return lines;
}
static bool numeric_clients;
static bool filter_idle;
static int print_size(uint64_t sz)
{
char units[] = { ' ', 'K', 'M', 'G' };
unsigned int u;
for (u = 0; u < ARRAY_SIZE(units) - 1; u++) {
if (sz & 1023 || sz < 1024)
break;
sz /= 1024;
}
return printf("%7"PRIu64"%c ", sz, units[u]);
}
static int
print_client(struct igt_drm_client *c, struct engines *engines, double t, int lines,
int con_w, int con_h, unsigned int period_us, int *class_w)
{
struct igt_drm_clients *clients = c->clients;
struct intel_clients *iclients = clients->private_data;
unsigned int i;
int len;
if (output_mode == INTERACTIVE) {
if (filter_idle && (!c->total_engine_time || c->samples < 2))
return lines;
lines++;
len = printf("%*s ", clients->max_pid_len, c->pid_str);
if (iclients->regions) {
if (aggregate_regions) {
uint64_t sz;
for (sz = 0, i = 0;
i <= c->regions->max_region_id; i++)
sz += c->memory[i].total;
len += print_size(sz);
for (sz = 0, i = 0;
i <= c->regions->max_region_id; i++)
sz += c->memory[i].resident;
len += print_size(sz);
} else {
len += print_size(c->memory[0].total);
len += print_size(c->memory[0].resident);
if (c->regions->num_regions > 1) {
len += print_size(c->memory[1].total);
len += print_size(c->memory[1].resident);
}
}
}
for (i = 0; i <= iclients->classes.max_engine_id; i++) {
double pct, max;
if (!iclients->classes.capacity[i])
continue;
if (c->samples < 2) {
len += n_spaces(*class_w);
continue;
}
pct = (double)c->utilization[i].delta_engine_time / period_us / 1e3 * 100;
/*
* Guard against possible time-drift between sampling
* client data and time we obtained our time-delta from
* PMU.
*/
max = 100.0 * iclients->classes.capacity[i];
if (pct > max)
pct = max;
print_percentage_bar(pct, max, *class_w,
numeric_clients);
len += *class_w;
}
printf(" %-*s\n", con_w - len - 1, c->print_name);
} else if (output_mode == JSON) {
char buf[64];
snprintf(buf, sizeof(buf), "%lu", c->id);
pops->open_struct(buf);
__json_add_member("name", c->print_name);
snprintf(buf, sizeof(buf), "%u", c->pid);
__json_add_member("pid", buf);
if (iclients->regions) {
pops->open_struct("memory");
for (i = 0; i < ARRAY_SIZE(json_memory_region_names);
i++) {
if (i > c->regions->max_region_id)
break;
pops->open_struct(json_memory_region_names[i]);
snprintf(buf, sizeof(buf), "%" PRIu64,
c->memory[i].total);
__json_add_member("total", buf);
snprintf(buf, sizeof(buf), "%" PRIu64,
c->memory[i].shared);
__json_add_member("shared", buf);
snprintf(buf, sizeof(buf), "%" PRIu64,
c->memory[i].resident);
__json_add_member("resident", buf);
snprintf(buf, sizeof(buf), "%" PRIu64,
c->memory[i].purgeable);
__json_add_member("purgeable", buf);
snprintf(buf, sizeof(buf), "%" PRIu64,
c->memory[i].active);
__json_add_member("active", buf);
pops->close_struct();
}
pops->close_struct();
}
if (c->samples > 1) {
pops->open_struct("engine-classes");
for (i = 0; i <= iclients->classes.max_engine_id; i++) {
double pct;
snprintf(buf, sizeof(buf), "%s",
iclients->classes.names[i]);
pops->open_struct(buf);
pct = (double)c->utilization[i].delta_engine_time / period_us / 1e3 * 100;
snprintf(buf, sizeof(buf), "%f", pct);
__json_add_member("busy", buf);
__json_add_member("unit", "%");
pops->close_struct();
}
pops->close_struct();
}
pops->close_struct();
}
return lines;
}
static int
print_clients_footer(struct igt_drm_clients *clients, double t,
int lines, int con_w, int con_h)
{
if (output_mode == INTERACTIVE) {
if (lines++ < con_h)
printf("\n");
} else {
struct intel_clients *iclients = clients->private_data;
if (iclients->classes.num_engines)
pops->close_struct();
}
return lines;
}
static void restore_term(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &termios_orig);
printf("\n");
}
static bool stop_top;
static void sigint_handler(int sig)
{
stop_top = true;
}
/* tr_pmu_name()
*
* Transliterate pci_slot_id to sysfs device name entry for discrete GPU.
* Discrete GPU PCI ID ("xxxx:yy:zz.z") device = "i915_xxxx_yy_zz.z".
*/
static char *tr_pmu_name(struct igt_device_card *card)
{
int ret;
const int bufsize = 18;
char *buf, *device = NULL;
assert(card->pci_slot_name[0]);
device = malloc(bufsize);
assert(device);
ret = snprintf(device, bufsize, "i915_%s", card->pci_slot_name);
assert(ret == (bufsize-1));
buf = device;
for (; *buf; buf++)
if (*buf == ':')
*buf = '_';
return device;
}
static void interactive_stdin(void)
{
struct termios termios = { };
int ret;
ret = tcgetattr(0, &termios);
assert(ret == 0);
memcpy(&termios_orig, &termios, sizeof(struct termios));
atexit(restore_term);
ret = fcntl(0, F_GETFL, NULL);
ret |= O_NONBLOCK;
ret = fcntl(0, F_SETFL, ret);
assert(ret == 0);
termios.c_lflag &= ~ICANON;
termios.c_cc[VMIN] = 1;
termios.c_cc[VTIME] = 0; /* Deciseconds only - we'll use poll. */
ret = tcsetattr(0, TCSAFLUSH, &termios);
assert(ret == 0);
}
static void select_client_sort(void)
{
struct {
int (*cmp)(const void *, const void *, void *);
const char *msg;
} cmp[] = {
{ client_last_cmp, "Sorting clients by current GPU usage." },
{ client_total_cmp, "Sorting clients by accummulated GPU usage." },
{ client_pid_cmp, "Sorting clients by pid." },
{ client_id_cmp, "Sorting clients by DRM id." },
};
static unsigned int client_sort;
bump:
if (++client_sort >= ARRAY_SIZE(cmp))
client_sort = 0;
client_cmp = cmp[client_sort].cmp;
header_msg = cmp[client_sort].msg;
/* Sort by client id makes no sense with pid aggregation. */
if (aggregate_pids && client_cmp == client_id_cmp)
goto bump;
}
static bool in_help;
static void process_help_stdin(void)
{
for (;;) {
int ret;
char c;
ret = read(0, &c, 1);
if (ret <= 0)
break;
switch (c) {
case 'q':
case 'h':
in_help = false;
break;
};
}
}
static void process_normal_stdin(void)
{
for (;;) {
int ret;
char c;
ret = read(0, &c, 1);
if (ret <= 0)
break;
switch (c) {
case 'q':
stop_top = true;
break;
case '1':
class_view ^= true;
if (class_view)
header_msg = "Aggregating engine classes.";
else
header_msg = "Showing physical engines.";
break;
case 'i':
filter_idle ^= true;
if (filter_idle)
header_msg = "Hiding inactive clients.";
else
header_msg = "Showing inactive clients.";
break;
case 'n':
numeric_clients ^= true;
break;
case 's':
select_client_sort();
break;
case 'h':
in_help = true;
break;
case 'H':
aggregate_pids ^= true;
if (aggregate_pids)
header_msg = "Aggregating clients.";
else
header_msg = "Showing individual clients.";
break;
case 'm':
aggregate_regions ^= true;
if (aggregate_regions)
header_msg = "Aggregating memory regions.";
else
header_msg = "Showing memory regions.";
break;
};
}
}
static void process_stdin(unsigned int timeout_us)
{
struct pollfd p = { .fd = 0, .events = POLLIN };
int ret;
ret = poll(&p, 1, timeout_us / 1000);
if (ret <= 0) {
if (ret < 0)
stop_top = true;
return;
}
if (in_help)
process_help_stdin();
else
process_normal_stdin();
}
static bool has_drm_fdinfo(const struct igt_device_card *card)
{
struct drm_client_fdinfo info = { };
unsigned int cnt;
int fd;
fd = open(card->render, O_RDWR);
if (fd < 0)
return false;
cnt = igt_parse_drm_fdinfo(fd, &info, NULL, 0, NULL, 0);
close(fd);
return cnt > 0;
}
static void show_help_screen(void)
{
printf(
"Help for interactive commands:\n\n"
" '1' Toggle between aggregated engine class and physical engine mode.\n"
" 'n' Toggle display of numeric client busyness overlay.\n"
" 's' Toggle between sort modes (runtime, total runtime, pid, client id).\n"
" 'i' Toggle display of clients which used no GPU time.\n"
" 'H' Toggle between per PID aggregation and individual clients.\n"
" 'm' Toggle between aggregated memory regions and full breakdown.\n"
"\n"
" 'h' or 'q' Exit interactive help.\n"
"\n");
}
static int gettime(struct timespec *ts)
{
memset(ts, 0, sizeof(*ts));
#ifdef CLOCK_MONOTONIC_RAW
if (!clock_gettime(CLOCK_MONOTONIC_RAW, ts))
return 0;
#endif
return clock_gettime(CLOCK_MONOTONIC, ts);
}
static unsigned long elapsed_us(struct timespec *prev, unsigned int period_us)
{
unsigned long elapsed;
struct timespec now;
if (gettime(&now))
return period_us;
elapsed = ((now.tv_nsec - prev->tv_nsec) / 1000 +
(unsigned long)USEC_PER_SEC * (now.tv_sec - prev->tv_sec));
*prev = now;
return elapsed;
}
static bool client_match(const struct igt_drm_clients *clients,
const struct drm_client_fdinfo *info)
{
struct intel_clients *iclients = clients->private_data;
if (strcmp(info->driver, "i915"))
return false;
if (strcmp(info->pdev, iclients->pci_slot))
return false;
return true;
}
static void
intel_init_clients(struct intel_clients *iclients,
const struct igt_device_card *card, struct engines *engines)
{
unsigned int i;
iclients->pci_slot = strdup(card->pci_slot_name[0] ?
card->pci_slot_name : IGPU_PCI);
assert(iclients->pci_slot);
iclients->classes.num_engines = engines->num_classes;
iclients->classes.max_engine_id = engines->num_classes - 1;
iclients->classes.capacity = calloc(engines->num_classes,
sizeof(*iclients->classes.capacity));
assert(iclients->classes.capacity);
iclients->classes.names = calloc(engines->num_classes,
sizeof(*iclients->classes.names));
assert(iclients->classes.names);
for (i = 0; i < engines->num_classes; i++) {
if (!engines->class[i].num_engines)
continue;
iclients->classes.num_engines++;
iclients->classes.max_engine_id = i;
iclients->classes.capacity[i] = engines->class[i].num_engines;
iclients->classes.names[i] = strdup(engines->class[i].name);
}
iclients->clients = igt_drm_clients_init(iclients);
}
static void intel_free_clients(struct intel_clients *iclients)
{
if (iclients->clients)
igt_drm_clients_free(iclients->clients);
free((void *)iclients->pci_slot);
free(iclients->classes.capacity);
free(iclients->classes.names);
}
static void intel_scan_clients(struct intel_clients *iclients)
{
static const char *engine_map[] = {
"render",
"copy",
"video",
"video-enhance",
"compute",
};
struct igt_drm_client *c;
unsigned int i;
igt_drm_clients_scan(iclients->clients, client_match,
engine_map, ARRAY_SIZE(engine_map),
memory_region_map, ARRAY_SIZE(memory_region_map));
iclients->regions = NULL;
if (!iclients->clients)
return;
/*
* Borrow memory region data from first client so we can use it
* when displaying stuff. All regions are the same due our client_match.
*/
igt_for_each_drm_client(iclients->clients, c, i) {
if (c->status == IGT_DRM_CLIENT_ALIVE) {
if (c->regions->num_regions)
iclients->regions = c->regions;
break;
}
}
}
int main(int argc, char **argv)
{
unsigned int period_us = DEFAULT_PERIOD_MS * 1000;
bool physical_engines = false;
bool separate_regions = false;
struct intel_clients iclients;
int con_w = -1, con_h = -1;
char *output_path = NULL;
struct engines *engines;
int ret = 0, ch;
bool list_device = false;
char *pmu_device, *opt_device = NULL;
struct igt_device_card card;
char *codename = NULL;
struct timespec ts;
/* Parse options */
while ((ch = getopt(argc, argv, "o:s:d:mpcJLlh")) != -1) {
switch (ch) {
case 'o':
output_path = optarg;
break;
case 's':
period_us = atoi(optarg) * 1000;
break;
case 'd':
opt_device = strdup(optarg);
break;
case 'p':
physical_engines = true;
break;
case 'm':
separate_regions = true;
break;
case 'c':
output_mode = CSV;
break;
case 'J':
output_mode = JSON;
break;
case 'L':
list_device = true;
break;
case 'l':
output_mode = TEXT;
break;
case 'h':
usage(argv[0]);
exit(0);
default:
fprintf(stderr, "Invalid option %c!\n", (char)optopt);
usage(argv[0]);
exit(1);
}
}
if (output_mode == INTERACTIVE && (output_path || isatty(1) != 1))
output_mode = TEXT;
if (output_path && strcmp(output_path, "-")) {
out = fopen(output_path, "w");
if (!out) {
fprintf(stderr, "Failed to open output file - '%s'!\n",
strerror(errno));
exit(1);
}
} else {
out = stdout;
}
text_header_repeat = output_mode == TEXT && isatty(fileno(out));
if (signal(SIGINT, sigint_handler) == SIG_ERR)
fprintf(stderr, "Failed to install signal handler!\n");
class_view = !physical_engines;
aggregate_regions = !separate_regions;
switch (output_mode) {
case INTERACTIVE:
pops = &term_pops;
interactive_stdin();
break;
case TEXT:
pops = &text_pops;
break;
case CSV:
pops = &csv_pops;
break;
case JSON:
pops = &json_pops;
break;
default:
assert(0);
break;
};
igt_devices_scan();
if (list_device) {
struct igt_devices_print_format fmt = {
.type = IGT_PRINT_USER,
.option = IGT_PRINT_PCI,
};
igt_devices_print(&fmt);
goto exit;
}
if (opt_device != NULL) {
ret = igt_device_card_match_pci(opt_device, &card);
if (!ret)
fprintf(stderr, "Requested device %s not found!\n", opt_device);
free(opt_device);
} else {
ret = igt_device_find_first_i915_discrete_card(&card);
if (!ret)
ret = igt_device_find_integrated_card(&card);
if (!ret)
fprintf(stderr, "No device filter specified and no discrete/integrated i915 devices found\n");
}
if (!ret) {
ret = EXIT_FAILURE;
goto exit;
}
if (card.pci_slot_name[0] && !is_igpu_pci(card.pci_slot_name))
pmu_device = tr_pmu_name(&card);
else
pmu_device = strdup("i915");
codename = igt_device_get_pretty_name(&card, false);
engines = discover_engines(pmu_device);
if (!engines) {
fprintf(stderr,
"Failed to detect engines! (%s)\n(Kernel 4.16 or newer is required for i915 PMU support.)\n",
strerror(errno));
ret = EXIT_FAILURE;
goto err_engines;
}
ret = pmu_init(engines);
if (ret) {
fprintf(stderr,
"Failed to initialize PMU! (%s)\n", strerror(errno));
if (errno == EACCES && geteuid())
fprintf(stderr,
"\n"
"When running as a normal user CAP_PERFMON is required to access performance\n"
"monitoring. See \"man 7 capabilities\", \"man 8 setcap\", or contact your\n"
"distribution vendor for assistance.\n"
"\n"
"More information can be found at 'Perf events and tool security' document:\n"
"https://www.kernel.org/doc/html/latest/admin-guide/perf-security.html\n");
ret = EXIT_FAILURE;
goto err_pmu;
}
ret = EXIT_SUCCESS;
init_engine_classes(engines);
if (has_drm_fdinfo(&card))
intel_init_clients(&iclients, &card, engines);
pmu_sample(engines);
intel_scan_clients(&iclients);
gettime(&ts);
if (output_mode == JSON)
printf("[\n");
while (!stop_top) {
struct igt_drm_clients *disp_clients;
struct igt_drm_client *c;
bool consumed = false;
unsigned int scan_us;
int j, lines = 0;
struct winsize ws;
double t;
/* Update terminal size. */
if (output_mode != INTERACTIVE) {
con_w = con_h = INT_MAX;
} else if (ioctl(0, TIOCGWINSZ, &ws) != -1) {
con_w = ws.ws_col;
con_h = ws.ws_row;
if (con_w == 0 && con_h == 0) {
/* Serial console. */
con_w = 80;
con_h = 24;
}
}
pmu_sample(engines);
t = (double)(engines->ts.cur - engines->ts.prev) / 1e9;
intel_scan_clients(&iclients);
disp_clients = display_clients(iclients.clients);
scan_us = elapsed_us(&ts, period_us);
if (stop_top)
break;
while (!consumed) {
pops->open_struct(NULL);
lines = print_header(&card, codename, engines,
t, lines, con_w, con_h,
&consumed);
if (in_help) {
show_help_screen();
break;
}
lines = print_imc(engines, t, lines, con_w, con_h);
lines = print_engines(engines, t, lines, con_w, con_h);
if (disp_clients) {
int class_w;
lines = print_clients_header(disp_clients, lines,
con_w, con_h,
&class_w);
igt_for_each_drm_client(disp_clients, c, j) {
assert(c->status != IGT_DRM_CLIENT_PROBE);
if (c->status != IGT_DRM_CLIENT_ALIVE)
break; /* Active clients are first in the array. */
if (lines >= con_h)
break;
lines = print_client(c, engines, t,
lines, con_w,
con_h, scan_us,
&class_w);
}
lines = print_clients_footer(disp_clients, t,
lines, con_w,
con_h);
}
pops->close_struct();
}
if (disp_clients != iclients.clients)
free_display_clients(disp_clients);
if (stop_top)
break;
if (output_mode == INTERACTIVE)
process_stdin(period_us);
else
usleep(period_us);
}
if (output_mode == JSON)
printf("]\n");
intel_free_clients(&iclients);
free(codename);
err_pmu:
free_engines(engines);
err_engines:
free(pmu_device);
exit:
igt_devices_free();
return ret;
}
|