aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test/process_SUITE.erl
blob: 89603b45f4b6d42e3a60a0363bed0b060b19ff22 (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%

-module(process_SUITE).

%% Tests processes, trapping exit messages and the BIFs:
%% 	exit/1
%%	exit/2
%%	process_info/1,2
%%	register/2 (partially)

-include("test_server.hrl").

-define(heap_binary_size, 64).

-export([all/1, spawn_with_binaries/1,
	 t_exit_1/1, t_exit_2/1, t_exit_2_other/1, t_exit_2_other_normal/1,
	 self_exit/1, normal_suicide_exit/1, abnormal_suicide_exit/1,
	 t_exit_2_catch/1, trap_exit_badarg/1, trap_exit_badarg_in_bif/1,
	 exit_and_timeout/1, exit_twice/1,
	 t_process_info/1, process_info_other_msg/1,
	 process_info_other_dist_msg/1,
	 process_info_2_list/1, process_info_lock_reschedule/1,
	 process_info_lock_reschedule2/1,
	 bump_reductions/1, low_prio/1, binary_owner/1, yield/1, yield2/1,
	 process_status_exiting/1,
	 otp_4725/1, bad_register/1, garbage_collect/1, otp_6237/1,
	 process_info_messages/1, process_flag_badarg/1, process_flag_heap_size/1,
	 spawn_opt_heap_size/1,
	 processes_large_tab/1, processes_default_tab/1, processes_small_tab/1,
	 processes_this_tab/1, processes_apply_trap/1,
	 processes_last_call_trap/1, processes_gc_trap/1,
	 processes_term_proc_list/1, processes_bif/1,
	 otp_7738/1, otp_7738_waiting/1, otp_7738_suspended/1,
	 otp_7738_resume/1]).
-export([prio_server/2, prio_client/2]).

-export([init_per_testcase/2, fin_per_testcase/2, end_per_suite/1]).

-export([hangaround/2, processes_bif_test/0, do_processes/1,
	 processes_term_proc_list_test/1]).

all(suite) ->
    [spawn_with_binaries, t_exit_1, t_exit_2,
     trap_exit_badarg, trap_exit_badarg_in_bif,
     t_process_info, process_info_other_msg, process_info_other_dist_msg,
     process_info_2_list,
     process_info_lock_reschedule, process_info_lock_reschedule2,
     process_status_exiting,
     bump_reductions, low_prio, yield, yield2, otp_4725, bad_register,
     garbage_collect, process_info_messages, process_flag_badarg, process_flag_heap_size,
     spawn_opt_heap_size, otp_6237, processes_bif, otp_7738].

init_per_testcase(Func, Config) when is_atom(Func), is_list(Config) ->
    Dog=?t:timetrap(?t:minutes(10)),
    [{watchdog, Dog},{testcase, Func}|Config].

fin_per_testcase(Func, Config) when is_atom(Func), is_list(Config) ->
    Dog=?config(watchdog, Config),
    ?t:timetrap_cancel(Dog).

end_per_suite(Config) ->
    catch erts_debug:set_internal_state(available_internal_state, false),
    Config.

fun_spawn(Fun) ->
    spawn_link(erlang, apply, [Fun, []]).

%% Tests that binaries as arguments to spawn/3 doesn't leak
%% (unclear if this test case will actually prove anything on
%% a modern computer with lots of memory).
spawn_with_binaries(Config) when is_list(Config) ->
    ?line L = lists:duplicate(2048, 42),
    ?line TwoMeg = lists:duplicate(1024, L),
    ?line Fun = fun() -> spawn(?MODULE, binary_owner, [list_to_binary(TwoMeg)]),
			 receive after 1 -> ok end end,
    ?line Iter = case test_server:purify_is_running() of
		     true -> 10;
		     false -> 150
		 end,
    ?line test_server:do_times(Iter, Fun),
    ok.

binary_owner(Bin) when is_binary(Bin) ->
    ok.

%% Tests exit/1 with a big message.
t_exit_1(Config) when is_list(Config) ->
    ?line start_spawner(),
    ?line Dog = test_server:timetrap(test_server:seconds(20)),
    ?line process_flag(trap_exit, true),
    ?line test_server:do_times(10, fun t_exit_1/0),
    ?line test_server:timetrap_cancel(Dog),
    ?line stop_spawner(),
    ok.

t_exit_1() ->
    ?line Pid = fun_spawn(fun() -> exit(kb_128()) end),
    ?line Garbage = kb_128(),
    ?line receive
	      {'EXIT', Pid, Garbage} -> ok
	  end.

t_exit_2(suite) -> [t_exit_2_other, t_exit_2_other_normal, 
		    self_exit, normal_suicide_exit,
		    abnormal_suicide_exit, t_exit_2_catch,
		    exit_and_timeout, exit_twice].

%% Tests exit/2 with a lot of data in the exit message.
t_exit_2_other(Config) when is_list(Config) ->
    ?line start_spawner(),
    ?line Dog = test_server:timetrap(test_server:seconds(20)),
    ?line process_flag(trap_exit, true),
    ?line test_server:do_times(10, fun t_exit_2_other/0),
    ?line test_server:timetrap_cancel(Dog),
    ?line stop_spawner(),
    ok.

t_exit_2_other() ->
    ?line Pid = fun_spawn(fun() -> receive x -> ok end end),
    ?line Garbage = kb_128(),
    ?line exit(Pid, Garbage),
    ?line receive
	      {'EXIT', Pid, Garbage} -> ok
	  end.

%% Tests that exit(Pid, normal) does not kill another process.;
t_exit_2_other_normal(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(20)),
    ?line process_flag(trap_exit, true),
    ?line Pid = fun_spawn(fun() -> receive x -> ok end end),
    ?line exit(Pid, normal),
    ?line receive
	      {'EXIT', Pid, Reason} ->
		  ?line test_server:fail({process_died, Reason})
	  after 1000 ->
		  ok
	  end,
    ?line case process_info(Pid) of
	      undefined ->
		  test_server:fail(process_died_on_normal);
	      List when is_list(List) ->
		  ok
	  end,
    exit(Pid, kill),
    ?line test_server:timetrap_cancel(Dog),
    ok.

%% Tests that we can trap an exit message sent with exit/2 from
%% the same process.
self_exit(Config) when is_list(Config) ->
    ?line start_spawner(),
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line process_flag(trap_exit, true),
    ?line test_server:do_times(200, fun self_exit/0),
    ?line test_server:timetrap_cancel(Dog),
    ?line stop_spawner(),
    ok.

self_exit() ->
    ?line Garbage = eight_kb(),
    ?line P = self(),
    ?line true = exit(P, Garbage),
    ?line receive
	      {'EXIT', P, Garbage} -> ok
	  end.

%% Tests exit(self(), normal) is equivalent to exit(normal) for a process
%% that doesn't trap exits.
normal_suicide_exit(Config) when is_list(Config) ->
    ?line process_flag(trap_exit, true),
    ?line Pid = fun_spawn(fun() -> exit(self(), normal) end),
    ?line receive
	      {'EXIT', Pid, normal} -> ok;
	      Other -> test_server:fail({bad_message, Other})
	  end.

%% Tests exit(self(), Term) is equivalent to exit(Term) for a process
%% that doesn't trap exits.";
abnormal_suicide_exit(Config) when is_list(Config) ->
    ?line Garbage = eight_kb(),
    ?line process_flag(trap_exit, true),
    ?line Pid = fun_spawn(fun() -> exit(self(), Garbage) end),
    ?line receive
	      {'EXIT', Pid, Garbage} -> ok;
	      Other -> test_server:fail({bad_message, Other})
	  end.

%% Tests that exit(self(), die) cannot be catched.
t_exit_2_catch(Config) when is_list(Config) ->
    ?line process_flag(trap_exit, true),
    ?line Pid = fun_spawn(fun() -> catch exit(self(), die) end),
    ?line receive
	      {'EXIT', Pid, normal} ->
		  test_server:fail(catch_worked);
	      {'EXIT', Pid, die} ->
		  ok;
	      Other ->
		  test_server:fail({bad_message, Other})
	  end.

%% Tests trapping of an 'EXIT' message generated by a bad argument to
%% the abs/1 bif.  The 'EXIT' message will intentionally be very big.
trap_exit_badarg(Config) when is_list(Config) ->
    ?line start_spawner(),
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line process_flag(trap_exit, true),
    ?line test_server:do_times(10, fun trap_exit_badarg/0),
    ?line test_server:timetrap_cancel(Dog),
    ?line stop_spawner(),
    ok.

trap_exit_badarg() ->
    ?line Pid = fun_spawn(fun() -> bad_guy(kb_128()) end),
    ?line Garbage = kb_128(),
    ?line receive
	      {'EXIT', Pid, {badarg,[{erlang,abs,[Garbage]},{?MODULE,bad_guy,1}|_]}} ->
		  ok;
	      Other ->
		  ?line ok = io:format("Bad EXIT message: ~P", [Other, 30]),
		  ?line test_server:fail(bad_exit_message)
	  end.

bad_guy(Arg) ->
    ?line abs(Arg).


kb_128() ->
    Eight = eight_kb(),
    {big_binary(),
     Eight, Eight, Eight, Eight, Eight, Eight, Eight, Eight,
     big_binary(),
     Eight, Eight, Eight, Eight, Eight, Eight, Eight, Eight,
     big_binary()}.

eight_kb() ->
    %%% This is really much more than eight kb, so vxworks platforms
    %%% gets away with 1/8 of the other platforms (due to limited
    %%% memory resources). 
    B64 = case os:type() of
	      vxworks ->
		  ?line lists:seq(1, 8);
	      _ ->
		  ?line lists:seq(1, 64)
	  end,
    ?line B512 = {<<1>>,B64,<<2,3>>,B64,make_unaligned_sub_binary(<<4,5,6,7,8,9>>),
		  B64,make_sub_binary([1,2,3,4,5,6]),
		  B64,make_sub_binary(lists:seq(1, ?heap_binary_size+1)),
		  B64,B64,B64,B64,big_binary()},
    ?line lists:duplicate(8, {B512,B512}).

big_binary() ->
    big_binary(10, [42]).
big_binary(0, Acc) ->
    list_to_binary(Acc);
big_binary(N, Acc) ->
    big_binary(N-1, [Acc|Acc]).

%% Test receiving an EXIT message when spawning a BIF with bad arguments.
trap_exit_badarg_in_bif(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(10)),
    ?line process_flag(trap_exit, true),
    ?line test_server:do_times(10, fun trap_exit_badarg_bif/0),
    ?line test_server:timetrap_cancel(Dog),
    ok.
    
trap_exit_badarg_bif() ->
    ?line Pid = spawn_link(erlang, node, [1]),
    ?line receive
	      {'EXIT', Pid, {badarg, _}} ->
		  ok;
	      Other ->
		  ?line test_server:fail({unexpected, Other})
	  end.

%% The following sequences of events have crasched Beam.
%%
%% 1) An exit is sent to a process which is currently not running.
%%    The exit reason will (on purpose) overwrite the message queue
%%    pointer.
%% 2) Before the process is scheduled in, it receives a timeout (from
%%    a 'receive after').
%% 3) The process will crash the next time it executes 'receive'.

