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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { Cc, Ci, Cu } = require("chrome");
const { reportException } = require("devtools/shared/DevToolsUtils");
const { expectState } = require("devtools/server/actors/common");
loader.lazyRequireGetter(this, "EventEmitter", "devtools/shared/event-emitter");
loader.lazyRequireGetter(
this,
"DeferredTask",
"resource://gre/modules/DeferredTask.jsm",
true
);
loader.lazyRequireGetter(
this,
"StackFrameCache",
"devtools/server/actors/utils/stack",
true
);
loader.lazyRequireGetter(this, "ChromeUtils");
loader.lazyRequireGetter(
this,
"ParentProcessTargetActor",
"devtools/server/actors/targets/parent-process",
true
);
loader.lazyRequireGetter(
this,
"ContentProcessTargetActor",
"devtools/server/actors/targets/content-process",
true
);
/**
* A class that returns memory data for a parent actor's window.
* Using a target-scoped actor with this instance will measure the memory footprint of its
* parent tab. Using a global-scoped actor instance however, will measure the memory
* footprint of the chrome window referenced by its root actor.
*
* To be consumed by actor's, like MemoryActor using this module to
* send information over RDP, and TimelineActor for using more light-weight
* utilities like GC events and measuring memory consumption.
*/
function Memory(parent, frameCache = new StackFrameCache()) {
EventEmitter.decorate(this);
this.parent = parent;
this._mgr = Cc["@mozilla.org/memory-reporter-manager;1"].getService(
Ci.nsIMemoryReporterManager
);
this.state = "detached";
this._dbg = null;
this._frameCache = frameCache;
this._onGarbageCollection = this._onGarbageCollection.bind(this);
this._emitAllocations = this._emitAllocations.bind(this);
this._onWindowReady = this._onWindowReady.bind(this);
EventEmitter.on(this.parent, "window-ready", this._onWindowReady);
}
Memory.prototype = {
destroy: function() {
EventEmitter.off(this.parent, "window-ready", this._onWindowReady);
this._mgr = null;
if (this.state === "attached") {
this.detach();
}
},
get dbg() {
if (!this._dbg) {
this._dbg = this.parent.makeDebugger();
}
return this._dbg;
},
/**
* Attach to this MemoryBridge.
*
* This attaches the MemoryBridge's Debugger instance so that you can start
* recording allocations or take a census of the heap. In addition, the
* MemoryBridge will start emitting GC events.
*/
attach: expectState(
"detached",
function() {
this.dbg.addDebuggees();
this.dbg.memory.onGarbageCollection = this._onGarbageCollection.bind(
this
);
this.state = "attached";
return this.state;
},
"attaching to the debugger"
),
/**
* Detach from this MemoryBridge.
*/
detach: expectState(
"attached",
function() {
this._clearDebuggees();
this.dbg.disable();
this._dbg = null;
this.state = "detached";
return this.state;
},
"detaching from the debugger"
),
/**
* Gets the current MemoryBridge attach/detach state.
*/
getState: function() {
return this.state;
},
_clearDebuggees: function() {
if (this._dbg) {
if (this.isRecordingAllocations()) {
this.dbg.memory.drainAllocationsLog();
}
this._clearFrames();
this.dbg.removeAllDebuggees();
}
},
_clearFrames: function() {
if (this.isRecordingAllocations()) {
this._frameCache.clearFrames();
}
},
/**
* Handler for the parent actor's "window-ready" event.
*/
_onWindowReady: function({ isTopLevel }) {
if (this.state == "attached") {
this._clearDebuggees();
if (isTopLevel && this.isRecordingAllocations()) {
this._frameCache.initFrames();
}
this.dbg.addDebuggees();
}
},
/**
* Returns a boolean indicating whether or not allocation
* sites are being tracked.
*/
isRecordingAllocations: function() {
return this.dbg.memory.trackingAllocationSites;
},
/**
* Save a heap snapshot scoped to the current debuggees' portion of the heap
* graph.
*
* @param {Object|null} boundaries
*
* @returns {String} The snapshot id.
*/
saveHeapSnapshot: expectState(
"attached",
function(boundaries = null) {
// If we are observing the whole process, then scope the snapshot
// accordingly. Otherwise, use the debugger's debuggees.
if (!boundaries) {
if (
this.parent instanceof ParentProcessTargetActor ||
this.parent instanceof ContentProcessTargetActor
) {
boundaries = { runtime: true };
} else {
boundaries = { debugger: this.dbg };
}
}
return ChromeUtils.saveHeapSnapshotGetId(boundaries);
},
"saveHeapSnapshot"
),
/**
* Take a census of the heap. See js/src/doc/Debugger/Debugger.Memory.md for
* more information.
*/
takeCensus: expectState(
"attached",
function() {
return this.dbg.memory.takeCensus();
},
"taking census"
),
/**
* Start recording allocation sites.
*
* @param {number} options.probability
* The probability we sample any given allocation when recording
* allocations. Must be between 0 and 1 -- defaults to 1.
* @param {number} options.maxLogLength
* The maximum number of allocation events to keep in the
* log. If new allocs occur while at capacity, oldest
* allocations are lost. Must fit in a 32 bit signed integer.
* @param {number} options.drainAllocationsTimeout
* A number in milliseconds of how often, at least, an `allocation`
* event gets emitted (and drained), and also emits and drains on every
* GC event, resetting the timer.
*/
startRecordingAllocations: expectState(
"attached",
function(options = {}) {
if (this.isRecordingAllocations()) {
return this._getCurrentTime();
}
this._frameCache.initFrames();
this.dbg.memory.allocationSamplingProbability =
options.probability != null ? options.probability : 1.0;
this.drainAllocationsTimeoutTimer = options.drainAllocationsTimeout;
if (this.drainAllocationsTimeoutTimer != null) {
if (this._poller) {
this._poller.disarm();
}
this._poller = new DeferredTask(
this._emitAllocations,
this.drainAllocationsTimeoutTimer,
0
);
this._poller.arm();
}
if (options.maxLogLength != null) {
this.dbg.memory.maxAllocationsLogLength = options.maxLogLength;
}
this.dbg.memory.trackingAllocationSites = true;
return this._getCurrentTime();
},
"starting recording allocations"
),
/**
* Stop recording allocation sites.
*/
stopRecordingAllocations: expectState(
"attached",
function() {
if (!this.isRecordingAllocations()) {
return this._getCurrentTime();
}
this.dbg.memory.trackingAllocationSites = false;
this._clearFrames();
if (this._poller) {
this._poller.disarm();
this._poller = null;
}
return this._getCurrentTime();
},
"stopping recording allocations"
),
/**
* Return settings used in `startRecordingAllocations` for `probability`
* and `maxLogLength`. Currently only uses in tests.
*/
getAllocationsSettings: expectState(
"attached",
function() {
return {
maxLogLength: this.dbg.memory.maxAllocationsLogLength,
probability: this.dbg.memory.allocationSamplingProbability,
};
},
"getting allocations settings"
),
/**
* Get a list of the most recent allocations since the last time we got
* allocations, as well as a summary of all allocations since we've been
* recording.
*
* @returns Object
* An object of the form:
*
* {
* allocations: [<index into "frames" below>, ...],
* allocationsTimestamps: [
* <timestamp for allocations[0]>,
* <timestamp for allocations[1]>,
* ...
* ],
* allocationSizes: [
* <bytesize for allocations[0]>,
* <bytesize for allocations[1]>,
* ...
* ],
* frames: [
* {
* line: <line number for this frame>,
* column: <column number for this frame>,
* source: <filename string for this frame>,
* functionDisplayName:
* <this frame's inferred function name function or null>,
* parent: <index into "frames">
* },
* ...
* ],
* }
*
* The timestamps' unit is microseconds since the epoch.
*
* Subsequent `getAllocations` request within the same recording and
* tab navigation will always place the same stack frames at the same
* indices as previous `getAllocations` requests in the same
* recording. In other words, it is safe to use the index as a
* unique, persistent id for its frame.
*
* Additionally, the root node (null) is always at index 0.
*
* We use the indices into the "frames" array to avoid repeating the
* description of duplicate stack frames both when listing
* allocations, and when many stacks share the same tail of older
* frames. There shouldn't be any duplicates in the "frames" array,
* as that would defeat the purpose of this compression trick.
*
* In the future, we might want to split out a frame's "source" and
* "functionDisplayName" properties out the same way we have split
* frames out with the "frames" array. While this would further
* compress the size of the response packet, it would increase CPU
* usage to build the packet, and it should, of course, be guided by
* profiling and done only when necessary.
*/
getAllocations: expectState(
"attached",
function() {
if (this.dbg.memory.allocationsLogOverflowed) {
// Since the last time we drained the allocations log, there have been
// more allocations than the log's capacity, and we lost some data. There
// isn't anything actionable we can do about this, but put a message in
// the browser console so we at least know that it occurred.
reportException(
"MemoryBridge.prototype.getAllocations",
"Warning: allocations log overflowed and lost some data."
);
}
const allocations = this.dbg.memory.drainAllocationsLog();
const packet = {
allocations: [],
allocationsTimestamps: [],
allocationSizes: [],
};
for (const { frame: stack, timestamp, size } of allocations) {
if (stack && Cu.isDeadWrapper(stack)) {
continue;
}
// Safe because SavedFrames are frozen/immutable.
const waived = Cu.waiveXrays(stack);
// Ensure that we have a form, size, and index for new allocations
// because we potentially haven't seen some or all of them yet. After this
// loop, we can rely on the fact that every frame we deal with already has
// its metadata stored.
const index = this._frameCache.addFrame(waived);
packet.allocations.push(index);
packet.allocationsTimestamps.push(timestamp);
packet.allocationSizes.push(size);
}
return this._frameCache.updateFramePacket(packet);
},
"getting allocations"
),
/*
* Force a browser-wide GC.
*/
forceGarbageCollection: function() {
for (let i = 0; i < 3; i++) {
Cu.forceGC();
}
},
/**
* Force an XPCOM cycle collection. For more information on XPCOM cycle
* collection, see
* https://developer.mozilla.org/en-US/docs/Interfacing_with_the_XPCOM_cycle_collector#What_the_cycle_collector_does
*/
forceCycleCollection: function() {
Cu.forceCC();
},
/**
* A method that returns a detailed breakdown of the memory consumption of the
* associated window.
*
* @returns object
*/
measure: function() {
const result = {};
const jsObjectsSize = {};
const jsStringsSize = {};
const jsOtherSize = {};
const domSize = {};
const styleSize = {};
const otherSize = {};
const totalSize = {};
const jsMilliseconds = {};
const nonJSMilliseconds = {};
try {
this._mgr.sizeOfTab(
this.parent.window,
jsObjectsSize,
jsStringsSize,
jsOtherSize,
domSize,
styleSize,
otherSize,
totalSize,
jsMilliseconds,
nonJSMilliseconds
);
result.total = totalSize.value;
result.domSize = domSize.value;
result.styleSize = styleSize.value;
result.jsObjectsSize = jsObjectsSize.value;
result.jsStringsSize = jsStringsSize.value;
result.jsOtherSize = jsOtherSize.value;
result.otherSize = otherSize.value;
result.jsMilliseconds = jsMilliseconds.value.toFixed(1);
result.nonJSMilliseconds = nonJSMilliseconds.value.toFixed(1);
} catch (e) {
reportException("MemoryBridge.prototype.measure", e);
}
return result;
},
residentUnique: function() {
return this._mgr.residentUnique;
},
/**
* Handler for GC events on the Debugger.Memory instance.
*/
_onGarbageCollection: function(data) {
this.emit("garbage-collection", data);
// If `drainAllocationsTimeout` set, fire an allocations event with the drained log,
// which will restart the timer.
if (this._poller) {
this._poller.disarm();
this._emitAllocations();
}
},
/**
* Called on `drainAllocationsTimeoutTimer` interval if and only if set
* during `startRecordingAllocations`, or on a garbage collection event if
* drainAllocationsTimeout was set.
* Drains allocation log and emits as an event and restarts the timer.
*/
_emitAllocations: function() {
this.emit("allocations", this.getAllocations());
this._poller.arm();
},
/**
* Accesses the docshell to return the current process time.
*/
_getCurrentTime: function() {
const docShell = this.parent.isRootActor
? this.parent.docShell
: this.parent.originalDocShell;
if (docShell) {
return docShell.now();
}
// When used from the ContentProcessTargetActor, parent has no docShell,
// so fallback to Cu.now
return Cu.now();
},
};
exports.Memory = Memory;
|