blob: 8366a6cc999db8a91ba915beafdc25373ba02738 [file] [log] [blame]
Chris Craik93216d02015-03-05 13:58:42 -08001<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
Chris Craik24385db2015-06-11 11:16:26 -07007<link rel="import" href="/model/object_instance.html">
8<link rel="import" href="/extras/chrome/cc/util.html">
Chris Craik93216d02015-03-05 13:58:42 -08009<script>
10'use strict';
11
Chris Craik24385db2015-06-11 11:16:26 -070012tr.exportTo('tr.e.tcmalloc', function() {
13 var ObjectSnapshot = tr.model.ObjectSnapshot;
Chris Craik93216d02015-03-05 13:58:42 -080014
15 /**
16 * @constructor
17 */
18 function HeapSnapshot() {
19 ObjectSnapshot.apply(this, arguments);
20 }
21
22 HeapSnapshot.prototype = {
23 __proto__: ObjectSnapshot.prototype,
24
25 preInitialize: function() {
Chris Craik24385db2015-06-11 11:16:26 -070026 tr.e.cc.preInitializeObject(this);
Chris Craik93216d02015-03-05 13:58:42 -080027
28 // TODO(jamescook): Any generic field setup can go here.
29 },
30
31 // TODO(jamescook): This seems to be called before the green dot is clicked.
32 // Consider doing it in heap_view.js.
33 initialize: function() {
34 if (this.args.length == 0)
35 throw new Error('No heap snapshot data.');
36
37 // The first entry is total allocations across all stack traces.
38 this.total_ = this.args[0];
39 // The rest is a list of allocations.
40 var allocs = this.args.slice(1);
41
42 // Build a nested dictionary of trace event names.
43 this.heap_ = {
44 children: {},
45 currentBytes: 0,
46 currentAllocs: 0,
47 totalBytes: 0,
48 totalAllocs: 0
49 };
50 for (var i = 0; i < allocs.length; i++) {
51 var alloc = allocs[i];
52 var traceNames = alloc.trace.split(' ');
53 // We don't want to record allocations caused by the heap profiling
54 // system itself, so skip allocations with this special name.
55 if (traceNames.indexOf('trace-memory-ignore') != -1)
56 continue;
57 var heapEntry = this.heap_;
58 // Walk down into the heap of stack traces.
59 for (var j = 0; j < traceNames.length; j++) {
60 // Look for existing children with this trace.
61 var traceName = traceNames[j];
62 // The empty trace name means "(here)", so don't roll those up into
63 // parent traces because they have already been counted.
64 if (traceName.length != 0) {
65 // Add up the total memory for intermediate entries, so the top of
66 // each subtree is the total memory for that tree.
67 heapEntry.currentBytes += alloc.currentBytes;
68 heapEntry.currentAllocs += alloc.currentAllocs;
69 heapEntry.totalBytes += alloc.totalBytes;
70 heapEntry.totalAllocs += alloc.totalAllocs;
71 }
72 if (!heapEntry.children[traceName]) {
73 // New trace entry at this depth, so create a child for it.
74 heapEntry.children[traceName] = {
75 children: {},
76 currentBytes: alloc.currentBytes,
77 currentAllocs: alloc.currentAllocs,
78 totalBytes: alloc.totalBytes,
79 totalAllocs: alloc.totalAllocs
80 };
81 }
82 // Descend into the children.
83 heapEntry = heapEntry.children[traceName];
84 }
85 }
86 }
87
88 };
89
90 ObjectSnapshot.register(
91 HeapSnapshot,
92 {typeName: 'memory::Heap'});
93
94 return {
95 HeapSnapshot: HeapSnapshot
96 };
97});
98</script>