summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/SimpleTest/MemoryStats.js
blob: 40548697eab821bf2101011ee775cdcce1902efc (plain)
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
/* -*- js-indent-level: 4; indent-tabs-mode: nil -*- */
/* vim:set ts=4 sw=4 sts=4 et: */
/* 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/. */

var MemoryStats = {};

/**
 * Statistics that we want to retrieve and display after every test is
 * done.  The keys of this table are intended to be identical to the
 * relevant attributes of nsIMemoryReporterManager.  However, since
 * nsIMemoryReporterManager doesn't necessarily support all these
 * statistics in all build configurations, we also use this table to
 * tell us whether statistics are supported or not.
 */
var MEM_STAT_UNKNOWN = 0;
var MEM_STAT_UNSUPPORTED = 1;
var MEM_STAT_SUPPORTED = 2;

MemoryStats._hasMemoryStatistics = {};
MemoryStats._hasMemoryStatistics.vsize = MEM_STAT_UNKNOWN;
MemoryStats._hasMemoryStatistics.vsizeMaxContiguous = MEM_STAT_UNKNOWN;
MemoryStats._hasMemoryStatistics.residentFast = MEM_STAT_UNKNOWN;
MemoryStats._hasMemoryStatistics.heapAllocated = MEM_STAT_UNKNOWN;

MemoryStats._getService = function (className, interfaceName) {
  var service;
  try {
    service = Cc[className].getService(Ci[interfaceName]);
  } catch (e) {
    service = SpecialPowers.Cc[className].getService(
      SpecialPowers.Ci[interfaceName]
    );
  }
  return service;
};

MemoryStats._nsIFile = function (pathname) {
  var f;
  var contractID = "@mozilla.org/file/local;1";
  try {
    f = Cc[contractID].createInstance(Ci.nsIFile);
  } catch (e) {
    f = SpecialPowers.Cc[contractID].createInstance(SpecialPowers.Ci.nsIFile);
  }
  f.initWithPath(pathname);
  return f;
};

MemoryStats.constructPathname = function (directory, basename) {
  var d = MemoryStats._nsIFile(directory);
  d.append(basename);
  return d.path;
};

MemoryStats.dump = function (
  testNumber,
  testURL,
  dumpOutputDirectory,
  dumpAboutMemory,
  dumpDMD
) {
  // Use dump because treeherder uses --quiet, which drops 'info'
  // from the structured logger.
  var info = function (message) {
    dump(message + "\n");
  };

  var mrm = MemoryStats._getService(
    "@mozilla.org/memory-reporter-manager;1",
    "nsIMemoryReporterManager"
  );
  var statMessage = "";
  for (var stat in MemoryStats._hasMemoryStatistics) {
    var supported = MemoryStats._hasMemoryStatistics[stat];
    var firstAccess = false;
    if (supported == MEM_STAT_UNKNOWN) {
      firstAccess = true;
      try {
        void mrm[stat];
        supported = MEM_STAT_SUPPORTED;
      } catch (e) {
        supported = MEM_STAT_UNSUPPORTED;
      }
      MemoryStats._hasMemoryStatistics[stat] = supported;
    }
    if (supported == MEM_STAT_SUPPORTED) {
      var sizeInMB = Math.round(mrm[stat] / (1024 * 1024));
      statMessage += " | " + stat + " " + sizeInMB + "MB";
    } else if (firstAccess) {
      info(
        "MEMORY STAT " + stat + " not supported in this build configuration."
      );
    }
  }
  if (statMessage.length) {
    info("MEMORY STAT" + statMessage);
  }

  if (dumpAboutMemory) {
    var basename = "about-memory-" + testNumber + ".json.gz";
    var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory, basename);
    info(testURL + " | MEMDUMP-START " + dumpfile);
    let md = MemoryStats._getService(
      "@mozilla.org/memory-info-dumper;1",
      "nsIMemoryInfoDumper"
    );
    md.dumpMemoryReportsToNamedFile(
      dumpfile,
      function () {
        info("TEST-INFO | " + testURL + " | MEMDUMP-END");
      },
      null,
      /* anonymize = */ false,
      /* minimize memory usage = */ false
    );
  }

  if (dumpDMD) {
    let md = MemoryStats._getService(
      "@mozilla.org/memory-info-dumper;1",
      "nsIMemoryInfoDumper"
    );
    md.dumpMemoryInfoToTempDir(
      String(testNumber),
      /* anonymize = */ false,
      /* minimize memory usage = */ false
    );
  }
};