exit_and_timeout(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(20)),

    ?line process_flag(trap_exit, true),
    ?line Parent = self(),
    ?line Low = fun_spawn(fun() -> eat_low(Parent) end),
    ?line High = fun_spawn(fun() -> eat_high(Low) end),
    ?line eat_wait_for(Low, High),

    ?line test_server:timetrap_cancel(Dog),
    ok.


eat_wait_for(Low, High) ->
    ?line receive
	      {'EXIT', Low, {you, are, dead}} ->
		  ok;
	      {'EXIT', High, normal} ->
		  eat_wait_for(Low, High);
	      Other ->
		  test_server:fail({bad_message, Other})
	  end.

eat_low(_Parent) ->
    receive
    after 2500 ->
	    ok
    end,
    receive
	Any ->
	    io:format("Received: ~p\n", [Any])
    after 1000 ->
	    ok
    end.
    
eat_high(Low) ->
    process_flag(priority, high),
    receive after 1000 -> ok end,
    exit(Low, {you, are, dead}),
    {_, Sec, _} = now(),
    loop(Sec, Sec).

%% Busy loop for 5 seconds.

loop(OrigSec, CurrentSec) when CurrentSec < OrigSec+5 ->
    {_, NewSec, _} = now(),
    loop(OrigSec, NewSec);
loop(_, _) ->
    ok.


%% Tries to send two different exit messages to a process.
%% (The second one should be ignored.)
exit_twice(Config) when is_list(Config) ->
    ?line Dog = test_server:timetrap(test_server:seconds(20)),

    ?line process_flag(trap_exit, true),
    ?line Low = fun_spawn(fun etwice_low/0),
    ?line High = fun_spawn(fun() -> etwice_high(Low) end),
    ?line etwice_wait_for(Low, High),

    ?line test_server:timetrap_cancel(Dog),
    ok.

etwice_wait_for(Low, High) ->
    ?line receive
	      {'EXIT', Low, first} ->
		  ok;
	      {'EXIT', Low, Other} ->
		  test_server:fail({wrong_exit_reason, Other});
	      {'EXIT', High, normal} ->
		  etwice_wait_for(Low, High);
	      Other ->
		  test_server:fail({bad_message, Other})
	  end.

etwice_low() ->
    etwice_low().

etwice_high(Low) ->
    process_flag(priority, high),
    exit(Low, first),
    exit(Low, second).

%% Tests the process_info/1 BIF.
t_process_info(Config) when is_list(Config) ->
    ?line [] = process_info(self(), registered_name),
    ?line register(my_name, self()),
    ?line {registered_name, my_name} = process_info(self(), registered_name),
    ?line {status, running} = process_info(self(), status),
    ?line {min_heap_size, 233} = process_info(self(), min_heap_size),
    ?line {min_bin_vheap_size, 46368} = process_info(self(), min_bin_vheap_size),
    ?line {current_function, {?MODULE, t_process_info, 1}} =
	process_info(self(), current_function),
    ?line Gleader = group_leader(),
    ?line {group_leader, Gleader} = process_info(self(), group_leader),
    ?line {'EXIT',{badarg,_Info}} = (catch process_info('not_a_pid')),
    ok.

%% Tests the process_info/1 BIF on another process with messages.
process_info_other_msg(Config) when is_list(Config) ->
    Self = self(),
    ?line Pid = spawn_link(fun() -> other_process(Self) end),
    receive
	{go_ahead,Pid} -> ok
    end,

    ?line Own = {my,own,message},

    ?line {messages,[Own]} = process_info(Pid, messages),
    
    ?line Garbage = kb_128(),
    ?line MsgA = {a,Garbage},
    ?line MsgB = {b,Garbage},
    ?line MsgC = {c,Garbage},
    ?line MsgD = {d,Garbage},
    ?line MsgE = {e,Garbage},

    ?line Pid ! MsgA,
    ?line {messages,[Own,MsgA]} = process_info(Pid, messages),
    ?line Pid ! MsgB,
    ?line {messages,[Own,MsgA,MsgB]} = process_info(Pid, messages),
    ?line Pid ! MsgC,
    ?line {messages,[Own,MsgA,MsgB,MsgC]} = process_info(Pid, messages),
    ?line Pid ! MsgD,
    ?line {messages,[Own,MsgA,MsgB,MsgC,MsgD]} = process_info(Pid, messages),
    ?line Pid ! MsgE,
    ?line {messages,[Own,MsgA,MsgB,MsgC,MsgD,MsgE]=All} = process_info(Pid, messages),
    ?line {memory,BytesOther} = process_info(Pid, memory),
    ?line {memory,BytesSelf} = process_info(self(), memory),

    io:format("Memory ~p: ~p\n", [Pid,BytesOther]),
    io:format("Memory ~p (self): ~p\n", [self(),BytesSelf]),

    [Own,MsgA,MsgB,MsgC,MsgD,MsgE] = All,

    ?line Pid ! {self(),empty},
    ?line receive
	      empty -> ok
	  end,
    ?line {messages,[]} = process_info(Pid, messages),

    ?line {min_heap_size, 233} = process_info(Pid, min_heap_size),
    ?line {min_bin_vheap_size, 46368} = process_info(Pid, min_bin_vheap_size),

    ?line Pid ! stop,
    ok.

process_info_other_dist_msg(Config) when is_list(Config) ->
    %%
    %% Check that process_info can handle messages that have not been
    %% decoded yet.
    %%
    ?line {ok, Node} = start_node(Config),
    ?line Self = self(),
    ?line Pid = spawn_link(fun() -> other_process(Self) end),
    ?line receive {go_ahead,Pid} -> ok end,

    ?line Own = {my,own,message},

    ?line {messages,[Own]} = process_info(Pid, messages),
    ?line Garbage = kb_128(),
    ?line MsgA = {a,self(),Garbage},
    ?line MsgB = {b,self(),Garbage},
    ?line MsgC = {c,self(),Garbage},
    ?line MsgD = {d,self(),Garbage},
    ?line MsgE = {e,self(),Garbage},

    %% We don't want the other process to decode messages itself
    %% therefore we suspend it.
    ?line true =  erlang:suspend_process(Pid),
    ?line spawn_link(Node, fun () ->
				   Pid ! MsgA,
				   Pid ! MsgB,
				   Pid ! MsgC,
				   Self ! check_abc
			   end),
    ?line receive check_abc -> ok end,
    ?line [{status,suspended},
	   {messages,[Own,MsgA,MsgB,MsgC]},
	   {status,suspended}]= process_info(Pid, [status,messages,status]),
    ?line spawn_link(Node, fun () ->
				   Pid ! MsgD,
				   Pid ! MsgE,
				   Self ! check_de
			   end),
    ?line receive check_de -> ok end,
    ?line {messages,[Own,MsgA,MsgB,MsgC,MsgD,MsgE]=All}
	= process_info(Pid, messages),
    ?line true = erlang:resume_process(Pid),
    ?line Pid ! {self(), get_all_messages},
    ?line receive
	      {all_messages, AllMsgs} ->
		  ?line All = AllMsgs
	  end,
    ?line {messages,[]} = process_info(Pid, messages),
    ?line Pid ! stop,
    ?line stop_node(Node),
    ?line ok.
    

other_process(Parent) ->
    self() ! {my,own,message},
    Parent ! {go_ahead,self()},
    other_process_1().

other_process_1() ->
    receive
	{Parent,get_all_messages} ->
	    Parent ! {all_messages, get_all_messages()},
	    other_process_1();
	{Parent,empty} ->
	    receive_all(),
	    Parent ! empty,
	    other_process_1();
	stop -> ok
    end.

get_all_messages() ->
    get_all_messages([]).

