/* 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 currentProfilerParams; nsCOMPtr profiler(do_GetService("@mozilla.org/tools/profiler;1")); DebugOnly rv = profiler->GetStartParams(getter_AddRefs(currentProfilerParams)); MOZ_ASSERT(NS_SUCCEEDED(rv)); StartProfiler(currentProfilerParams); } nsCOMPtr 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 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 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