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
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ProfileBufferEntry.h"
#include "mozilla/ProfilerMarkers.h"
#include "platform.h"
#include "ProfileBuffer.h"
#include "ProfiledThreadData.h"
#include "ProfilerBacktrace.h"
#include "ProfilerRustBindings.h"
#include "js/ProfilingFrameIterator.h"
#include "jsapi.h"
#include "jsfriendapi.h"
#include "mozilla/Base64.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/Logging.h"
#include "mozilla/JSONStringWriteFuncs.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Sprintf.h"
#include "mozilla/StackWalk.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "ProfilerCodeAddressService.h"
#include <ostream>
#include <type_traits>
using namespace mozilla;
using namespace mozilla::literals::ProportionValue_literals;
////////////////////////////////////////////////////////////////////////
// BEGIN ProfileBufferEntry
ProfileBufferEntry::ProfileBufferEntry()
: mKind(Kind::INVALID), mStorage{0, 0, 0, 0, 0, 0, 0, 0} {}
// aString must be a static string.
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, const char* aString)
: mKind(aKind) {
MOZ_ASSERT(aKind == Kind::Label);
memcpy(mStorage, &aString, sizeof(aString));
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, char aChars[kNumChars])
: mKind(aKind) {
MOZ_ASSERT(aKind == Kind::DynamicStringFragment);
memcpy(mStorage, aChars, kNumChars);
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, void* aPtr) : mKind(aKind) {
memcpy(mStorage, &aPtr, sizeof(aPtr));
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, double aDouble)
: mKind(aKind) {
memcpy(mStorage, &aDouble, sizeof(aDouble));
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, int aInt) : mKind(aKind) {
memcpy(mStorage, &aInt, sizeof(aInt));
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, int64_t aInt64)
: mKind(aKind) {
memcpy(mStorage, &aInt64, sizeof(aInt64));
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, uint64_t aUint64)
: mKind(aKind) {
memcpy(mStorage, &aUint64, sizeof(aUint64));
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, uint32_t aUint32)
: mKind(aKind) {
memcpy(mStorage, &aUint32, sizeof(aUint32));
}
ProfileBufferEntry::ProfileBufferEntry(Kind aKind, ProfilerThreadId aThreadId)
: mKind(aKind) {
static_assert(std::is_trivially_copyable_v<ProfilerThreadId>);
static_assert(sizeof(aThreadId) <= sizeof(mStorage));
memcpy(mStorage, &aThreadId, sizeof(aThreadId));
}
const char* ProfileBufferEntry::GetString() const {
const char* result;
memcpy(&result, mStorage, sizeof(result));
return result;
}
void* ProfileBufferEntry::GetPtr() const {
void* result;
memcpy(&result, mStorage, sizeof(result));
return result;
}
double ProfileBufferEntry::GetDouble() const {
double result;
memcpy(&result, mStorage, sizeof(result));
return result;
}
int ProfileBufferEntry::GetInt() const {
int result;
memcpy(&result, mStorage, sizeof(result));
return result;
}
int64_t ProfileBufferEntry::GetInt64() const {
int64_t result;
memcpy(&result, mStorage, sizeof(result));
return result;
}
uint64_t ProfileBufferEntry::GetUint64() const {
uint64_t result;
memcpy(&result, mStorage, sizeof(result));
return result;
}
uint32_t ProfileBufferEntry::GetUint32() const {
uint32_t result;
memcpy(&result, mStorage, sizeof(result));
return result;
}
ProfilerThreadId ProfileBufferEntry::GetThreadId() const {
ProfilerThreadId result;
static_assert(std::is_trivially_copyable_v<ProfilerThreadId>);
memcpy(&result, mStorage, sizeof(result));
return result;
}
void ProfileBufferEntry::CopyCharsInto(char (&aOutArray)[kNumChars]) const {
memcpy(aOutArray, mStorage, kNumChars);
}
// END ProfileBufferEntry
////////////////////////////////////////////////////////////////////////
struct TypeInfo {
Maybe<nsCString> mKeyedBy;
Maybe<nsCString> mName;
Maybe<nsCString> mLocation;
Maybe<unsigned> mLineNumber;
};
// As mentioned in ProfileBufferEntry.h, the JSON format contains many
// arrays whose elements are laid out according to various schemas to help
// de-duplication. This RAII class helps write these arrays by keeping track of
// the last non-null element written and adding the appropriate number of null
// elements when writing new non-null elements. It also automatically opens and
// closes an array element on the given JSON writer.
//
// You grant the AutoArraySchemaWriter exclusive access to the JSONWriter and
// the UniqueJSONStrings objects for the lifetime of AutoArraySchemaWriter. Do
// not access them independently while the AutoArraySchemaWriter is alive.
// If you need to add complex objects, call FreeFormElement(), which will give
// you temporary access to the writer.
//
// Example usage:
//
// // Define the schema of elements in this type of array: [FOO, BAR, BAZ]
// enum Schema : uint32_t {
// FOO = 0,
// BAR = 1,
// BAZ = 2
// };
//
// AutoArraySchemaWriter writer(someJsonWriter, someUniqueStrings);
// if (shouldWriteFoo) {
// writer.IntElement(FOO, getFoo());
// }
// ... etc ...
//
// The elements need to be added in-order.
class MOZ_RAII AutoArraySchemaWriter {
public:
explicit AutoArraySchemaWriter(SpliceableJSONWriter& aWriter)
: mJSONWriter(aWriter), mNextFreeIndex(0) {
mJSONWriter.StartArrayElement();
}
~AutoArraySchemaWriter() { mJSONWriter.EndArray(); }
template <typename T>
void IntElement(uint32_t aIndex, T aValue) {
static_assert(!std::is_same_v<T, uint64_t>,
"Narrowing uint64 -> int64 conversion not allowed");
FillUpTo(aIndex);
mJSONWriter.IntElement(static_cast<int64_t>(aValue));
}
void DoubleElement(uint32_t aIndex, double aValue) {
FillUpTo(aIndex);
mJSONWriter.DoubleElement(aValue);
}
void TimeMsElement(uint32_t aIndex, double aTime_ms) {
FillUpTo(aIndex);
mJSONWriter.TimeDoubleMsElement(aTime_ms);
}
void BoolElement(uint32_t aIndex, bool aValue) {
FillUpTo(aIndex);
mJSONWriter.BoolElement(aValue);
}
protected:
SpliceableJSONWriter& Writer() { return mJSONWriter; }
void FillUpTo(uint32_t aIndex) {
MOZ_ASSERT(aIndex >= mNextFreeIndex);
mJSONWriter.NullElements(aIndex - mNextFreeIndex);
mNextFreeIndex = aIndex + 1;
}
private:
SpliceableJSONWriter& mJSONWriter;
uint32_t mNextFreeIndex;
};
// Same as AutoArraySchemaWriter, but this can also write strings (output as
// indexes into the table of unique strings).
class MOZ_RAII AutoArraySchemaWithStringsWriter : public AutoArraySchemaWriter {
public:
AutoArraySchemaWithStringsWriter(SpliceableJSONWriter& aWriter,
UniqueJSONStrings& aStrings)
: AutoArraySchemaWriter(aWriter), mStrings(aStrings) {}
void StringElement(uint32_t aIndex, const Span<const char>& aValue) {
FillUpTo(aIndex);
mStrings.WriteElement(Writer(), aValue);
}
private:
UniqueJSONStrings& mStrings;
};
Maybe<UniqueStacks::StackKey> UniqueStacks::BeginStack(const FrameKey& aFrame) {
if (Maybe<uint32_t> frameIndex = GetOrAddFrameIndex(aFrame); frameIndex) {
return Some(StackKey(*frameIndex));
}
return Nothing{};
}
Vector<JITFrameInfoForBufferRange>&&
JITFrameInfo::MoveRangesWithNewFailureLatch(FailureLatch& aFailureLatch) && {
aFailureLatch.SetFailureFrom(mLocalFailureLatchSource);
return std::move(mRanges);
}
UniquePtr<UniqueJSONStrings>&&
JITFrameInfo::MoveUniqueStringsWithNewFailureLatch(
FailureLatch& aFailureLatch) && {
if (mUniqueStrings) {
mUniqueStrings->ChangeFailureLatchAndForwardState(aFailureLatch);
} else {
aFailureLatch.SetFailureFrom(mLocalFailureLatchSource);
}
return std::move(mUniqueStrings);
}
Maybe<UniqueStacks::StackKey> UniqueStacks::AppendFrame(
const StackKey& aStack, const FrameKey& aFrame) {
if (Maybe<uint32_t> stackIndex = GetOrAddStackIndex(aStack); stackIndex) {
if (Maybe<uint32_t> frameIndex = GetOrAddFrameIndex(aFrame); frameIndex) {
return Some(StackKey(aStack, *stackIndex, *frameIndex));
}
}
return Nothing{};
}
JITFrameInfoForBufferRange JITFrameInfoForBufferRange::Clone() const {
JITFrameInfoForBufferRange::JITAddressToJITFramesMap jitAddressToJITFramesMap;
MOZ_RELEASE_ASSERT(
jitAddressToJITFramesMap.reserve(mJITAddressToJITFramesMap.count()));
for (auto iter = mJITAddressToJITFramesMap.iter(); !iter.done();
iter.next()) {
const mozilla::Vector<JITFrameKey>& srcKeys = iter.get().value();
mozilla::Vector<JITFrameKey> destKeys;
MOZ_RELEASE_ASSERT(destKeys.appendAll(srcKeys));
jitAddressToJITFramesMap.putNewInfallible(iter.get().key(),
std::move(destKeys));
}
JITFrameInfoForBufferRange::JITFrameToFrameJSONMap jitFrameToFrameJSONMap;
MOZ_RELEASE_ASSERT(
jitFrameToFrameJSONMap.reserve(mJITFrameToFrameJSONMap.count()));
for (auto iter = mJITFrameToFrameJSONMap.iter(); !iter.done(); iter.next()) {
jitFrameToFrameJSONMap.putNewInfallible(iter.get().key(),
iter.get().value());
}
return JITFrameInfoForBufferRange{mRangeStart, mRangeEnd,
std::move(jitAddressToJITFramesMap),
std::move(jitFrameToFrameJSONMap)};
}
JITFrameInfo::JITFrameInfo(const JITFrameInfo& aOther,
mozilla::ProgressLogger aProgressLogger)
: mUniqueStrings(MakeUniqueFallible<UniqueJSONStrings>(
mLocalFailureLatchSource, *aOther.mUniqueStrings,
aProgressLogger.CreateSubLoggerFromTo(
0_pc, "Creating JIT frame info unique strings...", 49_pc,
"Created JIT frame info unique strings"))) {
if (!mUniqueStrings) {
mLocalFailureLatchSource.SetFailure(
"OOM in JITFrameInfo allocating mUniqueStrings");
return;
}
if (mRanges.reserve(aOther.mRanges.length())) {
for (auto&& [i, progressLogger] :
aProgressLogger.CreateLoopSubLoggersFromTo(50_pc, 100_pc,
aOther.mRanges.length(),
"Copying JIT frame info")) {
mRanges.infallibleAppend(aOther.mRanges[i].Clone());
}
} else {
mLocalFailureLatchSource.SetFailure("OOM in JITFrameInfo resizing mRanges");
}
}
bool UniqueStacks::FrameKey::NormalFrameData::operator==(
const NormalFrameData& aOther) const {
return mLocation == aOther.mLocation &&
mRelevantForJS == aOther.mRelevantForJS &&
mBaselineInterp == aOther.mBaselineInterp &&
mInnerWindowID == aOther.mInnerWindowID &&
mSourceId == aOther.mSourceId && mLine == aOther.mLine &&
mColumn == aOther.mColumn && mCategoryPair == aOther.mCategoryPair;
}
bool UniqueStacks::FrameKey::JITFrameData::operator==(
const JITFrameData& aOther) const {
return mCanonicalAddress == aOther.mCanonicalAddress &&
mDepth == aOther.mDepth && mRangeIndex == aOther.mRangeIndex;
}
// Consume aJITFrameInfo by stealing its string table and its JIT frame info
// ranges. The JIT frame info contains JSON which refers to strings from the
// JIT frame info's string table, so our string table needs to have the same
// strings at the same indices.
UniqueStacks::UniqueStacks(
FailureLatch& aFailureLatch, JITFrameInfo&& aJITFrameInfo,
ProfilerCodeAddressService* aCodeAddressService /* = nullptr */,
const nsTHashMap<SourceId, IndexIntoSourceTable>*
aSourceIdToIndexMap /* = nullptr */)
: mUniqueStrings(std::move(aJITFrameInfo)
.MoveUniqueStringsWithNewFailureLatch(aFailureLatch)),
mCodeAddressService(aCodeAddressService),
mFrameTableWriter(aFailureLatch),
mStackTableWriter(aFailureLatch),
mJITInfoRanges(std::move(aJITFrameInfo)
.MoveRangesWithNewFailureLatch(aFailureLatch)),
mSourceIdToIndexMap(aSourceIdToIndexMap) {
if (!mUniqueStrings) {
SetFailure("Did not get mUniqueStrings from JITFrameInfo");
return;
}
mFrameTableWriter.StartBareList();
mStackTableWriter.StartBareList();
}
Maybe<uint32_t> UniqueStacks::GetOrAddStackIndex(const StackKey& aStack) {
if (Failed()) {
return Nothing{};
}
uint32_t count = mStackToIndexMap.count();
auto entry = mStackToIndexMap.lookupForAdd(aStack);
if (entry) {
MOZ_ASSERT(entry->value() < count);
return Some(entry->value());
}
if (!mStackToIndexMap.add(entry, aStack, count)) {
SetFailure("OOM in UniqueStacks::GetOrAddStackIndex");
return Nothing{};
}
StreamStack(aStack);
return Some(count);
}
Maybe<Vector<UniqueStacks::FrameKey>>
UniqueStacks::LookupFramesForJITAddressFromBufferPos(void* aJITAddress,
uint64_t aBufferPos) {
JITFrameInfoForBufferRange* rangeIter =
std::lower_bound(mJITInfoRanges.begin(), mJITInfoRanges.end(), aBufferPos,
[](const JITFrameInfoForBufferRange& aRange,
uint64_t aPos) { return aRange.mRangeEnd < aPos; });
MOZ_RELEASE_ASSERT(
rangeIter != mJITInfoRanges.end() &&
rangeIter->mRangeStart <= aBufferPos &&
aBufferPos < rangeIter->mRangeEnd,
"Buffer position of jit address needs to be in one of the ranges");
using JITFrameKey = JITFrameInfoForBufferRange::JITFrameKey;
const JITFrameInfoForBufferRange& jitFrameInfoRange = *rangeIter;
auto jitFrameKeys =
jitFrameInfoRange.mJITAddressToJITFramesMap.lookup(aJITAddress);
if (!jitFrameKeys) {
return Nothing();
}
// Map the array of JITFrameKeys to an array of FrameKeys, and ensure that
// each of the FrameKeys exists in mFrameToIndexMap.
Vector<FrameKey> frameKeys;
MOZ_RELEASE_ASSERT(frameKeys.initCapacity(jitFrameKeys->value().length()));
for (const JITFrameKey& jitFrameKey : jitFrameKeys->value()) {
FrameKey frameKey(jitFrameKey.mCanonicalAddress, jitFrameKey.mDepth,
rangeIter - mJITInfoRanges.begin());
uint32_t index = mFrameToIndexMap.count();
auto entry = mFrameToIndexMap.lookupForAdd(frameKey);
if (!entry) {
// We need to add this frame to our frame table. The JSON for this frame
// already exists in jitFrameInfoRange, we just need to splice it into
// the frame table and give it an index.
auto frameJSON =
jitFrameInfoRange.mJITFrameToFrameJSONMap.lookup(jitFrameKey);
MOZ_RELEASE_ASSERT(frameJSON, "Should have cached JSON for this frame");
mFrameTableWriter.Splice(frameJSON->value());
MOZ_RELEASE_ASSERT(mFrameToIndexMap.add(entry, frameKey, index));
}
MOZ_RELEASE_ASSERT(frameKeys.append(std::move(frameKey)));
}
return Some(std::move(frameKeys));
}
Maybe<uint32_t> UniqueStacks::GetOrAddFrameIndex(const FrameKey& aFrame) {
if (Failed()) {
return Nothing{};
}
uint32_t count = mFrameToIndexMap.count();
auto entry = mFrameToIndexMap.lookupForAdd(aFrame);
if (entry) {
MOZ_ASSERT(entry->value() < count);
return Some(entry->value());
}
if (!mFrameToIndexMap.add(entry, aFrame, count)) {
SetFailure("OOM in UniqueStacks::GetOrAddFrameIndex");
return Nothing{};
}
StreamNonJITFrame(aFrame);
return Some(count);
}
void UniqueStacks::SpliceFrameTableElements(SpliceableJSONWriter& aWriter) {
mFrameTableWriter.EndBareList();
aWriter.TakeAndSplice(mFrameTableWriter.TakeChunkedWriteFunc());
}
void UniqueStacks::SpliceStackTableElements(SpliceableJSONWriter& aWriter) {
mStackTableWriter.EndBareList();
aWriter.TakeAndSplice(mStackTableWriter.TakeChunkedWriteFunc());
}
[[nodiscard]] nsAutoCString UniqueStacks::FunctionNameOrAddress(void* aPC) {
nsAutoCString nameOrAddress;
if (!mCodeAddressService ||
!mCodeAddressService->GetFunction(aPC, nameOrAddress) ||
nameOrAddress.IsEmpty()) {
nameOrAddress.AppendASCII("0x");
// `AppendInt` only knows `uint32_t` or `uint64_t`, but because these are
// just aliases for *two* of (`unsigned`, `unsigned long`, and `unsigned
// long long`), a call with `uintptr_t` could use the third type and
// therefore would be ambiguous.
// So we want to force using exactly `uint32_t` or `uint64_t`, whichever
// matches the size of `uintptr_t`.
// (The outer cast to `uint` should then be a no-op.)
using uint = std::conditional_t<sizeof(uintptr_t) <= sizeof(uint32_t),
uint32_t, uint64_t>;
nameOrAddress.AppendInt(static_cast<uint>(reinterpret_cast<uintptr_t>(aPC)),
16);
}
return nameOrAddress;
}
void UniqueStacks::StreamStack(const StackKey& aStack) {
enum Schema : uint32_t { PREFIX = 0, FRAME = 1 };
AutoArraySchemaWriter writer(mStackTableWriter);
if (aStack.mPrefixStackIndex.isSome()) {
writer.IntElement(PREFIX, *aStack.mPrefixStackIndex);
}
writer.IntElement(FRAME, aStack.mFrameIndex);
}
void UniqueStacks::StreamNonJITFrame(const FrameKey& aFrame) {
if (Failed()) {
return;
}
using NormalFrameData = FrameKey::NormalFrameData;
enum Schema : uint32_t {
LOCATION = 0,
RELEVANT_FOR_JS = 1,
INNER_WINDOW_ID = 2,
IMPLEMENTATION = 3,
LINE = 4,
COLUMN = 5,
CATEGORY = 6,
SUBCATEGORY = 7
};
AutoArraySchemaWithStringsWriter writer(mFrameTableWriter, *mUniqueStrings);
const NormalFrameData& data = aFrame.mData.as<NormalFrameData>();
writer.StringElement(LOCATION,
data.GetLocationWithSourceIndex(mSourceIdToIndexMap));
writer.BoolElement(RELEVANT_FOR_JS, data.mRelevantForJS);
// It's okay to convert uint64_t to double here because DOM always creates IDs
// that are convertible to double.
writer.DoubleElement(INNER_WINDOW_ID, data.mInnerWindowID);
// The C++ interpreter is the default implementation so we only emit element
// for Baseline Interpreter frames.
if (data.mBaselineInterp) {
writer.StringElement(IMPLEMENTATION, MakeStringSpan("blinterp"));
}
if (data.mLine.isSome()) {
writer.IntElement(LINE, *data.mLine);
}
if (data.mColumn.isSome()) {
writer.IntElement(COLUMN, *data.mColumn);
}
if (data.mCategoryPair.isSome()) {
const JS::ProfilingCategoryPairInfo& info =
JS::GetProfilingCategoryPairInfo(*data.mCategoryPair);
writer.IntElement(CATEGORY, uint32_t(info.mCategory));
writer.IntElement(SUBCATEGORY, info.mSubcategoryIndex);
}
}
static void StreamJITFrame(JSContext* aContext, SpliceableJSONWriter& aWriter,
UniqueJSONStrings& aUniqueStrings,
const JS::ProfiledFrameHandle& aJITFrame,
const nsTHashMap<SourceId, IndexIntoSourceTable>*
aSourceIdToIndexMap = nullptr) {
enum Schema : uint32_t {
LOCATION = 0,
RELEVANT_FOR_JS = 1,
INNER_WINDOW_ID = 2,
IMPLEMENTATION = 3,
LINE = 4,
COLUMN = 5,
CATEGORY = 6,
SUBCATEGORY = 7
};
AutoArraySchemaWithStringsWriter writer(aWriter, aUniqueStrings);
uint32_t sourceId = aJITFrame.sourceId();
nsCString labelWithSourceIndex(aJITFrame.label());
if (sourceId && aSourceIdToIndexMap) {
auto index = aSourceIdToIndexMap->MaybeGet(sourceId);
if (index) {
labelWithSourceIndex.AppendLiteral("[");
labelWithSourceIndex.AppendInt(*index);
labelWithSourceIndex.AppendLiteral("]");
}
}
writer.StringElement(LOCATION, labelWithSourceIndex);
writer.BoolElement(RELEVANT_FOR_JS, false);
// It's okay to convert uint64_t to double here because DOM always creates IDs
// that are convertible to double.
// Realm ID is the name of innerWindowID inside JS code.
writer.DoubleElement(INNER_WINDOW_ID, aJITFrame.realmID());
JS::ProfilingFrameIterator::FrameKind frameKind = aJITFrame.frameKind();
MOZ_ASSERT(frameKind == JS::ProfilingFrameIterator::Frame_Ion ||
frameKind == JS::ProfilingFrameIterator::Frame_Baseline);
writer.StringElement(IMPLEMENTATION,
frameKind == JS::ProfilingFrameIterator::Frame_Ion
? MakeStringSpan("ion")
: MakeStringSpan("baseline"));
const JS::ProfilingCategoryPairInfo& info = JS::GetProfilingCategoryPairInfo(
frameKind == JS::ProfilingFrameIterator::Frame_Ion
? JS::ProfilingCategoryPair::JS_IonMonkey
: JS::ProfilingCategoryPair::JS_Baseline);
writer.IntElement(CATEGORY, uint32_t(info.mCategory));
writer.IntElement(SUBCATEGORY, info.mSubcategoryIndex);
}
static nsCString JSONForJITFrame(
JSContext* aContext, const JS::ProfiledFrameHandle& aJITFrame,
UniqueJSONStrings& aUniqueStrings,
const nsTHashMap<SourceId, IndexIntoSourceTable>* aSourceIdToIndexMap =
nullptr) {
nsCString json;
JSONStringRefWriteFunc jw(json);
SpliceableJSONWriter writer(jw, aUniqueStrings.SourceFailureLatch());
StreamJITFrame(aContext, writer, aUniqueStrings, aJITFrame,
aSourceIdToIndexMap);
return json;
}
void JITFrameInfo::AddInfoForRange(
uint64_t aRangeStart, uint64_t aRangeEnd, JSContext* aCx,
const std::function<void(const std::function<void(void*)>&)>&
aJITAddressProvider,
const nsTHashMap<SourceId, IndexIntoSourceTable>* aSourceIdToIndexMap) {
if (mLocalFailureLatchSource.Failed()) {
return;
}
if (aRangeStart == aRangeEnd) {
return;
}
MOZ_RELEASE_ASSERT(aRangeStart < aRangeEnd);
if (!mRanges.empty()) {
const JITFrameInfoForBufferRange& prevRange = mRanges.back();
MOZ_RELEASE_ASSERT(prevRange.mRangeEnd <= aRangeStart,
"Ranges must be non-overlapping and added in-order.");
}
using JITFrameKey = JITFrameInfoForBufferRange::JITFrameKey;
JITFrameInfoForBufferRange::JITAddressToJITFramesMap jitAddressToJITFrameMap;
JITFrameInfoForBufferRange::JITFrameToFrameJSONMap jitFrameToFrameJSONMap;
aJITAddressProvider([&](void* aJITAddress) {
// Make sure that we have cached data for aJITAddress.
auto addressEntry = jitAddressToJITFrameMap.lookupForAdd(aJITAddress);
if (!addressEntry) {
Vector<JITFrameKey> jitFrameKeys;
for (JS::ProfiledFrameHandle handle :
JS::GetProfiledFrames(aCx, aJITAddress)) {
uint32_t depth = jitFrameKeys.length();
JITFrameKey jitFrameKey{handle.canonicalAddress(), depth};
auto frameEntry = jitFrameToFrameJSONMap.lookupForAdd(jitFrameKey);
if (!frameEntry) {
if (!jitFrameToFrameJSONMap.add(
frameEntry, jitFrameKey,
JSONForJITFrame(aCx, handle, *mUniqueStrings,
aSourceIdToIndexMap))) {
mLocalFailureLatchSource.SetFailure(
"OOM in JITFrameInfo::AddInfoForRange adding jit->frame map");
return;
}
}
if (!jitFrameKeys.append(jitFrameKey)) {
mLocalFailureLatchSource.SetFailure(
"OOM in JITFrameInfo::AddInfoForRange adding jit frame key");
return;
}
}
if (!jitAddressToJITFrameMap.add(addressEntry, aJITAddress,
std::move(jitFrameKeys))) {
mLocalFailureLatchSource.SetFailure(
"OOM in JITFrameInfo::AddInfoForRange adding addr->jit map");
return;
}
}
});
if (!mRanges.append(JITFrameInfoForBufferRange{
aRangeStart, aRangeEnd, std::move(jitAddressToJITFrameMap),
std::move(jitFrameToFrameJSONMap)})) {
mLocalFailureLatchSource.SetFailure(
"OOM in JITFrameInfo::AddInfoForRange adding range");
return;
}
}
struct ProfileSample {
uint32_t mStack = 0;
double mTime = 0.0;
Maybe<double> mResponsiveness;
RunningTimes mRunningTimes;
Maybe<int32_t> mArgumentValues;
};
// Write CPU measurements with "Delta" unit, which is some amount of work that
// happened since the previous sample.
static void WriteDelta(AutoArraySchemaWriter& aSchemaWriter, uint32_t aProperty,
uint64_t aDelta) {
aSchemaWriter.IntElement(aProperty, int64_t(aDelta));
}
static void WriteSample(SpliceableJSONWriter& aWriter,
const ProfileSample& aSample) {
enum Schema : uint32_t {
STACK = 0,
TIME = 1,
EVENT_DELAY = 2,
ARGUMENT_VALUES = 3
#define RUNNING_TIME_SCHEMA(index, name, unit, jsonProperty) , name
PROFILER_FOR_EACH_RUNNING_TIME(RUNNING_TIME_SCHEMA)
#undef RUNNING_TIME_SCHEMA
};
AutoArraySchemaWriter writer(aWriter);
writer.IntElement(STACK, aSample.mStack);
writer.TimeMsElement(TIME, aSample.mTime);
if (aSample.mResponsiveness.isSome()) {
writer.DoubleElement(EVENT_DELAY, *aSample.mResponsiveness);
}
if (aSample.mArgumentValues.isSome()) {
writer.IntElement(ARGUMENT_VALUES, *aSample.mArgumentValues);
}
#define RUNNING_TIME_STREAM(index, name, unit, jsonProperty) \
aSample.mRunningTimes.GetJson##name##unit().apply( \
[&writer](const uint64_t& aValue) { \
Write##unit(writer, name, aValue); \
});
PROFILER_FOR_EACH_RUNNING_TIME(RUNNING_TIME_STREAM)
#undef RUNNING_TIME_STREAM
}
static void StreamMarkerAfterKind(
ProfileBufferEntryReader& aER,
ProcessStreamingContext& aProcessStreamingContext) {
ThreadStreamingContext* threadData = nullptr;
mozilla::base_profiler_markers_detail::DeserializeAfterKindAndStream(
aER,
[&](ProfilerThreadId aThreadId) -> baseprofiler::SpliceableJSONWriter* {
threadData =
aProcessStreamingContext.GetThreadStreamingContext(aThreadId);
return threadData ? &threadData->mMarkersDataWriter : nullptr;
},
[&](ProfileChunkedBuffer& aChunkedBuffer) {
ProfilerBacktrace backtrace("", &aChunkedBuffer);
MOZ_ASSERT(threadData,
"threadData should have been set before calling here");
backtrace.StreamJSON(threadData->mMarkersDataWriter,
aProcessStreamingContext.ProcessStartTime(),
*threadData->mUniqueStacks);
},
[&](mozilla::base_profiler_markers_detail::Streaming::DeserializerTag
aTag) {
MOZ_ASSERT(threadData,
"threadData should have been set before calling here");
size_t payloadSize = aER.RemainingBytes();
ProfileBufferEntryReader::DoubleSpanOfConstBytes spans =
aER.ReadSpans(payloadSize);
if (MOZ_LIKELY(spans.IsSingleSpan())) {
// Only a single span, we can just refer to it directly
// instead of copying it.
profiler::ffi::gecko_profiler_serialize_marker_for_tag(
aTag, spans.mFirstOrOnly.Elements(), payloadSize,
&threadData->mMarkersDataWriter);
} else {
// Two spans, we need to concatenate them by copying.
uint8_t* payloadBuffer = new uint8_t[payloadSize];
spans.CopyBytesTo(payloadBuffer);
profiler::ffi::gecko_profiler_serialize_marker_for_tag(
aTag, payloadBuffer, payloadSize,
&threadData->mMarkersDataWriter);
delete[] payloadBuffer;
}
});
}
class EntryGetter {
public:
explicit EntryGetter(
ProfileChunkedBuffer::Reader& aReader,
mozilla::FailureLatch& aFailureLatch,
mozilla::ProgressLogger aProgressLogger = {},
uint64_t aInitialReadPos = 0,
ProcessStreamingContext* aStreamingContextForMarkers = nullptr)
: mFailureLatch(aFailureLatch),
mStreamingContextForMarkers(aStreamingContextForMarkers),
mBlockIt(
aReader.At(ProfileBufferBlockIndex::CreateFromProfileBufferIndex(
aInitialReadPos))),
mBlockItEnd(aReader.end()),
mRangeStart(mBlockIt.BufferRangeStart().ConvertToProfileBufferIndex()),
mRangeSize(
double(mBlockIt.BufferRangeEnd().ConvertToProfileBufferIndex() -
mRangeStart)),
mProgressLogger(std::move(aProgressLogger)) {
SetLocalProgress(ProgressLogger::NO_LOCATION_UPDATE);
if (!ReadLegacyOrEnd()) {
// Find and read the next non-legacy entry.
Next();
}
}
bool Has() const {
return (!mFailureLatch.Failed()) && (mBlockIt != mBlockItEnd);
}
const ProfileBufferEntry& Get() const {
MOZ_ASSERT(Has() || mFailureLatch.Failed(),
"Caller should have checked `Has()` before `Get()`");
return mEntry;
}
void Next() {
MOZ_ASSERT(Has() || mFailureLatch.Failed(),
"Caller should have checked `Has()` before `Next()`");
++mBlockIt;
ReadUntilLegacyOrEnd();
}
// Hand off the current iterator to the caller, which may be used to read
// any kind of entries (legacy or modern).
ProfileChunkedBuffer::BlockIterator Iterator() const { return mBlockIt; }
// After `Iterator()` was used, we can restart from *after* its updated
// position.
void RestartAfter(const ProfileChunkedBuffer::BlockIterator& it) {
mBlockIt = it;
if (!Has()) {
return;
}
Next();
}
ProfileBufferBlockIndex CurBlockIndex() const {
return mBlockIt.CurrentBlockIndex();
}
uint64_t CurPos() const {
return CurBlockIndex().ConvertToProfileBufferIndex();
}
void SetLocalProgress(const char* aLocation) {
mProgressLogger.SetLocalProgress(
ProportionValue{double(CurBlockIndex().ConvertToProfileBufferIndex() -
mRangeStart) /
mRangeSize},
aLocation);
}
private:
// Try to read the entry at the current `mBlockIt` position.
// * If we're at the end of the buffer, just return `true`.
// * If there is a "legacy" entry (containing a real `ProfileBufferEntry`),
// read it into `mEntry`, and return `true` as well.
// * Otherwise the entry contains a "modern" type that cannot be read into
// `mEntry`, return `false` (so `EntryGetter` can skip to another entry).
bool ReadLegacyOrEnd() {
if (!Has()) {
return true;
}
// Read the entry "kind", which is always at the start of all entries.
ProfileBufferEntryReader er = *mBlockIt;
auto type = static_cast<ProfileBufferEntry::Kind>(
er.ReadObject<ProfileBufferEntry::KindUnderlyingType>());
MOZ_ASSERT(static_cast<ProfileBufferEntry::KindUnderlyingType>(type) <
static_cast<ProfileBufferEntry::KindUnderlyingType>(
ProfileBufferEntry::Kind::MODERN_LIMIT));
if (type >= ProfileBufferEntry::Kind::LEGACY_LIMIT) {
if (type == ProfileBufferEntry::Kind::Marker &&
mStreamingContextForMarkers) {
StreamMarkerAfterKind(er, *mStreamingContextForMarkers);
if (!Has()) {
return true;
}
SetLocalProgress("Processed marker");
}
er.SetRemainingBytes(0);
return false;
}
// Here, we have a legacy item, we need to read it from the start.
// Because the above `ReadObject` moved the reader, we ned to reset it to
// the start of the entry before reading the whole entry.
er = *mBlockIt;
er.ReadBytes(&mEntry, er.RemainingBytes());
return true;
}
void ReadUntilLegacyOrEnd() {
for (;;) {
if (ReadLegacyOrEnd()) {
// Either we're at the end, or we could read a legacy entry -> Done.
break;
}
// Otherwise loop around until we hit a legacy entry or the end.
++mBlockIt;
}
SetLocalProgress(ProgressLogger::NO_LOCATION_UPDATE);
}
mozilla::FailureLatch& mFailureLatch;
ProcessStreamingContext* const mStreamingContextForMarkers;
ProfileBufferEntry mEntry;
ProfileChunkedBuffer::BlockIterator mBlockIt;
const ProfileChunkedBuffer::BlockIterator mBlockItEnd;
// Progress logger, and the data needed to compute the current relative
// position in the buffer.
const mozilla::ProfileBufferIndex mRangeStart;
const double mRangeSize;
mozilla::ProgressLogger mProgressLogger;
};
// The following grammar shows legal sequences of profile buffer entries.
// The sequences beginning with a ThreadId entry are known as "samples".
//
// (
// ( /* Samples */
// ThreadId
// TimeBeforeCompactStack
// RunningTimes?
// UnresponsivenessDurationMs?
// CompactStack
// /* internally including:
// ( NativeLeafAddr
// | Label FrameFlags? DynamicStringFragment*
// LineNumber? CategoryPair?
// | JitReturnAddr
// )+
// */
// )
// | ( /* Reference to a previous identical sample */
// ThreadId
// TimeBeforeSameSample
// RunningTimes?
// SameSample
// )
// | Marker
// | ( /* Counters */
// CounterId
// Time
// (
// CounterKey
// Count
// Number?
// )*
// )
// | CollectionStart
// | CollectionEnd
// | Pause
// | Resume
// | ( ProfilerOverheadTime /* Sampling start timestamp */
// ProfilerOverheadDuration /* Lock acquisition */
// ProfilerOverheadDuration /* Expired markers cleaning */
// ProfilerOverheadDuration /* Counters */
// ProfilerOverheadDuration /* Threads */
// )
// )*
//
// The most complicated part is the stack entry sequence that begins with
// Label. Here are some examples.
//
// - ProfilingStack frames without a dynamic string:
//
// Label("js::RunScript")
// CategoryPair(JS::ProfilingCategoryPair::JS)
//
// Label("XREMain::XRE_main")
// LineNumber(4660)
// CategoryPair(JS::ProfilingCategoryPair::OTHER)
//
// Label("ElementRestyler::ComputeStyleChangeFor")
// LineNumber(3003)
// CategoryPair(JS::ProfilingCategoryPair::CSS)
//
// - ProfilingStack frames with a dynamic string:
//
// Label("nsObserverService::NotifyObservers")
// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_LABEL_FRAME))
// DynamicStringFragment("domwindo")
// DynamicStringFragment("wopened")
// LineNumber(291)
// CategoryPair(JS::ProfilingCategoryPair::OTHER)
//
// Label("")
// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_JS_FRAME))
// DynamicStringFragment("closeWin")
// DynamicStringFragment("dow (chr")
// DynamicStringFragment("ome://gl")
// DynamicStringFragment("obal/con")
// DynamicStringFragment("tent/glo")
// DynamicStringFragment("balOverl")
// DynamicStringFragment("ay.js:5)")
// DynamicStringFragment("") # this string holds the closing '\0'
// LineNumber(25)
// CategoryPair(JS::ProfilingCategoryPair::JS)
//
// Label("")
// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_JS_FRAME))
// DynamicStringFragment("bound (s")
// DynamicStringFragment("elf-host")
// DynamicStringFragment("ed:914)")
// LineNumber(945)
// CategoryPair(JS::ProfilingCategoryPair::JS)
//
// - A profiling stack frame with an overly long dynamic string:
//
// Label("")
// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_LABEL_FRAME))
// DynamicStringFragment("(too lon")
// DynamicStringFragment("g)")
// LineNumber(100)
// CategoryPair(JS::ProfilingCategoryPair::NETWORK)
//
// - A wasm JIT frame:
//
// Label("")
// FrameFlags(uint64_t(0))
// DynamicStringFragment("wasm-fun")
// DynamicStringFragment("ction[87")
// DynamicStringFragment("36] (blo")
// DynamicStringFragment("b:http:/")
// DynamicStringFragment("/webasse")
// DynamicStringFragment("mbly.org")
// DynamicStringFragment("/3dc5759")
// DynamicStringFragment("4-ce58-4")
// DynamicStringFragment("626-975b")
// DynamicStringFragment("-08ad116")
// DynamicStringFragment("30bc1:38")
// DynamicStringFragment("29856)")
//
// - A JS frame in a synchronous sample:
//
// Label("")
// FrameFlags(uint64_t(ProfilingStackFrame::Flags::IS_LABEL_FRAME))
// DynamicStringFragment("u (https")
// DynamicStringFragment("://perf-")
// DynamicStringFragment("html.io/")
// DynamicStringFragment("ac0da204")
// DynamicStringFragment("aaa44d75")
// DynamicStringFragment("a800.bun")
// DynamicStringFragment("dle.js:2")
// DynamicStringFragment("5)")
// Because this is a format entirely internal to the Profiler, any parsing
// error indicates a bug in the ProfileBuffer writing or the parser itself,
// or possibly flaky hardware.
#define ERROR_AND_CONTINUE(msg) \
{ \
fprintf(stderr, "ProfileBuffer parse error: %s", msg); \
MOZ_ASSERT(false, msg); \
continue; \
}
struct StreamingParametersForThread {
SpliceableJSONWriter& mWriter;
UniqueStacks& mUniqueStacks;
ThreadStreamingContext::PreviousStackState& mPreviousStackState;
uint32_t& mPreviousStack;
Maybe<SpliceableJSONWriter&> mShapesWriter;
StreamingParametersForThread(
SpliceableJSONWriter& aWriter, UniqueStacks& aUniqueStacks,
ThreadStreamingContext::PreviousStackState& aPreviousStackState,
uint32_t& aPreviousStack)
: mWriter(aWriter),
mUniqueStacks(aUniqueStacks),
mPreviousStackState(aPreviousStackState),
mPreviousStack(aPreviousStack) {}
StreamingParametersForThread(
SpliceableJSONWriter& aWriter, UniqueStacks& aUniqueStacks,
ThreadStreamingContext::PreviousStackState& aPreviousStackState,
uint32_t& aPreviousStack, SpliceableJSONWriter& aShapesWriter)
: mWriter(aWriter),
mUniqueStacks(aUniqueStacks),
mPreviousStackState(aPreviousStackState),
mPreviousStack(aPreviousStack) {
mShapesWriter.emplace(aShapesWriter);
}
};
#ifdef MOZ_EXECUTION_TRACING
template <typename GetStreamingParametersForThreadCallback>
void ProfileBuffer::MaybeStreamExecutionTraceToJSON(
GetStreamingParametersForThreadCallback&&
aGetStreamingParametersForThreadCallback,
double aSinceTime) const {
JS::ExecutionTrace trace;
if (!JS_TracerSnapshotTrace(trace)) {
return;
}
for (const JS::ExecutionTrace::TracedJSContext& context : trace.contexts) {
Maybe<StreamingParametersForThread> streamingParameters =
std::forward<GetStreamingParametersForThreadCallback>(
aGetStreamingParametersForThreadCallback)(context.id);
// Ignore samples that are for the wrong thread.
if (!streamingParameters) {
continue;
}
SpliceableJSONWriter& writer = streamingParameters->mWriter;
UniqueStacks& uniqueStacks = streamingParameters->mUniqueStacks;
SpliceableJSONWriter& shapesWriter = *streamingParameters->mShapesWriter;
mozilla::Vector<UniqueStacks::StackKey> frameStack;
Maybe<UniqueStacks::StackKey> maybeStack =
uniqueStacks.BeginStack(UniqueStacks::FrameKey("(root)"));
if (!maybeStack) {
writer.SetFailure("BeginStack failure");
continue;
}
UniqueStacks::StackKey stack = *maybeStack;
if (!frameStack.append(stack)) {
writer.SetFailure("frameStack append failure");
continue;
}
for (const JS::ExecutionTrace::TracedEvent& event : context.events) {
if (event.time < aSinceTime) {
continue;
}
if (event.kind == JS::ExecutionTrace::EventKind::Error) {
writer.SetFailure("Error during tracing (likely OOM)");
continue;
}
Maybe<int32_t> maybeArguments;
if (event.kind == JS::ExecutionTrace::EventKind::FunctionEnter) {
HashMap<uint32_t, size_t>::Ptr functionName =
context.atoms.lookup(event.functionEvent.functionNameId);
// This is uncommon, but if one of our ring buffers wraps around, we
// can end up with missing function name entries
const char* functionNameStr = "<expired>";
if (functionName) {
functionNameStr = &trace.stringBuffer[functionName->value()];
}
HashMap<uint32_t, size_t>::Ptr scriptUrl =
context.scriptUrls.lookup(event.functionEvent.scriptId);
// See the comment above functionNameStr
const char* scriptUrlStr = "<expired>";
if (scriptUrl) {
scriptUrlStr = &trace.stringBuffer[scriptUrl->value()];
}
nsAutoCStringN<1024> name(functionNameStr);
name.AppendPrintf(" (%s:%u:%u)", scriptUrlStr,
event.functionEvent.lineNumber,
event.functionEvent.column);
JS::ProfilingCategoryPair categoryPair;
switch (event.functionEvent.implementation) {
case JS::ExecutionTrace::ImplementationType::Interpreter:
categoryPair = JS::ProfilingCategoryPair::JS;
break;
case JS::ExecutionTrace::ImplementationType::Baseline:
categoryPair = JS::ProfilingCategoryPair::JS_Baseline;
break;
case JS::ExecutionTrace::ImplementationType::Ion:
categoryPair = JS::ProfilingCategoryPair::JS_IonMonkey;
break;
case JS::ExecutionTrace::ImplementationType::Wasm:
categoryPair = JS::ProfilingCategoryPair::JS_WasmOther;
break;
}
UniqueStacks::FrameKey newFrame(nsCString(name.get()), true, false,
event.functionEvent.realmID,
// Even though it says scriptId, this is
// actually sourceId. See bug 1980369.
event.functionEvent.scriptId, Nothing{},
Nothing{}, Some(categoryPair));
maybeStack = uniqueStacks.AppendFrame(stack, newFrame);
if (!maybeStack) {
writer.SetFailure("AppendFrame failure");
continue;
}
stack = *maybeStack;
if (!frameStack.append(stack)) {
writer.SetFailure("frameStack append failure");
continue;
}
maybeArguments = Some(event.functionEvent.values);
} else if (event.kind == JS::ExecutionTrace::EventKind::LabelEnter) {
UniqueStacks::FrameKey newFrame(
nsCString(&trace.stringBuffer[event.labelEvent.label]), true, false,
0, 0, Nothing{}, Nothing{}, Some(JS::ProfilingCategoryPair::DOM));
maybeStack = uniqueStacks.AppendFrame(stack, newFrame);
if (!maybeStack) {
writer.SetFailure("AppendFrame failure");
continue;
}
stack = *maybeStack;
if (!frameStack.append(stack)) {
writer.SetFailure("frameStack append failure");
continue;
}
} else {
MOZ_ASSERT(event.kind == JS::ExecutionTrace::EventKind::LabelLeave ||
event.kind == JS::ExecutionTrace::EventKind::FunctionLeave);
if (frameStack.length() > 0) {
frameStack.popBack();
}
if (frameStack.length() > 0) {
stack = frameStack[frameStack.length() - 1];
} else {
maybeStack =
uniqueStacks.BeginStack(UniqueStacks::FrameKey("(root)"));
if (!maybeStack) {
writer.SetFailure("BeginStack failure");
continue;
}
stack = *maybeStack;
if (!frameStack.append(stack)) {
writer.SetFailure("frameStack append failure");
continue;
}
}
}
const Maybe<uint32_t> stackIndex = uniqueStacks.GetOrAddStackIndex(stack);
if (!stackIndex) {
writer.SetFailure("Can't add unique string for stack");
continue;
}
WriteSample(writer, ProfileSample{*stackIndex, event.time, Nothing{},
RunningTimes{}, maybeArguments});
}
if (mozilla::Base64Encode(
reinterpret_cast<const char*>(context.valueBuffer.begin()),
context.valueBuffer.length(),
uniqueStacks.TracedValues()) != NS_OK) {
writer.SetFailure("Failed to Base64 encode traced values buffer");
}
uint32_t expectedShapeId = 0;
for (const JS::ExecutionTrace::ShapeSummary shape :
context.shapeSummaries) {
MOZ_RELEASE_ASSERT(shape.id >= expectedShapeId);
if (shape.id > expectedShapeId) {
shapesWriter.NullElements(shape.id - expectedShapeId);
}
expectedShapeId = shape.id + 1;
shapesWriter.StartArrayElement();
size_t stringBufferOffset = shape.stringBufferOffset;
size_t classNameLength = strlen(&trace.stringBuffer[stringBufferOffset]);
shapesWriter.StringElement(mozilla::Span<char>(
&trace.stringBuffer[stringBufferOffset], classNameLength));
stringBufferOffset += classNameLength + 1;
for (uint32_t propertyIndex = 0; propertyIndex < shape.numProperties;
propertyIndex++) {
size_t len = strlen(&trace.stringBuffer[stringBufferOffset]);
shapesWriter.StringElement(
mozilla::Span<char>(&trace.stringBuffer[stringBufferOffset], len));
stringBufferOffset += len + 1;
}
shapesWriter.EndArray();
}
}
}
#endif
// GetStreamingParametersForThreadCallback:
// (ProfilerThreadId) -> Maybe<StreamingParametersForThread>
template <typename GetStreamingParametersForThreadCallback>
ProfilerThreadId ProfileBuffer::DoStreamSamplesAndMarkersToJSON(
mozilla::FailureLatch& aFailureLatch,
GetStreamingParametersForThreadCallback&&
aGetStreamingParametersForThreadCallback,
double aSinceTime, ProcessStreamingContext* aStreamingContextForMarkers,
mozilla::ProgressLogger aProgressLogger) const {
UniquePtr<char[]> dynStrBuf = MakeUnique<char[]>(kMaxFrameKeyLength);
return mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(aReader,
"ProfileChunkedBuffer cannot be out-of-session when sampler is "
"running");
ProfilerThreadId processedThreadId;
EntryGetter e(*aReader, aFailureLatch, std::move(aProgressLogger),
/* aInitialReadPos */ 0, aStreamingContextForMarkers);
for (;;) {
// This block skips entries until we find the start of the next sample.
// This is useful in three situations.
//
// - The circular buffer overwrites old entries, so when we start parsing
// we might be in the middle of a sample, and we must skip forward to
// the start of the next sample.
//
// - We skip samples that don't have an appropriate ThreadId or Time.
//
// - We skip range Pause, Resume, CollectionStart, Marker, Counter
// and CollectionEnd entries between samples.
while (e.Has()) {
if (e.Get().IsThreadId()) {
break;
}
e.Next();
}
if (!e.Has()) {
break;
}
// Due to the skip_to_next_sample block above, if we have an entry here it
// must be a ThreadId entry.
MOZ_ASSERT(e.Get().IsThreadId());
ProfilerThreadId threadId = e.Get().GetThreadId();
e.Next();
Maybe<StreamingParametersForThread> streamingParameters =
std::forward<GetStreamingParametersForThreadCallback>(
aGetStreamingParametersForThreadCallback)(threadId);
// Ignore samples that are for the wrong thread.
if (!streamingParameters) {
continue;
}
SpliceableJSONWriter& writer = streamingParameters->mWriter;
UniqueStacks& uniqueStacks = streamingParameters->mUniqueStacks;
ThreadStreamingContext::PreviousStackState& previousStackState =
streamingParameters->mPreviousStackState;
uint32_t& previousStack = streamingParameters->mPreviousStack;
auto ReadStack = [&](EntryGetter& e, double time, uint64_t entryPosition,
const Maybe<double>& unresponsiveDuration,
const RunningTimes& runningTimes) {
if (writer.Failed()) {
return;
}
Maybe<UniqueStacks::StackKey> maybeStack =
uniqueStacks.BeginStack(UniqueStacks::FrameKey("(root)"));
if (!maybeStack) {
writer.SetFailure("BeginStack failure");
return;
}
UniqueStacks::StackKey stack = *maybeStack;
int numFrames = 0;
while (e.Has()) {
if (e.Get().IsNativeLeafAddr()) {
numFrames++;
void* pc = e.Get().GetPtr();
e.Next();
nsAutoCString functionNameOrAddress =
uniqueStacks.FunctionNameOrAddress(pc);
maybeStack = uniqueStacks.AppendFrame(
stack, UniqueStacks::FrameKey(functionNameOrAddress.get()));
if (!maybeStack) {
writer.SetFailure("AppendFrame failure");
return;
}
stack = *maybeStack;
} else if (e.Get().IsLabel()) {
numFrames++;
const char* label = e.Get().GetString();
e.Next();
using FrameFlags = js::ProfilingStackFrame::Flags;
uint32_t frameFlags = 0;
if (e.Has() && e.Get().IsFrameFlags()) {
frameFlags = uint32_t(e.Get().GetUint64());
e.Next();
}
bool relevantForJS =
frameFlags & uint32_t(FrameFlags::RELEVANT_FOR_JS);
bool isBaselineInterp =
frameFlags & uint32_t(FrameFlags::IS_BLINTERP_FRAME);
// Copy potential dynamic string fragments into dynStrBuf, so that
// dynStrBuf will then contain the entire dynamic string.
size_t i = 0;
dynStrBuf[0] = '\0';
while (e.Has()) {
if (e.Get().IsDynamicStringFragment()) {
char chars[ProfileBufferEntry::kNumChars];
e.Get().CopyCharsInto(chars);
for (char c : chars) {
if (i < kMaxFrameKeyLength) {
dynStrBuf[i] = c;
i++;
}
}
e.Next();
} else {
break;
}
}
dynStrBuf[kMaxFrameKeyLength - 1] = '\0';
bool hasDynamicString = (i != 0);
nsAutoCStringN<1024> frameLabel;
if (label[0] != '\0' && hasDynamicString) {
if (frameFlags & uint32_t(FrameFlags::STRING_TEMPLATE_METHOD)) {
frameLabel.AppendPrintf("%s.%s", label, dynStrBuf.get());
} else if (frameFlags &
uint32_t(FrameFlags::STRING_TEMPLATE_GETTER)) {
frameLabel.AppendPrintf("get %s.%s", label, dynStrBuf.get());
} else if (frameFlags &
uint32_t(FrameFlags::STRING_TEMPLATE_SETTER)) {
frameLabel.AppendPrintf("set %s.%s", label, dynStrBuf.get());
} else {
frameLabel.AppendPrintf("%s %s", label, dynStrBuf.get());
}
} else if (hasDynamicString) {
frameLabel.Append(dynStrBuf.get());
} else {
frameLabel.Append(label);
}
uint64_t innerWindowID = 0;
if (e.Has() && e.Get().IsInnerWindowID()) {
innerWindowID = uint64_t(e.Get().GetUint64());
e.Next();
}
uint32_t sourceId = 0;
if (e.Has() && e.Get().IsSourceId()) {
sourceId = uint64_t(e.Get().GetUint32());
e.Next();
}
Maybe<unsigned> line;
if (e.Has() && e.Get().IsLineNumber()) {
line = Some(unsigned(e.Get().GetInt()));
e.Next();
}
Maybe<unsigned> column;
if (e.Has() && e.Get().IsColumnNumber()) {
column = Some(unsigned(e.Get().GetInt()));
e.Next();
}
Maybe<JS::ProfilingCategoryPair> categoryPair;
if (e.Has() && e.Get().IsCategoryPair()) {
categoryPair =
Some(JS::ProfilingCategoryPair(uint32_t(e.Get().GetInt())));
e.Next();
}
maybeStack = uniqueStacks.AppendFrame(
stack,
UniqueStacks::FrameKey(std::move(frameLabel), relevantForJS,
isBaselineInterp, innerWindowID,
sourceId, line, column, categoryPair));
if (!maybeStack) {
writer.SetFailure("AppendFrame failure");
return;
}
stack = *maybeStack;
} else if (e.Get().IsJitReturnAddr()) {
numFrames++;
// A JIT frame may expand to multiple frames due to inlining.
void* pc = e.Get().GetPtr();
const Maybe<Vector<UniqueStacks::FrameKey>>& frameKeys =
uniqueStacks.LookupFramesForJITAddressFromBufferPos(
pc, entryPosition ? entryPosition : e.CurPos());
MOZ_RELEASE_ASSERT(
frameKeys,
"Attempting to stream samples for a buffer range "
"for which we don't have JITFrameInfo?");
for (const UniqueStacks::FrameKey& frameKey : *frameKeys) {
maybeStack = uniqueStacks.AppendFrame(stack, frameKey);
if (!maybeStack) {
writer.SetFailure("AppendFrame failure");
return;
}
stack = *maybeStack;
}
e.Next();
} else {
break;
}
}
// Even if this stack is considered empty, it contains the root frame,
// which needs to be in the JSON output because following "same samples"
// may refer to it when reusing this sample.mStack.
const Maybe<uint32_t> stackIndex =
uniqueStacks.GetOrAddStackIndex(stack);
if (!stackIndex) {
writer.SetFailure("Can't add unique string for stack");
return;
}
// And store that possibly-empty stack in case it's followed by "same
// sample" entries.
previousStack = *stackIndex;
previousStackState = (numFrames == 0)
? ThreadStreamingContext::eStackWasEmpty
: ThreadStreamingContext::eStackWasNotEmpty;
// Even if too old or empty, we did process a sample for this thread id.
processedThreadId = threadId;
// Discard samples that are too old.
if (time < aSinceTime) {
return;
}
if (numFrames == 0 && runningTimes.IsEmpty()) {
// It is possible to have empty stacks if native stackwalking is
// disabled. Skip samples with empty stacks, unless we have useful
// running times.
return;
}
WriteSample(writer, ProfileSample{*stackIndex, time,
unresponsiveDuration, runningTimes});
}; // End of `ReadStack(EntryGetter&)` lambda.
if (e.Has() && e.Get().IsTime()) {
double time = e.Get().GetDouble();
e.Next();
// Note: Even if this sample is too old (before aSinceTime), we still
// need to read it, so that its frames are in the tables, in case there
// is a same-sample following it that would be after aSinceTime, which
// would need these frames to be present.
ReadStack(e, time, 0, Nothing{}, RunningTimes{});
e.SetLocalProgress("Processed sample");
} else if (e.Has() && e.Get().IsTimeBeforeCompactStack()) {
double time = e.Get().GetDouble();
// Note: Even if this sample is too old (before aSinceTime), we still
// need to read it, so that its frames are in the tables, in case there
// is a same-sample following it that would be after aSinceTime, which
// would need these frames to be present.
RunningTimes runningTimes;
Maybe<double> unresponsiveDuration;
ProfileChunkedBuffer::BlockIterator it = e.Iterator();
for (;;) {
++it;
if (it.IsAtEnd()) {
break;
}
ProfileBufferEntryReader er = *it;
ProfileBufferEntry::Kind kind =
er.ReadObject<ProfileBufferEntry::Kind>();
// There may be running times before the CompactStack.
if (kind == ProfileBufferEntry::Kind::RunningTimes) {
er.ReadIntoObject(runningTimes);
continue;
}
// There may be an UnresponsiveDurationMs before the CompactStack.
if (kind == ProfileBufferEntry::Kind::UnresponsiveDurationMs) {
unresponsiveDuration = Some(er.ReadObject<double>());
continue;
}
if (kind == ProfileBufferEntry::Kind::CompactStack) {
ProfileChunkedBuffer tempBuffer(
ProfileChunkedBuffer::ThreadSafety::WithoutMutex,
WorkerChunkManager());
er.ReadIntoObject(tempBuffer);
tempBuffer.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(aReader,
"Local ProfileChunkedBuffer cannot be out-of-session");
// This is a compact stack, it should only contain one sample.
EntryGetter stackEntryGetter(*aReader, aFailureLatch);
ReadStack(stackEntryGetter, time,
it.CurrentBlockIndex().ConvertToProfileBufferIndex(),
unresponsiveDuration, runningTimes);
});
WorkerChunkManager().Reset(tempBuffer.GetAllChunks());
break;
}
if (kind == ProfileBufferEntry::Kind::Marker &&
aStreamingContextForMarkers) {
StreamMarkerAfterKind(er, *aStreamingContextForMarkers);
continue;
}
MOZ_ASSERT(kind >= ProfileBufferEntry::Kind::LEGACY_LIMIT,
"There should be no legacy entries between "
"TimeBeforeCompactStack and CompactStack");
er.SetRemainingBytes(0);
}
e.RestartAfter(it);
e.SetLocalProgress("Processed compact sample");
} else if (e.Has() && e.Get().IsTimeBeforeSameSample()) {
if (previousStackState == ThreadStreamingContext::eNoStackYet) {
// We don't have any full sample yet, we cannot duplicate a "previous"
// one. This should only happen at most once per thread, for the very
// first sample.
continue;
}
ProfileSample sample;
// Keep the same `mStack` as previously output.
// Note that it may be empty, this is checked below before writing it.
sample.mStack = previousStack;
sample.mTime = e.Get().GetDouble();
// Ignore samples that are too old.
if (sample.mTime < aSinceTime) {
e.Next();
continue;
}
sample.mResponsiveness = Nothing{};
sample.mRunningTimes.Clear();
ProfileChunkedBuffer::BlockIterator it = e.Iterator();
for (;;) {
++it;
if (it.IsAtEnd()) {
break;
}
ProfileBufferEntryReader er = *it;
ProfileBufferEntry::Kind kind =
er.ReadObject<ProfileBufferEntry::Kind>();
// There may be running times before the SameSample.
if (kind == ProfileBufferEntry::Kind::RunningTimes) {
er.ReadIntoObject(sample.mRunningTimes);
continue;
}
if (kind == ProfileBufferEntry::Kind::SameSample) {
if (previousStackState == ThreadStreamingContext::eStackWasEmpty &&
sample.mRunningTimes.IsEmpty()) {
// Skip samples with empty stacks, unless we have useful running
// times.
break;
}
WriteSample(writer, sample);
break;
}
if (kind == ProfileBufferEntry::Kind::Marker &&
aStreamingContextForMarkers) {
StreamMarkerAfterKind(er, *aStreamingContextForMarkers);
continue;
}
MOZ_ASSERT(kind >= ProfileBufferEntry::Kind::LEGACY_LIMIT,
"There should be no legacy entries between "
"TimeBeforeSameSample and SameSample");
er.SetRemainingBytes(0);
}
e.RestartAfter(it);
e.SetLocalProgress("Processed repeated sample");
} else {
ERROR_AND_CONTINUE("expected a Time entry");
}
}
return processedThreadId;
});
}
ProfilerThreadId ProfileBuffer::StreamSamplesToJSON(
SpliceableJSONWriter& aWriter, ProfilerThreadId aThreadId,
double aSinceTime, UniqueStacks& aUniqueStacks,
mozilla::ProgressLogger aProgressLogger) const {
ThreadStreamingContext::PreviousStackState previousStackState =
ThreadStreamingContext::eNoStackYet;
uint32_t stack = 0u;
#ifdef DEBUG
int processedCount = 0;
#endif // DEBUG
return DoStreamSamplesAndMarkersToJSON(
aWriter.SourceFailureLatch(),
[&](ProfilerThreadId aReadThreadId) {
Maybe<StreamingParametersForThread> streamingParameters;
#ifdef DEBUG
++processedCount;
MOZ_ASSERT(
aThreadId.IsSpecified() ||
(processedCount == 1 && aReadThreadId.IsSpecified()),
"Unspecified aThreadId should only be used with 1-sample buffer");
#endif // DEBUG
if (!aThreadId.IsSpecified() || aThreadId == aReadThreadId) {
streamingParameters.emplace(aWriter, aUniqueStacks,
previousStackState, stack);
}
return streamingParameters;
},
aSinceTime, /* aStreamingContextForMarkers */ nullptr,
std::move(aProgressLogger));
}
void ProfileBuffer::StreamSamplesAndMarkersToJSON(
ProcessStreamingContext& aProcessStreamingContext,
mozilla::ProgressLogger aProgressLogger) const {
auto getStreamingParamsCallback = [&](ProfilerThreadId aReadThreadId) {
Maybe<StreamingParametersForThread> streamingParameters;
ThreadStreamingContext* threadData =
aProcessStreamingContext.GetThreadStreamingContext(aReadThreadId);
if (threadData) {
streamingParameters.emplace(
threadData->mSamplesDataWriter, *threadData->mUniqueStacks,
threadData->mPreviousStackState, threadData->mPreviousStack,
threadData->mShapesDataWriter);
}
return streamingParameters;
};
#ifdef MOZ_EXECUTION_TRACING
MaybeStreamExecutionTraceToJSON(getStreamingParamsCallback,
aProcessStreamingContext.GetSinceTime());
#endif
(void)DoStreamSamplesAndMarkersToJSON(
aProcessStreamingContext.SourceFailureLatch(), getStreamingParamsCallback,
aProcessStreamingContext.GetSinceTime(), &aProcessStreamingContext,
std::move(aProgressLogger));
}
void ProfileBuffer::AddJITInfoForRange(
uint64_t aRangeStart, ProfilerThreadId aThreadId, JSContext* aContext,
JITFrameInfo& aJITFrameInfo, mozilla::ProgressLogger aProgressLogger,
const nsTHashMap<SourceId, IndexIntoSourceTable>* aSourceIdToIndexMap)
const {
// We can only process JitReturnAddr entries if we have a JSContext.
MOZ_RELEASE_ASSERT(aContext);
aRangeStart = std::max(aRangeStart, BufferRangeStart());
aJITFrameInfo.AddInfoForRange(
aRangeStart, BufferRangeEnd(), aContext,
[&](const std::function<void(void*)>& aJITAddressConsumer) {
// Find all JitReturnAddr entries in the given range for the given
// thread, and call aJITAddressConsumer with those addresses.
mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(aReader,
"ProfileChunkedBuffer cannot be out-of-session when "
"sampler is running");
EntryGetter e(*aReader, aJITFrameInfo.LocalFailureLatchSource(),
std::move(aProgressLogger), aRangeStart);
while (true) {
// Advance to the next ThreadId entry.
while (e.Has() && !e.Get().IsThreadId()) {
e.Next();
}
if (!e.Has()) {
break;
}
MOZ_ASSERT(e.Get().IsThreadId());
ProfilerThreadId threadId = e.Get().GetThreadId();
e.Next();
// Ignore samples that are for a different thread.
if (threadId != aThreadId) {
continue;
}
if (e.Has() && e.Get().IsTime()) {
// Legacy stack.
e.Next();
while (e.Has() && !e.Get().IsThreadId()) {
if (e.Get().IsJitReturnAddr()) {
aJITAddressConsumer(e.Get().GetPtr());
}
e.Next();
}
} else if (e.Has() && e.Get().IsTimeBeforeCompactStack()) {
// Compact stack.
ProfileChunkedBuffer::BlockIterator it = e.Iterator();
for (;;) {
++it;
if (it.IsAtEnd()) {
break;
}
ProfileBufferEntryReader er = *it;
ProfileBufferEntry::Kind kind =
er.ReadObject<ProfileBufferEntry::Kind>();
if (kind == ProfileBufferEntry::Kind::CompactStack) {
ProfileChunkedBuffer tempBuffer(
ProfileChunkedBuffer::ThreadSafety::WithoutMutex,
WorkerChunkManager());
er.ReadIntoObject(tempBuffer);
tempBuffer.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(
aReader,
"Local ProfileChunkedBuffer cannot be out-of-session");
EntryGetter stackEntryGetter(
*aReader, aJITFrameInfo.LocalFailureLatchSource());
while (stackEntryGetter.Has()) {
if (stackEntryGetter.Get().IsJitReturnAddr()) {
aJITAddressConsumer(stackEntryGetter.Get().GetPtr());
}
stackEntryGetter.Next();
}
});
WorkerChunkManager().Reset(tempBuffer.GetAllChunks());
break;
}
MOZ_ASSERT(kind >= ProfileBufferEntry::Kind::LEGACY_LIMIT,
"There should be no legacy entries between "
"TimeBeforeCompactStack and CompactStack");
er.SetRemainingBytes(0);
}
e.Next();
} else if (e.Has() && e.Get().IsTimeBeforeSameSample()) {
// Sample index, nothing to do.
} else {
ERROR_AND_CONTINUE("expected a Time entry");
}
}
});
},
aSourceIdToIndexMap);
}
void ProfileBuffer::StreamMarkersToJSON(
SpliceableJSONWriter& aWriter, ProfilerThreadId aThreadId,
const TimeStamp& aProcessStartTime, double aSinceTime,
UniqueStacks& aUniqueStacks,
mozilla::ProgressLogger aProgressLogger) const {
mEntries.ReadEach([&](ProfileBufferEntryReader& aER) {
auto type = static_cast<ProfileBufferEntry::Kind>(
aER.ReadObject<ProfileBufferEntry::KindUnderlyingType>());
MOZ_ASSERT(static_cast<ProfileBufferEntry::KindUnderlyingType>(type) <
static_cast<ProfileBufferEntry::KindUnderlyingType>(
ProfileBufferEntry::Kind::MODERN_LIMIT));
if (type == ProfileBufferEntry::Kind::Marker) {
mozilla::base_profiler_markers_detail::DeserializeAfterKindAndStream(
aER,
[&](const ProfilerThreadId& aMarkerThreadId) {
return (!aThreadId.IsSpecified() || aMarkerThreadId == aThreadId)
? &aWriter
: nullptr;
},
[&](ProfileChunkedBuffer& aChunkedBuffer) {
ProfilerBacktrace backtrace("", &aChunkedBuffer);
backtrace.StreamJSON(aWriter, aProcessStartTime, aUniqueStacks);
},
[&](mozilla::base_profiler_markers_detail::Streaming::DeserializerTag
aTag) {
size_t payloadSize = aER.RemainingBytes();
ProfileBufferEntryReader::DoubleSpanOfConstBytes spans =
aER.ReadSpans(payloadSize);
if (MOZ_LIKELY(spans.IsSingleSpan())) {
// Only a single span, we can just refer to it directly
// instead of copying it.
profiler::ffi::gecko_profiler_serialize_marker_for_tag(
aTag, spans.mFirstOrOnly.Elements(), payloadSize, &aWriter);
} else {
// Two spans, we need to concatenate them by copying.
uint8_t* payloadBuffer = new uint8_t[payloadSize];
spans.CopyBytesTo(payloadBuffer);
profiler::ffi::gecko_profiler_serialize_marker_for_tag(
aTag, payloadBuffer, payloadSize, &aWriter);
delete[] payloadBuffer;
}
});
} else {
// The entry was not a marker, we need to skip to the end.
aER.SetRemainingBytes(0);
}
});
}
void ProfileBuffer::StreamProfilerOverheadToJSON(
SpliceableJSONWriter& aWriter, const TimeStamp& aProcessStartTime,
double aSinceTime, mozilla::ProgressLogger aProgressLogger) const {
const char* recordOverheads = getenv("MOZ_PROFILER_RECORD_OVERHEADS");
if (!recordOverheads || recordOverheads[0] == '\0') {
// Overheads were not recorded, return early.
return;
}
mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(aReader,
"ProfileChunkedBuffer cannot be out-of-session when sampler is "
"running");
EntryGetter e(*aReader, aWriter.SourceFailureLatch(),
std::move(aProgressLogger));
enum Schema : uint32_t {
TIME = 0,
LOCKING = 1,
MARKER_CLEANING = 2,
COUNTERS = 3,
THREADS = 4
};
aWriter.StartObjectProperty("profilerOverhead");
aWriter.StartObjectProperty("samples");
// Stream all sampling overhead data. We skip other entries, because we
// process them in StreamSamplesToJSON()/etc.
{
JSONSchemaWriter schema(aWriter);
schema.WriteField("time");
schema.WriteField("locking");
schema.WriteField("expiredMarkerCleaning");
schema.WriteField("counters");
schema.WriteField("threads");
}
aWriter.StartArrayProperty("data");
double firstTime = 0.0;
double lastTime = 0.0;
ProfilerStats intervals, overheads, lockings, cleanings, counters, threads;
while (e.Has()) {
// valid sequence: ProfilerOverheadTime, ProfilerOverheadDuration * 4
if (e.Get().IsProfilerOverheadTime()) {
double time = e.Get().GetDouble();
if (time >= aSinceTime) {
e.Next();
if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) {
ERROR_AND_CONTINUE(
"expected a ProfilerOverheadDuration entry after "
"ProfilerOverheadTime");
}
double locking = e.Get().GetDouble();
e.Next();
if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) {
ERROR_AND_CONTINUE(
"expected a ProfilerOverheadDuration entry after "
"ProfilerOverheadTime,ProfilerOverheadDuration");
}
double cleaning = e.Get().GetDouble();
e.Next();
if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) {
ERROR_AND_CONTINUE(
"expected a ProfilerOverheadDuration entry after "
"ProfilerOverheadTime,ProfilerOverheadDuration*2");
}
double counter = e.Get().GetDouble();
e.Next();
if (!e.Has() || !e.Get().IsProfilerOverheadDuration()) {
ERROR_AND_CONTINUE(
"expected a ProfilerOverheadDuration entry after "
"ProfilerOverheadTime,ProfilerOverheadDuration*3");
}
double thread = e.Get().GetDouble();
if (firstTime == 0.0) {
firstTime = time;
} else {
// Note that we'll have 1 fewer interval than other numbers (because
// we need both ends of an interval to know its duration). The final
// difference should be insignificant over the expected many
// thousands of iterations.
intervals.Count(time - lastTime);
}
lastTime = time;
overheads.Count(locking + cleaning + counter + thread);
lockings.Count(locking);
cleanings.Count(cleaning);
counters.Count(counter);
threads.Count(thread);
AutoArraySchemaWriter writer(aWriter);
writer.TimeMsElement(TIME, time);
writer.DoubleElement(LOCKING, locking);
writer.DoubleElement(MARKER_CLEANING, cleaning);
writer.DoubleElement(COUNTERS, counter);
writer.DoubleElement(THREADS, thread);
}
}
e.Next();
}
aWriter.EndArray(); // data
aWriter.EndObject(); // samples
// Only output statistics if there is at least one full interval (and
// therefore at least two samplings.)
if (intervals.n > 0) {
aWriter.StartObjectProperty("statistics");
aWriter.DoubleProperty("profiledDuration", lastTime - firstTime);
aWriter.IntProperty("samplingCount", overheads.n);
aWriter.DoubleProperty("overheadDurations", overheads.sum);
aWriter.DoubleProperty("overheadPercentage",
overheads.sum / (lastTime - firstTime));
#define PROFILER_STATS(name, var) \
aWriter.DoubleProperty("mean" name, (var).sum / (var).n); \
aWriter.DoubleProperty("min" name, (var).min); \
aWriter.DoubleProperty("max" name, (var).max);
PROFILER_STATS("Interval", intervals);
PROFILER_STATS("Overhead", overheads);
PROFILER_STATS("Lockings", lockings);
PROFILER_STATS("Cleaning", cleanings);
PROFILER_STATS("Counter", counters);
PROFILER_STATS("Thread", threads);
#undef PROFILER_STATS
aWriter.EndObject(); // statistics
}
aWriter.EndObject(); // profilerOverhead
});
}
struct CounterSample {
double mTime;
uint64_t mNumber;
int64_t mCount;
};
using CounterSamples = Vector<CounterSample>;
static LazyLogModule sFuzzyfoxLog("Fuzzyfox");
// HashMap lookup, if not found, a default value is inserted.
// Returns reference to (existing or new) value inside the HashMap.
template <typename HashM, typename Key>
static auto& LookupOrAdd(HashM& aMap, Key&& aKey) {
auto addPtr = aMap.lookupForAdd(aKey);
if (!addPtr) {
MOZ_RELEASE_ASSERT(aMap.add(addPtr, std::forward<Key>(aKey),
typename HashM::Entry::ValueType{}));
MOZ_ASSERT(!!addPtr);
}
return addPtr->value();
}
void ProfileBuffer::StreamCountersToJSON(
SpliceableJSONWriter& aWriter, const TimeStamp& aProcessStartTime,
double aSinceTime, mozilla::ProgressLogger aProgressLogger) const {
// Because this is a format entirely internal to the Profiler, any parsing
// error indicates a bug in the ProfileBuffer writing or the parser itself,
// or possibly flaky hardware.
mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(aReader,
"ProfileChunkedBuffer cannot be out-of-session when sampler is "
"running");
EntryGetter e(*aReader, aWriter.SourceFailureLatch(),
std::move(aProgressLogger));
enum Schema : uint32_t { TIME = 0, COUNT = 1, NUMBER = 2 };
// Stream all counters. We skip other entries, because we process them in
// StreamSamplesToJSON()/etc.
//
// Valid sequence in the buffer:
// CounterID
// Time
// ( Count Number? )*
//
// And the JSON (example):
// "counters": {
// "name": "malloc",
// "category": "Memory",
// "description": "Amount of allocated memory",
// "samples": {
// "schema": {"time": 0, "count": 1, "number": 2},
// "data": [
// [
// 16117.033968000002,
// 2446216,
// 6801320
// ],
// [
// 16118.037638,
// 2446216,
// 6801320
// ],
// ],
// },
// }
// Build the map of counters and populate it
HashMap<void*, CounterSamples> counters;
while (e.Has()) {
// skip all non-Counters, including if we start in the middle of a counter
if (e.Get().IsCounterId()) {
void* id = e.Get().GetPtr();
CounterSamples& data = LookupOrAdd(counters, id);
e.Next();
if (!e.Has() || !e.Get().IsTime()) {
ERROR_AND_CONTINUE("expected a Time entry");
}
double time = e.Get().GetDouble();
e.Next();
if (time >= aSinceTime) {
if (!e.Has() || !e.Get().IsCount()) {
ERROR_AND_CONTINUE("expected a Count entry");
}
int64_t count = e.Get().GetUint64();
e.Next();
uint64_t number;
if (!e.Has() || !e.Get().IsNumber()) {
number = 0;
} else {
number = e.Get().GetInt64();
e.Next();
}
CounterSample sample = {time, number, count};
MOZ_RELEASE_ASSERT(data.append(sample));
} else {
// skip counter sample - only need to skip the initial counter
// id, then let the loop at the top skip the rest
}
} else {
e.Next();
}
}
// we have a map of counter entries; dump them to JSON
if (counters.count() == 0) {
return;
}
aWriter.StartArrayProperty("counters");
for (auto iter = counters.iter(); !iter.done(); iter.next()) {
CounterSamples& samples = iter.get().value();
size_t size = samples.length();
if (size == 0) {
continue;
}
const BaseProfilerCount* base_counter =
static_cast<const BaseProfilerCount*>(iter.get().key());
aWriter.Start();
aWriter.StringProperty("name", MakeStringSpan(base_counter->mLabel));
aWriter.StringProperty("category",
MakeStringSpan(base_counter->mCategory));
aWriter.StringProperty("description",
MakeStringSpan(base_counter->mDescription));
bool hasNumber = false;
for (size_t i = 0; i < size; i++) {
if (samples[i].mNumber != 0) {
hasNumber = true;
break;
}
}
aWriter.StartObjectProperty("samples");
{
JSONSchemaWriter schema(aWriter);
schema.WriteField("time");
schema.WriteField("count");
if (hasNumber) {
schema.WriteField("number");
}
}
aWriter.StartArrayProperty("data");
double previousSkippedTime = 0.0;
uint64_t previousNumber = 0;
int64_t previousCount = 0;
for (size_t i = 0; i < size; i++) {
// Encode as deltas, and only encode if different than the previous
// or next sample; Always write the first and last samples.
if (i == 0 || i == size - 1 || samples[i].mNumber != previousNumber ||
samples[i].mCount != previousCount ||
// Ensure we ouput the first 0 before skipping samples.
(i >= 2 && (samples[i - 2].mNumber != previousNumber ||
samples[i - 2].mCount != previousCount))) {
if (i != 0 && samples[i].mTime >= samples[i - 1].mTime) {
MOZ_LOG(sFuzzyfoxLog, mozilla::LogLevel::Error,
("Fuzzyfox Profiler Assertion: %f >= %f", samples[i].mTime,
samples[i - 1].mTime));
}
MOZ_ASSERT(i == 0 || samples[i].mTime >= samples[i - 1].mTime);
MOZ_ASSERT(samples[i].mNumber >= previousNumber);
MOZ_ASSERT(samples[i].mNumber - previousNumber <=
uint64_t(std::numeric_limits<int64_t>::max()));
int64_t numberDelta =
static_cast<int64_t>(samples[i].mNumber - previousNumber);
int64_t countDelta = samples[i].mCount - previousCount;
if (previousSkippedTime != 0.0 &&
(numberDelta != 0 || countDelta != 0)) {
// Write the last skipped sample, unless the new one is all
// zeroes (that'd be redundant) This is useful to know when a
// certain value was last sampled, so that the front-end graph
// will be more correct.
AutoArraySchemaWriter writer(aWriter);
writer.TimeMsElement(TIME, previousSkippedTime);
// The deltas are effectively zeroes, since no change happened
// between the last actually-written sample and the last skipped
// one.
writer.IntElement(COUNT, 0);
if (hasNumber) {
writer.IntElement(NUMBER, 0);
}
}
AutoArraySchemaWriter writer(aWriter);
writer.TimeMsElement(TIME, samples[i].mTime);
writer.IntElement(COUNT, countDelta);
if (hasNumber) {
writer.IntElement(NUMBER, numberDelta);
}
previousSkippedTime = 0.0;
previousNumber = samples[i].mNumber;
previousCount = samples[i].mCount;
} else {
previousSkippedTime = samples[i].mTime;
}
}
aWriter.EndArray(); // data
aWriter.EndObject(); // samples
aWriter.End(); // for each counter
}
aWriter.EndArray(); // counters
});
}
#undef ERROR_AND_CONTINUE
static void AddPausedRange(SpliceableJSONWriter& aWriter, const char* aReason,
const Maybe<double>& aStartTime,
const Maybe<double>& aEndTime) {
aWriter.Start();
if (aStartTime) {
aWriter.TimeDoubleMsProperty("startTime", *aStartTime);
} else {
aWriter.NullProperty("startTime");
}
if (aEndTime) {
aWriter.TimeDoubleMsProperty("endTime", *aEndTime);
} else {
aWriter.NullProperty("endTime");
}
aWriter.StringProperty("reason", MakeStringSpan(aReason));
aWriter.End();
}
void ProfileBuffer::StreamPausedRangesToJSON(
SpliceableJSONWriter& aWriter, double aSinceTime,
mozilla::ProgressLogger aProgressLogger) const {
mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(aReader,
"ProfileChunkedBuffer cannot be out-of-session when sampler is "
"running");
EntryGetter e(*aReader, aWriter.SourceFailureLatch(),
aProgressLogger.CreateSubLoggerFromTo(
1_pc, "Streaming pauses...", 99_pc, "Streamed pauses"));
Maybe<double> currentPauseStartTime;
Maybe<double> currentCollectionStartTime;
while (e.Has()) {
if (e.Get().IsPause()) {
currentPauseStartTime = Some(e.Get().GetDouble());
} else if (e.Get().IsResume()) {
AddPausedRange(aWriter, "profiler-paused", currentPauseStartTime,
Some(e.Get().GetDouble()));
currentPauseStartTime = Nothing();
} else if (e.Get().IsCollectionStart()) {
currentCollectionStartTime = Some(e.Get().GetDouble());
} else if (e.Get().IsCollectionEnd()) {
AddPausedRange(aWriter, "collecting", currentCollectionStartTime,
Some(e.Get().GetDouble()));
currentCollectionStartTime = Nothing();
}
e.Next();
}
if (currentPauseStartTime) {
AddPausedRange(aWriter, "profiler-paused", currentPauseStartTime,
Nothing());
}
if (currentCollectionStartTime) {
AddPausedRange(aWriter, "collecting", currentCollectionStartTime,
Nothing());
}
});
}
bool ProfileBuffer::DuplicateLastSample(ProfilerThreadId aThreadId,
double aSampleTimeMs,
Maybe<uint64_t>& aLastSample,
const RunningTimes& aRunningTimes) {
if (!aLastSample) {
return false;
}
if (mEntries.IsIndexInCurrentChunk(ProfileBufferIndex{*aLastSample})) {
// The last (fully-written) sample is in this chunk, we can refer to it.
// Note that between now and when we write the SameSample below, another
// chunk could have been started, so the SameSample will in fact refer to a
// block in a previous chunk. This is okay, because:
// - When serializing to JSON, if that chunk is still there, we'll still be
// able to find that old stack, so nothing will be lost.
// - If unfortunately that chunk has been destroyed, we will lose this
// sample. But this will only happen to the first sample (per thread) in
// in the whole JSON output, because the next time we're here to duplicate
// the same sample again, IsIndexInCurrentChunk will say `false` and we
// will fall back to the normal copy or even re-sample. Losing the first
// sample out of many in a whole recording is acceptable.
//
// |---| = chunk, S = Sample, D = Duplicate, s = same sample
// |---S-s-s--| |s-D--s--s-| |s-D--s---s|
// Later, the first chunk is destroyed/recycled:
// |s-D--s--s-| |s-D--s---s| |-...
// Output: ^ ^ ^ ^
// `-|--|-------|--- Same but no previous -> lost.
// `--|-------|--- Full duplicate sample.
// `-------|--- Same with previous -> okay.
// `--- Same but now we have a previous -> okay!
AUTO_PROFILER_STATS(DuplicateLastSample_SameSample);
// Add the thread id first. We don't update `aLastSample` because we are not
// writing a full sample.
(void)AddThreadIdEntry(aThreadId);
// Copy the new time, to be followed by a SameSample.
AddEntry(ProfileBufferEntry::TimeBeforeSameSample(aSampleTimeMs));
// Add running times if they have data.
if (!aRunningTimes.IsEmpty()) {
mEntries.PutObjects(ProfileBufferEntry::Kind::RunningTimes,
aRunningTimes);
}
// Finish with a SameSample entry.
mEntries.PutObjects(ProfileBufferEntry::Kind::SameSample);
return true;
}
AUTO_PROFILER_STATS(DuplicateLastSample_copy);
ProfileChunkedBuffer tempBuffer(
ProfileChunkedBuffer::ThreadSafety::WithoutMutex, WorkerChunkManager());
auto retrieveWorkerChunk = MakeScopeExit(
[&]() { WorkerChunkManager().Reset(tempBuffer.GetAllChunks()); });
const bool ok = mEntries.Read([&](ProfileChunkedBuffer::Reader* aReader) {
MOZ_ASSERT(aReader,
"ProfileChunkedBuffer cannot be out-of-session when sampler is "
"running");
// DuplicateLastSample is only called during profiling, so we don't need a
// progress logger (only useful when capturing the final profile).
EntryGetter e(*aReader, mozilla::FailureLatchInfallibleSource::Singleton(),
ProgressLogger{}, *aLastSample);
if (e.CurPos() != *aLastSample) {
// The last sample is no longer within the buffer range, so we cannot
// use it. Reset the stored buffer position to Nothing().
aLastSample.reset();
return false;
}
MOZ_RELEASE_ASSERT(e.Has() && e.Get().IsThreadId() &&
e.Get().GetThreadId() == aThreadId);
e.Next();
// Go through the whole entry and duplicate it, until we find the next
// one.
while (e.Has()) {
switch (e.Get().GetKind()) {
case ProfileBufferEntry::Kind::Pause:
case ProfileBufferEntry::Kind::Resume:
case ProfileBufferEntry::Kind::PauseSampling:
case ProfileBufferEntry::Kind::ResumeSampling:
case ProfileBufferEntry::Kind::CollectionStart:
case ProfileBufferEntry::Kind::CollectionEnd:
case ProfileBufferEntry::Kind::ThreadId:
case ProfileBufferEntry::Kind::TimeBeforeSameSample:
// We're done.
return true;
case ProfileBufferEntry::Kind::Time:
// Copy with new time
AddEntry(tempBuffer, ProfileBufferEntry::Time(aSampleTimeMs));
break;
case ProfileBufferEntry::Kind::TimeBeforeCompactStack: {
// Copy with new time, followed by a compact stack.
AddEntry(tempBuffer,
ProfileBufferEntry::TimeBeforeCompactStack(aSampleTimeMs));
// Add running times if they have data.
if (!aRunningTimes.IsEmpty()) {
tempBuffer.PutObjects(ProfileBufferEntry::Kind::RunningTimes,
aRunningTimes);
}
// The `CompactStack` *must* be present afterwards, but may not
// immediately follow `TimeBeforeCompactStack` (e.g., some markers
// could be written in-between), so we need to look for it in the
// following entries.
ProfileChunkedBuffer::BlockIterator it = e.Iterator();
for (;;) {
++it;
if (it.IsAtEnd()) {
break;
}
ProfileBufferEntryReader er = *it;
auto kind = static_cast<ProfileBufferEntry::Kind>(
er.ReadObject<ProfileBufferEntry::KindUnderlyingType>());
MOZ_ASSERT(
static_cast<ProfileBufferEntry::KindUnderlyingType>(kind) <
static_cast<ProfileBufferEntry::KindUnderlyingType>(
ProfileBufferEntry::Kind::MODERN_LIMIT));
if (kind == ProfileBufferEntry::Kind::CompactStack) {
// Found our CompactStack, just make a copy of the whole entry.
er = *it;
auto bytes = er.RemainingBytes();
MOZ_ASSERT(bytes <
ProfileBufferChunkManager::scExpectedMaximumStackSize);
tempBuffer.Put(bytes, [&](Maybe<ProfileBufferEntryWriter>& aEW) {
MOZ_ASSERT(aEW.isSome(), "tempBuffer cannot be out-of-session");
aEW->WriteFromReader(er, bytes);
});
// CompactStack marks the end, we're done.
break;
}
MOZ_ASSERT(kind >= ProfileBufferEntry::Kind::LEGACY_LIMIT,
"There should be no legacy entries between "
"TimeBeforeCompactStack and CompactStack");
er.SetRemainingBytes(0);
// Here, we have encountered a non-legacy entry that was not the
// CompactStack we're looking for; just continue the search...
}
// We're done.
return true;
}
case ProfileBufferEntry::Kind::Number:
case ProfileBufferEntry::Kind::Count:
// Don't copy anything not part of a thread's stack sample
break;
case ProfileBufferEntry::Kind::CounterId:
// CounterId is normally followed by Time - if so, we'd like
// to skip it. If we duplicate Time, it won't hurt anything, just
// waste buffer space (and this can happen if the CounterId has
// fallen off the end of the buffer, but Time (and Number/Count)
// are still in the buffer).
e.Next();
if (e.Has() && e.Get().GetKind() != ProfileBufferEntry::Kind::Time) {
// this would only happen if there was an invalid sequence
// in the buffer. Don't skip it.
continue;
}
// we've skipped Time
break;
case ProfileBufferEntry::Kind::ProfilerOverheadTime:
// ProfilerOverheadTime is normally followed by
// ProfilerOverheadDuration*4 - if so, we'd like to skip it. Don't
// duplicate, as we are in the middle of a sampling and will soon
// capture its own overhead.
e.Next();
// A missing Time would only happen if there was an invalid
// sequence in the buffer. Don't skip unexpected entry.
if (e.Has() &&
e.Get().GetKind() !=
ProfileBufferEntry::Kind::ProfilerOverheadDuration) {
continue;
}
e.Next();
if (e.Has() &&
e.Get().GetKind() !=
ProfileBufferEntry::Kind::ProfilerOverheadDuration) {
continue;
}
e.Next();
if (e.Has() &&
e.Get().GetKind() !=
ProfileBufferEntry::Kind::ProfilerOverheadDuration) {
continue;
}
e.Next();
if (e.Has() &&
e.Get().GetKind() !=
ProfileBufferEntry::Kind::ProfilerOverheadDuration) {
continue;
}
// we've skipped ProfilerOverheadTime and
// ProfilerOverheadDuration*4.
break;
default: {
// Copy anything else we don't know about.
AddEntry(tempBuffer, e.Get());
break;
}
}
e.Next();
}
return true;
});
if (!ok) {
return false;
}
// If the buffer was big enough, there won't be any cleared blocks.
if (tempBuffer.GetState().mClearedBlockCount != 0) {
// No need to try to read stack again as it won't fit. Reset the stored
// buffer position to Nothing().
aLastSample.reset();
return false;
}
aLastSample = Some(AddThreadIdEntry(aThreadId));
mEntries.AppendContents(tempBuffer);
return true;
}
void ProfileBuffer::DiscardSamplesBeforeTime(double aTime) {
// This function does nothing!
// The duration limit will be removed from Firefox, see bug 1632365.
(void)aTime;
}
nsTHashMap<SourceId, IndexIntoSourceTable>
ProfileBuffer::StreamSourceTableToJSON(
SpliceableJSONWriter& aWriter,
const nsTArray<mozilla::JSSourceEntry>& aJSSourceEntries) const {
enum Schema : uint32_t { UUID = 0, FILENAME = 1 };
nsTHashMap<SourceId, IndexIntoSourceTable> sourceIdToIndexMap;
aWriter.StartObjectProperty("sources");
{
// Write the schema
{
JSONSchemaWriter schema(aWriter);
schema.WriteField("uuid");
schema.WriteField("filename");
}
// Write data array and build sourceId-to-index mapping
aWriter.StartArrayProperty("data");
uint32_t index = 0;
for (const auto& entry : aJSSourceEntries) {
// Build sourceId-to-index mapping
if (entry.sourceData.sourceId() != 0) {
MOZ_ASSERT(!sourceIdToIndexMap.Contains(entry.sourceData.sourceId()),
"Duplicate sourceId detected! This indicates sourceId "
"collision between different sources.");
sourceIdToIndexMap.InsertOrUpdate(entry.sourceData.sourceId(), index);
}
// Write [uuid, filename] entry
aWriter.StartArrayElement();
{
// TODO: Use AutoArraySchemaWithStringsWriter to write string indexes
// into string table once we have "process global" string table.
// Currently string tables are per-thread.
aWriter.StringElement(MakeStringSpan(entry.uuid.get()));
aWriter.StringElement(MakeStringSpan(entry.sourceData.filePath()));
}
aWriter.EndArray();
index++;
}
aWriter.EndArray();
}
aWriter.EndObject();
return sourceIdToIndexMap;
}
// END ProfileBuffer
////////////////////////////////////////////////////////////////////////
|