get_all_messages(Msgs) ->
    receive
	Msg ->
	    get_all_messages([Msg|Msgs])
    after 0 ->
	    lists:reverse(Msgs)
    end.

receive_all() ->
    receive
	_ -> receive_all()
    after 0 -> ok
    end.

chk_pi_order([],[]) ->
    ok;
chk_pi_order([{Arg, _}| Values], [Arg|Args]) ->
    chk_pi_order(Values, Args).

process_info_2_list(doc) ->
    [];
process_info_2_list(suite) ->
    [];
process_info_2_list(Config) when is_list(Config) ->
    ?line Proc = spawn(fun () ->
			       receive after infinity -> ok end end),
    register(process_SUITE_process_info_2_list1, self()),
    register(process_SUITE_process_info_2_list2, Proc),
    ?line erts_debug:set_internal_state(available_internal_state,true),
    ?line AllArgs = erts_debug:get_internal_state(process_info_args),
    ?line A1 = lists:sort(AllArgs) ++ [status] ++ lists:reverse(AllArgs),

    %% Verify that argument is accepted as single atom
    ?line lists:foreach(fun (A) ->
				?line {A, _} = process_info(Proc, A),
				?line {A, _} = process_info(self(), A)
			end,
			A1),

    %% Verify that order is preserved
    ?line ok = chk_pi_order(process_info(self(), A1), A1),
    ?line ok = chk_pi_order(process_info(Proc, A1), A1),

    %% Small arg list
    ?line A2 = [status, stack_size, trap_exit, priority],
    ?line [{status, _}, {stack_size, _}, {trap_exit, _}, {priority, _}]
	= process_info(Proc, A2),
    ?line [{status, _}, {stack_size, _}, {trap_exit, _}, {priority, _}]
	= process_info(self(), A2),

    %% Huge arg list (note values are shared)
    ?line A3 = lists:duplicate(5000,backtrace),
    ?line V3 = process_info(Proc, A3),
    ?line 5000 = length(V3),
    ?line lists:foreach(fun ({backtrace, _}) -> ok end, V3),
    ?line ok.
    
process_info_lock_reschedule(doc) ->
    [];
process_info_lock_reschedule(suite) ->
    [];
process_info_lock_reschedule(Config) when is_list(Config) ->
    %% We need a process that is running and an item that requires
    %% process_info to take the main process lock.
    ?line Target1 = spawn_link(fun tok_loop/0),
    ?line Name1 = process_info_lock_reschedule_running,
    ?line register(Name1, Target1),
    ?line Target2 = spawn_link(fun () -> receive after infinity -> ok end end),
    ?line Name2 = process_info_lock_reschedule_waiting,
    ?line register(Name2, Target2),
    ?line PI = fun(_) ->
		       ?line erlang:yield(),
		       ?line [{registered_name, Name1}]
			   = process_info(Target1, [registered_name]),
		       ?line [{registered_name, Name2}]
			   = process_info(Target2, [registered_name]),
		       ?line erlang:yield(),
		       ?line {registered_name, Name1}
			   = process_info(Target1, registered_name),
		       ?line {registered_name, Name2}
			   = process_info(Target2, registered_name),
		       ?line erlang:yield(),
		       ?line [{registered_name, Name1}| _]
			   = process_info(Target1),
		       ?line [{registered_name, Name2}| _]
			   = process_info(Target2)
	       end,
    ?line lists:foreach(PI, lists:seq(1,1000)),
    %% Make sure Target1 still is willing to "tok loop"
    ?line case process_info(Target1, status) of
	      {status, OkStatus} when OkStatus == runnable;
				      OkStatus == running;
				      OkStatus == garbage_collecting ->
		  ?line unlink(Target1),
		  ?line unlink(Target2),
		  ?line exit(Target1, bang),
		  ?line exit(Target2, bang),
		  ?line OkStatus;
	      {status, BadStatus} ->
		  ?line ?t:fail(BadStatus)
	  end.

pi_loop(_Name, _Pid, 0) ->
    ok;
pi_loop(Name, Pid, N) ->
    {registered_name, Name} = process_info(Pid, registered_name),
    pi_loop(Name, Pid, N-1).

process_info_lock_reschedule2(doc) ->
    [];
process_info_lock_reschedule2(suite) ->
    [];
process_info_lock_reschedule2(Config) when is_list(Config) ->
    ?line Parent = self(),
    ?line Fun = fun () ->
			receive {go, Name, Pid} -> ok end,
			pi_loop(Name, Pid, 10000),
			Parent ! {done, self()},
			receive after infinity -> ok end
		end,
    ?line P1 = spawn_link(Fun),
    ?line N1 = process_info_lock_reschedule2_1,
    ?line true = register(N1, P1),
    ?line P2 = spawn_link(Fun),
    ?line N2 = process_info_lock_reschedule2_2,
    ?line true = register(N2, P2),
    ?line P3 = spawn_link(Fun),
    ?line N3 = process_info_lock_reschedule2_3,
    ?line true = register(N3, P3),
    ?line P4 = spawn_link(Fun),
    ?line N4 = process_info_lock_reschedule2_4,
    ?line true = register(N4, P4),
    ?line P5 = spawn_link(Fun),
    ?line N5 = process_info_lock_reschedule2_5,
    ?line true = register(N5, P5),
    ?line P6 = spawn_link(Fun),
    ?line N6 = process_info_lock_reschedule2_6,
    ?line true = register(N6, P6),
    ?line P1 ! {go, N2, P2},
    ?line P2 ! {go, N1, P1},
    ?line P3 ! {go, N1, P1},
    ?line P4 ! {go, N1, P1},
    ?line P5 ! {go, N6, P6},
    ?line P6 ! {go, N5, P5},
    ?line receive {done, P1} -> ok end,
    ?line receive {done, P2} -> ok end,
    ?line receive {done, P3} -> ok end,
    ?line receive {done, P4} -> ok end,
    ?line receive {done, P5} -> ok end,
    ?line receive {done, P6} -> ok end,
    ?line unlink(P1), exit(P1, bang),
    ?line unlink(P2), exit(P2, bang),
    ?line unlink(P3), exit(P3, bang),
    ?line unlink(P4), exit(P4, bang),
    ?line unlink(P5), exit(P5, bang),
    ?line unlink(P6), exit(P6, bang),
    ?line ok.

process_status_exiting(Config) when is_list(Config) ->
    %% Make sure that erts_debug:get_internal_state({process_status,P})
    %% returns exiting if it is in status P_EXITING.
    ?line erts_debug:set_internal_state(available_internal_state,true),
    ?line Prio = process_flag(priority, max),
    ?line P = spawn_opt(fun () -> receive after infinity -> ok end end,
			[{priority, normal}]),
    ?line erlang:yield(),
    %% The tok_loop processes are here to make it hard for the exiting
    %% process to be scheduled in for exit...
    ?line TokLoops = lists:map(fun (_) ->
				       spawn_opt(fun tok_loop/0,
						 [link,{priority, high}])
			       end,
			       lists:seq(1, erlang:system_info(schedulers_online))),
    ?line exit(P, boom),
    ?line wait_until(
	    fun () ->
		    exiting =:= erts_debug:get_internal_state({process_status,P})
	    end),
    ?line lists:foreach(fun (Tok) -> unlink(Tok), exit(Tok,bang) end, TokLoops),
    ?line process_flag(priority, Prio),
    ?line ok.

otp_4725(Config) when is_list(Config) ->
    ?line Tester = self(),
    ?line Ref1 = make_ref(),
    ?line Pid1 = spawn_opt(fun () ->
				   Tester ! {Ref1, process_info(self())},
				   receive
				       Ref1 -> bye
				   end
			   end,
			   [link,
			    {priority, max},
			    {fullsweep_after, 600}]),
    ?line receive
	      {Ref1, ProcInfo1A} ->
		  ?line ProcInfo1B = process_info(Pid1),
		  ?line Pid1 ! Ref1,
		  ?line check_proc_infos(ProcInfo1A, ProcInfo1B)
	  end,
    ?line Ref2 = make_ref(),
    ?line Pid2 = spawn_opt(fun () ->
				   Tester ! {Ref2, process_info(self())},
				   receive
				       Ref2 -> bye
				   end
			   end,
			   []),
    ?line receive
	      {Ref2, ProcInfo2A} ->
		  ?line ProcInfo2B = process_info(Pid2),
		  ?line Pid2 ! Ref2,
		  ?line check_proc_infos(ProcInfo2A, ProcInfo2B)
	  end,
    ?line ok.

check_proc_infos(A, B) ->
    ?line IC = lists:keysearch(initial_call, 1, A),
    ?line IC = lists:keysearch(initial_call, 1, B),

    ?line L = lists:keysearch(links, 1, A),
    ?line L = lists:keysearch(links, 1, B),

    ?line D = lists:keysearch(dictionary, 1, A),
    ?line D = lists:keysearch(dictionary, 1, B),

    ?line TE = lists:keysearch(trap_exit, 1, A),
    ?line TE = lists:keysearch(trap_exit, 1, B),

    ?line EH = lists:keysearch(error_handler, 1, A),
    ?line EH = lists:keysearch(error_handler, 1, B),

    ?line P = lists:keysearch(priority, 1, A),
    ?line P = lists:keysearch(priority, 1, B),

    ?line GL = lists:keysearch(group_leader, 1, A),
    ?line GL = lists:keysearch(group_leader, 1, B),

    ?line GC = lists:keysearch(garbage_collection, 1, A),
    ?line GC = lists:keysearch(garbage_collection, 1, B),

    ?line ok.


