memory_dump_track_test_utils.html revision cef7893435aa41160dd1255c43cb8498279738cc
1<!DOCTYPE html>
2<!--
3Copyright (c) 2015 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-->
7
8<link rel="import" href="/tracing/model/global_memory_dump.html">
9<link rel="import" href="/tracing/model/memory_dump_test_utils.html">
10<link rel="import" href="/tracing/model/model.html">
11<link rel="import" href="/tracing/model/process_memory_dump.html">
12<link rel="import" href="/tracing/model/selection_state.html">
13<link rel="import" href="/tracing/model/vm_region.html">
14
15<script>
16'use strict';
17
18/**
19 * @fileoverview Helper functions for memory dump track tests.
20 */
21tr.exportTo('tr.ui.tracks', function() {
22  var ProcessMemoryDump = tr.model.ProcessMemoryDump;
23  var GlobalMemoryDump = tr.model.GlobalMemoryDump;
24  var VMRegion = tr.model.VMRegion;
25  var VMRegionClassificationNode = tr.model.VMRegionClassificationNode;
26  var SelectionState = tr.model.SelectionState;
27  var newAllocatorDump = tr.model.MemoryDumpTestUtils.newAllocatorDump;
28  var addOwnershipLink = tr.model.MemoryDumpTestUtils.addOwnershipLink;
29
30  function createVMRegions(pssValues) {
31    return VMRegionClassificationNode.fromRegions(
32        pssValues.map(function(pssValue, i) {
33          return VMRegion.fromDict({
34            startAddress: 1000 * i,
35            sizeInBytes: 1000,
36            protectionFlags: VMRegion.PROTECTION_FLAG_READ,
37            mappedFile: '[stack' + i + ']',
38            byteStats: {
39              privateDirtyResident: pssValue / 3,
40              swapped: pssValue * 3,
41              proportionalResident: pssValue
42            }
43          });
44        }));
45  }
46
47  function createAllocatorDumps(memoryDump, dumpData) {
48    // Create the individual allocator dumps.
49    var allocatorDumps = tr.b.mapItems(dumpData, function(allocatorName, data) {
50      var size = data.size;
51      assert.typeOf(size, 'number');  // Sanity check.
52      return newAllocatorDump(memoryDump, allocatorName, { size: size });
53    });
54
55    // Add ownership links between them.
56    tr.b.iterItems(dumpData, function(allocatorName, data) {
57      var owns = data.owns;
58      if (owns === undefined)
59        return;
60
61      var ownerDump = allocatorDumps[allocatorName];
62      assert.isDefined(ownerDump);  // Sanity check.
63      var ownedDump = allocatorDumps[owns];
64      assert.isDefined(ownedDump);  // Sanity check.
65
66      addOwnershipLink(ownerDump, ownedDump);
67    });
68
69    return tr.b.dictionaryValues(allocatorDumps);
70  }
71
72  function addProcessMemoryDump(globalMemoryDump, process, start,
73      opt_pssValues, opt_dumpData) {
74    var pmd = new ProcessMemoryDump(globalMemoryDump, process, start);
75    if (opt_pssValues !== undefined)
76      pmd.vmRegions = createVMRegions(opt_pssValues);
77    if (opt_dumpData !== undefined)
78      pmd.memoryAllocatorDumps = createAllocatorDumps(pmd, opt_dumpData);
79    globalMemoryDump.processMemoryDumps[process.pid] = pmd;
80    process.memoryDumps.push(pmd);
81  }
82
83  function createModelWithDumps(withVMRegions, withAllocatorDumps) {
84    var maybePssValues = function(pssValues) {
85      return withVMRegions ? pssValues : undefined;
86    };
87    var maybeDumpData = function(dumpData) {
88      return withAllocatorDumps ? dumpData : undefined;
89    };
90    return tr.c.TestUtils.newModel(function(model) {
91      // Construct a model with three processes.
92      var pa = model.getOrCreateProcess(3);
93      var pb = model.getOrCreateProcess(6);
94      var pc = model.getOrCreateProcess(9);
95
96      var gmd1 = new GlobalMemoryDump(model, 0);
97      // Intentionally undefined level of detail.
98      model.globalMemoryDumps.push(gmd1);
99      addProcessMemoryDump(gmd1, pa, 0, maybePssValues([111]));
100      addProcessMemoryDump(gmd1, pb, 0.2, undefined,
101          maybeDumpData({oilpan: {size: 1024}}));
102
103      var gmd2 = new GlobalMemoryDump(model, 5);
104      gmd2.levelOfDetail = 'detailed';
105      model.globalMemoryDumps.push(gmd2);
106      addProcessMemoryDump(gmd2, pa, 0);
107      addProcessMemoryDump(gmd2, pb, 4.99, maybePssValues([100, 50]),
108          maybeDumpData({v8: {size: 512}}));
109      addProcessMemoryDump(gmd2, pc, 5.12, undefined,
110          maybeDumpData({oilpan: {size: 128, owns: 'v8'},
111              v8: {size: 384, owns: 'tracing'}, tracing: {size: 65920}}));
112
113      var gmd3 = new GlobalMemoryDump(model, 15);
114      gmd3.levelOfDetail = 'detailed';
115      model.globalMemoryDumps.push(gmd3);
116      addProcessMemoryDump(gmd3, pa, 15.5, maybePssValues([]),
117          maybeDumpData({v8: {size: 768}}));
118      addProcessMemoryDump(gmd3, pc, 14.5, maybePssValues([70, 70, 70]),
119          maybeDumpData({oilpan: {size: 512}}));
120
121      var gmd4 = new GlobalMemoryDump(model, 18);
122      gmd4.levelOfDetail = 'light';
123      model.globalMemoryDumps.push(gmd4);
124    });
125  }
126
127  function createTestGlobalMemoryDumps(withVMRegions, withAllocatorDumps) {
128    var model = createModelWithDumps(withVMRegions, withAllocatorDumps);
129    var dumps = model.globalMemoryDumps;
130    dumps[1].selectionState = SelectionState.HIGHLIGHTED;
131    dumps[2].selectionState = SelectionState.SELECTED;
132    return dumps;
133  }
134
135  function createTestProcessMemoryDumps(withVMRegions, withAllocatorDumps) {
136    var model = createModelWithDumps(withVMRegions, withAllocatorDumps);
137    var dumps = model.getProcess(9).memoryDumps;
138    dumps[0].selectionState = SelectionState.SELECTED;
139    dumps[1].selectionState = SelectionState.HIGHLIGHTED;
140    return dumps;
141  }
142
143  return {
144    createTestGlobalMemoryDumps: createTestGlobalMemoryDumps,
145    createTestProcessMemoryDumps: createTestProcessMemoryDumps
146  };
147});
148</script>
149