michael@0: /* -*- js-indent-level: 4; tab-width: 4; indent-tabs-mode: nil -*- */ michael@0: /* vim:set ts=4 sw=4 sts=4 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var MemoryStats = {}; michael@0: michael@0: /** michael@0: * Statistics that we want to retrieve and display after every test is michael@0: * done. The keys of this table are intended to be identical to the michael@0: * relevant attributes of nsIMemoryReporterManager. However, since michael@0: * nsIMemoryReporterManager doesn't necessarily support all these michael@0: * statistics in all build configurations, we also use this table to michael@0: * tell us whether statistics are supported or not. michael@0: */ michael@0: var MEM_STAT_UNKNOWN = 0; michael@0: var MEM_STAT_UNSUPPORTED = 1; michael@0: var MEM_STAT_SUPPORTED = 2; michael@0: michael@0: MemoryStats._hasMemoryStatistics = {} michael@0: MemoryStats._hasMemoryStatistics.vsize = MEM_STAT_UNKNOWN; michael@0: MemoryStats._hasMemoryStatistics.vsizeMaxContiguous = MEM_STAT_UNKNOWN; michael@0: MemoryStats._hasMemoryStatistics.residentFast = MEM_STAT_UNKNOWN; michael@0: MemoryStats._hasMemoryStatistics.heapAllocated = MEM_STAT_UNKNOWN; michael@0: michael@0: MemoryStats._getService = function (className, interfaceName) { michael@0: var service; michael@0: try { michael@0: service = Cc[className].getService(Ci[interfaceName]); michael@0: } catch (e) { michael@0: service = SpecialPowers.Cc[className] michael@0: .getService(SpecialPowers.Ci[interfaceName]); michael@0: } michael@0: return service; michael@0: } michael@0: michael@0: MemoryStats._nsIFile = function (pathname) { michael@0: var f; michael@0: var contractID = "@mozilla.org/file/local;1"; michael@0: try { michael@0: f = Cc[contractID].createInstance(Ci.nsIFile); michael@0: } catch(e) { michael@0: f = SpecialPowers.Cc[contractID].createInstance(SpecialPowers.Ci.nsIFile); michael@0: } michael@0: f.initWithPath(pathname); michael@0: return f; michael@0: } michael@0: michael@0: MemoryStats.constructPathname = function (directory, basename) { michael@0: var d = MemoryStats._nsIFile(directory); michael@0: d.append(basename); michael@0: return d.path; michael@0: } michael@0: michael@0: MemoryStats.dump = function (dumpFn, michael@0: testNumber, michael@0: testURL, michael@0: dumpOutputDirectory, michael@0: dumpAboutMemory, michael@0: dumpDMD) { michael@0: var mrm = MemoryStats._getService("@mozilla.org/memory-reporter-manager;1", michael@0: "nsIMemoryReporterManager"); michael@0: for (var stat in MemoryStats._hasMemoryStatistics) { michael@0: var supported = MemoryStats._hasMemoryStatistics[stat]; michael@0: var firstAccess = false; michael@0: if (supported == MEM_STAT_UNKNOWN) { michael@0: firstAccess = true; michael@0: try { michael@0: var value = mrm[stat]; michael@0: supported = MEM_STAT_SUPPORTED; michael@0: } catch (e) { michael@0: supported = MEM_STAT_UNSUPPORTED; michael@0: } michael@0: MemoryStats._hasMemoryStatistics[stat] = supported; michael@0: } michael@0: if (supported == MEM_STAT_SUPPORTED) { michael@0: dumpFn("TEST-INFO | MEMORY STAT " + stat + " after test: " + mrm[stat]); michael@0: } else if (firstAccess) { michael@0: dumpFn("TEST-INFO | MEMORY STAT " + stat + " not supported in this build configuration."); michael@0: } michael@0: } michael@0: michael@0: if (dumpAboutMemory) { michael@0: var basename = "about-memory-" + testNumber + ".json.gz"; michael@0: var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory, michael@0: basename); michael@0: dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-START " + dumpfile); michael@0: var md = MemoryStats._getService("@mozilla.org/memory-info-dumper;1", michael@0: "nsIMemoryInfoDumper"); michael@0: md.dumpMemoryReportsToNamedFile(dumpfile, function () { michael@0: dumpFn("TEST-INFO | " + testURL + " | MEMDUMP-END"); michael@0: }, null); michael@0: michael@0: } michael@0: michael@0: if (dumpDMD && typeof(DMDReportAndDump) != undefined) { michael@0: var basename = "dmd-" + testNumber + ".txt"; michael@0: var dumpfile = MemoryStats.constructPathname(dumpOutputDirectory, michael@0: basename); michael@0: dumpFn("TEST-INFO | " + testURL + " | DMD-DUMP " + dumpfile); michael@0: DMDReportAndDump(dumpfile); michael@0: } michael@0: };