1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475
|
/*
* Copyright (C) 2014-2021 Canonical, Ltd.
* Copyright (C) 2022-2025 Colin Ian King.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "stress-ng.h"
#include "git-commit-id.h"
#include "core-bitops.h"
#include "core-builtin.h"
#include "core-attribute.h"
#include "core-capabilities.h"
#include "core-cpu-cache.h"
#include "core-hash.h"
#include "core-lock.h"
#include "core-numa.h"
#include "core-pthread.h"
#include "core-pragma.h"
#include "core-sort.h"
#include "core-target-clones.h"
#include <ctype.h>
#include <math.h>
#include <sched.h>
#include <stdarg.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <time.h>
#if defined(HAVE_EXECINFO_H)
#include <execinfo.h>
#endif
#if defined(HAVE_LINUX_FIEMAP_H)
#include <linux/fiemap.h>
#endif
#if defined(HAVE_SYS_AUXV_H)
#include <sys/auxv.h>
#endif
#if defined(HAVE_SYS_CAPABILITY_H)
#include <sys/capability.h>
#endif
#if defined(HAVE_SYS_LOADAVG_H)
#include <sys/loadavg.h>
#endif
#if defined(HAVE_MACH_MACH_H)
#include <mach/mach.h>
#endif
#if defined(HAVE_MACH_VM_STATISTICS_H)
#include <mach/vm_statistics.h>
#endif
#if (defined(__FreeBSD__) || \
defined(__OpenBSD__)) && \
defined(HAVE_SYS_MOUNT_H)
#include <sys/mount.h>
#endif
#if defined(__FreeBSD__) && \
defined(HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#if defined(HAVE_SYS_PRCTL_H)
#include <sys/prctl.h>
#endif
#if defined(HAVE_SYS_PROCCTL_H)
#include <sys/procctl.h>
#endif
#if defined(HAVE_SYS_STATVFS_H)
#include <sys/statvfs.h>
#endif
#if defined(HAVE_SYS_SWAP_H) && \
!defined(__sun__)
#include <sys/swap.h>
#endif
#if defined(HAVE_SYS_SYSCTL_H) && \
!defined(__linux__)
#include <sys/sysctl.h>
#endif
#if defined(HAVE_SYS_SYSMACROS_H)
#include <sys/sysmacros.h>
#endif
#if defined(HAVE_SYS_UTSNAME_H)
#include <sys/utsname.h>
#endif
#if defined(HAVE_LINUX_FS_H)
#include <linux/fs.h>
#endif
#if defined(HAVE_SYS_VFS_H)
#include <sys/vfs.h>
#endif
#if defined(HAVE_LINUX_MAGIC_H)
#include <linux/magic.h>
#endif
#if defined(HAVE_UVM_UVM_EXTERN_H)
#include <uvm/uvm_extern.h>
#endif
/* prctl(2) timer slack support */
#if defined(HAVE_SYS_PRCTL_H) && \
defined(HAVE_PRCTL) && \
defined(PR_SET_TIMERSLACK) && \
defined(PR_GET_TIMERSLACK)
#define HAVE_PRCTL_TIMER_SLACK
#endif
#if defined(NSIG)
#define STRESS_NSIG NSIG
#elif defined(_NSIG)
#define STRESS_NSIG _NSIG
#endif
#if defined(HAVE_COMPILER_TCC) || defined(HAVE_COMPILER_PCC)
int __dso_handle;
#endif
#define MEM_CACHE_SIZE (2 * MB)
#define PAGE_4K_SHIFT (12)
#define PAGE_4K (1 << PAGE_4K_SHIFT)
#define BACKTRACE_BUF_SIZE (64)
#define STRESS_ABS_MIN_STACK_SIZE (64 * 1024)
const char ALIGN64 NONSTRING stress_ascii64[64] =
"0123456789ABCDEFGHIJKLMNOPQRSTUV"
"WXYZabcdefghijklmnopqrstuvwxyz@!";
const char ALIGN64 NONSTRING stress_ascii32[32] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ_+@:#!";
static bool stress_stack_check_flag;
typedef struct {
const unsigned long int fs_magic;
const char * fs_name;
} stress_fs_name_t;
#if defined(HAVE_LINUX_MAGIC_H) && \
defined(HAVE_SYS_STATFS_H)
static const stress_fs_name_t stress_fs_names[] = {
#if defined(ADFS_SUPER_MAGIC)
{ ADFS_SUPER_MAGIC, "adfs" },
#endif
#if defined(AFFS_SUPER_MAGIC)
{ AFFS_SUPER_MAGIC, "affs" },
#endif
#if defined(AFS_SUPER_MAGIC)
{ AFS_SUPER_MAGIC, "afs" },
#endif
#if defined(AUTOFS_SUPER_MAGIC)
{ AUTOFS_SUPER_MAGIC, "autofs" },
#endif
#if defined(CEPH_SUPER_MAGIC)
{ CEPH_SUPER_MAGIC, "ceph" },
#endif
#if defined(CODA_SUPER_MAGIC)
{ CODA_SUPER_MAGIC, "coda" },
#endif
#if defined(CRAMFS_MAGIC)
{ CRAMFS_MAGIC, "cramfs" },
#endif
#if defined(CRAMFS_MAGIC_WEND)
{ CRAMFS_MAGIC, "cramfs" },
#endif
#if defined(DEBUGFS_MAGIC)
{ DEBUGFS_MAGIC, "debugfs" },
#endif
#if defined(SECURITYFS_MAGIC)
{ SECURITYFS_MAGIC, "securityfs" },
#endif
#if defined(SELINUX_MAGIC)
{ SELINUX_MAGIC, "selinux" },
#endif
#if defined(SMACK_MAGIC)
{ SMACK_MAGIC, "smack" },
#endif
#if defined(RAMFS_MAGIC)
{ RAMFS_MAGIC, "ramfs" },
#endif
#if defined(TMPFS_MAGIC)
{ TMPFS_MAGIC, "tmpfs" },
#endif
#if defined(HUGETLBFS_MAGIC)
{ HUGETLBFS_MAGIC, "hugetlbfs" },
#endif
#if defined(SQUASHFS_MAGIC)
{ SQUASHFS_MAGIC, "squashfs" },
#endif
#if defined(ECRYPTFS_SUPER_MAGIC)
{ ECRYPTFS_SUPER_MAGIC, "ecryptfs" },
#endif
#if defined(EFS_SUPER_MAGIC)
{ EFS_SUPER_MAGIC, "efs" },
#endif
#if defined(EROFS_SUPER_MAGIC_V1)
{ EROFS_SUPER_MAGIC_V1, "erofs" },
#endif
#if defined(EXT2_SUPER_MAGIC)
{ EXT2_SUPER_MAGIC, "ext2" },
#endif
#if defined(EXT3_SUPER_MAGIC)
{ EXT3_SUPER_MAGIC, "ext3" },
#endif
#if defined(XENFS_SUPER_MAGIC)
{ XENFS_SUPER_MAGIC, "xenfs" },
#endif
#if defined(EXT4_SUPER_MAGIC)
{ EXT4_SUPER_MAGIC, "ext4" },
#endif
#if defined(BTRFS_SUPER_MAGIC)
{ BTRFS_SUPER_MAGIC, "btrfs" },
#endif
#if defined(NILFS_SUPER_MAGIC)
{ NILFS_SUPER_MAGIC, "nilfs" },
#endif
#if defined(F2FS_SUPER_MAGIC)
{ F2FS_SUPER_MAGIC, "f2fs" },
#endif
#if defined(HPFS_SUPER_MAGIC)
{ HPFS_SUPER_MAGIC, "hpfs" },
#endif
#if defined(ISOFS_SUPER_MAGIC)
{ ISOFS_SUPER_MAGIC, "isofs" },
#endif
#if defined(JFFS2_SUPER_MAGIC)
{ JFFS2_SUPER_MAGIC, "jffs2" },
#endif
#if defined(XFS_SUPER_MAGIC)
{ XFS_SUPER_MAGIC, "xfs" },
#endif
#if defined(PSTOREFS_MAGIC)
{ PSTOREFS_MAGIC, "pstorefs" },
#endif
#if defined(EFIVARFS_MAGIC)
{ EFIVARFS_MAGIC, "efivars" },
#endif
#if defined(HOSTFS_SUPER_MAGIC)
{ HOSTFS_SUPER_MAGIC, "hostfs" },
#endif
#if defined(OVERLAYFS_SUPER_MAGIC)
{ OVERLAYFS_SUPER_MAGIC, "overlayfs" },
#endif
#if defined(FUSE_SUPER_MAGIC)
{ FUSE_SUPER_MAGIC, "fuse" },
#endif
#if defined(BCACHEFS_STATFS_MAGIC)
{ BCACHEFS_STATFS_MAGIC, "bcachefs" },
#else
{ 0xca451a4e, "bacachefs" },
#endif
#if defined(MINIX_SUPER_MAGIC)
{ MINIX_SUPER_MAGIC, "minix" },
#endif
#if defined(MINIX_SUPER_MAGIC2)
{ MINIX_SUPER_MAGIC2, "minix" },
#endif
#if defined(MINIX2_SUPER_MAGIC)
{ MINIX2_SUPER_MAGIC, "minix2" },
#endif
#if defined(MINIX3_SUPER_MAGIC)
{ MINIX3_SUPER_MAGIC, "minix3" },
#endif
#if defined(MSDOS_SUPER_MAGIC)
{ MSDOS_SUPER_MAGIC, "msdos" },
#endif
#if defined(EXFAT_SUPER_MAGIC)
{ EXFAT_SUPER_MAGIC, "exfat" },
#endif
#if defined(NCP_SUPER_MAGIC)
{ NCP_SUPER_MAGIC, "ncp" },
#endif
#if defined(NFS_SUPER_MAGIC)
{ NFS_SUPER_MAGIC, "nfs" },
#endif
#if defined(OCFS2_SUPER_MAGIC)
{ OCFS2_SUPER_MAGIC, "ocfs2" },
#endif
#if defined(OPENPROM_SUPER_MAGIC)
{ OPENPROM_SUPER_MAGIC, "openprom" },
#endif
#if defined(QNX4_SUPER_MAGIC)
{ QNX4_SUPER_MAGIC, "qnx4" },
#endif
#if defined(QNX6_SUPER_MAGIC)
{ QNX6_SUPER_MAGIC, "qnx6" },
#endif
#if defined(AFS_FS_MAGIC)
{ AFS_FS_MAGIC, "afs" },
#endif
#if defined(REISERFS_SUPER_MAGIC)
{ REISERFS_SUPER_MAGIC, "reiserfs" },
#endif
#if defined(SMB_SUPER_MAGIC)
{ SMB_SUPER_MAGIC, "smb" },
#endif
#if defined(CIFS_SUPER_MAGIC)
{ CIFS_SUPER_MAGIC, "cifs" },
#endif
#if defined(SMB2_SUPER_MAGIC)
{ SMB2_SUPER_MAGIC, "smb2" },
#endif
#if defined(CGROUP_SUPER_MAGIC)
{ CGROUP_SUPER_MAGIC, "cgroup" },
#endif
#if defined(CGROUP2_SUPER_MAGIC)
{ CGROUP2_SUPER_MAGIC, "cgroup2" },
#endif
#if defined(RDTGROUP_SUPER_MAGIC)
{ RDTGROUP_SUPER_MAGIC, "rdtgroup" },
#endif
#if defined(TRACEFS_MAGIC)
{ TRACEFS_MAGIC, "tracefs" },
#endif
#if defined(V9FS_MAGIC)
{ V9FS_MAGIC, "v9fs" },
#endif
#if defined(BDEVFS_MAGIC)
{ BDEVFS_MAGIC, "bdevfs" },
#endif
#if defined(DAXFS_MAGIC)
{ DAXFS_MAGIC, "daxfs" },
#endif
#if defined(BINFMTFS_MAGIC)
{ BINFMTFS_MAGIC, "binfmtfs" },
#endif
#if defined(DEVPTS_SUPER_MAGIC)
{ DEVPTS_SUPER_MAGIC, "devpts" },
#endif
#if defined(BINDERFS_SUPER_MAGIC)
{ BINDERFS_SUPER_MAGIC, "binderfs" },
#endif
#if defined(FUTEXFS_SUPER_MAGIC)
{ FUTEXFS_SUPER_MAGIC, "futexfs" },
#endif
#if defined(PIPEFS_MAGIC)
{ PIPEFS_MAGIC, "pipefs" },
#endif
#if defined(PROC_SUPER_MAGIC)
{ PROC_SUPER_MAGIC, "proc" },
#endif
#if defined(SOCKFS_MAGIC)
{ SOCKFS_MAGIC, "sockfs" },
#endif
#if defined(SYSFS_MAGIC)
{ SYSFS_MAGIC, "sysfs" },
#endif
#if defined(USBDEVICE_SUPER_MAGIC)
{ USBDEVICE_SUPER_MAGIC, "usbdev" },
#endif
#if defined(MTD_INODE_FS_MAGIC)
{ MTD_INODE_FS_MAGIC, "mtd" },
#endif
#if defined(ANON_INODE_FS_MAGIC)
{ ANON_INODE_FS_MAGIC, "anon" },
#endif
#if defined(BTRFS_TEST_MAGIC)
{ BTRFS_TEST_MAGIC, "btrfs" },
#endif
#if defined(NSFS_MAGIC)
{ NSFS_MAGIC, "nsfs" },
#endif
#if defined(BPF_FS_MAGIC)
{ BPF_FS_MAGIC, "bpf_fs" },
#endif
#if defined(AAFS_MAGIC)
{ AAFS_MAGIC, "aafs" },
#endif
#if defined(ZONEFS_MAGIC)
{ ZONEFS_MAGIC, "zonefs" },
#endif
#if defined(UDF_SUPER_MAGIC)
{ UDF_SUPER_MAGIC, "udf" },
#endif
#if defined(DMA_BUF_MAGIC)
{ DMA_BUF_MAGIC, "dmabuf" },
#endif
#if defined(DEVMEM_MAGIC)
{ DEVMEM_MAGIC, "devmem" },
#endif
#if defined(SECRETMEM_MAGIC)
{ SECRETMEM_MAGIC, "secretmem" },
#endif
#if defined(PID_FS_MAGIC)
{ PID_FS_MAGIC, "pidfs" },
#endif
#if defined(UBIFS_SUPER_MAGIC)
{ UBIFS_SUPER_MAGIC, "ubifs" },
#else
{ 0x24051905, "ubifs" },
#endif
{ 0x1badface, "bfs" },
#if defined(HFS_SUPER_MAGIC)
{ HFS_SUPER_MAGIC, "hfs" },
#else
{ 0x4244, "hfs" },
#endif
#if defined(HFSPLUS_SUPER_MAGIC)
{ HFSPLUS_SUPER_MAGIC, "hfsplus" },
#else
{ 0x482b, "hfsplus" },
#endif
#if defined(JFS_SUPER_MAGIC)
{ JFS_SUPER_MAGIC, "jfs" },
#else
{ 0x3153464a, "jfs" },
#endif
{ 0x2fc12fc1, "zfs" },
{ 0x53464846, "wsl" },
};
#endif
typedef struct {
const int signum; /* signal number */
const char *name; /* human readable signal name */
} stress_sig_name_t;
#define SIG_NAME(x) { x, #x }
static const stress_sig_name_t sig_names[] = {
#if defined(SIGABRT)
SIG_NAME(SIGABRT),
#endif
#if defined(SIGALRM)
SIG_NAME(SIGALRM),
#endif
#if defined(SIGBUS)
SIG_NAME(SIGBUS),
#endif
#if defined(SIGCHLD)
SIG_NAME(SIGCHLD),
#endif
#if defined(SIGCLD)
SIG_NAME(SIGCLD),
#endif
#if defined(SIGCONT)
SIG_NAME(SIGCONT),
#endif
#if defined(SIGEMT)
SIG_NAME(SIGEMT),
#endif
#if defined(SIGFPE)
SIG_NAME(SIGFPE),
#endif
#if defined(SIGHUP)
SIG_NAME(SIGHUP),
#endif
#if defined(SIGILL)
SIG_NAME(SIGILL),
#endif
#if defined(SIGINFO)
SIG_NAME(SIGINFO),
#endif
#if defined(SIGINT)
SIG_NAME(SIGINT),
#endif
#if defined(SIGIO)
SIG_NAME(SIGIO),
#endif
#if defined(SIGIOT)
SIG_NAME(SIGIOT),
#endif
#if defined(SIGKILL)
SIG_NAME(SIGKILL),
#endif
#if defined(SIGLOST)
SIG_NAME(SIGLOST),
#endif
#if defined(SIGPIPE)
SIG_NAME(SIGPIPE),
#endif
#if defined(SIGPOLL)
SIG_NAME(SIGPOLL),
#endif
#if defined(SIGPROF)
SIG_NAME(SIGPROF),
#endif
#if defined(SIGPWR)
SIG_NAME(SIGPWR),
#endif
#if defined(SIGQUIT)
SIG_NAME(SIGQUIT),
#endif
#if defined(SIGSEGV)
SIG_NAME(SIGSEGV),
#endif
#if defined(SIGSTKFLT)
SIG_NAME(SIGSTKFLT),
#endif
#if defined(SIGSTOP)
SIG_NAME(SIGSTOP),
#endif
#if defined(SIGSYS)
SIG_NAME(SIGSYS),
#endif
#if defined(SIGTERM)
SIG_NAME(SIGTERM),
#endif
#if defined(SIGTRAP)
SIG_NAME(SIGTRAP),
#endif
#if defined(SIGTSTP)
SIG_NAME(SIGTSTP),
#endif
#if defined(SIGTTIN)
SIG_NAME(SIGTTIN),
#endif
#if defined(SIGTTOU)
SIG_NAME(SIGTTOU),
#endif
#if defined(SIGUNUSED)
SIG_NAME(SIGUNUSED),
#endif
#if defined(SIGURG)
SIG_NAME(SIGURG),
#endif
#if defined(SIGUSR1)
SIG_NAME(SIGUSR1),
#endif
#if defined(SIGUSR2)
SIG_NAME(SIGUSR2),
#endif
#if defined(SIGVTALRM)
SIG_NAME(SIGVTALRM),
#endif
#if defined(SIGWINCH)
SIG_NAME(SIGWINCH),
#endif
#if defined(SIGXCPU)
SIG_NAME(SIGXCPU),
#endif
#if defined(SIGXFSZ)
SIG_NAME(SIGXFSZ),
#endif
};
static char *stress_temp_path;
/*
* stress_temp_path_free()
* free and NULLify temporary file path
*/
void stress_temp_path_free(void)
{
if (stress_temp_path)
free(stress_temp_path);
stress_temp_path = NULL;
}
/*
* stress_set_temp_path()
* set temporary file path, default
* is . - current dir
*/
int stress_set_temp_path(const char *path)
{
static const char *func = "stress_set_temp_path";
stress_temp_path_free();
if (!path) {
(void)fprintf(stderr, "%s: invalid NULL path\n", func);
return -1;
}
stress_temp_path = stress_const_optdup(path);
if (!stress_temp_path) {
(void)fprintf(stderr, "%s: aborting: cannot allocate memory for '%s'\n", func, path);
return -1;
}
return 0;
}
/*
* stress_get_temp_path()
* get temporary file path, return "." if null
*/
const char *stress_get_temp_path(void)
{
if (!stress_temp_path)
return ".";
return stress_temp_path;
}
/*
* stress_check_temp_path()
* check if temp path is accessible
*/
int stress_check_temp_path(void)
{
const char *path = stress_get_temp_path();
if (UNLIKELY(access(path, R_OK | W_OK) < 0)) {
(void)fprintf(stderr, "aborting: temp-path '%s' must be readable "
"and writeable\n", path);
return -1;
}
return 0;
}
/*
* stress_mk_filename()
* generate a full file name from a path and filename
*/
size_t stress_mk_filename(
char *fullname,
const size_t fullname_len,
const char *pathname,
const char *filename)
{
/*
* This may not be efficient, but it works. Do not
* be tempted to optimize this, it is not used frequently
* and is not a CPU bottleneck.
*/
(void)shim_strscpy(fullname, pathname, fullname_len);
(void)shim_strlcat(fullname, "/", fullname_len);
return shim_strlcat(fullname, filename, fullname_len);
}
/*
* stress_get_page_size()
* get page_size
*/
size_t stress_get_page_size(void)
{
static size_t page_size = 0;
/* Use cached size */
if (LIKELY(page_size > 0))
return page_size;
#if defined(_SC_PAGESIZE)
{
/* Use modern sysconf */
const long int sz = sysconf(_SC_PAGESIZE);
if (sz > 0) {
page_size = (size_t)sz;
return page_size;
}
}
#else
UNEXPECTED
#endif
#if defined(HAVE_GETPAGESIZE)
{
/* Use deprecated getpagesize */
const long int sz = getpagesize();
if (sz > 0) {
page_size = (size_t)sz;
return page_size;
}
}
#endif
/* Guess */
page_size = PAGE_4K;
return page_size;
}
/*
* stress_get_processors_online()
* get number of processors that are online
*/
int32_t stress_get_processors_online(void)
{
static int32_t processors_online = 0;
if (LIKELY(processors_online > 0))
return processors_online;
#if defined(_SC_NPROCESSORS_ONLN)
processors_online = (int32_t)sysconf(_SC_NPROCESSORS_ONLN);
if (UNLIKELY(processors_online < 0))
processors_online = 1;
#else
processors_online = 1;
UNEXPECTED
#endif
return processors_online;
}
/*
* stress_get_processors_configured()
* get number of processors that are configured
*/
int32_t stress_get_processors_configured(void)
{
static int32_t processors_configured = 0;
if (LIKELY(processors_configured > 0))
return processors_configured;
#if defined(_SC_NPROCESSORS_CONF)
processors_configured = (int32_t)sysconf(_SC_NPROCESSORS_CONF);
if (UNLIKELY(processors_configured < 0))
processors_configured = stress_get_processors_online();
#else
processors_configured = 1;
UNEXPECTED
#endif
return processors_configured;
}
/*
* stress_get_ticks_per_second()
* get number of ticks perf second
*/
int32_t stress_get_ticks_per_second(void)
{
#if defined(_SC_CLK_TCK)
static int32_t ticks_per_second = 0;
if (LIKELY(ticks_per_second > 0))
return ticks_per_second;
ticks_per_second = (int32_t)sysconf(_SC_CLK_TCK);
return ticks_per_second;
#else
UNEXPECTED
return -1;
#endif
}
/*
* stress_get_meminfo()
* wrapper for linux sysinfo
*/
static int stress_get_meminfo(
size_t *freemem,
size_t *totalmem,
size_t *freeswap,
size_t *totalswap)
{
if (UNLIKELY(!freemem || !totalmem || !freeswap || !totalswap))
return -1;
#if defined(HAVE_SYS_SYSINFO_H) && \
defined(HAVE_SYSINFO)
{
struct sysinfo info;
(void)shim_memset(&info, 0, sizeof(info));
if (LIKELY(sysinfo(&info) == 0)) {
*freemem = info.freeram * info.mem_unit;
*totalmem = info.totalram * info.mem_unit;
*freeswap = info.freeswap * info.mem_unit;
*totalswap = info.totalswap * info.mem_unit;
return 0;
}
}
#endif
#if defined(__FreeBSD__)
{
const size_t page_size = (size_t)stress_bsd_getsysctl_uint("vm.stats.vm.v_page_size");
#if 0
/*
* Enable total swap only when we can determine free swap
*/
const size_t max_size_t = (size_t)-1;
const uint64_t vm_swap_total = stress_bsd_getsysctl_uint64("vm.swap_total");
*totalswap = (vm_swap_total >= max_size_t) ? max_size_t : (size_t)vm_swap_total;
#endif
*freemem = page_size * stress_bsd_getsysctl_uint32("vm.stats.vm.v_free_count");
*totalmem = page_size *
(stress_bsd_getsysctl_uint32("vm.stats.vm.v_active_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_inactive_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_laundry_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_wire_count") +
stress_bsd_getsysctl_uint32("vm.stats.vm.v_free_count"));
*freeswap = 0;
*totalswap = 0;
return 0;
}
#endif
#if defined(__NetBSD__) && \
defined(HAVE_UVM_UVM_EXTERN_H)
{
struct uvmexp_sysctl u;
if (stress_bsd_getsysctl("vm.uvmexp2", &u, sizeof(u)) == 0) {
*freemem = (size_t)u.free * u.pagesize;
*totalmem = (size_t)u.npages * u.pagesize;
*totalswap = (size_t)u.swpages * u.pagesize;
*freeswap = *totalswap - (size_t)u.swpginuse * u.pagesize;
return 0;
}
}
#endif
#if defined(__APPLE__) && \
defined(HAVE_MACH_MACH_H) && \
defined(HAVE_MACH_VM_STATISTICS_H)
{
vm_statistics64_data_t vm_stat;
mach_port_t host = mach_host_self();
natural_t count = HOST_VM_INFO64_COUNT;
size_t page_size = stress_get_page_size();
int ret;
/* zero vm_stat, keep cppcheck silent */
(void)shim_memset(&vm_stat, 0, sizeof(vm_stat));
ret = host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_stat, &count);
if (ret >= 0) {
*freemem = page_size * vm_stat.free_count;
*totalmem = page_size * (vm_stat.active_count +
vm_stat.inactive_count +
vm_stat.wire_count +
vm_stat.zero_fill_count);
return 0;
}
}
#endif
*freemem = 0;
*totalmem = 0;
*freeswap = 0;
*totalswap = 0;
return -1;
}
/*
* stress_get_memlimits()
* get SHMALL and memory in system
* these are set to zero on failure
*/
void stress_get_memlimits(
size_t *shmall,
size_t *freemem,
size_t *totalmem,
size_t *freeswap,
size_t *totalswap)
{
#if defined(__linux__)
char buf[64];
#endif
if (UNLIKELY(!shmall || !freemem || !totalmem || !freeswap || !totalswap))
return;
(void)stress_get_meminfo(freemem, totalmem, freeswap, totalswap);
#if defined(__linux__)
if (LIKELY(stress_system_read("/proc/sys/kernel/shmall", buf, sizeof(buf)) > 0)) {
if (sscanf(buf, "%zu", shmall) == 1)
return;
}
#endif
*shmall = 0;
}
/*
* stress_get_memfree_str()
* get size of memory that's free in a string, non-reentrant
* note the ' ' space is prefixed before valid strings so the
* output can be used in messages such as:
* pr_fail("out of memory%s\n", stress_uint64_to_str());
*/
char *stress_get_memfree_str(void)
{
size_t freemem = 0, totalmem = 0, freeswap = 0, totalswap = 0;
char freemem_str[32], freeswap_str[32];
static char buf[96];
(void)shim_memset(buf, 0, sizeof(buf));
if (stress_get_meminfo(&freemem, &totalmem, &freeswap, &totalswap) < 0)
return buf;
if ((freemem == 0) && (totalmem == 0) && (freeswap == 0) && (totalswap == 0))
return buf;
(void)stress_uint64_to_str(freemem_str, sizeof(freemem_str), (uint64_t)freemem, 0, true);
(void)stress_uint64_to_str(freeswap_str, sizeof(freeswap_str), (uint64_t)freeswap, 0, true);
(void)snprintf(buf, sizeof(buf), " (%s mem free, %s swap free)", freemem_str, freeswap_str);
return buf;
}
/*
* stress_get_gpu_freq_mhz()
* get GPU frequency in MHz, set to 0.0 if not readable
*/
void stress_get_gpu_freq_mhz(double *gpu_freq)
{
if (UNLIKELY(!gpu_freq))
return;
#if defined(__linux__)
{
char buf[64];
if (stress_system_read("/sys/class/drm/card0/gt_cur_freq_mhz", buf, sizeof(buf)) > 0) {
if (sscanf(buf, "%lf", gpu_freq) == 1)
return;
} else if (stress_system_read("/sys/class/drm/card0/gt_cur_freq_mhz", buf, sizeof(buf)) > 0) {
if (sscanf(buf, "%lf", gpu_freq) == 1)
return;
}
}
#endif
*gpu_freq = 0.0;
}
#if !defined(PR_SET_MEMORY_MERGE)
#define PR_SET_MEMORY_MERGE (67)
#endif
/*
* stress_ksm_memory_merge()
* set kernel samepage merging flag (linux only)
*/
void stress_ksm_memory_merge(const int flag)
{
#if defined(__linux__) && \
defined(PR_SET_MEMORY_MERGE) && \
defined(HAVE_SYS_PRCTL_H)
if ((flag >= 0) && (flag <= 1)) {
static int prev_flag = -1;
if (flag != prev_flag) {
VOID_RET(int, prctl(PR_SET_MEMORY_MERGE, flag));
prev_flag = flag;
}
(void)stress_system_write("/sys/kernel/mm/ksm/run", "1\n", 2);
}
#else
(void)flag;
#endif
}
/*
* stress_low_memory()
* return true if running low on memory
*/
bool stress_low_memory(const size_t requested)
{
static size_t prev_freemem = 0;
static size_t prev_freeswap = 0;
size_t freemem, totalmem, freeswap, totalswap;
static double threshold = -1.0;
bool low_memory = false;
if (stress_get_meminfo(&freemem, &totalmem, &freeswap, &totalswap) == 0) {
/*
* Threshold not set, then get
*/
if (threshold < 0.0) {
size_t bytes = 0;
if (stress_get_setting("oom-avoid-bytes", &bytes)) {
threshold = 100.0 * (double)bytes / (double)freemem;
} else {
/* Not specified, then default to 2.5% */
threshold = 2.5;
}
}
/*
* Stats from previous call valid, then check for memory
* changes
*/
if ((prev_freemem + prev_freeswap) > 0) {
ssize_t delta;
delta = (ssize_t)prev_freemem - (ssize_t)freemem;
delta = (delta * 2) + requested;
/* memory shrinking quickly? */
if (delta > (ssize_t)freemem) {
low_memory = true;
goto update;
}
/* swap shrinking quickly? */
delta = (ssize_t)prev_freeswap - (ssize_t)freeswap;
if (delta > (ssize_t)freeswap / 8) {
low_memory = true;
goto update;
}
}
/* Not enough for allocation and slop? */
if (freemem < ((4 * MB) + requested)) {
low_memory = true;
goto update;
}
/* Less than threshold left? */
if (((double)(freemem - requested) * 100.0 / (double)totalmem) < threshold) {
low_memory = true;
goto update;
}
/* Any swap enabled with free memory we are too low? */
if ((totalswap > 0) && (freeswap + freemem < (requested + (2 * MB)))) {
low_memory = true;
goto update;
}
update:
prev_freemem = freemem;
prev_freeswap = freeswap;
/* low memory? automatically enable ksm memory merging */
if (low_memory)
stress_ksm_memory_merge(1);
}
return low_memory;
}
#if defined(_SC_AVPHYS_PAGES)
#define STRESS_SC_PAGES _SC_AVPHYS_PAGES
#elif defined(_SC_PHYS_PAGES)
#define STRESS_SC_PAGES _SC_PHYS_PAGES
#endif
/*
* stress_get_phys_mem_size()
* get size of physical memory still available, 0 if failed
*/
uint64_t stress_get_phys_mem_size(void)
{
#if defined(STRESS_SC_PAGES)
uint64_t phys_pages;
const size_t page_size = stress_get_page_size();
const uint64_t max_pages = ~0ULL / page_size;
long int ret;
errno = 0;
ret = sysconf(STRESS_SC_PAGES);
if (UNLIKELY((ret < 0) && (errno != 0)))
return 0ULL;
phys_pages = (uint64_t)ret;
/* Avoid overflow */
if (UNLIKELY(phys_pages > max_pages))
phys_pages = max_pages;
return phys_pages * page_size;
#else
UNEXPECTED
return 0ULL;
#endif
}
/*
* stress_get_filesystem_size()
* get size of free space still available on the
* file system where stress temporary path is located,
* return 0 if failed
*/
uint64_t stress_get_filesystem_size(void)
{
#if defined(HAVE_SYS_STATVFS_H)
int rc;
struct statvfs buf;
fsblkcnt_t blocks, max_blocks;
const char *path = stress_get_temp_path();
if (UNLIKELY(!path))
return 0;
(void)shim_memset(&buf, 0, sizeof(buf));
rc = statvfs(path, &buf);
if (UNLIKELY(rc < 0))
return 0;
max_blocks = (~(fsblkcnt_t)0) / buf.f_bsize;
blocks = buf.f_bavail;
if (blocks > max_blocks)
blocks = max_blocks;
return (uint64_t)buf.f_bsize * blocks;
#else
UNEXPECTED
return 0ULL;
#endif
}
/*
* stress_get_filesystem_available_inodes()
* get number of free available inodes on the current stress
* temporary path, return 0 if failed
*/
uint64_t stress_get_filesystem_available_inodes(void)
{
#if defined(HAVE_SYS_STATVFS_H)
int rc;
struct statvfs buf;
const char *path = stress_get_temp_path();
if (UNLIKELY(!path))
return 0;
(void)shim_memset(&buf, 0, sizeof(buf));
rc = statvfs(path, &buf);
if (UNLIKELY(rc < 0))
return 0;
return (uint64_t)buf.f_favail;
#else
UNEXPECTED
return 0ULL;
#endif
}
/*
* stress_usage_bytes()
* report how much memory is used per instance
* and in total compared to physical memory available
*/
void stress_usage_bytes(
stress_args_t *args,
const size_t vm_per_instance,
const size_t vm_total)
{
const uint64_t total_phys_mem = stress_get_phys_mem_size();
char s1[32], s2[32], s3[32];
pr_inf("%s: using %s per stressor instance (total %s of %s available memory)\n",
args->name,
stress_uint64_to_str(s1, sizeof(s1), (uint64_t)vm_per_instance, 2, true),
stress_uint64_to_str(s2, sizeof(s2), (uint64_t)vm_total, 2, true),
stress_uint64_to_str(s3, sizeof(s3), total_phys_mem, 2, true));
}
/*
* stress_fs_usage_bytes()
* report how much file sysytem is used per instance
* and in total compared to file system space available
*/
void stress_fs_usage_bytes(
stress_args_t *args,
const off_t fs_size_per_instance,
const off_t fs_size_total)
{
const off_t total_fs_size = (off_t)stress_get_filesystem_size();
char s1[32], s2[32], s3[32];
if (total_fs_size > 0) {
pr_inf("%s: using %s file system space per stressor instance (total %s of %s available file system space)\n",
args->name,
stress_uint64_to_str(s1, sizeof(s1), (uint64_t)fs_size_per_instance, 2, true),
stress_uint64_to_str(s2, sizeof(s2), (uint64_t)fs_size_total, 2, true),
stress_uint64_to_str(s3, sizeof(s3), total_fs_size, 2, true));
}
}
/*
* stress_set_nonblock()
* try to make fd non-blocking
*/
int stress_set_nonblock(const int fd)
{
int flags;
#if defined(O_NONBLOCK)
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
flags = 0;
return fcntl(fd, F_SETFL, O_NONBLOCK | flags);
#else
UNEXPECTED
flags = 1;
return ioctl(fd, FIOBIO, &flags);
#endif
}
/*
* stress_get_load_avg()
* get load average
*/
int stress_get_load_avg(
double *min1,
double *min5,
double *min15)
{
#if defined(HAVE_GETLOADAVG) && \
!defined(__UCLIBC__)
int rc;
double loadavg[3];
if (UNLIKELY(!min1 || !min5 || !min15))
return -1;
loadavg[0] = 0.0;
loadavg[1] = 0.0;
loadavg[2] = 0.0;
rc = getloadavg(loadavg, 3);
if (UNLIKELY(rc < 0))
goto fail;
*min1 = loadavg[0];
*min5 = loadavg[1];
*min15 = loadavg[2];
return 0;
fail:
#elif defined(HAVE_SYS_SYSINFO_H) && \
defined(HAVE_SYSINFO)
struct sysinfo info;
const double scale = 1.0 / (double)(1 << SI_LOAD_SHIFT);
if (UNLIKELY(!min1 || !min5 || !min15))
return -1;
if (UNLIKELY(sysinfo(&info) < 0))
goto fail;
*min1 = info.loads[0] * scale;
*min5 = info.loads[1] * scale;
*min15 = info.loads[2] * scale;
return 0;
fail:
#else
if (UNLIKELY(!min1 || !min5 || !min15))
return -1;
#endif
*min1 = *min5 = *min15 = 0.0;
return -1;
}
/*
* stress_parent_died_alarm()
* send child SIGALRM if the parent died
*/
void stress_parent_died_alarm(void)
{
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_PDEATHSIG)
(void)prctl(PR_SET_PDEATHSIG, SIGALRM);
#elif defined(HAVE_SYS_PROCCTL_H) && \
defined(__FreeBSD__) && \
defined(PROC_PDEATHSIG_CTL)
int sig = SIGALRM;
(void)procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &sig);
#else
UNEXPECTED
#endif
}
/*
* stress_process_dumpable()
* set dumpable flag, e.g. produce a core dump or not,
* don't print an error if these fail, it's not that
* critical
*/
int stress_process_dumpable(const bool dumpable)
{
int rc = 0;
(void)dumpable;
#if defined(RLIMIT_CORE)
{
struct rlimit lim;
int ret;
ret = getrlimit(RLIMIT_CORE, &lim);
if (LIKELY(ret == 0)) {
lim.rlim_cur = 0;
(void)setrlimit(RLIMIT_CORE, &lim);
}
lim.rlim_cur = 0;
lim.rlim_max = 0;
(void)setrlimit(RLIMIT_CORE, &lim);
}
#else
UNEXPECTED
#endif
/*
* changing PR_SET_DUMPABLE also affects the
* oom adjust capability, so for now, we disable
* this as I'd rather have a oom'able process when
* memory gets constrained. Don't enable this
* unless one checks that processes able oomable!
*/
#if 0 && defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_DUMPABLE)
#if !defined(PR_SET_DISABLE)
#define SUID_DUMP_DISABLE (0) /* No setuid dumping */
#endif
#if !defined(SUID_DUMP_USER)
#define SUID_DUMP_USER (1) /* Dump as user of process */
#endif
(void)prctl(PR_SET_DUMPABLE,
dumpable ? SUID_DUMP_USER : SUID_DUMP_DISABLE);
#endif
#if defined(__linux__)
{
char const *str = dumpable ? "0x33" : "0x00";
if (stress_system_write("/proc/self/coredump_filter", str, strlen(str)) < 0)
rc = -1;
}
#endif
return rc;
}
/*
* stress_set_timer_slack_ns()
* set timer slack in nanoseconds
*/
int stress_set_timer_slack_ns(const char *opt)
{
#if defined(HAVE_PRCTL_TIMER_SLACK)
uint32_t timer_slack;
timer_slack = stress_get_uint32(opt);
if (UNLIKELY(timer_slack == 0))
pr_inf("note: setting timer_slack to 0 resets it to the default of 50,000 ns\n");
(void)stress_set_setting("global", "timer-slack", TYPE_ID_UINT32, &timer_slack);
#else
UNEXPECTED
(void)opt;
#endif
return 0;
}
/*
* stress_set_timer_slack()
* set timer slack
*/
void stress_set_timer_slack(void)
{
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(HAVE_PRCTL_TIMER_SLACK)
uint32_t timer_slack;
if (stress_get_setting("timer-slack", &timer_slack))
(void)prctl(PR_SET_TIMERSLACK, timer_slack);
#else
UNEXPECTED
#endif
}
/*
* stress_set_proc_name_init()
* init setproctitle if supported
*/
void stress_set_proc_name_init(int argc, char *argv[], char *envp[])
{
#if defined(HAVE_SETPROCTITLE) && \
defined(HAVE_SETPROCTITLE_INIT)
(void)setproctitle_init(argc, argv, envp);
#else
(void)argc;
(void)argv;
(void)envp;
UNEXPECTED
#endif
}
/*
* stress_set_proc_name()
* Set process name, we don't care if it fails
*/
void stress_set_proc_name(const char *name)
{
char long_name[64];
if (UNLIKELY(!name))
return;
if (g_opt_flags & OPT_FLAGS_KEEP_NAME)
return;
(void)snprintf(long_name, sizeof(long_name), "%s-%s",
g_app_name, name);
#if defined(HAVE_SETPROCTITLE)
/* Sets argv[0] */
setproctitle("-%s", long_name);
#endif
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_NAME)
/* Sets the comm field */
(void)prctl(PR_SET_NAME, long_name);
#endif
}
/*
* stress_set_proc_state_str
* set process name based on run state string, see
* macros STRESS_STATE_*
*/
void stress_set_proc_state_str(const char *name, const char *str)
{
char long_name[64];
if (UNLIKELY(!name || !str))
return;
(void)str;
if (g_opt_flags & OPT_FLAGS_KEEP_NAME)
return;
(void)snprintf(long_name, sizeof(long_name), "%s-%s",
g_app_name, name);
#if defined(HAVE_BSD_UNISTD_H) && \
defined(HAVE_SETPROCTITLE)
setproctitle("-%s [%s]", long_name, str);
#endif
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_NAME)
/* Sets the comm field */
(void)prctl(PR_SET_NAME, long_name);
#endif
}
/*
* stress_set_proc_state
* set process name based on run state, see
* macros STRESS_STATE_*
*/
void stress_set_proc_state(const char *name, const int state)
{
static const char * const stress_states[] = {
"start",
"init",
"run",
"syncwait",
"deinit",
"stop",
"exit",
"wait",
"zombie",
};
if (UNLIKELY(!name))
return;
if (UNLIKELY((state < 0) || (state >= (int)SIZEOF_ARRAY(stress_states))))
return;
stress_set_proc_state_str(name, stress_states[state]);
}
/*
* stress_chr_munge()
* convert ch _ to -, otherwise don't change it
*/
static inline char PURE stress_chr_munge(const char ch)
{
return (ch == '_') ? '-' : ch;
}
/*
* stress_munge_underscore()
* turn '_' to '-' in strings with strscpy api
*/
size_t stress_munge_underscore(char *dst, const char *src, size_t len)
{
register char *d = dst;
register const char *s = src;
register size_t n = len;
if (LIKELY(n)) {
while (--n) {
register char c = *s++;
*d++ = stress_chr_munge(c);
if (c == '\0')
break;
}
}
if (!n) {
if (len)
*d = '\0';
while (*s)
s++;
}
return (s - src - 1);
}
/*
* stress_strcmp_munged()
* compare strings with _ comcompared to -
*/
int stress_strcmp_munged(const char *s1, const char *s2)
{
for (; *s1 && (stress_chr_munge(*s1) == stress_chr_munge(*s2)); s1++, s2++)
;
return (unsigned char)stress_chr_munge(*s1) - (unsigned char)stress_chr_munge(*s2);
}
/*
* stress_get_stack_direction_helper()
* helper to determine direction of stack
*/
static ssize_t NOINLINE OPTIMIZE0 stress_get_stack_direction_helper(const uint8_t *val1)
{
const uint8_t val2 = *val1;
const ssize_t diff = &val2 - (const uint8_t *)val1;
return (diff > 0) - (diff < 0);
}
/*
* stress_get_stack_direction()
* determine which way the stack goes, up / down
* just pass in any var on the stack before calling
* return:
* 1 - stack goes down (conventional)
* 0 - error
* -1 - stack goes up (unconventional)
*/
ssize_t stress_get_stack_direction(void)
{
uint8_t val1 = 0;
uint8_t waste[64];
waste[(sizeof waste) - 1] = 0;
return stress_get_stack_direction_helper(&val1);
}
/*
* stress_get_stack_top()
* Get the stack top given the start and size of the stack,
* offset by a bit of slop. Assumes stack is > 64 bytes
*/
void *stress_get_stack_top(void *start, size_t size)
{
const size_t offset = stress_get_stack_direction() < 0 ? (size - 64) : 64;
return (void *)((char *)start + offset);
}
/*
* stress_get_uint64_zero()
* return uint64 zero in way that force less smart
* static analysers to realise we are doing this
* to force a division by zero. I'd like to have
* a better solution than this ghastly way.
*/
uint64_t stress_get_uint64_zero(void)
{
return g_shared->zero;
}
/*
* stress_get_uint64_zero()
* return null in way that force less smart
* static analysers to realise we are doing this
* to force a division by zero. I'd like to have
* a better solution than this ghastly way.
*/
void *stress_get_null(void)
{
return (void *)(uintptr_t)g_shared->zero;
}
/*
* stress_base36_encode_uint64()
* encode 64 bit hash of filename into a unique base 36
* filename of up to 13 chars long + 1 char eos
*/
static void stress_base36_encode_uint64(char dst[14], uint64_t val)
{
static const char b36[] = "abcdefghijklmnopqrstuvwxyz0123456789";
const int b = 36;
char *ptr = dst;
while (val) {
*ptr++ = b36[val % b];
val /= b;
}
*ptr = '\0';
}
/*
* stress_temp_hash_truncate()
* filenames may be too long for the underlying filesystem
* so workaround this by hashing them into a 64 bit hex
* filename.
*/
static void stress_temp_hash_truncate(char *filename)
{
size_t f_namemax = 16;
size_t len = strlen(filename);
#if defined(HAVE_SYS_STATVFS_H)
struct statvfs buf;
(void)shim_memset(&buf, 0, sizeof(buf));
if (statvfs(stress_get_temp_path(), &buf) == 0)
f_namemax = buf.f_namemax;
#endif
if (strlen(filename) > f_namemax) {
const uint32_t upper = stress_hash_jenkin((uint8_t *)filename, len);
const uint32_t lower = stress_hash_pjw(filename);
const uint64_t val = ((uint64_t)upper << 32) | lower;
stress_base36_encode_uint64(filename, val);
}
}
/*
* stress_temp_filename()
* construct a temp filename
*/
int stress_temp_filename(
char *path,
const size_t len,
const char *name,
const pid_t pid,
const uint32_t instance,
const uint64_t magic)
{
char directoryname[PATH_MAX];
char filename[PATH_MAX];
(void)snprintf(directoryname, sizeof(directoryname),
"tmp-%s-%s-%d-%" PRIu32,
g_app_name, name, (int)pid, instance);
stress_temp_hash_truncate(directoryname);
(void)snprintf(filename, sizeof(filename),
"%s-%s-%d-%" PRIu32 "-%" PRIu64,
g_app_name, name, (int)pid, instance, magic);
stress_temp_hash_truncate(filename);
return snprintf(path, len, "%s/%s/%s",
stress_get_temp_path(), directoryname, filename);
}
/*
* stress_temp_filename_args()
* construct a temp filename using info from args
*/
int stress_temp_filename_args(
stress_args_t *args,
char *path,
const size_t len,
const uint64_t magic)
{
return stress_temp_filename(path, len, args->name,
args->pid, args->instance, magic);
}
/*
* stress_temp_dir()
* create a temporary directory name
*/
int stress_temp_dir(
char *path,
const size_t len,
const char *name,
const pid_t pid,
const uint32_t instance)
{
char directoryname[256];
int l;
(void)snprintf(directoryname, sizeof(directoryname),
"tmp-%s-%s-%d-%" PRIu32,
g_app_name, name, (int)pid, instance);
stress_temp_hash_truncate(directoryname);
l = snprintf(path, len, "%s/%s",
stress_get_temp_path(), directoryname);
return l;
}
/*
* stress_temp_dir_args()
* create a temporary directory name using info from args
*/
int stress_temp_dir_args(
stress_args_t *args,
char *path,
const size_t len)
{
return stress_temp_dir(path, len,
args->name, args->pid, args->instance);
}
/*
* stress_temp_dir_mk()
* create a temporary directory
*/
int stress_temp_dir_mk(
const char *name,
const pid_t pid,
const uint32_t instance)
{
int ret;
char tmp[PATH_MAX];
stress_temp_dir(tmp, sizeof(tmp), name, pid, instance);
ret = mkdir(tmp, S_IRWXU);
if (UNLIKELY(ret < 0)) {
ret = -errno;
pr_fail("%s: mkdir '%s' failed, errno=%d (%s)\n",
name, tmp, errno, strerror(errno));
(void)shim_rmdir(tmp);
}
return ret;
}
/*
* stress_temp_dir_mk_args()
* create a temporary director using info from args
*/
int stress_temp_dir_mk_args(stress_args_t *args)
{
return stress_temp_dir_mk(args->name, args->pid, args->instance);
}
/*
* stress_temp_dir_rm()
* remove a temporary directory
*/
int stress_temp_dir_rm(
const char *name,
const pid_t pid,
const uint32_t instance)
{
int ret;
char tmp[PATH_MAX + 1];
stress_temp_dir(tmp, sizeof(tmp), name, pid, instance);
ret = shim_rmdir(tmp);
if (UNLIKELY(ret < 0)) {
ret = -errno;
pr_fail("%s: rmdir '%s' failed, errno=%d (%s)\n",
name, tmp, errno, strerror(errno));
}
return ret;
}
/*
* stress_temp_dir_rm_args()
* remove a temporary directory using info from args
*/
int stress_temp_dir_rm_args(stress_args_t *args)
{
return stress_temp_dir_rm(args->name, args->pid, args->instance);
}
/*
* stress_get_signal_name()
* return string version of signal number, NULL if not found
*/
const char PURE *stress_get_signal_name(const int signum)
{
size_t i;
#if defined(SIGRTMIN) && \
defined(SIGRTMAX)
if ((signum >= SIGRTMIN) && (signum <= SIGRTMAX)) {
static char sigrtname[10];
(void)snprintf(sigrtname, sizeof(sigrtname), "SIGRT%d",
signum - SIGRTMIN);
return sigrtname;
}
#endif
for (i = 0; i < SIZEOF_ARRAY(sig_names); i++) {
if (signum == sig_names[i].signum)
return sig_names[i].name;
}
return NULL;
}
/*
* stress_strsignal()
* signum to human readable string
*/
const char *stress_strsignal(const int signum)
{
static char buffer[40];
const char *str = stress_get_signal_name(signum);
if (str)
(void)snprintf(buffer, sizeof(buffer), "signal %d '%s'",
signum, str);
else
(void)snprintf(buffer, sizeof(buffer), "signal %d", signum);
return buffer;
}
/*
* stress_little_endian()
* returns true if CPU is little endian
*/
bool PURE stress_little_endian(void)
{
const uint32_t x = 0x12345678;
const uint8_t *y = (const uint8_t *)&x;
return *y == 0x78;
}
/*
* stress_endian_str()
* return endianness as a string
*/
static const char * PURE stress_endian_str(void)
{
return stress_little_endian() ? "little endian" : "big endian";
}
/*
* stress_uint8rnd4()
* fill a uint8_t buffer full of random data
* buffer *must* be multiple of 4 bytes in size
*/
OPTIMIZE3 void stress_uint8rnd4(uint8_t *data, const size_t len)
{
register uint32_t *ptr32 = (uint32_t *)shim_assume_aligned(data, 4);
register const uint32_t *ptr32end = (uint32_t *)(data + len);
if (UNLIKELY(!data || (len < 4)))
return;
if (stress_little_endian()) {
while (ptr32 < ptr32end)
*ptr32++ = stress_mwc32();
} else {
while (ptr32 < ptr32end)
*ptr32++ = stress_swap32(stress_mwc32());
}
}
/*
* stress_get_libc_version()
* return human readable libc version (where possible)
*/
static char *stress_get_libc_version(void)
{
#if defined(__GLIBC__) && \
defined(__GLIBC_MINOR__)
static char buf[64];
(void)snprintf(buf, sizeof(buf), "glibc %d.%d", __GLIBC__, __GLIBC_MINOR__);
return buf;
#elif defined(__UCLIBC__) && \
defined(__UCLIBC_MAJOR__) && \
defined(__UCLIBC_MINOR__)
static char buf[64];
(void)snprintf(buf, sizeof(buf), "uclibc %d.%d", __UCLIBC_MAJOR__, __UCLIBC_MINOR__);
return buf;
#elif defined(__CYGWIN__)
return "Cygwin libc";
#elif defined(__DARWIN_C_LEVEL)
return "Darwin libc";
#elif defined(HAVE_CC_MUSL_GCC)
/* Built with MUSL_GCC, highly probably it's musl libc being used too */
return "musl libc";
#elif defined(__HAIKU__)
return "Haiku libc";
#else
return "unknown libc version";
#endif
}
#define XSTR(s) STR(s)
#define STR(s) #s
/*
* stress_buildinfo()
* info about compiler, built date and compilation flags
*/
void stress_buildinfo(void)
{
if (g_opt_flags & OPT_FLAGS_BUILDINFO) {
pr_inf("compiler: %s\n", stress_get_compiler());
#if defined(HAVE_SOURCE_DATE_EPOCH)
pr_inf("SOURCE_DATE_EPOCH: " XSTR(HAVE_SOURCE_DATE_EPOCH) "\n");
#endif
#if defined(HAVE_EXTRA_BUILDINFO)
#if defined(HAVE_CFLAGS)
pr_inf("CFLAGS: " HAVE_CFLAGS "\n");
#endif
#if defined(HAVE_CXXFLAGS)
pr_inf("CXXFLAGS: " HAVE_CXXFLAGS "\n");
#endif
#if defined(HAVE_LDFLAGS)
pr_inf("LDFLAGS: " HAVE_LDFLAGS "\n");
#endif
#endif
#if defined(__STDC_VERSION__)
pr_inf("STDC Version: " XSTR(__STDC_VERSION__) "\n");
#endif
#if defined(__STDC_HOSTED__)
pr_inf("STDC Hosted: " XSTR(__STDC_HOSTED__) "\n");
#endif
#if defined(BUILD_STATIC)
pr_inf("Build: static image\n");
#else
pr_inf("Build: dynamic link\n");
#endif
}
}
/*
* stress_yaml_buildinfo()
* log info about compiler, built date and compilation flags
*/
void stress_yaml_buildinfo(FILE *yaml)
{
if (UNLIKELY(!yaml))
return;
pr_yaml(yaml, "build-info:\n");
pr_yaml(yaml, " compiler: '%s'\n", stress_get_compiler());
#if defined(HAVE_SOURCE_DATE_EPOCH)
pr_yaml(yaml, "source-date-epoch: " XSTR(HAVE_SOURCE_DATE_EPOCH) "\n");
#endif
#if defined(HAVE_EXTRA_BUILDINFO)
#if defined(HAVE_CFLAGS)
pr_yaml(yaml, " cflags: '" HAVE_CFLAGS "'\n");
#endif
#if defined(HAVE_CXXFLAGS)
pr_yaml(yaml, " cxxflags: '" HAVE_CXXFLAGS "'\n");
#endif
#if defined(HAVE_LDFLAGS)
pr_yaml(yaml, " ldflags: '" HAVE_LDFLAGS "'\n");
#endif
#endif
#if defined(__STDC_VERSION__)
pr_yaml(yaml, " stdc-version: '" XSTR(__STDC_VERSION__) "'\n");
#endif
#if defined(__STDC_HOSTED__)
pr_yaml(yaml, " stdc-hosted: '" XSTR(__STDC_HOSTED__) "'\n");
#endif
pr_yaml(yaml, "\n");
}
#undef XSTR
#undef STR
/*
* stress_runinfo()
* short info about the system we are running stress-ng on
* for the -v option
*/
void stress_runinfo(void)
{
char real_path[PATH_MAX], *real_path_ret;
const char *temp_path = stress_get_temp_path();
const char *fs_type = stress_get_fs_type(temp_path);
size_t freemem, totalmem, freeswap, totalswap;
#if defined(HAVE_UNAME) && \
defined(HAVE_SYS_UTSNAME_H)
struct utsname uts;
#endif
if (!(g_opt_flags & OPT_FLAGS_PR_DEBUG))
return;
if (sizeof(STRESS_GIT_COMMIT_ID) > 1) {
pr_dbg("%s %s g%12.12s\n",
g_app_name, VERSION, STRESS_GIT_COMMIT_ID);
} else {
pr_dbg("%s %s\n",
g_app_name, VERSION);
}
#if defined(HAVE_UNAME) && \
defined(HAVE_SYS_UTSNAME_H)
if (LIKELY(uname(&uts) >= 0)) {
pr_dbg("system: %s %s %s %s %s, %s, %s, %s\n",
uts.sysname, uts.nodename, uts.release,
uts.version, uts.machine,
stress_get_compiler(),
stress_get_libc_version(),
stress_endian_str());
}
#else
pr_dbg("system: %s, %s, %s, %s\n",
stress_get_arch(),
stress_get_compiler(),
stress_get_libc_version(),
stress_endian_str());
#endif
if (stress_get_meminfo(&freemem, &totalmem, &freeswap, &totalswap) == 0) {
char ram_t[32], ram_f[32], ram_s[32];
stress_uint64_to_str(ram_t, sizeof(ram_t), (uint64_t)totalmem, 1, false);
stress_uint64_to_str(ram_f, sizeof(ram_f), (uint64_t)freemem, 1, false);
stress_uint64_to_str(ram_s, sizeof(ram_s), (uint64_t)freeswap, 1, false);
pr_dbg("RAM total: %s, RAM free: %s, swap free: %s\n", ram_t, ram_f, ram_s);
}
real_path_ret = realpath(temp_path, real_path);
pr_dbg("temporary file path: '%s'%s\n", real_path_ret ? real_path : temp_path, fs_type);
}
/*
* stress_yaml_runinfo()
* log info about the system we are running stress-ng on
*/
void stress_yaml_runinfo(FILE *yaml)
{
#if defined(HAVE_UNAME) && \
defined(HAVE_SYS_UTSNAME_H)
struct utsname uts;
#endif
#if defined(HAVE_SYS_SYSINFO_H) && \
defined(HAVE_SYSINFO)
struct sysinfo info;
#endif
time_t t;
struct tm *tm = NULL;
const size_t hostname_len = stress_get_hostname_length();
char *hostname;
const char *user = shim_getlogin();
if (UNLIKELY(!yaml))
return;
pr_yaml(yaml, "system-info:\n");
if (time(&t) != ((time_t)-1))
tm = localtime(&t);
pr_yaml(yaml, " stress-ng-version: '" VERSION "'\n");
pr_yaml(yaml, " run-by: '%s'\n", user ? user : "unknown");
if (LIKELY(tm != NULL)) {
pr_yaml(yaml, " date-yyyy-mm-dd: '%4.4d:%2.2d:%2.2d'\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
pr_yaml(yaml, " time-hh-mm-ss: '%2.2d:%2.2d:%2.2d'\n",
tm->tm_hour, tm->tm_min, tm->tm_sec);
pr_yaml(yaml, " epoch-secs: %ld\n", (long int)t);
}
hostname = (char *)malloc(hostname_len + 1);
if (hostname && !gethostname(hostname, hostname_len)) {
pr_yaml(yaml, " hostname: '%s'\n", hostname);
} else {
pr_yaml(yaml, " hostname: '%s'\n", "unknown");
}
free(hostname);
#if defined(HAVE_UNAME) && \
defined(HAVE_SYS_UTSNAME_H)
if (LIKELY(uname(&uts) >= 0)) {
pr_yaml(yaml, " sysname: '%s'\n", uts.sysname);
pr_yaml(yaml, " nodename: '%s'\n", uts.nodename);
pr_yaml(yaml, " release: '%s'\n", uts.release);
pr_yaml(yaml, " version: '%s'\n", uts.version);
pr_yaml(yaml, " machine: '%s'\n", uts.machine);
}
#else
pr_yaml(yaml, " machine: '%s'\n", stress_get_arch());
#endif
pr_yaml(yaml, " compiler: '%s'\n", stress_get_compiler());
pr_yaml(yaml, " libc: '%s'\n", stress_get_libc_version());
#if defined(HAVE_SYS_SYSINFO_H) && \
defined(HAVE_SYSINFO)
(void)shim_memset(&info, 0, sizeof(info));
if (LIKELY(sysinfo(&info) == 0)) {
pr_yaml(yaml, " uptime: %ld\n", info.uptime);
pr_yaml(yaml, " totalram: %lu\n", info.totalram);
pr_yaml(yaml, " freeram: %lu\n", info.freeram);
pr_yaml(yaml, " sharedram: %lu\n", info.sharedram);
pr_yaml(yaml, " bufferram: %lu\n", info.bufferram);
pr_yaml(yaml, " totalswap: %lu\n", info.totalswap);
pr_yaml(yaml, " freeswap: %lu\n", info.freeswap);
}
#endif
pr_yaml(yaml, " pagesize: %zd\n", stress_get_page_size());
pr_yaml(yaml, " cpus: %" PRId32 "\n", stress_get_processors_configured());
pr_yaml(yaml, " cpus-online: %" PRId32 "\n", stress_get_processors_online());
pr_yaml(yaml, " ticks-per-second: %" PRId32 "\n", stress_get_ticks_per_second());
pr_yaml(yaml, "\n");
}
/*
* stress_cache_alloc()
* allocate shared cache buffer
*/
int stress_cache_alloc(const char *name)
{
stress_cpu_cache_cpus_t *cpu_caches;
stress_cpu_cache_t *cache = NULL;
uint16_t max_cache_level = 0, level;
char cache_info[512];
const int numa_nodes = stress_numa_nodes();
cpu_caches = stress_cpu_cache_get_all_details();
if (g_shared->mem_cache.size > 0)
goto init_done;
if (!cpu_caches) {
if (stress_warn_once())
pr_dbg("%s: using defaults, cannot determine cache details\n", name);
g_shared->mem_cache.size = MEM_CACHE_SIZE * numa_nodes;
goto init_done;
}
max_cache_level = stress_cpu_cache_get_max_level(cpu_caches);
if (max_cache_level == 0) {
if (stress_warn_once())
pr_dbg("%s: using defaults, cannot determine cache level details\n", name);
g_shared->mem_cache.size = MEM_CACHE_SIZE * numa_nodes;
goto init_done;
}
if (g_shared->mem_cache.level > max_cache_level) {
if (stress_warn_once())
pr_dbg("%s: using cache maximum level L%d\n", name,
max_cache_level);
g_shared->mem_cache.level = max_cache_level;
}
cache = stress_cpu_cache_get(cpu_caches, g_shared->mem_cache.level);
if (!cache) {
if (stress_warn_once())
pr_dbg("%s: using built-in defaults as no suitable "
"cache found\n", name);
g_shared->mem_cache.size = MEM_CACHE_SIZE * numa_nodes;
goto init_done;
}
if (g_shared->mem_cache.ways > 0) {
uint64_t way_size;
if (g_shared->mem_cache.ways > cache->ways) {
if (stress_warn_once())
pr_inf("%s: cache way value too high - "
"defaulting to %d (the maximum)\n",
name, cache->ways);
g_shared->mem_cache.ways = cache->ways;
}
way_size = cache->size / cache->ways;
/* only fill the specified number of cache ways */
g_shared->mem_cache.size = way_size * g_shared->mem_cache.ways * numa_nodes;
} else {
/* fill the entire cache */
g_shared->mem_cache.size = cache->size * numa_nodes;
}
if (!g_shared->mem_cache.size) {
if (stress_warn_once())
pr_dbg("%s: using built-in defaults as "
"unable to determine cache size\n", name);
g_shared->mem_cache.size = MEM_CACHE_SIZE;
}
(void)shim_memset(cache_info, 0, sizeof(cache_info));
for (level = 1; level <= max_cache_level; level++) {
size_t cache_size = 0, cache_line_size = 0;
stress_cpu_cache_get_level_size(level, &cache_size, &cache_line_size);
if ((cache_size > 0) && (cache_line_size > 0)) {
char tmp[64];
(void)snprintf(tmp, sizeof(tmp), "%sL%" PRIu16 ": %zdK",
(level > 1) ? ", " : "", level, cache_size >> 10);
shim_strlcat(cache_info, tmp, sizeof(cache_info));
}
}
pr_dbg("CPU data cache: %s\n", cache_info);
init_done:
stress_free_cpu_caches(cpu_caches);
g_shared->mem_cache.buffer =
(uint8_t *)stress_mmap_populate(NULL, g_shared->mem_cache.size,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (g_shared->mem_cache.buffer == MAP_FAILED) {
g_shared->mem_cache.buffer = NULL;
pr_err("%s: failed to mmap shared cache buffer, errno=%d (%s)\n",
name, errno, strerror(errno));
return -1;
}
stress_set_vma_anon_name(g_shared->mem_cache.buffer, g_shared->mem_cache.size, "mem-cache");
g_shared->cacheline.size = (size_t)STRESS_PROCS_MAX * sizeof(uint8_t) * 2;
g_shared->cacheline.buffer =
(uint8_t *)stress_mmap_populate(NULL, g_shared->cacheline.size,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (g_shared->cacheline.buffer == MAP_FAILED) {
g_shared->cacheline.buffer = NULL;
pr_err("%s: failed to mmap cacheline buffer, errno=%d (%s)\n",
name, errno, strerror(errno));
return -1;
}
stress_set_vma_anon_name(g_shared->cacheline.buffer, g_shared->cacheline.size, "cacheline");
if (stress_warn_once()) {
if (numa_nodes > 1) {
pr_dbg("%s: shared cache buffer size: %" PRIu64 "K (LLC size x %d NUMA nodes)\n",
name, g_shared->mem_cache.size / 1024, numa_nodes);
} else {
pr_dbg("%s: shared cache buffer size: %" PRIu64 "K\n",
name, g_shared->mem_cache.size / 1024);
}
}
return 0;
}
/*
* stress_cache_free()
* free shared cache buffer
*/
void stress_cache_free(void)
{
if (g_shared->mem_cache.buffer)
(void)munmap((void *)g_shared->mem_cache.buffer, g_shared->mem_cache.size);
if (g_shared->cacheline.buffer)
(void)munmap((void *)g_shared->cacheline.buffer, g_shared->cacheline.size);
}
/*
* stress_system_write()
* write a buffer to a /sys or /proc entry
*/
ssize_t stress_system_write(
const char *path,
const char *buf,
const size_t buf_len)
{
int fd;
ssize_t ret;
if (UNLIKELY(!path || !buf))
return -EINVAL;
if (UNLIKELY(buf_len == 0))
return -EINVAL;
fd = open(path, O_WRONLY);
if (UNLIKELY(fd < 0))
return -errno;
ret = write(fd, buf, buf_len);
if (ret < (ssize_t)buf_len)
ret = -errno;
(void)close(fd);
return ret;
}
/*
* stress_system_discard()
* read and discard contents of a given file
*/
ssize_t stress_system_discard(const char *path)
{
int fd;
ssize_t ret;
if (UNLIKELY(!path))
return -EINVAL;
fd = open(path, O_RDONLY);
if (UNLIKELY(fd < 0))
return -errno;
ret = stress_read_discard(fd);
(void)close(fd);
return ret;
}
/*
* stress_system_read()
* read a buffer from a /sys or /proc entry
*/
ssize_t stress_system_read(
const char *path,
char *buf,
const size_t buf_len)
{
int fd;
ssize_t ret;
if (UNLIKELY(!path || !buf))
return -EINVAL;
if (UNLIKELY(buf_len == 0))
return -EINVAL;
(void)shim_memset(buf, 0, buf_len);
fd = open(path, O_RDONLY);
if (UNLIKELY(fd < 0))
return -errno;
ret = read(fd, buf, buf_len);
if (UNLIKELY(ret < 0)) {
buf[0] = '\0';
ret = -errno;
}
(void)close(fd);
if ((ssize_t)buf_len == ret)
buf[buf_len - 1] = '\0';
return ret;
}
/*
* stress_is_prime64()
* return true if 64 bit value n is prime
* http://en.wikipedia.org/wiki/Primality_test
*/
bool PURE stress_is_prime64(const uint64_t n)
{
register uint64_t i, max;
double max_d;
if (UNLIKELY(n <= 3))
return n >= 2;
if ((n % 2 == 0) || (n % 3 == 0))
return false;
max_d = 1.0 + shim_sqrt((double)n);
max = (uint64_t)max_d;
for (i = 5; i < max; i += 6)
if ((n % i == 0) || (n % (i + 2) == 0))
return false;
return true;
}
/*
* stress_get_next_prime64()
* find a prime that is not a multiple of n,
* used for file name striding. Minimum is 1009,
* max is unbounded. Return a prime > n, each
* call will return the next prime to keep the
* primes different each call.
*/
uint64_t stress_get_next_prime64(const uint64_t n)
{
static uint64_t p = 1009;
const uint64_t odd_n = (n & 0x0ffffffffffffffeUL) + 1;
int i;
if (LIKELY(p < odd_n))
p = odd_n;
/* Search for next prime.. */
for (i = 0; LIKELY(stress_continue_flag() && (i < 2000)); i++) {
p += 2;
if ((n % p) && stress_is_prime64(p))
return p;
}
/* Give up */
p = 1009;
return p;
}
/*
* stress_get_prime64()
* find a prime that is not a multiple of n,
* used for file name striding. Minimum is 1009,
* max is unbounded. Return a prime > n.
*/
uint64_t stress_get_prime64(const uint64_t n)
{
uint64_t p = 1009;
const uint64_t odd_n = (n & 0x0ffffffffffffffeUL) + 1;
int i;
if (LIKELY(p < odd_n))
p = odd_n;
/* Search for next prime.. */
for (i = 0; LIKELY(stress_continue_flag() && (i < 2000)); i++) {
p += 2;
if ((n % p) && stress_is_prime64(p))
return p;
}
/* Give up */
return 18446744073709551557ULL; /* Max 64 bit prime */
}
/*
* stress_get_max_file_limit()
* get max number of files that the current
* process can open not counting the files that
* may already been opened.
*/
size_t stress_get_max_file_limit(void)
{
#if defined(HAVE_GETDTABLESIZE)
int tablesize;
#endif
#if defined(RLIMIT_NOFILE)
struct rlimit rlim;
#endif
size_t max_rlim = SIZE_MAX;
size_t max_sysconf;
#if defined(HAVE_GETDTABLESIZE)
/* try the simple way first */
tablesize = getdtablesize();
if (tablesize > 0)
return (size_t)tablesize;
#endif
#if defined(RLIMIT_NOFILE)
if (!getrlimit(RLIMIT_NOFILE, &rlim))
max_rlim = (size_t)rlim.rlim_cur;
#endif
#if defined(_SC_OPEN_MAX)
{
const long int open_max = sysconf(_SC_OPEN_MAX);
max_sysconf = (open_max > 0) ? (size_t)open_max : SIZE_MAX;
}
#else
max_sysconf = SIZE_MAX;
UNEXPECTED
#endif
/* return the lowest of these two */
return STRESS_MINIMUM(max_rlim, max_sysconf);
}
/*
* stress_get_open_count(void)
* get number of open file descriptors
*/
static inline size_t stress_get_open_count(void)
{
#if defined(__linux__)
DIR *dir;
struct dirent *d;
size_t n = 0;
dir = opendir("/proc/self/fd");
if (!dir)
return (size_t)-1;
while ((d = readdir(dir)) != NULL) {
if (isdigit((unsigned char)d->d_name[0]))
n++;
}
(void)closedir(dir);
/*
* opendir used one extra fd that is now
* closed, so take that off the total
*/
return (n > 1) ? (n - 1) : n;
#else
return 0;
#endif
}
/*
* stress_get_file_limit()
* get max number of files that the current
* process can open excluding currently opened
* files.
*/
size_t stress_get_file_limit(void)
{
struct rlimit rlim;
size_t last_opened, opened, max = 65536; /* initial guess */
if (!getrlimit(RLIMIT_NOFILE, &rlim))
max = (size_t)rlim.rlim_cur;
last_opened = 0;
opened = stress_get_open_count();
if (opened == 0) {
size_t i;
/* Determine max number of free file descriptors we have */
for (i = 0; i < max; i++) {
if (fcntl((int)i, F_GETFL) > -1) {
opened++;
last_opened = i;
} else {
/*
* Hack: Over 250 contiguously closed files
* most probably indicates we're at the point
* were no more opened file descriptors are
* going to be found, so bail out rather then
* scanning for any more opened files
*/
if (i - last_opened > 250)
break;
}
}
}
return max - opened;
}
/*
* stress_get_bad_fd()
* return a fd that will produce -EINVAL when using it
* either because it is not open or it is just out of range
*/
int stress_get_bad_fd(void)
{
#if defined(RLIMIT_NOFILE) && \
defined(F_GETFL)
struct rlimit rlim;
(void)shim_memset(&rlim, 0, sizeof(rlim));
if (getrlimit(RLIMIT_NOFILE, &rlim) == 0) {
if (rlim.rlim_cur < INT_MAX - 1) {
if (fcntl((int)rlim.rlim_cur, F_GETFL) == -1) {
return (int)rlim.rlim_cur + 1;
}
}
}
#elif defined(F_GETFL)
int i;
for (i = 2048; i > fileno(stdout); i--) {
if (fcntl((int)i, F_GETFL) == -1)
return i;
}
#else
UNEXPECTED
#endif
return -1;
}
/*
* stress_sigaltstack_no_check()
* attempt to set up an alternative signal stack with no
* minimum size check on stack
* stack - must be at least MINSIGSTKSZ
* size - size of stack (- STACK_ALIGNMENT)
*/
int stress_sigaltstack_no_check(void *stack, const size_t size)
{
#if defined(HAVE_SIGALTSTACK)
stack_t ss;
if (stack == NULL) {
ss.ss_sp = NULL;
ss.ss_size = 0;
ss.ss_flags = SS_DISABLE;
} else {
ss.ss_sp = (void *)stack;
ss.ss_size = size;
ss.ss_flags = 0;
}
return sigaltstack(&ss, NULL);
#else
UNEXPECTED
(void)stack;
(void)size;
return 0;
#endif
}
/*
* stress_sigaltstack()
* attempt to set up an alternative signal stack
* stack - must be at least MINSIGSTKSZ
* size - size of stack (- STACK_ALIGNMENT)
*/
int stress_sigaltstack(void *stack, const size_t size)
{
#if defined(HAVE_SIGALTSTACK)
if (stack && (size < (size_t)STRESS_MINSIGSTKSZ)) {
pr_err("sigaltstack stack size %zu must be more than %zuK\n",
size, (size_t)STRESS_MINSIGSTKSZ / 1024);
return -1;
}
if (stress_sigaltstack_no_check(stack, size) < 0) {
pr_fail("sigaltstack failed, errno=%d (%s)\n",
errno, strerror(errno));
return -1;
}
#else
UNEXPECTED
(void)stack;
(void)size;
#endif
return 0;
}
/*
* stress_sigaltstack_disable()
* disable the alternative signal stack
*/
void stress_sigaltstack_disable(void)
{
#if defined(HAVE_SIGALTSTACK)
stack_t ss;
ss.ss_sp = NULL;
ss.ss_size = 0;
ss.ss_flags = SS_DISABLE;
sigaltstack(&ss, NULL);
#endif
return;
}
/*
* stress_mask_longjump_signals()
* mask all signals which may have handlers which use siglongjmp()
*/
void stress_mask_longjump_signals(sigset_t *set)
{
#if defined(SIGBUS)
sigaddset(set, SIGBUS);
#endif
#if defined(SIGFPE)
sigaddset(set, SIGFPE);
#endif
#if defined(SIGILL)
sigaddset(set, SIGILL);
#endif
#if defined(SIGSEGV)
sigaddset(set, SIGSEGV);
#endif
#if defined(SIGXFSZ)
sigaddset(set, SIGXFSZ);
#endif
#if defined(SIGXCPU)
sigaddset(set, SIGXCPU);
#endif
#if defined(SIGRTMIN)
sigaddset(set, SIGRTMIN);
#endif
}
/*
* stress_sighandler()
* set signal handler in generic way
*/
int stress_sighandler(
const char *name,
const int signum,
void (*handler)(int),
struct sigaction *orig_action)
{
struct sigaction new_action;
#if defined(HAVE_SIGALTSTACK)
{
static uint8_t *stack = NULL;
if (stack == NULL) {
/* Allocate stack, we currently leak this */
stack = (uint8_t *)stress_mmap_populate(NULL, STRESS_SIGSTKSZ,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (stack == MAP_FAILED) {
pr_inf("%s: sigaction %s: cannot allocated signal stack, "
"errno=%d (%s)\n",
name, stress_strsignal(signum),
errno, strerror(errno));
return -1;
}
stress_set_vma_anon_name(stack, STRESS_SIGSTKSZ, "sigstack");
if (stress_sigaltstack(stack, STRESS_SIGSTKSZ) < 0)
return -1;
}
}
#endif
(void)shim_memset(&new_action, 0, sizeof new_action);
new_action.sa_handler = handler;
(void)sigemptyset(&new_action.sa_mask);
/*
* Signals intended to stop stress-ng should never be interrupted
* by a signal with a handler which may not return to the caller.
*/
if ((signum == SIGALRM) || (signum == SIGINT) || (signum == SIGHUP)
|| (signum == SIGTERM))
stress_mask_longjump_signals(&new_action.sa_mask);
new_action.sa_flags = SA_NOCLDSTOP;
#if defined(HAVE_SIGALTSTACK)
new_action.sa_flags |= SA_ONSTACK;
#endif
if (sigaction(signum, &new_action, orig_action) < 0) {
pr_fail("%s: sigaction %s, errno=%d (%s)\n",
name, stress_strsignal(signum), errno, strerror(errno));
return -1;
}
return 0;
}
/* stress_sigchld_helper_handler()
* parent is informed child has terminated and
* it's time to stop
*/
static void MLOCKED_TEXT stress_sigchld_helper_handler(int signum)
{
if (signum == SIGCHLD)
stress_continue_set_flag(false);
}
/*
* stress_sigchld_set_handler()
* set sigchld handler
*/
int stress_sigchld_set_handler(stress_args_t *args)
{
return stress_sighandler(args->name, SIGCHLD, stress_sigchld_helper_handler, NULL);
}
/*
* stress_sighandler_default
* restore signal handler to default handler
*/
int stress_sighandler_default(const int signum)
{
struct sigaction new_action;
(void)shim_memset(&new_action, 0, sizeof new_action);
new_action.sa_handler = SIG_DFL;
return sigaction(signum, &new_action, NULL);
}
/*
* stress_handle_stop_stressing()
* set flag to indicate to stressor to stop stressing
*/
void stress_handle_stop_stressing(const int signum)
{
(void)signum;
stress_continue_set_flag(false);
/*
* Trigger another SIGARLM until stressor gets the message
* that it needs to terminate
*/
(void)alarm(1);
}
/*
* stress_sig_stop_stressing()
* install a handler that sets the global flag
* to indicate to a stressor to stop stressing
*/
int stress_sig_stop_stressing(const char *name, const int sig)
{
return stress_sighandler(name, sig, stress_handle_stop_stressing, NULL);
}
/*
* stress_sigrestore()
* restore a handler
*/
int stress_sigrestore(
const char *name,
const int signum,
struct sigaction *orig_action)
{
if (UNLIKELY(sigaction(signum, orig_action, NULL) < 0)) {
pr_fail("%s: sigaction %s restore, errno=%d (%s)\n",
name, stress_strsignal(signum), errno, strerror(errno));
return -1;
}
return 0;
}
/*
* stress_get_cpu()
* get cpu number that process is currently on
*/
unsigned int stress_get_cpu(void)
{
#if defined(HAVE_SCHED_GETCPU)
#if defined(__PPC64__) || defined(__ppc64__) || \
defined(__PPC__) || defined(__ppc__) || \
defined(__s390x__)
unsigned int cpu, node;
if (UNLIKELY(shim_getcpu(&cpu, &node, NULL) < 0))
return 0;
return cpu;
#else
const int cpu = sched_getcpu();
return (unsigned int)((cpu < 0) ? 0 : cpu);
#endif
#else
unsigned int cpu, node;
if (UNLIKELY(shim_getcpu(&cpu, &node, NULL) < 0))
return 0;
return cpu;
#endif
}
#define XSTRINGIFY(s) STRINGIFY(s)
#define STRINGIFY(s) #s
/*
* stress_get_compiler()
* return compiler info
*/
const char PURE *stress_get_compiler(void)
{
#if defined(HAVE_COMPILER_ICC) && \
defined(__INTEL_COMPILER) && \
defined(__INTEL_COMPILER_UPDATE) && \
defined(__INTEL_COMPILER_BUILD_DATE)
static const char cc[] = "icc " XSTRINGIFY(__INTEL_COMPILER) "." XSTRINGIFY(__INTEL_COMPILER_UPDATE) " Build " XSTRINGIFY(__INTEL_COMPILER_BUILD_DATE) "";
#elif defined(HAVE_COMPILER_ICC) && \
defined(__INTEL_COMPILER) && \
defined(__INTEL_COMPILER_UPDATE)
static const char cc[] = "icc " XSTRINGIFY(__INTEL_COMPILER) "." XSTRINGIFY(__INTEL_COMPILER_UPDATE) "";
#elif defined(__INTEL_CLANG_COMPILER)
static const char cc[] = "icx " XSTRINGIFY(__INTEL_CLANG_COMPILER) "";
#elif defined(__INTEL_LLVM_COMPILER)
static const char cc[] = "icx " XSTRINGIFY(__INTEL_LLVM_COMPILER) "";
#elif defined(__TINYC__)
static const char cc[] = "tcc " XSTRINGIFY(__TINYC__) "";
#elif defined(__PCC__) && \
defined(__PCC_MINOR__)
static const char cc[] = "pcc " XSTRINGIFY(__PCC__) "." XSTRINGIFY(__PCC_MINOR__) "." XSTRINGIFY(__PCC_MINORMINOR__) "";
#elif defined(__clang_major__) && \
defined(__clang_minor__) && \
defined(__clang_patchlevel__)
static const char cc[] = "clang " XSTRINGIFY(__clang_major__) "." XSTRINGIFY(__clang_minor__) "." XSTRINGIFY(__clang_patchlevel__) "";
#elif defined(__clang_major__) && \
defined(__clang_minor__)
static const char cc[] = "clang " XSTRINGIFY(__clang_major__) "." XSTRINGIFY(__clang_minor__) "";
#elif defined(__GNUC__) && \
defined(__GNUC_MINOR__) && \
defined(__GNUC_PATCHLEVEL__) && \
defined(HAVE_COMPILER_MUSL)
static const char cc[] = "musl-gcc " XSTRINGIFY(__GNUC__) "." XSTRINGIFY(__GNUC_MINOR__) "." XSTRINGIFY(__GNUC_PATCHLEVEL__) "";
#elif defined(__GNUC__) && \
defined(__GNUC_MINOR__) && \
defined(HAVE_COMPILER_MUSL)
static const char cc[] = "musl-gcc " XSTRINGIFY(__GNUC__) "." XSTRINGIFY(__GNUC_MINOR__) "";
#elif defined(__GNUC__) && \
defined(__GNUC_MINOR__) && \
defined(__GNUC_PATCHLEVEL__) && \
defined(HAVE_COMPILER_GCC)
static const char cc[] = "gcc " XSTRINGIFY(__GNUC__) "." XSTRINGIFY(__GNUC_MINOR__) "." XSTRINGIFY(__GNUC_PATCHLEVEL__) "";
#elif defined(__GNUC__) && \
defined(__GNUC_MINOR__) && \
defined(HAVE_COMPILER_GCC)
static const char cc[] = "gcc " XSTRINGIFY(__GNUC__) "." XSTRINGIFY(__GNUC_MINOR__) "";
#else
static const char cc[] = "cc unknown";
#endif
return cc;
}
/*
* stress_get_uname_info()
* return uname information
*/
const char *stress_get_uname_info(void)
{
#if defined(HAVE_UNAME) && \
defined(HAVE_SYS_UTSNAME_H)
struct utsname buf;
if (LIKELY(uname(&buf) >= 0)) {
static char str[sizeof(buf.machine) +
sizeof(buf.sysname) +
sizeof(buf.release) + 3];
(void)snprintf(str, sizeof(str), "%s %s %s", buf.machine, buf.sysname, buf.release);
return str;
}
#else
UNEXPECTED
#endif
return "unknown";
}
/*
* stress_unimplemented()
* report that a stressor is not implemented
* on a particular arch or kernel
*/
int PURE stress_unimplemented(stress_args_t *args)
{
(void)args;
return EXIT_NOT_IMPLEMENTED;
}
#if defined(F_SETPIPE_SZ)
/*
* stress_check_max_pipe_size()
* check if the given pipe size is allowed
*/
static inline int stress_check_max_pipe_size(
const size_t sz,
const size_t page_size)
{
int fds[2], rc = 0;
if (UNLIKELY(sz < page_size))
return -1;
if (UNLIKELY(pipe(fds) < 0))
return -1;
if (fcntl(fds[0], F_SETPIPE_SZ, sz) < 0)
rc = -1;
(void)close(fds[0]);
(void)close(fds[1]);
return rc;
}
#endif
/*
* stress_probe_max_pipe_size()
* determine the maximum allowed pipe size
*/
size_t stress_probe_max_pipe_size(void)
{
static size_t max_pipe_size;
#if defined(F_SETPIPE_SZ)
ssize_t ret;
size_t i, prev_sz, sz, min, max;
char buf[64];
size_t page_size;
#endif
/* Already determined? returned cached size */
if (max_pipe_size)
return max_pipe_size;
#if defined(F_SETPIPE_SZ)
page_size = stress_get_page_size();
/*
* Try and find maximum pipe size directly
*/
ret = stress_system_read("/proc/sys/fs/pipe-max-size", buf, sizeof(buf));
if (ret > 0) {
if (sscanf(buf, "%zu", &sz) == 1)
if (!stress_check_max_pipe_size(sz, page_size))
goto ret;
}
/*
* Need to find size by binary chop probing
*/
min = page_size;
max = INT_MAX;
prev_sz = 0;
sz = 0;
for (i = 0; i < 64; i++) {
sz = min + (max - min) / 2;
if (prev_sz == sz)
return sz;
prev_sz = sz;
if (stress_check_max_pipe_size(sz, page_size) == 0) {
min = sz;
} else {
max = sz;
}
}
ret:
max_pipe_size = sz;
#else
max_pipe_size = stress_get_page_size();
#endif
return max_pipe_size;
}
/*
* stress_align_address
* align address to alignment, alignment MUST be a power of 2
*/
void PURE *stress_align_address(const void *addr, const size_t alignment)
{
const uintptr_t uintptr =
((uintptr_t)addr + alignment) & ~(alignment - 1);
return (void *)uintptr;
}
/*
* stress_sigalrm_pending()
* return true if SIGALRM is pending
*/
bool stress_sigalrm_pending(void)
{
sigset_t set;
(void)sigemptyset(&set);
(void)sigpending(&set);
return sigismember(&set, SIGALRM);
}
/*
* stress_uint64_to_str()
* turn 64 bit size to human readable string, if no_zero is true, truncate
* to integer if decimal part is zero
*/
char *stress_uint64_to_str(char *str, size_t len, const uint64_t val, const int precision, bool no_zero)
{
typedef struct {
const uint64_t size;
const char *suffix;
} stress_size_info_t;
static const stress_size_info_t size_info[] = {
{ EB, "E" },
{ PB, "P" },
{ TB, "T" },
{ GB, "G" },
{ MB, "M" },
{ KB, "K" },
};
size_t i;
const char *suffix = "";
uint64_t scale = 1;
int prec = precision;
if (UNLIKELY((!str) || (len < 1)))
return str;
for (i = 0; i < SIZEOF_ARRAY(size_info); i++) {
const uint64_t scaled = val / size_info[i].size;
if ((scaled >= 1) && (scaled < 1024)) {
suffix = size_info[i].suffix;
scale = size_info[i].size;
break;
}
}
if (no_zero && ((val % scale) == 0))
prec = 0;
(void)snprintf(str, len, "%.*f%s", prec, (double)val / (double)scale, suffix);
return str;
}
/*
* stress_check_root()
* returns true if root
*/
static inline bool stress_check_root(void)
{
if (geteuid() == 0)
return true;
#if defined(__CYGWIN__)
{
/*
* Cygwin would only return uid 0 if the Windows user is mapped
* to this uid by a custom /etc/passwd file. Regardless of uid,
* a process has administrator privileges if the local
* administrator group (S-1-5-32-544) is present in the process
* token. By default, Cygwin maps this group to gid 544 but it
* may be mapped to gid 0 by a custom /etc/group file.
*/
gid_t *gids;
long int gids_max;
int ngids;
#if defined(_SC_NGROUPS_MAX)
gids_max = sysconf(_SC_NGROUPS_MAX);
if ((gids_max < 0) || (gids_max > 65536))
gids_max = 65536;
#else
gids_max = 65536;
#endif
gids = (gid_t *)calloc((size_t)gids_max, sizeof(*gids));
if (!gids)
return false;
ngids = getgroups((int)gids_max, gids);
if (ngids > 0) {
int i;
for (i = 0; i < ngids; i++) {
if ((gids[i] == 0) || (gids[i] == 544)) {
free(gids);
return true;
}
}
}
free(gids);
}
#endif
return false;
}
#if defined(HAVE_SYS_CAPABILITY_H)
void stress_getset_capability(void)
{
struct __user_cap_header_struct uch;
struct __user_cap_data_struct ucd[_LINUX_CAPABILITY_U32S_3];
uch.version = _LINUX_CAPABILITY_VERSION_3;
uch.pid = getpid();
if (capget(&uch, ucd) < 0)
return;
(void)capset(&uch, ucd);
}
#endif
#if defined(HAVE_SYS_CAPABILITY_H)
/*
* stress_check_capability()
* returns true if process has the given capability,
* if capability is SHIM_CAP_IS_ROOT then just check if process is
* root.
*/
bool stress_check_capability(const int capability)
{
int ret;
struct __user_cap_header_struct uch;
struct __user_cap_data_struct ucd[_LINUX_CAPABILITY_U32S_3];
uint32_t mask;
size_t idx;
if (capability == SHIM_CAP_IS_ROOT)
return stress_check_root();
(void)shim_memset(&uch, 0, sizeof uch);
(void)shim_memset(ucd, 0, sizeof ucd);
uch.version = _LINUX_CAPABILITY_VERSION_3;
uch.pid = getpid();
ret = capget(&uch, ucd);
if (ret < 0)
return stress_check_root();
idx = (size_t)CAP_TO_INDEX(capability);
mask = CAP_TO_MASK(capability);
return (ucd[idx].permitted &= mask) ? true : false;
}
#else
bool stress_check_capability(const int capability)
{
(void)capability;
return stress_check_root();
}
#endif
/*
* stress_drop_capabilities()
* drop all capabilities and disable any new privileges
*/
#if defined(HAVE_SYS_CAPABILITY_H)
int stress_drop_capabilities(const char *name)
{
int ret;
uint32_t i;
struct __user_cap_header_struct uch;
struct __user_cap_data_struct ucd[_LINUX_CAPABILITY_U32S_3];
(void)shim_memset(&uch, 0, sizeof uch);
(void)shim_memset(ucd, 0, sizeof ucd);
uch.version = _LINUX_CAPABILITY_VERSION_3;
uch.pid = getpid();
ret = capget(&uch, ucd);
if (UNLIKELY(ret < 0)) {
pr_fail("%s: capget on PID %" PRIdMAX " failed, errno=%d (%s)\n",
name, (intmax_t)uch.pid, errno, strerror(errno));
return -1;
}
/*
* We could just memset ucd to zero, but
* lets explicitly set all the capability
* bits to zero to show the intent
*/
for (i = 0; i <= CAP_LAST_CAP; i++) {
const uint32_t idx = CAP_TO_INDEX(i);
const uint32_t mask = CAP_TO_MASK(i);
ucd[idx].inheritable &= ~mask;
ucd[idx].permitted &= ~mask;
ucd[idx].effective &= ~mask;
}
ret = capset(&uch, ucd);
if (UNLIKELY(ret < 0)) {
pr_fail("%s: capset on PID %" PRIdMAX " failed, errno=%d (%s)\n",
name, (intmax_t)uch.pid, errno, strerror(errno));
return -1;
}
#if defined(HAVE_PRCTL) && \
defined(HAVE_SYS_PRCTL_H) && \
defined(PR_SET_NO_NEW_PRIVS)
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
if (UNLIKELY(ret < 0)) {
/* Older kernels that don't support this prctl throw EINVAL */
if (errno != EINVAL) {
pr_inf("%s: prctl PR_SET_NO_NEW_PRIVS on PID %" PRIdMAX " failed: "
"errno=%d (%s)\n",
name, (intmax_t)uch.pid, errno, strerror(errno));
}
return -1;
}
#endif
return 0;
}
#else
int stress_drop_capabilities(const char *name)
{
(void)name;
return 0;
}
#endif
/*
* stress_is_dot_filename()
* is filename "." or ".."
*/
bool PURE stress_is_dot_filename(const char *name)
{
if (UNLIKELY(!name))
return false;
if (!strcmp(name, "."))
return true;
if (!strcmp(name, ".."))
return true;
return false;
}
/*
* stress_const_optdup(const char *opt)
* duplicate a modifiable copy of a const option string opt
*/
char *stress_const_optdup(const char *opt)
{
char *str;
if (UNLIKELY(!opt))
return NULL;
str = shim_strdup(opt);
if (UNLIKELY(!str))
(void)fprintf(stderr, "out of memory duplicating option '%s'\n", opt);
return str;
}
/*
* stress_get_exec_text_addr()
* return length and start/end addresses of text segment
*/
size_t stress_exec_text_addr(char **start, char **end)
{
#if defined(HAVE_EXECUTABLE_START)
extern char __executable_start;
intptr_t text_start = (intptr_t)&__executable_start;
#elif defined(__APPLE__)
extern char _mh_execute_header;
intptr_t text_start = (intptr_t)&_mh_execute_header;
#elif defined(__OpenBSD__)
extern char _start[];
intptr_t text_start = (intptr_t)&_start[0];
#elif defined(HAVE_COMPILER_TCC)
extern char _start;
intptr_t text_start = (intptr_t)&_start;
#elif defined(__CYGWIN__)
extern char WinMainCRTStartup;
intptr_t text_start = (intptr_t)&WinMainCRTStartup;
#else
extern char _start;
intptr_t text_start = (intptr_t)&_start;
#endif
#if defined(__APPLE__)
extern void *get_etext(void);
intptr_t text_end = (intptr_t)get_etext();
#elif defined(HAVE_COMPILER_TCC)
extern char _etext;
intptr_t text_end = (intptr_t)&_etext;
#else
extern char etext;
intptr_t text_end = (intptr_t)&etext;
#endif
if (UNLIKELY(text_end <= text_start))
return 0;
if (UNLIKELY((start == NULL) || (end == NULL) || (text_start >= text_end)))
return 0;
*start = (char *)text_start;
*end = (char *)text_end;
return (size_t)(text_end - text_start);
}
/*
* stress_is_dev_tty()
* return true if fd is on a /dev/ttyN device. If it can't
* be determined than default to assuming it is.
*/
bool stress_is_dev_tty(const int fd)
{
#if defined(HAVE_TTYNAME)
const char *name = ttyname(fd);
if (UNLIKELY(!name))
return true;
return !strncmp("/dev/tty", name, 8);
#else
UNEXPECTED
(void)fd;
/* Assume it is */
return true;
#endif
}
/*
* stress_dirent_list_free()
* free dirent list
*/
void stress_dirent_list_free(struct dirent **dlist, const int n)
{
if (LIKELY(dlist != NULL)) {
int i;
for (i = 0; i < n; i++) {
if (dlist[i])
free(dlist[i]);
}
free(dlist);
}
}
/*
* stress_dirent_list_prune()
* remove . and .. files from directory list
*/
int stress_dirent_list_prune(struct dirent **dlist, const int n)
{
int i, j;
if (UNLIKELY(!dlist))
return -1;
for (i = 0, j = 0; i < n; i++) {
if (dlist[i]) {
if (stress_is_dot_filename(dlist[i]->d_name)) {
free(dlist[i]);
dlist[i] = NULL;
} else {
dlist[j] = dlist[i];
j++;
}
}
}
return j;
}
/*
* stress_warn_once_hash()
* computes a hash for a filename and a line and stores it,
* returns true if this is the first time this has been
* called for that specific filename and line
*
* Without libpthread this is potentially racy.
*/
bool stress_warn_once_hash(const char *filename, const int line)
{
uint32_t free_slot, i, j, h = (stress_hash_pjw(filename) + (uint32_t)line);
bool not_warned_yet = true;
if (UNLIKELY(!g_shared))
return true;
if (stress_lock_acquire(g_shared->warn_once.lock) < 0)
return true;
free_slot = STRESS_WARN_HASH_MAX;
/*
* Ensure hash is never zero so that it does not
* match and empty slot value of zero
*/
if (h == 0)
h += STRESS_WARN_HASH_MAX;
j = h % STRESS_WARN_HASH_MAX;
for (i = 0; i < STRESS_WARN_HASH_MAX; i++) {
if (g_shared->warn_once.hash[j] == h) {
not_warned_yet = false;
goto unlock;
}
if ((free_slot == STRESS_WARN_HASH_MAX) &&
(g_shared->warn_once.hash[j] == 0)) {
free_slot = j;
}
j = (j + 1) % STRESS_WARN_HASH_MAX;
}
if (free_slot != STRESS_WARN_HASH_MAX) {
g_shared->warn_once.hash[free_slot] = h;
}
unlock:
stress_lock_release(g_shared->warn_once.lock);
return not_warned_yet;
}
/*
* stress_ipv4_checksum()
* ipv4 data checksum
*/
uint16_t PURE OPTIMIZE3 stress_ipv4_checksum(uint16_t *ptr, const size_t sz)
{
register uint32_t sum = 0;
register size_t n = sz;
if (UNLIKELY(!ptr))
return 0;
while (n > 1) {
sum += *ptr++;
n -= 2;
}
if (n)
sum += *(uint8_t*)ptr;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return (uint16_t)~sum;
}
/*
* stress_uid_comp()
* uid comparison for sorting
*/
#if defined(HAVE_SETPWENT) && \
defined(HAVE_GETPWENT) && \
defined(HAVE_ENDPWENT) && \
!defined(BUILD_STATIC)
static PURE int stress_uid_comp(const void *p1, const void *p2)
{
const uid_t *uid1 = (const uid_t *)p1;
const uid_t *uid2 = (const uid_t *)p2;
if (*uid1 > *uid2)
return 1;
else if (*uid1 < *uid2)
return -1;
else
return 0;
}
/*
* stress_get_unused_uid()
* find the lowest free unused UID greater than 250,
* returns -1 if it can't find one and uid is set to 0;
* if successful it returns 0 and sets uid to the free uid.
*
* This also caches the uid so this can be called
* frequently. If the cached uid is in use it will
* perform the expensive lookup again.
*/
int stress_get_unused_uid(uid_t *uid)
{
static uid_t cached_uid = 0;
uid_t *uids;
if (!uid)
return -1;
*uid = 0;
/*
* If we have a cached unused uid and it's no longer
* unused then force a rescan for a new one
*/
if ((cached_uid != 0) && (getpwuid(cached_uid) != NULL))
cached_uid = 0;
if (cached_uid == 0) {
struct passwd *pw;
size_t i, n;
setpwent();
for (n = 0; getpwent() != NULL; n++) {
}
endpwent();
uids = (uid_t *)calloc(n, sizeof(*uids));
if (!uids)
return -1;
setpwent();
for (i = 0; i < n && (pw = getpwent()) != NULL; i++) {
uids[i] = pw->pw_uid;
}
endpwent();
n = i;
qsort(uids, n, sizeof(*uids), stress_uid_comp);
/* Look for a suitable gap from uid 250 upwards */
for (i = 0; i < n - 1; i++) {
/*
* Add a large gap in case new uids
* are added to reduce free uid race window
*/
const uid_t uid_try = uids[i] + 250;
if (uids[i + 1] > uid_try) {
if (getpwuid(uid_try) == NULL) {
cached_uid = uid_try;
break;
}
}
}
free(uids);
}
/*
* Not found?
*/
if (cached_uid == 0)
return -1;
*uid = cached_uid;
return 0;
}
#else
int stress_get_unused_uid(uid_t *uid)
{
if (uid)
*uid = 0;
return -1;
}
#endif
/*
* stress_read_discard(cont int fd)
* read and discard contents of file fd
*/
ssize_t stress_read_discard(const int fd)
{
ssize_t rbytes = 0, ret;
do {
char buffer[4096];
ret = read(fd, buffer, sizeof(buffer));
if (ret > 0)
rbytes += ret;
} while (ret > 0);
return rbytes;
}
/*
* stress_read_buffer()
* In addition to read() this function makes sure all bytes have been
* read. You're also able to ignore EINTR signals which could happen
* on alarm() in the parent process.
*/
ssize_t stress_read_buffer(
const int fd,
void *buffer,
const ssize_t size,
const bool ignore_sig_eintr)
{
ssize_t rbytes = 0, ret;
if (UNLIKELY(!buffer || (size < 1)))
return -1;
do {
char *ptr = ((char *)buffer) + rbytes;
ignore_eintr:
ret = read(fd, (void *)ptr, (size_t)(size - rbytes));
if (ignore_sig_eintr && (ret < 0) && (errno == EINTR))
goto ignore_eintr;
if (ret > 0)
rbytes += ret;
} while ((ret > 0) && (rbytes != size));
return (ret <= 0) ? ret : rbytes;
}
/*
* stress_write_buffer()
* In addition to write() this function makes sure all bytes have been
* written. You're also able to ignore EINTR interrupts which could happen
* on alarm() in the parent process.
*/
ssize_t stress_write_buffer(
const int fd,
const void *buffer,
const ssize_t size,
const bool ignore_sig_eintr)
{
ssize_t wbytes = 0, ret;
if (UNLIKELY(!buffer || (size < 1)))
return -1;
do {
const void *ptr = (void *)((uintptr_t)buffer + wbytes);
ignore_eintr:
ret = write(fd, ptr, (size_t)(size - wbytes));
/* retry if interrupted */
if (ignore_sig_eintr && (ret < 0) && (errno == EINTR))
goto ignore_eintr;
if (ret > 0)
wbytes += ret;
} while ((ret > 0) && (wbytes != size));
return (ret <= 0) ? ret : wbytes;
}
/*
* stress_kernel_release()
* turn release major.minor.patchlevel triplet into base 100 value
*/
int PURE stress_kernel_release(const int major, const int minor, const int patchlevel)
{
return (major * 10000) + (minor * 100) + patchlevel;
}
/*
* stress_get_kernel_release()
* return kernel release number in base 100, e.g.
* 4.15.2 -> 401502, return -1 if failed.
*/
int stress_get_kernel_release(void)
{
#if defined(HAVE_UNAME) && \
defined(HAVE_SYS_UTSNAME_H)
struct utsname buf;
int major = 0, minor = 0, patchlevel = 0;
if (UNLIKELY(uname(&buf) < 0))
return -1;
if (sscanf(buf.release, "%d.%d.%d\n", &major, &minor, &patchlevel) < 1)
return -1;
return stress_kernel_release(major, minor, patchlevel);
#else
UNEXPECTED
return -1;
#endif
}
/*
* stress_get_unused_pid_racy()
* try to find an unused pid. This is racy and may actually
* return pid that is unused at test time but will become
* used by the time the pid is accessed.
*/
pid_t stress_get_unused_pid_racy(const bool fork_test)
{
#if defined(PID_MAX_LIMIT)
pid_t max_pid = STRESS_MAXIMUM(PID_MAX_LIMIT, 1024);
#elif defined(PID_MAX)
pid_t max_pid = STRESS_MAXIMUM(PID_MAX, 1024);
#elif defined(PID_MAX_DEFAULT)
pid_t max_pid = STRESS_MAXIMUM(PID_MAX_DEFAULT, 1024);
#else
pid_t max_pid = 32767;
#endif
int i;
pid_t pid;
uint32_t n;
char buf[64];
/*
* Create a child, terminate it, use this pid as an unused
* pid. Slow but should be OK if system doesn't recycle PIDs
* quickly.
*/
if (fork_test) {
pid = fork();
if (pid == 0) {
_exit(0);
} else if (pid > 0) {
int status;
pid_t ret;
ret = waitpid(pid, &status, 0);
if ((ret == pid) &&
((shim_kill(pid, 0) < 0) && (errno == ESRCH))) {
return pid;
}
}
}
/*
* Make a random PID guess.
*/
n = (uint32_t)max_pid - 1023;
for (i = 0; i < 10; i++) {
pid = (pid_t)stress_mwc32modn(n) + 1023;
if ((shim_kill(pid, 0) < 0) && (errno == ESRCH))
return pid;
}
(void)shim_memset(buf, 0, sizeof(buf));
if (stress_system_read("/proc/sys/kernel/pid_max", buf, sizeof(buf) - 1) > 0)
max_pid = STRESS_MAXIMUM(atoi(buf), 1024);
n = (uint32_t)max_pid - 1023;
for (i = 0; i < 10; i++) {
pid = (pid_t)stress_mwc32modn(n) + 1023;
if ((shim_kill(pid, 0) < 0) && (errno == ESRCH))
return pid;
}
/*
* Give up.
*/
return max_pid;
}
/*
* stress_read_fdinfo()
* read the fdinfo for a specific pid's fd, Linux only
*/
int stress_read_fdinfo(const pid_t pid, const int fd)
{
#if defined(__linux__)
char path[PATH_MAX];
char buf[4096];
(void)snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d",
(int)pid, fd);
return (int)stress_system_read(path, buf, sizeof(buf));
#else
(void)pid;
(void)fd;
return 0;
#endif
}
/*
* stress_get_hostname_length()
* return the maximum allowed hostname length
*/
size_t stress_get_hostname_length(void)
{
#if defined(HOST_NAME_MAX)
return HOST_NAME_MAX + 1;
#elif defined(HAVE_UNAME) && \
defined(HAVE_SYS_UTSNAME_H)
struct utsname uts;
return sizeof(uts.nodename); /* Linux */
#else
return 255 + 1; /* SUSv2 */
#endif
}
/*
* stress_get_min_aux_sig_stack_size()
* For ARM we should check AT_MINSIGSTKSZ as this
* also includes SVE register saving overhead
* https://blog.linuxplumbersconf.org/2017/ocw/system/presentations/4671/original/plumbers-dm-2017.pdf
*/
static inline long int stress_get_min_aux_sig_stack_size(void)
{
#if defined(HAVE_SYS_AUXV_H) && \
defined(HAVE_GETAUXVAL) && \
defined(AT_MINSIGSTKSZ)
const long int sz = (long int)getauxval(AT_MINSIGSTKSZ);
if (LIKELY(sz > 0))
return sz;
#else
UNEXPECTED
#endif
return -1;
}
/*
* stress_get_sig_stack_size()
* wrapper for STRESS_SIGSTKSZ, try and find
* stack size required
*/
size_t stress_get_sig_stack_size(void)
{
static long int sz = -1;
long int min;
#if defined(_SC_SIGSTKSZ) || \
defined(SIGSTKSZ)
long int tmp;
#endif
/* return cached copy */
if (LIKELY(sz > 0))
return (size_t)sz;
min = stress_get_min_aux_sig_stack_size();
#if defined(_SC_SIGSTKSZ)
tmp = sysconf(_SC_SIGSTKSZ);
if (tmp > 0)
min = STRESS_MAXIMUM(tmp, min);
#endif
#if defined(SIGSTKSZ)
tmp = SIGSTKSZ;
if (tmp > 0)
min = STRESS_MAXIMUM(tmp, min);
#endif
sz = STRESS_MAXIMUM(STRESS_ABS_MIN_STACK_SIZE, min);
return (size_t)sz;
}
/*
* stress_get_min_sig_stack_size()
* wrapper for STRESS_MINSIGSTKSZ
*/
size_t stress_get_min_sig_stack_size(void)
{
static long int sz = -1;
long int min;
#if defined(_SC_MINSIGSTKSZ) || \
defined(SIGSTKSZ)
long int tmp;
#endif
/* return cached copy */
if (sz > 0)
return (size_t)sz;
min = stress_get_min_aux_sig_stack_size();
#if defined(_SC_MINSIGSTKSZ)
tmp = sysconf(_SC_MINSIGSTKSZ);
if (tmp > 0)
min = STRESS_MAXIMUM(tmp, min);
#endif
#if defined(SIGSTKSZ)
tmp = SIGSTKSZ;
if (tmp > 0)
min = STRESS_MAXIMUM(tmp, min);
#endif
sz = STRESS_MAXIMUM(STRESS_ABS_MIN_STACK_SIZE, min);
return (size_t)sz;
}
/*
* stress_get_min_pthread_stack_size()
* return the minimum size of stack for a pthread
*/
size_t stress_get_min_pthread_stack_size(void)
{
static long int sz = -1;
long int min, tmp;
/* return cached copy */
if (sz > 0)
return (size_t)sz;
min = stress_get_min_aux_sig_stack_size();
#if defined(__SC_THREAD_STACK_MIN_VALUE)
tmp = sysconf(__SC_THREAD_STACK_MIN_VALUE);
if (tmp > 0)
min = STRESS_MAXIMUM(tmp, min);
#endif
#if defined(_SC_THREAD_STACK_MIN_VALUE)
tmp = sysconf(_SC_THREAD_STACK_MIN_VALUE);
if (tmp > 0)
min = STRESS_MAXIMUM(tmp, min);
#endif
#if defined(PTHREAD_STACK_MIN)
tmp = PTHREAD_STACK_MIN;
if (tmp > 0)
tmp = STRESS_MAXIMUM(tmp, 8192);
else
tmp = 8192;
#else
tmp = 8192;
#endif
sz = STRESS_MAXIMUM(tmp, min);
return (size_t)sz;
}
/*
* stress_sig_handler_exit()
* signal handler that exits a process via _exit(0) for
* immediate dead stop termination.
*/
void NORETURN MLOCKED_TEXT stress_sig_handler_exit(int signum)
{
(void)signum;
_exit(0);
}
/*
* __stack_chk_fail()
* override stack smashing callback
*/
#if defined(HAVE_COMPILER_GCC_OR_MUSL) && \
!defined(HAVE_COMPILER_CLANG) && \
defined(HAVE_WEAK_ATTRIBUTE)
extern void __stack_chk_fail(void);
NORETURN WEAK void __stack_chk_fail(void)
{
if (stress_stack_check_flag) {
(void)fprintf(stderr, "Stack overflow detected! Aborting stress-ng.\n");
(void)fflush(stderr);
abort();
}
/* silently exit */
_exit(0);
}
#endif
/*
* stress_set_stack_smash_check_flag()
* set flag, true = report flag, false = silently ignore
*/
void stress_set_stack_smash_check_flag(const bool flag)
{
stress_stack_check_flag = flag;
}
static inline bool stress_is_a_pipe(const int fd)
{
struct stat statbuf;
if (shim_fstat(fd, &statbuf) != 0)
return false;
if (S_ISFIFO(statbuf.st_mode))
return true;
return false;
}
/*
* stress_get_tty_width()
* get tty column width
*/
int stress_get_tty_width(void)
{
const int default_width = 80;
#if defined(HAVE_WINSIZE) && \
defined(TIOCGWINSZ)
struct winsize ws;
int ret, fd;
if (stress_is_a_pipe(fileno(stdout)))
fd = fileno(stdin);
else
fd = fileno(stdout);
ret = ioctl(fd, TIOCGWINSZ, &ws);
if (UNLIKELY(ret < 0))
return default_width;
ret = (int)ws.ws_col;
if (UNLIKELY((ret <= 0) || (ret > 1024)))
return default_width;
return ret;
#else
return default_width;
#endif
}
/*
* stress_get_extents()
* try to determine number extents in a file
*/
size_t stress_get_extents(const int fd)
{
#if defined(FS_IOC_FIEMAP) && \
defined(HAVE_LINUX_FIEMAP_H)
struct fiemap fiemap;
(void)shim_memset(&fiemap, 0, sizeof(fiemap));
fiemap.fm_length = ~0UL;
/* Find out how many extents there are */
if (ioctl(fd, FS_IOC_FIEMAP, &fiemap) < 0)
return 0;
return fiemap.fm_mapped_extents;
#else
UNEXPECTED
(void)fd;
return 0;
#endif
}
/*
* stress_redo_fork()
* check fork errno (in err) and return true if
* an immediate fork can be retried due to known
* error cases that are retryable. Also force a
* scheduling yield.
*/
bool stress_redo_fork(stress_args_t *args, const int err)
{
/* Timed out! */
if (UNLIKELY(stress_time_now() > args->time_end)) {
stress_continue_set_flag(false);
return false;
}
/* More bogo-ops to go and errors indicate a fork retry? */
if (LIKELY(stress_continue(args)) &&
((err == EAGAIN) || (err == EINTR) || (err == ENOMEM))) {
(void)shim_sched_yield();
return true;
}
return false;
}
/*
* stress_sighandler_nop()
* no-operation signal handler
*/
void stress_sighandler_nop(int sig)
{
(void)sig;
}
/*
* stress_clear_warn_once()
* clear the linux warn once warnings flag, kernel warn once
* messages can be re-issued
*/
void stress_clear_warn_once(void)
{
#if defined(__linux__)
if (stress_check_capability(SHIM_CAP_IS_ROOT))
(void)stress_system_write("/sys/kernel/debug/clear_warn_once", "1", 1);
#endif
}
/*
* stress_flag_permutation()
* given flag mask in flags, generate all possible permutations
* of bit flags. e.g.
* flags = 0x81;
* -> b00000000
* b00000001
* b10000000
* b10000001
*/
size_t stress_flag_permutation(const int flags, int **permutations)
{
unsigned int flag_bits;
unsigned int n_bits;
register unsigned int j, n_flags;
int *perms;
if (UNLIKELY(!permutations))
return 0;
*permutations = NULL;
for (n_bits = 0, flag_bits = (unsigned int)flags; flag_bits; flag_bits >>= 1U)
n_bits += (flag_bits & 1U);
if (n_bits > STRESS_MAX_PERMUTATIONS)
n_bits = STRESS_MAX_PERMUTATIONS;
n_flags = 1U << n_bits;
perms = (int *)calloc((size_t)n_flags, sizeof(*perms));
if (UNLIKELY(!perms))
return 0;
/*
* Generate all the possible flag settings in order
*/
for (j = 0; j < n_flags; j++) {
register int i;
register unsigned int j_mask = 1U;
for (i = 0; i < 32; i++) {
const int i_mask = (int)(1U << i);
if (flags & i_mask) {
if (j & j_mask)
perms[j] |= i_mask;
j_mask <<= 1U;
}
}
}
*permutations = perms;
return (size_t)n_flags;
}
#if defined(HAVE_LINUX_MAGIC_H) && \
defined(HAVE_SYS_STATFS_H)
/*
* stress_fs_magic_to_name()
* return the human readable file system type based on fs type magic
*/
static const char *stress_fs_magic_to_name(const unsigned long int fs_magic)
{
static char unknown[32];
size_t i;
for (i = 0; i < SIZEOF_ARRAY(stress_fs_names); i++) {
if (stress_fs_names[i].fs_magic == fs_magic)
return stress_fs_names[i].fs_name;
}
(void)snprintf(unknown, sizeof(unknown), "unknown 0x%lx", fs_magic);
return unknown;
}
#endif
#if defined(HAVE_SYS_SYSMACROS_H) && \
defined(__linux__)
/*
* stress_find_partition_dev()
* find major device name of device with major/minor number
* via the partition info
*/
static bool stress_find_partition_dev(
const unsigned int devmajor,
const unsigned int devminor,
char *name,
const size_t name_len)
{
char buf[1024];
FILE *fp;
bool found = false;
if (!name)
return false;
if (name_len < 1)
return false;
*name = '\0';
fp = fopen("/proc/partitions", "r");
if (!fp)
return false;
while (fgets(buf, sizeof(buf), fp) != NULL) {
uint64_t blocks;
char devname[name_len + 1];
unsigned int pmajor, pminor;
if (sscanf(buf, "%u %u %" SCNu64 " %128s", &pmajor, &pminor, &blocks, devname) == 4) {
if ((devmajor == pmajor) && (devminor == pminor)) {
(void)shim_strscpy(name, devname, name_len);
found = true;
break;
}
}
}
(void)fclose(fp);
return found;
}
#endif
/*
* stress_get_fs_dev_model()
* file model name of device that the file is on
*/
static const char *stress_get_fs_dev_model(const char *filename)
{
#if defined(HAVE_SYS_SYSMACROS_H) && \
defined(__linux__)
struct stat statbuf;
static char buf[256];
char path[PATH_MAX];
if (UNLIKELY(!filename))
return NULL;
if (UNLIKELY(shim_stat(filename, &statbuf) < 0))
return NULL;
if (!stress_find_partition_dev(major(statbuf.st_dev), 0, buf, sizeof(buf)))
return NULL;
(void)snprintf(path, sizeof(path), "/sys/block/%s/device/model", buf);
if (stress_system_read(path, buf, sizeof(buf)) > 0) {
char *ptr;
for (ptr = buf; *ptr; ptr++) {
if (*ptr == '\n') {
*ptr = '\0';
ptr--;
break;
}
}
while (ptr >= buf && *ptr == ' ') {
*ptr = '\0';
ptr--;
}
return buf;
}
return NULL;
#else
(void)filename;
return NULL;
#endif
}
/*
* stress_get_fs_info()
* for a given filename, determine the filesystem it is stored
* on and return filesystem type and number of blocks
*/
const char *stress_get_fs_info(const char *filename, uintmax_t *blocks)
{
#if defined(HAVE_LINUX_MAGIC_H) && \
defined(HAVE_SYS_STATFS_H)
struct statfs buf;
*blocks = (intmax_t)0;
if (UNLIKELY(!filename))
return NULL;
if (UNLIKELY(statfs(filename, &buf) != 0))
return NULL;
*blocks = (uintmax_t)buf.f_bavail;
return stress_fs_magic_to_name((unsigned long int)buf.f_type);
#elif (defined(__FreeBSD__) && \
defined(HAVE_SYS_MOUNT_H) && \
defined(HAVE_SYS_PARAM_H)) || \
(defined(__OpenBSD__) && \
defined(HAVE_SYS_MOUNT_H))
struct statfs buf;
static char tmp[80];
*blocks = (intmax_t)0;
if (statfs(filename, &buf) != 0)
return NULL;
*blocks = (uintmax_t)buf.f_bavail;
(void)shim_strscpy(tmp, buf.f_fstypename, sizeof(tmp));
return tmp;
#else
(void)filename;
*blocks = (intmax_t)0;
return NULL;
#endif
}
/*
* stress_get_fs_type()
* return the file system type that the given filename is in
*/
const char *stress_get_fs_type(const char *filename)
{
uintmax_t blocks;
const char *fs_name = stress_get_fs_info(filename, &blocks);
const char *model = stress_get_fs_dev_model(filename);
if (fs_name) {
static char tmp[256];
(void)snprintf(tmp, sizeof(tmp), ", filesystem type: %s (%" PRIuMAX" blocks available%s%s)",
fs_name, blocks,
model ? ", " : "",
model ? model : "");
return tmp;
}
return "";
}
/*
* Indicate a stress test failed because of limited resources
* rather than a failure of the tests during execution.
* err is the errno of the failure.
*/
int PURE stress_exit_status(const int err)
{
switch (err) {
case ENOMEM:
case ENOSPC:
return EXIT_NO_RESOURCE;
case ENOSYS:
return EXIT_NOT_IMPLEMENTED;
}
return EXIT_FAILURE; /* cppcheck-suppress ConfigurationNotChecked */
}
/*
* stress_get_proc_self_exe_path()
* get process' executable path via readlink
*/
static char *stress_get_proc_self_exe_path(char *path, const char *proc_path, const size_t path_len)
{
ssize_t len;
if (UNLIKELY(!path || !proc_path))
return NULL;
len = shim_readlink(proc_path, path, path_len);
if (UNLIKELY((len < 0) || (len >= PATH_MAX)))
return NULL;
path[len] = '\0';
return path;
}
/*
* stress_get_proc_self_exe()
* determine the path to the executable, return NULL if not possible/failed
*/
char *stress_get_proc_self_exe(char *path, const size_t path_len)
{
#if defined(__linux__)
return stress_get_proc_self_exe_path(path, "/proc/self/exe", path_len);
#elif defined(__NetBSD__)
return stress_get_proc_self_exe_path(path, "/proc/curproc/exe", path_len);
#elif defined(__DragonFly__)
return stress_get_proc_self_exe_path(path, "/proc/curproc/file", path_len);
#elif defined(__FreeBSD__)
#if defined(CTL_KERN) && \
defined(KERN_PROC) && \
defined(KERN_PROC_PATHNAME)
static int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
size_t tmp_path_len = path_len;
int ret;
if (UNLIKELY(!path))
return NULL;
ret = sysctl(mib, SIZEOF_ARRAY(mib), (void *)path, &tmp_path_len, NULL, 0);
if (ret < 0) {
/* fall back to procfs */
return stress_get_proc_self_exe_path(path, "/proc/curproc/file", path_len);
}
return path;
#else
/* fall back to procfs */
if (UNLIKELY(!path))
return NULL;
return stress_get_proc_self_exe_path(path, "/proc/curproc/file", path_len);
#endif
#elif defined(__sun__) && \
defined(HAVE_GETEXECNAME)
const char *execname = getexecname();
if (UNLIKELY(!path))
return NULL;
(void)stress_get_proc_self_exe_path;
if (UNLIKELY(!execname))
return NULL;
/* Need to perform a string copy to deconstify execname */
(void)shim_strscpy(path, execname, path_len);
return path;
#elif defined(HAVE_PROGRAM_INVOCATION_NAME)
if (UNLIKELY(!path))
return NULL;
(void)stress_get_proc_self_exe_path;
/* this may return the wrong name if it's been argv modified */
(void)shim_strscpy(path, program_invocation_name, path_len);
return path;
#else
if (UNLIKELY(!path))
return NULL;
(void)stress_get_proc_self_exe_path;
(void)path;
(void)path_len;
return NULL;
#endif
}
#if defined(__FreeBSD__) || \
defined(__NetBSD__) || \
defined(__APPLE__)
/*
* stress_bsd_getsysctl()
* get sysctl using name, ptr to obj, size = size of obj
*/
int stress_bsd_getsysctl(const char *name, void *ptr, size_t size)
{
int ret;
size_t nsize = size;
if (UNLIKELY(!ptr || !name))
return -1;
(void)shim_memset(ptr, 0, size);
ret = sysctlbyname(name, ptr, &nsize, NULL, 0);
if ((ret < 0) || (nsize != size)) {
(void)shim_memset(ptr, 0, size);
return -1;
}
return 0;
}
/*
* stress_bsd_getsysctl_uint64()
* get sysctl by name, return uint64 value
*/
uint64_t stress_bsd_getsysctl_uint64(const char *name)
{
uint64_t val;
if (UNLIKELY(!name))
return 0ULL;
if (stress_bsd_getsysctl(name, &val, sizeof(val)) == 0)
return val;
return 0ULL;
}
/*
* stress_bsd_getsysctl_uint32()
* get sysctl by name, return uint32 value
*/
uint32_t stress_bsd_getsysctl_uint32(const char *name)
{
uint32_t val;
if (UNLIKELY(!name))
return 0UL;
if (stress_bsd_getsysctl(name, &val, sizeof(val)) == 0)
return val;
return 0UL;
}
/*
* stress_bsd_getsysctl_uint()
* get sysctl by name, return unsigned int value
*/
unsigned int stress_bsd_getsysctl_uint(const char *name)
{
unsigned int val;
if (UNLIKELY(!name))
return 0;
if (stress_bsd_getsysctl(name, &val, sizeof(val)) == 0)
return val;
return 0;
}
/*
* stress_bsd_getsysctl_int()
* get sysctl by name, return int value
*/
int stress_bsd_getsysctl_int(const char *name)
{
int val;
if (UNLIKELY(!name))
return 0;
if (stress_bsd_getsysctl(name, &val, sizeof(val)) == 0)
return val;
return 0;
}
#else
int PURE stress_bsd_getsysctl(const char *name, void *ptr, size_t size)
{
(void)name;
(void)ptr;
(void)size;
return 0;
}
uint64_t PURE stress_bsd_getsysctl_uint64(const char *name)
{
(void)name;
return 0ULL;
}
uint32_t PURE stress_bsd_getsysctl_uint32(const char *name)
{
(void)name;
return 0UL;
}
unsigned int PURE stress_bsd_getsysctl_uint(const char *name)
{
(void)name;
return 0;
}
int PURE stress_bsd_getsysctl_int(const char *name)
{
(void)name;
return 0;
}
#endif
/*
* stress_close_fds()
* close an array of file descriptors
*/
void stress_close_fds(int *fds, const size_t n)
{
size_t i, j;
if (UNLIKELY(!fds))
return;
if (UNLIKELY(n < 1))
return;
qsort(fds, n, sizeof(*fds), stress_sort_cmp_fwd_int);
for (j = 0; j < n - 1; j++) {
if (fds[j] >= 0)
break;
}
for (i = j; i < n - 1; i++) {
if (fds[i] + 1 != fds[i + 1])
goto close_slow;
}
if (shim_close_range(fds[j], fds[n - 1], 0) == 0)
return;
close_slow:
for (i = j; i < n; i++)
(void)close(fds[i]);
}
/*
* stress_file_rw_hint_short()
* hint that file data opened on fd has short lifetime
*/
void stress_file_rw_hint_short(const int fd)
{
#if defined(F_SET_FILE_RW_HINT) && \
defined(RWH_WRITE_LIFE_SHORT)
uint64_t hint = RWH_WRITE_LIFE_SHORT;
VOID_RET(int, fcntl(fd, F_SET_FILE_RW_HINT, &hint));
#else
(void)fd;
#endif
}
/*
* stress_set_vma_anon_name()
* set a name to an anonymously mapped vma
*/
void stress_set_vma_anon_name(const void *addr, const size_t size, const char *name)
{
#if defined(HAVE_SYS_PRCTL_H) && \
defined(HAVE_PRCTL) && \
defined(PR_SET_VMA) && \
defined(PR_SET_VMA_ANON_NAME)
VOID_RET(int, prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME,
(unsigned long int)addr,
(unsigned long int)size,
(unsigned long int)name));
#else
(void)addr;
(void)size;
(void)name;
#endif
}
/*
* stress_x86_readmsr()
* 64 bit read an MSR on a specified x86 CPU
*/
int stress_x86_readmsr64(const int cpu, const uint32_t reg, uint64_t *val)
{
#if defined(STRESS_ARCH_X86)
char buffer[PATH_MAX];
uint64_t value = 0;
int fd;
ssize_t ret;
if (UNLIKELY(!val))
return -1;
*val = ~0ULL;
(void)snprintf(buffer, sizeof(buffer), "/dev/cpu/%d/msr", cpu);
if ((fd = open(buffer, O_RDONLY)) < 0)
return -1;
ret = pread(fd, &value, 8, reg);
(void)close(fd);
if (ret < 0)
return -1;
*val = value;
return 0;
#else
(void)cpu;
(void)reg;
(void)val;
if (val)
*val = ~0ULL;
return -1;
#endif
}
/*
* stress_unset_chattr_flags()
* disable all chattr flags including the immutable flas
*/
void stress_unset_chattr_flags(const char *pathname)
{
#if defined(__linux__) && \
defined(_IOW)
#define SHIM_EXT2_IMMUTABLE_FL 0x00000010
#define SHIM_EXT2_IOC_SETFLAGS _IOW('f', 2, long int)
int fd;
unsigned long int flags = 0;
if (UNLIKELY(!pathname))
return;
fd = open(pathname, O_RDONLY);
if (UNLIKELY(fd < 0))
return;
VOID_RET(int, ioctl(fd, SHIM_EXT2_IOC_SETFLAGS, &flags));
(void)close(fd);
#undef SHIM_EXT2_IMMUTABLE_FL
#undef SHIM_EXT2_IOC_SETFLAGS
#else
(void)pathname;
#endif
}
/*
* stress_munmap_retry_enomem()
* retry munmap on ENOMEM errors as these can be due
* to low memory not allowing memory to be released
*/
int stress_munmap_retry_enomem(void *addr, size_t length)
{
int ret, i;
for (i = 1; i <= 10; i++) {
int saved_errno;
ret = munmap(addr, length);
if (LIKELY(ret == 0))
break;
if (errno != ENOMEM)
break;
saved_errno = errno;
(void)shim_usleep(10000 * i);
errno = saved_errno;
}
return ret;
}
/*
* stress_swapoff()
* swapoff and retry if EINTR occurs
*/
int stress_swapoff(const char *path)
{
#if defined(HAVE_SYS_SWAP_H) && \
defined(HAVE_SWAP)
int i;
if (UNLIKELY(!path)) {
errno = EINVAL;
return -1;
}
for (i = 0; i < 25; i++) {
int ret;
errno = 0;
ret = swapoff(path);
if (ret == 0)
return ret;
if ((ret < 0) && (errno != EINTR))
break;
}
return -1;
#else
if (!path) {
errno = EINVAL;
return -1;
}
errno = ENOSYS;
return -1;
#endif
}
/*
* Filter out dot files . and ..
*/
static int PURE stress_dot_filter(const struct dirent *d)
{
if (d->d_name[0] == '.') {
if (d->d_name[1] == '\0')
return 0;
if ((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
return 0;
}
return 1;
}
/*
* stress_unset_inode_flags()
* unset the inode flag bits specified in flag
*/
static void stress_unset_inode_flags(const char *filename, const int flag)
{
#if defined(FS_IOC_SETFLAGS)
int fd;
const long int new_flag = 0;
if (UNLIKELY(!filename))
return;
fd = open(filename, O_RDWR | flag);
if (UNLIKELY(fd < 0))
return;
VOID_RET(int, ioctl(fd, FS_IOC_SETFLAGS, &new_flag));
(void)close(fd);
#else
(void)filename;
(void)flag;
#endif
}
/*
* stress_clean_dir_files()
* recursively delete files in directories
*/
static void stress_clean_dir_files(
const char *temp_path,
const size_t temp_path_len,
char *path,
const size_t path_posn)
{
struct stat statbuf;
char *ptr = path + path_posn;
const char *end = path + PATH_MAX;
int n;
struct dirent **names = NULL;
if (UNLIKELY(!temp_path || !path))
return;
if (UNLIKELY(shim_stat(path, &statbuf) < 0)) {
pr_dbg("stress-ng: failed to stat %s, errno=%d (%s)\n", path, errno, strerror(errno));
return;
}
/* We don't follow symlinks */
if (S_ISLNK(statbuf.st_mode))
return;
/* We don't remove paths with .. in */
if (strstr(path, ".."))
return;
/* We don't remove paths that our out of the scope */
if (strncmp(path, temp_path, temp_path_len))
return;
n = scandir(path, &names, stress_dot_filter, alphasort);
if (n < 0) {
(void)shim_rmdir(path);
return;
}
while (n--) {
size_t name_len = strlen(names[n]->d_name) + 1;
#if !defined(DT_DIR) || \
!defined(DT_LNK) || \
!defined(DT_REG)
int ret;
#endif
/* No more space */
if (ptr + name_len > end) {
free(names[n]);
continue;
}
(void)snprintf(ptr, (size_t)(end - ptr), "/%s", names[n]->d_name);
name_len = strlen(ptr);
#if defined(DT_DIR) && \
defined(DT_LNK) && \
defined(DT_REG)
/* Modern fast d_type method */
switch (names[n]->d_type) {
case DT_DIR:
free(names[n]);
#if defined(O_DIRECTORY)
stress_unset_inode_flags(temp_path, O_DIRECTORY);
#endif
stress_unset_chattr_flags(path);
stress_clean_dir_files(temp_path, temp_path_len, path, path_posn + name_len);
(void)shim_rmdir(path);
break;
case DT_LNK:
case DT_REG:
free(names[n]);
stress_unset_inode_flags(temp_path, 0);
stress_unset_chattr_flags(path);
if (strstr(path, "swap"))
(void)stress_swapoff(path);
(void)shim_unlink(path);
break;
default:
free(names[n]);
break;
}
#else
/* Slower stat method */
free(names[n]);
ret = shim_stat(path, &statbuf);
if (ret < 0)
continue;
if ((statbuf.st_mode & S_IFMT) == S_IFDIR) {
#if defined(O_DIRECTORY)
stress_unset_inode_flags(temp_path, O_DIRECTORY);
#endif
stress_unset_chattr_flags(temp_path);
stress_clean_dir_files(temp_path, temp_path_len, path, path_posn + name_len);
(void)shim_rmdir(path);
} else if (((statbuf.st_mode & S_IFMT) == S_IFLNK) ||
((statbuf.st_mode & S_IFMT) == S_IFREG)) {
stress_unset_inode_flags(temp_path, 0);
stress_unset_chattr_flags(temp_path);
(void)unlink(path);
}
#endif
}
*ptr = '\0';
free(names);
(void)shim_rmdir(path);
}
/*
* stress_clean_dir()
* perform tidy up of any residual temp files; this
* happens if a stressor was terminated before it could
* tidy itself up, e.g. OOM'd or KILL'd
*/
void stress_clean_dir(
const char *name,
const pid_t pid,
const uint32_t instance)
{
char path[PATH_MAX];
const char *temp_path = stress_get_temp_path();
const size_t temp_path_len = strlen(temp_path);
if (LIKELY(name != NULL)) {
(void)stress_temp_dir(path, sizeof(path), name, pid, instance);
if (access(path, F_OK) == 0) {
pr_dbg("%s: removing temporary files in %s\n", name, path);
stress_clean_dir_files(temp_path, temp_path_len, path, strlen(path));
}
}
}
/*
* stress_random_small_sleep()
* 0..5000 us sleep, used in pthreads to add some
* small delay into startup to randomize any racy
* conditions
*/
void stress_random_small_sleep(void)
{
shim_usleep_interruptible(stress_mwc32modn(5000));
}
/*
* stress_yield_sleep_ms()
* force a yield, sleep if the yield was less than 1ms,
* and repeat if sleep was less than 1ms
*/
void stress_yield_sleep_ms(void)
{
const double t = stress_time_now();
do {
double duration;
(void)shim_sched_yield();
duration = stress_time_now() - t;
if (duration > 0.001)
break;
(void)shim_usleep(1000);
} while (stress_continue_flag());
}
static void stress_dbg(const char *fmt, ...) FORMAT(printf, 1, 2);
/*
* stress_dbg()
* simple debug, messages must be less than 256 bytes
*/
static void stress_dbg(const char *fmt, ...)
{
va_list ap;
int n, sz;
static char buf[256];
n = snprintf(buf, sizeof(buf), "stress-ng: debug: [%" PRIdMAX"] ", (intmax_t)getpid());
if (UNLIKELY(n < 0))
return;
sz = n;
va_start(ap, fmt);
n = vsnprintf(buf + sz, sizeof(buf) - sz, fmt, ap);
va_end(ap);
sz += n;
VOID_RET(ssize_t, write(fileno(stdout), buf, (size_t)sz));
}
/*
* stress_addr_readable()
* portable way to check if memory addr[0]..addr[len - 1] is readable,
* create pipe, see if write of the memory range works, failure (with
* EFAULT) will be used to indicate address range is not readable.
*/
bool stress_addr_readable(const void *addr, const size_t len)
{
int fds[2];
bool ret = false;
if (UNLIKELY(pipe(fds) < 0))
return ret;
if (write(fds[1], addr, len) == (ssize_t)len)
ret = true;
(void)close(fds[0]);
(void)close(fds[1]);
return ret;
}
/*
* stress_dump_data()
* dump to stdout 16 bytes of data code if it is readable. SIGILL address
* data is indicated with < > around it.
*/
static void stress_dump_data(
const uint8_t *addr,
const uint8_t *fault_addr,
const size_t len)
{
char buf[128];
if (stress_addr_readable(addr, len)) {
size_t i;
bool show_opcode = false;
int n, sz = 0;
n = snprintf(buf + sz, sizeof(buf) - sz, "stress-ng: info: 0x%16.16" PRIxPTR ":", (uintptr_t)addr);
if (n < 0)
return;
sz += n;
for (i = 0; i < len; i++) {
if (&addr[i] == fault_addr) {
n = snprintf(buf + sz, sizeof(buf) - sz, "<%-2.2x>", addr[i]);
if (n < 0)
return;
sz += n;
show_opcode = true;
} else {
n = snprintf(buf + sz, sizeof(buf) - sz, "%s%-2.2x", show_opcode ? "" : " ", addr[i]);
if (n < 0)
return;
sz += n;
show_opcode = false;
}
}
stress_dbg("%s\n", buf);
} else {
stress_dbg("stress-ng: info: 0x%16.16" PRIxPTR " not readable\n", (uintptr_t)addr);
}
}
/*
* stress_catch_sig_si_code()
* convert signal and si_code into human readable form
*/
static const PURE char *stress_catch_sig_si_code(const int sig, const int sig_code)
{
static const char unknown[] = "UNKNOWN";
switch (sig) {
case SIGILL:
switch (sig_code) {
#if defined(ILL_ILLOPC)
case ILL_ILLOPC:
return "ILL_ILLOPC";
#endif
#if defined(ILL_ILLOPN)
case ILL_ILLOPN:
return "ILL_ILLOPN";
#endif
#if defined(ILL_ILLADR)
case ILL_ILLADR:
return "ILL_ILLADR";
#endif
#if defined(ILL_ILLTRP)
case ILL_ILLTRP:
return "ILL_ILLTRP";
#endif
#if defined(ILL_PRVOPC)
case ILL_PRVOPC:
return "ILL_PRVOPC";
#endif
#if defined(ILL_PRVREG)
case ILL_PRVREG:
return "ILL_PRVREG";
#endif
#if defined(ILL_COPROC)
case ILL_COPROC:
return "ILL_COPROC";
#endif
#if defined(ILL_BADSTK)
case ILL_BADSTK:
return "ILL_BADSTK";
#endif
default:
return unknown;
}
break;
case SIGSEGV:
switch (sig_code) {
#if defined(SEGV_MAPERR)
case SEGV_MAPERR:
return "SEGV_MAPERR";
#endif
#if defined(SEGV_ACCERR)
case SEGV_ACCERR:
return "SEGV_ACCERR";
#endif
#if defined(SEGV_BNDERR)
case SEGV_BNDERR:
return "SEGV_BNDERR";
#endif
#if defined(SEGV_PKUERR)
case SEGV_PKUERR:
return "SEGV_PKUERR";
#endif
default:
return unknown;
}
break;
}
return unknown;
}
/*
* stress_dump_readable_data()
* 3 lines of memory hexdump, aligned to 16 bytes boundary
*/
static void stress_dump_readable_data(uint8_t *fault_addr)
{
int i;
uint8_t *addr = (uint8_t *)((uintptr_t)fault_addr & ~0xf);
for (i = 0; i < 3; i++, addr += 16) {
stress_dump_data(addr, fault_addr, 16);
}
}
/*
* stress_dump_map_info()
* find fault address in /proc/self/maps, dump out map info
*/
static void stress_dump_map_info(uint8_t *fault_addr)
{
#if defined(__linux__)
FILE *fp;
char buf[1024];
fp = fopen("/proc/self/maps", "r");
if (UNLIKELY(!fp))
return;
while ((fgets(buf, sizeof(buf), fp)) != NULL) {
uintptr_t begin, end;
if (sscanf(buf, "%" SCNxPTR "-%" SCNxPTR, &begin, &end) == 2) {
if (((uintptr_t)fault_addr >= begin) &&
((uintptr_t)fault_addr <= end)) {
char *ptr1, *ptr2;
/* truncate to first \n found */
ptr1 = strchr(buf, (int)'\n');
if (ptr1)
*ptr1 = '\0';
/* squeeze out duplicated spaces */
for (ptr1 = buf, ptr2 = buf; *ptr1; ptr1++) {
if ((*ptr1 == ' ') && (*(ptr1 + 1) == ' '))
continue;
*ptr2 = *ptr1;
ptr2++;
}
*ptr2 = '\0';
stress_dbg("stress-ng: info: %s\n", buf);
break;
}
}
}
(void)fclose(fp);
#else
(void)fault_addr;
#endif
}
/*
* stress_catch_sig_handler()
* handle signal, dump 16 bytes before and after the illegal opcode
* and terminate immediately to avoid any recursive signal handling
*/
static void stress_catch_sig_handler(
int sig,
siginfo_t *info,
void *ucontext,
const int sig_expected,
const char *sig_expected_name)
{
static bool handled = false;
(void)sig;
(void)ucontext;
if (handled)
_exit(EXIT_FAILURE);
handled = true;
if (sig == sig_expected) {
if (info) {
stress_dbg("caught %s, address 0x%16.16" PRIxPTR " (%s)\n",
sig_expected_name, (uintptr_t)info->si_addr,
stress_catch_sig_si_code(sig, info->si_code));
stress_dump_readable_data((uint8_t *)info->si_addr);
stress_dump_map_info((uint8_t *)info->si_addr);
} else {
stress_dbg("caught %s, unknown address\n", sig_expected_name);
}
} else {
if (info) {
stress_dbg("caught unexpected SIGNAL %d, address 0x%16.16" PRIxPTR "\n",
sig, (uintptr_t)info->si_addr);
stress_dump_readable_data((uint8_t *)info->si_addr);
stress_dump_map_info((uint8_t *)info->si_addr);
} else {
stress_dbg("caught unexpected SIGNAL %d, unknown address\n", sig);
}
}
/* Big fat abort */
_exit(EXIT_FAILURE);
}
/*
* stress_catch_sigill_handler()
* handler for SIGILL
*/
static void stress_catch_sigill_handler(
int sig,
siginfo_t *info,
void *ucontext)
{
stress_catch_sig_handler(sig, info, ucontext, SIGILL, "SIGILL");
}
/*
* stress_catch_sigsegv_handler()
* handler for SIGSEGV
*/
static void stress_catch_sigsegv_handler(
int sig,
siginfo_t *info,
void *ucontext)
{
stress_catch_sig_handler(sig, info, ucontext, SIGSEGV, "SIGSEGV");
}
/*
* stress_catch_sig()
* add signal handler to catch and dump illegal instructions,
* this is mainly to be used by any code using target clones
* just in case the compiler emits code that the target cannot
* actually execute.
*/
static void stress_catch_sig(
const int sig,
void (*handler)(int sig, siginfo_t *info, void *ucontext)
)
{
struct sigaction sa;
(void)shim_memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = handler;
#if defined(SA_SIGINFO)
sa.sa_flags = SA_SIGINFO;
#endif
(void)sigaction(sig, &sa, NULL);
}
/*
* stress_catch_sigill()
* catch and dump SIGILL signals
*/
void stress_catch_sigill(void)
{
stress_catch_sig(SIGILL, stress_catch_sigill_handler);
}
/*
* stress_catch_sigsegv()
* catch and dump SIGSEGV signals
*/
void stress_catch_sigsegv(void)
{
stress_catch_sig(SIGSEGV, stress_catch_sigsegv_handler);
}
#if defined(__linux__)
/*
* stress_process_info_dump()
* dump out /proc/$PID/filename data in human readable format
*/
static void stress_process_info_dump(
stress_args_t *args,
const pid_t pid,
const char *filename)
{
char path[4096];
char buf[8192];
char *ptr, *end, *begin, *emit;
ssize_t ret;
if (UNLIKELY(!filename))
return;
(void)snprintf(path, sizeof(path), "/proc/%" PRIdMAX "/%s", (intmax_t)pid, filename);
ret = stress_system_read(path, buf, sizeof(buf));
if (ret < 0)
return;
end = buf + ret;
/* data like /proc/$PID/cmdline has '\0' chars - replace with spaces */
for (ptr = buf; ptr < end; ptr++)
if (*ptr == '\0')
*ptr = ' ';
ptr = buf;
begin = ptr;
emit = NULL;
while (ptr < end) {
while (ptr < end) {
/* new line or eos, flush */
if (*ptr == '\n' || *ptr == '\0') {
*ptr = '\0';
emit = begin;
ptr++;
begin = ptr;
}
ptr++;
/* reached end, flush residual data out */
if (ptr == end)
emit = begin;
if (emit) {
pr_dbg("%s: [%" PRIdMAX "] %s: %s\n", args ? args->name : "main", (intmax_t)pid, filename, emit);
emit = NULL;
}
}
}
}
#endif
/*
* stress_process_info()
* dump out process specific debug from /proc
*/
void stress_process_info(stress_args_t *args, const pid_t pid)
{
#if defined(__linux__)
pr_block_begin();
stress_process_info_dump(args, pid, "cmdline");
stress_process_info_dump(args, pid, "syscall");
stress_process_info_dump(args, pid, "stack");
stress_process_info_dump(args, pid, "wchan");
pr_block_end();
#else
(void)args;
(void)pid;
#endif
}
/*
* stress_mmap_populate()
* try mmap with MAP_POPULATE option, if it fails
* retry without MAP_POPULATE. This prefaults pages
* into memory to avoid faulting during stressor
* execution. Useful for mappings that get accessed
* immediately after being mmap'd.
*/
void *stress_mmap_populate(
void *addr,
size_t length,
int prot,
int flags,
int fd,
off_t offset)
{
#if defined(MAP_POPULATE)
void *ret;
flags |= MAP_POPULATE;
ret = mmap(addr, length, prot, flags, fd, offset);
if (ret != MAP_FAILED)
return ret;
flags &= ~MAP_POPULATE;
#endif
return mmap(addr, length, prot, flags, fd, offset);
}
/*
* stress_get_machine_id()
* try to get a unique 64 bit machine id number
*/
uint64_t stress_get_machine_id(void)
{
uint64_t id = 0;
#if defined(__linux__)
{
char buf[17];
/* Try machine id from /etc */
if (stress_system_read("/etc/machine-id", buf, sizeof(buf)) > 0) {
buf[16] = '\0';
return (uint64_t)strtoll(buf, NULL, 16);
}
}
#endif
#if defined(__linux__)
{
char buf[17];
/* Try machine id from /var/lib */
if (stress_system_read("/var/lib/dbus/machine-id", buf, sizeof(buf)) > 0) {
buf[16] = '\0';
return (uint64_t)strtoll(buf, NULL, 16);
}
}
#endif
#if defined(HAVE_GETHOSTID)
{
/* Mangle 32 bit hostid to 64 bit */
uint64_t hostid = (uint64_t)gethostid();
id = hostid ^ ((~hostid) << 32);
}
#endif
#if defined(HAVE_GETHOSTNAME)
{
char buf[256];
/* Mangle hostname to 64 bit value */
if (gethostname(buf, sizeof(buf)) == 0) {
id ^= stress_hash_crc32c(buf) |
((uint64_t)stress_hash_x17(buf) << 32);
}
}
#endif
return id;
}
/*
* stress_zero_metrics()
* initialize metrics array 0..n-1 items
*/
void stress_zero_metrics(stress_metrics_t *metrics, const size_t n)
{
size_t i;
for (i = 0; i < n; i++) {
metrics[i].lock = NULL;
metrics[i].duration = 0.0;
metrics[i].count = 0.0;
metrics[i].t_start = 0.0;
}
}
/*
* stress_backtrace
* dump stack trace to stdout, this could be called
* from a signal context so try to keep buffer small
* and fflush on all printfs to ensure we dump as
* much as possible.
*/
void stress_backtrace(void)
{
#if defined(HAVE_EXECINFO_H) && \
defined(HAVE_BACKTRACE)
int i, n_ptrs;
void *buffer[BACKTRACE_BUF_SIZE];
char **strings;
n_ptrs = backtrace(buffer, BACKTRACE_BUF_SIZE);
if (n_ptrs < 1)
return;
strings = backtrace_symbols(buffer, n_ptrs);
if (!strings)
return;
printf("backtrace:\n");
fflush(stdout);
for (i = 0; i < n_ptrs; i++) {
printf(" %s\n", strings[i]);
fflush(stdout);
}
free(strings);
#endif
}
/*
* stress_data_is_not_zero()
* checks if buffer is zero, buffer must be 128 bit aligned
*/
bool OPTIMIZE3 stress_data_is_not_zero(uint64_t *buffer, const size_t len)
{
register const uint64_t *end64 = buffer + (len / sizeof(uint64_t));
register uint64_t *ptr64;
register const uint8_t *end8;
register uint8_t *ptr8;
PRAGMA_UNROLL_N(8)
for (ptr64 = buffer; ptr64 < end64; ptr64++) {
if (UNLIKELY(*ptr64))
return true;
}
end8 = ((uint8_t *)buffer) + len;
PRAGMA_UNROLL_N(8)
for (ptr8 = (uint8_t *)ptr64; ptr8 < end8; ptr8++) {
if (UNLIKELY(*ptr8))
return true;
}
return false;
}
|