| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 * This class shows how to use %GetOptimizationCount() and | 37 * This class shows how to use %GetOptimizationCount() and |
| 38 * %GetOptimizationStatus() to infer information about opts and deopts. | 38 * %GetOptimizationStatus() to infer information about opts and deopts. |
| 39 * Might be nice to put this into mjsunit.js, but that doesn't depend on | 39 * Might be nice to put this into mjsunit.js, but that doesn't depend on |
| 40 * the --allow-natives-syntax flag so far. | 40 * the --allow-natives-syntax flag so far. |
| 41 */ | 41 */ |
| 42 function OptTracker() { | 42 function OptTracker() { |
| 43 this.opt_counts_ = {}; | 43 this.opt_counts_ = {}; |
| 44 } | 44 } |
| 45 | 45 |
| 46 /** | 46 /** |
| 47 * The possible optimization states of a function. Must be in sync with the |
| 48 * return values of Runtime_GetOptimizationStatus() in runtime.cc! |
| 49 * @enum {int} |
| 50 */ |
| 51 OptTracker.OptimizationState = { |
| 52 YES: 1, |
| 53 NO: 2, |
| 54 ALWAYS: 3, |
| 55 NEVER: 4 |
| 56 }; |
| 57 |
| 58 /** |
| 47 * Always call this at the beginning of your test, once for each function | 59 * Always call this at the beginning of your test, once for each function |
| 48 * that you later want to track de/optimizations for. It is necessary because | 60 * that you later want to track de/optimizations for. It is necessary because |
| 49 * tests are sometimes executed several times in a row, and you want to | 61 * tests are sometimes executed several times in a row, and you want to |
| 50 * disregard counts from previous runs. | 62 * disregard counts from previous runs. |
| 51 */ | 63 */ |
| 52 OptTracker.prototype.CheckpointOptCount = function(func) { | 64 OptTracker.prototype.CheckpointOptCount = function(func) { |
| 53 this.opt_counts_[func] = %GetOptimizationCount(func); | 65 this.opt_counts_[func] = %GetOptimizationCount(func); |
| 54 }; | 66 }; |
| 55 | 67 |
| 56 OptTracker.prototype.AssertOptCount = function(func, optcount) { | 68 OptTracker.prototype.AssertOptCount = function(func, optcount) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 75 assertTrue(this.GetDeoptCount_(func) > 0); | 87 assertTrue(this.GetDeoptCount_(func) > 0); |
| 76 } else { | 88 } else { |
| 77 assertEquals(0, this.GetDeoptCount_(func)); | 89 assertEquals(0, this.GetDeoptCount_(func)); |
| 78 } | 90 } |
| 79 } | 91 } |
| 80 | 92 |
| 81 OptTracker.prototype.AssertIsOptimized = function(func, expect_optimized) { | 93 OptTracker.prototype.AssertIsOptimized = function(func, expect_optimized) { |
| 82 if (this.DisableAsserts_(func)) { | 94 if (this.DisableAsserts_(func)) { |
| 83 return; | 95 return; |
| 84 } | 96 } |
| 85 var opt_status = %GetOptimizationStatus(func); | 97 var raw_optimized = %GetOptimizationStatus(func); |
| 86 assertTrue((opt_status & V8OptimizationStatus.kIsFunction) !== 0); | 98 if (expect_optimized) { |
| 87 assertEquals(expect_optimized, | 99 assertEquals(OptTracker.OptimizationState.YES, raw_optimized); |
| 88 (opt_status & V8OptimizationStatus.kOptimized) !== 0); | 100 } else { |
| 101 assertEquals(OptTracker.OptimizationState.NO, raw_optimized); |
| 102 } |
| 89 } | 103 } |
| 90 | 104 |
| 91 /** | 105 /** |
| 92 * @private | 106 * @private |
| 93 */ | 107 */ |
| 94 OptTracker.prototype.GetOptCount_ = function(func) { | 108 OptTracker.prototype.GetOptCount_ = function(func) { |
| 95 var raw_count = %GetOptimizationCount(func); | 109 var raw_count = %GetOptimizationCount(func); |
| 96 if (func in this.opt_counts_) { | 110 if (func in this.opt_counts_) { |
| 97 var checkpointed_count = this.opt_counts_[func]; | 111 var checkpointed_count = this.opt_counts_[func]; |
| 98 return raw_count - checkpointed_count; | 112 return raw_count - checkpointed_count; |
| 99 } | 113 } |
| 100 return raw_count; | 114 return raw_count; |
| 101 } | 115 } |
| 102 | 116 |
| 103 /** | 117 /** |
| 104 * @private | 118 * @private |
| 105 */ | 119 */ |
| 106 OptTracker.prototype.GetDeoptCount_ = function(func) { | 120 OptTracker.prototype.GetDeoptCount_ = function(func) { |
| 107 var count = this.GetOptCount_(func); | 121 var count = this.GetOptCount_(func); |
| 108 var opt_status = %GetOptimizationStatus(func); | 122 if (%GetOptimizationStatus(func) == OptTracker.OptimizationState.YES) { |
| 109 if ((opt_status & V8OptimizationStatus.kOptimized) !== 0) { | |
| 110 count -= 1; | 123 count -= 1; |
| 111 } | 124 } |
| 112 return count; | 125 return count; |
| 113 } | 126 } |
| 114 | 127 |
| 115 /** | 128 /** |
| 116 * @private | 129 * @private |
| 117 */ | 130 */ |
| 118 OptTracker.prototype.DisableAsserts_ = function(func) { | 131 OptTracker.prototype.DisableAsserts_ = function(func) { |
| 119 var opt_status = %GetOptimizationStatus(func); | 132 switch(%GetOptimizationStatus(func)) { |
| 120 return (opt_status & V8OptimizationStatus.kAlwaysOptimize) !== 0 || | 133 case OptTracker.OptimizationState.YES: |
| 121 (opt_status & V8OptimizationStatus.kNeverOptimize) !== 0; | 134 case OptTracker.OptimizationState.NO: |
| 135 return false; |
| 136 case OptTracker.OptimizationState.ALWAYS: |
| 137 case OptTracker.OptimizationState.NEVER: |
| 138 return true; |
| 139 } |
| 140 return true; |
| 122 } | 141 } |
| 123 // (End of class OptTracker.) | 142 // (End of class OptTracker.) |
| 124 | 143 |
| 125 // Example function used by the test below. | 144 // Example function used by the test below. |
| 126 function f(a) { | 145 function f(a) { |
| 127 return a+1; | 146 return a+1; |
| 128 } | 147 } |
| 129 | 148 |
| 130 var tracker = new OptTracker(); | 149 var tracker = new OptTracker(); |
| 131 tracker.CheckpointOptCount(f); | 150 tracker.CheckpointOptCount(f); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 155 // Let's trigger optimization for another type. | 174 // Let's trigger optimization for another type. |
| 156 for (var i = 0; i < 5; i++) f("a"); | 175 for (var i = 0; i < 5; i++) f("a"); |
| 157 | 176 |
| 158 %OptimizeFunctionOnNextCall(f); | 177 %OptimizeFunctionOnNextCall(f); |
| 159 f("b"); | 178 f("b"); |
| 160 | 179 |
| 161 tracker.AssertOptCount(f, 2); | 180 tracker.AssertOptCount(f, 2); |
| 162 tracker.AssertIsOptimized(f, true); | 181 tracker.AssertIsOptimized(f, true); |
| 163 tracker.AssertDeoptHappened(f, true); | 182 tracker.AssertDeoptHappened(f, true); |
| 164 tracker.AssertDeoptCount(f, 1); | 183 tracker.AssertDeoptCount(f, 1); |
| OLD | NEW |