michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef TableTicker_h michael@0: #define TableTicker_h michael@0: michael@0: #include "platform.h" michael@0: #include "ProfileEntry.h" michael@0: #include "mozilla/Mutex.h" michael@0: #include "IntelPowerGadget.h" michael@0: michael@0: static bool michael@0: hasFeature(const char** aFeatures, uint32_t aFeatureCount, const char* aFeature) { michael@0: for(size_t i = 0; i < aFeatureCount; i++) { michael@0: if (strcmp(aFeatures[i], aFeature) == 0) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool michael@0: threadSelected(ThreadInfo* aInfo, char** aThreadNameFilters, uint32_t aFeatureCount) { michael@0: if (aFeatureCount == 0) { michael@0: return true; michael@0: } michael@0: michael@0: for (uint32_t i = 0; i < aFeatureCount; ++i) { michael@0: const char* filterPrefix = aThreadNameFilters[i]; michael@0: if (strncmp(aInfo->Name(), filterPrefix, strlen(filterPrefix)) == 0) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: extern TimeStamp sLastTracerEvent; michael@0: extern int sFrameNumber; michael@0: extern int sLastFrameNumber; michael@0: extern unsigned int sCurrentEventGeneration; michael@0: extern unsigned int sLastSampledEventGeneration; michael@0: michael@0: class BreakpadSampler; michael@0: michael@0: class TableTicker: public Sampler { michael@0: public: michael@0: TableTicker(double aInterval, int aEntrySize, michael@0: const char** aFeatures, uint32_t aFeatureCount, michael@0: const char** aThreadNameFilters, uint32_t aFilterCount) michael@0: : Sampler(aInterval, true, aEntrySize) michael@0: , mPrimaryThreadProfile(nullptr) michael@0: , mSaveRequested(false) michael@0: , mUnwinderThread(false) michael@0: , mFilterCount(aFilterCount) michael@0: #if defined(XP_WIN) michael@0: , mIntelPowerGadget(nullptr) michael@0: #endif michael@0: { michael@0: mUseStackWalk = hasFeature(aFeatures, aFeatureCount, "stackwalk"); michael@0: michael@0: //XXX: It's probably worth splitting the jank profiler out from the regular profiler at some point michael@0: mJankOnly = hasFeature(aFeatures, aFeatureCount, "jank"); michael@0: mProfileJS = hasFeature(aFeatures, aFeatureCount, "js"); michael@0: mProfileJava = hasFeature(aFeatures, aFeatureCount, "java"); michael@0: mProfilePower = hasFeature(aFeatures, aFeatureCount, "power"); michael@0: mProfileThreads = hasFeature(aFeatures, aFeatureCount, "threads"); michael@0: mUnwinderThread = hasFeature(aFeatures, aFeatureCount, "unwinder") || sps_version2(); michael@0: mAddLeafAddresses = hasFeature(aFeatures, aFeatureCount, "leaf"); michael@0: mPrivacyMode = hasFeature(aFeatures, aFeatureCount, "privacy"); michael@0: mAddMainThreadIO = hasFeature(aFeatures, aFeatureCount, "mainthreadio"); michael@0: michael@0: #if defined(XP_WIN) michael@0: if (mProfilePower) { michael@0: mIntelPowerGadget = new IntelPowerGadget(); michael@0: mProfilePower = mIntelPowerGadget->Init(); michael@0: } michael@0: #endif michael@0: michael@0: // Deep copy aThreadNameFilters michael@0: mThreadNameFilters = new char*[aFilterCount]; michael@0: for (uint32_t i = 0; i < aFilterCount; ++i) { michael@0: mThreadNameFilters[i] = strdup(aThreadNameFilters[i]); michael@0: } michael@0: michael@0: sStartTime = TimeStamp::Now(); michael@0: michael@0: { michael@0: mozilla::MutexAutoLock lock(*sRegisteredThreadsMutex); michael@0: michael@0: // Create ThreadProfile for each registered thread michael@0: for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) { michael@0: ThreadInfo* info = sRegisteredThreads->at(i); michael@0: michael@0: RegisterThread(info); michael@0: } michael@0: michael@0: SetActiveSampler(this); michael@0: } michael@0: } michael@0: michael@0: ~TableTicker() { michael@0: if (IsActive()) michael@0: Stop(); michael@0: michael@0: SetActiveSampler(nullptr); michael@0: michael@0: // Destroy ThreadProfile for all threads michael@0: { michael@0: mozilla::MutexAutoLock lock(*sRegisteredThreadsMutex); michael@0: michael@0: for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) { michael@0: ThreadInfo* info = sRegisteredThreads->at(i); michael@0: ThreadProfile* profile = info->Profile(); michael@0: if (profile) { michael@0: delete profile; michael@0: info->SetProfile(nullptr); michael@0: } michael@0: } michael@0: } michael@0: #if defined(XP_WIN) michael@0: delete mIntelPowerGadget; michael@0: #endif michael@0: } michael@0: michael@0: void RegisterThread(ThreadInfo* aInfo) { michael@0: if (!aInfo->IsMainThread() && !mProfileThreads) { michael@0: return; michael@0: } michael@0: michael@0: if (!threadSelected(aInfo, mThreadNameFilters, mFilterCount)) { michael@0: return; michael@0: } michael@0: michael@0: ThreadProfile* profile = new ThreadProfile(aInfo->Name(), michael@0: EntrySize(), michael@0: aInfo->Stack(), michael@0: aInfo->ThreadId(), michael@0: aInfo->GetPlatformData(), michael@0: aInfo->IsMainThread(), michael@0: aInfo->StackTop()); michael@0: aInfo->SetProfile(profile); michael@0: } michael@0: michael@0: // Called within a signal. This function must be reentrant michael@0: virtual void Tick(TickSample* sample); michael@0: michael@0: // Immediately captures the calling thread's call stack and returns it. michael@0: virtual SyncProfile* GetBacktrace(); michael@0: michael@0: // Called within a signal. This function must be reentrant michael@0: virtual void RequestSave() michael@0: { michael@0: mSaveRequested = true; michael@0: } michael@0: michael@0: virtual void HandleSaveRequest(); michael@0: michael@0: ThreadProfile* GetPrimaryThreadProfile() michael@0: { michael@0: if (!mPrimaryThreadProfile) { michael@0: mozilla::MutexAutoLock lock(*sRegisteredThreadsMutex); michael@0: michael@0: for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) { michael@0: ThreadInfo* info = sRegisteredThreads->at(i); michael@0: if (info->IsMainThread()) { michael@0: mPrimaryThreadProfile = info->Profile(); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return mPrimaryThreadProfile; michael@0: } michael@0: michael@0: void ToStreamAsJSON(std::ostream& stream); michael@0: virtual JSObject *ToJSObject(JSContext *aCx); michael@0: void StreamMetaJSCustomObject(JSStreamWriter& b); michael@0: bool HasUnwinderThread() const { return mUnwinderThread; } michael@0: bool ProfileJS() const { return mProfileJS; } michael@0: bool ProfileJava() const { return mProfileJava; } michael@0: bool ProfilePower() const { return mProfilePower; } michael@0: bool ProfileThreads() const { return mProfileThreads; } michael@0: bool InPrivacyMode() const { return mPrivacyMode; } michael@0: bool AddMainThreadIO() const { return mAddMainThreadIO; } michael@0: michael@0: protected: michael@0: // Called within a signal. This function must be reentrant michael@0: virtual void UnwinderTick(TickSample* sample); michael@0: michael@0: // Called within a signal. This function must be reentrant michael@0: virtual void InplaceTick(TickSample* sample); michael@0: michael@0: // Not implemented on platforms which do not support backtracing michael@0: void doNativeBacktrace(ThreadProfile &aProfile, TickSample* aSample); michael@0: michael@0: void StreamJSObject(JSStreamWriter& b); michael@0: michael@0: // This represent the application's main thread (SAMPLER_INIT) michael@0: ThreadProfile* mPrimaryThreadProfile; michael@0: bool mSaveRequested; michael@0: bool mAddLeafAddresses; michael@0: bool mUseStackWalk; michael@0: bool mJankOnly; michael@0: bool mProfileJS; michael@0: bool mProfileThreads; michael@0: bool mUnwinderThread; michael@0: bool mProfileJava; michael@0: bool mProfilePower; michael@0: michael@0: // Keep the thread filter to check against new thread that michael@0: // are started while profiling michael@0: char** mThreadNameFilters; michael@0: uint32_t mFilterCount; michael@0: bool mPrivacyMode; michael@0: bool mAddMainThreadIO; michael@0: #if defined(XP_WIN) michael@0: IntelPowerGadget* mIntelPowerGadget; michael@0: #endif michael@0: }; michael@0: michael@0: #endif michael@0: