1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1999-2016. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
/*
* Support functions for tracing.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_process.h"
#include "big.h"
#include "bif.h"
#include "dist.h"
#include "beam_bp.h"
#include "error.h"
#include "erl_binary.h"
#include "erl_bits.h"
#include "erl_thr_progress.h"
#include "erl_bif_unique.h"
#include "erl_map.h"
#if 0
#define DEBUG_PRINTOUTS
#else
#undef DEBUG_PRINTOUTS
#endif
/* Pseudo export entries. Never filled in with data, only used to
yield unique pointers of the correct type. */
Export exp_send, exp_receive, exp_timeout;
static ErtsTracer system_seq_tracer;
static Uint default_proc_trace_flags;
static ErtsTracer default_proc_tracer;
static Uint default_port_trace_flags;
static ErtsTracer default_port_tracer;
static Eterm system_monitor;
static Eterm system_profile;
#ifdef HAVE_ERTS_NOW_CPU
int erts_cpu_timestamp;
#endif
static erts_smp_mtx_t smq_mtx;
static erts_smp_rwmtx_t sys_trace_rwmtx;
enum ErtsSysMsgType {
SYS_MSG_TYPE_UNDEFINED,
SYS_MSG_TYPE_SYSMON,
SYS_MSG_TYPE_ERRLGR,
SYS_MSG_TYPE_SYSPROF
};
#define ERTS_TRACE_TS_NOW_MAX_SIZE \
4
#define ERTS_TRACE_TS_MONOTONIC_MAX_SIZE \
ERTS_MAX_SINT64_HEAP_SIZE
#define ERTS_TRACE_TS_STRICT_MONOTONIC_MAX_SIZE \
(3 + ERTS_MAX_SINT64_HEAP_SIZE \
+ ERTS_MAX_UINT64_HEAP_SIZE)
#define ERTS_TRACE_PATCH_TS_MAX_SIZE \
(1 + ((ERTS_TRACE_TS_NOW_MAX_SIZE \
> ERTS_TRACE_TS_MONOTONIC_MAX_SIZE) \
? ((ERTS_TRACE_TS_NOW_MAX_SIZE \
> ERTS_TRACE_TS_STRICT_MONOTONIC_MAX_SIZE) \
? ERTS_TRACE_TS_NOW_MAX_SIZE \
: ERTS_TRACE_TS_STRICT_MONOTONIC_MAX_SIZE) \
: ((ERTS_TRACE_TS_MONOTONIC_MAX_SIZE \
> ERTS_TRACE_TS_STRICT_MONOTONIC_MAX_SIZE) \
? ERTS_TRACE_TS_MONOTONIC_MAX_SIZE \
: ERTS_TRACE_TS_STRICT_MONOTONIC_MAX_SIZE)))
#define TFLGS_TS_TYPE(p) ERTS_TFLGS2TSTYPE(ERTS_TRACE_FLAGS((p)))
/*
* FUTURE CHANGES:
*
* The timestamp functionality has intentionally been
* split in two parts for future use even though it
* is not used like this today. take_timestamp() takes
* the timestamp and calculate heap need for it (which
* is not constant). write_timestamp() writes the
* timestamp to the allocated heap. That is, one typically
* want to take the timestamp before allocating the heap
* and then write it to the heap.
*
* The trace output functionality now use patch_ts_size(),
* write_ts(), and patch_ts(). write_ts() both takes the
* timestamp and writes it. Since we don't know the
* heap need when allocating the heap area we need to
* over allocate (maximum size from patch_ts_size()) and
* then potentially (often) shrink the heap area after the
* timestamp has been written. The only reason it is
* currently done this way is because we do not want to
* make major changes of the trace behavior in a patch.
* This is planned to be changed in next major release.
*/
typedef struct {
int ts_type_flag;
union {
struct {
Uint ms;
Uint s;
Uint us;
} now;
struct {
ErtsMonotonicTime time;
Sint64 raw_unique;
} monotonic;
} u;
} ErtsTraceTimeStamp;
static ERTS_INLINE Uint
take_timestamp(ErtsTraceTimeStamp *tsp, int ts_type)
{
int ts_type_flag = ts_type & -ts_type; /* least significant flag */
ASSERT(ts_type_flag == ERTS_TRACE_FLG_NOW_TIMESTAMP
|| ts_type_flag == ERTS_TRACE_FLG_MONOTONIC_TIMESTAMP
|| ts_type_flag == ERTS_TRACE_FLG_STRICT_MONOTONIC_TIMESTAMP
|| ts_type_flag == 0);
tsp->ts_type_flag = ts_type_flag;
switch (ts_type_flag) {
case 0:
return (Uint) 0;
case ERTS_TRACE_FLG_NOW_TIMESTAMP:
#ifdef HAVE_ERTS_NOW_CPU
if (erts_cpu_timestamp)
erts_get_now_cpu(&tsp->u.now.ms, &tsp->u.now.s, &tsp->u.now.us);
else
#endif
get_now(&tsp->u.now.ms, &tsp->u.now.s, &tsp->u.now.us);
return (Uint) 4;
case ERTS_TRACE_FLG_MONOTONIC_TIMESTAMP:
case ERTS_TRACE_FLG_STRICT_MONOTONIC_TIMESTAMP: {
Uint hsz = 0;
ErtsMonotonicTime mtime = erts_get_monotonic_time(NULL);
mtime = ERTS_MONOTONIC_TO_NSEC(mtime);
mtime += ERTS_MONOTONIC_OFFSET_NSEC;
hsz = (IS_SSMALL(mtime) ?
(Uint) 0
: ERTS_SINT64_HEAP_SIZE((Sint64) mtime));
tsp->u.monotonic.time = mtime;
if (ts_type_flag == ERTS_TRACE_FLG_STRICT_MONOTONIC_TIMESTAMP) {
Sint64 raw_unique;
hsz += 3; /* 2-tuple */
raw_unique = erts_raw_get_unique_monotonic_integer();
tsp->u.monotonic.raw_unique = raw_unique;
hsz += erts_raw_unique_monotonic_integer_heap_size(raw_unique, 0);
}
return hsz;
}
default:
ERTS_INTERNAL_ERROR("invalid timestamp type");
return 0;
}
}
static ERTS_INLINE Eterm
write_timestamp(ErtsTraceTimeStamp *tsp, Eterm **hpp)
{
int ts_type_flag = tsp->ts_type_flag;
Eterm res;
switch (ts_type_flag) {
case 0:
return NIL;
case ERTS_TRACE_FLG_NOW_TIMESTAMP:
res = TUPLE3(*hpp,
make_small(tsp->u.now.ms),
make_small(tsp->u.now.s),
make_small(tsp->u.now.us));
*hpp += 4;
return res;
case ERTS_TRACE_FLG_MONOTONIC_TIMESTAMP:
case ERTS_TRACE_FLG_STRICT_MONOTONIC_TIMESTAMP: {
Sint64 mtime, raw;
Eterm unique, emtime;
mtime = (Sint64) tsp->u.monotonic.time;
emtime = (IS_SSMALL(mtime)
? make_small((Sint64) mtime)
: erts_sint64_to_big((Sint64) mtime, hpp));
if (ts_type_flag == ERTS_TRACE_FLG_MONOTONIC_TIMESTAMP)
return emtime;
raw = tsp->u.monotonic.raw_unique;
unique = erts_raw_make_unique_monotonic_integer_value(hpp, raw, 0);
res = TUPLE2(*hpp, emtime, unique);
*hpp += 3;
return res;
}
default:
ERTS_INTERNAL_ERROR("invalid timestamp type");
return THE_NON_VALUE;
}
}
#define PATCH_TS_SIZE(p) patch_ts_size(TFLGS_TS_TYPE(p))
static ERTS_INLINE Uint
patch_ts_size(int ts_type)
{
int ts_type_flag = ts_type & -ts_type; /* least significant flag */
switch (ts_type_flag) {
case 0:
return 0;
case ERTS_TRACE_FLG_NOW_TIMESTAMP:
return 1 + ERTS_TRACE_TS_NOW_MAX_SIZE;
case ERTS_TRACE_FLG_MONOTONIC_TIMESTAMP:
return 1 + ERTS_TRACE_TS_MONOTONIC_MAX_SIZE;
case ERTS_TRACE_FLG_STRICT_MONOTONIC_TIMESTAMP:
return 1 + ERTS_TRACE_TS_STRICT_MONOTONIC_MAX_SIZE;
default:
ERTS_INTERNAL_ERROR("invalid timestamp type");
return 0;
}
}
/*
* Write a timestamp. The timestamp MUST be the last
* thing built on the heap. This since write_ts() might
* adjust the size of the used area.
*/
static Eterm
write_ts(int ts_type, Eterm *hp, ErlHeapFragment *bp, Process *tracer)
{
ErtsTraceTimeStamp ts;
Sint shrink;
Eterm res, *ts_hp = hp;
Uint hsz;
ASSERT(ts_type);
hsz = take_timestamp(&ts, ts_type);
res = write_timestamp(&ts, &ts_hp);
ASSERT(ts_hp == hp + hsz);
switch (ts.ts_type_flag) {
case ERTS_TRACE_FLG_MONOTONIC_TIMESTAMP:
shrink = ERTS_TRACE_TS_MONOTONIC_MAX_SIZE;
break;
case ERTS_TRACE_FLG_STRICT_MONOTONIC_TIMESTAMP:
shrink = ERTS_TRACE_TS_STRICT_MONOTONIC_MAX_SIZE;
break;
default:
return res;
}
shrink -= hsz;
ASSERT(shrink >= 0);
if (shrink) {
if (bp)
bp->used_size -= shrink;
#ifndef ERTS_SMP
else if (tracer) {
Eterm *endp = ts_hp + shrink;
HRelease(tracer, endp, ts_hp);
}
#endif
}
return res;
}
#ifdef ERTS_SMP
static void enqueue_sys_msg_unlocked(enum ErtsSysMsgType type,
Eterm from,
Eterm to,
Eterm msg,
ErlHeapFragment *bp);
static void enqueue_sys_msg(enum ErtsSysMsgType type,
Eterm from,
Eterm to,
Eterm msg,
ErlHeapFragment *bp);
static void init_sys_msg_dispatcher(void);
#endif
static void init_tracer_nif(void);
static int tracer_cmp_fun(void*, void*);
static HashValue tracer_hash_fun(void*);
static void *tracer_alloc_fun(void*);
static void tracer_free_fun(void*);
typedef struct ErtsTracerNif_ ErtsTracerNif;
void erts_init_trace(void) {
erts_smp_rwmtx_opt_t rwmtx_opts = ERTS_SMP_RWMTX_OPT_DEFAULT_INITER;
rwmtx_opts.type = ERTS_SMP_RWMTX_TYPE_EXTREMELY_FREQUENT_READ;
rwmtx_opts.lived = ERTS_SMP_RWMTX_LONG_LIVED;
erts_smp_rwmtx_init_opt(&sys_trace_rwmtx, &rwmtx_opts, "sys_tracers");
#ifdef HAVE_ERTS_NOW_CPU
erts_cpu_timestamp = 0;
#endif
erts_bif_trace_init();
erts_system_monitor_clear(NULL);
erts_system_profile_clear(NULL);
default_proc_trace_flags = F_INITIAL_TRACE_FLAGS;
default_proc_tracer = erts_tracer_nil;
default_port_trace_flags = F_INITIAL_TRACE_FLAGS;
default_port_tracer = erts_tracer_nil;
system_seq_tracer = erts_tracer_nil;
#ifdef ERTS_SMP
init_sys_msg_dispatcher();
#endif
init_tracer_nif();
}
#define ERTS_ALLOC_SYSMSG_HEAP(SZ, BPP, OHPP, UNUSED) \
(*(BPP) = new_message_buffer((SZ)), \
*(OHPP) = &(*(BPP))->off_heap, \
(*(BPP))->mem)
static ERTS_INLINE int
send_to_tracer_nif_raw(Process *c_p, Process *tracee, const ErtsTracer tracer,
Uint trace_flags, Eterm t_p_id, ErtsTracerNif *tnif,
Eterm tag, Eterm msg, Eterm extra, Eterm pam_result);
static ERTS_INLINE int
send_to_tracer_nif(Process *c_p, ErtsPTabElementCommon *t_p,
Eterm t_p_id, ErtsTracerNif *tnif, Eterm tag,
Eterm msg, Eterm extra);
static ERTS_INLINE Eterm
call_enabled_tracer(Process *c_p, const ErtsTracer tracer,
ErtsTracerNif **tnif_ref, Eterm tag, Eterm t_p_id);
static int
is_tracer_proc_enabled(Process* c_p, ErtsProcLocks c_p_locks,
ErtsPTabElementCommon *t_p,
ErtsTracerNif **tnif_ret, Eterm tag);
#define SEND_TO_TRACER(c_p, tag, msg) \
send_to_tracer_nif(c_p, &(c_p)->common, (c_p)->common.id, NULL, tag, \
msg, THE_NON_VALUE)
static Uint active_sched;
void
erts_system_profile_setup_active_schedulers(void)
{
ERTS_SMP_LC_ASSERT(erts_thr_progress_is_blocking());
active_sched = erts_active_schedulers();
}
static void
exiting_reset(Eterm exiting)
{
erts_smp_rwmtx_rwlock(&sys_trace_rwmtx);
if (exiting == system_monitor) {
#ifdef ERTS_SMP
system_monitor = NIL;
/* Let the trace message dispatcher clear flags, etc */
#else
erts_system_monitor_clear(NULL);
#endif
}
if (exiting == system_profile) {
#ifdef ERTS_SMP
system_profile = NIL;
/* Let the trace message dispatcher clear flags, etc */
#else
erts_system_profile_clear(NULL);
#endif
}
erts_smp_rwmtx_rwunlock(&sys_trace_rwmtx);
}
void
erts_trace_check_exiting(Eterm exiting)
{
int reset = 0;
erts_smp_rwmtx_rlock(&sys_trace_rwmtx);
if (exiting == system_monitor)
reset = 1;
else if (exiting == system_profile)
reset = 1;
erts_smp_rwmtx_runlock(&sys_trace_rwmtx);
if (reset)
exiting_reset(exiting);
}
ErtsTracer
erts_set_system_seq_tracer(Process *c_p, ErtsProcLocks c_p_locks, ErtsTracer new)
{
ErtsTracer old;
if (!ERTS_TRACER_IS_NIL(new)) {
Eterm nif_result = call_enabled_tracer(
NULL, new, NULL,
am_trace_status, am_undefined);
switch (nif_result) {
case am_trace: break;
default:
return THE_NON_VALUE;
}
}
erts_smp_rwmtx_rwlock(&sys_trace_rwmtx);
old = system_seq_tracer;
system_seq_tracer = erts_tracer_nil;
erts_tracer_update(&system_seq_tracer, new);
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "set seq tracer new=%T old=%T\n", new, old);
#endif
erts_smp_rwmtx_rwunlock(&sys_trace_rwmtx);
return old;
}
ErtsTracer
erts_get_system_seq_tracer(void)
{
ErtsTracer st;
erts_smp_rwmtx_rlock(&sys_trace_rwmtx);
st = system_seq_tracer;
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "get seq tracer %T\n", st);
#endif
erts_smp_rwmtx_runlock(&sys_trace_rwmtx);
if (st != erts_tracer_nil &&
call_enabled_tracer(NULL, st, NULL, am_trace_status, am_undefined) == am_remove) {
erts_set_system_seq_tracer(NULL, 0, erts_tracer_nil);
st = erts_tracer_nil;
}
return st;
}
static ERTS_INLINE void
get_default_tracing(Uint *flagsp, ErtsTracer *tracerp,
Uint *default_trace_flags,
ErtsTracer *default_tracer)
{
if (!(*default_trace_flags & TRACEE_FLAGS))
ERTS_TRACER_CLEAR(default_tracer);
if (ERTS_TRACER_IS_NIL(*default_tracer)) {
*default_trace_flags &= ~TRACEE_FLAGS;
} else {
Eterm nif_result = call_enabled_tracer(
NULL, *default_tracer, NULL,
am_trace_status, am_undefined);
switch (nif_result) {
case am_trace: break;
default: {
ErtsTracer curr_default_tracer = *default_tracer;
if (tracerp) {
/* we only have a rlock, so we have to unlock and then rwlock */
erts_smp_rwmtx_runlock(&sys_trace_rwmtx);
erts_smp_rwmtx_rwlock(&sys_trace_rwmtx);
}
/* check if someone else changed default tracer
while we got the write lock, if so we don't do
anything. */
if (curr_default_tracer == *default_tracer) {
*default_trace_flags &= ~TRACEE_FLAGS;
ERTS_TRACER_CLEAR(default_tracer);
}
if (tracerp) {
erts_smp_rwmtx_rwunlock(&sys_trace_rwmtx);
erts_smp_rwmtx_rlock(&sys_trace_rwmtx);
}
}
}
}
if (flagsp)
*flagsp = *default_trace_flags;
if (tracerp) {
erts_tracer_update(tracerp,*default_tracer);
}
}
static ERTS_INLINE void
erts_change_default_tracing(int setflags, Uint flags,
const ErtsTracer tracer,
Uint *default_trace_flags,
ErtsTracer *default_tracer)
{
if (setflags)
*default_trace_flags |= flags;
else
*default_trace_flags &= ~flags;
erts_tracer_update(default_tracer, tracer);
get_default_tracing(NULL, NULL, default_trace_flags, default_tracer);
}
void
erts_change_default_proc_tracing(int setflags, Uint flagsp,
const ErtsTracer tracer)
{
erts_smp_rwmtx_rwlock(&sys_trace_rwmtx);
erts_change_default_tracing(
setflags, flagsp, tracer,
&default_proc_trace_flags,
&default_proc_tracer);
erts_smp_rwmtx_rwunlock(&sys_trace_rwmtx);
}
void
erts_change_default_port_tracing(int setflags, Uint flagsp,
const ErtsTracer tracer)
{
erts_smp_rwmtx_rwlock(&sys_trace_rwmtx);
erts_change_default_tracing(
setflags, flagsp, tracer,
&default_port_trace_flags,
&default_port_tracer);
erts_smp_rwmtx_rwunlock(&sys_trace_rwmtx);
}
void
erts_get_default_proc_tracing(Uint *flagsp, ErtsTracer *tracerp)
{
erts_smp_rwmtx_rlock(&sys_trace_rwmtx);
*tracerp = erts_tracer_nil; /* initialize */
get_default_tracing(
flagsp, tracerp,
&default_proc_trace_flags,
&default_proc_tracer);
erts_smp_rwmtx_runlock(&sys_trace_rwmtx);
}
void
erts_get_default_port_tracing(Uint *flagsp, ErtsTracer *tracerp)
{
erts_smp_rwmtx_rlock(&sys_trace_rwmtx);
*tracerp = erts_tracer_nil; /* initialize */
get_default_tracing(
flagsp, tracerp,
&default_port_trace_flags,
&default_port_tracer);
erts_smp_rwmtx_runlock(&sys_trace_rwmtx);
}
void
erts_set_system_monitor(Eterm monitor)
{
erts_smp_rwmtx_rwlock(&sys_trace_rwmtx);
system_monitor = monitor;
erts_smp_rwmtx_rwunlock(&sys_trace_rwmtx);
}
Eterm
erts_get_system_monitor(void)
{
Eterm monitor;
erts_smp_rwmtx_rlock(&sys_trace_rwmtx);
monitor = system_monitor;
erts_smp_rwmtx_runlock(&sys_trace_rwmtx);
return monitor;
}
/* Performance monitoring */
void erts_set_system_profile(Eterm profile) {
erts_smp_rwmtx_rwlock(&sys_trace_rwmtx);
system_profile = profile;
erts_smp_rwmtx_rwunlock(&sys_trace_rwmtx);
}
Eterm
erts_get_system_profile(void) {
Eterm profile;
erts_smp_rwmtx_rlock(&sys_trace_rwmtx);
profile = system_profile;
erts_smp_rwmtx_runlock(&sys_trace_rwmtx);
return profile;
}
#ifdef HAVE_ERTS_NOW_CPU
# define GET_NOW(m, s, u) \
do { \
if (erts_cpu_timestamp) \
erts_get_now_cpu(m, s, u); \
else \
get_now(m, s, u); \
} while (0)
#else
# define GET_NOW(m, s, u) do {get_now(m, s, u);} while (0)
#endif
static void
write_sys_msg_to_port(Eterm unused_to,
Port* trace_port,
Eterm unused_from,
enum ErtsSysMsgType unused_type,
Eterm message) {
byte *buffer;
byte *ptr;
unsigned size;
size = erts_encode_ext_size(message);
buffer = (byte *) erts_alloc(ERTS_ALC_T_TMP, size);
ptr = buffer;
erts_encode_ext(message, &ptr);
if (!(ptr <= buffer+size)) {
erts_exit(ERTS_ERROR_EXIT, "Internal error in do_send_to_port: %d\n", ptr-buffer);
}
#ifndef ERTS_SMP
if (!INVALID_TRACER_PORT(trace_port, trace_port->common.id))
#endif
erts_raw_port_command(trace_port, buffer, ptr-buffer);
erts_free(ERTS_ALC_T_TMP, (void *) buffer);
}
#ifndef ERTS_SMP
/* Profile send
* Checks if profiler is port or process
* Eterm msg is local, need copying.
*/
static void
profile_send(Eterm from, Eterm message) {
Uint sz = 0;
Uint *hp = NULL;
Eterm msg = NIL;
Process *profile_p = NULL;
Eterm profiler = erts_get_system_profile();
/* do not profile profiler pid */
if (from == profiler) return;
if (is_internal_port(profiler)) {
Port *profiler_port = NULL;
/* not smp */
profiler_port = erts_id2port_sflgs(profiler,
NULL,
0,
ERTS_PORT_SFLGS_INVALID_TRACER_LOOKUP);
if (profiler_port) {
write_sys_msg_to_port(profiler,
profiler_port,
NIL, /* or current process->common.id */
SYS_MSG_TYPE_SYSPROF,
message);
erts_port_release(profiler_port);
}
} else {
ErtsMessage *mp;
ASSERT(is_internal_pid(profiler));
profile_p = erts_proc_lookup(profiler);
if (!profile_p)
return;
sz = size_object(message);
mp = erts_alloc_message(sz, &hp);
if (sz == 0)
msg = message;
else
msg = copy_struct(message, sz, &hp, &mp->hfrag.off_heap);
erts_queue_message(profile_p, NULL, mp, msg);
}
}
#endif
static void
trace_sched_aux(Process *p, ErtsProcLocks locks, Eterm what)
{
Eterm tmp, *hp;
int curr_func;
ErtsTracerNif *tnif = NULL;
if (ERTS_TRACER_IS_NIL(ERTS_TRACER(p)))
return;
switch (what) {
case am_out:
case am_out_exiting:
case am_out_exited:
case am_in:
case am_in_exiting:
break;
default:
ASSERT(0);
break;
}
if (!is_tracer_proc_enabled(p, locks, &p->common, &tnif, what))
return;
if (ERTS_PROC_IS_EXITING(p))
curr_func = 0;
else {
if (!p->current)
p->current = find_function_from_pc(p->i);
curr_func = p->current != NULL;
}
if (!curr_func) {
tmp = make_small(0);
} else {
hp = HAlloc(p, 4);
tmp = TUPLE3(hp,p->current[0],p->current[1],make_small(p->current[2]));
hp += 4;
}
send_to_tracer_nif(p, &p->common, p->common.id, tnif,
what, tmp, THE_NON_VALUE);
}
/* Send {trace_ts, Pid, What, {Mod, Func, Arity}, Timestamp}
* or {trace, Pid, What, {Mod, Func, Arity}}
*
* where 'What' is supposed to be 'in', 'out', 'in_exiting',
* 'out_exiting', or 'out_exited'.
*/
void
trace_sched(Process *p, ErtsProcLocks locks, Eterm what)
{
trace_sched_aux(p, locks, what);
}
/* Send {trace_ts, Pid, Send, Msg, DestPid, Timestamp}
* or {trace, Pid, Send, Msg, DestPid}
*
* where 'Send' is 'send' or 'send_to_non_existing_process'.
*/
void
trace_send(Process *p, Eterm to, Eterm msg)
{
Eterm operation = am_send;
ErtsTracerNif *tnif = NULL;
ASSERT(ARE_TRACE_FLAGS_ON(p, F_TRACE_SEND));
if (is_internal_pid(to)) {
if (!erts_proc_lookup(to))
goto send_to_non_existing_process;
}
else if(is_external_pid(to)
&& external_pid_dist_entry(to) == erts_this_dist_entry) {
send_to_non_existing_process:
operation = am_send_to_non_existing_process;
}
if (is_tracer_proc_enabled(p, ERTS_PROC_LOCK_MAIN, &p->common, &tnif, operation))
send_to_tracer_nif(p, &p->common, p->common.id, tnif, operation, msg, to);
}
/* Send {trace_ts, Pid, receive, Msg, Timestamp}
* or {trace, Pid, receive, Msg}
*/
void
trace_receive(Process *c_p, Eterm msg)
{
ErtsTracerNif *tnif = NULL;
if (is_tracer_proc_enabled(NULL, 0, &c_p->common,
&tnif, am_receive))
send_to_tracer_nif(NULL, &c_p->common, c_p->common.id,
tnif, am_receive, msg, THE_NON_VALUE);
}
int
seq_trace_update_send(Process *p)
{
ErtsTracer seq_tracer = erts_get_system_seq_tracer();
ASSERT((is_tuple(SEQ_TRACE_TOKEN(p)) || is_nil(SEQ_TRACE_TOKEN(p))));
if (have_no_seqtrace(SEQ_TRACE_TOKEN(p)) ||
(seq_tracer != NIL &&
call_enabled_tracer( NULL, seq_tracer, NULL, am_trace_status,
p ? p->common.id : am_undefined) != am_trace)
#ifdef USE_VM_PROBES
|| (SEQ_TRACE_TOKEN(p) == am_have_dt_utag)
#endif
) {
return 0;
}
SEQ_TRACE_TOKEN_SENDER(p) = p->common.id;
SEQ_TRACE_TOKEN_SERIAL(p) =
make_small(++(p -> seq_trace_clock));
SEQ_TRACE_TOKEN_LASTCNT(p) =
make_small(p -> seq_trace_lastcnt);
return 1;
}
/* Send a sequential trace message to the sequential tracer.
* p is the caller (which contains the trace token),
* msg is the original message, type is trace type (SEQ_TRACE_SEND etc),
* and receiver is the receiver of the message.
*
* The message to be received by the sequential tracer is:
*
* TraceMsg =
* {seq_trace, Label, {Type, {Lastcnt, Serial}, Sender, Receiver, Msg} [,Timestamp] }
*
*/
void
seq_trace_output_generic(Eterm token, Eterm msg, Uint type,
Eterm receiver, Process *process, Eterm exitfrom)
{
Eterm mess;
Eterm* hp;
Eterm label;
Eterm lastcnt_serial;
Eterm type_atom;
ErtsTracer seq_tracer;
int seq_tracer_flags = 0;
#define LOCAL_HEAP_SIZE (64)
DeclareTmpHeapNoproc(local_heap,LOCAL_HEAP_SIZE);
seq_tracer = erts_get_system_seq_tracer();
ASSERT(is_tuple(token) || is_nil(token));
if (token == NIL || (process && ERTS_TRACE_FLAGS(process) & F_SENSITIVE) ||
ERTS_TRACER_IS_NIL(seq_tracer) ||
call_enabled_tracer(
NULL, seq_tracer, NULL, am_trace_status,
process ? process->common.id : am_undefined) != am_trace) {
return;
}
if ((unsigned_val(SEQ_TRACE_T_FLAGS(token)) & type) == 0) {
/* No flags set, nothing to do */
return;
}
switch (type) {
case SEQ_TRACE_SEND: type_atom = am_send; break;
case SEQ_TRACE_PRINT: type_atom = am_print; break;
case SEQ_TRACE_RECEIVE: type_atom = am_receive; break;
default:
erts_exit(ERTS_ERROR_EXIT, "invalid type in seq_trace_output_generic: %d:\n", type);
return; /* To avoid warning */
}
UseTmpHeapNoproc(LOCAL_HEAP_SIZE);
hp = local_heap;
label = SEQ_TRACE_T_LABEL(token);
lastcnt_serial = TUPLE2(hp, SEQ_TRACE_T_LASTCNT(token),
SEQ_TRACE_T_SERIAL(token));
hp += 3;
if (exitfrom != NIL) {
msg = TUPLE3(hp, am_EXIT, exitfrom, msg);
hp += 4;
}
mess = TUPLE5(hp, type_atom, lastcnt_serial, SEQ_TRACE_T_SENDER(token),
receiver, msg);
hp += 6;
seq_tracer_flags |= ERTS_SEQTFLGS2TFLGS(unsigned_val(SEQ_TRACE_T_FLAGS(token)));
send_to_tracer_nif_raw(NULL, process, seq_tracer, seq_tracer_flags,
label, NULL, am_seq_trace, mess,
THE_NON_VALUE, am_true);
UnUseTmpHeapNoproc(LOCAL_HEAP_SIZE);
#undef LOCAL_HEAP_SIZE
}
/* Send {trace_ts, Pid, return_to, {Mod, Func, Arity}, Timestamp}
* or {trace, Pid, return_to, {Mod, Func, Arity}}
*/
void
erts_trace_return_to(Process *p, BeamInstr *pc)
{
Eterm mfa;
BeamInstr *code_ptr = find_function_from_pc(pc);
if (!code_ptr) {
mfa = am_undefined;
} else {
Eterm *hp = HAlloc(p, 4);
mfa = TUPLE3(hp, code_ptr[0], code_ptr[1], make_small(code_ptr[2]));
}
SEND_TO_TRACER(p, am_return_to, mfa);
}
/* Send {trace_ts, Pid, return_from, {Mod, Name, Arity}, Retval, Timestamp}
* or {trace, Pid, return_from, {Mod, Name, Arity}, Retval}
*/
void
erts_trace_return(Process* p, BeamInstr* fi, Eterm retval, ErtsTracer *tracer)
{
Eterm* hp;
Eterm mfa, mod, name;
int arity;
Uint meta_flags, *tracee_flags;
ASSERT(tracer);
if (ERTS_TRACER_COMPARE(*tracer, erts_tracer_true)) {
/* Breakpoint trace enabled without specifying tracer =>
* use process tracer and flags
*/
tracer = &ERTS_TRACER(p);
}
if (ERTS_TRACER_IS_NIL(*tracer)) {
/* Trace disabled */
return;
}
ASSERT(IS_TRACER_VALID(*tracer));
if (tracer == &ERTS_TRACER(p)) {
/* Tracer specified in process structure =>
* non-breakpoint trace =>
* use process flags
*/
tracee_flags = &ERTS_TRACE_FLAGS(p);
if (! (*tracee_flags & F_TRACE_CALLS)) {
return;
}
} else {
/* Tracer not specified in process structure =>
* tracer specified in breakpoint =>
* meta trace =>
* use fixed flag set instead of process flags
*/
meta_flags = F_TRACE_CALLS | F_NOW_TS;
tracee_flags = &meta_flags;
}
mod = fi[0];
name = fi[1];
arity = fi[2];
hp = HAlloc(p, 4);
mfa = TUPLE3(hp, mod, name, make_small(arity));
hp += 4;
send_to_tracer_nif_raw(p, NULL, *tracer, *tracee_flags, p->common.id,
NULL, am_return_from, mfa, retval, am_true);
}
/* Send {trace_ts, Pid, exception_from, {Mod, Name, Arity}, {Class,Value},
* Timestamp}
* or {trace, Pid, exception_from, {Mod, Name, Arity}, {Class,Value},
* Timestamp}
*
* Where Class is atomic but Value is any term.
*/
void
erts_trace_exception(Process* p, BeamInstr mfa[3], Eterm class, Eterm value,
ErtsTracer *tracer)
{
Eterm* hp;
Eterm mfa_tuple, cv;
Uint meta_flags, *tracee_flags;
ASSERT(tracer);
if (ERTS_TRACER_COMPARE(*tracer, erts_tracer_true)) {
/* Breakpoint trace enabled without specifying tracer =>
* use process tracer and flags
*/
tracer = &ERTS_TRACER(p);
}
if (ERTS_TRACER_IS_NIL(*tracer)) {
/* Trace disabled */
return;
}
ASSERT(IS_TRACER_VALID(*tracer));
if (tracer == &ERTS_TRACER(p)) {
/* Tracer specified in process structure =>
* non-breakpoint trace =>
* use process flags
*/
tracee_flags = &ERTS_TRACE_FLAGS(p);
if (! (*tracee_flags & F_TRACE_CALLS)) {
return;
}
} else {
/* Tracer not specified in process structure =>
* tracer specified in breakpoint =>
* meta trace =>
* use fixed flag set instead of process flags
*/
meta_flags = F_TRACE_CALLS | F_NOW_TS;
tracee_flags = &meta_flags;
}
hp = HAlloc(p, 7);;
mfa_tuple = TUPLE3(hp, (Eterm) mfa[0], (Eterm) mfa[1], make_small((Eterm)mfa[2]));
hp += 4;
cv = TUPLE2(hp, class, value);
hp += 3;
send_to_tracer_nif_raw(p, NULL, *tracer, *tracee_flags, p->common.id,
NULL, am_exception_from, mfa_tuple, cv, am_true);
}
/*
* This function implements the new call trace.
*
* Send {trace_ts, Pid, call, {Mod, Func, A}, PamResult, Timestamp}
* or {trace_ts, Pid, call, {Mod, Func, A}, Timestamp}
* or {trace, Pid, call, {Mod, Func, A}, PamResult}
* or {trace, Pid, call, {Mod, Func, A}
*
* where 'A' is arity or argument list depending on trace flag 'arity'.
*
* If *tracer_pid is am_true, it is a breakpoint trace that shall use
* the process tracer, if it is NIL no trace message is generated,
* if it is a pid or port we do a meta trace.
*/
Uint32
erts_call_trace(Process* p, BeamInstr mfa[3], Binary *match_spec,
Eterm* args, int local, ErtsTracer *tracer)
{
Eterm* hp;
Eterm mfa_tuple;
int arity;
int i;
Uint32 return_flags;
Eterm pam_result = am_true;
Uint meta_flags, *tracee_flags;
ErtsTracerNif *tnif = NULL;
Eterm transformed_args[MAX_ARG];
ErtsTracer pre_ms_tracer = erts_tracer_nil;
ASSERT(tracer);
if (ERTS_TRACER_COMPARE(*tracer, erts_tracer_true)) {
/* Breakpoint trace enabled without specifying tracer =>
* use process tracer and flags
*/
tracer = &ERTS_TRACER(p);
}
if (ERTS_TRACER_IS_NIL(*tracer)) {
/* Trace disabled */
return 0;
}
ASSERT(IS_TRACER_VALID(*tracer));
if (tracer == &ERTS_TRACER(p)) {
/* Tracer specified in process structure =>
* non-breakpoint trace =>
* use process flags
*/
tracee_flags = &ERTS_TRACE_FLAGS(p);
if (!is_tracer_proc_enabled(p, ERTS_PROC_LOCK_MAIN, &p->common, &tnif, am_call)) {
return 0;
}
} else {
/* Tracer not specified in process structure =>
* tracer specified in breakpoint =>
* meta trace =>
* use fixed flag set instead of process flags
*/
if (ERTS_TRACE_FLAGS(p) & F_SENSITIVE) {
/* No trace messages for sensitive processes. */
return 0;
}
meta_flags = F_TRACE_CALLS | F_NOW_TS;
tracee_flags = &meta_flags;
switch (call_enabled_tracer(p, *tracer, &tnif, am_call, p->common.id)) {
default:
case am_remove: *tracer = erts_tracer_nil;
case am_discard: return 0;
case am_trace: break;
}
}
/*
* Because of the delayed sub-binary creation optimization introduced in
* R12B, (at most) one of arguments can be a match context instead of
* a binary. Since we don't want to handle match contexts in utility functions
* such as size_object() and copy_struct(), we must make sure that we
* temporarily convert any match contexts to sub binaries.
*/
arity = (Eterm) mfa[2];
for (i = 0; i < arity; i++) {
Eterm arg = args[i];
if (is_boxed(arg) && header_is_bin_matchstate(*boxed_val(arg))) {
ErlBinMatchState* ms = (ErlBinMatchState *) boxed_val(arg);
ErlBinMatchBuffer* mb = &ms->mb;
Uint bit_size;
ErlSubBin *sub_bin_heap = (ErlSubBin *)HAlloc(p, ERL_SUB_BIN_SIZE);
bit_size = mb->size - mb->offset;
sub_bin_heap->thing_word = HEADER_SUB_BIN;
sub_bin_heap->size = BYTE_OFFSET(bit_size);
sub_bin_heap->bitsize = BIT_OFFSET(bit_size);
sub_bin_heap->offs = BYTE_OFFSET(mb->offset);
sub_bin_heap->bitoffs = BIT_OFFSET(mb->offset);
sub_bin_heap->is_writable = 0;
sub_bin_heap->orig = mb->orig;
arg = make_binary(sub_bin_heap);
}
transformed_args[i] = arg;
}
args = transformed_args;
/*
* If there is a PAM program, run it. Return if it fails.
*
* Some precedence rules:
*
* - No proc flags, e.g 'silent' or 'return_to'
* has any effect on meta trace.
* - The 'silent' process trace flag silences all call
* related messages, e.g 'call', 'return_to' and 'return_from'.
* - The {message,_} PAM function does not affect {return_trace}.
* - The {message,false} PAM function shall give the same
* 'call' trace message as no PAM match.
* - The {message,true} PAM function shall give the same
* 'call' trace message as a nonexistent PAM program.
*/
return_flags = 0;
if (match_spec) {
/* we have to make a copy of the tracer here as the match spec
may remove it, and we still want to generate a trace message */
erts_tracer_update(&pre_ms_tracer, *tracer);
tracer = &pre_ms_tracer;
pam_result = erts_match_set_run(p, match_spec, args, arity,
ERTS_PAM_TMP_RESULT, &return_flags);
if (is_non_value(pam_result)) {
erts_match_set_release_result(p);
UnUseTmpHeap(ERL_SUB_BIN_SIZE,p);
ERTS_TRACER_CLEAR(&pre_ms_tracer);
return 0;
}
}
if (tracee_flags == &meta_flags) {
/* Meta trace */
if (pam_result == am_false) {
erts_match_set_release_result(p);
UnUseTmpHeap(ERL_SUB_BIN_SIZE,p);
ERTS_TRACER_CLEAR(&pre_ms_tracer);
return return_flags;
}
} else {
/* Non-meta trace */
if (*tracee_flags & F_TRACE_SILENT) {
erts_match_set_release_result(p);
UnUseTmpHeap(ERL_SUB_BIN_SIZE,p);
ERTS_TRACER_CLEAR(&pre_ms_tracer);
return 0;
}
if (pam_result == am_false) {
erts_match_set_release_result(p);
UnUseTmpHeap(ERL_SUB_BIN_SIZE,p);
ERTS_TRACER_CLEAR(&pre_ms_tracer);
return return_flags;
}
if (local && (*tracee_flags & F_TRACE_RETURN_TO)) {
return_flags |= MATCH_SET_RETURN_TO_TRACE;
}
}
ASSERT(!ERTS_TRACER_IS_NIL(*tracer));
/*
* Build the the {M,F,A} tuple in the local heap.
* (A is arguments or arity.)
*/
if (*tracee_flags & F_TRACE_ARITY_ONLY) {
hp = HAlloc(p, 4);
mfa_tuple = make_small(arity);
} else {
hp = HAlloc(p, 4 + arity * 2);
mfa_tuple = NIL;
for (i = arity-1; i >= 0; i--) {
mfa_tuple = CONS(hp, args[i], mfa_tuple);
hp += 2;
}
}
mfa_tuple = TUPLE3(hp, (Eterm) mfa[0], (Eterm) mfa[1], mfa_tuple);
hp += 4;
/*
* Build the trace tuple and send it to the port.
*/
send_to_tracer_nif_raw(p, NULL, *tracer, *tracee_flags, p->common.id,
tnif, am_call, mfa_tuple, THE_NON_VALUE, pam_result);
erts_match_set_release_result(p);
if (match_spec && tracer == &pre_ms_tracer)
ERTS_TRACER_CLEAR(&pre_ms_tracer);
return return_flags;
}
/* Sends trace message:
* {trace_ts, ProcessPid, What, Data, Timestamp}
* or {trace, ProcessPid, What, Data}
*
* 'what' must be atomic, 'data' may be a deep term.
* 'c_p' is the currently executing process, may be NULL.
* 't_p' is the traced process.
*/
void
trace_proc(Process *c_p, ErtsProcLocks c_p_locks,
Process *t_p, Eterm what, Eterm data)
{
ErtsTracerNif *tnif = NULL;
if (is_tracer_proc_enabled(c_p, c_p_locks, &t_p->common, &tnif, what))
send_to_tracer_nif(c_p, &t_p->common, t_p->common.id, tnif,
what, data, THE_NON_VALUE);
}
/* Sends trace message:
* {trace_ts, ParentPid, spawn, ChildPid, {Mod, Func, Args}, Timestamp}
* or {trace, ParentPid, spawn, ChildPid, {Mod, Func, Args}}
*
* 'pid' is the ChildPid, 'mod' and 'func' must be atomic,
* and 'args' may be a deep term.
*/
void
trace_proc_spawn(Process *p, Eterm pid,
Eterm mod, Eterm func, Eterm args)
{
ErtsTracerNif *tnif = NULL;
if (is_tracer_proc_enabled(p, ERTS_PROC_LOCKS_ALL &
~(ERTS_PROC_LOCK_STATUS|ERTS_PROC_LOCK_TRACE),
&p->common, &tnif, am_spawn)) {
Eterm mfa;
Eterm* hp;
hp = HAlloc(p, 4);
mfa = TUPLE3(hp, mod, func, args);
hp += 4;
send_to_tracer_nif(p, &p->common, p->common.id, tnif, am_spawn, pid, mfa);
}
}
void save_calls(Process *p, Export *e)
{
struct saved_calls *scb = ERTS_PROC_GET_SAVED_CALLS_BUF(p);
if (scb) {
Export **ct = &scb->ct[0];
int len = scb->len;
ct[scb->cur] = e;
if (++scb->cur >= len)
scb->cur = 0;
if (scb->n < len)
scb->n++;
}
}
/* Sends trace message:
* {trace_ts, Pid, What, Msg, Timestamp}
* or {trace, Pid, What, Msg}
*
* where 'What' must be atomic and 'Msg' is:
* [{heap_size, HeapSize}, {old_heap_size, OldHeapSize},
* {stack_size, StackSize}, {recent_size, RecentSize},
* {mbuf_size, MbufSize}]
*
* where 'HeapSize', 'OldHeapSize', 'StackSize', 'RecentSize and 'MbufSize'
* are all small (atomic) integers.
*/
void
trace_gc(Process *p, Eterm what)
{
ErtsTracerNif *tnif = NULL;
Eterm* hp;
Eterm msg = NIL;
Uint size = 0;
if (is_tracer_proc_enabled(
p, ERTS_PROC_LOCK_MAIN, &p->common, &tnif, what)) {
(void) erts_process_gc_info(p, &size, NULL);
hp = HAlloc(p, size);
msg = erts_process_gc_info(p, NULL, &hp);
send_to_tracer_nif(p, &p->common, p->common.id, tnif, what,
msg, THE_NON_VALUE);
}
}
void
monitor_long_schedule_proc(Process *p, BeamInstr *in_fp, BeamInstr *out_fp, Uint time)
{
ErlHeapFragment *bp;
ErlOffHeap *off_heap;
#ifndef ERTS_SMP
Process *monitor_p;
#endif
Uint hsz;
Eterm *hp, list, in_mfa = am_undefined, out_mfa = am_undefined;
Eterm in_tpl, out_tpl, tmo_tpl, tmo, msg;
#ifndef ERTS_SMP
ASSERT(is_internal_pid(system_monitor));
monitor_p = erts_proc_lookup(system_monitor);
if (!monitor_p || p == monitor_p) {
return;
}
#endif
/*
* Size: {monitor, pid, long_schedule, [{timeout, T}, {in, {M,F,A}},{out,{M,F,A}}]} ->
* 5 (top tuple of 4), (3 (elements) * 2 (cons)) + 3 (timeout tuple of 2) + size of Timeout +
* (2 * 3 (in/out tuple of 2)) +
* 0 (unknown) or 4 (MFA tuple of 3) + 0 (unknown) or 4 (MFA tuple of 3)
* = 20 + (in_fp != NULL) ? 4 : 0 + (out_fp != NULL) ? 4 : 0 + size of Timeout
*/
hsz = 20 + ((in_fp != NULL) ? 4 : 0) + ((out_fp != NULL) ? 4 : 0);
(void) erts_bld_uint(NULL, &hsz, time);
hp = ERTS_ALLOC_SYSMSG_HEAP(hsz, &bp, &off_heap, monitor_p);
tmo = erts_bld_uint(&hp, NULL, time);
if (in_fp != NULL) {
in_mfa = TUPLE3(hp,(Eterm) in_fp[0], (Eterm) in_fp[1], make_small(in_fp[2]));
hp +=4;
}
if (out_fp != NULL) {
out_mfa = TUPLE3(hp,(Eterm) out_fp[0], (Eterm) out_fp[1], make_small(out_fp[2]));
hp +=4;
}
tmo_tpl = TUPLE2(hp,am_timeout, tmo);
hp += 3;
in_tpl = TUPLE2(hp,am_in,in_mfa);
hp += 3;
out_tpl = TUPLE2(hp,am_out,out_mfa);
hp += 3;
list = CONS(hp,out_tpl,NIL);
hp += 2;
list = CONS(hp,in_tpl,list);
hp += 2;
list = CONS(hp,tmo_tpl,list);
hp += 2;
msg = TUPLE4(hp, am_monitor, p->common.id, am_long_schedule, list);
hp += 5;
#ifdef ERTS_SMP
enqueue_sys_msg(SYS_MSG_TYPE_SYSMON, p->common.id, NIL, msg, bp);
#else
{
ErtsMessage *mp = erts_alloc_message(0, NULL);
mp->data.heap_frag = bp;
erts_queue_message(monitor_p, NULL, mp, msg);
}
#endif
}
void
monitor_long_schedule_port(Port *pp, ErtsPortTaskType type, Uint time)
{
ErlHeapFragment *bp;
ErlOffHeap *off_heap;
#ifndef ERTS_SMP
Process *monitor_p;
#endif
Uint hsz;
Eterm *hp, list, op;
Eterm op_tpl, tmo_tpl, tmo, msg;
#ifndef ERTS_SMP
ASSERT(is_internal_pid(system_monitor));
monitor_p = erts_proc_lookup(system_monitor);
if (!monitor_p) {
return;
}
#endif
/*
* Size: {monitor, port, long_schedule, [{timeout, T}, {op, Operation}]} ->
* 5 (top tuple of 4), (2 (elements) * 2 (cons)) + 3 (timeout tuple of 2)
* + size of Timeout + 3 (op tuple of 2 atoms)
* = 15 + size of Timeout
*/
hsz = 15;
(void) erts_bld_uint(NULL, &hsz, time);
hp = ERTS_ALLOC_SYSMSG_HEAP(hsz, &bp, &off_heap, monitor_p);
switch (type) {
case ERTS_PORT_TASK_PROC_SIG: op = am_proc_sig; break;
case ERTS_PORT_TASK_TIMEOUT: op = am_timeout; break;
case ERTS_PORT_TASK_INPUT: op = am_input; break;
case ERTS_PORT_TASK_OUTPUT: op = am_output; break;
case ERTS_PORT_TASK_EVENT: op = am_event; break;
case ERTS_PORT_TASK_DIST_CMD: op = am_dist_cmd; break;
default: op = am_undefined; break;
}
tmo = erts_bld_uint(&hp, NULL, time);
op_tpl = TUPLE2(hp,am_port_op,op);
hp += 3;
tmo_tpl = TUPLE2(hp,am_timeout, tmo);
hp += 3;
list = CONS(hp,op_tpl,NIL);
hp += 2;
list = CONS(hp,tmo_tpl,list);
hp += 2;
msg = TUPLE4(hp, am_monitor, pp->common.id, am_long_schedule, list);
hp += 5;
#ifdef ERTS_SMP
enqueue_sys_msg(SYS_MSG_TYPE_SYSMON, pp->common.id, NIL, msg, bp);
#else
{
ErtsMessage *mp = erts_alloc_message(0, NULL);
mp->data.heap_frag = bp;
erts_queue_message(monitor_p, NULL, mp, msg);
}
#endif
}
void
monitor_long_gc(Process *p, Uint time) {
ErlHeapFragment *bp;
ErlOffHeap *off_heap;
#ifndef ERTS_SMP
Process *monitor_p;
#endif
Uint hsz;
Eterm *hp, list, msg;
Eterm tags[] = {
am_timeout,
am_old_heap_block_size,
am_heap_block_size,
am_mbuf_size,
am_stack_size,
am_old_heap_size,
am_heap_size
};
UWord values[] = {
time,
OLD_HEAP(p) ? OLD_HEND(p) - OLD_HEAP(p) : 0,
HEAP_SIZE(p),
MBUF_SIZE(p),
STACK_START(p) - p->stop,
OLD_HEAP(p) ? OLD_HTOP(p) - OLD_HEAP(p) : 0,
HEAP_TOP(p) - HEAP_START(p)
};
#ifdef DEBUG
Eterm *hp_end;
#endif
#ifndef ERTS_SMP
ASSERT(is_internal_pid(system_monitor));
monitor_p = erts_proc_lookup(system_monitor);
if (!monitor_p || p == monitor_p)
return;
#endif
hsz = 0;
(void) erts_bld_atom_uword_2tup_list(NULL,
&hsz,
sizeof(values)/sizeof(*values),
tags,
values);
hsz += 5 /* 4-tuple */;
hp = ERTS_ALLOC_SYSMSG_HEAP(hsz, &bp, &off_heap, monitor_p);
#ifdef DEBUG
hp_end = hp + hsz;
#endif
list = erts_bld_atom_uword_2tup_list(&hp,
NULL,
sizeof(values)/sizeof(*values),
tags,
values);
msg = TUPLE4(hp, am_monitor, p->common.id, am_long_gc, list);
#ifdef DEBUG
hp += 5 /* 4-tuple */;
ASSERT(hp == hp_end);
#endif
#ifdef ERTS_SMP
enqueue_sys_msg(SYS_MSG_TYPE_SYSMON, p->common.id, NIL, msg, bp);
#else
{
ErtsMessage *mp = erts_alloc_message(0, NULL);
mp->data.heap_frag = bp;
erts_queue_message(monitor_p, NULL, mp, msg);
}
#endif
}
void
monitor_large_heap(Process *p) {
ErlHeapFragment *bp;
ErlOffHeap *off_heap;
#ifndef ERTS_SMP
Process *monitor_p;
#endif
Uint hsz;
Eterm *hp, list, msg;
Eterm tags[] = {
am_old_heap_block_size,
am_heap_block_size,
am_mbuf_size,
am_stack_size,
am_old_heap_size,
am_heap_size
};
UWord values[] = {
OLD_HEAP(p) ? OLD_HEND(p) - OLD_HEAP(p) : 0,
HEAP_SIZE(p),
MBUF_SIZE(p),
STACK_START(p) - p->stop,
OLD_HEAP(p) ? OLD_HTOP(p) - OLD_HEAP(p) : 0,
HEAP_TOP(p) - HEAP_START(p)
};
#ifdef DEBUG
Eterm *hp_end;
#endif
#ifndef ERTS_SMP
ASSERT(is_internal_pid(system_monitor));
monitor_p = erts_proc_lookup(system_monitor);
if (!monitor_p || p == monitor_p) {
return;
}
#endif
hsz = 0;
(void) erts_bld_atom_uword_2tup_list(NULL,
&hsz,
sizeof(values)/sizeof(*values),
tags,
values);
hsz += 5 /* 4-tuple */;
hp = ERTS_ALLOC_SYSMSG_HEAP(hsz, &bp, &off_heap, monitor_p);
#ifdef DEBUG
hp_end = hp + hsz;
#endif
list = erts_bld_atom_uword_2tup_list(&hp,
NULL,
sizeof(values)/sizeof(*values),
tags,
values);
msg = TUPLE4(hp, am_monitor, p->common.id, am_large_heap, list);
#ifdef DEBUG
hp += 5 /* 4-tuple */;
ASSERT(hp == hp_end);
#endif
#ifdef ERTS_SMP
enqueue_sys_msg(SYS_MSG_TYPE_SYSMON, p->common.id, NIL, msg, bp);
#else
{
ErtsMessage *mp = erts_alloc_message(0, NULL);
mp->data.heap_frag = bp;
erts_queue_message(monitor_p, NULL, mp, msg);
}
#endif
}
void
monitor_generic(Process *p, Eterm type, Eterm spec) {
ErlHeapFragment *bp;
ErlOffHeap *off_heap;
#ifndef ERTS_SMP
Process *monitor_p;
#endif
Eterm *hp, msg;
#ifndef ERTS_SMP
ASSERT(is_internal_pid(system_monitor));
monitor_p = erts_proc_lookup(system_monitor);
if (!monitor_p || p == monitor_p)
return;
#endif
hp = ERTS_ALLOC_SYSMSG_HEAP(5, &bp, &off_heap, monitor_p);
msg = TUPLE4(hp, am_monitor, p->common.id, type, spec);
hp += 5;
#ifdef ERTS_SMP
enqueue_sys_msg(SYS_MSG_TYPE_SYSMON, p->common.id, NIL, msg, bp);
#else
{
ErtsMessage *mp = erts_alloc_message(0, NULL);
mp->data.heap_frag = bp;
erts_queue_message(monitor_p, NULL, mp, msg);
}
#endif
}
/* Begin system_profile tracing */
/* Scheduler profiling */
void
profile_scheduler(Eterm scheduler_id, Eterm state) {
Eterm *hp, msg;
ErlHeapFragment *bp = NULL;
#ifndef ERTS_SMP
#define LOCAL_HEAP_SIZE (7 + ERTS_TRACE_PATCH_TS_MAX_SIZE)
DeclareTmpHeapNoproc(local_heap,LOCAL_HEAP_SIZE);
UseTmpHeapNoproc(LOCAL_HEAP_SIZE);
hp = local_heap;
#else
Uint hsz;
hsz = 7 + patch_ts_size(erts_system_profile_ts_type)-1;
bp = new_message_buffer(hsz);
hp = bp->mem;
#endif
erts_smp_mtx_lock(&smq_mtx);
switch (state) {
case am_active:
active_sched++;
break;
case am_inactive:
active_sched--;
break;
default:
ASSERT(!"Invalid state");
break;
}
msg = TUPLE6(hp, am_profile, am_scheduler, scheduler_id,
state, make_small(active_sched),
NIL /* Will be overwritten by timestamp */);
hp += 7;
/* Write timestamp in element 6 of the 'msg' tuple */
hp[-1] = write_ts(erts_system_profile_ts_type, hp, bp, NULL);
#ifndef ERTS_SMP
profile_send(NIL, msg);
UnUseTmpHeapNoproc(LOCAL_HEAP_SIZE);
#undef LOCAL_HEAP_SIZE
#else
enqueue_sys_msg_unlocked(SYS_MSG_TYPE_SYSPROF, NIL, NIL, msg, bp);
#endif
erts_smp_mtx_unlock(&smq_mtx);
}
void
profile_scheduler_q(Eterm scheduler_id, Eterm state, Eterm no_schedulers, Uint Ms, Uint s, Uint us) {
Eterm *hp, msg, timestamp;
#ifndef ERTS_SMP
#define LOCAL_HEAP_SIZE (4 + 7)
DeclareTmpHeapNoproc(local_heap,LOCAL_HEAP_SIZE);
UseTmpHeapNoproc(LOCAL_HEAP_SIZE);
hp = local_heap;
#else
ErlHeapFragment *bp;
Uint hsz;
hsz = 4 + 7;
bp = new_message_buffer(hsz);
hp = bp->mem;
#endif
erts_smp_mtx_lock(&smq_mtx);
timestamp = TUPLE3(hp, make_small(Ms), make_small(s), make_small(us)); hp += 4;
msg = TUPLE6(hp, am_profile, am_scheduler, scheduler_id, state, no_schedulers, timestamp); hp += 7;
#ifndef ERTS_SMP
profile_send(NIL, msg);
UnUseTmpHeapNoproc(LOCAL_HEAP_SIZE);
#undef LOCAL_HEAP_SIZE
#else
enqueue_sys_msg_unlocked(SYS_MSG_TYPE_SYSPROF, NIL, NIL, msg, bp);
#endif
erts_smp_mtx_unlock(&smq_mtx);
}
/* Port profiling */
void
trace_port_open(Port *p, Eterm calling_pid, Eterm drv_name) {
ErtsTracerNif *tnif = NULL;
ERTS_SMP_CHK_NO_PROC_LOCKS;
if (is_tracer_proc_enabled(NULL, 0, &p->common, &tnif, am_open))
send_to_tracer_nif(NULL, &p->common, p->common.id, tnif, am_open,
calling_pid, drv_name);
}
/* Sends trace message:
* {trace_ts, PortPid, What, Data, Timestamp}
* or {trace, PortPid, What, Data}
*
* 'what' must be atomic, 'data' must be atomic.
* 't_p' is the traced port.
*/
void
trace_port(Port *t_p, Eterm what, Eterm data) {
ErtsTracerNif *tnif = NULL;
ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked(t_p)
|| erts_thr_progress_is_blocking());
ERTS_SMP_CHK_NO_PROC_LOCKS;
if (is_tracer_proc_enabled(NULL, 0, &t_p->common, &tnif, what))
send_to_tracer_nif(NULL, &t_p->common, t_p->common.id, tnif,
what, data, THE_NON_VALUE);
}
static Eterm
trace_port_tmp_binary(char *bin, Sint sz, Binary **bptrp, Eterm **hp)
{
if (sz <= ERL_ONHEAP_BIN_LIMIT) {
ErlHeapBin *hb = (ErlHeapBin *)*hp;
hb->thing_word = header_heap_bin(sz);
hb->size = sz;
sys_memcpy(hb->data, bin, sz);
*hp += heap_bin_size(sz);
return make_binary(hb);
} else {
ProcBin* pb = (ProcBin *)*hp;
Binary *bptr = erts_bin_nrml_alloc(sz);
erts_refc_init(&bptr->refc, 1);
sys_memcpy(bptr->orig_bytes, bin, sz);
pb->thing_word = HEADER_PROC_BIN;
pb->size = sz;
pb->next = NULL;
pb->val = bptr;
pb->bytes = (byte*) bptr->orig_bytes;
pb->flags = 0;
*bptrp = bptr;
*hp += PROC_BIN_SIZE;
return make_binary(pb);
}
}
/* Sends trace message:
* {trace, PortPid, 'receive', {pid(), {command, iolist()}}}
* {trace, PortPid, 'receive', {pid(), {control, pid()}}}
* {trace, PortPid, 'receive', {pid(), exit}}
*
*/
void
trace_port_receive(Port *t_p, Eterm caller, Eterm what, ...)
{
ErtsTracerNif *tnif = NULL;
ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked(t_p)
|| erts_thr_progress_is_blocking());
ERTS_SMP_CHK_NO_PROC_LOCKS;
if (is_tracer_proc_enabled(NULL, 0, &t_p->common, &tnif, am_receive)) {
/* We can use a stack heap here, as the nif is called in the
context of a port */
#define LOCAL_HEAP_SIZE (3 + 3 + heap_bin_size(ERL_ONHEAP_BIN_LIMIT) + 3)
DeclareTmpHeapNoproc(local_heap,LOCAL_HEAP_SIZE);
Eterm *hp, data, *orig_hp = NULL;
Binary *bptr = NULL;
va_list args;
UseTmpHeapNoproc(LOCAL_HEAP_SIZE);
hp = local_heap;
if (what == am_close) {
data = what;
} else {
Eterm arg;
va_start(args, what);
if (what == am_command) {
char *bin = va_arg(args, char *);
Sint sz = va_arg(args, Sint);
va_end(args);
arg = trace_port_tmp_binary(bin, sz, &bptr, &hp);
} else if (what == am_call || what == am_control) {
unsigned int command = va_arg(args, unsigned int);
char *bin = va_arg(args, char *);
Sint sz = va_arg(args, Sint);
Eterm cmd;
va_end(args);
arg = trace_port_tmp_binary(bin, sz, &bptr, &hp);
#if defined(ARCH_32)
if (!IS_USMALL(0, command)) {
*hp = make_pos_bignum_header(1);
BIG_DIGIT(hp, 0) = (Uint)command;
cmd = make_big(hp);
hp += 2;
} else
#endif
{
cmd = make_small((Sint)command);
}
arg = TUPLE2(hp, cmd, arg);
hp += 3;
} else if (what == am_commandv) {
ErlIOVec *evp = va_arg(args, ErlIOVec*);
int i;
va_end(args);
if ((6 + evp->vsize * (2+PROC_BIN_SIZE+ERL_SUB_BIN_SIZE)) > LOCAL_HEAP_SIZE) {
hp = erts_alloc(ERTS_ALC_T_TMP,
(6 + evp->vsize * (2+PROC_BIN_SIZE+ERL_SUB_BIN_SIZE)) * sizeof(Eterm));
orig_hp = hp;
}
arg = NIL;
/* Convert each element in the ErlIOVec to a sub bin that points
to a procbin. We don't have to increment the proc bin refc as
the port task keeps the reference alive. */
for (i = evp->vsize-1; i >= 0; i--) {
if (evp->iov[i].iov_len) {
ProcBin* pb = (ProcBin*)hp;
ErlSubBin *sb;
ASSERT(evp->binv[i]);
pb->thing_word = HEADER_PROC_BIN;
pb->val = ErlDrvBinary2Binary(evp->binv[i]);
pb->size = pb->val->orig_size;
pb->next = NULL;
pb->bytes = (byte*) pb->val->orig_bytes;
pb->flags = 0;
hp += PROC_BIN_SIZE;
sb = (ErlSubBin*) hp;
sb->thing_word = HEADER_SUB_BIN;
sb->size = evp->iov[i].iov_len;
sb->offs = (byte*)(evp->iov[i].iov_base) - pb->bytes;
sb->orig = make_binary(pb);
sb->bitoffs = 0;
sb->bitsize = 0;
sb->is_writable = 0;
hp += ERL_SUB_BIN_SIZE;
arg = CONS(hp, make_binary(sb), arg);
hp += 2;
}
}
what = am_command;
} else {
arg = va_arg(args, Eterm);
va_end(args);
}
data = TUPLE2(hp, what, arg);
hp += 3;
}
data = TUPLE2(hp, caller, data);
send_to_tracer_nif(NULL, &t_p->common, t_p->common.id, tnif,
am_receive, data, THE_NON_VALUE);
if (bptr && erts_refc_dectest(&bptr->refc, 1) == 0)
erts_bin_free(bptr);
if (orig_hp)
erts_free(ERTS_ALC_T_TMP, orig_hp);
UnUseTmpHeapNoproc(LOCAL_HEAP_SIZE);
}
#undef LOCAL_HEAP_SIZE
}
void
trace_port_send(Port *t_p, Eterm receiver, Eterm msg, int exists)
{
ErtsTracerNif *tnif = NULL;
Eterm op = exists ? am_send : am_send_to_non_existing_process;
ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked(t_p)
|| erts_thr_progress_is_blocking());
ERTS_SMP_CHK_NO_PROC_LOCKS;
if (is_tracer_proc_enabled(NULL, 0, &t_p->common, &tnif, op))
send_to_tracer_nif(NULL, &t_p->common, t_p->common.id, tnif,
op, msg, receiver);
}
void trace_port_send_binary(Port *t_p, Eterm to, Eterm what, char *bin, Sint sz)
{
ErtsTracerNif *tnif = NULL;
ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked(t_p)
|| erts_thr_progress_is_blocking());
ERTS_SMP_CHK_NO_PROC_LOCKS;
if (is_tracer_proc_enabled(NULL, 0, &t_p->common, &tnif, am_send)) {
Eterm msg;
Binary* bptr = NULL;
#define LOCAL_HEAP_SIZE (3 + 3 + heap_bin_size(ERL_ONHEAP_BIN_LIMIT))
DeclareTmpHeapNoproc(local_heap,LOCAL_HEAP_SIZE);
Eterm *hp;
ERTS_CT_ASSERT(heap_bin_size(ERL_ONHEAP_BIN_LIMIT) >= PROC_BIN_SIZE);
UseTmpHeapNoproc(LOCAL_HEAP_SIZE);
hp = local_heap;
msg = trace_port_tmp_binary(bin, sz, &bptr, &hp);
msg = TUPLE2(hp, what, msg);
hp += 3;
msg = TUPLE2(hp, t_p->common.id, msg);
hp += 3;
send_to_tracer_nif(NULL, &t_p->common, t_p->common.id, tnif,
am_send, msg, to);
if (bptr && erts_refc_dectest(&bptr->refc, 1) == 0)
erts_bin_free(bptr);
UnUseTmpHeapNoproc(LOCAL_HEAP_SIZE);
#undef LOCAL_HEAP_SIZE
}
}
/* Send {trace_ts, Pid, What, {Mod, Func, Arity}, Timestamp}
* or {trace, Pid, What, {Mod, Func, Arity}}
*
* where 'What' is supposed to be 'in' or 'out' and
* where 'where' is supposed to be location (callback)
* for the port.
*/
void
trace_sched_ports(Port *p, Eterm what) {
trace_sched_ports_where(p, what, make_small(0));
}
void
trace_sched_ports_where(Port *t_p, Eterm what, Eterm where) {
ErtsTracerNif *tnif = NULL;
ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked(t_p)
|| erts_thr_progress_is_blocking());
ERTS_SMP_CHK_NO_PROC_LOCKS;
if (is_tracer_proc_enabled(NULL, 0, &t_p->common, &tnif, what))
send_to_tracer_nif(NULL, &t_p->common, t_p->common.id,
tnif, what, where, THE_NON_VALUE);
}
/* Port profiling */
void
profile_runnable_port(Port *p, Eterm status) {
Eterm *hp, msg;
ErlHeapFragment *bp = NULL;
Eterm count = make_small(0);
#ifndef ERTS_SMP
#define LOCAL_HEAP_SIZE (6 + ERTS_TRACE_PATCH_TS_MAX_SIZE)
DeclareTmpHeapNoproc(local_heap,LOCAL_HEAP_SIZE);
UseTmpHeapNoproc(LOCAL_HEAP_SIZE);
hp = local_heap;
#else
Uint hsz;
hsz = 6 + patch_ts_size(erts_system_profile_ts_type)-1;
bp = new_message_buffer(hsz);
hp = bp->mem;
#endif
erts_smp_mtx_lock(&smq_mtx);
msg = TUPLE5(hp, am_profile, p->common.id, status, count,
NIL /* Will be overwritten by timestamp */);
hp += 6;
/* Write timestamp in element 5 of the 'msg' tuple */
hp[-1] = write_ts(erts_system_profile_ts_type, hp, bp, NULL);
#ifndef ERTS_SMP
profile_send(p->common.id, msg);
UnUseTmpHeapNoproc(LOCAL_HEAP_SIZE);
#undef LOCAL_HEAP_SIZE
#else
enqueue_sys_msg_unlocked(SYS_MSG_TYPE_SYSPROF, p->common.id, NIL, msg, bp);
#endif
erts_smp_mtx_unlock(&smq_mtx);
}
/* Process profiling */
void
profile_runnable_proc(Process *p, Eterm status){
Eterm *hp, msg;
Eterm where = am_undefined;
ErlHeapFragment *bp = NULL;
int use_current = 1;
#ifndef ERTS_SMP
#define LOCAL_HEAP_SIZE (4 + 6 + ERTS_TRACE_PATCH_TS_MAX_SIZE)
DeclareTmpHeapNoproc(local_heap,LOCAL_HEAP_SIZE);
UseTmpHeapNoproc(LOCAL_HEAP_SIZE);
hp = local_heap;
#else
Uint hsz = 4 + 6 + patch_ts_size(erts_system_profile_ts_type)-1;
#endif
if (ERTS_PROC_IS_EXITING(p)) {
use_current = 0;
/* could probably set 'where' to 'exiting' here,
* though it's not documented as such */
} else {
if (!p->current) {
p->current = find_function_from_pc(p->i);
}
use_current = p->current != NULL;
}
#ifdef ERTS_SMP
if (!use_current) {
hsz -= 4;
}
bp = new_message_buffer(hsz);
hp = bp->mem;
#endif
if (use_current) {
where = TUPLE3(hp, p->current[0], p->current[1], make_small(p->current[2])); hp += 4;
} else {
where = make_small(0);
}
erts_smp_mtx_lock(&smq_mtx);
msg = TUPLE5(hp, am_profile, p->common.id, status, where,
NIL /* Will be overwritten by timestamp */);
hp += 6;
/* Write timestamp in element 5 of the 'msg' tuple */
hp[-1] = write_ts(erts_system_profile_ts_type, hp, bp, NULL);
#ifndef ERTS_SMP
profile_send(p->common.id, msg);
UnUseTmpHeapNoproc(LOCAL_HEAP_SIZE);
#undef LOCAL_HEAP_SIZE
#else
enqueue_sys_msg_unlocked(SYS_MSG_TYPE_SYSPROF, p->common.id, NIL, msg, bp);
#endif
erts_smp_mtx_unlock(&smq_mtx);
}
/* End system_profile tracing */
#ifdef ERTS_SMP
typedef struct ErtsSysMsgQ_ ErtsSysMsgQ;
struct ErtsSysMsgQ_ {
ErtsSysMsgQ *next;
enum ErtsSysMsgType type;
Eterm from;
Eterm to;
Eterm msg;
ErlHeapFragment *bp;
};
static ErtsSysMsgQ *sys_message_queue;
static ErtsSysMsgQ *sys_message_queue_end;
static erts_tid_t sys_msg_dispatcher_tid;
static erts_cnd_t smq_cnd;
ERTS_QUALLOC_IMPL(smq_element, ErtsSysMsgQ, 20, ERTS_ALC_T_SYS_MSG_Q)
static void
enqueue_sys_msg_unlocked(enum ErtsSysMsgType type,
Eterm from,
Eterm to,
Eterm msg,
ErlHeapFragment *bp)
{
ErtsSysMsgQ *smqp;
smqp = smq_element_alloc();
smqp->next = NULL;
smqp->type = type;
smqp->from = from;
smqp->to = to;
smqp->msg = msg;
smqp->bp = bp;
if (sys_message_queue_end) {
ASSERT(sys_message_queue);
sys_message_queue_end->next = smqp;
}
else {
ASSERT(!sys_message_queue);
sys_message_queue = smqp;
}
sys_message_queue_end = smqp;
erts_smp_cnd_signal(&smq_cnd);
}
static void
enqueue_sys_msg(enum ErtsSysMsgType type,
Eterm from,
Eterm to,
Eterm msg,
ErlHeapFragment *bp)
{
erts_smp_mtx_lock(&smq_mtx);
enqueue_sys_msg_unlocked(type, from, to, msg, bp);
erts_smp_mtx_unlock(&smq_mtx);
}
void
erts_queue_error_logger_message(Eterm from, Eterm msg, ErlHeapFragment *bp)
{
enqueue_sys_msg(SYS_MSG_TYPE_ERRLGR, from, am_error_logger, msg, bp);
}
#ifdef DEBUG_PRINTOUTS
static void
print_msg_type(ErtsSysMsgQ *smqp)
{
switch (smqp->type) {
case SYS_MSG_TYPE_SYSMON:
erts_fprintf(stderr, "SYSMON ");
break;
case SYS_MSG_TYPE_SYSPROF:
erts_fprintf(stderr, "SYSPROF ");
break;
case SYS_MSG_TYPE_ERRLGR:
erts_fprintf(stderr, "ERRLGR ");
break;
default:
erts_fprintf(stderr, "??? ");
break;
}
}
#endif
static void
sys_msg_disp_failure(ErtsSysMsgQ *smqp, Eterm receiver)
{
switch (smqp->type) {
case SYS_MSG_TYPE_SYSMON:
if (receiver == NIL
&& !erts_system_monitor_long_gc
&& !erts_system_monitor_long_schedule
&& !erts_system_monitor_large_heap
&& !erts_system_monitor_flags.busy_port
&& !erts_system_monitor_flags.busy_dist_port)
break; /* Everything is disabled */
erts_smp_thr_progress_block();
if (system_monitor == receiver || receiver == NIL)
erts_system_monitor_clear(NULL);
erts_smp_thr_progress_unblock();
break;
case SYS_MSG_TYPE_SYSPROF:
if (receiver == NIL
&& !erts_system_profile_flags.runnable_procs
&& !erts_system_profile_flags.runnable_ports
&& !erts_system_profile_flags.exclusive
&& !erts_system_profile_flags.scheduler)
break;
/* Block system to clear flags */
erts_smp_thr_progress_block();
if (system_profile == receiver || receiver == NIL) {
erts_system_profile_clear(NULL);
}
erts_smp_thr_progress_unblock();
break;
case SYS_MSG_TYPE_ERRLGR: {
char *no_elgger = "(no error logger present)";
Eterm *tp;
Eterm tag;
if (is_not_tuple(smqp->msg)) {
unexpected_elmsg:
erts_fprintf(stderr,
"%s unexpected error logger message: %T\n",
no_elgger,
smqp->msg);
}
tp = tuple_val(smqp->msg);
if (arityval(tp[0]) != 2)
goto unexpected_elmsg;
if (is_not_tuple(tp[2]))
goto unexpected_elmsg;
tp = tuple_val(tp[2]);
if (arityval(tp[0]) != 3)
goto unexpected_elmsg;
tag = tp[1];
if (is_not_tuple(tp[3]))
goto unexpected_elmsg;
tp = tuple_val(tp[3]);
if (arityval(tp[0]) != 3)
goto unexpected_elmsg;
if (is_not_list(tp[3]))
goto unexpected_elmsg;
erts_fprintf(stderr, "%s %T: %T\n",
no_elgger, tag, CAR(list_val(tp[3])));
break;
}
default:
ASSERT(0);
}
}
static void
sys_msg_dispatcher_wakeup(void *vwait_p)
{
int *wait_p = (int *) vwait_p;
erts_smp_mtx_lock(&smq_mtx);
*wait_p = 0;
erts_smp_cnd_signal(&smq_cnd);
erts_smp_mtx_unlock(&smq_mtx);
}
static void
sys_msg_dispatcher_prep_wait(void *vwait_p)
{
int *wait_p = (int *) vwait_p;
erts_smp_mtx_lock(&smq_mtx);
*wait_p = 1;
erts_smp_mtx_unlock(&smq_mtx);
}
static void
sys_msg_dispatcher_fin_wait(void *vwait_p)
{
int *wait_p = (int *) vwait_p;
erts_smp_mtx_lock(&smq_mtx);
*wait_p = 0;
erts_smp_mtx_unlock(&smq_mtx);
}
static void
sys_msg_dispatcher_wait(void *vwait_p)
{
int *wait_p = (int *) vwait_p;
erts_smp_mtx_lock(&smq_mtx);
while (*wait_p)
erts_smp_cnd_wait(&smq_cnd, &smq_mtx);
erts_smp_mtx_unlock(&smq_mtx);
}
static void *
sys_msg_dispatcher_func(void *unused)
{
ErtsThrPrgrCallbacks callbacks;
ErtsSysMsgQ *local_sys_message_queue = NULL;
int wait = 0;
#ifdef ERTS_ENABLE_LOCK_CHECK
erts_lc_set_thread_name("system message dispatcher");
#endif
callbacks.arg = (void *) &wait;
callbacks.wakeup = sys_msg_dispatcher_wakeup;
callbacks.prepare_wait = sys_msg_dispatcher_prep_wait;
callbacks.wait = sys_msg_dispatcher_wait;
callbacks.finalize_wait = sys_msg_dispatcher_fin_wait;
erts_thr_progress_register_managed_thread(NULL, &callbacks, 0);
while (1) {
int end_wait = 0;
ErtsSysMsgQ *smqp;
ERTS_SMP_LC_ASSERT(!erts_thr_progress_is_blocking());
erts_smp_mtx_lock(&smq_mtx);
/* Free previously used queue ... */
while (local_sys_message_queue) {
smqp = local_sys_message_queue;
local_sys_message_queue = smqp->next;
smq_element_free(smqp);
}
/* Fetch current trace message queue ... */
if (!sys_message_queue) {
erts_smp_mtx_unlock(&smq_mtx);
end_wait = 1;
erts_thr_progress_active(NULL, 0);
erts_thr_progress_prepare_wait(NULL);
erts_smp_mtx_lock(&smq_mtx);
}
while (!sys_message_queue)
erts_smp_cnd_wait(&smq_cnd, &smq_mtx);
local_sys_message_queue = sys_message_queue;
sys_message_queue = NULL;
sys_message_queue_end = NULL;
erts_smp_mtx_unlock(&smq_mtx);
if (end_wait) {
erts_thr_progress_finalize_wait(NULL);
erts_thr_progress_active(NULL, 1);
}
/* Send trace messages ... */
ASSERT(local_sys_message_queue);
for (smqp = local_sys_message_queue; smqp; smqp = smqp->next) {
Eterm receiver;
ErtsProcLocks proc_locks = ERTS_PROC_LOCKS_MSG_SEND;
Process *proc = NULL;
Port *port = NULL;
if (erts_thr_progress_update(NULL))
erts_thr_progress_leader_update(NULL);
#ifdef DEBUG_PRINTOUTS
print_msg_type(smqp);
#endif
switch (smqp->type) {
case SYS_MSG_TYPE_SYSMON:
receiver = erts_get_system_monitor();
if (smqp->from == receiver) {
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "MSG=%T to %T... ",
smqp->msg, receiver);
#endif
goto drop_sys_msg;
}
break;
case SYS_MSG_TYPE_SYSPROF:
receiver = erts_get_system_profile();
if (smqp->from == receiver) {
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "MSG=%T to %T... ",
smqp->msg, receiver);
#endif
goto drop_sys_msg;
}
break;
case SYS_MSG_TYPE_ERRLGR:
receiver = am_error_logger;
break;
default:
receiver = NIL;
break;
}
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "MSG=%T to %T... ", smqp->msg, receiver);
#endif
if (is_internal_pid(receiver)) {
proc = erts_pid2proc(NULL, 0, receiver, proc_locks);
if (!proc) {
/* Bad tracer */
goto failure;
}
else {
ErtsMessage *mp;
queue_proc_msg:
mp = erts_alloc_message(0, NULL);
mp->data.heap_frag = smqp->bp;
erts_queue_message(proc,&proc_locks,mp,smqp->msg);
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "delivered\n");
#endif
erts_smp_proc_unlock(proc, proc_locks);
}
}
else if (receiver == am_error_logger) {
proc = erts_whereis_process(NULL,0,receiver,proc_locks,0);
if (!proc)
goto failure;
else if (smqp->from == proc->common.id)
goto drop_sys_msg;
else
goto queue_proc_msg;
}
else if (is_internal_port(receiver)) {
port = erts_thr_id2port_sflgs(receiver,
ERTS_PORT_SFLGS_INVALID_TRACER_LOOKUP);
if (!port)
goto failure;
else {
write_sys_msg_to_port(receiver,
port,
smqp->from,
smqp->type,
smqp->msg);
if (port->control_flags & PORT_CONTROL_FLAG_HEAVY)
port->control_flags &= ~PORT_CONTROL_FLAG_HEAVY;
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "delivered\n");
#endif
erts_thr_port_release(port);
if (smqp->bp)
free_message_buffer(smqp->bp);
}
}
else {
failure:
sys_msg_disp_failure(smqp, receiver);
drop_sys_msg:
if (proc)
erts_smp_proc_unlock(proc, proc_locks);
if (smqp->bp)
free_message_buffer(smqp->bp);
#ifdef DEBUG_PRINTOUTS
erts_fprintf(stderr, "dropped\n");
#endif
}
}
}
return NULL;
}
void
erts_foreach_sys_msg_in_q(void (*func)(Eterm,
Eterm,
Eterm,
ErlHeapFragment *))
{
ErtsSysMsgQ *sm;
erts_smp_mtx_lock(&smq_mtx);
for (sm = sys_message_queue; sm; sm = sm->next) {
Eterm to;
switch (sm->type) {
case SYS_MSG_TYPE_SYSMON:
to = erts_get_system_monitor();
break;
case SYS_MSG_TYPE_SYSPROF:
to = erts_get_system_profile();
break;
case SYS_MSG_TYPE_ERRLGR:
to = am_error_logger;
break;
default:
to = NIL;
break;
}
(*func)(sm->from, to, sm->msg, sm->bp);
}
erts_smp_mtx_unlock(&smq_mtx);
}
static void
init_sys_msg_dispatcher(void)
{
erts_smp_thr_opts_t thr_opts = ERTS_SMP_THR_OPTS_DEFAULT_INITER;
thr_opts.detached = 1;
thr_opts.name = "sys_msg_dispatcher";
init_smq_element_alloc();
sys_message_queue = NULL;
sys_message_queue_end = NULL;
erts_smp_cnd_init(&smq_cnd);
erts_smp_mtx_init(&smq_mtx, "sys_msg_q");
erts_smp_thr_create(&sys_msg_dispatcher_tid,
sys_msg_dispatcher_func,
NULL,
&thr_opts);
}
#endif
#include "erl_nif.h"
struct ErtsTracerNif_ {
HashBucket hb;
Eterm module;
struct erl_module_nif* nif_mod;
ErlNifFunc *enabled;
ErlNifFunc *trace;
};
static Hash *tracer_hash = NULL;
static erts_smp_rwmtx_t tracer_mtx;
static ErtsTracerNif *
load_tracer_nif(const ErtsTracer tracer)
{
Module* mod = erts_get_module(ERTS_TRACER_MODULE(tracer),
erts_active_code_ix());
struct erl_module_instance *instance;
ErlNifFunc *funcs;
int num_of_funcs;
ErtsTracerNif tnif_tmpl, *tnif;
int i;
if (mod && mod->curr.nif != NULL) {
instance = &mod->curr;
} else {
return NULL;
}
tnif_tmpl.enabled = NULL;
tnif_tmpl.trace = NULL;
tnif_tmpl.nif_mod = instance->nif;
tnif_tmpl.module = ERTS_TRACER_MODULE(tracer);
num_of_funcs = erts_nif_get_funcs(instance->nif, &funcs);
for(i = 0; i < num_of_funcs; i++) {
if (strcmp("enabled",funcs[i].name) == 0 && funcs[i].arity == 3) {
tnif_tmpl.enabled = funcs + i;
} else if (strcmp("trace",funcs[i].name) == 0 && funcs[i].arity == 6) {
tnif_tmpl.trace = funcs + i;
}
}
if (tnif_tmpl.enabled == NULL ||
tnif_tmpl.trace == NULL ) {
return NULL;
}
erts_smp_rwmtx_rwlock(&tracer_mtx);
tnif = hash_put(tracer_hash, &tnif_tmpl);
erts_smp_rwmtx_rwunlock(&tracer_mtx);
return tnif;
}
static ERTS_INLINE ErtsTracerNif *
lookup_tracer_nif(const ErtsTracer tracer)
{
ErtsTracerNif tnif_tmpl;
ErtsTracerNif *tnif;
tnif_tmpl.module = ERTS_TRACER_MODULE(tracer);
erts_smp_rwmtx_rlock(&tracer_mtx);
if ((tnif = hash_get(tracer_hash, &tnif_tmpl)) == NULL) {
erts_smp_rwmtx_runlock(&tracer_mtx);
tnif = load_tracer_nif(tracer);
ASSERT(!tnif || tnif->nif_mod);
return tnif;
}
erts_smp_rwmtx_runlock(&tracer_mtx);
ASSERT(tnif->nif_mod);
return tnif;
}
/* This function converts an Erlang tracer term to ErtsTracer.
It returns THE_NON_VALUE if an invalid tracer term was given.
Accepted input is:
pid() || port() || {prefix, pid()} || {prefix, port()} ||
{prefix, atom(), term()} || {atom(), term()}
*/
ErtsTracer
erts_term_to_tracer(Eterm prefix, Eterm t)
{
ErtsTracer tracer = erts_tracer_nil;
ASSERT(is_atom(prefix) || prefix == THE_NON_VALUE);
if (!is_nil(t)) {
Eterm module = am_erl_tracer, state = THE_NON_VALUE;
Eterm hp[2];
if (is_tuple(t)) {
Eterm *tp = tuple_val(t);
if (prefix != THE_NON_VALUE) {
if (arityval(tp[0]) == 2 && tp[1] == prefix)
t = tp[2];
else if (arityval(tp[0]) == 3 && tp[1] == prefix && is_atom(tp[2])) {
module = tp[2];
state = tp[3];
}
} else {
if (arityval(tp[0]) == 2 && is_atom(tp[2])) {
module = tp[1];
state = tp[2];
}
}
}
if (state == THE_NON_VALUE && (is_internal_pid(t) || is_internal_port(t)))
state = t;
if (state == THE_NON_VALUE)
return THE_NON_VALUE;
erts_tracer_update(&tracer, CONS(hp, module, state));
}
if (!lookup_tracer_nif(tracer)) {
ASSERT(ERTS_TRACER_MODULE(tracer) != am_erl_tracer);
ERTS_TRACER_CLEAR(&tracer);
return THE_NON_VALUE;
}
return tracer;
}
Eterm
erts_tracer_to_term(Process *p, ErtsTracer tracer)
{
if (ERTS_TRACER_IS_NIL(tracer))
return am_false;
if (ERTS_TRACER_MODULE(tracer) == am_erl_tracer)
/* Have to manage these specifically in order to be
backwards compatible */
return ERTS_TRACER_STATE(tracer);
else {
Eterm *hp = HAlloc(p, 3);
return TUPLE2(hp, ERTS_TRACER_MODULE(tracer),
copy_object(ERTS_TRACER_STATE(tracer), p));
}
}
static ERTS_INLINE int
send_to_tracer_nif_raw(Process *c_p, Process *tracee,
const ErtsTracer tracer, Uint tracee_flags,
Eterm t_p_id, ErtsTracerNif *tnif, Eterm tag, Eterm msg,
Eterm extra, Eterm pam_result)
{
if (tnif || (tnif = lookup_tracer_nif(tracer)) != NULL) {
#define MAP_SIZE 3
Eterm argv[6],
local_heap[3+MAP_SIZE /* values */+(MAP_SIZE+1 /* keys */)];
flatmap_t *map = (flatmap_t*)(local_heap+(MAP_SIZE+1));
Eterm *map_values = flatmap_get_values(map);
int argc = 6;
argv[0] = tag;
argv[1] = ERTS_TRACER_STATE(tracer);
argv[2] = t_p_id;
argv[3] = msg;
argv[4] = extra == THE_NON_VALUE ? am_undefined : extra;
argv[5] = make_flatmap(map);
map->thing_word = MAP_HEADER_FLATMAP;
map->size = MAP_SIZE;
map->keys = TUPLE3(local_heap, am_match_spec_result, am_scheduler_id,
am_timestamp);
*map_values++ = pam_result;
if (tracee_flags & F_TRACE_SCHED_NO)
*map_values++ = make_small(erts_get_scheduler_id());
else
*map_values++ = am_undefined;
if (tracee_flags & F_NOW_TS)
#ifdef HAVE_ERTS_NOW_CPU
if (erts_cpu_timestamp)
*map_values++ = am_cpu_timestamp;
else
#endif
*map_values++ = am_timestamp;
else if (tracee_flags & F_STRICT_MON_TS)
*map_values++ = am_strict_monotonic;
else if (tracee_flags & F_MON_TS)
*map_values++ = am_monotonic;
else
*map_values++ = am_undefined;
#undef MAP_SIZE
erts_nif_call_function(c_p, tracee ? tracee : c_p,
tnif->nif_mod, tnif->trace, argc, argv);
}
return 1;
}
static ERTS_INLINE int
send_to_tracer_nif(Process *c_p, ErtsPTabElementCommon *t_p,
Eterm t_p_id, ErtsTracerNif *tnif, Eterm tag,
Eterm msg, Eterm extra)
{
#if defined(ERTS_SMP) && defined(ERTS_ENABLE_LOCK_CHECK)
if (c_p) {
/* We have to hold the main lock of the currently executing process */
erts_proc_lc_chk_have_proc_locks(c_p, ERTS_PROC_LOCK_MAIN);
}
if (is_internal_pid(t_p->id)) {
/* We have to have at least one lock */
ERTS_SMP_LC_ASSERT(erts_proc_lc_my_proc_locks((Process*)t_p) & ERTS_PROC_LOCKS_ALL);
} else {
ASSERT(is_internal_port(t_p->id));
ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked((Port*)t_p));
}
#endif
return send_to_tracer_nif_raw(c_p,
is_internal_pid(t_p->id) ? (Process*)t_p : NULL,
t_p->tracer, t_p->trace_flags,
t_p_id, tnif, tag, msg, extra,
am_true);
}
static ERTS_INLINE Eterm
call_enabled_tracer(Process *c_p, const ErtsTracer tracer,
ErtsTracerNif **tnif_ret, Eterm tag, Eterm t_p_id)
{
ErtsTracerNif *tnif = lookup_tracer_nif(tracer);
if (tnif) {
Eterm argv[] = {tag, ERTS_TRACER_STATE(tracer), t_p_id};
if (tnif_ret) *tnif_ret = tnif;
return erts_nif_call_function(
c_p, NULL, tnif->nif_mod, tnif->enabled, 3, argv);
}
return am_remove;
}
static int
is_tracer_proc_enabled(Process* c_p, ErtsProcLocks c_p_locks,
ErtsPTabElementCommon *t_p,
ErtsTracerNif **tnif_ret, Eterm tag)
{
Eterm nif_result;
#if defined(ERTS_SMP) && defined(ERTS_ENABLE_LOCK_CHECK)
if (c_p)
ERTS_SMP_LC_ASSERT(erts_proc_lc_my_proc_locks(c_p) == c_p_locks
|| erts_thr_progress_is_blocking());
if (is_internal_pid(t_p->id)) {
/* We have to have at least one lock */
ERTS_SMP_LC_ASSERT(erts_proc_lc_my_proc_locks((Process*)t_p) & ERTS_PROC_LOCKS_ALL
|| erts_thr_progress_is_blocking());
} else {
ASSERT(is_internal_port(t_p->id));
ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked((Port*)t_p)
|| erts_thr_progress_is_blocking());
}
#endif
nif_result = call_enabled_tracer(c_p, t_p->tracer, tnif_ret, tag, t_p->id);
switch (nif_result) {
case am_discard: return 0;
case am_trace: return 1;
case THE_NON_VALUE:
case am_remove: break;
default:
/* only am_remove should be returned, but if
something else is returned we fall-through
and remove the tracer. */
ASSERT(0);
}
/* Only remove tracer on self() and ports */
if (is_internal_port(t_p->id) || (c_p && c_p->common.id == t_p->id)) {
ErtsProcLocks c_p_xlocks = 0;
if (is_internal_pid(t_p->id)) {
ERTS_SMP_LC_ASSERT(erts_proc_lc_my_proc_locks(c_p) & ERTS_PROC_LOCK_MAIN);
if (c_p_locks != ERTS_PROC_LOCKS_ALL) {
c_p_xlocks = ~c_p_locks & ERTS_PROC_LOCKS_ALL;
if (erts_smp_proc_trylock(c_p, c_p_xlocks) == EBUSY) {
erts_smp_proc_unlock(c_p, c_p_locks & ~ERTS_PROC_LOCK_MAIN);
erts_smp_proc_lock(c_p, ERTS_PROC_LOCKS_ALL_MINOR);
}
}
}
erts_tracer_replace(t_p, erts_tracer_nil);
t_p->trace_flags &= ~TRACEE_FLAGS;
if (c_p_xlocks)
erts_smp_proc_unlock(c_p, c_p_xlocks);
}
return 0;
}
int erts_is_tracer_proc_enabled(Process* c_p, ErtsProcLocks c_p_locks,
ErtsPTabElementCommon *t_p, Eterm type)
{
return is_tracer_proc_enabled(c_p, c_p_locks, t_p, NULL, am_trace_status);
}
int erts_is_tracer_enabled(Process *c_p, const ErtsTracer tracer)
{
ErtsTracerNif *tnif = lookup_tracer_nif(tracer);
if (tnif) {
Eterm nif_result = call_enabled_tracer(c_p, tracer, &tnif,
am_trace_status,
c_p->common.id);
switch (nif_result) {
case am_discard:
case am_trace: return 1;
default:
break;
}
}
return 0;
}
void erts_tracer_replace(ErtsPTabElementCommon *t_p, const ErtsTracer tracer)
{
#if defined(ERTS_SMP) && defined(ERTS_ENABLE_LOCK_CHECK)
if (is_internal_pid(t_p->id) && !erts_thr_progress_is_blocking()) {
erts_proc_lc_chk_have_proc_locks((Process*)t_p, ERTS_PROC_LOCKS_ALL);
} else if (is_internal_port(t_p->id)) {
ERTS_LC_ASSERT(erts_lc_is_port_locked((Port*)t_p)
|| erts_thr_progress_is_blocking());
}
#endif
if (ERTS_TRACER_COMPARE(t_p->tracer, tracer))
return;
erts_tracer_update(&t_p->tracer, tracer);
}
static void free_tracer(void *p)
{
ErtsTracer tracer = (ErtsTracer)p;
if (is_immed(ERTS_TRACER_STATE(tracer))) {
erts_free(ERTS_ALC_T_HEAP_FRAG, ptr_val(tracer));
} else {
ErlHeapFragment *hf = (void*)((char*)(ptr_val(tracer)) - offsetof(ErlHeapFragment, mem));
free_message_buffer(hf);
}
}
/* un-define erts_tracer_update before implementation */
#ifdef erts_tracer_update
#undef erts_tracer_update
#endif
/*
* ErtsTracer is either NIL, 'true' or [Mod | State]
*
* - If State is immediate then the memory for
* the cons cell is just two words + sizeof(ErtsThrPrgrLaterOp) large.
* - If State is a complex term then the cons cell
* is allocated in an ErlHeapFragment where the cons
* ptr points to the mem field. So in order to get the
* ptr to the fragment you do this:
* (char*)(ptr_val(tracer)) - offsetof(ErlHeapFragment, mem)
* Normally you shouldn't have to care about this though
* as erts_tracer_update takes care of it for you.
*
* When ErtsTracer is stored in the stack as part of a
* return trace, the cons cell is stored on the heap of
* the process.
*
* The cons cell is not always stored on the heap as:
* 1) for port/meta tracing there is no heap
* 2) we would need the main lock in order to
* read the tracer which is undesirable.
*
* One way to optimize this (memory wise) is to keep an refc and only bump
* the refc when *tracer is NIL.
*/
void
erts_tracer_update(ErtsTracer *tracer, const ErtsTracer new_tracer)
{
ErlHeapFragment *hf;
if (is_not_nil(*tracer)) {
Uint offs = 2;
UWord size = 2 * sizeof(Eterm) + sizeof(ErtsThrPrgrLaterOp);
ASSERT(is_list(*tracer));
if (is_not_immed(ERTS_TRACER_STATE(*tracer))) {
hf = (void*)(((char*)(ptr_val(*tracer)) - offsetof(ErlHeapFragment, mem)));
offs = hf->used_size;
size = hf->alloc_size * sizeof(Eterm) + sizeof(ErlHeapFragment);
ASSERT(offs == size_object(*tracer));
}
/* We schedule the free:ing of the tracer until after a thread progress
has been made so that we know that no schedulers have any references
to it. Because we do this, it is possible to release all locks of a
process/port and still use the ErtsTracer of that port/process
without having to worry if it is free'd.
*/
erts_schedule_thr_prgr_later_cleanup_op(
free_tracer, (void*)(*tracer),
(ErtsThrPrgrLaterOp*)(ptr_val(*tracer) + offs),
size);
}
if (is_nil(new_tracer)) {
*tracer = new_tracer;
} else if (is_immed(ERTS_TRACER_STATE(new_tracer))) {
/* If tracer state is an immediate we only allocate a 2 Eterm heap.
Not sure if it is worth it, we save 4 words (sizeof(ErlHeapFragment))
per tracer. */
Eterm *hp = erts_alloc(ERTS_ALC_T_HEAP_FRAG,
2*sizeof(Eterm) + sizeof(ErtsThrPrgrLaterOp));
*tracer = CONS(hp, ERTS_TRACER_MODULE(new_tracer),
ERTS_TRACER_STATE(new_tracer));
} else {
Eterm *hp, tracer_state = ERTS_TRACER_STATE(new_tracer),
tracer_module = ERTS_TRACER_MODULE(new_tracer);
Uint sz = size_object(tracer_state);
hf = new_message_buffer(sz + 2 /* cons cell */ + (sizeof(ErtsThrPrgrLaterOp)+sizeof(Eterm)-1)/sizeof(Eterm));
hp = hf->mem + 2;
hf->used_size -= (sizeof(ErtsThrPrgrLaterOp)+sizeof(Eterm)-1)/sizeof(Eterm);
*tracer = copy_struct(tracer_state, sz, &hp, &hf->off_heap);
*tracer = CONS(hf->mem, tracer_module, *tracer);
ASSERT((void*)(((char*)(ptr_val(*tracer)) - offsetof(ErlHeapFragment, mem))) == hf);
}
}
static void init_tracer_nif()
{
erts_smp_rwmtx_opt_t rwmtx_opt = ERTS_SMP_RWMTX_OPT_DEFAULT_INITER;
rwmtx_opt.type = ERTS_SMP_RWMTX_TYPE_EXTREMELY_FREQUENT_READ;
rwmtx_opt.lived = ERTS_SMP_RWMTX_LONG_LIVED;
erts_smp_rwmtx_init_opt(&tracer_mtx, &rwmtx_opt, "tracer_mtx");
erts_tracer_nif_clear();
}
int erts_tracer_nif_clear()
{
erts_smp_rwmtx_rlock(&tracer_mtx);
if (!tracer_hash || tracer_hash->nobjs) {
HashFunctions hf;
hf.hash = tracer_hash_fun;
hf.cmp = tracer_cmp_fun;
hf.alloc = tracer_alloc_fun;
hf.free = tracer_free_fun;
hf.meta_alloc = (HMALLOC_FUN) erts_alloc;
hf.meta_free = (HMFREE_FUN) erts_free;
hf.meta_print = (HMPRINT_FUN) erts_print;
erts_smp_rwmtx_runlock(&tracer_mtx);
erts_smp_rwmtx_rwlock(&tracer_mtx);
if (tracer_hash)
hash_delete(tracer_hash);
tracer_hash = hash_new(ERTS_ALC_T_TRACER_NIF, "tracer_hash", 10, hf);
erts_smp_rwmtx_rwunlock(&tracer_mtx);
return 1;
}
erts_smp_rwmtx_runlock(&tracer_mtx);
return 0;
}
static int tracer_cmp_fun(void* a, void* b)
{
return ((ErtsTracerNif*)a)->module != ((ErtsTracerNif*)b)->module;
}
static HashValue tracer_hash_fun(void* obj)
{
return make_internal_hash(((ErtsTracerNif*)obj)->module);
}
static void *tracer_alloc_fun(void* tmpl)
{
ErtsTracerNif *obj = erts_alloc(ERTS_ALC_T_TRACER_NIF,
sizeof(ErtsTracerNif) +
sizeof(ErtsThrPrgrLaterOp));
memcpy(obj, tmpl, sizeof(*obj));
return obj;
}
static void tracer_free_fun_cb(void* obj)
{
erts_free(ERTS_ALC_T_TRACER_NIF, obj);
}
static void tracer_free_fun(void* obj)
{
ErtsTracerNif *tnif = obj;
erts_schedule_thr_prgr_later_op(
tracer_free_fun_cb, obj,
(ErtsThrPrgrLaterOp*)(tnif + 1));
}
|