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
|
/*
* block queue tracing parse application
*
* Copyright (C) 2005 Jens Axboe <axboe@suse.de>
* Copyright (C) 2006 Jens Axboe <axboe@kernel.dk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <locale.h>
#include <libgen.h>
#include "blktrace.h"
#include "rbtree.h"
#include "jhash.h"
static char blkparse_version[] = "1.2.0";
struct skip_info {
unsigned long start, end;
struct skip_info *prev, *next;
};
struct per_dev_info {
dev_t dev;
char *name;
int backwards;
unsigned long long events;
unsigned long long first_reported_time;
unsigned long long last_reported_time;
unsigned long long last_read_time;
struct io_stats io_stats;
unsigned long skips;
unsigned long long seq_skips;
unsigned int max_depth[2];
unsigned int cur_depth[2];
struct rb_root rb_track;
int nfiles;
int ncpus;
unsigned long *cpu_map;
unsigned int cpu_map_max;
struct per_cpu_info *cpus;
};
/*
* some duplicated effort here, we can unify this hash and the ppi hash later
*/
struct process_pid_map {
pid_t pid;
char comm[16];
struct process_pid_map *hash_next, *list_next;
};
#define PPM_HASH_SHIFT (8)
#define PPM_HASH_SIZE (1 << PPM_HASH_SHIFT)
#define PPM_HASH_MASK (PPM_HASH_SIZE - 1)
static struct process_pid_map *ppm_hash_table[PPM_HASH_SIZE];
struct per_process_info {
struct process_pid_map *ppm;
struct io_stats io_stats;
struct per_process_info *hash_next, *list_next;
int more_than_one;
/*
* individual io stats
*/
unsigned long long longest_allocation_wait[2];
unsigned long long longest_dispatch_wait[2];
unsigned long long longest_completion_wait[2];
};
#define PPI_HASH_SHIFT (8)
#define PPI_HASH_SIZE (1 << PPI_HASH_SHIFT)
#define PPI_HASH_MASK (PPI_HASH_SIZE - 1)
static struct per_process_info *ppi_hash_table[PPI_HASH_SIZE];
static struct per_process_info *ppi_list;
static int ppi_list_entries;
static struct option l_opts[] = {
{
.name = "act-mask",
.has_arg = required_argument,
.flag = NULL,
.val = 'a'
},
{
.name = "set-mask",
.has_arg = required_argument,
.flag = NULL,
.val = 'A'
},
{
.name = "batch",
.has_arg = required_argument,
.flag = NULL,
.val = 'b'
},
{
.name = "input-directory",
.has_arg = required_argument,
.flag = NULL,
.val = 'D'
},
{
.name = "dump-binary",
.has_arg = required_argument,
.flag = NULL,
.val = 'd'
},
{
.name = "format",
.has_arg = required_argument,
.flag = NULL,
.val = 'f'
},
{
.name = "format-spec",
.has_arg = required_argument,
.flag = NULL,
.val = 'F'
},
{
.name = "hash-by-name",
.has_arg = no_argument,
.flag = NULL,
.val = 'h'
},
{
.name = "input",
.has_arg = required_argument,
.flag = NULL,
.val = 'i'
},
{
.name = "no-msgs",
.has_arg = no_argument,
.flag = NULL,
.val = 'M'
},
{
.name = "output",
.has_arg = required_argument,
.flag = NULL,
.val = 'o'
},
{
.name = "no-text-output",
.has_arg = no_argument,
.flag = NULL,
.val = 'O'
},
{
.name = "quiet",
.has_arg = no_argument,
.flag = NULL,
.val = 'q'
},
{
.name = "per-program-stats",
.has_arg = no_argument,
.flag = NULL,
.val = 's'
},
{
.name = "track-ios",
.has_arg = no_argument,
.flag = NULL,
.val = 't'
},
{
.name = "stopwatch",
.has_arg = required_argument,
.flag = NULL,
.val = 'w'
},
{
.name = "verbose",
.has_arg = no_argument,
.flag = NULL,
.val = 'v'
},
{
.name = "version",
.has_arg = no_argument,
.flag = NULL,
.val = 'V'
},
{
.name = NULL,
}
};
/*
* for sorting the displayed output
*/
struct trace {
struct blk_io_trace *bit;
struct rb_node rb_node;
struct trace *next;
unsigned long read_sequence;
};
static struct rb_root rb_sort_root;
static unsigned long rb_sort_entries;
static struct trace *trace_list;
/*
* allocation cache
*/
static struct blk_io_trace *bit_alloc_list;
static struct trace *t_alloc_list;
/*
* for tracking individual ios
*/
struct io_track {
struct rb_node rb_node;
struct process_pid_map *ppm;
__u64 sector;
unsigned long long allocation_time;
unsigned long long queue_time;
unsigned long long dispatch_time;
unsigned long long completion_time;
};
static int ndevices;
static struct per_dev_info *devices;
static char *get_dev_name(struct per_dev_info *, char *, int);
static int trace_rb_insert_last(struct per_dev_info *, struct trace *);
FILE *ofp = NULL;
static char *output_name;
static char *input_dir;
static unsigned long long genesis_time;
static unsigned long long last_allowed_time;
static unsigned long long stopwatch_start; /* start from zero by default */
static unsigned long long stopwatch_end = -1ULL; /* "infinity" */
static unsigned long read_sequence;
static int per_process_stats;
static int per_device_and_cpu_stats = 1;
static int track_ios;
static int ppi_hash_by_pid = 1;
static int verbose;
static unsigned int act_mask = -1U;
static int stats_printed;
static int bin_output_msgs = 1;
int data_is_native = -1;
static FILE *dump_fp;
static char *dump_binary;
static unsigned int t_alloc_cache;
static unsigned int bit_alloc_cache;
#define RB_BATCH_DEFAULT (512)
static unsigned int rb_batch = RB_BATCH_DEFAULT;
static int pipeline;
static char *pipename;
static int text_output = 1;
#define is_done() (*(volatile int *)(&done))
static volatile int done;
struct timespec abs_start_time;
static unsigned long long start_timestamp;
static int have_drv_data = 0;
#define JHASH_RANDOM (0x3af5f2ee)
#define CPUS_PER_LONG (8 * sizeof(unsigned long))
#define CPU_IDX(cpu) ((cpu) / CPUS_PER_LONG)
#define CPU_BIT(cpu) ((cpu) & (CPUS_PER_LONG - 1))
static void output_binary(void *buf, int len)
{
if (dump_binary) {
size_t n = fwrite(buf, len, 1, dump_fp);
if (n != 1) {
perror(dump_binary);
fclose(dump_fp);
dump_binary = NULL;
}
}
}
static void resize_cpu_info(struct per_dev_info *pdi, int cpu)
{
struct per_cpu_info *cpus = pdi->cpus;
int ncpus = pdi->ncpus;
int new_count = cpu + 1;
int new_space, size;
char *new_start;
size = new_count * sizeof(struct per_cpu_info);
cpus = realloc(cpus, size);
if (!cpus) {
char name[20];
fprintf(stderr, "Out of memory, CPU info for device %s (%d)\n",
get_dev_name(pdi, name, sizeof(name)), size);
exit(1);
}
new_start = (char *)cpus + (ncpus * sizeof(struct per_cpu_info));
new_space = (new_count - ncpus) * sizeof(struct per_cpu_info);
memset(new_start, 0, new_space);
pdi->ncpus = new_count;
pdi->cpus = cpus;
for (new_count = 0; new_count < pdi->ncpus; new_count++) {
struct per_cpu_info *pci = &pdi->cpus[new_count];
if (!pci->fd) {
pci->fd = -1;
memset(&pci->rb_last, 0, sizeof(pci->rb_last));
pci->rb_last_entries = 0;
pci->last_sequence = -1;
}
}
}
static struct per_cpu_info *get_cpu_info(struct per_dev_info *pdi, int cpu)
{
struct per_cpu_info *pci;
if (cpu >= pdi->ncpus)
resize_cpu_info(pdi, cpu);
pci = &pdi->cpus[cpu];
pci->cpu = cpu;
return pci;
}
static int resize_devices(char *name)
{
int size = (ndevices + 1) * sizeof(struct per_dev_info);
devices = realloc(devices, size);
if (!devices) {
fprintf(stderr, "Out of memory, device %s (%d)\n", name, size);
return 1;
}
memset(&devices[ndevices], 0, sizeof(struct per_dev_info));
devices[ndevices].name = name;
ndevices++;
return 0;
}
static struct per_dev_info *get_dev_info(dev_t dev)
{
struct per_dev_info *pdi;
int i;
for (i = 0; i < ndevices; i++) {
if (!devices[i].dev)
devices[i].dev = dev;
if (devices[i].dev == dev)
return &devices[i];
}
if (resize_devices(NULL))
return NULL;
pdi = &devices[ndevices - 1];
pdi->dev = dev;
pdi->first_reported_time = 0;
pdi->last_read_time = 0;
return pdi;
}
static void insert_skip(struct per_cpu_info *pci, unsigned long start,
unsigned long end)
{
struct skip_info *sip;
for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
if (end == (sip->start - 1)) {
sip->start = start;
return;
} else if (start == (sip->end + 1)) {
sip->end = end;
return;
}
}
sip = malloc(sizeof(struct skip_info));
sip->start = start;
sip->end = end;
sip->prev = sip->next = NULL;
if (pci->skips_tail == NULL)
pci->skips_head = pci->skips_tail = sip;
else {
sip->prev = pci->skips_tail;
pci->skips_tail->next = sip;
pci->skips_tail = sip;
}
}
static void remove_sip(struct per_cpu_info *pci, struct skip_info *sip)
{
if (sip->prev == NULL) {
if (sip->next == NULL)
pci->skips_head = pci->skips_tail = NULL;
else {
pci->skips_head = sip->next;
sip->next->prev = NULL;
}
} else if (sip->next == NULL) {
pci->skips_tail = sip->prev;
sip->prev->next = NULL;
} else {
sip->prev->next = sip->next;
sip->next->prev = sip->prev;
}
sip->prev = sip->next = NULL;
free(sip);
}
#define IN_SKIP(sip,seq) (((sip)->start <= (seq)) && ((seq) <= sip->end))
static int check_current_skips(struct per_cpu_info *pci, unsigned long seq)
{
struct skip_info *sip;
for (sip = pci->skips_tail; sip != NULL; sip = sip->prev) {
if (IN_SKIP(sip, seq)) {
if (sip->start == seq) {
if (sip->end == seq)
remove_sip(pci, sip);
else
sip->start += 1;
} else if (sip->end == seq)
sip->end -= 1;
else {
sip->end = seq - 1;
insert_skip(pci, seq + 1, sip->end);
}
return 1;
}
}
return 0;
}
static void collect_pdi_skips(struct per_dev_info *pdi)
{
struct skip_info *sip;
int cpu;
pdi->skips = 0;
pdi->seq_skips = 0;
for (cpu = 0; cpu < pdi->ncpus; cpu++) {
struct per_cpu_info *pci = &pdi->cpus[cpu];
for (sip = pci->skips_head; sip != NULL; sip = sip->next) {
pdi->skips++;
pdi->seq_skips += (sip->end - sip->start + 1);
if (verbose)
fprintf(stderr,"(%d,%d): skipping %lu -> %lu\n",
MAJOR(pdi->dev), MINOR(pdi->dev),
sip->start, sip->end);
}
}
}
static void cpu_mark_online(struct per_dev_info *pdi, unsigned int cpu)
{
if (cpu >= pdi->cpu_map_max || !pdi->cpu_map) {
int new_max = (cpu + CPUS_PER_LONG) & ~(CPUS_PER_LONG - 1);
unsigned long *map = malloc(new_max / sizeof(long));
memset(map, 0, new_max / sizeof(long));
if (pdi->cpu_map) {
memcpy(map, pdi->cpu_map, pdi->cpu_map_max / sizeof(long));
free(pdi->cpu_map);
}
pdi->cpu_map = map;
pdi->cpu_map_max = new_max;
}
pdi->cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
}
static inline void cpu_mark_offline(struct per_dev_info *pdi, int cpu)
{
pdi->cpu_map[CPU_IDX(cpu)] &= ~(1UL << CPU_BIT(cpu));
}
static inline int cpu_is_online(struct per_dev_info *pdi, int cpu)
{
return (pdi->cpu_map[CPU_IDX(cpu)] & (1UL << CPU_BIT(cpu))) != 0;
}
static inline int ppm_hash_pid(pid_t pid)
{
return jhash_1word(pid, JHASH_RANDOM) & PPM_HASH_MASK;
}
static struct process_pid_map *find_ppm(pid_t pid)
{
const int hash_idx = ppm_hash_pid(pid);
struct process_pid_map *ppm;
ppm = ppm_hash_table[hash_idx];
while (ppm) {
if (ppm->pid == pid)
return ppm;
ppm = ppm->hash_next;
}
return NULL;
}
static struct process_pid_map *add_ppm_hash(pid_t pid, const char *name)
{
const int hash_idx = ppm_hash_pid(pid);
struct process_pid_map *ppm;
ppm = find_ppm(pid);
if (!ppm) {
ppm = malloc(sizeof(*ppm));
memset(ppm, 0, sizeof(*ppm));
ppm->pid = pid;
memset(ppm->comm, 0, sizeof(ppm->comm));
strncpy(ppm->comm, name, sizeof(ppm->comm));
ppm->comm[sizeof(ppm->comm) - 1] = '\0';
ppm->hash_next = ppm_hash_table[hash_idx];
ppm_hash_table[hash_idx] = ppm;
}
return ppm;
}
static void handle_notify(struct blk_io_trace *bit)
{
void *payload = (caddr_t) bit + sizeof(*bit);
__u32 two32[2];
switch (bit->action) {
case BLK_TN_PROCESS:
add_ppm_hash(bit->pid, payload);
break;
case BLK_TN_TIMESTAMP:
if (bit->pdu_len != sizeof(two32))
return;
memcpy(two32, payload, sizeof(two32));
if (!data_is_native) {
two32[0] = be32_to_cpu(two32[0]);
two32[1] = be32_to_cpu(two32[1]);
}
start_timestamp = bit->time;
abs_start_time.tv_sec = two32[0];
abs_start_time.tv_nsec = two32[1];
if (abs_start_time.tv_nsec < 0) {
abs_start_time.tv_sec--;
abs_start_time.tv_nsec += 1000000000;
}
break;
case BLK_TN_MESSAGE:
if (bit->pdu_len > 0) {
char msg[bit->pdu_len+1];
memcpy(msg, (char *)payload, bit->pdu_len);
msg[bit->pdu_len] = '\0';
fprintf(ofp,
"%3d,%-3d %2d %8s %5d.%09lu %5u %2s %3s %s\n",
MAJOR(bit->device), MINOR(bit->device),
bit->cpu, "0", (int) SECONDS(bit->time),
(unsigned long) NANO_SECONDS(bit->time),
0, "m", "N", msg);
}
break;
default:
/* Ignore unknown notify events */
;
}
}
char *find_process_name(pid_t pid)
{
struct process_pid_map *ppm = find_ppm(pid);
if (ppm)
return ppm->comm;
return NULL;
}
static inline int ppi_hash_pid(pid_t pid)
{
return jhash_1word(pid, JHASH_RANDOM) & PPI_HASH_MASK;
}
static inline int ppi_hash_name(const char *name)
{
return jhash(name, 16, JHASH_RANDOM) & PPI_HASH_MASK;
}
static inline int ppi_hash(struct per_process_info *ppi)
{
struct process_pid_map *ppm = ppi->ppm;
if (ppi_hash_by_pid)
return ppi_hash_pid(ppm->pid);
return ppi_hash_name(ppm->comm);
}
static inline void add_ppi_to_hash(struct per_process_info *ppi)
{
const int hash_idx = ppi_hash(ppi);
ppi->hash_next = ppi_hash_table[hash_idx];
ppi_hash_table[hash_idx] = ppi;
}
static inline void add_ppi_to_list(struct per_process_info *ppi)
{
ppi->list_next = ppi_list;
ppi_list = ppi;
ppi_list_entries++;
}
static struct per_process_info *find_ppi_by_name(char *name)
{
const int hash_idx = ppi_hash_name(name);
struct per_process_info *ppi;
ppi = ppi_hash_table[hash_idx];
while (ppi) {
struct process_pid_map *ppm = ppi->ppm;
if (!strcmp(ppm->comm, name))
return ppi;
ppi = ppi->hash_next;
}
return NULL;
}
static struct per_process_info *find_ppi_by_pid(pid_t pid)
{
const int hash_idx = ppi_hash_pid(pid);
struct per_process_info *ppi;
ppi = ppi_hash_table[hash_idx];
while (ppi) {
struct process_pid_map *ppm = ppi->ppm;
if (ppm->pid == pid)
return ppi;
ppi = ppi->hash_next;
}
return NULL;
}
static struct per_process_info *find_ppi(pid_t pid)
{
struct per_process_info *ppi;
char *name;
if (ppi_hash_by_pid)
return find_ppi_by_pid(pid);
name = find_process_name(pid);
if (!name)
return NULL;
ppi = find_ppi_by_name(name);
if (ppi && ppi->ppm->pid != pid)
ppi->more_than_one = 1;
return ppi;
}
/*
* struct trace and blktrace allocation cache, we do potentially
* millions of mallocs for these structures while only using at most
* a few thousand at the time
*/
static inline void t_free(struct trace *t)
{
if (t_alloc_cache < 1024) {
t->next = t_alloc_list;
t_alloc_list = t;
t_alloc_cache++;
} else
free(t);
}
static inline struct trace *t_alloc(void)
{
struct trace *t = t_alloc_list;
if (t) {
t_alloc_list = t->next;
t_alloc_cache--;
return t;
}
return malloc(sizeof(*t));
}
static inline void bit_free(struct blk_io_trace *bit)
{
if (bit_alloc_cache < 1024 && !bit->pdu_len) {
/*
* abuse a 64-bit field for a next pointer for the free item
*/
bit->time = (__u64) (unsigned long) bit_alloc_list;
bit_alloc_list = (struct blk_io_trace *) bit;
bit_alloc_cache++;
} else
free(bit);
}
static inline struct blk_io_trace *bit_alloc(void)
{
struct blk_io_trace *bit = bit_alloc_list;
if (bit) {
bit_alloc_list = (struct blk_io_trace *) (unsigned long) \
bit->time;
bit_alloc_cache--;
return bit;
}
return malloc(sizeof(*bit));
}
static inline void __put_trace_last(struct per_dev_info *pdi, struct trace *t)
{
struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
rb_erase(&t->rb_node, &pci->rb_last);
pci->rb_last_entries--;
bit_free(t->bit);
t_free(t);
}
static void put_trace(struct per_dev_info *pdi, struct trace *t)
{
rb_erase(&t->rb_node, &rb_sort_root);
rb_sort_entries--;
trace_rb_insert_last(pdi, t);
}
static inline int trace_rb_insert(struct trace *t, struct rb_root *root)
{
struct rb_node **p = &root->rb_node;
struct rb_node *parent = NULL;
struct trace *__t;
while (*p) {
parent = *p;
__t = rb_entry(parent, struct trace, rb_node);
if (t->bit->time < __t->bit->time)
p = &(*p)->rb_left;
else if (t->bit->time > __t->bit->time)
p = &(*p)->rb_right;
else if (t->bit->device < __t->bit->device)
p = &(*p)->rb_left;
else if (t->bit->device > __t->bit->device)
p = &(*p)->rb_right;
else if (t->bit->sequence < __t->bit->sequence)
p = &(*p)->rb_left;
else /* >= sequence */
p = &(*p)->rb_right;
}
rb_link_node(&t->rb_node, parent, p);
rb_insert_color(&t->rb_node, root);
return 0;
}
static inline int trace_rb_insert_sort(struct trace *t)
{
if (!trace_rb_insert(t, &rb_sort_root)) {
rb_sort_entries++;
return 0;
}
return 1;
}
static int trace_rb_insert_last(struct per_dev_info *pdi, struct trace *t)
{
struct per_cpu_info *pci = get_cpu_info(pdi, t->bit->cpu);
if (trace_rb_insert(t, &pci->rb_last))
return 1;
pci->rb_last_entries++;
if (pci->rb_last_entries > rb_batch * pdi->nfiles) {
struct rb_node *n = rb_first(&pci->rb_last);
t = rb_entry(n, struct trace, rb_node);
__put_trace_last(pdi, t);
}
return 0;
}
static struct trace *trace_rb_find(dev_t device, unsigned long sequence,
struct rb_root *root, int order)
{
struct rb_node *n = root->rb_node;
struct rb_node *prev = NULL;
struct trace *__t;
while (n) {
__t = rb_entry(n, struct trace, rb_node);
prev = n;
if (device < __t->bit->device)
n = n->rb_left;
else if (device > __t->bit->device)
n = n->rb_right;
else if (sequence < __t->bit->sequence)
n = n->rb_left;
else if (sequence > __t->bit->sequence)
n = n->rb_right;
else
return __t;
}
/*
* hack - the list may not be sequence ordered because some
* events don't have sequence and time matched. so we end up
* being a little off in the rb lookup here, because we don't
* know the time we are looking for. compensate by browsing
* a little ahead from the last entry to find the match
*/
if (order && prev) {
int max = 5;
while (((n = rb_next(prev)) != NULL) && max--) {
__t = rb_entry(n, struct trace, rb_node);
if (__t->bit->device == device &&
__t->bit->sequence == sequence)
return __t;
prev = n;
}
}
return NULL;
}
static inline struct trace *trace_rb_find_last(struct per_dev_info *pdi,
struct per_cpu_info *pci,
unsigned long seq)
{
return trace_rb_find(pdi->dev, seq, &pci->rb_last, 0);
}
static inline int track_rb_insert(struct per_dev_info *pdi,struct io_track *iot)
{
struct rb_node **p = &pdi->rb_track.rb_node;
struct rb_node *parent = NULL;
struct io_track *__iot;
while (*p) {
parent = *p;
__iot = rb_entry(parent, struct io_track, rb_node);
if (iot->sector < __iot->sector)
p = &(*p)->rb_left;
else if (iot->sector > __iot->sector)
p = &(*p)->rb_right;
else {
fprintf(stderr,
"sector alias (%Lu) on device %d,%d!\n",
(unsigned long long) iot->sector,
MAJOR(pdi->dev), MINOR(pdi->dev));
return 1;
}
}
rb_link_node(&iot->rb_node, parent, p);
rb_insert_color(&iot->rb_node, &pdi->rb_track);
return 0;
}
static struct io_track *__find_track(struct per_dev_info *pdi, __u64 sector)
{
struct rb_node *n = pdi->rb_track.rb_node;
struct io_track *__iot;
while (n) {
__iot = rb_entry(n, struct io_track, rb_node);
if (sector < __iot->sector)
n = n->rb_left;
else if (sector > __iot->sector)
n = n->rb_right;
else
return __iot;
}
return NULL;
}
static struct io_track *find_track(struct per_dev_info *pdi, pid_t pid,
__u64 sector)
{
struct io_track *iot;
iot = __find_track(pdi, sector);
if (!iot) {
iot = malloc(sizeof(*iot));
iot->ppm = find_ppm(pid);
if (!iot->ppm)
iot->ppm = add_ppm_hash(pid, "unknown");
iot->sector = sector;
track_rb_insert(pdi, iot);
}
return iot;
}
static void log_track_frontmerge(struct per_dev_info *pdi,
struct blk_io_trace *t)
{
struct io_track *iot;
if (!track_ios)
return;
iot = __find_track(pdi, t->sector + t_sec(t));
if (!iot) {
if (verbose)
fprintf(stderr, "merge not found for (%d,%d): %llu\n",
MAJOR(pdi->dev), MINOR(pdi->dev),
(unsigned long long) t->sector + t_sec(t));
return;
}
rb_erase(&iot->rb_node, &pdi->rb_track);
iot->sector -= t_sec(t);
track_rb_insert(pdi, iot);
}
static void log_track_getrq(struct per_dev_info *pdi, struct blk_io_trace *t)
{
struct io_track *iot;
if (!track_ios)
return;
iot = find_track(pdi, t->pid, t->sector);
iot->allocation_time = t->time;
}
static inline int is_remapper(struct per_dev_info *pdi)
{
int major = MAJOR(pdi->dev);
return (major == 253 || major == 9);
}
/*
* for md/dm setups, the interesting cycle is Q -> C. So track queueing
* time here, as dispatch time
*/
static void log_track_queue(struct per_dev_info *pdi, struct blk_io_trace *t)
{
struct io_track *iot;
if (!track_ios)
return;
if (!is_remapper(pdi))
return;
iot = find_track(pdi, t->pid, t->sector);
iot->dispatch_time = t->time;
}
/*
* return time between rq allocation and insertion
*/
static unsigned long long log_track_insert(struct per_dev_info *pdi,
struct blk_io_trace *t)
{
unsigned long long elapsed;
struct io_track *iot;
if (!track_ios)
return -1;
iot = find_track(pdi, t->pid, t->sector);
iot->queue_time = t->time;
if (!iot->allocation_time)
return -1;
elapsed = iot->queue_time - iot->allocation_time;
if (per_process_stats) {
struct per_process_info *ppi = find_ppi(iot->ppm->pid);
int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
if (ppi && elapsed > ppi->longest_allocation_wait[w])
ppi->longest_allocation_wait[w] = elapsed;
}
return elapsed;
}
/*
* return time between queue and issue
*/
static unsigned long long log_track_issue(struct per_dev_info *pdi,
struct blk_io_trace *t)
{
unsigned long long elapsed;
struct io_track *iot;
if (!track_ios)
return -1;
if ((t->action & BLK_TC_ACT(BLK_TC_FS)) == 0)
return -1;
iot = __find_track(pdi, t->sector);
if (!iot) {
if (verbose)
fprintf(stderr, "issue not found for (%d,%d): %llu\n",
MAJOR(pdi->dev), MINOR(pdi->dev),
(unsigned long long) t->sector);
return -1;
}
iot->dispatch_time = t->time;
elapsed = iot->dispatch_time - iot->queue_time;
if (per_process_stats) {
struct per_process_info *ppi = find_ppi(iot->ppm->pid);
int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
if (ppi && elapsed > ppi->longest_dispatch_wait[w])
ppi->longest_dispatch_wait[w] = elapsed;
}
return elapsed;
}
/*
* return time between dispatch and complete
*/
static unsigned long long log_track_complete(struct per_dev_info *pdi,
struct blk_io_trace *t)
{
unsigned long long elapsed;
struct io_track *iot;
if (!track_ios)
return -1;
iot = __find_track(pdi, t->sector);
if (!iot) {
if (verbose)
fprintf(stderr,"complete not found for (%d,%d): %llu\n",
MAJOR(pdi->dev), MINOR(pdi->dev),
(unsigned long long) t->sector);
return -1;
}
iot->completion_time = t->time;
elapsed = iot->completion_time - iot->dispatch_time;
if (per_process_stats) {
struct per_process_info *ppi = find_ppi(iot->ppm->pid);
int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
if (ppi && elapsed > ppi->longest_completion_wait[w])
ppi->longest_completion_wait[w] = elapsed;
}
/*
* kill the trace, we don't need it after completion
*/
rb_erase(&iot->rb_node, &pdi->rb_track);
free(iot);
return elapsed;
}
static struct io_stats *find_process_io_stats(pid_t pid)
{
struct per_process_info *ppi = find_ppi(pid);
if (!ppi) {
ppi = malloc(sizeof(*ppi));
memset(ppi, 0, sizeof(*ppi));
ppi->ppm = find_ppm(pid);
if (!ppi->ppm)
ppi->ppm = add_ppm_hash(pid, "unknown");
add_ppi_to_hash(ppi);
add_ppi_to_list(ppi);
}
return &ppi->io_stats;
}
static char *get_dev_name(struct per_dev_info *pdi, char *buffer, int size)
{
if (pdi->name)
snprintf(buffer, size, "%s", pdi->name);
else
snprintf(buffer, size, "%d,%d",MAJOR(pdi->dev),MINOR(pdi->dev));
return buffer;
}
static void check_time(struct per_dev_info *pdi, struct blk_io_trace *bit)
{
unsigned long long this = bit->time;
unsigned long long last = pdi->last_reported_time;
pdi->backwards = (this < last) ? 'B' : ' ';
pdi->last_reported_time = this;
}
static inline void __account_m(struct io_stats *ios, struct blk_io_trace *t,
int rw)
{
if (rw) {
ios->mwrites++;
ios->mwrite_kb += t_kb(t);
ios->mwrite_b += t_b(t);
} else {
ios->mreads++;
ios->mread_kb += t_kb(t);
ios->mread_b += t_b(t);
}
}
static inline void account_m(struct blk_io_trace *t, struct per_cpu_info *pci,
int rw)
{
__account_m(&pci->io_stats, t, rw);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_m(ios, t, rw);
}
}
static inline void __account_pc_queue(struct io_stats *ios,
struct blk_io_trace *t, int rw)
{
if (rw) {
ios->qwrites_pc++;
ios->qwrite_kb_pc += t_kb(t);
ios->qwrite_b_pc += t_b(t);
} else {
ios->qreads_pc++;
ios->qread_kb += t_kb(t);
ios->qread_b_pc += t_b(t);
}
}
static inline void account_pc_queue(struct blk_io_trace *t,
struct per_cpu_info *pci, int rw)
{
__account_pc_queue(&pci->io_stats, t, rw);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_pc_queue(ios, t, rw);
}
}
static inline void __account_pc_issue(struct io_stats *ios, int rw,
unsigned int bytes)
{
if (rw) {
ios->iwrites_pc++;
ios->iwrite_kb_pc += bytes >> 10;
ios->iwrite_b_pc += bytes & 1023;
} else {
ios->ireads_pc++;
ios->iread_kb_pc += bytes >> 10;
ios->iread_b_pc += bytes & 1023;
}
}
static inline void account_pc_issue(struct blk_io_trace *t,
struct per_cpu_info *pci, int rw)
{
__account_pc_issue(&pci->io_stats, rw, t->bytes);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_pc_issue(ios, rw, t->bytes);
}
}
static inline void __account_pc_requeue(struct io_stats *ios,
struct blk_io_trace *t, int rw)
{
if (rw) {
ios->wrqueue_pc++;
ios->iwrite_kb_pc -= t_kb(t);
ios->iwrite_b_pc -= t_b(t);
} else {
ios->rrqueue_pc++;
ios->iread_kb_pc -= t_kb(t);
ios->iread_b_pc -= t_b(t);
}
}
static inline void account_pc_requeue(struct blk_io_trace *t,
struct per_cpu_info *pci, int rw)
{
__account_pc_requeue(&pci->io_stats, t, rw);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_pc_requeue(ios, t, rw);
}
}
static inline void __account_pc_c(struct io_stats *ios, int rw)
{
if (rw)
ios->cwrites_pc++;
else
ios->creads_pc++;
}
static inline void account_pc_c(struct blk_io_trace *t,
struct per_cpu_info *pci, int rw)
{
__account_pc_c(&pci->io_stats, rw);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_pc_c(ios, rw);
}
}
static inline void __account_queue(struct io_stats *ios, struct blk_io_trace *t,
int rw)
{
if (rw) {
ios->qwrites++;
ios->qwrite_kb += t_kb(t);
ios->qwrite_b += t_b(t);
} else {
ios->qreads++;
ios->qread_kb += t_kb(t);
ios->qread_b += t_b(t);
}
}
static inline void account_queue(struct blk_io_trace *t,
struct per_cpu_info *pci, int rw)
{
__account_queue(&pci->io_stats, t, rw);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_queue(ios, t, rw);
}
}
static inline void __account_c(struct io_stats *ios, int rw, int bytes)
{
if (rw) {
ios->cwrites++;
ios->cwrite_kb += bytes >> 10;
ios->cwrite_b += bytes & 1023;
} else {
ios->creads++;
ios->cread_kb += bytes >> 10;
ios->cread_b += bytes & 1023;
}
}
static inline void account_c(struct blk_io_trace *t, struct per_cpu_info *pci,
int rw, int bytes)
{
__account_c(&pci->io_stats, rw, bytes);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_c(ios, rw, bytes);
}
}
static inline void __account_issue(struct io_stats *ios, int rw,
unsigned int bytes)
{
if (rw) {
ios->iwrites++;
ios->iwrite_kb += bytes >> 10;
ios->iwrite_b += bytes & 1023;
} else {
ios->ireads++;
ios->iread_kb += bytes >> 10;
ios->iread_b += bytes & 1023;
}
}
static inline void account_issue(struct blk_io_trace *t,
struct per_cpu_info *pci, int rw)
{
__account_issue(&pci->io_stats, rw, t->bytes);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_issue(ios, rw, t->bytes);
}
}
static inline void __account_unplug(struct io_stats *ios, int timer)
{
if (timer)
ios->timer_unplugs++;
else
ios->io_unplugs++;
}
static inline void account_unplug(struct blk_io_trace *t,
struct per_cpu_info *pci, int timer)
{
__account_unplug(&pci->io_stats, timer);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_unplug(ios, timer);
}
}
static inline void __account_requeue(struct io_stats *ios,
struct blk_io_trace *t, int rw)
{
if (rw) {
ios->wrqueue++;
ios->iwrite_kb -= t_kb(t);
ios->iwrite_b -= t_b(t);
} else {
ios->rrqueue++;
ios->iread_kb -= t_kb(t);
ios->iread_b -= t_b(t);
}
}
static inline void account_requeue(struct blk_io_trace *t,
struct per_cpu_info *pci, int rw)
{
__account_requeue(&pci->io_stats, t, rw);
if (per_process_stats) {
struct io_stats *ios = find_process_io_stats(t->pid);
__account_requeue(ios, t, rw);
}
}
static void log_complete(struct per_dev_info *pdi, struct per_cpu_info *pci,
struct blk_io_trace *t, char *act)
{
process_fmt(act, pci, t, log_track_complete(pdi, t), 0, NULL);
}
static void log_insert(struct per_dev_info *pdi, struct per_cpu_info *pci,
struct blk_io_trace *t, char *act)
{
process_fmt(act, pci, t, log_track_insert(pdi, t), 0, NULL);
}
static void log_queue(struct per_cpu_info *pci, struct blk_io_trace *t,
char *act)
{
process_fmt(act, pci, t, -1, 0, NULL);
}
static void log_issue(struct per_dev_info *pdi, struct per_cpu_info *pci,
struct blk_io_trace *t, char *act)
{
process_fmt(act, pci, t, log_track_issue(pdi, t), 0, NULL);
}
static void log_merge(struct per_dev_info *pdi, struct per_cpu_info *pci,
struct blk_io_trace *t, char *act)
{
if (act[0] == 'F')
log_track_frontmerge(pdi, t);
process_fmt(act, pci, t, -1ULL, 0, NULL);
}
static void log_action(struct per_cpu_info *pci, struct blk_io_trace *t,
char *act)
{
process_fmt(act, pci, t, -1ULL, 0, NULL);
}
static void log_generic(struct per_cpu_info *pci, struct blk_io_trace *t,
char *act)
{
process_fmt(act, pci, t, -1ULL, 0, NULL);
}
static void log_unplug(struct per_cpu_info *pci, struct blk_io_trace *t,
char *act)
{
process_fmt(act, pci, t, -1ULL, 0, NULL);
}
static void log_split(struct per_cpu_info *pci, struct blk_io_trace *t,
char *act)
{
process_fmt(act, pci, t, -1ULL, 0, NULL);
}
static void log_pc(struct per_cpu_info *pci, struct blk_io_trace *t, char *act)
{
unsigned char *buf = (unsigned char *) t + sizeof(*t);
process_fmt(act, pci, t, -1ULL, t->pdu_len, buf);
}
static void dump_trace_pc(struct blk_io_trace *t, struct per_dev_info *pdi,
struct per_cpu_info *pci)
{
int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
int act = t->action & 0xffff;
switch (act) {
case __BLK_TA_QUEUE:
log_generic(pci, t, "Q");
account_pc_queue(t, pci, w);
break;
case __BLK_TA_GETRQ:
log_generic(pci, t, "G");
break;
case __BLK_TA_SLEEPRQ:
log_generic(pci, t, "S");
break;
case __BLK_TA_REQUEUE:
/*
* can happen if we miss traces, don't let it go
* below zero
*/
if (pdi->cur_depth[w])
pdi->cur_depth[w]--;
account_pc_requeue(t, pci, w);
log_generic(pci, t, "R");
break;
case __BLK_TA_ISSUE:
account_pc_issue(t, pci, w);
pdi->cur_depth[w]++;
if (pdi->cur_depth[w] > pdi->max_depth[w])
pdi->max_depth[w] = pdi->cur_depth[w];
log_pc(pci, t, "D");
break;
case __BLK_TA_COMPLETE:
if (pdi->cur_depth[w])
pdi->cur_depth[w]--;
log_pc(pci, t, "C");
account_pc_c(t, pci, w);
break;
case __BLK_TA_INSERT:
log_pc(pci, t, "I");
break;
default:
fprintf(stderr, "Bad pc action %x\n", act);
break;
}
}
static void dump_trace_fs(struct blk_io_trace *t, struct per_dev_info *pdi,
struct per_cpu_info *pci)
{
int w = (t->action & BLK_TC_ACT(BLK_TC_WRITE)) != 0;
int act = t->action & 0xffff;
switch (act) {
case __BLK_TA_QUEUE:
log_track_queue(pdi, t);
account_queue(t, pci, w);
log_queue(pci, t, "Q");
break;
case __BLK_TA_INSERT:
log_insert(pdi, pci, t, "I");
break;
case __BLK_TA_BACKMERGE:
account_m(t, pci, w);
log_merge(pdi, pci, t, "M");
break;
case __BLK_TA_FRONTMERGE:
account_m(t, pci, w);
log_merge(pdi, pci, t, "F");
break;
case __BLK_TA_GETRQ:
log_track_getrq(pdi, t);
log_generic(pci, t, "G");
break;
case __BLK_TA_SLEEPRQ:
log_generic(pci, t, "S");
break;
case __BLK_TA_REQUEUE:
/*
* can happen if we miss traces, don't let it go
* below zero
*/
if (pdi->cur_depth[w])
pdi->cur_depth[w]--;
account_requeue(t, pci, w);
log_queue(pci, t, "R");
break;
case __BLK_TA_ISSUE:
account_issue(t, pci, w);
pdi->cur_depth[w]++;
if (pdi->cur_depth[w] > pdi->max_depth[w])
pdi->max_depth[w] = pdi->cur_depth[w];
log_issue(pdi, pci, t, "D");
break;
case __BLK_TA_COMPLETE:
if (pdi->cur_depth[w])
pdi->cur_depth[w]--;
account_c(t, pci, w, t->bytes);
log_complete(pdi, pci, t, "C");
break;
case __BLK_TA_PLUG:
log_action(pci, t, "P");
break;
case __BLK_TA_UNPLUG_IO:
account_unplug(t, pci, 0);
log_unplug(pci, t, "U");
break;
case __BLK_TA_UNPLUG_TIMER:
account_unplug(t, pci, 1);
log_unplug(pci, t, "UT");
break;
case __BLK_TA_SPLIT:
log_split(pci, t, "X");
break;
case __BLK_TA_BOUNCE:
log_generic(pci, t, "B");
break;
case __BLK_TA_REMAP:
log_generic(pci, t, "A");
break;
case __BLK_TA_DRV_DATA:
have_drv_data = 1;
/* dump to binary file only */
break;
default:
fprintf(stderr, "Bad fs action %x\n", t->action);
break;
}
}
static void dump_trace(struct blk_io_trace *t, struct per_cpu_info *pci,
struct per_dev_info *pdi)
{
if (text_output) {
if (t->action == BLK_TN_MESSAGE)
handle_notify(t);
else if (t->action & BLK_TC_ACT(BLK_TC_PC))
dump_trace_pc(t, pdi, pci);
else
dump_trace_fs(t, pdi, pci);
}
if (!pdi->events)
pdi->first_reported_time = t->time;
pdi->events++;
if (bin_output_msgs ||
!(t->action & BLK_TC_ACT(BLK_TC_NOTIFY) &&
t->action == BLK_TN_MESSAGE))
output_binary(t, sizeof(*t) + t->pdu_len);
}
/*
* print in a proper way, not too small and not too big. if more than
* 1000,000K, turn into M and so on
*/
static char *size_cnv(char *dst, unsigned long long num, int in_kb)
{
char suff[] = { '\0', 'K', 'M', 'G', 'P' };
unsigned int i = 0;
if (in_kb)
i++;
while (num > 1000 * 1000ULL && (i < sizeof(suff) - 1)) {
i++;
num /= 1000;
}
sprintf(dst, "%'8Lu%c", num, suff[i]);
return dst;
}
static void dump_io_stats(struct per_dev_info *pdi, struct io_stats *ios,
char *msg)
{
static char x[256], y[256];
fprintf(ofp, "%s\n", msg);
fprintf(ofp, " Reads Queued: %s, %siB\t",
size_cnv(x, ios->qreads, 0),
size_cnv(y, ios->qread_kb + (ios->qread_b>>10), 1));
fprintf(ofp, " Writes Queued: %s, %siB\n",
size_cnv(x, ios->qwrites, 0),
size_cnv(y, ios->qwrite_kb + (ios->qwrite_b>>10), 1));
fprintf(ofp, " Read Dispatches: %s, %siB\t",
size_cnv(x, ios->ireads, 0),
size_cnv(y, ios->iread_kb + (ios->iread_b>>10), 1));
fprintf(ofp, " Write Dispatches: %s, %siB\n",
size_cnv(x, ios->iwrites, 0),
size_cnv(y, ios->iwrite_kb + (ios->iwrite_b>>10), 1));
fprintf(ofp, " Reads Requeued: %s\t\t", size_cnv(x, ios->rrqueue, 0));
fprintf(ofp, " Writes Requeued: %s\n", size_cnv(x, ios->wrqueue, 0));
fprintf(ofp, " Reads Completed: %s, %siB\t",
size_cnv(x, ios->creads, 0),
size_cnv(y, ios->cread_kb + (ios->cread_b>>10), 1));
fprintf(ofp, " Writes Completed: %s, %siB\n",
size_cnv(x, ios->cwrites, 0),
size_cnv(y, ios->cwrite_kb + (ios->cwrite_b>>10), 1));
fprintf(ofp, " Read Merges: %s, %siB\t",
size_cnv(x, ios->mreads, 0),
size_cnv(y, ios->mread_kb + (ios->mread_b>>10), 1));
fprintf(ofp, " Write Merges: %s, %siB\n",
size_cnv(x, ios->mwrites, 0),
size_cnv(y, ios->mwrite_kb + (ios->mwrite_b>>10), 1));
if (pdi) {
fprintf(ofp, " Read depth: %'8u%8c\t", pdi->max_depth[0], ' ');
fprintf(ofp, " Write depth: %'8u\n", pdi->max_depth[1]);
}
if (ios->qreads_pc || ios->qwrites_pc || ios->ireads_pc || ios->iwrites_pc ||
ios->rrqueue_pc || ios->wrqueue_pc || ios->creads_pc || ios->cwrites_pc) {
fprintf(ofp, " PC Reads Queued: %s, %siB\t",
size_cnv(x, ios->qreads_pc, 0),
size_cnv(y,
ios->qread_kb_pc + (ios->qread_b_pc>>10), 1));
fprintf(ofp, " PC Writes Queued: %s, %siB\n",
size_cnv(x, ios->qwrites_pc, 0),
size_cnv(y,
ios->qwrite_kb_pc + (ios->qwrite_b_pc>>10), 1));
fprintf(ofp, " PC Read Disp.: %s, %siB\t",
size_cnv(x, ios->ireads_pc, 0),
size_cnv(y,
ios->iread_kb_pc + (ios->iread_b_pc>>10), 1));
fprintf(ofp, " PC Write Disp.: %s, %siB\n",
size_cnv(x, ios->iwrites_pc, 0),
size_cnv(y,
ios->iwrite_kb_pc + (ios->iwrite_b_pc>>10),
1));
fprintf(ofp, " PC Reads Req.: %s\t\t", size_cnv(x, ios->rrqueue_pc, 0));
fprintf(ofp, " PC Writes Req.: %s\n", size_cnv(x, ios->wrqueue_pc, 0));
fprintf(ofp, " PC Reads Compl.: %s\t\t", size_cnv(x, ios->creads_pc, 0));
fprintf(ofp, " PC Writes Compl.: %s\n", size_cnv(x, ios->cwrites_pc, 0));
}
fprintf(ofp, " IO unplugs: %'8lu%8c\t", ios->io_unplugs, ' ');
fprintf(ofp, " Timer unplugs: %'8lu\n", ios->timer_unplugs);
}
static void dump_wait_stats(struct per_process_info *ppi)
{
unsigned long rawait = ppi->longest_allocation_wait[0] / 1000;
unsigned long rdwait = ppi->longest_dispatch_wait[0] / 1000;
unsigned long rcwait = ppi->longest_completion_wait[0] / 1000;
unsigned long wawait = ppi->longest_allocation_wait[1] / 1000;
unsigned long wdwait = ppi->longest_dispatch_wait[1] / 1000;
unsigned long wcwait = ppi->longest_completion_wait[1] / 1000;
fprintf(ofp, " Allocation wait: %'8lu%8c\t", rawait, ' ');
fprintf(ofp, " Allocation wait: %'8lu\n", wawait);
fprintf(ofp, " Dispatch wait: %'8lu%8c\t", rdwait, ' ');
fprintf(ofp, " Dispatch wait: %'8lu\n", wdwait);
fprintf(ofp, " Completion wait: %'8lu%8c\t", rcwait, ' ');
fprintf(ofp, " Completion wait: %'8lu\n", wcwait);
}
static int ppi_name_compare(const void *p1, const void *p2)
{
struct per_process_info *ppi1 = *((struct per_process_info **) p1);
struct per_process_info *ppi2 = *((struct per_process_info **) p2);
int res;
res = strverscmp(ppi1->ppm->comm, ppi2->ppm->comm);
if (!res)
res = ppi1->ppm->pid > ppi2->ppm->pid;
return res;
}
static void sort_process_list(void)
{
struct per_process_info **ppis;
struct per_process_info *ppi;
int i = 0;
ppis = malloc(ppi_list_entries * sizeof(struct per_process_info *));
ppi = ppi_list;
while (ppi) {
ppis[i++] = ppi;
ppi = ppi->list_next;
}
qsort(ppis, ppi_list_entries, sizeof(ppi), ppi_name_compare);
i = ppi_list_entries - 1;
ppi_list = NULL;
while (i >= 0) {
ppi = ppis[i];
ppi->list_next = ppi_list;
ppi_list = ppi;
i--;
}
free(ppis);
}
static void show_process_stats(void)
{
struct per_process_info *ppi;
sort_process_list();
ppi = ppi_list;
while (ppi) {
struct process_pid_map *ppm = ppi->ppm;
char name[64];
if (ppi->more_than_one)
sprintf(name, "%s (%u, ...)", ppm->comm, ppm->pid);
else
sprintf(name, "%s (%u)", ppm->comm, ppm->pid);
dump_io_stats(NULL, &ppi->io_stats, name);
dump_wait_stats(ppi);
ppi = ppi->list_next;
}
fprintf(ofp, "\n");
}
static void show_device_and_cpu_stats(void)
{
struct per_dev_info *pdi;
struct per_cpu_info *pci;
struct io_stats total, *ios;
unsigned long long rrate, wrate, msec;
int i, j, pci_events;
char line[3 + 8/*cpu*/ + 2 + 32/*dev*/ + 3];
char name[32];
double ratio;
for (pdi = devices, i = 0; i < ndevices; i++, pdi++) {
memset(&total, 0, sizeof(total));
pci_events = 0;
if (i > 0)
fprintf(ofp, "\n");
for (pci = pdi->cpus, j = 0; j < pdi->ncpus; j++, pci++) {
if (!pci->nelems)
continue;
ios = &pci->io_stats;
total.qreads += ios->qreads;
total.qwrites += ios->qwrites;
total.creads += ios->creads;
total.cwrites += ios->cwrites;
total.mreads += ios->mreads;
total.mwrites += ios->mwrites;
total.ireads += ios->ireads;
total.iwrites += ios->iwrites;
total.rrqueue += ios->rrqueue;
total.wrqueue += ios->wrqueue;
total.qread_kb += ios->qread_kb;
total.qwrite_kb += ios->qwrite_kb;
total.cread_kb += ios->cread_kb;
total.cwrite_kb += ios->cwrite_kb;
total.iread_kb += ios->iread_kb;
total.iwrite_kb += ios->iwrite_kb;
total.mread_kb += ios->mread_kb;
total.mwrite_kb += ios->mwrite_kb;
total.qread_b += ios->qread_b;
total.qwrite_b += ios->qwrite_b;
total.cread_b += ios->cread_b;
total.cwrite_b += ios->cwrite_b;
total.iread_b += ios->iread_b;
total.iwrite_b += ios->iwrite_b;
total.mread_b += ios->mread_b;
total.mwrite_b += ios->mwrite_b;
total.qreads_pc += ios->qreads_pc;
total.qwrites_pc += ios->qwrites_pc;
total.creads_pc += ios->creads_pc;
total.cwrites_pc += ios->cwrites_pc;
total.ireads_pc += ios->ireads_pc;
total.iwrites_pc += ios->iwrites_pc;
total.rrqueue_pc += ios->rrqueue_pc;
total.wrqueue_pc += ios->wrqueue_pc;
total.qread_kb_pc += ios->qread_kb_pc;
total.qwrite_kb_pc += ios->qwrite_kb_pc;
total.iread_kb_pc += ios->iread_kb_pc;
total.iwrite_kb_pc += ios->iwrite_kb_pc;
total.qread_b_pc += ios->qread_b_pc;
total.qwrite_b_pc += ios->qwrite_b_pc;
total.iread_b_pc += ios->iread_b_pc;
total.iwrite_b_pc += ios->iwrite_b_pc;
total.timer_unplugs += ios->timer_unplugs;
total.io_unplugs += ios->io_unplugs;
snprintf(line, sizeof(line) - 1, "CPU%d (%s):",
j, get_dev_name(pdi, name, sizeof(name)));
dump_io_stats(pdi, ios, line);
pci_events++;
}
if (pci_events > 1) {
fprintf(ofp, "\n");
snprintf(line, sizeof(line) - 1, "Total (%s):",
get_dev_name(pdi, name, sizeof(name)));
dump_io_stats(NULL, &total, line);
}
wrate = rrate = 0;
msec = (pdi->last_reported_time - pdi->first_reported_time) / 1000000;
if (msec) {
rrate = ((1000 * total.cread_kb) + total.cread_b) /
msec;
wrate = ((1000 * total.cwrite_kb) + total.cwrite_b) /
msec;
}
fprintf(ofp, "\nThroughput (R/W): %'LuKiB/s / %'LuKiB/s\n",
rrate, wrate);
fprintf(ofp, "Events (%s): %'Lu entries\n",
get_dev_name(pdi, line, sizeof(line)), pdi->events);
collect_pdi_skips(pdi);
if (!pdi->skips && !pdi->events)
ratio = 0.0;
else
ratio = 100.0 * ((double)pdi->seq_skips /
(double)(pdi->events + pdi->seq_skips));
fprintf(ofp, "Skips: %'lu forward (%'llu - %5.1lf%%)\n",
pdi->skips, pdi->seq_skips, ratio);
}
}
static void find_genesis(void)
{
struct trace *t = trace_list;
genesis_time = -1ULL;
while (t != NULL) {
if (t->bit->time < genesis_time)
genesis_time = t->bit->time;
t = t->next;
}
/* The time stamp record will usually be the first
* record in the trace, but not always.
*/
if (start_timestamp
&& start_timestamp != genesis_time) {
long delta = genesis_time - start_timestamp;
abs_start_time.tv_sec += SECONDS(delta);
abs_start_time.tv_nsec += NANO_SECONDS(delta);
if (abs_start_time.tv_nsec < 0) {
abs_start_time.tv_nsec += 1000000000;
abs_start_time.tv_sec -= 1;
} else
if (abs_start_time.tv_nsec > 1000000000) {
abs_start_time.tv_nsec -= 1000000000;
abs_start_time.tv_sec += 1;
}
}
}
static inline int check_stopwatch(struct blk_io_trace *bit)
{
if (bit->time < stopwatch_end &&
bit->time >= stopwatch_start)
return 0;
return 1;
}
/*
* return youngest entry read
*/
static int sort_entries(unsigned long long *youngest)
{
struct per_dev_info *pdi = NULL;
struct per_cpu_info *pci = NULL;
struct trace *t;
if (!genesis_time)
find_genesis();
*youngest = 0;
while ((t = trace_list) != NULL) {
struct blk_io_trace *bit = t->bit;
trace_list = t->next;
bit->time -= genesis_time;
if (bit->time < *youngest || !*youngest)
*youngest = bit->time;
if (!pdi || pdi->dev != bit->device) {
pdi = get_dev_info(bit->device);
pci = NULL;
}
if (!pci || pci->cpu != bit->cpu)
pci = get_cpu_info(pdi, bit->cpu);
if (bit->sequence < pci->smallest_seq_read)
pci->smallest_seq_read = bit->sequence;
if (check_stopwatch(bit)) {
bit_free(bit);
t_free(t);
continue;
}
if (trace_rb_insert_sort(t))
return -1;
}
return 0;
}
/*
* to continue, we must have traces from all online cpus in the tree
*/
static int check_cpu_map(struct per_dev_info *pdi)
{
unsigned long *cpu_map;
struct rb_node *n;
struct trace *__t;
unsigned int i;
int ret, cpu;
/*
* create a map of the cpus we have traces for
*/
cpu_map = malloc(pdi->cpu_map_max / sizeof(long));
memset(cpu_map, 0, sizeof(*cpu_map));
n = rb_first(&rb_sort_root);
while (n) {
__t = rb_entry(n, struct trace, rb_node);
cpu = __t->bit->cpu;
cpu_map[CPU_IDX(cpu)] |= (1UL << CPU_BIT(cpu));
n = rb_next(n);
}
/*
* we can't continue if pdi->cpu_map has entries set that we don't
* have in the sort rbtree. the opposite is not a problem, though
*/
ret = 0;
for (i = 0; i < pdi->cpu_map_max / CPUS_PER_LONG; i++) {
if (pdi->cpu_map[i] & ~(cpu_map[i])) {
ret = 1;
break;
}
}
free(cpu_map);
return ret;
}
static int check_sequence(struct per_dev_info *pdi, struct trace *t, int force)
{
struct blk_io_trace *bit = t->bit;
unsigned long expected_sequence;
struct per_cpu_info *pci;
struct trace *__t;
pci = get_cpu_info(pdi, bit->cpu);
expected_sequence = pci->last_sequence + 1;
if (!expected_sequence) {
/*
* 1 should be the first entry, just allow it
*/
if (bit->sequence == 1)
return 0;
if (bit->sequence == pci->smallest_seq_read)
return 0;
return check_cpu_map(pdi);
}
if (bit->sequence == expected_sequence)
return 0;
/*
* we may not have seen that sequence yet. if we are not doing
* the final run, break and wait for more entries.
*/
if (expected_sequence < pci->smallest_seq_read) {
__t = trace_rb_find_last(pdi, pci, expected_sequence);
if (!__t)
goto skip;
__put_trace_last(pdi, __t);
return 0;
} else if (!force) {
return 1;
} else {
skip:
if (check_current_skips(pci, bit->sequence))
return 0;
if (expected_sequence < bit->sequence)
insert_skip(pci, expected_sequence, bit->sequence - 1);
return 0;
}
}
static void show_entries_rb(int force)
{
struct per_dev_info *pdi = NULL;
struct per_cpu_info *pci = NULL;
struct blk_io_trace *bit;
struct rb_node *n;
struct trace *t;
while ((n = rb_first(&rb_sort_root)) != NULL) {
if (is_done() && !force && !pipeline)
break;
t = rb_entry(n, struct trace, rb_node);
bit = t->bit;
if (read_sequence - t->read_sequence < 1 && !force)
break;
if (!pdi || pdi->dev != bit->device) {
pdi = get_dev_info(bit->device);
pci = NULL;
}
if (!pdi) {
fprintf(stderr, "Unknown device ID? (%d,%d)\n",
MAJOR(bit->device), MINOR(bit->device));
break;
}
if (!(bit->action == BLK_TN_MESSAGE) &&
check_sequence(pdi, t, force))
break;
if (!force && bit->time > last_allowed_time)
break;
check_time(pdi, bit);
if (!pci || pci->cpu != bit->cpu)
pci = get_cpu_info(pdi, bit->cpu);
if (!(bit->action == BLK_TN_MESSAGE))
pci->last_sequence = bit->sequence;
pci->nelems++;
if (bit->action & (act_mask << BLK_TC_SHIFT))
dump_trace(bit, pci, pdi);
put_trace(pdi, t);
}
}
static int read_data(int fd, void *buffer, int bytes, int block, int *fdblock)
{
int ret, bytes_left, fl;
void *p;
if (block != *fdblock) {
fl = fcntl(fd, F_GETFL);
if (!block) {
*fdblock = 0;
fcntl(fd, F_SETFL, fl | O_NONBLOCK);
} else {
*fdblock = 1;
fcntl(fd, F_SETFL, fl & ~O_NONBLOCK);
}
}
bytes_left = bytes;
p = buffer;
while (bytes_left > 0) {
ret = read(fd, p, bytes_left);
if (!ret)
return 1;
else if (ret < 0) {
if (errno != EAGAIN) {
perror("read");
return -1;
}
/*
* never do partial reads. we can return if we
* didn't read anything and we should not block,
* otherwise wait for data
*/
if ((bytes_left == bytes) && !block)
return 1;
usleep(10);
continue;
} else {
p += ret;
bytes_left -= ret;
}
}
return 0;
}
static inline __u16 get_pdulen(struct blk_io_trace *bit)
{
if (data_is_native)
return bit->pdu_len;
return __bswap_16(bit->pdu_len);
}
static inline __u32 get_magic(struct blk_io_trace *bit)
{
if (data_is_native)
return bit->magic;
return __bswap_32(bit->magic);
}
static int read_events(int fd, int always_block, int *fdblock)
{
struct per_dev_info *pdi = NULL;
unsigned int events = 0;
while (!is_done() && events < rb_batch) {
struct blk_io_trace *bit;
struct trace *t;
int pdu_len, should_block, ret;
__u32 magic;
bit = bit_alloc();
should_block = !events || always_block;
ret = read_data(fd, bit, sizeof(*bit), should_block, fdblock);
if (ret) {
bit_free(bit);
if (!events && ret < 0)
events = ret;
break;
}
/*
* look at first trace to check whether we need to convert
* data in the future
*/
if (data_is_native == -1 && check_data_endianness(bit->magic))
break;
magic = get_magic(bit);
if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
fprintf(stderr, "Bad magic %x\n", magic);
break;
}
pdu_len = get_pdulen(bit);
if (pdu_len) {
void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
if (read_data(fd, ptr + sizeof(*bit), pdu_len, 1, fdblock)) {
bit_free(ptr);
break;
}
bit = ptr;
}
trace_to_cpu(bit);
if (verify_trace(bit)) {
bit_free(bit);
continue;
}
/*
* not a real trace, so grab and handle it here
*/
if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
handle_notify(bit);
output_binary(bit, sizeof(*bit) + bit->pdu_len);
continue;
}
t = t_alloc();
memset(t, 0, sizeof(*t));
t->bit = bit;
t->read_sequence = read_sequence;
t->next = trace_list;
trace_list = t;
if (!pdi || pdi->dev != bit->device)
pdi = get_dev_info(bit->device);
if (bit->time > pdi->last_read_time)
pdi->last_read_time = bit->time;
events++;
}
return events;
}
/*
* Managing input streams
*/
struct ms_stream {
struct ms_stream *next;
struct trace *first, *last;
struct per_dev_info *pdi;
unsigned int cpu;
};
#define MS_HASH(d, c) ((MAJOR(d) & 0xff) ^ (MINOR(d) & 0xff) ^ (cpu & 0xff))
struct ms_stream *ms_head;
struct ms_stream *ms_hash[256];
static void ms_sort(struct ms_stream *msp);
static int ms_prime(struct ms_stream *msp);
static inline struct trace *ms_peek(struct ms_stream *msp)
{
return (msp == NULL) ? NULL : msp->first;
}
static inline __u64 ms_peek_time(struct ms_stream *msp)
{
return ms_peek(msp)->bit->time;
}
static inline void ms_resort(struct ms_stream *msp)
{
if (msp->next && ms_peek_time(msp) > ms_peek_time(msp->next)) {
ms_head = msp->next;
msp->next = NULL;
ms_sort(msp);
}
}
static inline void ms_deq(struct ms_stream *msp)
{
msp->first = msp->first->next;
if (!msp->first) {
msp->last = NULL;
if (!ms_prime(msp)) {
ms_head = msp->next;
msp->next = NULL;
return;
}
}
ms_resort(msp);
}
static void ms_sort(struct ms_stream *msp)
{
__u64 msp_t = ms_peek_time(msp);
struct ms_stream *this_msp = ms_head;
if (this_msp == NULL)
ms_head = msp;
else if (msp_t < ms_peek_time(this_msp)) {
msp->next = this_msp;
ms_head = msp;
}
else {
while (this_msp->next && ms_peek_time(this_msp->next) < msp_t)
this_msp = this_msp->next;
msp->next = this_msp->next;
this_msp->next = msp;
}
}
static int ms_prime(struct ms_stream *msp)
{
__u32 magic;
unsigned int i;
struct trace *t;
struct per_dev_info *pdi = msp->pdi;
struct per_cpu_info *pci = get_cpu_info(pdi, msp->cpu);
struct blk_io_trace *bit = NULL;
int ret, pdu_len, ndone = 0;
for (i = 0; !is_done() && pci->fd >= 0 && i < rb_batch; i++) {
bit = bit_alloc();
ret = read_data(pci->fd, bit, sizeof(*bit), 1, &pci->fdblock);
if (ret)
goto err;
if (data_is_native == -1 && check_data_endianness(bit->magic))
goto err;
magic = get_magic(bit);
if ((magic & 0xffffff00) != BLK_IO_TRACE_MAGIC) {
fprintf(stderr, "Bad magic %x\n", magic);
goto err;
}
pdu_len = get_pdulen(bit);
if (pdu_len) {
void *ptr = realloc(bit, sizeof(*bit) + pdu_len);
ret = read_data(pci->fd, ptr + sizeof(*bit), pdu_len,
1, &pci->fdblock);
if (ret) {
free(ptr);
bit = NULL;
goto err;
}
bit = ptr;
}
trace_to_cpu(bit);
if (verify_trace(bit))
goto err;
if (bit->cpu != pci->cpu) {
fprintf(stderr, "cpu %d trace info has error cpu %d\n",
pci->cpu, bit->cpu);
continue;
}
if (bit->action & BLK_TC_ACT(BLK_TC_NOTIFY) && bit->action != BLK_TN_MESSAGE) {
handle_notify(bit);
output_binary(bit, sizeof(*bit) + bit->pdu_len);
bit_free(bit);
i -= 1;
continue;
}
if (bit->time > pdi->last_read_time)
pdi->last_read_time = bit->time;
t = t_alloc();
memset(t, 0, sizeof(*t));
t->bit = bit;
if (msp->first == NULL)
msp->first = msp->last = t;
else {
msp->last->next = t;
msp->last = t;
}
ndone++;
}
return ndone;
err:
if (bit) bit_free(bit);
cpu_mark_offline(pdi, pci->cpu);
close(pci->fd);
pci->fd = -1;
return ndone;
}
static struct ms_stream *ms_alloc(struct per_dev_info *pdi, int cpu)
{
struct ms_stream *msp = malloc(sizeof(*msp));
msp->next = NULL;
msp->first = msp->last = NULL;
msp->pdi = pdi;
msp->cpu = cpu;
if (ms_prime(msp))
ms_sort(msp);
return msp;
}
static int setup_file(struct per_dev_info *pdi, int cpu)
{
int len = 0;
struct stat st;
char *p, *dname;
struct per_cpu_info *pci = get_cpu_info(pdi, cpu);
pci->cpu = cpu;
pci->fdblock = -1;
p = strdup(pdi->name);
dname = dirname(p);
if (strcmp(dname, ".")) {
input_dir = dname;
p = strdup(pdi->name);
strcpy(pdi->name, basename(p));
}
free(p);
if (input_dir)
len = sprintf(pci->fname, "%s/", input_dir);
snprintf(pci->fname + len, sizeof(pci->fname)-1-len,
"%s.blktrace.%d", pdi->name, pci->cpu);
if (stat(pci->fname, &st) < 0)
return 0;
if (!st.st_size)
return 1;
pci->fd = open(pci->fname, O_RDONLY);
if (pci->fd < 0) {
perror(pci->fname);
return 0;
}
printf("Input file %s added\n", pci->fname);
cpu_mark_online(pdi, pci->cpu);
pdi->nfiles++;
ms_alloc(pdi, pci->cpu);
return 1;
}
static int handle(struct ms_stream *msp)
{
struct trace *t;
struct per_dev_info *pdi;
struct per_cpu_info *pci;
struct blk_io_trace *bit;
t = ms_peek(msp);
bit = t->bit;
pdi = msp->pdi;
pci = get_cpu_info(pdi, msp->cpu);
pci->nelems++;
bit->time -= genesis_time;
if (t->bit->time > stopwatch_end)
return 0;
pdi->last_reported_time = bit->time;
if ((bit->action & (act_mask << BLK_TC_SHIFT))&&
t->bit->time >= stopwatch_start)
dump_trace(bit, pci, pdi);
ms_deq(msp);
if (text_output)
trace_rb_insert_last(pdi, t);
else {
bit_free(t->bit);
t_free(t);
}
return 1;
}
/*
* Check if we need to sanitize the name. We allow 'foo', or if foo.blktrace.X
* is given, then strip back down to 'foo' to avoid missing files.
*/
static int name_fixup(char *name)
{
char *b;
if (!name)
return 1;
b = strstr(name, ".blktrace.");
if (b)
*b = '\0';
return 0;
}
static int do_file(void)
{
int i, cpu, ret;
struct per_dev_info *pdi;
/*
* first prepare all files for reading
*/
for (i = 0; i < ndevices; i++) {
pdi = &devices[i];
ret = name_fixup(pdi->name);
if (ret)
return ret;
for (cpu = 0; setup_file(pdi, cpu); cpu++)
;
if (!cpu) {
fprintf(stderr,"No input files found for %s\n",
pdi->name);
return 1;
}
}
/*
* Get the initial time stamp
*/
if (ms_head)
genesis_time = ms_peek_time(ms_head);
/*
* Keep processing traces while any are left
*/
while (!is_done() && ms_head && handle(ms_head))
;
return 0;
}
static void do_pipe(int fd)
{
unsigned long long youngest;
int events, fdblock;
last_allowed_time = -1ULL;
fdblock = -1;
while ((events = read_events(fd, 0, &fdblock)) > 0) {
read_sequence++;
#if 0
smallest_seq_read = -1U;
#endif
if (sort_entries(&youngest))
break;
if (youngest > stopwatch_end)
break;
show_entries_rb(0);
}
if (rb_sort_entries)
show_entries_rb(1);
}
static int do_fifo(void)
{
int fd;
if (!strcmp(pipename, "-"))
fd = dup(STDIN_FILENO);
else
fd = open(pipename, O_RDONLY);
if (fd == -1) {
perror("dup stdin");
return -1;
}
do_pipe(fd);
close(fd);
return 0;
}
static void show_stats(void)
{
if (!ofp)
return;
if (stats_printed)
return;
stats_printed = 1;
if (per_process_stats)
show_process_stats();
if (per_device_and_cpu_stats)
show_device_and_cpu_stats();
fflush(ofp);
}
static void handle_sigint(__attribute__((__unused__)) int sig)
{
done = 1;
}
/*
* Extract start and duration times from a string, allowing
* us to specify a time interval of interest within a trace.
* Format: "duration" (start is zero) or "start:duration".
*/
static int find_stopwatch_interval(char *string)
{
double value;
char *sp;
value = strtod(string, &sp);
if (sp == string) {
fprintf(stderr,"Invalid stopwatch timer: %s\n", string);
return 1;
}
if (*sp == ':') {
stopwatch_start = DOUBLE_TO_NANO_ULL(value);
string = sp + 1;
value = strtod(string, &sp);
if (sp == string || *sp != '\0') {
fprintf(stderr,"Invalid stopwatch duration time: %s\n",
string);
return 1;
}
} else if (*sp != '\0') {
fprintf(stderr,"Invalid stopwatch start timer: %s\n", string);
return 1;
}
stopwatch_end = DOUBLE_TO_NANO_ULL(value);
if (stopwatch_end <= stopwatch_start) {
fprintf(stderr, "Invalid stopwatch interval: %Lu -> %Lu\n",
stopwatch_start, stopwatch_end);
return 1;
}
return 0;
}
static int is_pipe(const char *str)
{
struct stat st;
if (!strcmp(str, "-"))
return 1;
if (!stat(str, &st) && S_ISFIFO(st.st_mode))
return 1;
return 0;
}
#define S_OPTS "a:A:b:D:d:f:F:hi:o:Oqstw:vVM"
static char usage_str[] = "\n\n" \
"-i <file> | --input=<file>\n" \
"[ -a <action field> | --act-mask=<action field> ]\n" \
"[ -A <action mask> | --set-mask=<action mask> ]\n" \
"[ -b <traces> | --batch=<traces> ]\n" \
"[ -d <file> | --dump-binary=<file> ]\n" \
"[ -D <dir> | --input-directory=<dir> ]\n" \
"[ -f <format> | --format=<format> ]\n" \
"[ -F <spec> | --format-spec=<spec> ]\n" \
"[ -h | --hash-by-name ]\n" \
"[ -o <file> | --output=<file> ]\n" \
"[ -O | --no-text-output ]\n" \
"[ -q | --quiet ]\n" \
"[ -s | --per-program-stats ]\n" \
"[ -t | --track-ios ]\n" \
"[ -w <time> | --stopwatch=<time> ]\n" \
"[ -M | --no-msgs\n" \
"[ -v | --verbose ]\n" \
"[ -V | --version ]\n\n" \
"\t-a Only trace specified actions. See documentation\n" \
"\t-A Give trace mask as a single value. See documentation\n" \
"\t-b stdin read batching\n" \
"\t-d Output file. If specified, binary data is written to file\n" \
"\t-D Directory to prepend to input file names\n" \
"\t-f Output format. Customize the output format. The format field\n" \
"\t identifies can be found in the documentation\n" \
"\t-F Format specification. Can be found in the documentation\n" \
"\t-h Hash processes by name, not pid\n" \
"\t-i Input file containing trace data, or '-' for stdin\n" \
"\t-o Output file. If not given, output is stdout\n" \
"\t-O Do NOT output text data\n" \
"\t-q Quiet. Don't display any stats at the end of the trace\n" \
"\t-s Show per-program io statistics\n" \
"\t-t Track individual ios. Will tell you the time a request took\n" \
"\t to get queued, to get dispatched, and to get completed\n" \
"\t-w Only parse data between the given time interval in seconds.\n" \
"\t If 'start' isn't given, blkparse defaults the start time to 0\n" \
"\t-M Do not output messages to binary file\n" \
"\t-v More verbose for marginal errors\n" \
"\t-V Print program version info\n\n";
static void usage(char *prog)
{
fprintf(stderr, "Usage: %s %s", prog, usage_str);
}
int main(int argc, char *argv[])
{
int i, c, ret, mode;
int act_mask_tmp = 0;
char *ofp_buffer = NULL;
char *bin_ofp_buffer = NULL;
while ((c = getopt_long(argc, argv, S_OPTS, l_opts, NULL)) != -1) {
switch (c) {
case 'a':
i = find_mask_map(optarg);
if (i < 0) {
fprintf(stderr,"Invalid action mask %s\n",
optarg);
return 1;
}
act_mask_tmp |= i;
break;
case 'A':
if ((sscanf(optarg, "%x", &i) != 1) ||
!valid_act_opt(i)) {
fprintf(stderr,
"Invalid set action mask %s/0x%x\n",
optarg, i);
return 1;
}
act_mask_tmp = i;
break;
case 'i':
if (is_pipe(optarg) && !pipeline) {
pipeline = 1;
pipename = strdup(optarg);
} else if (resize_devices(optarg) != 0)
return 1;
break;
case 'D':
input_dir = optarg;
break;
case 'o':
output_name = optarg;
break;
case 'O':
text_output = 0;
break;
case 'b':
rb_batch = atoi(optarg);
if (rb_batch <= 0)
rb_batch = RB_BATCH_DEFAULT;
break;
case 's':
per_process_stats = 1;
break;
case 't':
track_ios = 1;
break;
case 'q':
per_device_and_cpu_stats = 0;
break;
case 'w':
if (find_stopwatch_interval(optarg) != 0)
return 1;
break;
case 'f':
set_all_format_specs(optarg);
break;
case 'F':
if (add_format_spec(optarg) != 0)
return 1;
break;
case 'h':
ppi_hash_by_pid = 0;
break;
case 'v':
verbose++;
break;
case 'V':
printf("%s version %s\n", argv[0], blkparse_version);
return 0;
case 'd':
dump_binary = optarg;
break;
case 'M':
bin_output_msgs = 0;
break;
default:
usage(argv[0]);
return 1;
}
}
while (optind < argc) {
if (is_pipe(argv[optind]) && !pipeline) {
pipeline = 1;
pipename = strdup(argv[optind]);
} else if (resize_devices(argv[optind]) != 0)
return 1;
optind++;
}
if (!pipeline && !ndevices) {
usage(argv[0]);
return 1;
}
if (act_mask_tmp != 0)
act_mask = act_mask_tmp;
memset(&rb_sort_root, 0, sizeof(rb_sort_root));
signal(SIGINT, handle_sigint);
signal(SIGHUP, handle_sigint);
signal(SIGTERM, handle_sigint);
setlocale(LC_NUMERIC, "en_US");
if (text_output) {
if (!output_name) {
ofp = fdopen(STDOUT_FILENO, "w");
mode = _IOLBF;
} else {
char ofname[PATH_MAX];
snprintf(ofname, sizeof(ofname) - 1, "%s", output_name);
ofp = fopen(ofname, "w");
mode = _IOFBF;
}
if (!ofp) {
perror("fopen");
return 1;
}
ofp_buffer = malloc(4096);
if (setvbuf(ofp, ofp_buffer, mode, 4096)) {
perror("setvbuf");
return 1;
}
}
if (dump_binary) {
if (!strcmp(dump_binary, "-"))
dump_fp = stdout;
else {
dump_fp = fopen(dump_binary, "w");
if (!dump_fp) {
perror(dump_binary);
dump_binary = NULL;
return 1;
}
}
bin_ofp_buffer = malloc(128 * 1024);
if (setvbuf(dump_fp, bin_ofp_buffer, _IOFBF, 128 * 1024)) {
perror("setvbuf binary");
return 1;
}
}
if (pipeline)
ret = do_fifo();
else
ret = do_file();
if (!ret)
show_stats();
if (have_drv_data && !dump_binary)
printf("\ndiscarded traces containing low-level device driver "
"specific data (only available in binary output)\n");
if (ofp_buffer) {
fflush(ofp);
free(ofp_buffer);
}
if (bin_ofp_buffer) {
fflush(dump_fp);
free(bin_ofp_buffer);
}
return ret;
}
|