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

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

mercurial