%% Dummies.

start_spawner() ->
    ok.

stop_spawner() ->
    ok.

%% Tests erlang:bump_reductions/1.
bump_reductions(Config) when is_list(Config) ->
    ?line erlang:garbage_collect(),
    ?line receive after 1 -> ok end,		% Clear reductions.
    ?line {reductions,R1} = process_info(self(), reductions),
    ?line true = erlang:bump_reductions(100),
    ?line {reductions,R2} = process_info(self(), reductions),
    ?line case R2-R1 of
	      Diff when Diff < 100 ->
		  ?line ok = io:format("R1 = ~w, R2 = ~w", [R1, R2]),
		  ?line test_server:fail({small_diff, Diff});
	      Diff when Diff > 110 ->
		  ?line ok = io:format("R1 = ~w, R2 = ~w", [R1, R2]),
		  ?line test_server:fail({big_diff, Diff});
	      Diff ->
		  io:format("~p\n", [Diff]),
		  ok
	  end,

    %% Make sure that a bignum reduction doesn't crash the emulator (32-bit CPU).
    bump_big(R2, 16#08000000).

bump_big(Prev, Limit) ->
    ?line true = erlang:bump_reductions(100000), %Limited to CONTEXT_REDUCTIONS.
    ?line case process_info(self(), reductions) of
	      {reductions,Big} when is_integer(Big), Big > Limit ->
		  ?line erlang:garbage_collect(),
		  ?line io:format("~p\n", [Big]);
	      {reductions,R} when is_integer(R), R > Prev ->
		  bump_big(R, Limit)
	  end,
    ok.

%% Priority 'low' should be mixed with 'normal' using a factor of
%% about 8. (OTP-2644)
low_prio(Config) when is_list(Config) ->
    case erlang:system_info(schedulers_online) of
	1 ->
	    ?line ok = low_prio_test(Config);
	_ -> 
	    ?line erlang:system_flag(multi_scheduling, block),
	    ?line ok = low_prio_test(Config),
	    ?line erlang:system_flag(multi_scheduling, unblock),
	    ?line {comment,
		   "Test not written for SMP runtime system. "
		   "Multi scheduling blocked during test."}
    end.

low_prio_test(Config) when is_list(Config) ->
    ?line process_flag(trap_exit, true),
    ?line S = spawn_link(?MODULE, prio_server, [0, 0]),
    ?line PCs = spawn_prio_clients(S, erlang:system_info(schedulers_online)),
    ?line timer:sleep(2000),
    ?line lists:foreach(fun (P) -> exit(P, kill) end, PCs),
    ?line S ! exit,
    ?line receive {'EXIT', S, {A, B}} -> check_prio(A, B) end,
    ok.

check_prio(A, B) ->
    ?line Prop = A/B,
    ?line ok = io:format("Low=~p, High=~p, Prop=~p\n", [A, B, Prop]),

    %% It isn't 1/8, it's more like 0.3, but let's check that
    %% the low-prio processes get some little chance to run at all.
    ?line true = (Prop < 1.0),
    ?line true = (Prop > 1/32).

prio_server(A, B) ->
    receive
	low ->
	    prio_server(A+1, B);
	normal ->
	    prio_server(A, B+1);
	exit ->
	    exit({A, B})
    end.

spawn_prio_clients(_, 0) ->
    [];
spawn_prio_clients(S, N) ->
    [spawn_opt(?MODULE, prio_client, [S, normal], [link, {priority,normal}]),
     spawn_opt(?MODULE, prio_client, [S, low], [link, {priority,low}])
     | spawn_prio_clients(S, N-1)].

prio_client(S, Prio) ->
    S ! Prio,
    prio_client(S, Prio).

make_sub_binary(Bin) when is_binary(Bin) ->
    {_,B} = split_binary(list_to_binary([0,1,3,Bin]), 3),
    B;
make_sub_binary(List) ->
    make_sub_binary(list_to_binary(List)).

make_unaligned_sub_binary(Bin0) ->
    Bin1 = <<0:3,Bin0/binary,31:5>>,
    Sz = size(Bin0),
    <<0:3,Bin:Sz/binary,31:5>> = id(Bin1),
    Bin.

yield(doc) ->
    "Tests erlang:yield/1.";
yield(Config) when is_list(Config) ->
    case catch erlang:system_info(modified_timing_level) of
	Level when is_integer(Level) ->
	    {skipped,
	     "Modified timing (level " ++ integer_to_list(Level)
	     ++ ") is enabled. Testcase gets messed up by modfied "
	     "timing."};
	_ ->
	    MS = erlang:system_flag(multi_scheduling, block),
	    yield_test(),
	    erlang:system_flag(multi_scheduling, unblock),
	    case MS of
		blocked ->
		    {comment,
		     "Multi-scheduling blocked during test. This test-case "
		     "was not written to work with multiple schedulers (the "
		     "yield2 test-case tests almost the same thing)."};
		_ ->
		    ok
	    end
    end.

yield_test() ->
    ?line erlang:garbage_collect(),
    ?line receive after 1 -> ok end,		% Clear reductions.
    ?line SC = schedcnt(start),
    ?line {reductions, R1} = process_info(self(), reductions),
    ?line {ok, true} = call_yield(middle),
    ?line true = call_yield(final),
    ?line true = call_yield(),
    ?line true = apply(erlang, yield, []),
    ?line {reductions, R2} = process_info(self(), reductions),
    ?line Schedcnt = schedcnt(stop, SC),
    ?line case {R2-R1, Schedcnt} of
	      {Diff, 4} when Diff < 30 ->
		  ?line ok = io:format("R1 = ~w, R2 = ~w, Schedcnt = ~w", 
				       [R1, R2, Schedcnt]);
	      {Diff, _} ->
		  ?line ok = io:format("R1 = ~w, R2 = ~w, Schedcnt = ~w", 
				       [R1, R2, Schedcnt]),
		  ?line test_server:fail({measurement_error, Diff, Schedcnt})
	  end.

call_yield() ->
    erlang:yield().

call_yield(middle) ->
    {ok, erlang:yield()};
call_yield(final) ->
    case self() of
	Self when is_pid(Self) ->
	    ok
    end,
    erlang:yield().

schedcnt(start) ->
    Ref = make_ref(),
    Fun = 
	fun (F, Cnt) ->
		receive
		    {Ref, Parent} ->
			Parent ! {Ref, Cnt}
		after 0 ->
			erlang:yield(),
			F(F, Cnt+1)
		end
	end,
    Pid = spawn_link(fun () -> Fun(Fun, 0) end),
    {Ref, Pid}.

schedcnt(stop, {Ref, Pid}) when is_reference(Ref), is_pid(Pid) ->
    Pid ! {Ref, self()},
    receive
	{Ref, Cnt} ->
	    Cnt
    end.

yield2(doc) -> [];
yield2(suite) -> [];
yield2(Config) when is_list(Config) ->
    ?line Me = self(),
    ?line Go = make_ref(),
    ?line RedDiff = make_ref(),
    ?line Done = make_ref(),
    ?line P = spawn(fun () ->
			    receive Go -> ok end,
			    {reductions, R1} = process_info(self(), reductions),
			    {ok, true} = call_yield(middle),
			    true = call_yield(final),
			    true = call_yield(),
			    true = apply(erlang, yield, []),
			    {reductions, R2} = process_info(self(), reductions),
			    Me ! {RedDiff, R2 - R1},
			    exit(Done)
		    end),
    ?line erlang:yield(),

    ?line 1 = erlang:trace(P, true, [running, procs, {tracer, self()}]),

    ?line P ! Go,

    %% receive Go -> ok end,
    ?line {trace, P, in, _} = next_tmsg(P),

    %% {ok, true} = call_yield(middle),
    ?line {trace, P, out, _} = next_tmsg(P),
    ?line {trace, P, in, _} = next_tmsg(P),

    %% true = call_yield(final),
    ?line {trace, P, out, _} = next_tmsg(P),
    ?line {trace, P, in, _} = next_tmsg(P),

    %% true = call_yield(),
    ?line {trace, P, out, _} = next_tmsg(P),
    ?line {trace, P, in, _} = next_tmsg(P),

    %% true = apply(erlang, yield, []),
    ?line {trace, P, out, _} = next_tmsg(P),
    ?line {trace, P, in, _} = next_tmsg(P),

    %% exit(Done)
    ?line {trace, P, exit, Done} = next_tmsg(P),


    ?line receive
	      {RedDiff, Reductions} when Reductions < 30, Reductions > 0 ->
		  io:format("Reductions = ~p~n", [Reductions]),
		  ?line ok;
	      {RedDiff, Reductions} ->
		  ?line ?t:fail({unexpected_reduction_count, Reductions})
	  end,

    ?line none = next_tmsg(P),

    ?line ok.

next_tmsg(Pid) ->
    receive
	TMsg when is_tuple(TMsg),
		  element(1, TMsg) == trace,
		  element(2, TMsg) == Pid ->
	    TMsg
    after 100 ->
	    none
    end.

%% Test that bad arguments to register/2 cause an exception.
bad_register(Config) when is_list(Config) ->
    Name = a_long_and_unused_name,

    ?line {'EXIT',{badarg,_}} = (catch register({bad,name}, self())),
    ?line fail_register(undefined, self()),
    ?line fail_register([bad,name], self()),

    ?line {Dead,Mref} = spawn_monitor(fun() -> true end),
    receive
	{'DOWN',Mref,process,Dead,_} -> ok
    end,
    ?line fail_register(Name, Dead),
    ?line fail_register(Name, make_ref()),
    ?line fail_register(Name, []),
    ?line fail_register(Name, {bad,process}),
    ?line fail_register(Name, <<>>),
    ok.

fail_register(Name, Process) ->
    {'EXIT',{badarg,_}} = (catch register(Name, Process)),
    {'EXIT',{badarg,_}} = (catch Name ! anything_goes),
    ok.

garbage_collect(doc) -> [];
garbage_collect(suite) -> [];
garbage_collect(Config) when is_list(Config) ->
    ?line Prio = process_flag(priority, high),
    ?line true = erlang:garbage_collect(),
    ?line TokLoopers = lists:map(fun (_) ->
					 spawn_opt(fun tok_loop/0,
						   [{priority, low}, link])
				 end,
				 lists:seq(1, 10)),
    ?line lists:foreach(fun (Pid) ->
				?line Mon = erlang:monitor(process, Pid),
				?line DownBefore = receive
						       {'DOWN', Mon, _, _, _} ->
							   ?line true
						   after 0 ->
							   ?line false
						   end,
				?line GC = erlang:garbage_collect(Pid),
				?line DownAfter = receive
						      {'DOWN', Mon, _, _, _} ->
							  ?line true
						  after 0 ->
							  ?line false
						  end,
				?line true = erlang:demonitor(Mon),
				?line case {DownBefore, DownAfter} of
					  {true, _} -> ?line false = GC;
					  {false, false} -> ?line true = GC;
					  _ -> ?line GC
				      end
			end,
			processes()),
    ?line lists:foreach(fun (Pid) ->
				unlink(Pid),
				exit(Pid, bang)
			end, TokLoopers),
    ?line process_flag(priority, Prio),
    ?line ok.

process_info_messages(doc) ->
    ["This used to cause the nofrag emulator to dump core"];
process_info_messages(suite) ->
    [];
process_info_messages(Config) when is_list(Config) ->
    ?line process_info_messages_test(),
    ?line ok.

process_info_messages_loop(0) -> ok;
process_info_messages_loop(N) -> process_info_messages_loop(N-1).

process_info_messages_send_my_msgs_to(Rcvr) ->
    receive
	Msg ->
	    Rcvr ! Msg,
	    process_info_messages_send_my_msgs_to(Rcvr)
    after 0 ->
	    ok
    end.

process_info_messages_test() ->
    ?line Go = make_ref(),
    ?line Done = make_ref(),
    ?line Rcvr = self(),
    ?line Rcvr2 = spawn_link(fun () ->
				     receive {Go, Rcvr} -> ok end,
				     garbage_collect(),
				     Rcvr ! {Done, self()}
			     end),
    ?line Sndrs = lists:map(
		    fun (_) ->
			    spawn_link(fun () ->
					       Rcvr ! {Go, self()},
					       receive {Go, Rcvr} -> ok end,
					       BigData = lists:seq(1, 1000),
					       Rcvr ! BigData,
					       Rcvr ! BigData,
					       Rcvr ! BigData,
					       Rcvr ! {Done, self()}
				       end)
		    end,
		    lists:seq(1, 10)),
    ?line lists:foreach(fun (Sndr) -> receive {Go, Sndr} -> ok end end,
			Sndrs),
    ?line garbage_collect(),
    ?line erlang:yield(),
    ?line lists:foreach(fun (Sndr) -> Sndr ! {Go, self()} end, Sndrs),
    ?line process_info_messages_loop(100000000),
    ?line Msgs = process_info(self(), messages),
    ?line lists:foreach(fun (Sndr) -> receive {Done, Sndr} -> ok end end,
			Sndrs),
    ?line garbage_collect(),
    ?line Rcvr2 ! Msgs,
    ?line process_info_messages_send_my_msgs_to(Rcvr2),
    ?line Rcvr2 ! {Go, self()},
    ?line garbage_collect(),
    ?line receive {Done, Rcvr2} -> ok end,
    ?line Msgs.

chk_badarg(Fun) ->
    try Fun(), exit(no_badarg) catch error:badarg -> ok end.

process_flag_badarg(doc) ->
    [];
process_flag_badarg(suite) ->
    [];
process_flag_badarg(Config) when is_list(Config) ->
    ?line chk_badarg(fun () -> process_flag(gurka, banan) end),
    ?line chk_badarg(fun () -> process_flag(trap_exit, gurka) end),
    ?line chk_badarg(fun () -> process_flag(error_handler, 1) end),
    ?line chk_badarg(fun () -> process_flag(min_heap_size, gurka) end),
    ?line chk_badarg(fun () -> process_flag(min_bin_vheap_size, gurka) end),
    ?line chk_badarg(fun () -> process_flag(min_bin_vheap_size, -1) end),
    ?line chk_badarg(fun () -> process_flag(priority, 4711) end),
    ?line chk_badarg(fun () -> process_flag(save_calls, hmmm) end),
    ?line P= spawn_link(fun () -> receive die -> ok end end),
    ?line chk_badarg(fun () -> process_flag(P, save_calls, hmmm) end),
    ?line chk_badarg(fun () -> process_flag(gurka, save_calls, hmmm) end),
    ?line P ! die,
    ?line ok.

-include_lib("stdlib/include/ms_transform.hrl").

otp_6237(doc) -> [];
otp_6237(suite) -> [];
otp_6237(Config) when is_list(Config) ->
    ?line Slctrs = lists:map(fun (_) ->
				     spawn_link(fun () ->
							otp_6237_select_loop()
						end)
			     end,
			     lists:seq(1,5)),
    ?line lists:foreach(fun (_) -> otp_6237_test() end, lists:seq(1, 100)),
    ?line lists:foreach(fun (S) -> unlink(S),exit(S, kill) end, Slctrs), 
    ?line ok.
				
otp_6237_test() ->
    ?line Parent = self(),
    ?line Inited = make_ref(),
    ?line Die = make_ref(),
    ?line Pid = spawn_link(fun () ->
				   register(otp_6237,self()),
				   otp_6237 = ets:new(otp_6237,
						      [named_table,
						       ordered_set]),
				   ets:insert(otp_6237,
					      [{I,I}
					       || I <- lists:seq(1, 100)]),
				   %% Inserting a lot of bif timers
				   %% increase the possibility that
				   %% the test will fail when the
				   %% original cleanup order is used
				   lists:foreach(
				     fun (_) ->
					     erlang:send_after(1000000,
							       self(),
							       {a,b,c})
				     end,
				     lists:seq(1,1000)),
				   Parent ! Inited,
				   receive Die -> bye end
			   end),
    ?line receive
	      Inited -> ?line ok
	  end,
    ?line Pid ! Die,
    otp_6237_whereis_loop().

otp_6237_whereis_loop() ->
    ?line case whereis(otp_6237) of
	      undefined ->
		  ?line otp_6237 = ets:new(otp_6237,
					   [named_table,ordered_set]),
		  ?line ets:delete(otp_6237),
		  ?line ok;
	      _ ->
		  ?line otp_6237_whereis_loop()
	  end.
	     
otp_6237_select_loop() ->
    catch ets:select(otp_6237, ets:fun2ms(fun({K, does_not_exist}) -> K end)),
    otp_6237_select_loop().


processes_bif(doc) ->
    [];
processes_bif(suite) ->
    [processes_large_tab,
     processes_default_tab,
     processes_small_tab,
     processes_this_tab,
     processes_last_call_trap,
     processes_apply_trap,
     processes_gc_trap,
     processes_term_proc_list].

-define(NoTestProcs, 10000).
-record(processes_bif_info, {min_start_reds,
			     tab_chunks,
			     tab_chunks_size,
			     tab_indices_per_red,
			     free_term_proc_reds,
			     term_procs_per_red,
			     term_procs_max_reds,
			     conses_per_red,
			     debug_level}).

processes_large_tab(doc) ->
    [];
processes_large_tab(suite) ->
    [];
processes_large_tab(Config) when is_list(Config) ->
    ?line enable_internal_state(),
    ?line MaxDbgLvl = 20,
    ?line MinProcTabSize = 2*(1 bsl 15),
    ?line ProcTabSize0 = 1000000,
    ?line ProcTabSize1 = case {erlang:system_info(schedulers_online),
			       erlang:system_info(logical_processors)} of
			     {Schdlrs, Cpus} when is_integer(Cpus),
			                          Schdlrs =< Cpus ->
				 ProcTabSize0;
			     _ ->
				 ProcTabSize0 div 4
			 end,
    ?line ProcTabSize2 = case erlang:system_info(debug_compiled) of
		     true -> ProcTabSize1 - 500000;
		     false -> ProcTabSize1
		 end,
    %% With high debug levels this test takes so long time that
    %% the connection times out; therefore, shrink the test on
    %% high debug levels.
    ?line DbgLvl = case erts_debug:get_internal_state(processes_bif_info) of
		       #processes_bif_info{debug_level = Lvl} when Lvl > MaxDbgLvl ->
			   20;
		       #processes_bif_info{debug_level = Lvl} when Lvl < 0 ->
			   ?line ?t:fail({debug_level, Lvl});
		       #processes_bif_info{debug_level = Lvl} ->
			   Lvl
		   end,
    ?line ProcTabSize3 = ProcTabSize2 - (1300000 * DbgLvl div MaxDbgLvl),
    ?line ProcTabSize = case ProcTabSize3 < MinProcTabSize of
			    true -> MinProcTabSize;
			    false -> ProcTabSize3
			end,
    ?line {ok, LargeNode} = start_node(Config,
				       "+P " ++ integer_to_list(ProcTabSize)),
    ?line Res = rpc:call(LargeNode, ?MODULE, processes_bif_test, []),
    ?line case rpc:call(LargeNode,
			erts_debug,
			get_internal_state,
			[processes_bif_info]) of
	      #processes_bif_info{tab_chunks = Chunks} when is_integer(Chunks),
							    Chunks > 1 -> ok;
	      PBInfo -> ?t:fail(PBInfo)
	  end,
    ?line stop_node(LargeNode),
    ?line chk_processes_bif_test_res(Res).

processes_default_tab(doc) ->
    [];
processes_default_tab(suite) ->
    [];
processes_default_tab(Config) when is_list(Config) ->
    ?line {ok, DefaultNode} = start_node(Config, ""),
    ?line Res = rpc:call(DefaultNode, ?MODULE, processes_bif_test, []),
    ?line stop_node(DefaultNode),
    ?line chk_processes_bif_test_res(Res).

processes_small_tab(doc) ->
    [];
processes_small_tab(suite) ->
    [];
processes_small_tab(Config) when is_list(Config) ->
    ?line {ok, SmallNode} = start_node(Config, "+P 500"),
    ?line Res = rpc:call(SmallNode, ?MODULE, processes_bif_test, []),
    ?line PBInfo = rpc:call(SmallNode,
			    erts_debug,
			    get_internal_state,
			    [processes_bif_info]),
    ?line stop_node(SmallNode),
    ?line 1 = PBInfo#processes_bif_info.tab_chunks,
    ?line chk_processes_bif_test_res(Res).

processes_this_tab(doc) ->
    [];
processes_this_tab(suite) ->
    [];
processes_this_tab(Config) when is_list(Config) ->
    ?line chk_processes_bif_test_res(processes_bif_test()).

chk_processes_bif_test_res(ok) -> ok;
chk_processes_bif_test_res({comment, _} = Comment) -> Comment;
chk_processes_bif_test_res(Failure) -> ?t:fail(Failure).

print_processes_bif_info(#processes_bif_info{min_start_reds = MinStartReds,
					     tab_chunks = TabChunks,
					     tab_chunks_size = TabChunksSize,
					     tab_indices_per_red = TabIndPerRed,
					     free_term_proc_reds = FreeTPReds,
					     term_procs_per_red = TPPerRed,
					     term_procs_max_reds = TPMaxReds,
					     conses_per_red = ConsesPerRed,
					     debug_level = DbgLvl}) ->
    ?t:format("processes/0 bif info on node ~p:~n"
	      "Min start reductions = ~p~n"
	      "Process table chunks = ~p~n"
	      "Process table chunks size = ~p~n"
	      "Process table indices per reduction = ~p~n"
	      "Reduction cost for free() on terminated process struct = ~p~n"
	      "Inspect terminated processes per reduction = ~p~n"
	      "Max reductions during inspection of terminated processes = ~p~n"
	      "Create cons-cells per reduction = ~p~n"
	      "Debug level = ~p~n",
	      [node(),
	       MinStartReds,
	       TabChunks,
	       TabChunksSize,
	       TabIndPerRed,
	       FreeTPReds,
	       TPPerRed,
	       TPMaxReds,
	       ConsesPerRed,
	       DbgLvl]).

