2017-01-27 03:35:53 +03: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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "MemoryReportRequest.h"
|
2018-11-14 21:06:52 +03:00
|
|
|
#include "mozilla/RDDParent.h"
|
2017-01-27 03:35:55 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2017-01-27 03:35:54 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2017-01-27 03:35:55 +03:00
|
|
|
#include "mozilla/gfx/GPUParent.h"
|
2017-01-27 03:35:53 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
MemoryReportRequestHost::MemoryReportRequestHost(uint32_t aGeneration)
|
|
|
|
: mGeneration(aGeneration),
|
|
|
|
mSuccess(false)
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
2017-01-27 03:35:54 +03:00
|
|
|
MOZ_COUNT_CTOR(MemoryReportRequestHost);
|
2017-01-27 03:35:53 +03:00
|
|
|
mReporterManager = nsMemoryReporterManager::GetOrCreate();
|
|
|
|
NS_WARNING_ASSERTION(mReporterManager, "GetOrCreate failed");
|
|
|
|
}
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
void
|
|
|
|
MemoryReportRequestHost::RecvReport(const MemoryReport& aReport)
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
2017-01-27 03:35:54 +03:00
|
|
|
// Skip reports from older generations. We need to do this here since we
|
|
|
|
// could receive older reports from a subprocesses before it acknowledges
|
|
|
|
// a new request, and we only track one active request per process.
|
|
|
|
if (aReport.generation() != mGeneration) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-27 03:35:53 +03:00
|
|
|
if (mReporterManager) {
|
|
|
|
mReporterManager->HandleChildReport(mGeneration, aReport);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
void
|
|
|
|
MemoryReportRequestHost::Finish(uint32_t aGeneration)
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
2017-01-27 03:35:54 +03:00
|
|
|
// Skip reports from older generations. See the comment in RecvReport.
|
|
|
|
if (mGeneration != aGeneration) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSuccess = true;
|
2017-01-27 03:35:53 +03:00
|
|
|
}
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
MemoryReportRequestHost::~MemoryReportRequestHost()
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
2017-01-27 03:35:54 +03:00
|
|
|
MOZ_COUNT_DTOR(MemoryReportRequestHost);
|
|
|
|
|
2017-01-27 03:35:53 +03:00
|
|
|
if (mReporterManager) {
|
2017-01-27 03:35:54 +03:00
|
|
|
mReporterManager->EndProcessReport(mGeneration, mSuccess);
|
2017-01-27 03:35:53 +03:00
|
|
|
mReporterManager = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
NS_IMPL_ISUPPORTS(MemoryReportRequestClient, nsIRunnable)
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
MemoryReportRequestClient::Start(uint32_t aGeneration,
|
|
|
|
bool aAnonymize,
|
|
|
|
bool aMinimizeMemoryUsage,
|
|
|
|
const MaybeFileDesc& aDMDFile,
|
|
|
|
const nsACString& aProcessString)
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
2017-01-27 03:35:54 +03:00
|
|
|
RefPtr<MemoryReportRequestClient> request = new MemoryReportRequestClient(
|
|
|
|
aGeneration,
|
|
|
|
aAnonymize,
|
|
|
|
aDMDFile,
|
|
|
|
aProcessString);
|
|
|
|
|
|
|
|
DebugOnly<nsresult> rv;
|
|
|
|
if (aMinimizeMemoryUsage) {
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
rv = mgr->MinimizeMemoryUsage(request);
|
|
|
|
// mgr will eventually call actor->Run()
|
|
|
|
} else {
|
|
|
|
rv = request->Run();
|
|
|
|
}
|
2017-01-27 03:35:53 +03:00
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "actor operation failed");
|
|
|
|
}
|
2017-01-27 03:35:53 +03:00
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
MemoryReportRequestClient::MemoryReportRequestClient(uint32_t aGeneration,
|
|
|
|
bool aAnonymize,
|
|
|
|
const MaybeFileDesc& aDMDFile,
|
|
|
|
const nsACString& aProcessString)
|
|
|
|
: mGeneration(aGeneration),
|
|
|
|
mAnonymize(aAnonymize),
|
2017-01-27 03:35:53 +03:00
|
|
|
mProcessString(aProcessString)
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
|
|
|
if (aDMDFile.type() == MaybeFileDesc::TFileDescriptor) {
|
|
|
|
mDMDFile = aDMDFile.get_FileDescriptor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
MemoryReportRequestClient::~MemoryReportRequestClient()
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class HandleReportCallback final : public nsIHandleReportCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
explicit HandleReportCallback(uint32_t aGeneration,
|
2017-01-27 03:35:53 +03:00
|
|
|
const nsACString& aProcess)
|
2017-01-27 03:35:54 +03:00
|
|
|
: mGeneration(aGeneration)
|
2017-01-27 03:35:53 +03:00
|
|
|
, mProcess(aProcess)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHOD Callback(const nsACString& aProcess, const nsACString &aPath,
|
|
|
|
int32_t aKind, int32_t aUnits, int64_t aAmount,
|
|
|
|
const nsACString& aDescription,
|
|
|
|
nsISupports* aUnused) override
|
|
|
|
{
|
|
|
|
MemoryReport memreport(mProcess, nsCString(aPath), aKind, aUnits,
|
2017-01-27 03:35:54 +03:00
|
|
|
aAmount, mGeneration, nsCString(aDescription));
|
|
|
|
switch (XRE_GetProcessType()) {
|
|
|
|
case GeckoProcessType_Content:
|
|
|
|
ContentChild::GetSingleton()->SendAddMemoryReport(memreport);
|
|
|
|
break;
|
2017-01-27 03:35:55 +03:00
|
|
|
case GeckoProcessType_GPU:
|
|
|
|
Unused << gfx::GPUParent::GetSingleton()->SendAddMemoryReport(memreport);
|
|
|
|
break;
|
2018-11-14 21:06:52 +03:00
|
|
|
case GeckoProcessType_RDD:
|
|
|
|
Unused << RDDParent::GetSingleton()->SendAddMemoryReport(memreport);
|
|
|
|
break;
|
2017-01-27 03:35:54 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled process type");
|
|
|
|
}
|
2017-01-27 03:35:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
~HandleReportCallback() = default;
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
uint32_t mGeneration;
|
2017-01-27 03:35:53 +03:00
|
|
|
const nsCString mProcess;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(
|
|
|
|
HandleReportCallback
|
|
|
|
, nsIHandleReportCallback
|
|
|
|
)
|
|
|
|
|
|
|
|
class FinishReportingCallback final : public nsIFinishReportingCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
explicit FinishReportingCallback(uint32_t aGeneration)
|
|
|
|
: mGeneration(aGeneration)
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Callback(nsISupports* aUnused) override
|
|
|
|
{
|
2017-01-27 03:35:54 +03:00
|
|
|
bool sent = false;
|
|
|
|
switch (XRE_GetProcessType()) {
|
|
|
|
case GeckoProcessType_Content:
|
|
|
|
sent = ContentChild::GetSingleton()->SendFinishMemoryReport(mGeneration);
|
|
|
|
break;
|
2017-01-27 03:35:55 +03:00
|
|
|
case GeckoProcessType_GPU:
|
|
|
|
sent = gfx::GPUParent::GetSingleton()->SendFinishMemoryReport(mGeneration);
|
|
|
|
break;
|
2018-11-14 21:06:52 +03:00
|
|
|
case GeckoProcessType_RDD:
|
|
|
|
sent = RDDParent::GetSingleton()->SendFinishMemoryReport(mGeneration);
|
|
|
|
break;
|
2017-01-27 03:35:54 +03:00
|
|
|
default:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unhandled process type");
|
|
|
|
}
|
2017-01-27 03:35:53 +03:00
|
|
|
return sent ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~FinishReportingCallback() = default;
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
uint32_t mGeneration;
|
2017-01-27 03:35:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(
|
|
|
|
FinishReportingCallback
|
|
|
|
, nsIFinishReportingCallback
|
|
|
|
)
|
|
|
|
|
2017-01-27 03:35:54 +03:00
|
|
|
NS_IMETHODIMP MemoryReportRequestClient::Run()
|
2017-01-27 03:35:53 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> mgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
|
|
|
|
// Run the reporters. The callback will turn each measurement into a
|
|
|
|
// MemoryReport.
|
|
|
|
RefPtr<HandleReportCallback> handleReport =
|
2017-01-27 03:35:54 +03:00
|
|
|
new HandleReportCallback(mGeneration, mProcessString);
|
2017-01-27 03:35:53 +03:00
|
|
|
RefPtr<FinishReportingCallback> finishReporting =
|
2017-01-27 03:35:54 +03:00
|
|
|
new FinishReportingCallback(mGeneration);
|
2017-01-27 03:35:53 +03:00
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
mgr->GetReportsForThisProcessExtended(handleReport, nullptr, mAnonymize,
|
|
|
|
FileDescriptorToFILE(mDMDFile, "wb"),
|
|
|
|
finishReporting, nullptr);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv),
|
|
|
|
"GetReportsForThisProcessExtended failed");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|