Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 1 | // Copyright 2010 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 | #ifndef V8_V8_PROFILER_H_ |
| 29 | #define V8_V8_PROFILER_H_ |
| 30 | |
| 31 | #include "v8.h" |
| 32 | |
| 33 | #ifdef _WIN32 |
| 34 | // Setup for Windows DLL export/import. See v8.h in this directory for |
| 35 | // information on how to build/use V8 as a DLL. |
| 36 | #if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED) |
| 37 | #error both BUILDING_V8_SHARED and USING_V8_SHARED are set - please check the\ |
| 38 | build configuration to ensure that at most one of these is set |
| 39 | #endif |
| 40 | |
| 41 | #ifdef BUILDING_V8_SHARED |
| 42 | #define V8EXPORT __declspec(dllexport) |
| 43 | #elif USING_V8_SHARED |
| 44 | #define V8EXPORT __declspec(dllimport) |
| 45 | #else |
| 46 | #define V8EXPORT |
| 47 | #endif |
| 48 | |
| 49 | #else // _WIN32 |
| 50 | |
| 51 | // Setup for Linux shared library export. See v8.h in this directory for |
| 52 | // information on how to build/use V8 as shared library. |
| 53 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(V8_SHARED) |
| 54 | #define V8EXPORT __attribute__ ((visibility("default"))) |
| 55 | #else // defined(__GNUC__) && (__GNUC__ >= 4) |
| 56 | #define V8EXPORT |
| 57 | #endif // defined(__GNUC__) && (__GNUC__ >= 4) |
| 58 | |
| 59 | #endif // _WIN32 |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Profiler support for the V8 JavaScript engine. |
| 64 | */ |
| 65 | namespace v8 { |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * CpuProfileNode represents a node in a call graph. |
| 70 | */ |
| 71 | class V8EXPORT CpuProfileNode { |
| 72 | public: |
| 73 | /** Returns function name (empty string for anonymous functions.) */ |
| 74 | Handle<String> GetFunctionName() const; |
| 75 | |
| 76 | /** Returns resource name for script from where the function originates. */ |
| 77 | Handle<String> GetScriptResourceName() const; |
| 78 | |
| 79 | /** |
| 80 | * Returns the number, 1-based, of the line where the function originates. |
| 81 | * kNoLineNumberInfo if no line number information is available. |
| 82 | */ |
| 83 | int GetLineNumber() const; |
| 84 | |
| 85 | /** |
| 86 | * Returns total (self + children) execution time of the function, |
| 87 | * in milliseconds, estimated by samples count. |
| 88 | */ |
| 89 | double GetTotalTime() const; |
| 90 | |
| 91 | /** |
| 92 | * Returns self execution time of the function, in milliseconds, |
| 93 | * estimated by samples count. |
| 94 | */ |
| 95 | double GetSelfTime() const; |
| 96 | |
| 97 | /** Returns the count of samples where function exists. */ |
| 98 | double GetTotalSamplesCount() const; |
| 99 | |
| 100 | /** Returns the count of samples where function was currently executing. */ |
| 101 | double GetSelfSamplesCount() const; |
| 102 | |
| 103 | /** Returns function entry UID. */ |
| 104 | unsigned GetCallUid() const; |
| 105 | |
| 106 | /** Returns child nodes count of the node. */ |
| 107 | int GetChildrenCount() const; |
| 108 | |
| 109 | /** Retrieves a child node by index. */ |
| 110 | const CpuProfileNode* GetChild(int index) const; |
| 111 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 112 | static const int kNoLineNumberInfo = Message::kNoLineNumberInfo; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | |
| 116 | /** |
| 117 | * CpuProfile contains a CPU profile in a form of two call trees: |
| 118 | * - top-down (from main() down to functions that do all the work); |
| 119 | * - bottom-up call graph (in backward direction). |
| 120 | */ |
| 121 | class V8EXPORT CpuProfile { |
| 122 | public: |
| 123 | /** Returns CPU profile UID (assigned by the profiler.) */ |
| 124 | unsigned GetUid() const; |
| 125 | |
| 126 | /** Returns CPU profile title. */ |
| 127 | Handle<String> GetTitle() const; |
| 128 | |
| 129 | /** Returns the root node of the bottom up call tree. */ |
| 130 | const CpuProfileNode* GetBottomUpRoot() const; |
| 131 | |
| 132 | /** Returns the root node of the top down call tree. */ |
| 133 | const CpuProfileNode* GetTopDownRoot() const; |
| 134 | }; |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * Interface for controlling CPU profiling. |
| 139 | */ |
| 140 | class V8EXPORT CpuProfiler { |
| 141 | public: |
| 142 | /** |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 143 | * A note on security tokens usage. As scripts from different |
| 144 | * origins can run inside a single V8 instance, it is possible to |
| 145 | * have functions from different security contexts intermixed in a |
| 146 | * single CPU profile. To avoid exposing function names belonging to |
| 147 | * other contexts, filtering by security token is performed while |
| 148 | * obtaining profiling results. |
| 149 | */ |
| 150 | |
| 151 | /** |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 152 | * Returns the number of profiles collected (doesn't include |
| 153 | * profiles that are being collected at the moment of call.) |
| 154 | */ |
| 155 | static int GetProfilesCount(); |
| 156 | |
| 157 | /** Returns a profile by index. */ |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 158 | static const CpuProfile* GetProfile( |
| 159 | int index, |
| 160 | Handle<Value> security_token = Handle<Value>()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 161 | |
| 162 | /** Returns a profile by uid. */ |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 163 | static const CpuProfile* FindProfile( |
| 164 | unsigned uid, |
| 165 | Handle<Value> security_token = Handle<Value>()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 166 | |
| 167 | /** |
| 168 | * Starts collecting CPU profile. Title may be an empty string. It |
| 169 | * is allowed to have several profiles being collected at |
| 170 | * once. Attempts to start collecting several profiles with the same |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 171 | * title are silently ignored. While collecting a profile, functions |
| 172 | * from all security contexts are included in it. The token-based |
| 173 | * filtering is only performed when querying for a profile. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 174 | */ |
| 175 | static void StartProfiling(Handle<String> title); |
| 176 | |
| 177 | /** |
| 178 | * Stops collecting CPU profile with a given title and returns it. |
| 179 | * If the title given is empty, finishes the last profile started. |
| 180 | */ |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 181 | static const CpuProfile* StopProfiling( |
| 182 | Handle<String> title, |
| 183 | Handle<Value> security_token = Handle<Value>()); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 187 | class HeapGraphNode; |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * HeapSnapshotEdge represents a directed connection between heap |
| 192 | * graph nodes: from retaners to retained nodes. |
| 193 | */ |
| 194 | class V8EXPORT HeapGraphEdge { |
| 195 | public: |
| 196 | enum Type { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 197 | kContextVariable = 0, // A variable from a function context. |
| 198 | kElement = 1, // An element of an array. |
| 199 | kProperty = 2, // A named object property. |
| 200 | kInternal = 3 // A link that can't be accessed from JS, |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 201 | // thus, its name isn't a real property name. |
| 202 | }; |
| 203 | |
| 204 | /** Returns edge type (see HeapGraphEdge::Type). */ |
| 205 | Type GetType() const; |
| 206 | |
| 207 | /** |
| 208 | * Returns edge name. This can be a variable name, an element index, or |
| 209 | * a property name. |
| 210 | */ |
| 211 | Handle<Value> GetName() const; |
| 212 | |
| 213 | /** Returns origin node. */ |
| 214 | const HeapGraphNode* GetFromNode() const; |
| 215 | |
| 216 | /** Returns destination node. */ |
| 217 | const HeapGraphNode* GetToNode() const; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | class V8EXPORT HeapGraphPath { |
| 222 | public: |
| 223 | /** Returns the number of edges in the path. */ |
| 224 | int GetEdgesCount() const; |
| 225 | |
| 226 | /** Returns an edge from the path. */ |
| 227 | const HeapGraphEdge* GetEdge(int index) const; |
| 228 | |
| 229 | /** Returns origin node. */ |
| 230 | const HeapGraphNode* GetFromNode() const; |
| 231 | |
| 232 | /** Returns destination node. */ |
| 233 | const HeapGraphNode* GetToNode() const; |
| 234 | }; |
| 235 | |
| 236 | |
| 237 | /** |
| 238 | * HeapGraphNode represents a node in a heap graph. |
| 239 | */ |
| 240 | class V8EXPORT HeapGraphNode { |
| 241 | public: |
| 242 | enum Type { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 243 | kInternal = 0, // Internal node, a virtual one, for housekeeping. |
| 244 | kArray = 1, // An array of elements. |
| 245 | kString = 2, // A string. |
| 246 | kObject = 3, // A JS object (except for arrays and strings). |
| 247 | kCode = 4, // Compiled code. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 248 | kClosure = 5, // Function closure. |
| 249 | kRegExp = 6, // RegExp. |
| 250 | kHeapNumber = 7 // Number stored in the heap. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | /** Returns node type (see HeapGraphNode::Type). */ |
| 254 | Type GetType() const; |
| 255 | |
| 256 | /** |
| 257 | * Returns node name. Depending on node's type this can be the name |
| 258 | * of the constructor (for objects), the name of the function (for |
| 259 | * closures), string value, or an empty string (for compiled code). |
| 260 | */ |
| 261 | Handle<String> GetName() const; |
| 262 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 263 | /** |
| 264 | * Returns node id. For the same heap object, the id remains the same |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 265 | * across all snapshots. Not applicable to aggregated heap snapshots |
| 266 | * as they only contain aggregated instances. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 267 | */ |
| 268 | uint64_t GetId() const; |
| 269 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 270 | /** |
| 271 | * Returns the number of instances. Only applicable to aggregated |
| 272 | * heap snapshots. |
| 273 | */ |
| 274 | int GetInstancesCount() const; |
| 275 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 276 | /** Returns node's own size, in bytes. */ |
| 277 | int GetSelfSize() const; |
| 278 | |
| 279 | /** Returns node's network (self + reachable nodes) size, in bytes. */ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 280 | int GetReachableSize() const; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 281 | |
| 282 | /** |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 283 | * Returns node's retained size, in bytes. That is, self + sizes of |
| 284 | * the objects that are reachable only from this object. In other |
| 285 | * words, the size of memory that will be reclaimed having this node |
| 286 | * collected. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 287 | */ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 288 | int GetRetainedSize() const; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 289 | |
| 290 | /** Returns child nodes count of the node. */ |
| 291 | int GetChildrenCount() const; |
| 292 | |
| 293 | /** Retrieves a child by index. */ |
| 294 | const HeapGraphEdge* GetChild(int index) const; |
| 295 | |
| 296 | /** Returns retainer nodes count of the node. */ |
| 297 | int GetRetainersCount() const; |
| 298 | |
| 299 | /** Returns a retainer by index. */ |
| 300 | const HeapGraphEdge* GetRetainer(int index) const; |
| 301 | |
| 302 | /** Returns the number of simple retaining paths from the root to the node. */ |
| 303 | int GetRetainingPathsCount() const; |
| 304 | |
| 305 | /** Returns a retaining path by index. */ |
| 306 | const HeapGraphPath* GetRetainingPath(int index) const; |
| 307 | }; |
| 308 | |
| 309 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 310 | class V8EXPORT HeapSnapshotsDiff { |
| 311 | public: |
| 312 | /** Returns the root node for added nodes. */ |
| 313 | const HeapGraphNode* GetAdditionsRoot() const; |
| 314 | |
| 315 | /** Returns the root node for deleted nodes. */ |
| 316 | const HeapGraphNode* GetDeletionsRoot() const; |
| 317 | }; |
| 318 | |
| 319 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 320 | /** |
| 321 | * HeapSnapshots record the state of the JS heap at some moment. |
| 322 | */ |
| 323 | class V8EXPORT HeapSnapshot { |
| 324 | public: |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 325 | enum Type { |
| 326 | kFull = 0, // Heap snapshot with all instances and references. |
| 327 | kAggregated = 1 // Snapshot doesn't contain individual heap entries, |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 328 | // instead they are grouped by constructor name. |
| 329 | }; |
| 330 | enum SerializationFormat { |
| 331 | kJSON = 0 // See format description near 'Serialize' method. |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | /** Returns heap snapshot type. */ |
| 335 | Type GetType() const; |
| 336 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 337 | /** Returns heap snapshot UID (assigned by the profiler.) */ |
| 338 | unsigned GetUid() const; |
| 339 | |
| 340 | /** Returns heap snapshot title. */ |
| 341 | Handle<String> GetTitle() const; |
| 342 | |
| 343 | /** Returns the root node of the heap graph. */ |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 344 | const HeapGraphNode* GetRoot() const; |
| 345 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 346 | /** |
| 347 | * Returns a diff between this snapshot and another one. Only snapshots |
| 348 | * of the same type can be compared. |
| 349 | */ |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 350 | const HeapSnapshotsDiff* CompareWith(const HeapSnapshot* snapshot) const; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 351 | |
| 352 | /** |
| 353 | * Prepare a serialized representation of the snapshot. The result |
| 354 | * is written into the stream provided in chunks of specified size. |
| 355 | * The total length of the serialized snapshot is unknown in |
| 356 | * advance, it is can be roughly equal to JS heap size (that means, |
| 357 | * it can be really big - tens of megabytes). |
| 358 | * |
| 359 | * For the JSON format, heap contents are represented as an object |
| 360 | * with the following structure: |
| 361 | * |
| 362 | * { |
| 363 | * snapshot: {title: "...", uid: nnn}, |
| 364 | * nodes: [ |
| 365 | * meta-info (JSON string), |
| 366 | * nodes themselves |
| 367 | * ], |
| 368 | * strings: [strings] |
| 369 | * } |
| 370 | * |
| 371 | * Outgoing node links are stored after each node. Nodes reference strings |
| 372 | * and other nodes by their indexes in corresponding arrays. |
| 373 | */ |
| 374 | void Serialize(OutputStream* stream, SerializationFormat format) const; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 375 | }; |
| 376 | |
| 377 | |
| 378 | /** |
| 379 | * Interface for controlling heap profiling. |
| 380 | */ |
| 381 | class V8EXPORT HeapProfiler { |
| 382 | public: |
| 383 | /** Returns the number of snapshots taken. */ |
| 384 | static int GetSnapshotsCount(); |
| 385 | |
| 386 | /** Returns a snapshot by index. */ |
| 387 | static const HeapSnapshot* GetSnapshot(int index); |
| 388 | |
| 389 | /** Returns a profile by uid. */ |
| 390 | static const HeapSnapshot* FindSnapshot(unsigned uid); |
| 391 | |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 392 | /** |
| 393 | * Takes a heap snapshot and returns it. Title may be an empty string. |
| 394 | * See HeapSnapshot::Type for types description. |
| 395 | */ |
| 396 | static const HeapSnapshot* TakeSnapshot( |
| 397 | Handle<String> title, |
| 398 | HeapSnapshot::Type type = HeapSnapshot::kFull); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 402 | } // namespace v8 |
| 403 | |
| 404 | |
| 405 | #undef V8EXPORT |
| 406 | |
| 407 | |
| 408 | #endif // V8_V8_PROFILER_H_ |