processes_bif_cleaner() ->
    receive {'EXIT', _, _} -> ok end,
    processes_bif_cleaner().

spawn_initial_hangarounds(Cleaner) ->
    TabSz = erlang:system_info(process_limit),
    spawn_initial_hangarounds(Cleaner,
			      TabSz,
			      TabSz*2,
			      0,
			      []).

processes_unexpected_result(CorrectProcs, Procs) ->
    ProcInfo = [registered_name,
		initial_call,
		current_function,
		status,
		priority],
    MissingProcs = CorrectProcs -- Procs,
    ?t:format("Missing processes: ~p",
	      [lists:map(fun (Pid) ->
				 [{pid, Pid}
				  | case process_info(Pid, ProcInfo) of
					undefined -> [];
					Res -> Res
				    end]
			 end,
			 MissingProcs)]),
    SuperfluousProcs = Procs -- CorrectProcs,
    ?t:format("Superfluous processes: ~p",
	      [lists:map(fun (Pid) ->
				 [{pid, Pid}
				  | case process_info(Pid, ProcInfo) of
					undefined -> [];
					Res -> Res
				    end]
			 end,
			 SuperfluousProcs)]),
    ?t:fail(unexpected_result).

hangaround(Cleaner, Type) ->
    %% Type is only used to distinguish different processes from
    %% when doing process_info
    try link(Cleaner) catch error:Reason -> exit(Reason) end,
    receive after infinity -> ok end,
    exit(Type).

