testing/mochitest/tests/SimpleTest/MemoryStats.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- js-indent-level: 4; tab-width: 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/. */
     7 var MemoryStats = {};
     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;
    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;
    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]
    33                                .getService(SpecialPowers.Ci[interfaceName]);
    34     }
    35     return service;
    36 }
    38 MemoryStats._nsIFile = function (pathname) {
    39     var f;
    40     var contractID = "@mozilla.org/file/local;1";
    41     try {
    42         f = Cc[contractID].createInstance(Ci.nsIFile);
    43     } catch(e) {
    44         f = SpecialPowers.Cc[contractID].createInstance(SpecialPowers.Ci.nsIFile);
    45     }
    46     f.initWithPath(pathname);
    47     return f;
    48 }
    50 MemoryStats.constructPathname = function (directory, basename) {
    51     var d = MemoryStats._nsIFile(directory);
    52     d.append(basename);
    53     return d.path;
    54 }
    56 MemoryStats.dump = function (dumpFn,
    57                              testNumber,
    58                              testURL,
    59                              dumpOutputDirectory,
    60                              dumpAboutMemory,
    61                              dumpDMD) {
    62     var mrm = MemoryStats._getService("@mozilla.org/memory-reporter-manager;1",
    63                                       "nsIMemoryReporterManager");
    64     for (var stat in MemoryStats._hasMemoryStatistics) {
    65         var supported = MemoryStats._hasMemoryStatistics[stat];
    66         var firstAccess = false;
    67         if (supported == MEM_STAT_UNKNOWN) {
    68             firstAccess = true;
    69             try {
    70                 var value = mrm[stat];
    71                 supported = MEM_STAT_SUPPORTED;
    72             } catch (e) {
    73                 supported = MEM_STAT_UNSUPPORTED;
    74             }
    75             MemoryStats._hasMemoryStatistics[stat] = supported;
    76         }
    77         if (supported == MEM_STAT_SUPPORTED) {
    78             dumpFn("TEST-INFO | MEMORY STAT " + stat + " after test: " + mrm[stat]);
    79         } else if (firstAccess) {
    80             dumpFn("TEST-INFO | MEMORY STAT " + stat + " not supported in this build configuration.");
    81         }
    82     }
    84     if (dumpAboutMemory) {
    85         var basename = "about-memory-" + testNumber + ".json.gz";
    86         var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory,
    87                                                      basename);
    88         dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-START " + dumpfile);
    89         var md = MemoryStats._getService("@mozilla.org/memory-info-dumper;1",
    90                                          "nsIMemoryInfoDumper");
    91         md.dumpMemoryReportsToNamedFile(dumpfile, function () {
    92             dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-END");
    93         }, null);
    95     }
    97     if (dumpDMD && typeof(DMDReportAndDump) != undefined) {
    98         var basename = "dmd-" + testNumber + ".txt";
    99         var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory,
   100                                                      basename);
   101         dumpFn("TEST-INFO | " + testURL + " | DMD-DUMP " + dumpfile);
   102         DMDReportAndDump(dumpfile);
   103     }
   104 };

mercurial