blob: cd52da545a6d9ae24d69c61b52aa28349769cfbd [file] [log] [blame]
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001// 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// This is a supplementary file for test-log/EquivalenceOfLoggingAndTraversal.
29
30function parseState(s) {
31 switch (s) {
32 case "": return Profile.CodeState.COMPILED;
33 case "~": return Profile.CodeState.OPTIMIZABLE;
34 case "*": return Profile.CodeState.OPTIMIZED;
35 }
36 throw new Error("unknown code state: " + s);
37}
38
39function LogProcessor() {
40 LogReader.call(this, {
41 'code-creation': {
42 parsers: [null, parseInt, parseInt, null, 'var-args'],
43 processor: this.processCodeCreation },
44 'code-move': { parsers: [parseInt, parseInt],
45 processor: this.processCodeMove },
46 'code-delete': { parsers: [parseInt],
47 processor: this.processCodeDelete },
48 'sfi-move': { parsers: [parseInt, parseInt],
49 processor: this.processFunctionMove },
50 'shared-library': null,
51 'profiler': null,
52 'tick': null });
53 this.profile = new Profile();
54
55}
56LogProcessor.prototype.__proto__ = LogReader.prototype;
57
58LogProcessor.prototype.processCodeCreation = function(
59 type, start, size, name, maybe_func) {
60 if (type != "LazyCompile" && type != "Script" && type != "Function") return;
61 // Discard types to avoid discrepancies in "LazyCompile" vs. "Function".
62 type = "";
63 if (maybe_func.length) {
64 var funcAddr = parseInt(maybe_func[0]);
65 var state = parseState(maybe_func[1]);
66 this.profile.addFuncCode(type, name, start, size, funcAddr, state);
67 } else {
68 this.profile.addCode(type, name, start, size);
69 }
70};
71
72LogProcessor.prototype.processCodeMove = function(from, to) {
73 this.profile.moveCode(from, to);
74};
75
76LogProcessor.prototype.processCodeDelete = function(start) {
77 this.profile.deleteCode(start);
78};
79
80LogProcessor.prototype.processFunctionMove = function(from, to) {
81 this.profile.moveFunc(from, to);
82};
83
84function RunTest() {
85 // _log must be provided externally.
86 var log_lines = _log.split("\n");
87 var line, pos = 0, log_lines_length = log_lines.length;
88 if (log_lines_length < 2)
89 return "log_lines_length < 2";
90 var logging_processor = new LogProcessor();
91 for ( ; pos < log_lines_length; ++pos) {
92 line = log_lines[pos];
93 if (line === "test-logging-done,\"\"") {
94 ++pos;
95 break;
96 }
97 logging_processor.processLogLine(line);
98 }
99 logging_processor.profile.cleanUpFuncEntries();
100 var logging_entries =
101 logging_processor.profile.codeMap_.getAllDynamicEntriesWithAddresses();
102 if (logging_entries.length === 0)
103 return "logging_entries.length === 0";
104 var traversal_processor = new LogProcessor();
105 for ( ; pos < log_lines_length; ++pos) {
106 line = log_lines[pos];
107 if (line === "test-traversal-done,\"\"") break;
108 traversal_processor.processLogLine(line);
109 }
110 var traversal_entries =
111 traversal_processor.profile.codeMap_.getAllDynamicEntriesWithAddresses();
112 if (traversal_entries.length === 0)
113 return "traversal_entries.length === 0";
114
115 function addressComparator(entryA, entryB) {
116 return entryA[0] < entryB[0] ? -1 : (entryA[0] > entryB[0] ? 1 : 0);
117 }
118
119 logging_entries.sort(addressComparator);
120 traversal_entries.sort(addressComparator);
121
122 function entityNamesEqual(entityA, entityB) {
123 if ("getRawName" in entityB &&
124 entityNamesEqual.builtins.indexOf(entityB.getRawName()) !== -1) {
125 return true;
126 }
127 if (entityNamesEqual.builtins.indexOf(entityB.getName()) !== -1) return true;
128 return entityA.getName() === entityB.getName();
129 }
130 entityNamesEqual.builtins =
131 ["Boolean", "Function", "Number", "Object",
132 "Script", "String", "RegExp", "Date", "Error"];
133
134 function entitiesEqual(entityA, entityB) {
135 if (entityA === null && entityB !== null) return true;
136 if (entityA !== null && entityB === null) return false;
137 return entityA.size === entityB.size && entityNamesEqual(entityA, entityB);
138 }
139
140 var l_pos = 0, t_pos = 0;
141 var l_len = logging_entries.length, t_len = traversal_entries.length;
142 var comparison = [];
143 var equal = true;
144 // Do a merge-like comparison of entries. At the same address we expect to
145 // find the same entries. We skip builtins during log parsing, but compiled
146 // functions traversal may erroneously recognize them as functions, so we are
147 // expecting more functions in traversal vs. logging.
148 while (l_pos < l_len && t_pos < t_len) {
149 var entryA = logging_entries[l_pos];
150 var entryB = traversal_entries[t_pos];
151 var cmp = addressComparator(entryA, entryB);
152 var entityA = entryA[1], entityB = entryB[1];
153 var address = entryA[0];
154 if (cmp < 0) {
155 ++l_pos;
156 entityB = null;
157 } else if (cmp > 0) {
158 ++t_pos;
159 entityA = null;
160 address = entryB[0];
161 } else {
162 ++l_pos;
163 ++t_pos;
164 }
165 var entities_equal = entitiesEqual(entityA, entityB);
166 if (!entities_equal) equal = false;
167 comparison.push([entities_equal, address, entityA, entityB]);
168 }
169 if (l_pos < l_len) equal = false;
170 while (l_pos < l_len) {
171 var entryA = logging_entries[l_pos++];
172 comparison.push([false, entryA[0], entryA[1], null]);
173 }
174 return [equal, comparison];
175}
176
177var result = RunTest();
178if (typeof result !== "string") {
179 var out = [];
180 if (!result[0]) {
181 var comparison = result[1];
182 for (var i = 0, l = comparison.length; i < l; ++i) {
183 var c = comparison[i];
184 out.push((c[0] ? " " : "* ") +
185 c[1].toString(16) + " " +
186 (c[2] ? c[2] : "---") + " " +
187 (c[3] ? c[3] : "---"));
188 }
189 }
190 result[0] ? true : out.join("\n");
191} else {
192 result;
193}