spawn_initial_hangarounds(_Cleaner, NP, Max, Len, HAs) when NP > Max ->
    {Len, HAs};
spawn_initial_hangarounds(Cleaner, NP, Max, Len, HAs) ->
    erts_debug:set_internal_state(next_pid,NP),
    HA1 = spawn_opt(?MODULE, hangaround, [Cleaner, initial_hangaround],
		    [{priority, low}]),
    HA2 = spawn_opt(?MODULE, hangaround, [Cleaner, initial_hangaround],
		    [{priority, normal}]),
    HA3 = spawn_opt(?MODULE, hangaround, [Cleaner, initial_hangaround],
		    [{priority, high}]),
    spawn_initial_hangarounds(Cleaner, NP+30, Max, Len+3, [HA1,HA2,HA3|HAs]).

do_processes(WantReds) ->
    erts_debug:set_internal_state(reds_left, WantReds),
    processes().

processes_bif_test() ->
    ?line Tester = self(),
    ?line enable_internal_state(),
    ?line PBInfo = erts_debug:get_internal_state(processes_bif_info),
    ?line print_processes_bif_info(PBInfo),
    ?line WantReds = PBInfo#processes_bif_info.min_start_reds + 10,
    ?line WillTrap = case PBInfo of
			#processes_bif_info{tab_chunks = 1} ->
			    false;
			#processes_bif_info{tab_chunks = Chunks,
					    tab_chunks_size = ChunksSize,
					    tab_indices_per_red = IndiciesPerRed
					   } ->
			    Chunks*ChunksSize >= IndiciesPerRed*WantReds
		    end,
    ?line Processes = fun () ->
			      erts_debug:set_internal_state(reds_left,WantReds),
			      processes()
		      end,

    ?line ok = do_processes_bif_test(WantReds, WillTrap, Processes),

    case WillTrap of
	false ->
	    ok;
	true ->
	    %% Do it again with a process suspended while
	    %% in the processes/0 bif.
	    ?line erlang:system_flag(multi_scheduling, block),
	    ?line Suspendee = spawn_link(fun () ->
						 Tester ! {suspend_me, self()},
						 Tester ! {self(),
							   done,
							   hd(Processes())},
						 receive
						 after infinity ->
							 ok
						 end
					 end),
	    ?line receive {suspend_me, Suspendee} -> ok end,
	    ?line erlang:suspend_process(Suspendee),
	    ?line erlang:system_flag(multi_scheduling, unblock),
	    
	    ?line [{status,suspended},
		   {current_function,{erlang,processes_trap,2}}]
		= process_info(Suspendee, [status, current_function]),

	    ?line ok = do_processes_bif_test(WantReds, WillTrap, Processes),
	    
	    ?line erlang:resume_process(Suspendee),
	    ?line receive {Suspendee, done, _} -> ok end,
	    ?line unlink(Suspendee),
	    ?line exit(Suspendee, bang)
    end,
    case get(processes_bif_testcase_comment) of
	undefined -> ?line ok;
	Comment -> ?line {comment, Comment}
    end.
    
