log-eq-of-logging-and-traversal.js 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Copyright 2011 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// This is a supplementary file for test-log/EquivalenceOfLoggingAndTraversal.

function parseState(s) {
  switch (s) {
  case "": return Profile.CodeState.COMPILED;
  case "~": return Profile.CodeState.OPTIMIZABLE;
  case "*": return Profile.CodeState.OPTIMIZED;
  }
  throw new Error("unknown code state: " + s);
}

function LogProcessor() {
  LogReader.call(this, {
      'code-creation': {
42 43
          parsers: [parseString, parseInt, parseInt, parseInt, parseInt,
                    parseString, parseVarArgs],
44 45 46
          processor: this.processCodeCreation },
      'code-move': { parsers: [parseInt, parseInt],
          processor: this.processCodeMove },
47
      'code-delete': parseString,
48 49
      'sfi-move': { parsers: [parseInt, parseInt],
          processor: this.processFunctionMove },
50 51 52
      'shared-library': parseString,
      'profiler': parseString,
      'tick': parseString });
53 54 55 56 57 58
  this.profile = new Profile();

}
LogProcessor.prototype.__proto__ = LogReader.prototype;

LogProcessor.prototype.processCodeCreation = function(
59 60 61 62
    type, kind, timestamp, start, size, name, maybe_func) {
  if (type != "LazyCompile" && type != "Script" && type != "Function") {
    return;
  }
63 64 65 66
  // Scripts will compile into anonymous functions starting at 1:1. Adjust the
  // name here so that it matches corrsponding function's name during the heap
  // traversal.
  if (type == "Script") name = " :1:1";
67 68 69 70 71
  // Discard types to avoid discrepancies in "LazyCompile" vs. "Function".
  type = "";
  if (maybe_func.length) {
    var funcAddr = parseInt(maybe_func[0]);
    var state = parseState(maybe_func[1]);
72
    this.profile.addFuncCode(type, name, timestamp, start, size, funcAddr, state);
73
  } else {
74
    this.profile.addCode(type, name, timestamp, start, size);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  }
};

LogProcessor.prototype.processCodeMove = function(from, to) {
  this.profile.moveCode(from, to);
};

LogProcessor.prototype.processFunctionMove = function(from, to) {
  this.profile.moveFunc(from, to);
};

function RunTest() {
  // _log must be provided externally.
  var log_lines = _log.split("\n");
  var line, pos = 0, log_lines_length = log_lines.length;
  if (log_lines_length < 2)
    return "log_lines_length < 2";
  var logging_processor = new LogProcessor();
  for ( ; pos < log_lines_length; ++pos) {
    line = log_lines[pos];
    if (line === "test-logging-done,\"\"") {
      ++pos;
      break;
    }
    logging_processor.processLogLine(line);
  }
  logging_processor.profile.cleanUpFuncEntries();
  var logging_entries =
    logging_processor.profile.codeMap_.getAllDynamicEntriesWithAddresses();
  if (logging_entries.length === 0)
    return "logging_entries.length === 0";
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  var traversal_processor = new LogProcessor();
  for ( ; pos < log_lines_length; ++pos) {
    line = log_lines[pos];
    if (line === "test-traversal-done,\"\"") break;
    traversal_processor.processLogLine(line);
  }
  var traversal_entries =
    traversal_processor.profile.codeMap_.getAllDynamicEntriesWithAddresses();
  if (traversal_entries.length === 0)
    return "traversal_entries.length === 0";

  function addressComparator(entryA, entryB) {
    return entryA[0] < entryB[0] ? -1 : (entryA[0] > entryB[0] ? 1 : 0);
  }

  logging_entries.sort(addressComparator);
  traversal_entries.sort(addressComparator);

  function entityNamesEqual(entityA, entityB) {
    if ("getRawName" in entityB &&
        entityNamesEqual.builtins.indexOf(entityB.getRawName()) !== -1) {
      return true;
    }
    if (entityNamesEqual.builtins.indexOf(entityB.getName()) !== -1) return true;
    return entityA.getName() === entityB.getName();
  }
  entityNamesEqual.builtins =
    ["Boolean", "Function", "Number", "Object",
     "Script", "String", "RegExp", "Date", "Error"];

  function entitiesEqual(entityA, entityB) {
138 139
    if ((entityA === null && entityB !== null) ||
      (entityA !== null && entityB === null)) return true;
140 141 142
    return entityA.size === entityB.size && entityNamesEqual(entityA, entityB);
  }

143 144
  var l_pos = 0, t_pos = 0;
  var l_len = logging_entries.length, t_len = traversal_entries.length;
145 146 147 148 149 150
  var comparison = [];
  var equal = true;
  // Do a merge-like comparison of entries. At the same address we expect to
  // find the same entries. We skip builtins during log parsing, but compiled
  // functions traversal may erroneously recognize them as functions, so we are
  // expecting more functions in traversal vs. logging.
151 152
  // Since we don't track code deletions, logging can also report more entries
  // than traversal.
153 154 155
  while (l_pos < l_len && t_pos < t_len) {
    var entryA = logging_entries[l_pos];
    var entryB = traversal_entries[t_pos];
156 157 158 159
    var cmp = addressComparator(entryA, entryB);
    var entityA = entryA[1], entityB = entryB[1];
    var address = entryA[0];
    if (cmp < 0) {
160
      ++l_pos;
161 162
      entityB = null;
    } else if (cmp > 0) {
163
      ++t_pos;
164 165 166
      entityA = null;
      address = entryB[0];
    } else {
167 168
      ++l_pos;
      ++t_pos;
169 170 171 172 173 174 175 176
    }
    var entities_equal = entitiesEqual(entityA, entityB);
    if (!entities_equal) equal = false;
    comparison.push([entities_equal, address, entityA, entityB]);
  }
  return [equal, comparison];
}

177

178 179 180 181 182 183 184 185 186 187 188 189
var result = RunTest();
if (typeof result !== "string") {
  var out = [];
  if (!result[0]) {
    var comparison = result[1];
    for (var i = 0, l = comparison.length; i < l; ++i) {
      var c = comparison[i];
      out.push((c[0] ? "  " : "* ") +
               c[1].toString(16) + " " +
               (c[2] ? c[2] : "---") + " " +
               (c[3] ? c[3] : "---"));
    }
190 191 192 193 194 195 196
    out.push("================================================")
    out.push("MAKE SURE TO USE A CLEAN ISOLATiE!");
    out.push("Use tools/test.py");
    out.push("================================================")
    out.push("*   Lines are the same");
    out.push("--- Line is missing"
    out.push("================================================")
197 198 199 200 201
  }
  result[0] ? true : out.join("\n");
} else {
  result;
}