OLD | NEW |
---|---|
(Empty) | |
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 // This class shows how to use %GetOptimizationCount() and | |
31 // %IsOptimizedFunction() to infer information about opts and deopts. | |
32 // Might be nice to put this into mjsunit.js, but that doesn't depend on | |
33 // the --allow-natives-syntax flag so far. | |
34 function OptTracker() { | |
35 this.YES = 1; | |
36 this.NO = 2; | |
37 this.ALWAYS = 3; | |
38 this.NEVER = 4; | |
Mike West
2011/05/04 08:18:03
Nit: These can be pulled out into a static member
Jakob Kummerow
2011/05/06 15:58:15
Done.
| |
39 this.opt_counts_ = new Object(); | |
Mike West
2011/05/04 08:18:03
Nit: Object literals (`{}`) are preferred.
Jakob Kummerow
2011/05/06 15:58:15
Done.
| |
40 } | |
41 | |
42 // Always call this at the beginning of your test, once for each function | |
43 // that you later want to track de/optimizations for. It is necessary because | |
44 // tests are sometimes executed several times in a row, and you want to | |
45 // disregard counts from previous runs. | |
46 OptTracker.prototype.CheckpointOptCount = function(func) { | |
47 this.opt_counts_[func] = %GetOptimizationCount(func); | |
48 } | |
49 | |
50 OptTracker.prototype.AssertOptCount = function(func, optcount) { | |
51 if (this.DisableAsserts_(func)) { | |
52 return; | |
Mike West
2011/05/04 08:18:03
Nit: If asserts are disabled, you should probably
Jakob Kummerow
2011/05/06 15:58:15
This function doesn't have a return value in any c
| |
53 } | |
54 assertEquals(optcount, this.GetOptCount_(func)); | |
55 } | |
56 | |
57 OptTracker.prototype.AssertDeoptCount = function(func, deopt_count) { | |
58 if (this.DisableAsserts_(func)) { | |
59 return; | |
60 } | |
61 assertEquals(deopt_count, this.GetDeoptCount_(func)); | |
62 } | |
63 | |
64 OptTracker.prototype.AssertDeoptHappened = function(func, expect_deopt) { | |
65 if (this.DisableAsserts_(func)) { | |
66 return; | |
67 } | |
68 if (expect_deopt) { | |
69 assertTrue(this.GetDeoptCount_(func) > 0); | |
70 return; | |
71 } | |
72 assertEquals(0, this.GetDeoptCount_(func)); | |
73 } | |
74 | |
75 OptTracker.prototype.AssertIsOptimized = function(func, expect_optimized) { | |
76 var raw_optimized = %IsOptimizedFunction(func); | |
77 if (raw_optimized == this.ALWAYS || | |
Mike West
2011/05/04 08:18:03
Nit: Use `DisableAsserts_`.
Jakob Kummerow
2011/05/06 15:58:15
Done.
| |
78 raw_optimized == this.NEVER) { | |
79 return; | |
80 } | |
81 if (expect_optimized) { | |
82 assertEquals(this.YES, raw_optimized); | |
83 } else { | |
84 assertEquals(this.NO, raw_optimized); | |
85 } | |
86 } | |
87 | |
88 // "private" methods. Do not call these from the outside. | |
89 | |
90 OptTracker.prototype.GetOptCount_ = function(func) { | |
91 var raw_count = %GetOptimizationCount(func); | |
92 if (func in this.opt_counts_) { | |
93 var checkpointed_count = this.opt_counts_[func]; | |
94 return raw_count - checkpointed_count; | |
95 } | |
96 return raw_count; | |
97 } | |
98 | |
99 OptTracker.prototype.GetDeoptCount_ = function(func) { | |
100 var count = this.GetOptCount_(func); | |
101 if (%IsOptimizedFunction(func) == this.YES) { | |
102 count -= 1; | |
103 } | |
104 return count; | |
105 } | |
106 | |
107 OptTracker.prototype.DisableAsserts_ = function(func) { | |
108 switch(%IsOptimizedFunction(func)) { | |
109 case this.YES: | |
110 case this.NO: | |
111 return false; | |
112 case this.ALWAYS: | |
113 case this.NEVER: | |
114 return true; | |
115 } | |
116 return false; | |
117 } | |
118 | |
119 | |
120 // Example function used by the test below. | |
121 function f(a) { | |
122 return a+1; | |
123 } | |
124 | |
125 var tracker = new OptTracker(); | |
126 tracker.CheckpointOptCount(f); | |
127 | |
128 tracker.AssertOptCount(f, 0); | |
129 tracker.AssertIsOptimized(f, false); | |
130 tracker.AssertDeoptHappened(f, false); | |
131 tracker.AssertDeoptCount(f, 0); | |
132 | |
133 for (var i = 0; i < 5; i++) f(1); | |
134 | |
135 tracker.AssertOptCount(f, 0); | |
136 tracker.AssertIsOptimized(f, false); | |
137 tracker.AssertDeoptHappened(f, false); | |
138 tracker.AssertDeoptCount(f, 0); | |
139 | |
140 %OptimizeFunctionOnNextCall(f); | |
141 f(1); | |
142 | |
143 tracker.AssertOptCount(f, 1); | |
144 tracker.AssertIsOptimized(f, true); | |
145 tracker.AssertDeoptHappened(f, false); | |
146 tracker.AssertDeoptCount(f, 0); | |
147 | |
148 %DeoptimizeFunction(f); | |
149 | |
150 tracker.AssertOptCount(f, 1); | |
151 tracker.AssertIsOptimized(f, false); | |
152 tracker.AssertDeoptHappened(f, true); | |
153 tracker.AssertDeoptCount(f, 1); | |
154 | |
155 for (var i = 0; i < 5; i++) f("a"); | |
Mike West
2011/05/04 08:18:03
Nit: You're intentionally switching from numbers t
Jakob Kummerow
2011/05/06 15:58:15
Done.
| |
156 %OptimizeFunctionOnNextCall(f); | |
157 f("b"); | |
158 | |
159 tracker.AssertOptCount(f, 2); | |
160 tracker.AssertIsOptimized(f, true); | |
161 tracker.AssertDeoptHappened(f, true); | |
162 tracker.AssertDeoptCount(f, 1); | |
OLD | NEW |