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