do_processes_bif_test(WantReds, DieTest, Processes) ->
    ?line Tester = self(),
    ?line SpawnProcesses = fun (Prio) ->
				   spawn_opt(?MODULE,
					     do_processes,
					     [WantReds],
					     [link, {priority, Prio}])
			   end,
    ?line Cleaner = spawn_link(fun () ->
				       process_flag(trap_exit, true),
				       Tester ! {cleaner_alive, self()},
				       processes_bif_cleaner()
			       end),
    ?line receive {cleaner_alive, Cleaner} -> ok end,
    try
	?line DoIt = make_ref(),
	?line GetGoing = make_ref(),
	?line {NoTestProcs, TestProcs} = spawn_initial_hangarounds(Cleaner),
	?line ?t:format("Testing with ~p processes~n", [NoTestProcs]),
	?line SpawnHangAround = fun () ->
					spawn(?MODULE,
					      hangaround,
					      [Cleaner, new_hangaround])
				end,
	?line Killer = spawn_opt(fun () ->
					 Splt = NoTestProcs div 10,
					 {TP1, TP23} = lists:split(Splt,
								   TestProcs),
					 {TP2, TP3} = lists:split(Splt, TP23),
					 erlang:system_flag(multi_scheduling,
							    block),
					 Tester ! DoIt,
					 receive GetGoing -> ok end,
					 erlang:system_flag(multi_scheduling,
							    unblock),
					 SpawnProcesses(high),
					 lists:foreach(
					   fun (P) ->
						   SpawnHangAround(),
						   exit(P, bang)
					   end,
					   TP1),
					 SpawnProcesses(high),
					 erlang:yield(),
					 lists:foreach(
					   fun (P) ->
						   SpawnHangAround(),
						   exit(P, bang)
					   end,
					   TP2),
					 SpawnProcesses(high),
					 lists:foreach(
					   fun (P) ->
						   SpawnHangAround(),
						   exit(P, bang)
					   end,
					   TP3)
				 end,
				 [{priority, high}, link]),
	?line receive DoIt -> ok end,
	?line process_flag(priority, low),
	?line SpawnProcesses(low),
	?line erlang:yield(),
	?line process_flag(priority, normal),
	?line CorrectProcs0 = erts_debug:get_internal_state(processes),
	?line Killer ! GetGoing,
	?line erts_debug:set_internal_state(reds_left, WantReds),
	?line Procs0 = processes(),
	?line Procs = lists:sort(Procs0),
	?line CorrectProcs = lists:sort(CorrectProcs0),
	?line LengthCorrectProcs = length(CorrectProcs),
	?line ?t:format("~p = length(CorrectProcs)~n", [LengthCorrectProcs]),
	?line true = LengthCorrectProcs > NoTestProcs,
	?line case CorrectProcs =:= Procs of
		  true ->
		      ?line ok;
		  false ->
		      ?line processes_unexpected_result(CorrectProcs, Procs)
	      end,
	?line unlink(Killer),
	?line exit(Killer, bang)
    after
	unlink(Cleaner),
        exit(Cleaner, kill),
        %% Wait for the system to recover to a normal state...
	wait_until_system_recover()
    end,
    ?line do_processes_bif_die_test(DieTest, Processes),
    ?line ok.


do_processes_bif_die_test(false, _Processes) ->
    ?line ?t:format("Skipping test killing process executing processes/0~n",[]),
    ?line ok;
do_processes_bif_die_test(true, Processes) ->
    ?line do_processes_bif_die_test(5, Processes);
do_processes_bif_die_test(N, Processes) ->
    ?line ?t:format("Doing test killing process executing processes/0~n",[]),
    try
	?line Tester = self(),
	?line Oooh_Nooooooo = make_ref(),
	?line {_, DieWhileDoingMon} = erlang:spawn_monitor(
					fun () ->
						Victim = self(),
						spawn_opt(
						  fun () ->
							  exit(Victim, got_him)
						  end,
						  [link,
						   {priority, max}]),
						Tester ! {Oooh_Nooooooo,
							  hd(Processes())},
						exit(ohhhh_nooooo)
					end),
	?line receive
		  {'DOWN', DieWhileDoingMon, _, _, Reason} ->
		      case Reason of
			  got_him -> ok;
			  _ -> throw({kill_in_trap, Reason})
		      end
	      end,
	?line receive
		  {Oooh_Nooooooo, _} ->
		      ?line throw({kill_in_trap, 'Oooh_Nooooooo'})
	      after 0 ->
		      ?line ok
	      end,
	?line PrcsCllrsSeqLen = 2*erlang:system_info(schedulers_online),
	?line PrcsCllrsSeq = lists:seq(1, PrcsCllrsSeqLen),
	?line ProcsCallers = lists:map(
			       fun (_) ->
				       spawn_link(
					 fun () ->
						 Tester ! hd(Processes())
					 end)
			       end,
			       PrcsCllrsSeq),
	?line erlang:yield(),
	{ProcsCallers1, ProcsCallers2} = lists:split(PrcsCllrsSeqLen div 2,
						     ProcsCallers),
	?line process_flag(priority, high),
	?line lists:foreach(
		fun (P) ->
			unlink(P),
			exit(P, bang)
		end,
		lists:reverse(ProcsCallers2) ++ ProcsCallers1),
	?line process_flag(priority, normal),
	?line ok
    catch
	throw:{kill_in_trap, R} when N > 0 ->
	    ?t:format("Failed to kill in trap: ~p~n", [R]),
	    ?t:format("Trying again~p~n", []),
	    do_processes_bif_die_test(N-1, Processes)
    end.
	    

wait_until_system_recover() ->
    %% If system hasn't recovered after 10 seconds we give up
    Tmr = erlang:start_timer(10000, self(), no_more_wait),
    wait_until_system_recover(Tmr).

wait_until_system_recover(Tmr) ->
    try
	lists:foreach(fun (P) when P == self() ->
			      ok;
			  (P) ->
			      case process_info(P, initial_call) of
				  {initial_call,{?MODULE, _, _}} ->
				      throw(wait);
				  {initial_call,{_, _, _}} ->
				      ok;
				  undefined ->
				      ok
			      end
		      end,
		      processes())
    catch
	throw:wait ->
	    receive
		{timeout, Tmr, _} ->
		    Comment = "WARNING: Test processes still hanging around!",
		    ?t:format("~s~n", [Comment]),
		    put(processes_bif_testcase_comment, Comment),
		    lists:foreach(
		      fun (P) when P == self() ->
			      ok;
			  (P) ->
			      case process_info(P, initial_call) of
				  {initial_call,{?MODULE, _, _} = MFA} ->
				      ?t:format("~p ~p~n", [P, MFA]);
				  {initial_call,{_, _, _}} ->
				      ok;
				  undefined ->
				      ok
			      end
		      end,
		      processes())
	    after 100 ->
		    wait_until_system_recover(Tmr)
	    end
    end,
    erlang:cancel_timer(Tmr),
    receive {timeout, Tmr, _} -> ok after 0 -> ok end,
    ok.

processes_last_call_trap(doc) ->
    [];
processes_last_call_trap(suite) ->
    [];
processes_last_call_trap(Config) when is_list(Config) ->
    ?line enable_internal_state(),
    ?line Processes = fun () -> processes() end,
    ?line PBInfo = erts_debug:get_internal_state(processes_bif_info),
    ?line print_processes_bif_info(PBInfo),
    ?line WantReds = case PBInfo#processes_bif_info.min_start_reds of
			 R when R > 10 -> R - 1;
			 _R -> 9
		     end,
    ?line lists:foreach(fun (_) ->
				?line erts_debug:set_internal_state(reds_left,
								    WantReds),
				Processes(),
				?line erts_debug:set_internal_state(reds_left,
								    WantReds),
				my_processes()
			end,
			lists:seq(1,100)).
    
my_processes() ->
    processes().

processes_apply_trap(doc) ->
    [];
processes_apply_trap(suite) ->
    [];
processes_apply_trap(Config) when is_list(Config) ->
    ?line enable_internal_state(),
    ?line PBInfo = erts_debug:get_internal_state(processes_bif_info),
    ?line print_processes_bif_info(PBInfo),
    ?line WantReds = case PBInfo#processes_bif_info.min_start_reds of
			 R when R > 10 -> R - 1;
			 _R -> 9
		     end,
    ?line lists:foreach(fun (_) ->
				?line erts_debug:set_internal_state(reds_left,
								    WantReds),
				?line apply(erlang, processes, [])
			end,
			lists:seq(1,100)).

processes_gc_trap(doc) ->
    [];
processes_gc_trap(suite) ->
    [];
processes_gc_trap(Config) when is_list(Config) ->
    ?line Tester = self(),
    ?line enable_internal_state(),
    ?line PBInfo = erts_debug:get_internal_state(processes_bif_info),
    ?line print_processes_bif_info(PBInfo),
    ?line WantReds = PBInfo#processes_bif_info.min_start_reds + 10,
    ?line Processes = fun () ->
			      erts_debug:set_internal_state(reds_left,WantReds),
			      processes()
		      end,

    ?line erlang:system_flag(multi_scheduling, block),
    ?line Suspendee = spawn_link(fun () ->
					 Tester ! {suspend_me, self()},
					 Tester ! {self(),
						   done,
						   hd(Processes())},
					 receive after infinity -> ok end
				 end),
    ?line receive {suspend_me, Suspendee} -> ok end,
    ?line erlang:suspend_process(Suspendee),
    ?line erlang:system_flag(multi_scheduling, unblock),
	    
    ?line [{status,suspended}, {current_function,{erlang,processes_trap,2}}]
	= process_info(Suspendee, [status, current_function]),

    ?line erlang:garbage_collect(Suspendee),
    ?line erlang:garbage_collect(Suspendee),
	    
    ?line erlang:resume_process(Suspendee),
    ?line receive {Suspendee, done, _} -> ok end,
    ?line erlang:garbage_collect(Suspendee),
    ?line erlang:garbage_collect(Suspendee),

    ?line unlink(Suspendee),
    ?line exit(Suspendee, bang),
    ?line ok.

process_flag_heap_size(doc) ->
    [];
process_flag_heap_size(suite) ->
    [];
process_flag_heap_size(Config) when is_list(Config) ->
    HSize  = 2584,   % must be gc fib number
    VHSize = 317811, % must be gc fib number
    ?line OldHmin = erlang:process_flag(min_heap_size, HSize),
    ?line {min_heap_size, HSize} = erlang:process_info(self(), min_heap_size),
    ?line OldVHmin = erlang:process_flag(min_bin_vheap_size, VHSize),
    ?line {min_bin_vheap_size, VHSize} = erlang:process_info(self(), min_bin_vheap_size),
    ?line HSize = erlang:process_flag(min_heap_size, OldHmin),
    ?line VHSize = erlang:process_flag(min_bin_vheap_size, OldVHmin),
    ?line ok.

