| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 * | 39 * |
| 40 * @param {Array.<Object>} dispatchTable A table used for parsing and processing | 40 * @param {Array.<Object>} dispatchTable A table used for parsing and processing |
| 41 * log records. | 41 * log records. |
| 42 * @constructor | 42 * @constructor |
| 43 */ | 43 */ |
| 44 devtools.profiler.LogReader = function(dispatchTable) { | 44 devtools.profiler.LogReader = function(dispatchTable) { |
| 45 /** | 45 /** |
| 46 * @type {Array.<Object>} | 46 * @type {Array.<Object>} |
| 47 */ | 47 */ |
| 48 this.dispatchTable_ = dispatchTable; | 48 this.dispatchTable_ = dispatchTable; |
| 49 this.dispatchTable_['alias'] = |
| 50 { parsers: [null, null], processor: this.processAlias_ }; |
| 51 this.dispatchTable_['repeat'] = |
| 52 { parsers: [parseInt, 'var-args'], processor: this.processRepeat_, |
| 53 backrefs: true }; |
| 54 |
| 55 /** |
| 56 * A key-value map for aliases. Translates short name -> full name. |
| 57 * @type {Object} |
| 58 */ |
| 59 this.aliases_ = {}; |
| 60 |
| 61 /** |
| 62 * A key-value map for previous address values. |
| 63 * @type {Object} |
| 64 */ |
| 65 this.prevAddresses_ = {}; |
| 66 |
| 67 /** |
| 68 * A key-value map for events than can be backreference-compressed. |
| 69 * @type {Object} |
| 70 */ |
| 71 this.backRefsCommands_ = {}; |
| 72 this.initBackRefsCommands_(); |
| 73 |
| 74 /** |
| 75 * Back references for decompression. |
| 76 * @type {Array.<string>} |
| 77 */ |
| 78 this.backRefs_ = []; |
| 49 | 79 |
| 50 /** | 80 /** |
| 51 * Current line. | 81 * Current line. |
| 52 * @type {number} | 82 * @type {number} |
| 53 */ | 83 */ |
| 54 this.lineNum_ = 0; | 84 this.lineNum_ = 0; |
| 55 | 85 |
| 56 /** | 86 /** |
| 57 * CSV lines parser. | 87 * CSV lines parser. |
| 58 * @type {devtools.profiler.CsvParser} | 88 * @type {devtools.profiler.CsvParser} |
| 59 */ | 89 */ |
| 60 this.csvParser_ = new devtools.profiler.CsvParser(); | 90 this.csvParser_ = new devtools.profiler.CsvParser(); |
| 61 }; | 91 }; |
| 62 | 92 |
| 63 | 93 |
| 64 /** | 94 /** |
| 95 * Creates a parser for an address entry. |
| 96 * |
| 97 * @param {string} addressTag Address tag to perform offset decoding. |
| 98 * @return {function(string):number} Address parser. |
| 99 */ |
| 100 devtools.profiler.LogReader.prototype.createAddressParser = function( |
| 101 addressTag) { |
| 102 var self = this; |
| 103 return (function (str) { |
| 104 var value = parseInt(str, 16); |
| 105 var firstChar = str.charAt(0); |
| 106 if (firstChar == '+' || firstChar == '-') { |
| 107 var addr = self.prevAddresses_[addressTag]; |
| 108 addr += value; |
| 109 self.prevAddresses_[addressTag] = addr; |
| 110 return addr; |
| 111 } else if (firstChar != '0' || str.charAt(1) != 'x') { |
| 112 self.prevAddresses_[addressTag] = value; |
| 113 } |
| 114 return value; |
| 115 }); |
| 116 }; |
| 117 |
| 118 |
| 119 /** |
| 120 * Expands an alias symbol, if applicable. |
| 121 * |
| 122 * @param {string} symbol Symbol to expand. |
| 123 * @return {string} Expanded symbol, or the input symbol itself. |
| 124 */ |
| 125 devtools.profiler.LogReader.prototype.expandAlias = function(symbol) { |
| 126 return symbol in this.aliases_ ? this.aliases_[symbol] : symbol; |
| 127 }; |
| 128 |
| 129 |
| 130 /** |
| 65 * Used for printing error messages. | 131 * Used for printing error messages. |
| 66 * | 132 * |
| 67 * @param {string} str Error message. | 133 * @param {string} str Error message. |
| 68 */ | 134 */ |
| 69 devtools.profiler.LogReader.prototype.printError = function(str) { | 135 devtools.profiler.LogReader.prototype.printError = function(str) { |
| 70 // Do nothing. | 136 // Do nothing. |
| 71 }; | 137 }; |
| 72 | 138 |
| 73 | 139 |
| 74 /** | 140 /** |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 break; | 227 break; |
| 162 } | 228 } |
| 163 } | 229 } |
| 164 | 230 |
| 165 // Run the processor. | 231 // Run the processor. |
| 166 dispatch.processor.apply(this, parsedFields); | 232 dispatch.processor.apply(this, parsedFields); |
| 167 }; | 233 }; |
| 168 | 234 |
| 169 | 235 |
| 170 /** | 236 /** |
| 237 * Decompresses a line if it was backreference-compressed. |
| 238 * |
| 239 * @param {string} line Possibly compressed line. |
| 240 * @return {string} Decompressed line. |
| 241 * @private |
| 242 */ |
| 243 devtools.profiler.LogReader.prototype.expandBackRef_ = function(line) { |
| 244 var backRefPos; |
| 245 // Filter out case when a regexp is created containing '#'. |
| 246 if (line.charAt(line.length - 1) != '"' |
| 247 && (backRefPos = line.lastIndexOf('#')) != -1) { |
| 248 var backRef = line.substr(backRefPos + 1); |
| 249 var backRefIdx = parseInt(backRef, 10) - 1; |
| 250 var colonPos = backRef.indexOf(':'); |
| 251 var backRefStart = |
| 252 colonPos != -1 ? parseInt(backRef.substr(colonPos + 1), 10) : 0; |
| 253 line = line.substr(0, backRefPos) + |
| 254 this.backRefs_[backRefIdx].substr(backRefStart); |
| 255 } |
| 256 this.backRefs_.unshift(line); |
| 257 if (this.backRefs_.length > 10) { |
| 258 this.backRefs_.length = 10; |
| 259 } |
| 260 return line; |
| 261 }; |
| 262 |
| 263 |
| 264 /** |
| 265 * Initializes the map of backward reference compressible commands. |
| 266 * @private |
| 267 */ |
| 268 devtools.profiler.LogReader.prototype.initBackRefsCommands_ = function() { |
| 269 for (var event in this.dispatchTable_) { |
| 270 var dispatch = this.dispatchTable_[event]; |
| 271 if (dispatch && dispatch.backrefs) { |
| 272 this.backRefsCommands_[event] = true; |
| 273 } |
| 274 } |
| 275 }; |
| 276 |
| 277 |
| 278 /** |
| 279 * Processes alias log record. Adds an alias to a corresponding map. |
| 280 * |
| 281 * @param {string} symbol Short name. |
| 282 * @param {string} expansion Long name. |
| 283 * @private |
| 284 */ |
| 285 devtools.profiler.LogReader.prototype.processAlias_ = function( |
| 286 symbol, expansion) { |
| 287 if (expansion in this.dispatchTable_) { |
| 288 this.dispatchTable_[symbol] = this.dispatchTable_[expansion]; |
| 289 if (expansion in this.backRefsCommands_) { |
| 290 this.backRefsCommands_[symbol] = true; |
| 291 } |
| 292 } else { |
| 293 this.aliases_[symbol] = expansion; |
| 294 } |
| 295 }; |
| 296 |
| 297 |
| 298 /** |
| 171 * Processes log lines. | 299 * Processes log lines. |
| 172 * | 300 * |
| 173 * @param {Array.<string>} lines Log lines. | 301 * @param {Array.<string>} lines Log lines. |
| 174 * @private | 302 * @private |
| 175 */ | 303 */ |
| 176 devtools.profiler.LogReader.prototype.processLog_ = function(lines) { | 304 devtools.profiler.LogReader.prototype.processLog_ = function(lines) { |
| 177 for (var i = 0, n = lines.length; i < n; ++i, ++this.lineNum_) { | 305 for (var i = 0, n = lines.length; i < n; ++i, ++this.lineNum_) { |
| 178 var line = lines[i]; | 306 var line = lines[i]; |
| 179 if (!line) { | 307 if (!line) { |
| 180 continue; | 308 continue; |
| 181 } | 309 } |
| 182 try { | 310 try { |
| 311 if (line.charAt(0) == '#' || |
| 312 line.substr(0, line.indexOf(',')) in this.backRefsCommands_) { |
| 313 line = this.expandBackRef_(line); |
| 314 } |
| 183 var fields = this.csvParser_.parseLine(line); | 315 var fields = this.csvParser_.parseLine(line); |
| 184 this.dispatchLogRow_(fields); | 316 this.dispatchLogRow_(fields); |
| 185 } catch (e) { | 317 } catch (e) { |
| 186 this.printError('line ' + (this.lineNum_ + 1) + ': ' + (e.message || e)); | 318 this.printError('line ' + (this.lineNum_ + 1) + ': ' + (e.message || e)); |
| 187 } | 319 } |
| 188 } | 320 } |
| 189 }; | 321 }; |
| 322 |
| 323 |
| 324 /** |
| 325 * Processes repeat log record. Expands it according to calls count and |
| 326 * invokes processing. |
| 327 * |
| 328 * @param {number} count Count. |
| 329 * @param {Array.<string>} cmd Parsed command. |
| 330 * @private |
| 331 */ |
| 332 devtools.profiler.LogReader.prototype.processRepeat_ = function(count, cmd) { |
| 333 // Replace the repeat-prefixed command from backrefs list with a non-prefixed. |
| 334 this.backRefs_[0] = cmd.join(','); |
| 335 for (var i = 0; i < count; ++i) { |
| 336 this.dispatchLogRow_(cmd); |
| 337 } |
| 338 }; |
| OLD | NEW |