diff --git a/testing/mochitest/runtests.py b/testing/mochitest/runtests.py index 957d7b1283a9..84c07e34e2ef 100644 --- a/testing/mochitest/runtests.py +++ b/testing/mochitest/runtests.py @@ -3879,6 +3879,26 @@ toolbar#nav-bar { self.log.warning("Force-terminating active process(es).") browser_pid = browser_pid or proc.pid + + # Send a signal to start the profiler - if we're running on Linux or MacOS + if mozinfo.isLinux or mozinfo.isMac: + self.log.info("Sending SIGUSR1 to pid %d start the profiler." % browser_pid) + os.kill(browser_pid, signal.SIGUSR1) + self.log.info("Waiting 10s to capture a profile.") + time.sleep(10) + self.log.info("Sending SIGUSR2 to pid %d stop the profiler." % browser_pid) + os.kill(browser_pid, signal.SIGUSR2) + # We trigger `killPid` further down in this function, which will + # stop the profiler writing to disk. See Bug 1906151 for more + # details, andBug 1905929 for an intermediate solution that would + # allow this test to watch for the profile file being completed. + self.log.info("Wait 10s for Firefox to write the profile to disk.") + time.sleep(10) + else: + self.log.info( + "Not sending a signal to start the profiler - not on MacOS or Linux. See Bug 1823370." + ) + child_pids = self.extract_child_pids(processLog, browser_pid) self.log.info("Found child pids: %s" % child_pids) diff --git a/tools/profiler/core/platform.cpp b/tools/profiler/core/platform.cpp index 8087184b2a3d..bbb8713e4f07 100644 --- a/tools/profiler/core/platform.cpp +++ b/tools/profiler/core/platform.cpp @@ -941,7 +941,7 @@ class CorePS { PS_GET_AND_SET(const nsACString&, ProcessName) PS_GET_AND_SET(const nsACString&, ETLDplus1) #if !defined(XP_WIN) - PS_GET_AND_SET(const Maybe>&, DownloadDirectory) + PS_GET_AND_SET(const Maybe>&, AsyncSignalDumpDirectory) #endif static void SetBandwidthCounter(ProfilerBandwidthCounter* aBandwidthCounter) { @@ -1007,7 +1007,7 @@ class CorePS { // Cached download directory for when we need to dump profiles to disk. #if !defined(XP_WIN) - Maybe> mDownloadDirectory; + Maybe> mAsyncSignalDumpDirectory; #endif }; @@ -5653,10 +5653,10 @@ Maybe profiler_find_dump_path() { // Acquire the lock so that we can get things from CorePS PSAutoLock lock; Maybe> downloadDir = Nothing(); - downloadDir = CorePS::DownloadDirectory(lock); + downloadDir = CorePS::AsyncSignalDumpDirectory(lock); // This needs to be done within the context of the lock, as otherwise - // another thread might modify CorePS::mDownloadDirectory while we're + // another thread might modify CorePS::mAsyncSignalDumpDirectory while we're // cloning the pointer. if (downloadDir) { nsCOMPtr d; @@ -6848,12 +6848,12 @@ bool profiler_is_paused() { } // See `ProfilerControl.h` for more details. -void profiler_lookup_download_directory() { +void profiler_lookup_async_signal_dump_directory() { // This implementation is causing issues on Windows (see Bug 1890154) but as it // only exists to support the posix signal handling (on non-windows platforms) // we can remove it for now. #if !defined(XP_WIN) - LOG("profiler_lookup_download_directory"); + LOG("profiler_lookup_async_signal_dump_directory"); MOZ_ASSERT( NS_IsMainThread(), @@ -6864,16 +6864,45 @@ void profiler_lookup_download_directory() { // take the lock so that we can write to CorePS PSAutoLock lock; + nsresult rv; - nsCOMPtr tDownloadDir; - nsresult rv = NS_GetSpecialDirectory(NS_OS_DEFAULT_DOWNLOAD_DIR, - getter_AddRefs(tDownloadDir)); - if (NS_FAILED(rv)) { - LOG("Failed to find download directory. Profiler signal handling will not " - "be able to save to disk. Error: %s", - GetStaticErrorName(rv)); + // Check to see if we have a `MOZ_UPLOAD_DIR` first - i.e., check to see if + // we're running in CI. + LOG("Checking if MOZ_UPLOAD_DIR exists"); + const char* mozUploadDir = getenv("MOZ_UPLOAD_DIR"); + if (mozUploadDir && mozUploadDir[0] != '\0') { + LOG("Found MOZ_UPLOAD_DIR at: %s", mozUploadDir); + // We want to do the right thing, and turn this into an nsIFile. Go through + // the motions here: + nsCOMPtr mozUploadDirFile = + do_CreateInstance("@mozilla.org/file/local;1", &rv); + + if (NS_FAILED(rv)) { + LOG("Failed to create nsIFile for MOZ_UPLOAD_DIR: %s, Error: %s", + mozUploadDir, GetStaticErrorName(rv)); + } else { + rv = mozUploadDirFile->InitWithNativePath( + nsDependentCString(mozUploadDir)); + if (NS_FAILED(rv)) { + LOG("Failed to assign a filepath while creating MOZ_UPLOAD_DIR file " + "%s, Error %s ", + mozUploadDir, GetStaticErrorName(rv)); + } else { + CorePS::SetAsyncSignalDumpDirectory(lock, Some(mozUploadDirFile)); + } + } } else { - CorePS::SetDownloadDirectory(lock, Some(tDownloadDir)); + LOG("Defaulting to the user's Download directory for profile dumps") + nsCOMPtr tDownloadDir; + rv = NS_GetSpecialDirectory(NS_OS_DEFAULT_DOWNLOAD_DIR, + getter_AddRefs(tDownloadDir)); + if (NS_FAILED(rv)) { + LOG("Failed to find download directory. Profiler signal handling will " + "not be able to save to disk. Error: %s", + GetStaticErrorName(rv)); + } else { + CorePS::SetAsyncSignalDumpDirectory(lock, Some(tDownloadDir)); + } } #endif } diff --git a/tools/profiler/public/ProfilerControl.h b/tools/profiler/public/ProfilerControl.h index 53fec681ceda..62b9ed3e0d21 100644 --- a/tools/profiler/public/ProfilerControl.h +++ b/tools/profiler/public/ProfilerControl.h @@ -40,7 +40,7 @@ static inline void profiler_init(void* stackTop) {} static inline void profiler_shutdown( IsFastShutdown aIsFastShutdown = IsFastShutdown::No) {} -static inline void profiler_lookup_download_directory() {} +static inline void profiler_lookup_async_signal_dump_directory() {} #else // !MOZ_GECKO_PROFILER @@ -138,7 +138,7 @@ void profiler_ensure_started( // third option, by giving us a hook to look for the download directory when // the time is right. This might be triggered internally (e.g. when we start // profiling), or externally, e.g. after the directory service is initialised. -void profiler_lookup_download_directory(); +void profiler_lookup_async_signal_dump_directory(); //--------------------------------------------------------------------------- // Control the profiler diff --git a/xpcom/build/XPCOMInit.cpp b/xpcom/build/XPCOMInit.cpp index 6275ca9c53d9..df51aa99f767 100644 --- a/xpcom/build/XPCOMInit.cpp +++ b/xpcom/build/XPCOMInit.cpp @@ -469,7 +469,7 @@ NS_InitXPCOM(nsIServiceManager** aResult, nsIFile* aBinDirectory, // Now that both the profiler and directory services have been started // we can find the download directory, where the profiler can write // profiles if necessary - profiler_lookup_download_directory(); + profiler_lookup_async_signal_dump_directory(); // Init mozilla::SharedThreadPool (which needs the service manager). mozilla::SharedThreadPool::InitStatics();