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/. */
|
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
#include "mozilla/JSONWriter.h"
|
|
|
|
#include "mozilla/UniquePtr.h"
|
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-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
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
// This class wraps GZFileWriter so it can be used with JSONWriter, overcoming
|
|
|
|
// the following two problems:
|
|
|
|
// - It provides a JSONWriterFunc::Write() that calls nsGZFileWriter::Write().
|
|
|
|
// - It can be stored as a UniquePtr, whereas nsGZFileWriter is refcounted.
|
|
|
|
class GZWriterWrapper : public JSONWriteFunc
|
2012-12-11 04:05:07 +04:00
|
|
|
{
|
2014-09-15 10:36:18 +04:00
|
|
|
public:
|
|
|
|
explicit GZWriterWrapper(nsGZFileWriter* aGZWriter)
|
|
|
|
: mGZWriter(aGZWriter)
|
|
|
|
{}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
void Write(const char* aStr)
|
|
|
|
{
|
|
|
|
(void)mGZWriter->Write(aStr);
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-10-16 06:12:14 +04:00
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
nsresult Finish() { return mGZWriter->Finish(); }
|
2013-10-23 09:26:24 +04:00
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
private:
|
|
|
|
nsRefPtr<nsGZFileWriter> mGZWriter;
|
|
|
|
};
|
2013-10-23 09:26:24 +04:00
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
// We need two callbacks: one that handles reports, and one that is called at
|
|
|
|
// the end of reporting. Both the callbacks need access to the same JSONWriter,
|
|
|
|
// so we implement both of them in this one class.
|
|
|
|
class HandleReportAndFinishReportingCallbacks MOZ_FINAL
|
|
|
|
: public nsIHandleReportCallback, public nsIFinishReportingCallback
|
2014-09-10 11:32:58 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
HandleReportAndFinishReportingCallbacks(UniquePtr<JSONWriter> aWriter,
|
|
|
|
nsIFinishDumpingCallback* aFinishDumping,
|
|
|
|
nsISupports* aFinishDumpingData)
|
|
|
|
: mWriter(Move(aWriter))
|
2014-09-10 11:34:47 +04:00
|
|
|
, mFinishDumping(aFinishDumping)
|
2014-09-10 11:32:58 +04:00
|
|
|
, mFinishDumpingData(aFinishDumpingData)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
// This is the callback for nsIHandleReportCallback.
|
|
|
|
NS_IMETHOD Callback(const nsACString& aProcess, const nsACString& aPath,
|
|
|
|
int32_t aKind, int32_t aUnits, int64_t aAmount,
|
|
|
|
const nsACString& aDescription,
|
|
|
|
nsISupports* aData)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
} else if (ContentChild* cc = ContentChild::GetSingleton()) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWriter->StartObjectElement();
|
|
|
|
{
|
|
|
|
mWriter->StringProperty("process", process.get());
|
|
|
|
mWriter->StringProperty("path", PromiseFlatCString(aPath).get());
|
|
|
|
mWriter->IntProperty("kind", aKind);
|
|
|
|
mWriter->IntProperty("units", aUnits);
|
|
|
|
mWriter->IntProperty("amount", aAmount);
|
|
|
|
mWriter->StringProperty("description",
|
|
|
|
PromiseFlatCString(aDescription).get());
|
|
|
|
}
|
|
|
|
mWriter->EndObject();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the callback for nsIFinishReportingCallback.
|
2014-09-10 11:32:58 +04:00
|
|
|
NS_IMETHOD Callback(nsISupports* aData)
|
|
|
|
{
|
2014-09-15 10:36:18 +04:00
|
|
|
mWriter->EndArray(); // end of "reports" array
|
|
|
|
mWriter->End();
|
2014-09-10 11:32:58 +04:00
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
// The call to Finish() deallocates the memory allocated by the first Write
|
2014-09-10 11:34:47 +04:00
|
|
|
// call. Because that memory was live while the memory reporters ran and
|
2014-09-15 10:36:18 +04:00
|
|
|
// was 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
|
2014-09-10 11:34:47 +04:00
|
|
|
// after DMD finishes.
|
2014-09-15 10:36:18 +04:00
|
|
|
nsresult rv = static_cast<GZWriterWrapper*>(mWriter->WriteFunc())->Finish();
|
2014-09-10 11:32:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!mFinishDumping) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mFinishDumping->Callback(mFinishDumpingData);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-09-15 10:36:18 +04:00
|
|
|
~HandleReportAndFinishReportingCallbacks() {}
|
2014-09-10 11:32:58 +04:00
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
UniquePtr<JSONWriter> mWriter;
|
2014-09-10 11:32:58 +04:00
|
|
|
nsCOMPtr<nsIFinishDumpingCallback> mFinishDumping;
|
|
|
|
nsCOMPtr<nsISupports> mFinishDumpingData;
|
|
|
|
};
|
|
|
|
|
2014-09-15 10:36:18 +04:00
|
|
|
NS_IMPL_ISUPPORTS(HandleReportAndFinishReportingCallbacks,
|
|
|
|
nsIHandleReportCallback, nsIFinishReportingCallback)
|
2014-09-10 11:32:58 +04:00
|
|
|
|
2014-09-10 11:41:50 +04:00
|
|
|
class TempDirFinishCallback MOZ_FINAL : public nsIFinishDumpingCallback
|
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-09-10 11:41:50 +04:00
|
|
|
TempDirFinishCallback(nsIFile* aReportsTmpFile,
|
|
|
|
const nsCString& aReportsFinalFilename)
|
2014-09-10 11:34:47 +04:00
|
|
|
: mReportsTmpFile(aReportsTmpFile)
|
2014-09-10 11:30:17 +04:00
|
|
|
, mReportsFilename(aReportsFinalFilename)
|
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)
|
|
|
|
{
|
|
|
|
// Rename the memory reports file, now that we're done writing all the
|
|
|
|
// files. Its final name is "memory-report<-identifier>-<pid>.json.gz".
|
|
|
|
|
2014-09-10 11:30:17 +04:00
|
|
|
nsCOMPtr<nsIFile> reportsFinalFile;
|
2014-09-10 11:34:47 +04:00
|
|
|
nsresult rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR,
|
|
|
|
getter_AddRefs(reportsFinalFile));
|
2014-09-10 11:27:45 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
2014-09-10 11:30:17 +04:00
|
|
|
rv = reportsFinalFile->AppendNative(NS_LITERAL_CSTRING("memory-reports"));
|
2014-09-10 11:27:45 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-10 11:30:17 +04:00
|
|
|
rv = reportsFinalFile->AppendNative(mReportsFilename);
|
2014-09-10 11:27:45 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:30:17 +04:00
|
|
|
rv = reportsFinalFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
2014-09-10 11:27:45 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:30:17 +04:00
|
|
|
nsAutoString reportsFinalFilename;
|
|
|
|
rv = reportsFinalFile->GetLeafName(reportsFinalFilename);
|
2014-09-10 11:27:45 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:30:17 +04:00
|
|
|
rv = mReportsTmpFile->MoveTo(/* directory */ nullptr,
|
|
|
|
reportsFinalFilename);
|
2014-09-10 11:27:45 +04:00
|
|
|
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;
|
2014-09-10 11:30:17 +04:00
|
|
|
mReportsTmpFile->GetPath(path);
|
2014-09-10 11:27:45 +04:00
|
|
|
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-09-10 11:41:50 +04:00
|
|
|
~TempDirFinishCallback() {}
|
2014-07-01 02:11:53 +04:00
|
|
|
|
2014-09-10 11:30:17 +04:00
|
|
|
nsCOMPtr<nsIFile> mReportsTmpFile;
|
|
|
|
nsCString mReportsFilename;
|
2014-03-05 06:27:13 +04:00
|
|
|
};
|
|
|
|
|
2014-09-10 11:41:50 +04:00
|
|
|
NS_IMPL_ISUPPORTS(TempDirFinishCallback, nsIFinishDumpingCallback)
|
2014-03-05 06:27:13 +04:00
|
|
|
|
2014-09-10 11:40:10 +04:00
|
|
|
static nsresult
|
|
|
|
DumpMemoryInfoToFile(
|
|
|
|
nsIFile* aReportsFile,
|
|
|
|
nsIFinishDumpingCallback* aFinishDumping,
|
|
|
|
nsISupports* aFinishDumpingData,
|
|
|
|
bool aAnonymize,
|
|
|
|
bool aMinimizeMemoryUsage,
|
|
|
|
nsAString& aDMDIdentifier)
|
|
|
|
{
|
2014-09-15 10:36:18 +04:00
|
|
|
nsRefPtr<nsGZFileWriter> gzWriter = new nsGZFileWriter();
|
|
|
|
nsresult rv = gzWriter->Init(aReportsFile);
|
2014-09-10 11:40:10 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-09-15 10:36:18 +04:00
|
|
|
auto jsonWriter =
|
|
|
|
MakeUnique<JSONWriter>(MakeUnique<GZWriterWrapper>(gzWriter));
|
2014-09-10 11:40:10 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
2014-09-15 10:36:18 +04:00
|
|
|
|
|
|
|
// This is the first write to the file, and it causes |aWriter| to allocate
|
|
|
|
// over 200 KiB of memory.
|
|
|
|
jsonWriter->Start();
|
|
|
|
{
|
|
|
|
// Increment this number if the format changes.
|
|
|
|
jsonWriter->IntProperty("version", 1);
|
|
|
|
jsonWriter->BoolProperty("hasMozMallocUsableSize",
|
|
|
|
mgr->GetHasMozMallocUsableSize());
|
|
|
|
jsonWriter->StartArrayProperty("reports");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<HandleReportAndFinishReportingCallbacks>
|
|
|
|
handleReportAndFinishReporting =
|
|
|
|
new HandleReportAndFinishReportingCallbacks(Move(jsonWriter),
|
|
|
|
aFinishDumping,
|
|
|
|
aFinishDumpingData);
|
|
|
|
rv = mgr->GetReportsExtended(handleReportAndFinishReporting, nullptr,
|
|
|
|
handleReportAndFinishReporting, nullptr,
|
2014-09-10 11:40:10 +04:00
|
|
|
aAnonymize,
|
|
|
|
aMinimizeMemoryUsage,
|
|
|
|
aDMDIdentifier);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-09-10 11:40:36 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMemoryInfoDumper::DumpMemoryReportsToNamedFile(
|
|
|
|
const nsAString& aFilename,
|
|
|
|
nsIFinishDumpingCallback* aFinishDumping,
|
|
|
|
nsISupports* aFinishDumpingData,
|
|
|
|
bool aAnonymize)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aFilename.IsEmpty());
|
|
|
|
|
|
|
|
// Create the file.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> reportsFile;
|
|
|
|
nsresult rv = NS_NewLocalFile(aFilename, false, getter_AddRefs(reportsFile));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
reportsFile->InitWithPath(aFilename);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool exists;
|
|
|
|
rv = reportsFile->Exists(&exists);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exists) {
|
|
|
|
rv = reportsFile->Create(nsIFile::NORMAL_FILE_TYPE, 0644);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString dmdIdent = EmptyString();
|
|
|
|
return DumpMemoryInfoToFile(reportsFile, aFinishDumping, aFinishDumpingData,
|
|
|
|
aAnonymize, /* minimizeMemoryUsage = */ false,
|
|
|
|
dmdIdent);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
|
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-09-15 10:36:18 +04:00
|
|
|
nsCString reportsFinalFilename;
|
2014-07-03 02:59:02 +04:00
|
|
|
MakeFilename("unified-memory-report", identifier, getpid(), "json.gz",
|
2014-09-10 11:30:17 +04:00
|
|
|
reportsFinalFilename);
|
2013-03-28 07:31:26 +04:00
|
|
|
|
2014-09-10 11:30:17 +04:00
|
|
|
nsCOMPtr<nsIFile> reportsTmpFile;
|
2013-03-28 07:31:26 +04:00
|
|
|
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-") +
|
2014-09-10 11:30:17 +04:00
|
|
|
reportsFinalFilename,
|
|
|
|
getter_AddRefs(reportsTmpFile),
|
2014-03-20 11:29:51 +04:00
|
|
|
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
|
|
|
|
2014-09-10 11:41:50 +04:00
|
|
|
nsRefPtr<TempDirFinishCallback> finishDumping =
|
|
|
|
new TempDirFinishCallback(reportsTmpFile, reportsFinalFilename);
|
2014-09-10 11:34:47 +04:00
|
|
|
|
2014-09-10 11:40:10 +04:00
|
|
|
return DumpMemoryInfoToFile(reportsTmpFile, finishDumping, nullptr,
|
|
|
|
aAnonymize, aMinimizeMemoryUsage, identifier);
|
2014-03-05 06:27:13 +04:00
|
|
|
}
|
2012-12-11 04:05:07 +04:00
|
|
|
|
2013-03-28 07:31:26 +04:00
|
|
|
#ifdef MOZ_DMD
|
2014-11-18 13:21:06 +03:00
|
|
|
dmd::DMDFuncs::Singleton dmd::DMDFuncs::sSingleton;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-03 07:34:58 +04:00
|
|
|
// Create a filename like dmd-<identifier>-<pid>.json.gz, which will be used
|
2013-03-28 07:31:26 +04:00
|
|
|
// if DMD is enabled.
|
2012-12-11 04:05:07 +04:00
|
|
|
nsCString dmdFilename;
|
2014-09-03 07:34:58 +04:00
|
|
|
MakeFilename("dmd", aIdentifier, aPid, "json.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)
|
|
|
|
{
|
2014-09-03 07:34:58 +04:00
|
|
|
nsRefPtr<nsGZFileWriter> gzWriter = new nsGZFileWriter();
|
|
|
|
nsresult rv = gzWriter->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.
|
2014-11-13 03:24:28 +03:00
|
|
|
dmd::AnalyzeReports(MakeUnique<GZWriterWrapper>(gzWriter));
|
2012-12-11 04:05:07 +04:00
|
|
|
|
2014-09-03 07:34:58 +04:00
|
|
|
rv = gzWriter->Finish();
|
2014-03-05 06:27:13 +04:00
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#endif // MOZ_DMD
|
|
|
|
|