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 30/** 31 * This class shows how to use %GetOptimizationCount() and 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 34 * the --allow-natives-syntax flag so far. 35 */ 36function OptTracker() { 37 this.opt_counts_ = {}; 38} 39 40/** 41 * The possible optimization states of a function. Must be in sync with the 42 * return values of Runtime_GetOptimizationStatus() in runtime.cc! 43 * @enum {int} 44 */ 45OptTracker.OptimizationState = { 46 YES: 1, 47 NO: 2, 48 ALWAYS: 3, 49 NEVER: 4 50}; 51 52/** 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 55 * tests are sometimes executed several times in a row, and you want to 56 * disregard counts from previous runs. 57 */ 58OptTracker.prototype.CheckpointOptCount = function(func) { 59 this.opt_counts_[func] = %GetOptimizationCount(func); 60}; 61 62OptTracker.prototype.AssertOptCount = function(func, optcount) { 63 if (this.DisableAsserts_(func)) { 64 return; 65 } 66 assertEquals(optcount, this.GetOptCount_(func)); 67}; 68 69OptTracker.prototype.AssertDeoptCount = function(func, deopt_count) { 70 if (this.DisableAsserts_(func)) { 71 return; 72 } 73 assertEquals(deopt_count, this.GetDeoptCount_(func)); 74}; 75 76OptTracker.prototype.AssertDeoptHappened = function(func, expect_deopt) { 77 if (this.DisableAsserts_(func)) { 78 return; 79 } 80 if (expect_deopt) { 81 assertTrue(this.GetDeoptCount_(func) > 0); 82 } else { 83 assertEquals(0, this.GetDeoptCount_(func)); 84 } 85} 86 87OptTracker.prototype.AssertIsOptimized = function(func, expect_optimized) { 88 if (this.DisableAsserts_(func)) { 89 return; 90 } 91 var raw_optimized = %GetOptimizationStatus(func); 92 if (expect_optimized) { 93 assertEquals(OptTracker.OptimizationState.YES, raw_optimized); 94 } else { 95 assertEquals(OptTracker.OptimizationState.NO, raw_optimized); 96 } 97} 98 99/** 100 * @private 101 */ 102OptTracker.prototype.GetOptCount_ = function(func) { 103 var raw_count = %GetOptimizationCount(func); 104 if (func in this.opt_counts_) { 105 var checkpointed_count = this.opt_counts_[func]; 106 return raw_count - checkpointed_count; 107 } 108 return raw_count; 109} 110 111/** 112 * @private 113 */ 114OptTracker.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 */ 125OptTracker.prototype.DisableAsserts_ = function(func) { 126 switch(%GetOptimizationStatus(func)) { 127 case OptTracker.OptimizationState.YES: 128 case OptTracker.OptimizationState.NO: 129 return false; 130 case OptTracker.OptimizationState.ALWAYS: 131 case OptTracker.OptimizationState.NEVER: 132 return true; 133 } 134 return false; 135} 136// (End of class OptTracker.) 137 138// Example function used by the test below. 139function f(a) { 140 return a+1; 141} 142 143var tracker = new OptTracker(); 144tracker.CheckpointOptCount(f); 145 146tracker.AssertOptCount(f, 0); 147tracker.AssertIsOptimized(f, false); 148tracker.AssertDeoptHappened(f, false); 149tracker.AssertDeoptCount(f, 0); 150 151f(1); 152 153%OptimizeFunctionOnNextCall(f); 154f(1); 155 156tracker.AssertOptCount(f, 1); 157tracker.AssertIsOptimized(f, true); 158tracker.AssertDeoptHappened(f, false); 159tracker.AssertDeoptCount(f, 0); 160 161%DeoptimizeFunction(f); 162 163tracker.AssertOptCount(f, 1); 164tracker.AssertIsOptimized(f, false); 165tracker.AssertDeoptHappened(f, true); 166tracker.AssertDeoptCount(f, 1); 167 168// Let's trigger optimization for another type. 169for (var i = 0; i < 5; i++) f("a"); 170 171%OptimizeFunctionOnNextCall(f); 172f("b"); 173 174tracker.AssertOptCount(f, 2); 175tracker.AssertIsOptimized(f, true); 176tracker.AssertDeoptHappened(f, true); 177tracker.AssertDeoptCount(f, 1); 178