1// Copyright 2011 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Flags: --allow-natives-syntax 29// Flags: --noconcurrent-recompilation --noconcurrent-osr 30 31if (%IsConcurrentRecompilationSupported()) { 32 print("Concurrent recompilation is turned on after all. Skipping this test."); 33 quit(); 34} 35 36/** 37 * This class shows how to use %GetOptimizationCount() and 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 40 * the --allow-natives-syntax flag so far. 41 */ 42function OptTracker() { 43 this.opt_counts_ = {}; 44} 45 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 */ 51OptTracker.OptimizationState = { 52 YES: 1, 53 NO: 2, 54 ALWAYS: 3, 55 NEVER: 4 56}; 57 58/** 59 * Always call this at the beginning of your test, once for each function 60 * that you later want to track de/optimizations for. It is necessary because 61 * tests are sometimes executed several times in a row, and you want to 62 * disregard counts from previous runs. 63 */ 64OptTracker.prototype.CheckpointOptCount = function(func) { 65 this.opt_counts_[func] = %GetOptimizationCount(func); 66}; 67 68OptTracker.prototype.AssertOptCount = function(func, optcount) { 69 if (this.DisableAsserts_(func)) { 70 return; 71 } 72 assertEquals(optcount, this.GetOptCount_(func)); 73}; 74 75OptTracker.prototype.AssertDeoptCount = function(func, deopt_count) { 76 if (this.DisableAsserts_(func)) { 77 return; 78 } 79 assertEquals(deopt_count, this.GetDeoptCount_(func)); 80}; 81 82OptTracker.prototype.AssertDeoptHappened = function(func, expect_deopt) { 83 if (this.DisableAsserts_(func)) { 84 return; 85 } 86 if (expect_deopt) { 87 assertTrue(this.GetDeoptCount_(func) > 0); 88 } else { 89 assertEquals(0, this.GetDeoptCount_(func)); 90 } 91} 92 93OptTracker.prototype.AssertIsOptimized = function(func, expect_optimized) { 94 if (this.DisableAsserts_(func)) { 95 return; 96 } 97 var raw_optimized = %GetOptimizationStatus(func); 98 if (expect_optimized) { 99 assertEquals(OptTracker.OptimizationState.YES, raw_optimized); 100 } else { 101 assertEquals(OptTracker.OptimizationState.NO, raw_optimized); 102 } 103} 104 105/** 106 * @private 107 */ 108OptTracker.prototype.GetOptCount_ = function(func) { 109 var raw_count = %GetOptimizationCount(func); 110 if (func in this.opt_counts_) { 111 var checkpointed_count = this.opt_counts_[func]; 112 return raw_count - checkpointed_count; 113 } 114 return raw_count; 115} 116 117/** 118 * @private 119 */ 120OptTracker.prototype.GetDeoptCount_ = function(func) { 121 var count = this.GetOptCount_(func); 122 if (%GetOptimizationStatus(func) == OptTracker.OptimizationState.YES) { 123 count -= 1; 124 } 125 return count; 126} 127 128/** 129 * @private 130 */ 131OptTracker.prototype.DisableAsserts_ = function(func) { 132 switch(%GetOptimizationStatus(func)) { 133 case OptTracker.OptimizationState.YES: 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; 141} 142// (End of class OptTracker.) 143 144// Example function used by the test below. 145function f(a) { 146 return a+1; 147} 148 149var tracker = new OptTracker(); 150tracker.CheckpointOptCount(f); 151 152tracker.AssertOptCount(f, 0); 153tracker.AssertIsOptimized(f, false); 154tracker.AssertDeoptHappened(f, false); 155tracker.AssertDeoptCount(f, 0); 156 157f(1); 158 159%OptimizeFunctionOnNextCall(f); 160f(1); 161 162tracker.AssertOptCount(f, 1); 163tracker.AssertIsOptimized(f, true); 164tracker.AssertDeoptHappened(f, false); 165tracker.AssertDeoptCount(f, 0); 166 167%DeoptimizeFunction(f); 168 169tracker.AssertOptCount(f, 1); 170tracker.AssertIsOptimized(f, false); 171tracker.AssertDeoptHappened(f, true); 172tracker.AssertDeoptCount(f, 1); 173 174// Let's trigger optimization for another type. 175for (var i = 0; i < 5; i++) f("a"); 176 177%OptimizeFunctionOnNextCall(f); 178f("b"); 179 180tracker.AssertOptCount(f, 2); 181tracker.AssertIsOptimized(f, true); 182tracker.AssertDeoptHappened(f, true); 183tracker.AssertDeoptCount(f, 1); 184