2013-03-28 07:31:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-10-16 06:12:14 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2012-10-30 04:41:14 +04:00
|
|
|
#include "mozilla/nsMemoryInfoDumper.h"
|
2014-03-20 11:29:51 +04:00
|
|
|
#include "nsDumpUtils.h"
|
2012-10-16 06:12:14 +04:00
|
|
|
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "nsIConsoleService.h"
|
2014-05-13 21:13:00 +04:00
|
|
|
#include "nsCycleCollector.h"
|
2012-10-16 06:12:14 +04:00
|
|
|
#include "nsICycleCollectorListener.h"
|
2013-01-18 09:43:21 +04:00
|
|
|
#include "nsIMemoryReporter.h"
|
2012-10-16 06:12:14 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsGZFileWriter.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsPrintfCString.h"
|
2013-09-24 01:30:40 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2013-09-11 00:56:05 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIFile.h"
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include <process.h>
|
2014-07-03 02:59:02 +04:00
|
|
|
#ifndef getpid
|
2012-10-16 06:12:14 +04:00
|
|
|
#define getpid _getpid
|
2014-07-03 02:59:02 +04:00
|
|
|
#endif
|
2012-10-16 06:12:14 +04:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-29 07:24:00 +04:00
|
|
|
#ifdef XP_UNIX
|
2014-04-25 04:35:59 +04:00
|
|
|
#define MOZ_SUPPORTS_FIFO 1
|
|
|
|
#endif
|
|
|
|
|
2013-12-05 18:38:46 +04:00
|
|
|
#if defined(XP_LINUX) || defined(__FreeBSD__)
|
2014-04-25 04:35:59 +04:00
|
|
|
#define MOZ_SUPPORTS_RT_SIGNALS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOZ_SUPPORTS_RT_SIGNALS)
|
2012-10-16 06:12:14 +04:00
|
|
|
#include <fcntl.h>
|
2013-01-14 10:43:25 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2014-04-25 04:35:59 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MOZ_SUPPORTS_FIFO)
|
2014-04-25 02:58:57 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-10-16 06:12:14 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
class DumpMemoryInfoToTempDirRunnable : public nsRunnable
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-03-28 07:31:26 +04:00
|
|
|
DumpMemoryInfoToTempDirRunnable(const nsAString& aIdentifier,
|
2014-05-21 10:06:54 +04:00
|
|
|
bool aAnonymize, bool aMinimizeMemoryUsage)
|
2014-05-05 21:30:39 +04:00
|
|
|
: mIdentifier(aIdentifier)
|
2014-05-21 10:06:54 +04:00
|
|
|
, mAnonymize(aAnonymize)
|
2014-05-05 21:30:39 +04:00
|
|
|
, mMinimizeMemoryUsage(aMinimizeMemoryUsage)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
nsCOMPtr<nsIMemoryInfoDumper> dumper =
|
|
|
|
do_GetService("@mozilla.org/memory-info-dumper;1");
|
2014-05-21 10:06:54 +04:00
|
|
|
dumper->DumpMemoryInfoToTempDir(mIdentifier, mAnonymize,
|
|
|
|
mMinimizeMemoryUsage);
|
2012-10-16 06:12:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const nsString mIdentifier;
|
2014-05-21 10:06:54 +04:00
|
|
|
const bool mAnonymize;
|
2012-10-16 06:12:14 +04:00
|
|
|
const bool mMinimizeMemoryUsage;
|
|
|
|
};
|
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
class GCAndCCLogDumpRunnable MOZ_FINAL
|
|
|
|
: public nsRunnable
|
|
|
|
, public nsIDumpGCAndCCLogsCallback
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-05-13 21:13:00 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
GCAndCCLogDumpRunnable(const nsAString& aIdentifier,
|
2013-07-08 21:20:39 +04:00
|
|
|
bool aDumpAllTraces,
|
2012-10-16 06:12:14 +04:00
|
|
|
bool aDumpChildProcesses)
|
|
|
|
: mIdentifier(aIdentifier)
|
2013-07-08 21:20:39 +04:00
|
|
|
, mDumpAllTraces(aDumpAllTraces)
|
2012-10-16 06:12:14 +04:00
|
|
|
, mDumpChildProcesses(aDumpChildProcesses)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
2013-07-08 21:20:39 +04:00
|
|
|
nsCOMPtr<nsIMemoryInfoDumper> dumper =
|
|
|
|
do_GetService("@mozilla.org/memory-info-dumper;1");
|
|
|
|
|
2014-02-01 02:43:08 +04:00
|
|
|
dumper->DumpGCAndCCLogsToFile(mIdentifier, mDumpAllTraces,
|
2014-05-13 21:13:00 +04:00
|
|
|
mDumpChildProcesses, this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnDump(nsIFile* aGCLog, nsIFile* aCCLog, bool aIsParent) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnFinish() MOZ_OVERRIDE
|
|
|
|
{
|
2012-10-16 06:12:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-07-09 01:23:17 +04:00
|
|
|
~GCAndCCLogDumpRunnable() {}
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
const nsString mIdentifier;
|
2013-07-08 21:20:39 +04:00
|
|
|
const bool mDumpAllTraces;
|
2012-10-16 06:12:14 +04:00
|
|
|
const bool mDumpChildProcesses;
|
|
|
|
};
|
|
|
|
|
2014-08-25 23:17:15 +04:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(GCAndCCLogDumpRunnable, nsRunnable,
|
|
|
|
nsIDumpGCAndCCLogsCallback)
|
2014-05-13 21:13:00 +04:00
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-04-25 04:35:59 +04:00
|
|
|
#if defined(MOZ_SUPPORTS_RT_SIGNALS) // {
|
2012-10-16 06:12:14 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following code supports dumping about:memory upon receiving a signal.
|
|
|
|
*
|
|
|
|
* We listen for the following signals:
|
|
|
|
*
|
|
|
|
* - SIGRTMIN: Dump our memory reporters (and those of our child
|
|
|
|
* processes),
|
|
|
|
* - SIGRTMIN + 1: Dump our memory reporters (and those of our child
|
|
|
|
* processes) after minimizing memory usage, and
|
|
|
|
* - SIGRTMIN + 2: Dump the GC and CC logs in this and our child processes.
|
|
|
|
*
|
|
|
|
* When we receive one of these signals, we write the signal number to a pipe.
|
|
|
|
* The IO thread then notices that the pipe has been written to, and kicks off
|
|
|
|
* the appropriate task on the main thread.
|
|
|
|
*
|
|
|
|
* This scheme is similar to using signalfd(), except it's portable and it
|
|
|
|
* doesn't require the use of sigprocmask, which is problematic because it
|
|
|
|
* masks signals received by child processes.
|
|
|
|
*
|
|
|
|
* In theory, we could use Chromium's MessageLoopForIO::CatchSignal() for this.
|
|
|
|
* But that uses libevent, which does not handle the realtime signals (bug
|
|
|
|
* 794074).
|
|
|
|
*/
|
|
|
|
|
|
|
|
// It turns out that at least on some systems, SIGRTMIN is not a compile-time
|
|
|
|
// constant, so these have to be set at runtime.
|
2014-03-20 11:29:51 +04:00
|
|
|
static uint8_t sDumpAboutMemorySignum; // SIGRTMIN
|
|
|
|
static uint8_t sDumpAboutMemoryAfterMMUSignum; // SIGRTMIN + 1
|
|
|
|
static uint8_t sGCAndCCDumpSignum; // SIGRTMIN + 2
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2014-05-15 00:14:58 +04:00
|
|
|
void doMemoryReport(const uint8_t aRecvSig)
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
2014-03-20 11:29:51 +04:00
|
|
|
// Dump our memory reports (but run this on the main thread!).
|
2014-05-21 10:06:54 +04:00
|
|
|
bool minimize = aRecvSig == sDumpAboutMemoryAfterMMUSignum;
|
2014-05-15 00:14:58 +04:00
|
|
|
LOG("SignalWatcher(sig %d) dispatching memory report runnable.", aRecvSig);
|
2014-03-20 11:29:51 +04:00
|
|
|
nsRefPtr<DumpMemoryInfoToTempDirRunnable> runnable =
|
|
|
|
new DumpMemoryInfoToTempDirRunnable(/* identifier = */ EmptyString(),
|
2014-05-21 10:06:54 +04:00
|
|
|
/* anonymize = */ false,
|
|
|
|
minimize);
|
2014-03-20 11:29:51 +04:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
2013-01-14 10:43:25 +04:00
|
|
|
|
2014-05-15 00:14:58 +04:00
|
|
|
void doGCCCDump(const uint8_t aRecvSig)
|
2013-01-14 10:43:25 +04:00
|
|
|
{
|
2014-05-15 00:14:58 +04:00
|
|
|
LOG("SignalWatcher(sig %d) dispatching GC/CC log runnable.", aRecvSig);
|
2014-03-20 11:29:51 +04:00
|
|
|
// Dump GC and CC logs (from the main thread).
|
|
|
|
nsRefPtr<GCAndCCLogDumpRunnable> runnable =
|
2014-08-25 23:17:15 +04:00
|
|
|
new GCAndCCLogDumpRunnable(/* identifier = */ EmptyString(),
|
|
|
|
/* allTraces = */ true,
|
|
|
|
/* dumpChildProcesses = */ true);
|
2014-03-20 11:29:51 +04:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
2013-01-14 10:43:25 +04:00
|
|
|
|
2014-04-25 04:35:59 +04:00
|
|
|
} // anonymous namespace
|
|
|
|
#endif // MOZ_SUPPORTS_RT_SIGNALS }
|
|
|
|
|
|
|
|
#if defined(MOZ_SUPPORTS_FIFO) // {
|
|
|
|
namespace {
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void
|
|
|
|
doMemoryReport(const nsCString& aInputStr)
|
2014-04-25 04:35:59 +04:00
|
|
|
{
|
2014-05-21 10:06:54 +04:00
|
|
|
bool minimize = aInputStr.EqualsLiteral("minimize memory report");
|
2014-08-25 23:17:15 +04:00
|
|
|
LOG("FifoWatcher(command:%s) dispatching memory report runnable.",
|
|
|
|
aInputStr.get());
|
2014-04-25 04:35:59 +04:00
|
|
|
nsRefPtr<DumpMemoryInfoToTempDirRunnable> runnable =
|
|
|
|
new DumpMemoryInfoToTempDirRunnable(/* identifier = */ EmptyString(),
|
2014-05-21 10:06:54 +04:00
|
|
|
/* anonymize = */ false,
|
|
|
|
minimize);
|
2014-04-25 04:35:59 +04:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void
|
|
|
|
doGCCCDump(const nsCString& aInputStr)
|
2014-04-25 04:35:59 +04:00
|
|
|
{
|
2014-05-22 07:48:50 +04:00
|
|
|
bool doAllTracesGCCCDump = aInputStr.EqualsLiteral("gc log");
|
2014-05-13 21:41:38 +04:00
|
|
|
LOG("FifoWatcher(command:%s) dispatching GC/CC log runnable.", aInputStr.get());
|
2014-04-25 04:35:59 +04:00
|
|
|
nsRefPtr<GCAndCCLogDumpRunnable> runnable =
|
|
|
|
new GCAndCCLogDumpRunnable(/* identifier = */ EmptyString(),
|
2014-05-05 21:30:39 +04:00
|
|
|
doAllTracesGCCCDump,
|
|
|
|
/* dumpChildProcesses = */ true);
|
2014-04-25 04:35:59 +04:00
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
bool
|
|
|
|
SetupFifo()
|
2014-04-25 02:58:57 +04:00
|
|
|
{
|
|
|
|
static bool fifoCallbacksRegistered = false;
|
|
|
|
|
|
|
|
if (!FifoWatcher::MaybeCreate()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!fifoCallbacksRegistered,
|
|
|
|
"FifoWatcher callbacks should be registered only once");
|
|
|
|
|
|
|
|
FifoWatcher* fw = FifoWatcher::GetSingleton();
|
|
|
|
// Dump our memory reports (but run this on the main thread!).
|
|
|
|
fw->RegisterCallback(NS_LITERAL_CSTRING("memory report"),
|
|
|
|
doMemoryReport);
|
|
|
|
fw->RegisterCallback(NS_LITERAL_CSTRING("minimize memory report"),
|
|
|
|
doMemoryReport);
|
|
|
|
// Dump GC and CC logs (from the main thread).
|
|
|
|
fw->RegisterCallback(NS_LITERAL_CSTRING("gc log"),
|
|
|
|
doGCCCDump);
|
|
|
|
fw->RegisterCallback(NS_LITERAL_CSTRING("abbreviated gc log"),
|
|
|
|
doGCCCDump);
|
|
|
|
|
|
|
|
fifoCallbacksRegistered = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
void
|
|
|
|
OnFifoEnabledChange(const char* /*unused*/, void* /*unused*/)
|
2014-04-25 02:58:57 +04:00
|
|
|
{
|
|
|
|
LOG("%s changed", FifoWatcher::kPrefName);
|
|
|
|
if (SetupFifo()) {
|
|
|
|
Preferences::UnregisterCallback(OnFifoEnabledChange,
|
|
|
|
FifoWatcher::kPrefName,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 10:43:25 +04:00
|
|
|
} // anonymous namespace
|
2014-04-25 04:35:59 +04:00
|
|
|
#endif // MOZ_SUPPORTS_FIFO }
|
2012-10-30 04:41:14 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsMemoryInfoDumper, nsIMemoryInfoDumper)
|
2012-10-30 04:41:14 +04:00
|
|
|
|
|
|
|
nsMemoryInfoDumper::nsMemoryInfoDumper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMemoryInfoDumper::~nsMemoryInfoDumper()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
/* static */ void
|
2012-10-30 04:41:14 +04:00
|
|
|
nsMemoryInfoDumper::Initialize()
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
2014-04-25 04:35:59 +04:00
|
|
|
#if defined(MOZ_SUPPORTS_RT_SIGNALS)
|
2014-03-20 11:29:51 +04:00
|
|
|
SignalPipeWatcher* sw = SignalPipeWatcher::GetSingleton();
|
|
|
|
|
|
|
|
// Dump memory reporters (and those of our child processes)
|
|
|
|
sDumpAboutMemorySignum = SIGRTMIN;
|
|
|
|
sw->RegisterCallback(sDumpAboutMemorySignum, doMemoryReport);
|
|
|
|
// Dump our memory reporters after minimizing memory usage
|
|
|
|
sDumpAboutMemoryAfterMMUSignum = SIGRTMIN + 1;
|
|
|
|
sw->RegisterCallback(sDumpAboutMemoryAfterMMUSignum, doMemoryReport);
|
|
|
|
// Dump the GC and CC logs in this and our child processes.
|
|
|
|
sGCAndCCDumpSignum = SIGRTMIN + 2;
|
|
|
|
sw->RegisterCallback(sGCAndCCDumpSignum, doGCCCDump);
|
2014-04-25 04:35:59 +04:00
|
|
|
#endif
|
2014-03-20 11:29:51 +04:00
|
|
|
|
2014-04-25 04:35:59 +04:00
|
|
|
#if defined(MOZ_SUPPORTS_FIFO)
|
2014-04-25 02:58:57 +04:00
|
|
|
if (!SetupFifo()) {
|
|
|
|
// NB: This gets loaded early enough that it's possible there is a user pref
|
|
|
|
// set to enable the fifo watcher that has not been loaded yet. Register
|
|
|
|
// to attempt to initialize if the fifo watcher becomes enabled by
|
|
|
|
// a user pref.
|
|
|
|
Preferences::RegisterCallback(OnFifoEnabledChange,
|
|
|
|
FifoWatcher::kPrefName,
|
|
|
|
nullptr);
|
2014-03-20 11:29:51 +04:00
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
static void
|
|
|
|
EnsureNonEmptyIdentifier(nsAString& aIdentifier)
|
|
|
|
{
|
|
|
|
if (!aIdentifier.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the identifier is empty, set it to the number of whole seconds since the
|
|
|
|
// epoch. This identifier will appear in the files that this process
|
|
|
|
// generates and also the files generated by this process's children, allowing
|
|
|
|
// us to identify which files are from the same memory report request.
|
|
|
|
aIdentifier.AppendInt(static_cast<int64_t>(PR_Now()) / 1000000);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
// Use XPCOM refcounting to fire |onFinish| when all reference-holders
|
|
|
|
// (remote dump actors or the |DumpGCAndCCLogsToFile| activation itself)
|
|
|
|
// have gone away.
|
2014-08-25 23:17:15 +04:00
|
|
|
class nsDumpGCAndCCLogsCallbackHolder MOZ_FINAL
|
|
|
|
: public nsIDumpGCAndCCLogsCallback
|
2014-05-13 21:13:00 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-08-05 17:36:32 +04:00
|
|
|
explicit nsDumpGCAndCCLogsCallbackHolder(nsIDumpGCAndCCLogsCallback* aCallback)
|
2014-05-13 21:13:00 +04:00
|
|
|
: mCallback(aCallback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP OnFinish()
|
|
|
|
{
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP OnDump(nsIFile* aGCLog, nsIFile* aCCLog, bool aIsParent)
|
|
|
|
{
|
|
|
|
return mCallback->OnDump(aGCLog, aCCLog, aIsParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsDumpGCAndCCLogsCallbackHolder()
|
|
|
|
{
|
|
|
|
unused << mCallback->OnFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDumpGCAndCCLogsCallback> mCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsDumpGCAndCCLogsCallbackHolder, nsIDumpGCAndCCLogsCallback)
|
|
|
|
|
2012-10-30 04:41:14 +04:00
|
|
|
NS_IMETHODIMP
|
2014-02-01 02:43:08 +04:00
|
|
|
nsMemoryInfoDumper::DumpGCAndCCLogsToFile(const nsAString& aIdentifier,
|
|
|
|
bool aDumpAllTraces,
|
|
|
|
bool aDumpChildProcesses,
|
2014-05-13 21:13:00 +04:00
|
|
|
nsIDumpGCAndCCLogsCallback* aCallback)
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
|
|
|
nsString identifier(aIdentifier);
|
2012-10-16 06:12:14 +04:00
|
|
|
EnsureNonEmptyIdentifier(identifier);
|
2014-05-13 21:13:00 +04:00
|
|
|
nsCOMPtr<nsIDumpGCAndCCLogsCallback> callbackHolder =
|
|
|
|
new nsDumpGCAndCCLogsCallbackHolder(aCallback);
|
2012-10-16 06:12:14 +04:00
|
|
|
|
|
|
|
if (aDumpChildProcesses) {
|
|
|
|
nsTArray<ContentParent*> children;
|
|
|
|
ContentParent::GetAll(children);
|
|
|
|
for (uint32_t i = 0; i < children.Length(); i++) {
|
2014-05-13 21:13:00 +04:00
|
|
|
ContentParent* cp = children[i];
|
|
|
|
nsCOMPtr<nsICycleCollectorLogSink> logSink =
|
|
|
|
nsCycleCollector_createLogSink();
|
|
|
|
|
|
|
|
logSink->SetFilenameIdentifier(identifier);
|
|
|
|
logSink->SetProcessIdentifier(cp->Pid());
|
|
|
|
|
|
|
|
unused << cp->CycleCollectWithLogs(aDumpAllTraces, logSink,
|
|
|
|
callbackHolder);
|
2012-10-16 06:12:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsICycleCollectorListener> logger =
|
|
|
|
do_CreateInstance("@mozilla.org/cycle-collector-logger;1");
|
|
|
|
|
2013-07-08 21:20:39 +04:00
|
|
|
if (aDumpAllTraces) {
|
|
|
|
nsCOMPtr<nsICycleCollectorListener> allTracesLogger;
|
|
|
|
logger->AllTraces(getter_AddRefs(allTracesLogger));
|
|
|
|
logger = allTracesLogger;
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
nsCOMPtr<nsICycleCollectorLogSink> logSink;
|
|
|
|
logger->GetLogSink(getter_AddRefs(logSink));
|
|
|
|
|
|
|
|
logSink->SetFilenameIdentifier(identifier);
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
nsJSContext::CycleCollectNow(logger);
|
2014-02-01 02:43:08 +04:00
|
|
|
|
2014-05-13 21:13:00 +04:00
|
|
|
nsCOMPtr<nsIFile> gcLog, ccLog;
|
|
|
|
logSink->GetGcLog(getter_AddRefs(gcLog));
|
|
|
|
logSink->GetCcLog(getter_AddRefs(ccLog));
|
|
|
|
callbackHolder->OnDump(gcLog, ccLog, /* parent = */ true);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryInfoDumper::DumpGCAndCCLogsToSink(bool aDumpAllTraces,
|
|
|
|
nsICycleCollectorLogSink* aSink)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsICycleCollectorListener> logger =
|
|
|
|
do_CreateInstance("@mozilla.org/cycle-collector-logger;1");
|
|
|
|
|
|
|
|
if (aDumpAllTraces) {
|
|
|
|
nsCOMPtr<nsICycleCollectorListener> allTracesLogger;
|
|
|
|
logger->AllTraces(getter_AddRefs(allTracesLogger));
|
|
|
|
logger = allTracesLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger->SetLogSink(aSink);
|
|
|
|
|
|
|
|
nsJSContext::CycleCollectNow(logger);
|
2014-02-01 02:43:08 +04:00
|
|
|
|
2012-10-30 04:41:14 +04:00
|
|
|
return NS_OK;
|
2012-10-16 06:12:14 +04:00
|
|
|
}
|
|
|
|
|
2012-10-30 04:41:14 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
#define DUMP(o, s) \
|
|
|
|
do { \
|
|
|
|
nsresult rv = (o)->Write(s); \
|
2013-11-20 01:27:37 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) \
|
|
|
|
return rv; \
|
2012-10-16 06:12:14 +04:00
|
|
|
} while (0)
|
|
|
|
|
2013-10-23 09:26:24 +04:00
|
|
|
class DumpReportCallback MOZ_FINAL : public nsIHandleReportCallback
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
2013-08-28 03:24:51 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-08-05 17:36:32 +04:00
|
|
|
explicit DumpReportCallback(nsGZFileWriter* aWriter)
|
2013-12-02 04:14:26 +04:00
|
|
|
: mIsFirst(true)
|
|
|
|
, mWriter(aWriter)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2013-08-28 03:24:51 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
NS_IMETHOD Callback(const nsACString& aProcess, const nsACString& aPath,
|
2014-05-05 21:30:39 +04:00
|
|
|
int32_t aKind, int32_t aUnits, int64_t aAmount,
|
2014-05-13 21:41:38 +04:00
|
|
|
const nsACString& aDescription,
|
|
|
|
nsISupports* aData)
|
2013-08-28 03:24:51 +04:00
|
|
|
{
|
2013-12-02 04:14:26 +04:00
|
|
|
if (mIsFirst) {
|
|
|
|
DUMP(mWriter, "[");
|
|
|
|
mIsFirst = false;
|
|
|
|
} else {
|
|
|
|
DUMP(mWriter, ",");
|
|
|
|
}
|
2013-08-28 03:24:51 +04:00
|
|
|
|
2013-12-02 04:14:26 +04:00
|
|
|
nsAutoCString process;
|
|
|
|
if (aProcess.IsEmpty()) {
|
|
|
|
// If the process is empty, the report originated with the process doing
|
|
|
|
// the dumping. In that case, generate the process identifier, which is of
|
|
|
|
// the form "$PROCESS_NAME (pid $PID)", or just "(pid $PID)" if we don't
|
|
|
|
// have a process name. If we're the main process, we let $PROCESS_NAME be
|
|
|
|
// "Main Process".
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
|
|
|
// We're the main process.
|
|
|
|
process.AssignLiteral("Main Process");
|
2014-05-13 21:41:38 +04:00
|
|
|
} else if (ContentChild* cc = ContentChild::GetSingleton()) {
|
2013-12-02 04:14:26 +04:00
|
|
|
// Try to get the process name from ContentChild.
|
|
|
|
cc->GetProcessName(process);
|
|
|
|
}
|
|
|
|
ContentChild::AppendProcessId(process);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Otherwise, the report originated with another process and already has a
|
|
|
|
// process name. Just use that.
|
|
|
|
process = aProcess;
|
|
|
|
}
|
|
|
|
|
|
|
|
DUMP(mWriter, "\n {\"process\": \"");
|
|
|
|
DUMP(mWriter, process);
|
|
|
|
|
|
|
|
DUMP(mWriter, "\", \"path\": \"");
|
|
|
|
nsCString path(aPath);
|
|
|
|
path.ReplaceSubstring("\\", "\\\\"); /* <backslash> --> \\ */
|
|
|
|
path.ReplaceSubstring("\"", "\\\""); // " --> \"
|
|
|
|
DUMP(mWriter, path);
|
|
|
|
|
|
|
|
DUMP(mWriter, "\", \"kind\": ");
|
|
|
|
DUMP(mWriter, nsPrintfCString("%d", aKind));
|
|
|
|
|
|
|
|
DUMP(mWriter, ", \"units\": ");
|
|
|
|
DUMP(mWriter, nsPrintfCString("%d", aUnits));
|
|
|
|
|
|
|
|
DUMP(mWriter, ", \"amount\": ");
|
|
|
|
DUMP(mWriter, nsPrintfCString("%lld", aAmount));
|
|
|
|
|
|
|
|
nsCString description(aDescription);
|
|
|
|
description.ReplaceSubstring("\\", "\\\\"); /* <backslash> --> \\ */
|
|
|
|
description.ReplaceSubstring("\"", "\\\""); // " --> \"
|
|
|
|
description.ReplaceSubstring("\n", "\\n"); // <newline> --> \n
|
|
|
|
DUMP(mWriter, ", \"description\": \"");
|
|
|
|
DUMP(mWriter, description);
|
|
|
|
DUMP(mWriter, "\"}");
|
|
|
|
|
|
|
|
return NS_OK;
|
2013-08-28 03:24:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-07-01 02:11:53 +04:00
|
|
|
~DumpReportCallback() {}
|
|
|
|
|
2013-10-23 03:58:33 +04:00
|
|
|
bool mIsFirst;
|
2013-12-02 04:14:26 +04:00
|
|
|
nsRefPtr<nsGZFileWriter> mWriter;
|
2012-10-16 06:12:14 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(DumpReportCallback, nsIHandleReportCallback)
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2012-10-30 04:41:14 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-12-11 04:05:07 +04:00
|
|
|
static void
|
2014-05-13 21:41:38 +04:00
|
|
|
MakeFilename(const char* aPrefix, const nsAString& aIdentifier,
|
2014-07-03 02:59:02 +04:00
|
|
|
int aPid, const char* aSuffix, nsACString& aResult)
|
2012-10-16 06:12:14 +04:00
|
|
|
{
|
2012-12-11 04:05:07 +04:00
|
|
|
aResult = nsPrintfCString("%s-%s-%d.%s",
|
|
|
|
aPrefix,
|
|
|
|
NS_ConvertUTF16toUTF8(aIdentifier).get(),
|
2014-07-03 02:59:02 +04:00
|
|
|
aPid, aSuffix);
|
2012-12-11 04:05:07 +04:00
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2012-12-11 04:05:07 +04:00
|
|
|
#ifdef MOZ_DMD
|
|
|
|
struct DMDWriteState
|
|
|
|
{
|
|
|
|
static const size_t kBufSize = 4096;
|
|
|
|
char mBuf[kBufSize];
|
|
|
|
nsRefPtr<nsGZFileWriter> mGZWriter;
|
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
DMDWriteState(nsGZFileWriter* aGZWriter)
|
2012-12-11 04:05:07 +04:00
|
|
|
: mGZWriter(aGZWriter)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2012-12-11 04:05:07 +04:00
|
|
|
};
|
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
static void
|
|
|
|
DMDWrite(void* aState, const char* aFmt, va_list ap)
|
2012-12-11 04:05:07 +04:00
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
DMDWriteState* state = (DMDWriteState*)aState;
|
2012-12-11 04:05:07 +04:00
|
|
|
vsnprintf(state->mBuf, state->kBufSize, aFmt, ap);
|
|
|
|
unused << state->mGZWriter->Write(state->mBuf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
static nsresult
|
2013-10-23 09:26:24 +04:00
|
|
|
DumpHeader(nsIGZFileWriter* aWriter)
|
2012-12-11 04:05:07 +04:00
|
|
|
{
|
2012-10-16 06:12:14 +04:00
|
|
|
// Increment this number if the format changes.
|
2013-01-16 06:44:11 +04:00
|
|
|
//
|
2013-03-28 07:31:26 +04:00
|
|
|
// This is the first write to the file, and it causes |aWriter| to allocate
|
2013-01-16 06:44:11 +04:00
|
|
|
// over 200 KiB of memory.
|
|
|
|
//
|
2013-03-28 07:31:26 +04:00
|
|
|
DUMP(aWriter, "{\n \"version\": 1,\n");
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
DUMP(aWriter, " \"hasMozMallocUsableSize\": ");
|
2012-10-16 06:12:14 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(!mgr)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
DUMP(aWriter, mgr->GetHasMozMallocUsableSize() ? "true" : "false");
|
|
|
|
DUMP(aWriter, ",\n");
|
|
|
|
DUMP(aWriter, " \"reports\": ");
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2013-10-23 09:26:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
DumpFooter(nsIGZFileWriter* aWriter)
|
|
|
|
{
|
|
|
|
DUMP(aWriter, "\n ]\n}\n");
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-05 06:27:13 +04:00
|
|
|
class TempDirMemoryFinishCallback MOZ_FINAL : public nsIFinishReportingCallback
|
2013-10-23 09:26:24 +04:00
|
|
|
{
|
2014-03-05 06:27:13 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2013-10-23 09:26:24 +04:00
|
|
|
|
2014-05-13 21:41:38 +04:00
|
|
|
TempDirMemoryFinishCallback(nsGZFileWriter* aWriter,
|
|
|
|
nsIFile* aTmpFile,
|
|
|
|
const nsCString& aFilename,
|
|
|
|
const nsString& aIdentifier)
|
2014-03-05 06:27:13 +04:00
|
|
|
: mrWriter(aWriter)
|
|
|
|
, mrTmpFile(aTmpFile)
|
|
|
|
, mrFilename(aFilename)
|
|
|
|
, mIdentifier(aIdentifier)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2014-09-10 11:27:45 +04:00
|
|
|
NS_IMETHOD Callback(nsISupports* aData)
|
|
|
|
{
|
|
|
|
nsresult rv = DumpFooter(mrWriter);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The call to Finish() deallocates the memory allocated by mrWriter's first
|
|
|
|
// DUMP() call (within DumpProcessMemoryReportsToGZFileWriter()). Because
|
|
|
|
// that memory was live while the memory reporters ran and thus measured by
|
|
|
|
// them -- by "heap-allocated" if nothing else -- we want DMD to see it as
|
|
|
|
// well. So we deliberately don't call Finish() until after DMD finishes.
|
|
|
|
rv = mrWriter->Finish();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the memory reports file, now that we're done writing all the
|
|
|
|
// files. Its final name is "memory-report<-identifier>-<pid>.json.gz".
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> mrFinalFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mrFinalFile));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
rv = mrFinalFile->AppendNative(NS_LITERAL_CSTRING("memory-reports"));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rv = mrFinalFile->AppendNative(mrFilename);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mrFinalFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString mrActualFinalFilename;
|
|
|
|
rv = mrFinalFile->GetLeafName(mrActualFinalFilename);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mrTmpFile->MoveTo(/* directory */ nullptr, mrActualFinalFilename);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write a message to the console.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> cs =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString path;
|
|
|
|
mrTmpFile->GetPath(path);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString msg = NS_LITERAL_STRING("nsIMemoryInfoDumper dumped reports to ");
|
|
|
|
msg.Append(path);
|
|
|
|
return cs->LogStringMessage(msg.get());
|
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
2014-03-05 06:27:13 +04:00
|
|
|
private:
|
2014-07-01 02:11:53 +04:00
|
|
|
~TempDirMemoryFinishCallback() {}
|
|
|
|
|
2014-03-05 06:27:13 +04:00
|
|
|
nsRefPtr<nsGZFileWriter> mrWriter;
|
|
|
|
nsCOMPtr<nsIFile> mrTmpFile;
|
|
|
|
nsCString mrFilename;
|
|
|
|
nsString mIdentifier;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(TempDirMemoryFinishCallback, nsIFinishReportingCallback)
|
2014-03-05 06:27:13 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryInfoDumper::DumpMemoryInfoToTempDir(const nsAString& aIdentifier,
|
2014-05-21 10:06:54 +04:00
|
|
|
bool aAnonymize,
|
2014-03-05 06:27:13 +04:00
|
|
|
bool aMinimizeMemoryUsage)
|
2013-03-28 07:31:26 +04:00
|
|
|
{
|
2014-03-05 06:27:13 +04:00
|
|
|
nsString identifier(aIdentifier);
|
|
|
|
EnsureNonEmptyIdentifier(identifier);
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2012-12-11 04:05:07 +04:00
|
|
|
// Open a new file named something like
|
|
|
|
//
|
2013-03-28 07:31:26 +04:00
|
|
|
// incomplete-memory-report-<identifier>-<pid>.json.gz
|
|
|
|
//
|
|
|
|
// in NS_OS_TEMP_DIR for writing. When we're finished writing the report,
|
|
|
|
// we'll rename this file and get rid of the "incomplete-" prefix.
|
2012-12-11 04:05:07 +04:00
|
|
|
//
|
2013-03-28 07:31:26 +04:00
|
|
|
// We do this because we don't want scripts which poll the filesystem
|
|
|
|
// looking for memory report dumps to grab a file before we're finished
|
|
|
|
// writing to it.
|
|
|
|
|
|
|
|
// Note that |mrFilename| is missing the "incomplete-" prefix; we'll tack
|
|
|
|
// that on in a moment.
|
|
|
|
nsCString mrFilename;
|
2014-03-05 06:27:13 +04:00
|
|
|
// The "unified" indicates that we merge the memory reports from all
|
|
|
|
// processes and write out one file, rather than a separate file for
|
|
|
|
// each process as was the case before bug 946407. This is so that
|
|
|
|
// the get_about_memory.py script in the B2G repository can
|
|
|
|
// determine when it's done waiting for files to appear.
|
2014-07-03 02:59:02 +04:00
|
|
|
MakeFilename("unified-memory-report", identifier, getpid(), "json.gz",
|
|
|
|
mrFilename);
|
2013-03-28 07:31:26 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> mrTmpFile;
|
|
|
|
nsresult rv;
|
2014-03-20 11:29:51 +04:00
|
|
|
// In Android case, this function will open a file named aFilename under
|
|
|
|
// specific folder (/data/local/tmp/memory-reports). Otherwise, it will
|
|
|
|
// open a file named aFilename under "NS_OS_TEMP_DIR".
|
|
|
|
rv = nsDumpUtils::OpenTempFile(NS_LITERAL_CSTRING("incomplete-") +
|
|
|
|
mrFilename,
|
|
|
|
getter_AddRefs(mrTmpFile),
|
|
|
|
NS_LITERAL_CSTRING("memory-reports"));
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-12-02 04:29:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
|
|
|
nsRefPtr<nsGZFileWriter> mrWriter = new nsGZFileWriter();
|
|
|
|
rv = mrWriter->Init(mrTmpFile);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
|
|
|
// Dump the memory reports to the file.
|
2014-03-05 06:27:13 +04:00
|
|
|
rv = DumpHeader(mrWriter);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-03-05 06:27:13 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-03-05 06:27:13 +04:00
|
|
|
|
|
|
|
// Process reporters.
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
nsRefPtr<DumpReportCallback> dumpReport = new DumpReportCallback(mrWriter);
|
|
|
|
nsRefPtr<nsIFinishReportingCallback> finishReport =
|
|
|
|
new TempDirMemoryFinishCallback(mrWriter, mrTmpFile, mrFilename, identifier);
|
|
|
|
rv = mgr->GetReportsExtended(dumpReport, nullptr,
|
|
|
|
finishReport, nullptr,
|
2014-05-21 10:06:54 +04:00
|
|
|
aAnonymize,
|
2014-03-05 06:27:13 +04:00
|
|
|
aMinimizeMemoryUsage,
|
2014-09-01 09:35:28 +04:00
|
|
|
/* DMDident = */ identifier);
|
2014-03-05 06:27:13 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2012-12-11 04:05:07 +04:00
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
#ifdef MOZ_DMD
|
2014-03-05 06:27:13 +04:00
|
|
|
nsresult
|
2014-07-03 02:59:02 +04:00
|
|
|
nsMemoryInfoDumper::OpenDMDFile(const nsAString& aIdentifier, int aPid,
|
|
|
|
FILE** aOutFile)
|
2014-03-05 06:27:13 +04:00
|
|
|
{
|
2014-05-23 06:45:14 +04:00
|
|
|
if (!dmd::IsRunning()) {
|
2014-07-03 02:59:02 +04:00
|
|
|
*aOutFile = nullptr;
|
2014-04-24 23:43:11 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
// Create a filename like dmd-<identifier>-<pid>.txt.gz, which will be used
|
|
|
|
// if DMD is enabled.
|
2012-12-11 04:05:07 +04:00
|
|
|
nsCString dmdFilename;
|
2014-07-03 02:59:02 +04:00
|
|
|
MakeFilename("dmd", aIdentifier, aPid, "txt.gz", dmdFilename);
|
2012-12-11 04:05:07 +04:00
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
// Open a new DMD file named |dmdFilename| in NS_OS_TEMP_DIR for writing,
|
|
|
|
// and dump DMD output to it. This must occur after the memory reporters
|
|
|
|
// have been run (above), but before the memory-reports file has been
|
|
|
|
// renamed (so scripts can detect the DMD file, if present).
|
|
|
|
|
2014-07-03 02:59:02 +04:00
|
|
|
nsresult rv;
|
2012-12-11 04:05:07 +04:00
|
|
|
nsCOMPtr<nsIFile> dmdFile;
|
2014-03-20 11:29:51 +04:00
|
|
|
rv = nsDumpUtils::OpenTempFile(dmdFilename,
|
|
|
|
getter_AddRefs(dmdFile),
|
|
|
|
NS_LITERAL_CSTRING("memory-reports"));
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2014-07-03 02:59:02 +04:00
|
|
|
rv = dmdFile->OpenANSIFileDesc("wb", aOutFile);
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
2014-09-05 07:45:22 +04:00
|
|
|
|
|
|
|
// Print the path, because on some platforms (e.g. Mac) it's not obvious.
|
|
|
|
nsCString path;
|
|
|
|
rv = dmdFile->GetNativePath(path);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
dmd::StatusMsg("opened %s for writing\n", path.get());
|
|
|
|
|
2014-07-03 02:59:02 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2012-12-11 04:05:07 +04:00
|
|
|
|
2014-07-03 02:59:02 +04:00
|
|
|
nsresult
|
|
|
|
nsMemoryInfoDumper::DumpDMDToFile(FILE* aFile)
|
|
|
|
{
|
2012-12-11 04:05:07 +04:00
|
|
|
nsRefPtr<nsGZFileWriter> dmdWriter = new nsGZFileWriter();
|
2014-07-03 02:59:02 +04:00
|
|
|
nsresult rv = dmdWriter->InitANSIFileDesc(aFile);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-12-11 04:05:07 +04:00
|
|
|
|
2014-05-30 10:46:09 +04:00
|
|
|
// Dump DMD's memory reports analysis to the file.
|
2012-12-11 04:05:07 +04:00
|
|
|
DMDWriteState state(dmdWriter);
|
|
|
|
dmd::Writer w(DMDWrite, &state);
|
2014-05-30 10:46:09 +04:00
|
|
|
dmd::AnalyzeReports(w);
|
2012-12-11 04:05:07 +04:00
|
|
|
|
|
|
|
rv = dmdWriter->Finish();
|
2014-03-05 06:27:13 +04:00
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif // MOZ_DMD
|
|
|
|
|
2013-10-23 09:26:24 +04:00
|
|
|
// This dumps the JSON footer and closes the file, and then calls the given
|
|
|
|
// nsIFinishDumpingCallback.
|
|
|
|
class FinishReportingCallback MOZ_FINAL : public nsIFinishReportingCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
FinishReportingCallback(nsIFinishDumpingCallback* aFinishDumping,
|
|
|
|
nsISupports* aFinishDumpingData)
|
|
|
|
: mFinishDumping(aFinishDumping)
|
|
|
|
, mFinishDumpingData(aFinishDumpingData)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2013-10-23 09:26:24 +04:00
|
|
|
|
|
|
|
NS_IMETHOD Callback(nsISupports* aData)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGZFileWriter> writer = do_QueryInterface(aData);
|
|
|
|
NS_ENSURE_TRUE(writer, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult rv = DumpFooter(writer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = writer->Finish();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-12-03 01:11:43 +04:00
|
|
|
if (!mFinishDumping) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-10-23 09:26:24 +04:00
|
|
|
return mFinishDumping->Callback(mFinishDumpingData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-07-01 02:11:53 +04:00
|
|
|
~FinishReportingCallback() {}
|
|
|
|
|
2013-10-23 09:26:24 +04:00
|
|
|
nsCOMPtr<nsIFinishDumpingCallback> mFinishDumping;
|
|
|
|
nsCOMPtr<nsISupports> mFinishDumpingData;
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(FinishReportingCallback, nsIFinishReportingCallback)
|
2013-10-23 09:26:24 +04:00
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
NS_IMETHODIMP
|
2013-10-23 09:26:24 +04:00
|
|
|
nsMemoryInfoDumper::DumpMemoryReportsToNamedFile(
|
|
|
|
const nsAString& aFilename,
|
|
|
|
nsIFinishDumpingCallback* aFinishDumping,
|
2014-05-21 10:06:54 +04:00
|
|
|
nsISupports* aFinishDumpingData,
|
|
|
|
bool aAnonymize)
|
2013-03-28 07:31:26 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aFilename.IsEmpty());
|
|
|
|
|
|
|
|
// Create the file.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> mrFile;
|
|
|
|
nsresult rv = NS_NewLocalFile(aFilename, false, getter_AddRefs(mrFile));
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
|
|
|
mrFile->InitWithPath(aFilename);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
|
|
|
bool exists;
|
|
|
|
rv = mrFile->Exists(&exists);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
rv = mrFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write the memory reports to the file.
|
|
|
|
|
|
|
|
nsRefPtr<nsGZFileWriter> mrWriter = new nsGZFileWriter();
|
|
|
|
rv = mrWriter->Init(mrFile);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
2013-10-23 09:26:24 +04:00
|
|
|
rv = DumpHeader(mrWriter);
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return rv;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2013-03-28 07:31:26 +04:00
|
|
|
|
2013-10-23 09:26:24 +04:00
|
|
|
// Process reports and finish up.
|
2014-09-01 09:35:28 +04:00
|
|
|
nsCOMPtr<nsIMemoryReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
2013-12-02 04:14:26 +04:00
|
|
|
nsRefPtr<DumpReportCallback> dumpReport = new DumpReportCallback(mrWriter);
|
2013-10-23 09:26:24 +04:00
|
|
|
nsRefPtr<FinishReportingCallback> finishReporting =
|
|
|
|
new FinishReportingCallback(aFinishDumping, aFinishDumpingData);
|
2014-05-21 10:06:54 +04:00
|
|
|
return mgr->GetReports(dumpReport, nullptr, finishReporting, mrWriter,
|
|
|
|
aAnonymize);
|
2013-03-28 07:31:26 +04:00
|
|
|
}
|
|
|
|
|
2012-10-16 06:12:14 +04:00
|
|
|
#undef DUMP
|