mirror of
https://github.com/HeyPuter/firefox.git
synced 2026-11-30 14:17:36 -10:00
Currently the profiler mostly uses an array of strings to represent which features are available and in use. This patch changes the profiler core to use a uint32_t bitfield, which is a much simpler and faster representation. (nsProfiler and the profiler add-on still use the array of strings, alas.) The new ProfilerFeature type defines the values in the bitfield. One side-effect of this change is that profiler_feature_active() now can be used to query all features. Previously it was just a subset. Another side-effect is that profiler_get_available_features() no longer incorrectly indicates support for Java and stack-walking when they aren't supported. (The handling of task tracer support is unchanged, because the old code handled it correctly.)
158 lines
5.0 KiB
C++
158 lines
5.0 KiB
C++
/* 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 "CrossProcessProfilerController.h"
|
|
|
|
#include "mozilla/Move.h"
|
|
#include "mozilla/ProfilerTypes.h"
|
|
#include "nsIProfiler.h"
|
|
#include "nsISupports.h"
|
|
#include "nsIObserver.h"
|
|
#include "nsProfiler.h"
|
|
#include "ProfilerControllingProcess.h"
|
|
|
|
namespace mozilla {
|
|
|
|
static const char* sObserverTopics[] = {
|
|
"profiler-started",
|
|
"profiler-stopped",
|
|
"profiler-paused",
|
|
"profiler-resumed",
|
|
"profiler-subprocess-gather",
|
|
};
|
|
|
|
// ProfilerObserver is a refcounted class that gets registered with the
|
|
// observer service and just forwards Observe() calls to mController.
|
|
// This indirection makes the CrossProcessProfilerController API nicer because
|
|
// it doesn't require a separate Init() method to register with the observer
|
|
// service, and because not being refcounted allows CPPC to be managed with a
|
|
// UniquePtr.
|
|
// The life time of ProfilerObserver is bounded by the life time of CPPC: CPPC
|
|
// unregisters the ProfilerObserver from the observer service in its
|
|
// destructor, and then it drops its reference to the ProfilerObserver, which
|
|
// destroys the ProfilerObserver.
|
|
class ProfilerObserver final : public nsIObserver
|
|
{
|
|
public:
|
|
explicit ProfilerObserver(CrossProcessProfilerController& aController)
|
|
: mController(aController)
|
|
{}
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_IMETHOD Observe(nsISupports* aSubject, const char* aTopic,
|
|
const char16_t* aData) override
|
|
{
|
|
mController.Observe(aSubject, aTopic);
|
|
return NS_OK;
|
|
}
|
|
|
|
private:
|
|
~ProfilerObserver() {}
|
|
|
|
CrossProcessProfilerController& mController;
|
|
};
|
|
|
|
NS_IMPL_ISUPPORTS(ProfilerObserver, nsIObserver)
|
|
|
|
CrossProcessProfilerController::CrossProcessProfilerController(
|
|
ProfilerControllingProcess* aProcess)
|
|
: mProcess(aProcess)
|
|
, mObserver(new ProfilerObserver(*this))
|
|
{
|
|
if (profiler_is_active()) {
|
|
// If the profiler is already running in this process, start it in the
|
|
// child process immediately.
|
|
nsCOMPtr<nsIProfilerStartParams> currentProfilerParams;
|
|
nsCOMPtr<nsIProfiler> profiler(do_GetService("@mozilla.org/tools/profiler;1"));
|
|
DebugOnly<nsresult> rv = profiler->GetStartParams(getter_AddRefs(currentProfilerParams));
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
StartProfiler(currentProfilerParams);
|
|
}
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
if (obs) {
|
|
size_t length = ArrayLength(sObserverTopics);
|
|
for (size_t i = 0; i < length; ++i) {
|
|
obs->AddObserver(mObserver, sObserverTopics[i], false);
|
|
}
|
|
}
|
|
}
|
|
|
|
CrossProcessProfilerController::~CrossProcessProfilerController()
|
|
{
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
if (obs) {
|
|
size_t length = ArrayLength(sObserverTopics);
|
|
for (size_t i = 0; i < length; ++i) {
|
|
obs->RemoveObserver(mObserver, sObserverTopics[i]);
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
CrossProcessProfilerController::StartProfiler(nsIProfilerStartParams* aParams)
|
|
{
|
|
if (NS_WARN_IF(!aParams)) {
|
|
return;
|
|
}
|
|
|
|
ProfilerInitParams ipcParams;
|
|
|
|
ipcParams.enabled() = true;
|
|
aParams->GetEntries(&ipcParams.entries());
|
|
aParams->GetInterval(&ipcParams.interval());
|
|
aParams->GetFeatures(&ipcParams.features());
|
|
ipcParams.filters() = aParams->GetFilters();
|
|
|
|
mProcess->SendStartProfiler(ipcParams);
|
|
}
|
|
|
|
void
|
|
CrossProcessProfilerController::Observe(nsISupports* aSubject,
|
|
const char* aTopic)
|
|
{
|
|
if (!strcmp(aTopic, "profiler-subprocess-gather")) {
|
|
// profiler-subprocess-gather is the request to capture the profile. We
|
|
// need to tell the other process that we're interested in its profile,
|
|
// and we tell the gatherer that we've forwarded the request, so that it
|
|
// can keep track of the number of pending profiles.
|
|
nsProfiler::GetOrCreate()->WillGatherOOPProfile();
|
|
mProcess->SendGatherProfile();
|
|
}
|
|
// These four notifications are sent by the profiler when its corresponding
|
|
// methods are called inside this process. These state changes just need to
|
|
// be forwarded to the other process.
|
|
else if (!strcmp(aTopic, "profiler-started")) {
|
|
nsCOMPtr<nsIProfilerStartParams> params(do_QueryInterface(aSubject));
|
|
StartProfiler(params);
|
|
}
|
|
else if (!strcmp(aTopic, "profiler-stopped")) {
|
|
mProcess->SendStopProfiler();
|
|
}
|
|
else if (!strcmp(aTopic, "profiler-paused")) {
|
|
mProcess->SendPauseProfiler(true);
|
|
}
|
|
else if (!strcmp(aTopic, "profiler-resumed")) {
|
|
mProcess->SendPauseProfiler(false);
|
|
}
|
|
}
|
|
|
|
// This is called in response to a SendGatherProfile request, or when the
|
|
// other process exits while the profiler is running.
|
|
void
|
|
CrossProcessProfilerController::RecvProfile(const nsCString& aProfile,
|
|
bool aIsExitProfile)
|
|
{
|
|
// Pass our process's profile along to nsProfiler.
|
|
if (aIsExitProfile) {
|
|
nsProfiler::GetOrCreate()->OOPExitProfile(aProfile);
|
|
} else {
|
|
nsProfiler::GetOrCreate()->GatheredOOPProfile(aProfile);
|
|
}
|
|
}
|
|
|
|
} // namespace mozilla
|