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
|
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <json.h>
#include "igt_aux.h"
#include "igt_core.h"
#include "runnercomms.h"
#include "resultgen.h"
#include "settings.h"
#include "executor.h"
#include "output_strings.h"
#define INCOMPLETE_EXITCODE -1234
#define GRACEFUL_EXITCODE -SIGHUP
_Static_assert(INCOMPLETE_EXITCODE != IGT_EXIT_SKIP, "exit code clash");
_Static_assert(INCOMPLETE_EXITCODE != IGT_EXIT_SUCCESS, "exit code clash");
_Static_assert(INCOMPLETE_EXITCODE != IGT_EXIT_INVALID, "exit code clash");
_Static_assert(INCOMPLETE_EXITCODE != GRACEFUL_EXITCODE, "exit code clash");
struct subtest
{
char *name;
char **dynamic_names;
size_t dynamic_size;
};
struct subtest_list
{
struct subtest *subs;
size_t size;
};
struct results
{
struct json_object *tests;
struct json_object *totals;
struct json_object *runtimes;
};
static void add_dynamic_subtest(struct subtest *subtest, char *dynamic)
{
size_t len = strlen(dynamic);
size_t i;
if (len == 0)
return;
if (dynamic[len - 1] == '\n')
dynamic[len - 1] = '\0';
/* Don't add if we already have this one */
for (i = 0; i < subtest->dynamic_size; i++)
if (!strcmp(dynamic, subtest->dynamic_names[i]))
return;
subtest->dynamic_size++;
subtest->dynamic_names = realloc(subtest->dynamic_names, sizeof(*subtest->dynamic_names) * subtest->dynamic_size);
subtest->dynamic_names[subtest->dynamic_size - 1] = dynamic;
}
static void add_subtest(struct subtest_list *subtests, char *subtest)
{
size_t len = strlen(subtest);
size_t i;
if (len == 0)
return;
if (subtest[len - 1] == '\n')
subtest[len - 1] = '\0';
/* Don't add if we already have this subtest */
for (i = 0; i < subtests->size; i++)
if (!strcmp(subtest, subtests->subs[i].name))
return;
subtests->size++;
subtests->subs = realloc(subtests->subs, sizeof(*subtests->subs) * subtests->size);
memset(&subtests->subs[subtests->size - 1], 0, sizeof(struct subtest));
subtests->subs[subtests->size - 1].name = subtest;
}
static void free_subtest(struct subtest *subtest)
{
size_t i;
for (i = 0; i < subtest->dynamic_size; i++)
free(subtest->dynamic_names[i]);
free(subtest->dynamic_names);
}
static void free_subtests(struct subtest_list *subtests)
{
size_t i;
for (i = 0; i < subtests->size; i++)
free_subtest(&subtests->subs[i]);
free(subtests->subs);
}
/*
* A lot of string handling here operates on an mmapped buffer, and
* thus we can't assume null-terminated strings. Buffers will be
* passed around as pointer+size, or pointer+pointer-past-the-end, the
* mem*() family of functions is used instead of str*().
*/
static char *find_line_starting_with(char *haystack, const char *needle, char *end)
{
while (haystack < end) {
char *line_end = memchr(haystack, '\n', end - haystack);
if (end - haystack < strlen(needle))
return NULL;
if (!memcmp(haystack, needle, strlen(needle)))
return haystack;
if (line_end == NULL)
return NULL;
haystack = line_end + 1;
}
return NULL;
}
static const char *next_line(const char *line, const char *bufend)
{
char *ret;
if (!line)
return NULL;
ret = memchr(line, '\n', bufend - line);
if (ret)
ret++;
if (ret < bufend)
return ret;
else
return NULL;
}
static void append_line(char **buf, size_t *buflen, const char *line)
{
size_t linelen = strlen(line);
*buf = realloc(*buf, *buflen + linelen + 1);
strcpy(*buf + *buflen, line);
*buflen += linelen;
}
static const struct {
const char *output_str;
const char *result_str;
} resultmap[] = {
{ "SUCCESS", "pass" },
{ "SKIP", "skip" },
{ "FAIL", "fail" },
{ "CRASH", "crash" },
{ "TIMEOUT", "timeout" },
};
static void parse_result_string(const char *resultstring, size_t len, const char **result, double *time)
{
size_t i;
size_t wordlen = 0;
while (wordlen < len && !isspace(resultstring[wordlen])) {
wordlen++;
}
*result = NULL;
for (i = 0; i < (sizeof(resultmap) / sizeof(resultmap[0])); i++) {
if (!strncmp(resultstring, resultmap[i].output_str, wordlen)) {
*result = resultmap[i].result_str;
break;
}
}
/* If the result string is unknown, use incomplete */
if (!*result)
*result = "incomplete";
/*
* Check for subtest runtime after the result. The string is
* '(' followed by the runtime in seconds as floating point,
* followed by 's)'.
*/
wordlen++;
if (wordlen < len && resultstring[wordlen] == '(') {
char *dup;
wordlen++;
dup = malloc(len - wordlen + 1);
memcpy(dup, resultstring + wordlen, len - wordlen);
dup[len - wordlen] = '\0';
*time = strtod(dup, NULL);
free(dup);
}
}
static void parse_subtest_result(const char *subtest,
const char *resulttextprefix,
const char **result,
double *time,
const char *line,
const char *bufend)
{
const char *resultstring;
size_t subtestlen = strlen(subtest);
const char *line_end;
size_t linelen;
*result = "incomplete";
*time = 0.0;
/*
* The result line structure is:
*
* - The string "Subtest " (`SUBTEST_RESULT` from output_strings.h)
* - The subtest name
* - The characters ':' and ' '
* - Subtest result string
* - Optional:
* -- The characters ' ' and '('
* -- Subtest runtime in seconds as floating point
* -- The characters 's' and ')'
*
* Example:
* Subtest subtestname: PASS (0.003s)
*
* For dynamic subtests the same structure applies, but the
* string "Subtest " is "Dynamic subtest "
* instead. (`DYNAMIC_SUBTEST_RESULT` from output_strings.h)
*/
if (!line)
return;
line_end = memchr(line, '\n', bufend - line);
linelen = line_end != NULL ? line_end - line : bufend - line;
if (strlen(resulttextprefix) + subtestlen + strlen(": ") > linelen ||
strncmp(line + strlen(resulttextprefix), subtest, subtestlen)) {
/* This is not the correct result line */
return;
}
resultstring = line + strlen(resulttextprefix) + subtestlen + strlen(": ");
parse_result_string(resultstring, linelen - (resultstring - line), result, time);
}
static struct json_object *get_or_create_json_object(struct json_object *base,
const char *key)
{
struct json_object *ret;
if (json_object_object_get_ex(base, key, &ret))
return ret;
ret = json_object_new_object();
json_object_object_add(base, key, ret);
return ret;
}
static void set_result(struct json_object *obj, const char *result)
{
if (result)
json_object_object_add(obj, "result",
json_object_new_string(result));
}
static void add_runtime(struct json_object *obj, double time)
{
double oldtime;
struct json_object *timeobj = get_or_create_json_object(obj, "time");
struct json_object *oldend;
json_object_object_add(timeobj, "__type__",
json_object_new_string("TimeAttribute"));
json_object_object_add(timeobj, "start",
json_object_new_double(0.0));
if (!json_object_object_get_ex(timeobj, "end", &oldend)) {
json_object_object_add(timeobj, "end",
json_object_new_double(time));
return;
}
/* Add the runtime to the existing runtime. */
oldtime = json_object_get_double(oldend);
time += oldtime;
json_object_object_add(timeobj, "end",
json_object_new_double(time));
}
static void set_runtime(struct json_object *obj, double time)
{
struct json_object *timeobj = get_or_create_json_object(obj, "time");
json_object_object_add(timeobj, "__type__",
json_object_new_string("TimeAttribute"));
json_object_object_add(timeobj, "start",
json_object_new_double(0.0));
json_object_object_add(timeobj, "end",
json_object_new_double(time));
}
struct match_item
{
const char *where;
const char *what;
};
struct matches
{
struct match_item *items;
size_t size;
};
struct match_needle
{
const char *str;
bool (*validate)(const char *needle, const char *line, const char *bufend);
};
static void match_add(struct matches *matches, const char *where, const char *what)
{
struct match_item newitem = { where, what };
matches->size++;
matches->items = realloc(matches->items, matches->size * sizeof(*matches->items));
matches->items[matches->size - 1] = newitem;
}
static struct matches find_matches(const char *buf, const char *bufend,
const struct match_needle *needles)
{
struct matches ret = {};
while (buf < bufend) {
const struct match_needle *needle;
for (needle = needles; needle->str; needle++) {
if (bufend - buf < strlen(needle->str))
continue;
if (!memcmp(buf, needle->str, strlen(needle->str)) &&
(!needle->validate || needle->validate(needle->str, buf, bufend))) {
match_add(&ret, buf, needle->str);
goto end_find;
}
}
end_find:
buf = next_line(buf, bufend);
if (!buf)
break;
}
return ret;
}
static bool valid_char_for_subtest_name(char x)
{
return x == '-' || x == '_' || isalnum(x);
}
static bool is_subtest_result_line(const char *needle, const char *line, const char *bufend)
{
line += strlen(needle);
/*
* At this point of the string we're expecting:
* - The subtest name (one or more of a-z, A-Z, 0-9, '-' and '_')
* - The characters ':' and ' '
*
* If we find all those, allow parsing this line as [dynamic]
* subtest result.
*/
if (!valid_char_for_subtest_name(*line))
return false;
while (line < bufend && valid_char_for_subtest_name(*line))
line++;
if (line >= bufend || *line++ != ':')
return false;
if (line >= bufend || *line++ != ' ')
return false;
return true;
}
static void free_matches(struct matches *matches)
{
free(matches->items);
}
static struct json_object *new_escaped_json_string(const char *buf, size_t len)
{
struct json_object *obj;
char *str = NULL;
size_t strsize = 0;
size_t i;
/*
* Test output may be garbage; strings passed to json-c need to be
* UTF-8 encoded so any non-ASCII characters are converted to their
* UTF-8 representation, which requires 2 bytes per character.
*/
str = malloc(len * 2);
if (!str)
return NULL;
for (i = 0; i < len; i++) {
if (buf[i] > 0 && buf[i] < 128) {
str[strsize] = buf[i];
++strsize;
} else {
/* Encode > 128 character to UTF-8. */
str[strsize] = ((unsigned char)buf[i] >> 6) | 0xC0;
str[strsize + 1] = ((unsigned char)buf[i] & 0x3F) | 0x80;
strsize += 2;
}
}
obj = json_object_new_string_len(str, strsize);
free(str);
return obj;
}
static void add_igt_version(struct json_object *testobj,
const char *igt_version,
size_t igt_version_len)
{
if (igt_version)
json_object_object_add(testobj, "igt-version",
new_escaped_json_string(igt_version, igt_version_len));
}
enum subtest_find_pattern {
PATTERN_BEGIN,
PATTERN_RESULT,
};
static int find_subtest_idx_limited(struct matches matches,
const char *bufend,
const char *linekey,
enum subtest_find_pattern pattern,
const char *subtest_name,
int first,
int last)
{
char *full_line;
int line_len;
int k;
switch (pattern) {
case PATTERN_BEGIN:
line_len = asprintf(&full_line, "%s%s\n", linekey, subtest_name);
break;
case PATTERN_RESULT:
line_len = asprintf(&full_line, "%s%s: ", linekey, subtest_name);
break;
default:
assert(!"Unknown pattern");
}
if (line_len < 0)
return -1;
for (k = first; k < last; k++) {
ptrdiff_t rem = bufend - matches.items[k].where;
if (matches.items[k].what == linekey &&
!memcmp(matches.items[k].where,
full_line,
min_t(ptrdiff_t, line_len, rem)))
break;
}
free(full_line);
if (k == last)
k = -1;
return k;
}
static int find_subtest_idx(struct matches matches,
const char *bufend,
const char *linekey,
enum subtest_find_pattern pattern,
const char *subtest_name)
{
return find_subtest_idx_limited(matches, bufend, linekey, pattern, subtest_name, 0, matches.size);
}
static const char *find_subtest_begin_limit_limited(struct matches matches,
int begin_idx,
int result_idx,
const char *buf,
const char *bufend,
int first_idx)
{
/* No matching output at all, include everything */
if (begin_idx < first_idx && result_idx < first_idx)
return buf;
if (begin_idx < first_idx) {
/*
* Subtest didn't start, but we have the
* result. Probably because an igt_fixture
* made it fail/skip.
*
* We go backwards one match from the result match,
* and start from the next line.
*/
if (result_idx > first_idx)
return next_line(matches.items[result_idx - 1].where, bufend);
else
return buf;
}
/* Include all non-special output before the beginning line. */
if (begin_idx <= first_idx)
return buf;
return next_line(matches.items[begin_idx - 1].where, bufend);
}
static const char *find_subtest_begin_limit(struct matches matches,
int begin_idx,
int result_idx,
const char *buf,
const char *bufend)
{
return find_subtest_begin_limit_limited(matches, begin_idx, result_idx, buf, bufend, 0);
}
static const char *find_subtest_end_limit_limited(struct matches matches,
int begin_idx,
int result_idx,
const char *buf,
const char *bufend,
int first_idx,
int last_idx)
{
int k;
/* No matching output at all, include everything */
if (begin_idx < first_idx && result_idx < first_idx)
return bufend;
if (result_idx < first_idx) {
/*
* Incomplete result. Include all output up to the
* next starting subtest, or the result of one.
*/
for (k = begin_idx + 1; k < last_idx; k++) {
if (matches.items[k].what == STARTING_SUBTEST ||
matches.items[k].what == SUBTEST_RESULT)
return matches.items[k].where;
}
return bufend;
}
/* Include all non-special output to the next match, whatever it is. */
if (result_idx < last_idx - 1)
return matches.items[result_idx + 1].where;
return bufend;
}
static const char *find_subtest_end_limit(struct matches matches,
int begin_idx,
int result_idx,
const char *buf,
const char *bufend)
{
return find_subtest_end_limit_limited(matches, begin_idx, result_idx, buf, bufend, 0, matches.size);
}
static void process_dynamic_subtest_output(const char *piglit_name,
const char *igt_version,
size_t igt_version_len,
struct matches matches,
int begin_idx,
int result_idx,
const char *beg,
const char *end,
const char *key,
struct json_object *tests,
struct subtest *subtest)
{
size_t k;
if (result_idx < 0) {
/* If the subtest itself is incomplete, stop at the next start/end of a subtest */
for (result_idx = begin_idx + 1;
result_idx < matches.size;
result_idx++) {
if (matches.items[result_idx].what == STARTING_SUBTEST ||
matches.items[result_idx].what == SUBTEST_RESULT)
break;
}
}
for (k = begin_idx + 1; k < result_idx; k++) {
struct json_object *current_dynamic_test = NULL;
int dyn_result_idx;
char dynamic_name[256];
char dynamic_piglit_name[256];
const char *dynbeg, *dynend;
if (matches.items[k].what != STARTING_DYNAMIC_SUBTEST)
continue;
if (sscanf(matches.items[k].where + strlen(STARTING_DYNAMIC_SUBTEST), "%s", dynamic_name) != 1) {
/* Cannot parse name, just ignore this one */
continue;
}
dyn_result_idx = find_subtest_idx_limited(matches, end, DYNAMIC_SUBTEST_RESULT, PATTERN_RESULT, dynamic_name, k, result_idx);
dynbeg = find_subtest_begin_limit_limited(matches, k, dyn_result_idx, beg, end, begin_idx + 1);
dynend = find_subtest_end_limit_limited(matches, k, dyn_result_idx, beg, end, begin_idx + 1, result_idx);
generate_piglit_name_for_dynamic(piglit_name, dynamic_name, dynamic_piglit_name, sizeof(dynamic_piglit_name));
add_dynamic_subtest(subtest, strdup(dynamic_name));
current_dynamic_test = get_or_create_json_object(tests, dynamic_piglit_name);
json_object_object_add(current_dynamic_test, key,
new_escaped_json_string(dynbeg, dynend - dynbeg));
add_igt_version(current_dynamic_test, igt_version, igt_version_len);
if (!json_object_object_get_ex(current_dynamic_test, "result", NULL)) {
const char *dynresulttext;
double dyntime;
parse_subtest_result(dynamic_name,
DYNAMIC_SUBTEST_RESULT,
&dynresulttext, &dyntime,
dyn_result_idx < 0 ? NULL : matches.items[dyn_result_idx].where,
dynend);
/*
* If a dynamic subsubtest is considered
* incomplete we need to check parent's status
* first, to be sure that the binary hasn't
* aborted or stopped gracefully (exit
* code). If it has aborted then we have to
* attribute this status to our subsubtest.
*/
if (!strcmp(dynresulttext, "incomplete")) {
struct json_object *parent_subtest;
if (json_object_object_get_ex(tests, piglit_name, &parent_subtest) &&
json_object_object_get_ex(parent_subtest, "result", &parent_subtest)) {
const char *resulttext = json_object_get_string(parent_subtest);
if (!strcmp(resulttext, "abort") ||
!strcmp(resulttext, "notrun"))
dynresulttext = resulttext;
}
}
set_result(current_dynamic_test, dynresulttext);
set_runtime(current_dynamic_test, dyntime);
}
}
}
static bool fill_from_output(int fd, const char *binary, const char *key,
struct subtest_list *subtests,
struct json_object *tests)
{
char *buf, *bufend, *nullchr;
struct stat statbuf;
char piglit_name[256];
char *igt_version = NULL;
size_t igt_version_len = 0;
struct json_object *current_test = NULL;
struct match_needle needles[] = {
{ STARTING_SUBTEST, NULL },
{ SUBTEST_RESULT, is_subtest_result_line },
{ STARTING_DYNAMIC_SUBTEST, NULL },
{ DYNAMIC_SUBTEST_RESULT, is_subtest_result_line },
{ NULL, NULL },
};
struct matches matches = {};
size_t i;
if (fstat(fd, &statbuf))
return false;
if (statbuf.st_size != 0) {
buf = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED)
return false;
} else {
buf = NULL;
}
/*
* Avoid null characters: Just pretend the output stops at the
* first such character, if any.
*/
if ((nullchr = memchr(buf, '\0', statbuf.st_size)) != NULL) {
statbuf.st_size = nullchr - buf;
}
bufend = buf + statbuf.st_size;
igt_version = find_line_starting_with(buf, IGT_VERSIONSTRING, bufend);
if (igt_version) {
char *newline = memchr(igt_version, '\n', bufend - igt_version);
igt_version_len = newline - igt_version;
}
/* TODO: Refactor to helper functions */
if (subtests->size == 0) {
/* No subtests */
generate_piglit_name(binary, NULL, piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(tests, piglit_name);
json_object_object_add(current_test, key,
new_escaped_json_string(buf, statbuf.st_size));
add_igt_version(current_test, igt_version, igt_version_len);
return true;
}
matches = find_matches(buf, bufend, needles);
for (i = 0; i < subtests->size; i++) {
int begin_idx, result_idx;
const char *resulttext;
const char *beg, *end;
double time;
generate_piglit_name(binary, subtests->subs[i].name, piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(tests, piglit_name);
begin_idx = find_subtest_idx(matches, bufend, STARTING_SUBTEST, PATTERN_BEGIN, subtests->subs[i].name);
result_idx = find_subtest_idx(matches, bufend, SUBTEST_RESULT, PATTERN_RESULT, subtests->subs[i].name);
beg = find_subtest_begin_limit(matches, begin_idx, result_idx, buf, bufend);
end = find_subtest_end_limit(matches, begin_idx, result_idx, buf, bufend);
json_object_object_add(current_test, key,
new_escaped_json_string(beg, end - beg));
add_igt_version(current_test, igt_version, igt_version_len);
if (!json_object_object_get_ex(current_test, "result", NULL)) {
parse_subtest_result(subtests->subs[i].name,
SUBTEST_RESULT,
&resulttext, &time,
result_idx < 0 ? NULL : matches.items[result_idx].where,
end);
set_result(current_test, resulttext);
set_runtime(current_test, time);
}
process_dynamic_subtest_output(piglit_name,
igt_version, igt_version_len,
matches,
begin_idx, result_idx,
beg, end,
key,
tests,
&subtests->subs[i]);
}
free_matches(&matches);
return true;
}
/*
* This regexp controls the kmsg handling. All kernel log records that
* have log level of warning or higher convert the result to
* dmesg-warn/dmesg-fail unless they match this regexp.
*
* TODO: Move this to external files, i915-suppressions.txt,
* general-suppressions.txt et al.
*/
#define _ "|"
static const char igt_dmesg_whitelist[] =
"ACPI: button: The lid device is not compliant to SW_LID" _
"ACPI: .*: Unable to dock!" _
"IRQ [0-9]+: no longer affine to CPU[0-9]+" _
"IRQ fixup: irq [0-9]+ move in progress, old vector [0-9]+" _
/* i915 tests set module options, expected message */
"Setting dangerous option [a-z_]+ - tainting kernel" _
/* Raw printk() call, uses default log level (warn) */
"Suspending console\\(s\\) \\(use no_console_suspend to debug\\)" _
"atkbd serio[0-9]+: Failed to (deactivate|enable) keyboard on isa[0-9]+/serio[0-9]+" _
"cache: parent cpu[0-9]+ should not be sleeping" _
"hpet[0-9]+: lost [0-9]+ rtc interrupts" _
/* i915 selftests terminate normally with ENODEV from the
* module load after the testing finishes, which produces this
* message.
*/
"i915: probe of [0-9a-fA-F:.]+ failed with error -25" _
/* swiotbl warns even when asked not to */
"mock: DMA: Out of SW-IOMMU space for [0-9]+ bytes" _
"usb usb[0-9]+: root hub lost power or was reset"
;
#undef _
static const char igt_piglit_style_dmesg_blacklist[] =
"(\\[drm:|drm_|intel_|i915_|\\[drm\\])";
static bool init_dmesg_regex(GRegex **re, const char *regex, const char *msg)
{
GError *err = NULL;
*re = g_regex_new(regex, G_REGEX_OPTIMIZE, 0, &err);
if (err) {
fprintf(stderr, "Cannot compile %s : %s\n",
msg, err->message);
g_error_free(err);
return false;
}
return true;
}
static bool init_regex_whitelist(struct settings *settings, GRegex **re)
{
const char *regex = settings->piglit_style_dmesg ?
igt_piglit_style_dmesg_blacklist :
igt_dmesg_whitelist;
const char *what = settings->piglit_style_dmesg ?
"piglit style dmesg blocklist" :
"igt dmesg whitelist";
return init_dmesg_regex(re, regex, what);
}
static bool not_ignored(GRegex *re, const char *msg)
{
if (!re)
return true;
return !g_regex_match(re, msg, 0, NULL);
}
static void clean_regex(GRegex **re)
{
if (*re)
g_regex_unref(*re);
*re = NULL;
}
static void add_ignored_regex(GRegex **re, char *src)
{
char *s;
s = strchr(src, '\n');
if (s)
*s = 0;
if (*re)
g_regex_unref(*re);
init_dmesg_regex(re, src, "ignore match");
fprintf(stderr, "igt_resultgen: Added ignore regex '%s'\n", src);
}
static bool parse_dmesg_line(char* line,
unsigned *flags, unsigned long long *ts_usec,
char *continuation, char **message)
{
unsigned long long seq;
int s;
s = sscanf(line, "%u,%llu,%llu,%c;", flags, &seq, ts_usec, continuation);
if (s != 4) {
/*
* Machine readable key/value pairs begin with
* a space. We ignore them.
*/
if (line[0] != ' ') {
fprintf(stderr, "Cannot parse kmsg record: %s\n", line);
}
return false;
}
*message = strchr(line, ';');
if (*message == NULL) {
fprintf(stderr, "No ; found in kmsg record, this shouldn't happen\n");
return false;
}
(*message)++;
return true;
}
static void generate_formatted_dmesg_line(char *message,
unsigned flags,
unsigned long long ts_usec,
char **formatted)
{
char prefix[512];
size_t messagelen;
size_t prefixlen;
char *p, *f;
snprintf(prefix, sizeof(prefix),
"<%u> [%llu.%06llu] ",
flags & 0x07,
ts_usec / 1000000,
ts_usec % 1000000);
messagelen = strlen(message);
prefixlen = strlen(prefix);
/*
* Decoding the hex escapes only makes the string shorter, so
* we can use the original length
*/
*formatted = malloc(strlen(prefix) + messagelen + 1);
strcpy(*formatted, prefix);
f = *formatted + prefixlen;
for (p = message; *p; p++, f++) {
if (p - message + 4 < messagelen &&
p[0] == '\\' && p[1] == 'x') {
int c = 0;
/* newline and tab are not isprint(), but they are isspace() */
if (sscanf(p, "\\x%2x", &c) == 1 &&
(isprint(c) || isspace(c))) {
*f = c;
p += 3;
continue;
}
}
*f = *p;
}
*f = '\0';
}
static void add_dmesg(struct json_object *obj,
const char *dmesg, size_t dmesglen,
const char *warnings, size_t warningslen)
{
json_object_object_add(obj, "dmesg",
new_escaped_json_string(dmesg, dmesglen));
if (warnings) {
json_object_object_add(obj, "dmesg-warnings",
new_escaped_json_string(warnings, warningslen));
}
}
static void add_empty_dmesgs_where_missing(struct json_object *tests,
char *binary,
struct subtest_list *subtests)
{
struct json_object *current_test;
char piglit_name[256];
char dynamic_piglit_name[256];
size_t i, k;
for (i = 0; i < subtests->size; i++) {
generate_piglit_name(binary, subtests->subs[i].name, piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(tests, piglit_name);
if (!json_object_object_get_ex(current_test, "dmesg", NULL)) {
add_dmesg(current_test, "", 0, NULL, 0);
}
for (k = 0; k < subtests->subs[i].dynamic_size; k++) {
generate_piglit_name_for_dynamic(piglit_name, subtests->subs[i].dynamic_names[k],
dynamic_piglit_name, sizeof(dynamic_piglit_name));
current_test = get_or_create_json_object(tests, dynamic_piglit_name);
if (!json_object_object_get_ex(current_test, "dmesg", NULL)) {
add_dmesg(current_test, "", 0, NULL, 0);
}
}
}
}
static bool fill_from_dmesg(int fd,
struct settings *settings,
char *binary,
struct subtest_list *subtests,
struct json_object *tests)
{
char *line = NULL;
char *warnings = NULL, *dynamic_warnings = NULL;
char *dmesg = NULL, *dynamic_dmesg = NULL;
size_t linelen = 0;
size_t warningslen = 0, dynamic_warnings_len = 0;
size_t dmesglen = 0, dynamic_dmesg_len = 0;
struct json_object *current_test = NULL;
struct json_object *current_dynamic_test = NULL;
FILE *f = fdopen(fd, "r");
char piglit_name[256];
char dynamic_piglit_name[256];
size_t i;
GRegex *re;
GRegex *re_ignore; /* regex for dynamically ignored dmesg line */
if (!f) {
return false;
}
if (!init_regex_whitelist(settings, &re)) {
fclose(f);
return false;
}
re_ignore = NULL;
while (getline(&line, &linelen, f) > 0) {
char *formatted;
unsigned flags;
unsigned long long ts_usec;
char continuation;
char *message, *subtest, *dynamic_subtest, *ignore;
if (!parse_dmesg_line(line, &flags, &ts_usec, &continuation, &message))
continue;
generate_formatted_dmesg_line(message, flags, ts_usec, &formatted);
if ((subtest = strstr(message, STARTING_SUBTEST_DMESG)) != NULL) {
if (current_test != NULL) {
/* Done with the previous subtest, file up */
add_dmesg(current_test, dmesg, dmesglen, warnings, warningslen);
free(dmesg);
free(warnings);
dmesg = warnings = NULL;
dmesglen = warningslen = 0;
if (current_dynamic_test != NULL)
add_dmesg(current_dynamic_test, dynamic_dmesg, dynamic_dmesg_len, dynamic_warnings, dynamic_warnings_len);
free(dynamic_dmesg);
free(dynamic_warnings);
dynamic_dmesg = dynamic_warnings = NULL;
dynamic_dmesg_len = dynamic_warnings_len = 0;
current_dynamic_test = NULL;
}
subtest += strlen(STARTING_SUBTEST_DMESG);
generate_piglit_name(binary, subtest, piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(tests, piglit_name);
clean_regex(&re_ignore);
}
if (current_test != NULL &&
(dynamic_subtest = strstr(message, STARTING_DYNAMIC_SUBTEST_DMESG)) != NULL) {
if (current_dynamic_test != NULL) {
/* Done with the previous dynamic subtest, file up */
add_dmesg(current_dynamic_test, dynamic_dmesg, dynamic_dmesg_len, dynamic_warnings, dynamic_warnings_len);
free(dynamic_dmesg);
free(dynamic_warnings);
dynamic_dmesg = dynamic_warnings = NULL;
dynamic_dmesg_len = dynamic_warnings_len = 0;
}
dynamic_subtest += strlen(STARTING_DYNAMIC_SUBTEST_DMESG);
generate_piglit_name_for_dynamic(piglit_name, dynamic_subtest, dynamic_piglit_name, sizeof(dynamic_piglit_name));
current_dynamic_test = get_or_create_json_object(tests, dynamic_piglit_name);
clean_regex(&re_ignore);
}
if ((ignore = strstr(message, IGT_ADD_IGNORED_REGEX_DMESG)) != NULL)
add_ignored_regex(&re_ignore, ignore + strlen(IGT_ADD_IGNORED_REGEX_DMESG));
if (settings->piglit_style_dmesg) {
if ((flags & 0x07) <= settings->dmesg_warn_level && continuation != 'c' &&
g_regex_match(re, message, 0, NULL) &&
not_ignored(re_ignore, message)) {
append_line(&warnings, &warningslen, formatted);
if (current_test != NULL)
append_line(&dynamic_warnings, &dynamic_warnings_len, formatted);
}
} else {
if ((flags & 0x07) <= settings->dmesg_warn_level && continuation != 'c' &&
!g_regex_match(re, message, 0, NULL) &&
not_ignored(re_ignore, message)) {
append_line(&warnings, &warningslen, formatted);
if (current_test != NULL)
append_line(&dynamic_warnings, &dynamic_warnings_len, formatted);
}
}
append_line(&dmesg, &dmesglen, formatted);
append_line(&dynamic_dmesg, &dynamic_dmesg_len, formatted);
free(formatted);
}
free(line);
if (current_test != NULL) {
add_dmesg(current_test, dmesg, dmesglen, warnings, warningslen);
if (current_dynamic_test != NULL) {
add_dmesg(current_dynamic_test, dynamic_dmesg, dynamic_dmesg_len, dynamic_warnings, dynamic_warnings_len);
}
} else {
/*
* Didn't get any subtest messages at all. If there
* are subtests, add all of the dmesg gotten to all of
* them.
*/
for (i = 0; i < subtests->size; i++) {
generate_piglit_name(binary, subtests->subs[i].name, piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(tests, piglit_name);
/*
* Don't bother with warnings, any subtests
* there are would have skip as their result
* anyway.
*/
add_dmesg(current_test, dmesg, dmesglen, NULL, 0);
}
if (subtests->size == 0) {
generate_piglit_name(binary, NULL, piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(tests, piglit_name);
add_dmesg(current_test, dmesg, dmesglen, warnings, warningslen);
}
}
add_empty_dmesgs_where_missing(tests, binary, subtests);
free(dmesg);
free(dynamic_dmesg);
free(warnings);
free(dynamic_warnings);
clean_regex(&re_ignore);
g_regex_unref(re);
fclose(f);
return true;
}
static const char *result_from_exitcode(int exitcode)
{
switch (exitcode) {
case IGT_EXIT_SKIP:
return "skip";
case IGT_EXIT_SUCCESS:
return "pass";
case IGT_EXIT_INVALID:
return "skip";
case IGT_EXIT_ABORT:
return "abort";
case INCOMPLETE_EXITCODE:
return "incomplete";
case GRACEFUL_EXITCODE:
return "notrun";
default:
return "fail";
}
}
static void fill_from_journal(int fd,
struct job_list_entry *entry,
struct subtest_list *subtests,
struct results *results)
{
FILE *f = fdopen(fd, "r");
char *line = NULL;
size_t linelen = 0;
ssize_t read;
char exitline[] = "exit:";
char timeoutline[] = "timeout:";
int exitcode = INCOMPLETE_EXITCODE;
bool has_timeout = false;
struct json_object *tests = results->tests;
struct json_object *runtimes = results->runtimes;
while ((read = getline(&line, &linelen, f)) > 0) {
if (read >= strlen(exitline) && !memcmp(line, exitline, strlen(exitline))) {
char *p = strchr(line, '(');
char piglit_name[256];
double time = 0.0;
struct json_object *obj;
exitcode = atoi(line + strlen(exitline));
if (p)
time = strtod(p + 1, NULL);
generate_piglit_name(entry->binary, NULL, piglit_name, sizeof(piglit_name));
obj = get_or_create_json_object(runtimes, piglit_name);
add_runtime(obj, time);
/* If no subtests, the test result node also gets the runtime */
if (subtests->size == 0 && entry->subtest_count == 0) {
obj = get_or_create_json_object(tests, piglit_name);
add_runtime(obj, time);
}
} else if (read >= strlen(timeoutline) && !memcmp(line, timeoutline, strlen(timeoutline))) {
has_timeout = true;
if (subtests->size) {
/* Assign the timeout to the previously appeared subtest */
char *last_subtest = subtests->subs[subtests->size - 1].name;
char piglit_name[256];
char *p = strchr(line, '(');
double time = 0.0;
struct json_object *obj;
generate_piglit_name(entry->binary, last_subtest, piglit_name, sizeof(piglit_name));
obj = get_or_create_json_object(tests, piglit_name);
set_result(obj, "timeout");
if (p)
time = strtod(p + 1, NULL);
/* Add runtime for the subtest... */
add_runtime(obj, time);
/* ... and also for the binary */
generate_piglit_name(entry->binary, NULL, piglit_name, sizeof(piglit_name));
obj = get_or_create_json_object(runtimes, piglit_name);
add_runtime(obj, time);
}
} else {
add_subtest(subtests, strdup(line));
}
}
if (subtests->size && (exitcode == IGT_EXIT_ABORT || exitcode == GRACEFUL_EXITCODE)) {
char *last_subtest = subtests->subs[subtests->size - 1].name;
char subtest_piglit_name[256];
struct json_object *subtest_obj;
generate_piglit_name(entry->binary, last_subtest, subtest_piglit_name, sizeof(subtest_piglit_name));
subtest_obj = get_or_create_json_object(tests, subtest_piglit_name);
set_result(subtest_obj, exitcode == IGT_EXIT_ABORT ? "abort" : "notrun");
}
if (subtests->size == 0) {
char *subtestname = NULL;
char piglit_name[256];
struct json_object *obj;
const char *result = has_timeout ? "timeout" : result_from_exitcode(exitcode);
/*
* If the test was killed before it printed that it's
* entering a subtest, we would incorrectly generate
* results as the binary had no subtests. If we know
* otherwise, do otherwise.
*/
if (entry->subtest_count > 0) {
subtestname = entry->subtests[0];
add_subtest(subtests, strdup(subtestname));
}
generate_piglit_name(entry->binary, subtestname, piglit_name, sizeof(piglit_name));
obj = get_or_create_json_object(tests, piglit_name);
set_result(obj, result);
}
free(line);
fclose(f);
}
typedef enum comms_state {
STATE_INITIAL = 0,
STATE_AFTER_EXEC,
STATE_SUBTEST_STARTED,
STATE_DYNAMIC_SUBTEST_STARTED,
STATE_BETWEEN_DYNAMIC_SUBTESTS,
STATE_BETWEEN_SUBTESTS,
STATE_EXITED,
} comms_state_t;
struct comms_context
{
comms_state_t state;
struct json_object *binaryruntimeobj;
struct json_object *current_test;
struct json_object *current_dynamic_subtest;
char *current_subtest_name;
char *current_dynamic_subtest_name;
char *outbuf, *errbuf;
size_t outbuflen, errbuflen;
size_t outidx, nextoutidx;
size_t erridx, nexterridx;
size_t dynoutidx, nextdynoutidx;
size_t dynerridx, nextdynerridx;
char *igt_version;
char *subtestresult;
char *dynamicsubtestresult;
double subtest_execution_time;
double dynamic_subtest_execution_time;
char *cmdline;
int exitcode;
struct subtest_list *subtests;
struct subtest *subtest;
struct results *results;
struct job_list_entry *entry;
const char *binary;
};
static void comms_free_context(struct comms_context *context)
{
free(context->current_subtest_name);
free(context->current_dynamic_subtest_name);
free(context->outbuf);
free(context->errbuf);
free(context->igt_version);
free(context->subtestresult);
free(context->dynamicsubtestresult);
free(context->cmdline);
}
static void comms_inject_subtest_start_log(struct comms_context *context,
const char *prefix,
const char *subtestname)
{
char msg[512];
snprintf(msg, sizeof(msg), "%s%s\n", prefix, subtestname);
append_line(&context->outbuf, &context->outbuflen, msg);
append_line(&context->errbuf, &context->errbuflen, msg);
}
static void comms_inject_subtest_end_log(struct comms_context *context,
const char *prefix,
const char *subtestname,
const char *subtestresult,
const char *timeused)
{
char msg[512];
snprintf(msg, sizeof(msg), "%s%s: %s (%ss)\n", prefix, subtestname, subtestresult, timeused);
append_line(&context->outbuf, &context->outbuflen, msg);
append_line(&context->errbuf, &context->errbuflen, msg);
}
static void comms_finish_subtest(struct comms_context *context)
{
json_object_object_add(context->current_test, "out",
new_escaped_json_string(context->outbuf + context->outidx, context->outbuflen - context->outidx));
json_object_object_add(context->current_test, "err",
new_escaped_json_string(context->errbuf + context->outidx, context->errbuflen - context->erridx));
if (context->igt_version)
add_igt_version(context->current_test, context->igt_version, strlen(context->igt_version));
if (context->subtestresult == NULL)
context->subtestresult = strdup("incomplete");
set_result(context->current_test, context->subtestresult);
add_runtime(context->current_test, context->subtest_execution_time);
free(context->subtestresult);
context->subtestresult = NULL;
context->current_test = NULL;
context->outidx = context->nextoutidx;
context->erridx = context->nexterridx;
}
static void comms_finish_dynamic_subtest(struct comms_context *context)
{
json_object_object_add(context->current_dynamic_subtest, "out",
new_escaped_json_string(context->outbuf + context->dynoutidx, context->outbuflen - context->dynoutidx));
json_object_object_add(context->current_dynamic_subtest, "err",
new_escaped_json_string(context->errbuf + context->dynerridx, context->errbuflen - context->dynerridx));
if (context->igt_version)
add_igt_version(context->current_dynamic_subtest, context->igt_version, strlen(context->igt_version));
if (context->dynamicsubtestresult == NULL)
context->dynamicsubtestresult = strdup("incomplete");
set_result(context->current_dynamic_subtest, context->dynamicsubtestresult);
add_runtime(context->current_dynamic_subtest, context->dynamic_subtest_execution_time);
free(context->dynamicsubtestresult);
context->dynamicsubtestresult = NULL;
context->current_dynamic_subtest = NULL;
context->dynoutidx = context->nextdynoutidx;
context->dynerridx = context->nextdynerridx;
}
static void comms_add_new_subtest(struct comms_context *context,
const char *subtestname)
{
char piglit_name[256];
add_subtest(context->subtests, strdup(subtestname));
context->subtest = &context->subtests->subs[context->subtests->size - 1];
generate_piglit_name(context->binary, subtestname, piglit_name, sizeof(piglit_name));
context->current_test = get_or_create_json_object(context->results->tests, piglit_name);
free(context->current_subtest_name);
context->current_subtest_name = strdup(subtestname);
}
static void comms_add_new_dynamic_subtest(struct comms_context *context,
const char *dynamic_name)
{
char piglit_name[256];
char dynamic_piglit_name[256];
add_dynamic_subtest(context->subtest, strdup(dynamic_name));
generate_piglit_name(context->binary, context->current_subtest_name, piglit_name, sizeof(piglit_name));
generate_piglit_name_for_dynamic(piglit_name, dynamic_name, dynamic_piglit_name, sizeof(dynamic_piglit_name));
context->current_dynamic_subtest = get_or_create_json_object(context->results->tests, dynamic_piglit_name);
free(context->current_dynamic_subtest_name);
context->current_dynamic_subtest_name = strdup(dynamic_name);
}
static bool comms_handle_log(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
char **textbuf;
size_t *textlen;
if (helper.log.stream == STDOUT_FILENO) {
textbuf = &context->outbuf;
textlen = &context->outbuflen;
} else {
textbuf = &context->errbuf;
textlen = &context->errbuflen;
}
append_line(textbuf, textlen, helper.log.text);
return true;
}
static bool comms_handle_exec(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
switch (context->state) {
case STATE_INITIAL:
break;
case STATE_AFTER_EXEC:
/*
* Resume after an exec that didn't involve any
* subtests. Resumes can only happen for tests with
* subtests, so while we might have logs already
* collected, we have nowhere to put them. The joblist
* doesn't help, because the ordering is up to the
* test.
*/
printf("Warning: Need to discard %zd bytes of logs, no subtest data\n", context->outbuflen + context->errbuflen);
context->outbuflen = context->errbuflen = 0;
context->outidx = context->erridx = 0;
context->nextoutidx = context->nexterridx = 0;
break;
case STATE_SUBTEST_STARTED:
case STATE_DYNAMIC_SUBTEST_STARTED:
case STATE_BETWEEN_DYNAMIC_SUBTESTS:
case STATE_BETWEEN_SUBTESTS:
case STATE_EXITED:
/* A resume exec, so we're already collecting data. */
assert(context->current_test != NULL);
comms_finish_subtest(context);
break;
default:
assert(false); /* unreachable */
}
free(context->cmdline);
context->cmdline = strdup(helper.exec.cmdline);
context->state = STATE_AFTER_EXEC;
return true;
}
static bool comms_handle_exit(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
char piglit_name[256];
if (context->state == STATE_AFTER_EXEC) {
/*
* Exit after exec, so we didn't get any
* subtests. Check if there's supposed to be any,
* otherwise stuff logs into the binary's result.
*/
char *subtestname = NULL;
if (context->entry->subtest_count > 0) {
subtestname = context->entry->subtests[0];
add_subtest(context->subtests, strdup(subtestname));
}
generate_piglit_name(context->binary, subtestname, piglit_name, sizeof(piglit_name));
context->current_test = get_or_create_json_object(context->results->tests, piglit_name);
/* Get result from exitcode unless we have an override already */
if (context->subtestresult == NULL)
context->subtestresult = strdup(result_from_exitcode(helper.exit.exitcode));
} else if (helper.exit.exitcode == IGT_EXIT_ABORT || helper.exit.exitcode == GRACEFUL_EXITCODE) {
/*
* If we did get subtests, we need to assign the
* special exitcode results to the last subtest,
* normal and dynamic
*/
const char *result = helper.exit.exitcode == IGT_EXIT_ABORT ? "abort" : "notrun";
free(context->subtestresult);
context->subtestresult = strdup(result);
free(context->dynamicsubtestresult);
context->dynamicsubtestresult = strdup(result);
}
context->exitcode = helper.exit.exitcode;
add_runtime(context->binaryruntimeobj, strtod(helper.exit.timeused, NULL));
context->state = STATE_EXITED;
return true;
}
static bool comms_handle_subtest_start(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
char errmsg[512];
switch (context->state) {
case STATE_INITIAL:
case STATE_EXITED:
/* Subtest starts when we're not even running? (Before exec or after exit) */
fprintf(stderr, "Error: Unexpected subtest start (binary wasn't running)\n");
return false;
case STATE_SUBTEST_STARTED:
case STATE_DYNAMIC_SUBTEST_STARTED:
case STATE_BETWEEN_DYNAMIC_SUBTESTS:
/*
* Subtest starts when the previous one was still
* running. Text-based parsing would figure that a
* resume happened, but we know the real deal with
* socket comms.
*/
snprintf(errmsg, sizeof(errmsg),
"\nrunner: Subtest %s already running when subtest %s starts. This is a test bug.\n",
context->current_subtest_name,
helper.subteststart.name);
append_line(&context->errbuf, &context->errbuflen, errmsg);
if (context->state == STATE_DYNAMIC_SUBTEST_STARTED ||
context->state == STATE_BETWEEN_DYNAMIC_SUBTESTS)
comms_finish_dynamic_subtest(context);
/* fallthrough */
case STATE_BETWEEN_SUBTESTS:
/* Already collecting for a subtest, finish it up */
if (context->current_dynamic_subtest)
comms_finish_dynamic_subtest(context);
comms_finish_subtest(context);
/* fallthrough */
case STATE_AFTER_EXEC:
comms_add_new_subtest(context, helper.subteststart.name);
/* Subtest starting message is not in logs with socket comms, inject it manually */
comms_inject_subtest_start_log(context, STARTING_SUBTEST, helper.subteststart.name);
free(context->subtestresult);
context->subtestresult = NULL;
break;
default:
assert(false); /* unreachable */
}
context->state = STATE_SUBTEST_STARTED;
return true;
}
static bool comms_handle_subtest_result(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
char errmsg[512];
switch (context->state) {
case STATE_INITIAL:
case STATE_EXITED:
/* Subtest result when we're not even running? (Before exec or after exit) */
fprintf(stderr, "Error: Unexpected subtest result (binary wasn't running)\n");
return false;
case STATE_DYNAMIC_SUBTEST_STARTED:
/*
* Subtest result when dynamic subtest is still
* running. Text-based parsing would consider that an
* incomplete, we're able to inject a warning.
*/
snprintf(errmsg, sizeof(errmsg),
"\nrunner: Dynamic subtest %s still running when subtest %s ended. This is a test bug.\n",
context->current_dynamic_subtest_name,
helper.subtestresult.name);
append_line(&context->errbuf, &context->errbuflen, errmsg);
comms_finish_dynamic_subtest(context);
break;
case STATE_BETWEEN_SUBTESTS:
/* Subtest result without starting it, and we're already collecting logs for a previous test */
comms_finish_subtest(context);
comms_add_new_subtest(context, helper.subtestresult.name);
break;
case STATE_AFTER_EXEC:
/* Subtest result without starting it, so comes from a fixture. We're not yet collecting logs for anything. */
comms_add_new_subtest(context, helper.subtestresult.name);
break;
case STATE_SUBTEST_STARTED:
case STATE_BETWEEN_DYNAMIC_SUBTESTS:
/* Normal flow */
break;
default:
assert(false); /* unreachable */
}
comms_inject_subtest_end_log(context,
SUBTEST_RESULT,
helper.subtestresult.name,
helper.subtestresult.result,
helper.subtestresult.timeused);
/* Next subtest, if any, will begin its logs right after that result line */
context->nextoutidx = context->outbuflen;
context->nexterridx = context->errbuflen;
/*
* Only store the actual result from the packet if we don't
* already have one. If we do, it's from an override.
*/
if (context->subtestresult == NULL) {
const char *mappedresult;
parse_result_string(helper.subtestresult.result,
strlen(helper.subtestresult.result),
&mappedresult, NULL);
context->subtestresult = strdup(mappedresult);
context->subtest_execution_time = strtod(helper.subtestresult.timeused, NULL);
}
context->state = STATE_BETWEEN_SUBTESTS;
return true;
}
static bool comms_handle_dynamic_subtest_start(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
char errmsg[512];
switch (context->state) {
case STATE_INITIAL:
case STATE_EXITED:
/* Dynamic subtest starts when we're not even running? (Before exec or after exit) */
fprintf(stderr, "Error: Unexpected dynamic subtest start (binary wasn't running)\n");
return false;
case STATE_AFTER_EXEC:
/* Binary was running but a subtest wasn't. We don't know where to inject an error message. */
fprintf(stderr, "Error: Unexpected dynamic subtest start (subtest wasn't running)\n");
return false;
case STATE_BETWEEN_SUBTESTS:
/*
* Dynamic subtest starts when a subtest is not
* running. We can't know which subtest this dynamic
* subtest was supposed to be in. But we can inject a
* warn into the previous subtest.
*/
snprintf(errmsg, sizeof(errmsg),
"\nrunner: Dynamic subtest %s started when not inside a subtest. This is a test bug.\n",
helper.dynamicsubteststart.name);
append_line(&context->errbuf, &context->errbuflen, errmsg);
/* Leave the state as is and hope for the best */
return true;
case STATE_DYNAMIC_SUBTEST_STARTED:
snprintf(errmsg, sizeof(errmsg),
"\nrunner: Dynamic subtest %s already running when dynamic subtest %s starts. This is a test bug.\n",
context->current_dynamic_subtest_name,
helper.dynamicsubteststart.name);
append_line(&context->errbuf, &context->errbuflen, errmsg);
/* fallthrough */
case STATE_BETWEEN_DYNAMIC_SUBTESTS:
comms_finish_dynamic_subtest(context);
/* fallthrough */
case STATE_SUBTEST_STARTED:
comms_add_new_dynamic_subtest(context, helper.dynamicsubteststart.name);
/* Dynamic subtest starting message is not in logs with socket comms, inject it manually */
comms_inject_subtest_start_log(context, STARTING_DYNAMIC_SUBTEST, helper.dynamicsubteststart.name);
free(context->dynamicsubtestresult);
context->dynamicsubtestresult = NULL;
break;
default:
assert(false); /* unreachable */
}
context->state = STATE_DYNAMIC_SUBTEST_STARTED;
return true;
}
static bool comms_handle_dynamic_subtest_result(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
char errmsg[512];
switch (context->state) {
case STATE_INITIAL:
case STATE_EXITED:
/* Dynamic subtest result when we're not even running? (Before exec or after exit) */
fprintf(stderr, "Error: Unexpected dynamic subtest result (binary wasn't running)\n");
return false;
case STATE_AFTER_EXEC:
/* Binary was running but a subtest wasn't. We don't know where to inject an error message. */
fprintf(stderr, "Error: Unexpected dynamic subtest result (subtest wasn't running)\n");
return false;
case STATE_BETWEEN_SUBTESTS:
/*
* Dynamic subtest result when a subtest is not
* running. We can't know which subtest this dynamic
* subtest was supposed to be in. But we can inject a
* warn into the previous subtest.
*/
snprintf(errmsg, sizeof(errmsg),
"\nrunner: Dynamic subtest %s result when not inside a subtest. This is a test bug.\n",
helper.dynamicsubtestresult.name);
append_line(&context->errbuf, &context->errbuflen, errmsg);
/* Leave the state as is and hope for the best */
return true;
case STATE_BETWEEN_DYNAMIC_SUBTESTS:
/*
* Result without starting. There's no
* skip_subtests_henceforth equivalent for dynamic
* subtests so this shouldn't happen, but we can
* handle it nevertheless.
*/
comms_finish_dynamic_subtest(context);
/* fallthrough */
case STATE_SUBTEST_STARTED:
/* Result without starting, but we aren't collecting for a dynamic subtest yet */
comms_add_new_dynamic_subtest(context, helper.dynamicsubtestresult.name);
break;
case STATE_DYNAMIC_SUBTEST_STARTED:
/* Normal flow */
break;
default:
assert(false); /* unreachable */
}
comms_inject_subtest_end_log(context,
DYNAMIC_SUBTEST_RESULT,
helper.dynamicsubtestresult.name,
helper.dynamicsubtestresult.result,
helper.dynamicsubtestresult.timeused);
/* Next dynamic subtest, if any, will begin its logs right after that result line */
context->nextdynoutidx = context->outbuflen;
context->nextdynerridx = context->errbuflen;
/*
* Only store the actual result from the packet if we don't
* already have one. If we do, it's from an override.
*/
if (context->dynamicsubtestresult == NULL) {
const char *mappedresult;
parse_result_string(helper.dynamicsubtestresult.result,
strlen(helper.dynamicsubtestresult.result),
&mappedresult, NULL);
context->dynamicsubtestresult = strdup(mappedresult);
context->dynamic_subtest_execution_time = strtod(helper.dynamicsubtestresult.timeused, NULL);
}
context->state = STATE_BETWEEN_DYNAMIC_SUBTESTS;
return true;
}
static bool comms_handle_versionstring(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
free(context->igt_version);
context->igt_version = strdup(helper.versionstring.text);
return true;
}
static bool comms_handle_result_override(const struct runnerpacket *packet,
runnerpacket_read_helper helper,
void *userdata)
{
struct comms_context *context = userdata;
if (context->current_dynamic_subtest) {
free(context->dynamicsubtestresult);
context->dynamicsubtestresult = strdup(helper.resultoverride.result);
}
free(context->subtestresult);
context->subtestresult = strdup(helper.resultoverride.result);
return true;
}
static int fill_from_comms(int fd,
struct job_list_entry *entry,
struct subtest_list *subtests,
struct results *results)
{
struct comms_context context = {};
struct comms_visitor visitor = {
.log = comms_handle_log,
.exec = comms_handle_exec,
.exit = comms_handle_exit,
.subtest_start = comms_handle_subtest_start,
.subtest_result = comms_handle_subtest_result,
.dynamic_subtest_start = comms_handle_dynamic_subtest_start,
.dynamic_subtest_result = comms_handle_dynamic_subtest_result,
.versionstring = comms_handle_versionstring,
.result_override = comms_handle_result_override,
.userdata = &context,
};
char piglit_name[256];
int ret = COMMSPARSE_EMPTY;
if (fd < 0)
return COMMSPARSE_EMPTY;
context.entry = entry;
context.binary = entry->binary;
generate_piglit_name(entry->binary, NULL, piglit_name, sizeof(piglit_name));
context.binaryruntimeobj = get_or_create_json_object(results->runtimes, piglit_name);
context.results = results;
context.subtests = subtests;
ret = comms_read_dump(fd, &visitor);
if (context.current_dynamic_subtest != NULL)
comms_finish_dynamic_subtest(&context);
if (context.current_test != NULL)
comms_finish_subtest(&context);
comms_free_context(&context);
return ret;
}
static bool result_is_requested(struct job_list_entry *entry,
const char *subtestname,
const char *dynamic_name)
{
char entryname[512];
size_t i;
if (dynamic_name)
snprintf(entryname, sizeof(entryname) - 1, "%s@%s", subtestname, dynamic_name);
else
strncpy(entryname, subtestname, sizeof(entryname) - 1);
for (i = 0; i < entry->subtest_count; i++) {
if (!strcmp(entry->subtests[i], entryname))
return true;
}
return false;
}
static void prune_subtests(struct settings *settings,
struct job_list_entry *entry,
struct subtest_list *subtests,
struct json_object *tests)
{
char piglit_name[256];
char dynamic_piglit_name[256];
size_t i, k;
if (settings->prune_mode == PRUNE_KEEP_ALL)
return;
for (i = 0; i < subtests->size; i++) {
generate_piglit_name(entry->binary, subtests->subs[i].name, piglit_name, sizeof(piglit_name));
if (settings->prune_mode == PRUNE_KEEP_DYNAMIC) {
if (subtests->subs[i].dynamic_size)
json_object_object_del(tests, piglit_name);
continue;
}
assert(settings->prune_mode == PRUNE_KEEP_SUBTESTS || settings->prune_mode == PRUNE_KEEP_REQUESTED);
if (settings->prune_mode == PRUNE_KEEP_REQUESTED &&
!result_is_requested(entry, subtests->subs[i].name, NULL)) {
json_object_object_del(tests, piglit_name);
}
for (k = 0; k < subtests->subs[i].dynamic_size; k++) {
if (settings->prune_mode == PRUNE_KEEP_SUBTESTS ||
(settings->prune_mode == PRUNE_KEEP_REQUESTED &&
!result_is_requested(entry, subtests->subs[i].name, subtests->subs[i].dynamic_names[k]))) {
generate_piglit_name_for_dynamic(piglit_name, subtests->subs[i].dynamic_names[k],
dynamic_piglit_name, sizeof(dynamic_piglit_name));
json_object_object_del(tests, dynamic_piglit_name);
}
}
}
}
static bool stderr_contains_warnings(const char *beg, const char *end)
{
struct match_needle needles[] = {
{ STARTING_SUBTEST, NULL },
{ SUBTEST_RESULT, is_subtest_result_line },
{ STARTING_DYNAMIC_SUBTEST, NULL },
{ DYNAMIC_SUBTEST_RESULT, is_subtest_result_line },
{ NULL, NULL },
};
struct matches matches;
size_t i = 0;
bool found = false;
matches = find_matches(beg, end, needles);
while (i < matches.size) {
if (matches.items[i].where != beg) {
found = true;
break;
}
beg = next_line(beg, end);
i++;
}
free_matches(&matches);
return found;
}
static bool json_field_has_data(struct json_object *obj, const char *key)
{
struct json_object *field;
if (json_object_object_get_ex(obj, key, &field))
return strcmp(json_object_get_string(field), "");
return false;
}
static void override_completely_empty_results(struct json_object *obj)
{
if (json_field_has_data(obj, "out") ||
json_field_has_data(obj, "err") ||
json_field_has_data(obj, "dmesg"))
return;
json_object_object_add(obj, "out",
json_object_new_string("This test didn't produce any output. "
"The machine probably rebooted ungracefully.\n"));
set_result(obj, "incomplete");
}
static void override_result_single(struct json_object *obj)
{
const char *errtext = "", *result = "";
struct json_object *textobj;
bool dmesgwarns = false;
if (json_object_object_get_ex(obj, "err", &textobj))
errtext = json_object_get_string(textobj);
if (json_object_object_get_ex(obj, "result", &textobj))
result = json_object_get_string(textobj);
if (json_object_object_get_ex(obj, "dmesg-warnings", &textobj))
dmesgwarns = true;
if (!strcmp(result, "pass") &&
stderr_contains_warnings(errtext, errtext + strlen(errtext))) {
set_result(obj, "warn");
result = "warn";
}
if (dmesgwarns) {
if (!strcmp(result, "pass") || !strcmp(result, "warn")) {
set_result(obj, "dmesg-warn");
} else if (!strcmp(result, "fail")) {
set_result(obj, "dmesg-fail");
}
}
override_completely_empty_results(obj);
}
static void override_results(char *binary,
struct subtest_list *subtests,
struct json_object *tests)
{
struct json_object *obj;
char piglit_name[256];
char dynamic_piglit_name[256];
size_t i, k;
if (subtests->size == 0) {
generate_piglit_name(binary, NULL, piglit_name, sizeof(piglit_name));
obj = get_or_create_json_object(tests, piglit_name);
override_result_single(obj);
return;
}
for (i = 0; i < subtests->size; i++) {
generate_piglit_name(binary, subtests->subs[i].name, piglit_name, sizeof(piglit_name));
obj = get_or_create_json_object(tests, piglit_name);
override_result_single(obj);
for (k = 0; k < subtests->subs[i].dynamic_size; k++) {
generate_piglit_name_for_dynamic(piglit_name, subtests->subs[i].dynamic_names[k],
dynamic_piglit_name, sizeof(dynamic_piglit_name));
obj = get_or_create_json_object(tests, dynamic_piglit_name);
override_result_single(obj);
}
}
}
static struct json_object *get_totals_object(struct json_object *totals,
const char *key)
{
struct json_object *obj = NULL;
if (json_object_object_get_ex(totals, key, &obj))
return obj;
obj = json_object_new_object();
json_object_object_add(totals, key, obj);
json_object_object_add(obj, "crash", json_object_new_int(0));
json_object_object_add(obj, "pass", json_object_new_int(0));
json_object_object_add(obj, "dmesg-fail", json_object_new_int(0));
json_object_object_add(obj, "dmesg-warn", json_object_new_int(0));
json_object_object_add(obj, "skip", json_object_new_int(0));
json_object_object_add(obj, "incomplete", json_object_new_int(0));
json_object_object_add(obj, "abort", json_object_new_int(0));
json_object_object_add(obj, "timeout", json_object_new_int(0));
json_object_object_add(obj, "notrun", json_object_new_int(0));
json_object_object_add(obj, "fail", json_object_new_int(0));
json_object_object_add(obj, "warn", json_object_new_int(0));
return obj;
}
static void add_result_to_totals(struct json_object *totals,
const char *result)
{
json_object *numobj = NULL;
int old;
if (!json_object_object_get_ex(totals, result, &numobj)) {
fprintf(stderr, "Warning: Totals object without count for %s\n", result);
return;
}
old = json_object_get_int(numobj);
json_object_object_add(totals, result, json_object_new_int(old + 1));
}
static void add_to_totals(const char *binary,
struct subtest_list *subtests,
struct results *results)
{
struct json_object *test, *resultobj, *emptystrtotal, *roottotal, *binarytotal;
char piglit_name[256];
char dynamic_piglit_name[256];
const char *result;
size_t i, k;
generate_piglit_name(binary, NULL, piglit_name, sizeof(piglit_name));
emptystrtotal = get_totals_object(results->totals, "");
roottotal = get_totals_object(results->totals, "root");
binarytotal = get_totals_object(results->totals, piglit_name);
if (subtests->size == 0) {
test = get_or_create_json_object(results->tests, piglit_name);
if (!json_object_object_get_ex(test, "result", &resultobj)) {
fprintf(stderr, "Warning: No results set for %s\n", piglit_name);
return;
}
result = json_object_get_string(resultobj);
add_result_to_totals(emptystrtotal, result);
add_result_to_totals(roottotal, result);
add_result_to_totals(binarytotal, result);
return;
}
for (i = 0; i < subtests->size; i++) {
generate_piglit_name(binary, subtests->subs[i].name, piglit_name, sizeof(piglit_name));
if (json_object_object_get_ex(results->tests, piglit_name, &test)) {
if (!json_object_object_get_ex(test, "result", &resultobj)) {
fprintf(stderr, "Warning: No results set for %s\n", piglit_name);
return;
}
result = json_object_get_string(resultobj);
add_result_to_totals(emptystrtotal, result);
add_result_to_totals(roottotal, result);
add_result_to_totals(binarytotal, result);
}
for (k = 0; k < subtests->subs[i].dynamic_size; k++) {
generate_piglit_name_for_dynamic(piglit_name, subtests->subs[i].dynamic_names[k],
dynamic_piglit_name, sizeof(dynamic_piglit_name));
if (json_object_object_get_ex(results->tests, dynamic_piglit_name, &test)) {
if (!json_object_object_get_ex(test, "result", &resultobj)) {
fprintf(stderr, "Warning: No results set for %s\n", dynamic_piglit_name);
return;
}
result = json_object_get_string(resultobj);
add_result_to_totals(emptystrtotal, result);
add_result_to_totals(roottotal, result);
add_result_to_totals(binarytotal, result);
}
}
}
}
static bool parse_test_directory(int dirfd,
struct job_list_entry *entry,
struct settings *settings,
struct results *results)
{
int fds[_F_LAST];
struct subtest_list subtests = {};
int commsparsed;
if (!open_output_files_rdonly(dirfd, fds)) {
struct stat statbuf;
size_t fsizes[_F_LAST]; /* checks for both comm and out/err empty */
int fst_err;
for (int i = 0; i < _F_LAST; ++i) {
fsizes[i] = 0;
if (fds[i] > 0) {
fst_err = fstat(fds[i], &statbuf);
if (!fst_err)
fsizes[i] = statbuf.st_size;
else
fprintf(stderr, "results: %s: stats failed: %s\n",
get_out_filename(i), strerror(fst_err));
} else {
fprintf(stderr, "results: %s: open failed: %s\n",
get_out_filename(i), strerror(-fds[i]));
}
}
/* out/err/dmesg.txt should exist, otherwise there was crash */
if (fds[_F_OUT] < 0 || fds[_F_ERR] < 0 || fds[_F_DMESG] < 0) {
fprintf(stderr, "results: missing output file(s)\n");
close_outputs(fds);
return false;
}
if (!fsizes[_F_SOCKET] && !fsizes[_F_OUT] && !fsizes[_F_ERR]) {
if (!fsizes[_F_DMESG])
fprintf(stderr, "results: empty output files\n");
else
fprintf(stderr, "results: comms/err/out empty, using only dmesg\n");
}
}
/*
* Get test output from socket comms if it exists, otherwise
* parse stdout/stderr
*/
commsparsed = fill_from_comms(fds[_F_SOCKET], entry, &subtests, results);
if (commsparsed == COMMSPARSE_ERROR) {
fprintf(stderr, "Error parsing output files (comms)\n");
}
if (commsparsed == COMMSPARSE_EMPTY) {
/*
* fill_from_journal fills the subtests struct and
* adds timeout results where applicable.
*/
if (fds[_F_JOURNAL] > 0)
fill_from_journal(fds[_F_JOURNAL], entry, &subtests, results);
if (!fill_from_output(fds[_F_OUT], entry->binary, "out", &subtests, results->tests) ||
!fill_from_output(fds[_F_ERR], entry->binary, "err", &subtests, results->tests)) {
fprintf(stderr, "Error parsing output files (out.txt, err.txt)\n");
}
}
if (!fill_from_dmesg(fds[_F_DMESG], settings, entry->binary, &subtests, results->tests)) {
fprintf(stderr, "Error parsing output files (dmesg.txt)\n");
}
override_results(entry->binary, &subtests, results->tests);
prune_subtests(settings, entry, &subtests, results->tests);
add_to_totals(entry->binary, &subtests, results);
close_outputs(fds);
free_subtests(&subtests);
return true;
}
static void try_add_notrun_results(const struct job_list_entry *entry,
const struct settings *settings,
struct results *results)
{
struct subtest_list subtests = {};
struct json_object *current_test;
size_t i;
if (entry->subtest_count == 0) {
char piglit_name[256];
/* We cannot distinguish no-subtests from run-all-subtests in multiple-mode */
if (settings->multiple_mode)
return;
generate_piglit_name(entry->binary, NULL, piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(results->tests, piglit_name);
json_object_object_add(current_test, "out", json_object_new_string(""));
json_object_object_add(current_test, "err", json_object_new_string(""));
json_object_object_add(current_test, "dmesg", json_object_new_string(""));
json_object_object_add(current_test, "result", json_object_new_string("notrun"));
}
for (i = 0; i < entry->subtest_count; i++) {
char piglit_name[256];
generate_piglit_name(entry->binary, entry->subtests[i], piglit_name, sizeof(piglit_name));
current_test = get_or_create_json_object(results->tests, piglit_name);
json_object_object_add(current_test, "out", json_object_new_string(""));
json_object_object_add(current_test, "err", json_object_new_string(""));
json_object_object_add(current_test, "dmesg", json_object_new_string(""));
json_object_object_add(current_test, "result", json_object_new_string("notrun"));
add_subtest(&subtests, strdup(entry->subtests[i]));
}
add_to_totals(entry->binary, &subtests, results);
free_subtests(&subtests);
}
static void create_result_root_nodes(struct json_object *root,
struct results *results)
{
results->tests = json_object_new_object();
json_object_object_add(root, "tests", results->tests);
results->totals = json_object_new_object();
json_object_object_add(root, "totals", results->totals);
results->runtimes = json_object_new_object();
json_object_object_add(root, "runtimes", results->runtimes);
}
struct json_object *generate_results_json(int dirfd)
{
struct settings settings;
struct job_list job_list;
struct json_object *obj, *elapsed, *arr;
struct results results;
int testdirfd, fd;
size_t i;
init_settings(&settings);
init_job_list(&job_list);
if (!read_settings_from_dir(&settings, dirfd)) {
fprintf(stderr, "resultgen: Cannot parse settings\n");
return NULL;
}
if (!read_job_list(&job_list, dirfd)) {
fprintf(stderr, "resultgen: Cannot parse job list\n");
return NULL;
}
obj = json_object_new_object();
json_object_object_add(obj, "__type__", json_object_new_string("TestrunResult"));
json_object_object_add(obj, "results_version", json_object_new_int(10));
json_object_object_add(obj, "name",
settings.name ?
json_object_new_string(settings.name) :
json_object_new_string(""));
if ((fd = openat(dirfd, "uname.txt", O_RDONLY)) >= 0) {
char buf[128];
ssize_t r = read(fd, buf, sizeof(buf));
if (r > 0 && buf[r - 1] == '\n')
r--;
json_object_object_add(obj, "uname",
new_escaped_json_string(buf, r));
close(fd);
}
arr = json_object_new_array();
for (i = 0; i < settings.cmdline.argc; i++)
json_object_array_add(arr, json_object_new_string(settings.cmdline.argv[i]));
json_object_object_add(obj, "cmdline", arr);
elapsed = json_object_new_object();
json_object_object_add(elapsed, "__type__", json_object_new_string("TimeAttribute"));
if ((fd = openat(dirfd, "starttime.txt", O_RDONLY)) >= 0) {
char buf[128] = {};
read(fd, buf, sizeof(buf));
json_object_object_add(elapsed, "start", json_object_new_double(atof(buf)));
close(fd);
}
if ((fd = openat(dirfd, "endtime.txt", O_RDONLY)) >= 0) {
char buf[128] = {};
read(fd, buf, sizeof(buf));
json_object_object_add(elapsed, "end", json_object_new_double(atof(buf)));
close(fd);
}
json_object_object_add(obj, "time_elapsed", elapsed);
create_result_root_nodes(obj, &results);
/*
* Result fields that won't be added:
*
* - glxinfo
* - wglinfo
* - clinfo
*
* Result fields that are TODO:
*
* - lspci
* - options
*/
for (i = 0; i < job_list.size; i++) {
char name[16];
snprintf(name, 16, "%zd", i);
fprintf(stderr, "results: parsing output: %s/ for test: %s\n",
name, job_list.entries[i].binary);
if ((testdirfd = openat(dirfd, name, O_DIRECTORY | O_RDONLY)) < 0) {
if (settings.log_level >= LOG_LEVEL_NORMAL)
fprintf(stderr, "results: no output, setting notrun\n");
try_add_notrun_results(&job_list.entries[i], &settings, &results);
continue;
}
if (!parse_test_directory(testdirfd, &job_list.entries[i], &settings, &results)) {
if (settings.log_level >= LOG_LEVEL_NORMAL)
fprintf(stderr, "results: no useful output, setting notrun\n");
try_add_notrun_results(&job_list.entries[i], &settings, &results);
}
close(testdirfd);
}
if ((fd = openat(dirfd, "aborted.txt", O_RDONLY)) >= 0) {
char buf[4096];
char piglit_name[] = "igt@runner@aborted";
struct subtest_list abortsub = {};
struct json_object *aborttest = get_or_create_json_object(results.tests, piglit_name);
ssize_t s;
add_subtest(&abortsub, strdup("aborted"));
s = read(fd, buf, sizeof(buf));
json_object_object_add(aborttest, "out",
new_escaped_json_string(buf, s));
json_object_object_add(aborttest, "err",
json_object_new_string(""));
json_object_object_add(aborttest, "dmesg",
json_object_new_string(""));
json_object_object_add(aborttest, "result",
json_object_new_string("fail"));
add_to_totals("runner", &abortsub, &results);
free_subtests(&abortsub);
close(fd);
}
clear_settings(&settings);
free_job_list(&job_list);
return obj;
}
bool generate_results(int dirfd)
{
struct json_object *obj = generate_results_json(dirfd);
const char *json_string;
int resultsfd;
if (obj == NULL)
return false;
/* TODO: settings.overwrite */
if ((resultsfd = openat(dirfd, "results.json", O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
fprintf(stderr, "resultgen: Cannot create results file\n");
return false;
}
json_string = json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PRETTY);
if (json_string == NULL) {
fprintf(stderr, "resultgen: Failed to create json representation of the results.\n");
fprintf(stderr, " This usually means that the results are too big\n");
fprintf(stderr, " to fit in the memory as the text representation\n");
fprintf(stderr, " is being created.\n\n");
fprintf(stderr, " Either something was spamming the logs or your\n");
fprintf(stderr, " system is very low on free mem.\n");
close(resultsfd);
return false;
}
write(resultsfd, json_string, strlen(json_string));
close(resultsfd);
return true;
}
bool generate_results_path(char *resultspath)
{
int dirfd = open(resultspath, O_DIRECTORY | O_RDONLY);
if (dirfd < 0)
return false;
return generate_results(dirfd);
}
|