tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MemoryStats.js (3918B)


      1 /* -*- js-indent-level: 4; indent-tabs-mode: nil -*- */
      2 /* vim:set ts=4 sw=4 sts=4 et: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 var MemoryStats = {};
      8 
      9 /**
     10 * Statistics that we want to retrieve and display after every test is
     11 * done.  The keys of this table are intended to be identical to the
     12 * relevant attributes of nsIMemoryReporterManager.  However, since
     13 * nsIMemoryReporterManager doesn't necessarily support all these
     14 * statistics in all build configurations, we also use this table to
     15 * tell us whether statistics are supported or not.
     16 */
     17 var MEM_STAT_UNKNOWN = 0;
     18 var MEM_STAT_UNSUPPORTED = 1;
     19 var MEM_STAT_SUPPORTED = 2;
     20 
     21 MemoryStats._hasMemoryStatistics = {};
     22 MemoryStats._hasMemoryStatistics.vsize = MEM_STAT_UNKNOWN;
     23 MemoryStats._hasMemoryStatistics.vsizeMaxContiguous = MEM_STAT_UNKNOWN;
     24 MemoryStats._hasMemoryStatistics.residentFast = MEM_STAT_UNKNOWN;
     25 MemoryStats._hasMemoryStatistics.heapAllocated = MEM_STAT_UNKNOWN;
     26 
     27 MemoryStats._getService = function (className, interfaceName) {
     28  var service;
     29  try {
     30    service = Cc[className].getService(Ci[interfaceName]);
     31  } catch (e) {
     32    service = SpecialPowers.Cc[className].getService(
     33      SpecialPowers.Ci[interfaceName]
     34    );
     35  }
     36  return service;
     37 };
     38 
     39 MemoryStats._nsIFile = function (pathname) {
     40  var f;
     41  var contractID = "@mozilla.org/file/local;1";
     42  try {
     43    f = Cc[contractID].createInstance(Ci.nsIFile);
     44  } catch (e) {
     45    f = SpecialPowers.Cc[contractID].createInstance(SpecialPowers.Ci.nsIFile);
     46  }
     47  f.initWithPath(pathname);
     48  return f;
     49 };
     50 
     51 MemoryStats.constructPathname = function (directory, basename) {
     52  var d = MemoryStats._nsIFile(directory);
     53  d.append(basename);
     54  return d.path;
     55 };
     56 
     57 MemoryStats.dump = function (
     58  testNumber,
     59  testURL,
     60  dumpOutputDirectory,
     61  dumpAboutMemory,
     62  dumpDMD
     63 ) {
     64  // Use dump because treeherder uses --quiet, which drops 'info'
     65  // from the structured logger.
     66  var info = function (message) {
     67    dump(message + "\n");
     68  };
     69 
     70  var mrm = MemoryStats._getService(
     71    "@mozilla.org/memory-reporter-manager;1",
     72    "nsIMemoryReporterManager"
     73  );
     74  var statMessage = "";
     75  for (var stat in MemoryStats._hasMemoryStatistics) {
     76    var supported = MemoryStats._hasMemoryStatistics[stat];
     77    var firstAccess = false;
     78    if (supported == MEM_STAT_UNKNOWN) {
     79      firstAccess = true;
     80      try {
     81        void mrm[stat];
     82        supported = MEM_STAT_SUPPORTED;
     83      } catch (e) {
     84        supported = MEM_STAT_UNSUPPORTED;
     85      }
     86      MemoryStats._hasMemoryStatistics[stat] = supported;
     87    }
     88    if (supported == MEM_STAT_SUPPORTED) {
     89      var sizeInMB = Math.round(mrm[stat] / (1024 * 1024));
     90      statMessage += " | " + stat + " " + sizeInMB + "MB";
     91    } else if (firstAccess) {
     92      info(
     93        "MEMORY STAT " + stat + " not supported in this build configuration."
     94      );
     95    }
     96  }
     97  if (statMessage.length) {
     98    info("MEMORY STAT" + statMessage);
     99  }
    100 
    101  if (dumpAboutMemory) {
    102    var basename = "about-memory-" + testNumber + ".json.gz";
    103    var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory, basename);
    104    info(testURL + " | MEMDUMP-START " + dumpfile);
    105    let md = MemoryStats._getService(
    106      "@mozilla.org/memory-info-dumper;1",
    107      "nsIMemoryInfoDumper"
    108    );
    109    md.dumpMemoryReportsToNamedFile(
    110      dumpfile,
    111      function () {
    112        info("TEST-INFO | " + testURL + " | MEMDUMP-END");
    113      },
    114      null,
    115      /* anonymize = */ false,
    116      /* minimize memory usage = */ false
    117    );
    118  }
    119 
    120  if (dumpDMD) {
    121    let md = MemoryStats._getService(
    122      "@mozilla.org/memory-info-dumper;1",
    123      "nsIMemoryInfoDumper"
    124    );
    125    md.dumpMemoryInfoToTempDir(
    126      String(testNumber),
    127      /* anonymize = */ false,
    128      /* minimize memory usage = */ false
    129    );
    130  }
    131 };