spawn_opt_heap_size(doc) ->
    [];
spawn_opt_heap_size(suite) ->
    [];
spawn_opt_heap_size(Config) when is_list(Config) ->
    HSize  = 987,   % must be gc fib number
    VHSize = 46368, % must be gc fib number
    ?line Pid  = spawn_opt(fun () -> receive stop -> ok end end,
	[{min_heap_size, HSize},{ min_bin_vheap_size, VHSize}]),
    ?line {min_heap_size, HSize} = process_info(Pid, min_heap_size),
    ?line {min_bin_vheap_size, VHSize} = process_info(Pid, min_bin_vheap_size),
    ?line Pid ! stop,
    ?line ok.

processes_term_proc_list(doc) ->
    [];
processes_term_proc_list(suite) ->
    [];
processes_term_proc_list(Config) when is_list(Config) ->
    ?line Tester = self(),
    ?line as_expected = processes_term_proc_list_test(false),
    ?line {ok, Node} = start_node(Config, "+Mis true"),
    ?line RT = spawn_link(Node,
			  fun () ->
				  receive after 1000 -> ok end,
				  processes_term_proc_list_test(false),
				  Tester ! {it_worked, self()}
			  end),
    ?line receive {it_worked, RT} -> ok end,
    ?line stop_node(Node),
    ?line ok.
    
-define(CHK_TERM_PROC_LIST(MC, XB),
	chk_term_proc_list(?LINE, MC, XB)).

chk_term_proc_list(Line, MustChk, ExpectBlks) ->
    case {MustChk, instrument:memory_status(types)} of
	{false, false} ->
	    not_enabled;
	{_, MS} ->
	    {value,
	     {processes_term_proc_el,
	      DL}} = lists:keysearch(processes_term_proc_el, 1, MS),
	    case lists:keysearch(blocks, 1, DL) of
		{value, {blocks, ExpectBlks, _, _}} ->
		    ok;
		{value, {blocks, Blks, _, _}} ->
		    exit({line, Line,
			  mismatch, expected, ExpectBlks, actual, Blks});
		Unexpected ->
		    exit(Unexpected)
	    end
    end,
    ok.

processes_term_proc_list_test(MustChk) ->
    ?line Tester = self(),
    ?line enable_internal_state(),
    ?line PBInfo = erts_debug:get_internal_state(processes_bif_info),
    ?line print_processes_bif_info(PBInfo),
    ?line WantReds = PBInfo#processes_bif_info.min_start_reds + 10,
    ?line #processes_bif_info{tab_chunks = Chunks,
			      tab_chunks_size = ChunksSize,
			      tab_indices_per_red = IndiciesPerRed
			     } = PBInfo,
    ?line true = Chunks > 1,
    ?line true = Chunks*ChunksSize >= IndiciesPerRed*WantReds,
    ?line Processes = fun () ->
			      erts_debug:set_internal_state(reds_left,
							    WantReds),
			      processes()
		      end,
    ?line Exit = fun (P) ->
			 unlink(P),
			 exit(P, bang),
			 wait_until(
			   fun () ->
				   not lists:member(
					 P,
					 erts_debug:get_internal_state(
					   processes))
			   end)
		 end,
    ?line SpawnSuspendProcessesProc
	= fun () ->
		  erlang:system_flag(multi_scheduling, block),
		  P = spawn_link(fun () ->
					 Tester ! {suspend_me, self()},
					 Tester ! {self(),
						   done,
						   hd(Processes())},
					 receive after infinity -> ok end
				 end),
		  receive {suspend_me, P} -> ok end,
		  erlang:suspend_process(P),
		  erlang:system_flag(multi_scheduling, unblock),
		  [{status,suspended},
		   {current_function,{erlang,processes_trap,2}}]
		      = process_info(P, [status, current_function]),
		  P
	  end,
    ?line ResumeProcessesProc = fun (P) ->
					erlang:resume_process(P),
					receive {P, done, _} -> ok end
				end,
    ?line ?CHK_TERM_PROC_LIST(MustChk, 0),
    ?line HangAround = fun () -> receive after infinity -> ok end end,
    ?line HA1 = spawn_link(HangAround),
    ?line HA2 = spawn_link(HangAround),
    ?line HA3 = spawn_link(HangAround),
    ?line S1 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 1),
    ?line Exit(HA1),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 2),
    ?line S2 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 3),
    ?line S3 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 4),
    ?line Exit(HA2),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 5),
    ?line S4 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 6),
    ?line Exit(HA3),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 7),
    ?line ResumeProcessesProc(S1),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 5),
    ?line ResumeProcessesProc(S3),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 4),
    ?line ResumeProcessesProc(S4),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 3),
    ?line ResumeProcessesProc(S2),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 0),
    ?line Exit(S1),
    ?line Exit(S2),
    ?line Exit(S3),
    ?line Exit(S4),


    ?line HA4 = spawn_link(HangAround),
    ?line HA5 = spawn_link(HangAround),
    ?line HA6 = spawn_link(HangAround),
    ?line S5 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 1),
    ?line Exit(HA4),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 2),
    ?line S6 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 3),
    ?line Exit(HA5),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 4),
    ?line S7 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 5),
    ?line Exit(HA6),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 6),
    ?line S8 = SpawnSuspendProcessesProc(),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 7),

    ?line erlang:system_flag(multi_scheduling, block),
    ?line Exit(S8),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 7),
    ?line Exit(S5),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 6),
    ?line Exit(S7),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 6),
    ?line Exit(S6),
    ?line ?CHK_TERM_PROC_LIST(MustChk, 0),
    ?line erlang:system_flag(multi_scheduling, unblock),
    ?line as_expected.

otp_7738(doc) ->
    [];
otp_7738(suite) ->
    [otp_7738_waiting, otp_7738_suspended, otp_7738_resume].

otp_7738_waiting(doc) ->
    [];
otp_7738_waiting(suite) ->
    [];
otp_7738_waiting(Config) when is_list(Config) ->
    ?line otp_7738_test(waiting).

otp_7738_suspended(doc) ->
    [];
otp_7738_suspended(suite) ->
    [];
otp_7738_suspended(Config) when is_list(Config) ->
    ?line otp_7738_test(suspended).

otp_7738_resume(doc) ->
    [];
otp_7738_resume(suite) ->
    [];
otp_7738_resume(Config) when is_list(Config) ->
    ?line otp_7738_test(resume).

otp_7738_test(Type) ->
    ?line T = self(),
    ?line S = spawn_link(fun () ->
			   receive
			       {suspend, Suspendee} ->
				   erlang:suspend_process(Suspendee),
				   T ! {suspended, Suspendee},
				   receive
				   after 10 ->
					   erlang:resume_process(Suspendee),
					   Suspendee ! wake_up
				   end;
			       {send, To, Msg} ->
				   receive after 10 -> ok end,
				   To ! Msg
			   end
			 end),
    ?line R = spawn_link(fun () ->
				 X = lists:seq(1, 20000000),
				 T ! {initialized, self()},
				 ?line case Type of
					   _ when Type == suspended;
						  Type == waiting ->
					       receive _ -> ok end;
					   _ when Type == resume ->
					       Receive = fun (F) ->
								 receive
								     _ ->
									 ok
								 after 0 ->
									 F(F)
								 end
							 end,
					       Receive(Receive)
				       end,
				 T ! {woke_up, self()},
				 id(X)
			 end),
    ?line receive {initialized, R} -> ok end,
    ?line receive after 10 -> ok end,
    ?line case Type of
	      suspended ->
		  ?line erlang:suspend_process(R),
		  ?line S ! {send, R, wake_up};
	      waiting ->
		  ?line S ! {send, R, wake_up};
	      resume ->
		  ?line S ! {suspend, R},
		  ?line receive {suspended, R} -> ok end
	  end,
    ?line erlang:garbage_collect(R),
    ?line case Type of
	      suspended ->
		  ?line erlang:resume_process(R);
	      _ ->
		  ?line ok
	  end,
    ?line receive
	      {woke_up, R} ->
		  ?line ok
	  after 2000 ->
		  ?line I = process_info(R, [status, message_queue_len]),
		  ?line ?t:format("~p~n", [I]),
		  ?line ?t:fail(no_progress)
	  end,
    ?line ok.

%% Internal functions

wait_until(Fun) ->
    case Fun() of
	true -> true;
	false -> receive after 10 -> wait_until(Fun) end
    end.

tok_loop() ->
    tok_loop(hej).

tok_loop(hej) ->
    tok_loop(hopp);
tok_loop(hopp) ->
    tok_loop(hej).

id(I) -> I.
    
start_node(Config) ->
    start_node(Config, "").

start_node(Config, Args) when is_list(Config) ->
    ?line Pa = filename:dirname(code:which(?MODULE)),
    ?line {A, B, C} = now(),
    ?line Name = list_to_atom(atom_to_list(?MODULE)
			      ++ "-"
			      ++ atom_to_list(?config(testcase, Config))
			      ++ "-"
			      ++ integer_to_list(A)
			      ++ "-"
			      ++ integer_to_list(B)
			      ++ "-"
			      ++ integer_to_list(C)),
    ?line ?t:start_node(Name, slave, [{args, "-pa "++Pa++" "++Args}]).

stop_node(Node) ->
    ?t:stop_node(Node).

enable_internal_state() ->
    case catch erts_debug:get_internal_state(available_internal_state) of
	true -> true;
	_ -> erts_debug:set_internal_state(available_internal_state, true)
    end.