| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Dart test program testing code GC. | 5 // Dart test program testing code GC. |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import "dart:async"; | 8 import "dart:async"; |
| 9 import "dart:io"; | 9 import "dart:io"; |
| 10 | 10 |
| 11 | 11 |
| 12 int foo(int x) { | 12 int foo(int x) { |
| 13 x = x + 1; | 13 x = x + 1; |
| 14 return x; | 14 return x; |
| 15 } | 15 } |
| 16 | 16 |
| 17 | 17 |
| 18 List<int> bar() { | 18 List<int> bar() { |
| 19 // A couple of big allocations trigger GC. | 19 // A couple of big allocations trigger GC. |
| 20 var l = new List.filled(700000, 7); | 20 var l = new List.filled(700000, 7); |
| 21 return l; | 21 return l; |
| 22 } | 22 } |
| 23 | 23 |
| 24 | 24 |
| 25 doTest() { | 25 doTest() { |
| 26 var i = 0; | 26 var i = 0; |
| 27 foo(1); // Initial call to compile. | 27 var ret = foo(1); // Initial call to compile. |
| 28 print("foo=$ret"); |
| 28 // Time passes, GC runs, foo's code is dropped. | 29 // Time passes, GC runs, foo's code is dropped. |
| 29 var ms = const Duration(milliseconds: 100); | 30 var ms = const Duration(milliseconds: 100); |
| 30 var t = new Timer.periodic(ms, (timer) { | 31 var t = new Timer.periodic(ms, (timer) { |
| 31 i++; | 32 i++; |
| 32 bar(); | 33 bar(); |
| 33 if (i > 1) { | 34 if (i > 1) { |
| 34 timer.cancel(); | 35 timer.cancel(); |
| 35 // foo is called again to make sure we can still run it even after | 36 // foo is called again to make sure we can still run it even after |
| 36 // its code has been detached. | 37 // its code has been detached. |
| 37 foo(2); | 38 var ret = foo(2); |
| 39 print("foo=$ret"); |
| 38 } | 40 } |
| 39 }); | 41 }); |
| 40 } | 42 } |
| 41 | 43 |
| 42 | 44 |
| 43 main(List<String> arguments) { | 45 main(List<String> arguments) { |
| 44 if (arguments.contains("--run")) { | 46 if (arguments.contains("--run")) { |
| 45 doTest(); | 47 doTest(); |
| 46 } else { | 48 } else { |
| 47 // Run the test and capture stdout. | 49 // Run the test and capture stdout. |
| 48 var pr = Process.runSync(Platform.executable, | 50 var pr = Process.runSync(Platform.executable, |
| 49 ["--collect-code", | 51 ["--collect-code", |
| 50 "--code-collection-interval-in-us=100000", | 52 "--code-collection-interval-in-us=0", |
| 51 "--old_gen_growth_rate=10", | 53 "--old_gen_growth_rate=10", |
| 52 "--log-code-drop", | 54 "--log-code-drop", |
| 53 "--optimization-counter-threshold=-1", | 55 "--optimization-counter-threshold=-1", |
| 54 "--package-root=${Platform.packageRoot}", | 56 "--package-root=${Platform.packageRoot}", |
| 55 Platform.script.toFilePath(), | 57 Platform.script.toFilePath(), |
| 56 "--run"]); | 58 "--run"]); |
| 57 | 59 |
| 60 Expect.equals(0, pr.exitCode); |
| 61 |
| 58 // Code drops are logged with --log-code-drop. Look through stdout for the | 62 // Code drops are logged with --log-code-drop. Look through stdout for the |
| 59 // message that foo's code was dropped. | 63 // message that foo's code was dropped. |
| 60 var found = false; | 64 var count = 0; |
| 61 pr.stdout.split("\n").forEach((line) { | 65 pr.stdout.split("\n").forEach((line) { |
| 66 if (line.contains("foo=2")) { |
| 67 Expect.equals(0, count); |
| 68 count++; |
| 69 } |
| 62 if (line.contains("Detaching code") && line.contains("foo")) { | 70 if (line.contains("Detaching code") && line.contains("foo")) { |
| 63 found = true; | 71 Expect.equals(1, count); |
| 72 count++; |
| 73 } |
| 74 if (line.contains("foo=3")) { |
| 75 Expect.equals(2, count); |
| 76 count++; |
| 64 } | 77 } |
| 65 }); | 78 }); |
| 66 Expect.isTrue(found); | 79 Expect.equals(3, count); |
| 67 } | 80 } |
| 68 } | 81 } |
| OLD | NEW |