| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 | 28 |
| 29 // Initlialize namespaces | |
| 30 var devtools = devtools || {}; | |
| 31 devtools.profiler = devtools.profiler || {}; | |
| 32 | |
| 33 | |
| 34 /** | 29 /** |
| 35 * Constructs a mapper that maps addresses into code entries. | 30 * Constructs a mapper that maps addresses into code entries. |
| 36 * | 31 * |
| 37 * @constructor | 32 * @constructor |
| 38 */ | 33 */ |
| 39 devtools.profiler.CodeMap = function() { | 34 function CodeMap() { |
| 40 /** | 35 /** |
| 41 * Dynamic code entries. Used for JIT compiled code. | 36 * Dynamic code entries. Used for JIT compiled code. |
| 42 */ | 37 */ |
| 43 this.dynamics_ = new goog.structs.SplayTree(); | 38 this.dynamics_ = new SplayTree(); |
| 44 | 39 |
| 45 /** | 40 /** |
| 46 * Name generator for entries having duplicate names. | 41 * Name generator for entries having duplicate names. |
| 47 */ | 42 */ |
| 48 this.dynamicsNameGen_ = new devtools.profiler.CodeMap.NameGenerator(); | 43 this.dynamicsNameGen_ = new CodeMap.NameGenerator(); |
| 49 | 44 |
| 50 /** | 45 /** |
| 51 * Static code entries. Used for statically compiled code. | 46 * Static code entries. Used for statically compiled code. |
| 52 */ | 47 */ |
| 53 this.statics_ = new goog.structs.SplayTree(); | 48 this.statics_ = new SplayTree(); |
| 54 | 49 |
| 55 /** | 50 /** |
| 56 * Libraries entries. Used for the whole static code libraries. | 51 * Libraries entries. Used for the whole static code libraries. |
| 57 */ | 52 */ |
| 58 this.libraries_ = new goog.structs.SplayTree(); | 53 this.libraries_ = new SplayTree(); |
| 59 | 54 |
| 60 /** | 55 /** |
| 61 * Map of memory pages occupied with static code. | 56 * Map of memory pages occupied with static code. |
| 62 */ | 57 */ |
| 63 this.pages_ = []; | 58 this.pages_ = []; |
| 64 }; | 59 }; |
| 65 | 60 |
| 66 | 61 |
| 67 /** | 62 /** |
| 68 * The number of alignment bits in a page address. | 63 * The number of alignment bits in a page address. |
| 69 */ | 64 */ |
| 70 devtools.profiler.CodeMap.PAGE_ALIGNMENT = 12; | 65 CodeMap.PAGE_ALIGNMENT = 12; |
| 71 | 66 |
| 72 | 67 |
| 73 /** | 68 /** |
| 74 * Page size in bytes. | 69 * Page size in bytes. |
| 75 */ | 70 */ |
| 76 devtools.profiler.CodeMap.PAGE_SIZE = | 71 CodeMap.PAGE_SIZE = |
| 77 1 << devtools.profiler.CodeMap.PAGE_ALIGNMENT; | 72 1 << CodeMap.PAGE_ALIGNMENT; |
| 78 | 73 |
| 79 | 74 |
| 80 /** | 75 /** |
| 81 * Adds a dynamic (i.e. moveable and discardable) code entry. | 76 * Adds a dynamic (i.e. moveable and discardable) code entry. |
| 82 * | 77 * |
| 83 * @param {number} start The starting address. | 78 * @param {number} start The starting address. |
| 84 * @param {devtools.profiler.CodeMap.CodeEntry} codeEntry Code entry object. | 79 * @param {CodeMap.CodeEntry} codeEntry Code entry object. |
| 85 */ | 80 */ |
| 86 devtools.profiler.CodeMap.prototype.addCode = function(start, codeEntry) { | 81 CodeMap.prototype.addCode = function(start, codeEntry) { |
| 87 this.dynamics_.insert(start, codeEntry); | 82 this.dynamics_.insert(start, codeEntry); |
| 88 }; | 83 }; |
| 89 | 84 |
| 90 | 85 |
| 91 /** | 86 /** |
| 92 * Moves a dynamic code entry. Throws an exception if there is no dynamic | 87 * Moves a dynamic code entry. Throws an exception if there is no dynamic |
| 93 * code entry with the specified starting address. | 88 * code entry with the specified starting address. |
| 94 * | 89 * |
| 95 * @param {number} from The starting address of the entry being moved. | 90 * @param {number} from The starting address of the entry being moved. |
| 96 * @param {number} to The destination address. | 91 * @param {number} to The destination address. |
| 97 */ | 92 */ |
| 98 devtools.profiler.CodeMap.prototype.moveCode = function(from, to) { | 93 CodeMap.prototype.moveCode = function(from, to) { |
| 99 var removedNode = this.dynamics_.remove(from); | 94 var removedNode = this.dynamics_.remove(from); |
| 100 this.dynamics_.insert(to, removedNode.value); | 95 this.dynamics_.insert(to, removedNode.value); |
| 101 }; | 96 }; |
| 102 | 97 |
| 103 | 98 |
| 104 /** | 99 /** |
| 105 * Discards a dynamic code entry. Throws an exception if there is no dynamic | 100 * Discards a dynamic code entry. Throws an exception if there is no dynamic |
| 106 * code entry with the specified starting address. | 101 * code entry with the specified starting address. |
| 107 * | 102 * |
| 108 * @param {number} start The starting address of the entry being deleted. | 103 * @param {number} start The starting address of the entry being deleted. |
| 109 */ | 104 */ |
| 110 devtools.profiler.CodeMap.prototype.deleteCode = function(start) { | 105 CodeMap.prototype.deleteCode = function(start) { |
| 111 var removedNode = this.dynamics_.remove(start); | 106 var removedNode = this.dynamics_.remove(start); |
| 112 }; | 107 }; |
| 113 | 108 |
| 114 | 109 |
| 115 /** | 110 /** |
| 116 * Adds a library entry. | 111 * Adds a library entry. |
| 117 * | 112 * |
| 118 * @param {number} start The starting address. | 113 * @param {number} start The starting address. |
| 119 * @param {devtools.profiler.CodeMap.CodeEntry} codeEntry Code entry object. | 114 * @param {CodeMap.CodeEntry} codeEntry Code entry object. |
| 120 */ | 115 */ |
| 121 devtools.profiler.CodeMap.prototype.addLibrary = function( | 116 CodeMap.prototype.addLibrary = function( |
| 122 start, codeEntry) { | 117 start, codeEntry) { |
| 123 this.markPages_(start, start + codeEntry.size); | 118 this.markPages_(start, start + codeEntry.size); |
| 124 this.libraries_.insert(start, codeEntry); | 119 this.libraries_.insert(start, codeEntry); |
| 125 }; | 120 }; |
| 126 | 121 |
| 127 | 122 |
| 128 /** | 123 /** |
| 129 * Adds a static code entry. | 124 * Adds a static code entry. |
| 130 * | 125 * |
| 131 * @param {number} start The starting address. | 126 * @param {number} start The starting address. |
| 132 * @param {devtools.profiler.CodeMap.CodeEntry} codeEntry Code entry object. | 127 * @param {CodeMap.CodeEntry} codeEntry Code entry object. |
| 133 */ | 128 */ |
| 134 devtools.profiler.CodeMap.prototype.addStaticCode = function( | 129 CodeMap.prototype.addStaticCode = function( |
| 135 start, codeEntry) { | 130 start, codeEntry) { |
| 136 this.statics_.insert(start, codeEntry); | 131 this.statics_.insert(start, codeEntry); |
| 137 }; | 132 }; |
| 138 | 133 |
| 139 | 134 |
| 140 /** | 135 /** |
| 141 * @private | 136 * @private |
| 142 */ | 137 */ |
| 143 devtools.profiler.CodeMap.prototype.markPages_ = function(start, end) { | 138 CodeMap.prototype.markPages_ = function(start, end) { |
| 144 for (var addr = start; addr <= end; | 139 for (var addr = start; addr <= end; |
| 145 addr += devtools.profiler.CodeMap.PAGE_SIZE) { | 140 addr += CodeMap.PAGE_SIZE) { |
| 146 this.pages_[addr >>> devtools.profiler.CodeMap.PAGE_ALIGNMENT] = 1; | 141 this.pages_[addr >>> CodeMap.PAGE_ALIGNMENT] = 1; |
| 147 } | 142 } |
| 148 }; | 143 }; |
| 149 | 144 |
| 150 | 145 |
| 151 /** | 146 /** |
| 152 * @private | 147 * @private |
| 153 */ | 148 */ |
| 154 devtools.profiler.CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) { | 149 CodeMap.prototype.isAddressBelongsTo_ = function(addr, node) { |
| 155 return addr >= node.key && addr < (node.key + node.value.size); | 150 return addr >= node.key && addr < (node.key + node.value.size); |
| 156 }; | 151 }; |
| 157 | 152 |
| 158 | 153 |
| 159 /** | 154 /** |
| 160 * @private | 155 * @private |
| 161 */ | 156 */ |
| 162 devtools.profiler.CodeMap.prototype.findInTree_ = function(tree, addr) { | 157 CodeMap.prototype.findInTree_ = function(tree, addr) { |
| 163 var node = tree.findGreatestLessThan(addr); | 158 var node = tree.findGreatestLessThan(addr); |
| 164 return node && this.isAddressBelongsTo_(addr, node) ? node.value : null; | 159 return node && this.isAddressBelongsTo_(addr, node) ? node.value : null; |
| 165 }; | 160 }; |
| 166 | 161 |
| 167 | 162 |
| 168 /** | 163 /** |
| 169 * Finds a code entry that contains the specified address. Both static and | 164 * Finds a code entry that contains the specified address. Both static and |
| 170 * dynamic code entries are considered. | 165 * dynamic code entries are considered. |
| 171 * | 166 * |
| 172 * @param {number} addr Address. | 167 * @param {number} addr Address. |
| 173 */ | 168 */ |
| 174 devtools.profiler.CodeMap.prototype.findEntry = function(addr) { | 169 CodeMap.prototype.findEntry = function(addr) { |
| 175 var pageAddr = addr >>> devtools.profiler.CodeMap.PAGE_ALIGNMENT; | 170 var pageAddr = addr >>> CodeMap.PAGE_ALIGNMENT; |
| 176 if (pageAddr in this.pages_) { | 171 if (pageAddr in this.pages_) { |
| 177 // Static code entries can contain "holes" of unnamed code. | 172 // Static code entries can contain "holes" of unnamed code. |
| 178 // In this case, the whole library is assigned to this address. | 173 // In this case, the whole library is assigned to this address. |
| 179 return this.findInTree_(this.statics_, addr) || | 174 return this.findInTree_(this.statics_, addr) || |
| 180 this.findInTree_(this.libraries_, addr); | 175 this.findInTree_(this.libraries_, addr); |
| 181 } | 176 } |
| 182 var min = this.dynamics_.findMin(); | 177 var min = this.dynamics_.findMin(); |
| 183 var max = this.dynamics_.findMax(); | 178 var max = this.dynamics_.findMax(); |
| 184 if (max != null && addr < (max.key + max.value.size) && addr >= min.key) { | 179 if (max != null && addr < (max.key + max.value.size) && addr >= min.key) { |
| 185 var dynaEntry = this.findInTree_(this.dynamics_, addr); | 180 var dynaEntry = this.findInTree_(this.dynamics_, addr); |
| 186 if (dynaEntry == null) return null; | 181 if (dynaEntry == null) return null; |
| 187 // Dedupe entry name. | 182 // Dedupe entry name. |
| 188 if (!dynaEntry.nameUpdated_) { | 183 if (!dynaEntry.nameUpdated_) { |
| 189 dynaEntry.name = this.dynamicsNameGen_.getName(dynaEntry.name); | 184 dynaEntry.name = this.dynamicsNameGen_.getName(dynaEntry.name); |
| 190 dynaEntry.nameUpdated_ = true; | 185 dynaEntry.nameUpdated_ = true; |
| 191 } | 186 } |
| 192 return dynaEntry; | 187 return dynaEntry; |
| 193 } | 188 } |
| 194 return null; | 189 return null; |
| 195 }; | 190 }; |
| 196 | 191 |
| 197 | 192 |
| 198 /** | 193 /** |
| 199 * Returns a dynamic code entry using its starting address. | 194 * Returns a dynamic code entry using its starting address. |
| 200 * | 195 * |
| 201 * @param {number} addr Address. | 196 * @param {number} addr Address. |
| 202 */ | 197 */ |
| 203 devtools.profiler.CodeMap.prototype.findDynamicEntryByStartAddress = | 198 CodeMap.prototype.findDynamicEntryByStartAddress = |
| 204 function(addr) { | 199 function(addr) { |
| 205 var node = this.dynamics_.find(addr); | 200 var node = this.dynamics_.find(addr); |
| 206 return node ? node.value : null; | 201 return node ? node.value : null; |
| 207 }; | 202 }; |
| 208 | 203 |
| 209 | 204 |
| 210 /** | 205 /** |
| 211 * Returns an array of all dynamic code entries. | 206 * Returns an array of all dynamic code entries. |
| 212 */ | 207 */ |
| 213 devtools.profiler.CodeMap.prototype.getAllDynamicEntries = function() { | 208 CodeMap.prototype.getAllDynamicEntries = function() { |
| 214 return this.dynamics_.exportValues(); | 209 return this.dynamics_.exportValues(); |
| 215 }; | 210 }; |
| 216 | 211 |
| 217 | 212 |
| 218 /** | 213 /** |
| 219 * Returns an array of all static code entries. | 214 * Returns an array of all static code entries. |
| 220 */ | 215 */ |
| 221 devtools.profiler.CodeMap.prototype.getAllStaticEntries = function() { | 216 CodeMap.prototype.getAllStaticEntries = function() { |
| 222 return this.statics_.exportValues(); | 217 return this.statics_.exportValues(); |
| 223 }; | 218 }; |
| 224 | 219 |
| 225 | 220 |
| 226 /** | 221 /** |
| 227 * Returns an array of all libraries entries. | 222 * Returns an array of all libraries entries. |
| 228 */ | 223 */ |
| 229 devtools.profiler.CodeMap.prototype.getAllLibrariesEntries = function() { | 224 CodeMap.prototype.getAllLibrariesEntries = function() { |
| 230 return this.libraries_.exportValues(); | 225 return this.libraries_.exportValues(); |
| 231 }; | 226 }; |
| 232 | 227 |
| 233 | 228 |
| 234 /** | 229 /** |
| 235 * Creates a code entry object. | 230 * Creates a code entry object. |
| 236 * | 231 * |
| 237 * @param {number} size Code entry size in bytes. | 232 * @param {number} size Code entry size in bytes. |
| 238 * @param {string} opt_name Code entry name. | 233 * @param {string} opt_name Code entry name. |
| 239 * @constructor | 234 * @constructor |
| 240 */ | 235 */ |
| 241 devtools.profiler.CodeMap.CodeEntry = function(size, opt_name) { | 236 CodeMap.CodeEntry = function(size, opt_name) { |
| 242 this.size = size; | 237 this.size = size; |
| 243 this.name = opt_name || ''; | 238 this.name = opt_name || ''; |
| 244 this.nameUpdated_ = false; | 239 this.nameUpdated_ = false; |
| 245 }; | 240 }; |
| 246 | 241 |
| 247 | 242 |
| 248 devtools.profiler.CodeMap.CodeEntry.prototype.getName = function() { | 243 CodeMap.CodeEntry.prototype.getName = function() { |
| 249 return this.name; | 244 return this.name; |
| 250 }; | 245 }; |
| 251 | 246 |
| 252 | 247 |
| 253 devtools.profiler.CodeMap.CodeEntry.prototype.toString = function() { | 248 CodeMap.CodeEntry.prototype.toString = function() { |
| 254 return this.name + ': ' + this.size.toString(16); | 249 return this.name + ': ' + this.size.toString(16); |
| 255 }; | 250 }; |
| 256 | 251 |
| 257 | 252 |
| 258 devtools.profiler.CodeMap.NameGenerator = function() { | 253 CodeMap.NameGenerator = function() { |
| 259 this.knownNames_ = {}; | 254 this.knownNames_ = {}; |
| 260 }; | 255 }; |
| 261 | 256 |
| 262 | 257 |
| 263 devtools.profiler.CodeMap.NameGenerator.prototype.getName = function(name) { | 258 CodeMap.NameGenerator.prototype.getName = function(name) { |
| 264 if (!(name in this.knownNames_)) { | 259 if (!(name in this.knownNames_)) { |
| 265 this.knownNames_[name] = 0; | 260 this.knownNames_[name] = 0; |
| 266 return name; | 261 return name; |
| 267 } | 262 } |
| 268 var count = ++this.knownNames_[name]; | 263 var count = ++this.knownNames_[name]; |
| 269 return name + ' {' + count + '}'; | 264 return name + ' {' + count + '}'; |
| 270 }; | 265 }; |
| OLD | NEW |