Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
|
Jakob Kummerow
2012/01/16 11:41:25
nit: 2012
fschneider
2012/01/19 10:26:11
Done.
| |
| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 27 | 27 |
| 28 // Flags: --allow-natives-syntax | 28 // Flags: --allow-natives-syntax |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * This class shows how to use %GetOptimizationCount() and | 31 * This class shows how to use %GetOptimizationCount() and |
| 32 * %GetOptimizationStatus() to infer information about opts and deopts. | 32 * %GetOptimizationStatus() to infer information about opts and deopts. |
| 33 * Might be nice to put this into mjsunit.js, but that doesn't depend on | 33 * Might be nice to put this into mjsunit.js, but that doesn't depend on |
| 34 * the --allow-natives-syntax flag so far. | 34 * the --allow-natives-syntax flag so far. |
| 35 */ | 35 */ |
| 36 function OptTracker() { | 36 function OptTracker() { |
| 37 this.opt_counts_ = {}; | 37 this.deopt_counts_ = {}; |
| 38 } | 38 } |
| 39 | 39 |
| 40 /** | 40 /** |
| 41 * The possible optimization states of a function. Must be in sync with the | 41 * The possible optimization states of a function. Must be in sync with the |
| 42 * return values of Runtime_GetOptimizationStatus() in runtime.cc! | 42 * return values of Runtime_GetOptimizationStatus() in runtime.cc! |
| 43 * @enum {int} | 43 * @enum {int} |
| 44 */ | 44 */ |
| 45 OptTracker.OptimizationState = { | 45 OptTracker.OptimizationState = { |
| 46 YES: 1, | 46 YES: 1, |
| 47 NO: 2, | 47 NO: 2, |
| 48 ALWAYS: 3, | 48 ALWAYS: 3, |
| 49 NEVER: 4 | 49 NEVER: 4 |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 /** | 52 /** |
| 53 * Always call this at the beginning of your test, once for each function | 53 * Always call this at the beginning of your test, once for each function |
| 54 * that you later want to track de/optimizations for. It is necessary because | 54 * that you later want to track de/optimizations for. It is necessary because |
| 55 * tests are sometimes executed several times in a row, and you want to | 55 * tests are sometimes executed several times in a row, and you want to |
| 56 * disregard counts from previous runs. | 56 * disregard counts from previous runs. |
| 57 */ | 57 */ |
| 58 OptTracker.prototype.CheckpointOptCount = function(func) { | 58 OptTracker.prototype.CheckpointDeoptCount = function(func) { |
| 59 this.opt_counts_[func] = %GetOptimizationCount(func); | 59 this.deopt_counts_[func] = %GetDeoptimizationCount(func); |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 OptTracker.prototype.AssertOptCount = function(func, optcount) { | 62 OptTracker.prototype.AssertOptCount = function(func, optcount) { |
| 63 if (this.DisableAsserts_(func)) { | 63 if (this.DisableAsserts_(func)) { |
| 64 return; | 64 return; |
| 65 } | 65 } |
| 66 assertEquals(optcount, this.GetOptCount_(func)); | 66 assertEquals(optcount, this.GetOptCount_(func)); |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 OptTracker.prototype.AssertDeoptCount = function(func, deopt_count) { | 69 OptTracker.prototype.AssertDeoptCount = function(func, deopt_count) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 93 assertEquals(OptTracker.OptimizationState.YES, raw_optimized); | 93 assertEquals(OptTracker.OptimizationState.YES, raw_optimized); |
| 94 } else { | 94 } else { |
| 95 assertEquals(OptTracker.OptimizationState.NO, raw_optimized); | 95 assertEquals(OptTracker.OptimizationState.NO, raw_optimized); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 /** | 99 /** |
| 100 * @private | 100 * @private |
| 101 */ | 101 */ |
| 102 OptTracker.prototype.GetOptCount_ = function(func) { | 102 OptTracker.prototype.GetOptCount_ = function(func) { |
| 103 var raw_count = %GetOptimizationCount(func); | 103 var count = this.GetDeoptCount_(func); |
| 104 if (func in this.opt_counts_) { | 104 if (%GetOptimizationStatus(func) == OptTracker.OptimizationState.YES) { |
| 105 var checkpointed_count = this.opt_counts_[func]; | 105 count += 1; |
| 106 } | |
| 107 return count; | |
| 108 } | |
| 109 | |
| 110 | |
| 111 /** | |
| 112 * @private | |
| 113 */ | |
| 114 OptTracker.prototype.GetDeoptCount_ = function(func) { | |
| 115 var raw_count = %GetDeoptimizationCount(func); | |
| 116 if (func in this.deopt_counts_) { | |
| 117 var checkpointed_count = this.deopt_counts_[func]; | |
| 106 return raw_count - checkpointed_count; | 118 return raw_count - checkpointed_count; |
| 107 } | 119 } |
| 108 return raw_count; | 120 return raw_count; |
| 109 } | 121 } |
| 110 | 122 |
| 111 /** | 123 /** |
| 112 * @private | |
| 113 */ | |
| 114 OptTracker.prototype.GetDeoptCount_ = function(func) { | |
| 115 var count = this.GetOptCount_(func); | |
| 116 if (%GetOptimizationStatus(func) == OptTracker.OptimizationState.YES) { | |
| 117 count -= 1; | |
| 118 } | |
| 119 return count; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * @private | 124 * @private |
| 124 */ | 125 */ |
| 125 OptTracker.prototype.DisableAsserts_ = function(func) { | 126 OptTracker.prototype.DisableAsserts_ = function(func) { |
| 126 switch(%GetOptimizationStatus(func)) { | 127 switch(%GetOptimizationStatus(func)) { |
| 127 case OptTracker.OptimizationState.YES: | 128 case OptTracker.OptimizationState.YES: |
| 128 case OptTracker.OptimizationState.NO: | 129 case OptTracker.OptimizationState.NO: |
| 129 return false; | 130 return false; |
| 130 case OptTracker.OptimizationState.ALWAYS: | 131 case OptTracker.OptimizationState.ALWAYS: |
| 131 case OptTracker.OptimizationState.NEVER: | 132 case OptTracker.OptimizationState.NEVER: |
| 132 return true; | 133 return true; |
| 133 } | 134 } |
| 134 return false; | 135 return false; |
| 135 } | 136 } |
| 136 // (End of class OptTracker.) | 137 // (End of class OptTracker.) |
| 137 | 138 |
| 138 // Example function used by the test below. | 139 // Example function used by the test below. |
| 139 function f(a) { | 140 function f(a) { |
| 140 return a+1; | 141 return a+1; |
| 141 } | 142 } |
| 142 | 143 |
| 143 var tracker = new OptTracker(); | 144 var tracker = new OptTracker(); |
| 144 tracker.CheckpointOptCount(f); | 145 tracker.CheckpointDeoptCount(f); |
| 145 | 146 |
| 146 tracker.AssertOptCount(f, 0); | 147 tracker.AssertOptCount(f, 0); |
| 147 tracker.AssertIsOptimized(f, false); | 148 tracker.AssertIsOptimized(f, false); |
| 148 tracker.AssertDeoptHappened(f, false); | 149 tracker.AssertDeoptHappened(f, false); |
| 149 tracker.AssertDeoptCount(f, 0); | 150 tracker.AssertDeoptCount(f, 0); |
| 150 | 151 |
| 151 f(1); | 152 f(1); |
| 152 | 153 |
| 153 %OptimizeFunctionOnNextCall(f); | 154 %OptimizeFunctionOnNextCall(f); |
| 154 f(1); | 155 f(1); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 168 // Let's trigger optimization for another type. | 169 // Let's trigger optimization for another type. |
| 169 for (var i = 0; i < 5; i++) f("a"); | 170 for (var i = 0; i < 5; i++) f("a"); |
| 170 | 171 |
| 171 %OptimizeFunctionOnNextCall(f); | 172 %OptimizeFunctionOnNextCall(f); |
| 172 f("b"); | 173 f("b"); |
| 173 | 174 |
| 174 tracker.AssertOptCount(f, 2); | 175 tracker.AssertOptCount(f, 2); |
| 175 tracker.AssertIsOptimized(f, true); | 176 tracker.AssertIsOptimized(f, true); |
| 176 tracker.AssertDeoptHappened(f, true); | 177 tracker.AssertDeoptHappened(f, true); |
| 177 tracker.AssertDeoptCount(f, 1); | 178 tracker.AssertDeoptCount(f, 1); |
| OLD | NEW |