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
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.
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.
28 // Flags: --allow-natives-syntax
29 // Flags: --noconcurrent-recompilation --noconcurrent-osr
31 if (%IsConcurrentRecompilationSupported()) {
32 print("Concurrent recompilation is turned on after all. Skipping this test.");
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.
42 function OptTracker() {
43 this.opt_counts_ = {};
47 * The possible optimization states of a function. Must be in sync with the
48 * return values of Runtime_GetOptimizationStatus() in runtime.cc!
51 OptTracker.OptimizationState = {
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.
64 OptTracker.prototype.CheckpointOptCount = function(func) {
65 this.opt_counts_[func] = %GetOptimizationCount(func);
68 OptTracker.prototype.AssertOptCount = function(func, optcount) {
69 if (this.DisableAsserts_(func)) {
72 assertEquals(optcount, this.GetOptCount_(func));
75 OptTracker.prototype.AssertDeoptCount = function(func, deopt_count) {
76 if (this.DisableAsserts_(func)) {
79 assertEquals(deopt_count, this.GetDeoptCount_(func));
82 OptTracker.prototype.AssertDeoptHappened = function(func, expect_deopt) {
83 if (this.DisableAsserts_(func)) {
87 assertTrue(this.GetDeoptCount_(func) > 0);
89 assertEquals(0, this.GetDeoptCount_(func));
93 OptTracker.prototype.AssertIsOptimized = function(func, expect_optimized) {
94 if (this.DisableAsserts_(func)) {
97 var raw_optimized = %GetOptimizationStatus(func);
98 if (expect_optimized) {
99 assertEquals(OptTracker.OptimizationState.YES, raw_optimized);
101 assertEquals(OptTracker.OptimizationState.NO, raw_optimized);
108 OptTracker.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;
120 OptTracker.prototype.GetDeoptCount_ = function(func) {
121 var count = this.GetOptCount_(func);
122 if (%GetOptimizationStatus(func) == OptTracker.OptimizationState.YES) {
131 OptTracker.prototype.DisableAsserts_ = function(func) {
132 switch(%GetOptimizationStatus(func)) {
133 case OptTracker.OptimizationState.YES:
134 case OptTracker.OptimizationState.NO:
136 case OptTracker.OptimizationState.ALWAYS:
137 case OptTracker.OptimizationState.NEVER:
142 // (End of class OptTracker.)
144 // Example function used by the test below.
149 var tracker = new OptTracker();
150 tracker.CheckpointOptCount(f);
152 tracker.AssertOptCount(f, 0);
153 tracker.AssertIsOptimized(f, false);
154 tracker.AssertDeoptHappened(f, false);
155 tracker.AssertDeoptCount(f, 0);
159 %OptimizeFunctionOnNextCall(f);
162 tracker.AssertOptCount(f, 1);
163 tracker.AssertIsOptimized(f, true);
164 tracker.AssertDeoptHappened(f, false);
165 tracker.AssertDeoptCount(f, 0);
167 %DeoptimizeFunction(f);
169 tracker.AssertOptCount(f, 1);
170 tracker.AssertIsOptimized(f, false);
171 tracker.AssertDeoptHappened(f, true);
172 tracker.AssertDeoptCount(f, 1);
174 // Let's trigger optimization for another type.
175 for (var i = 0; i < 5; i++) f("a");
177 %OptimizeFunctionOnNextCall(f);
180 tracker.AssertOptCount(f, 2);
181 tracker.AssertIsOptimized(f, true);
182 tracker.AssertDeoptHappened(f, true);
183 tracker.AssertDeoptCount(f, 1);