Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef TableTicker_h |
michael@0 | 7 | #define TableTicker_h |
michael@0 | 8 | |
michael@0 | 9 | #include "platform.h" |
michael@0 | 10 | #include "ProfileEntry.h" |
michael@0 | 11 | #include "mozilla/Mutex.h" |
michael@0 | 12 | #include "IntelPowerGadget.h" |
michael@0 | 13 | |
michael@0 | 14 | static bool |
michael@0 | 15 | hasFeature(const char** aFeatures, uint32_t aFeatureCount, const char* aFeature) { |
michael@0 | 16 | for(size_t i = 0; i < aFeatureCount; i++) { |
michael@0 | 17 | if (strcmp(aFeatures[i], aFeature) == 0) |
michael@0 | 18 | return true; |
michael@0 | 19 | } |
michael@0 | 20 | return false; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | static bool |
michael@0 | 24 | threadSelected(ThreadInfo* aInfo, char** aThreadNameFilters, uint32_t aFeatureCount) { |
michael@0 | 25 | if (aFeatureCount == 0) { |
michael@0 | 26 | return true; |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | for (uint32_t i = 0; i < aFeatureCount; ++i) { |
michael@0 | 30 | const char* filterPrefix = aThreadNameFilters[i]; |
michael@0 | 31 | if (strncmp(aInfo->Name(), filterPrefix, strlen(filterPrefix)) == 0) { |
michael@0 | 32 | return true; |
michael@0 | 33 | } |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | return false; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | extern TimeStamp sLastTracerEvent; |
michael@0 | 40 | extern int sFrameNumber; |
michael@0 | 41 | extern int sLastFrameNumber; |
michael@0 | 42 | extern unsigned int sCurrentEventGeneration; |
michael@0 | 43 | extern unsigned int sLastSampledEventGeneration; |
michael@0 | 44 | |
michael@0 | 45 | class BreakpadSampler; |
michael@0 | 46 | |
michael@0 | 47 | class TableTicker: public Sampler { |
michael@0 | 48 | public: |
michael@0 | 49 | TableTicker(double aInterval, int aEntrySize, |
michael@0 | 50 | const char** aFeatures, uint32_t aFeatureCount, |
michael@0 | 51 | const char** aThreadNameFilters, uint32_t aFilterCount) |
michael@0 | 52 | : Sampler(aInterval, true, aEntrySize) |
michael@0 | 53 | , mPrimaryThreadProfile(nullptr) |
michael@0 | 54 | , mSaveRequested(false) |
michael@0 | 55 | , mUnwinderThread(false) |
michael@0 | 56 | , mFilterCount(aFilterCount) |
michael@0 | 57 | #if defined(XP_WIN) |
michael@0 | 58 | , mIntelPowerGadget(nullptr) |
michael@0 | 59 | #endif |
michael@0 | 60 | { |
michael@0 | 61 | mUseStackWalk = hasFeature(aFeatures, aFeatureCount, "stackwalk"); |
michael@0 | 62 | |
michael@0 | 63 | //XXX: It's probably worth splitting the jank profiler out from the regular profiler at some point |
michael@0 | 64 | mJankOnly = hasFeature(aFeatures, aFeatureCount, "jank"); |
michael@0 | 65 | mProfileJS = hasFeature(aFeatures, aFeatureCount, "js"); |
michael@0 | 66 | mProfileJava = hasFeature(aFeatures, aFeatureCount, "java"); |
michael@0 | 67 | mProfilePower = hasFeature(aFeatures, aFeatureCount, "power"); |
michael@0 | 68 | mProfileThreads = hasFeature(aFeatures, aFeatureCount, "threads"); |
michael@0 | 69 | mUnwinderThread = hasFeature(aFeatures, aFeatureCount, "unwinder") || sps_version2(); |
michael@0 | 70 | mAddLeafAddresses = hasFeature(aFeatures, aFeatureCount, "leaf"); |
michael@0 | 71 | mPrivacyMode = hasFeature(aFeatures, aFeatureCount, "privacy"); |
michael@0 | 72 | mAddMainThreadIO = hasFeature(aFeatures, aFeatureCount, "mainthreadio"); |
michael@0 | 73 | |
michael@0 | 74 | #if defined(XP_WIN) |
michael@0 | 75 | if (mProfilePower) { |
michael@0 | 76 | mIntelPowerGadget = new IntelPowerGadget(); |
michael@0 | 77 | mProfilePower = mIntelPowerGadget->Init(); |
michael@0 | 78 | } |
michael@0 | 79 | #endif |
michael@0 | 80 | |
michael@0 | 81 | // Deep copy aThreadNameFilters |
michael@0 | 82 | mThreadNameFilters = new char*[aFilterCount]; |
michael@0 | 83 | for (uint32_t i = 0; i < aFilterCount; ++i) { |
michael@0 | 84 | mThreadNameFilters[i] = strdup(aThreadNameFilters[i]); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | sStartTime = TimeStamp::Now(); |
michael@0 | 88 | |
michael@0 | 89 | { |
michael@0 | 90 | mozilla::MutexAutoLock lock(*sRegisteredThreadsMutex); |
michael@0 | 91 | |
michael@0 | 92 | // Create ThreadProfile for each registered thread |
michael@0 | 93 | for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) { |
michael@0 | 94 | ThreadInfo* info = sRegisteredThreads->at(i); |
michael@0 | 95 | |
michael@0 | 96 | RegisterThread(info); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | SetActiveSampler(this); |
michael@0 | 100 | } |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | ~TableTicker() { |
michael@0 | 104 | if (IsActive()) |
michael@0 | 105 | Stop(); |
michael@0 | 106 | |
michael@0 | 107 | SetActiveSampler(nullptr); |
michael@0 | 108 | |
michael@0 | 109 | // Destroy ThreadProfile for all threads |
michael@0 | 110 | { |
michael@0 | 111 | mozilla::MutexAutoLock lock(*sRegisteredThreadsMutex); |
michael@0 | 112 | |
michael@0 | 113 | for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) { |
michael@0 | 114 | ThreadInfo* info = sRegisteredThreads->at(i); |
michael@0 | 115 | ThreadProfile* profile = info->Profile(); |
michael@0 | 116 | if (profile) { |
michael@0 | 117 | delete profile; |
michael@0 | 118 | info->SetProfile(nullptr); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | #if defined(XP_WIN) |
michael@0 | 123 | delete mIntelPowerGadget; |
michael@0 | 124 | #endif |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | void RegisterThread(ThreadInfo* aInfo) { |
michael@0 | 128 | if (!aInfo->IsMainThread() && !mProfileThreads) { |
michael@0 | 129 | return; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | if (!threadSelected(aInfo, mThreadNameFilters, mFilterCount)) { |
michael@0 | 133 | return; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | ThreadProfile* profile = new ThreadProfile(aInfo->Name(), |
michael@0 | 137 | EntrySize(), |
michael@0 | 138 | aInfo->Stack(), |
michael@0 | 139 | aInfo->ThreadId(), |
michael@0 | 140 | aInfo->GetPlatformData(), |
michael@0 | 141 | aInfo->IsMainThread(), |
michael@0 | 142 | aInfo->StackTop()); |
michael@0 | 143 | aInfo->SetProfile(profile); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | // Called within a signal. This function must be reentrant |
michael@0 | 147 | virtual void Tick(TickSample* sample); |
michael@0 | 148 | |
michael@0 | 149 | // Immediately captures the calling thread's call stack and returns it. |
michael@0 | 150 | virtual SyncProfile* GetBacktrace(); |
michael@0 | 151 | |
michael@0 | 152 | // Called within a signal. This function must be reentrant |
michael@0 | 153 | virtual void RequestSave() |
michael@0 | 154 | { |
michael@0 | 155 | mSaveRequested = true; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | virtual void HandleSaveRequest(); |
michael@0 | 159 | |
michael@0 | 160 | ThreadProfile* GetPrimaryThreadProfile() |
michael@0 | 161 | { |
michael@0 | 162 | if (!mPrimaryThreadProfile) { |
michael@0 | 163 | mozilla::MutexAutoLock lock(*sRegisteredThreadsMutex); |
michael@0 | 164 | |
michael@0 | 165 | for (uint32_t i = 0; i < sRegisteredThreads->size(); i++) { |
michael@0 | 166 | ThreadInfo* info = sRegisteredThreads->at(i); |
michael@0 | 167 | if (info->IsMainThread()) { |
michael@0 | 168 | mPrimaryThreadProfile = info->Profile(); |
michael@0 | 169 | break; |
michael@0 | 170 | } |
michael@0 | 171 | } |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | return mPrimaryThreadProfile; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | void ToStreamAsJSON(std::ostream& stream); |
michael@0 | 178 | virtual JSObject *ToJSObject(JSContext *aCx); |
michael@0 | 179 | void StreamMetaJSCustomObject(JSStreamWriter& b); |
michael@0 | 180 | bool HasUnwinderThread() const { return mUnwinderThread; } |
michael@0 | 181 | bool ProfileJS() const { return mProfileJS; } |
michael@0 | 182 | bool ProfileJava() const { return mProfileJava; } |
michael@0 | 183 | bool ProfilePower() const { return mProfilePower; } |
michael@0 | 184 | bool ProfileThreads() const { return mProfileThreads; } |
michael@0 | 185 | bool InPrivacyMode() const { return mPrivacyMode; } |
michael@0 | 186 | bool AddMainThreadIO() const { return mAddMainThreadIO; } |
michael@0 | 187 | |
michael@0 | 188 | protected: |
michael@0 | 189 | // Called within a signal. This function must be reentrant |
michael@0 | 190 | virtual void UnwinderTick(TickSample* sample); |
michael@0 | 191 | |
michael@0 | 192 | // Called within a signal. This function must be reentrant |
michael@0 | 193 | virtual void InplaceTick(TickSample* sample); |
michael@0 | 194 | |
michael@0 | 195 | // Not implemented on platforms which do not support backtracing |
michael@0 | 196 | void doNativeBacktrace(ThreadProfile &aProfile, TickSample* aSample); |
michael@0 | 197 | |
michael@0 | 198 | void StreamJSObject(JSStreamWriter& b); |
michael@0 | 199 | |
michael@0 | 200 | // This represent the application's main thread (SAMPLER_INIT) |
michael@0 | 201 | ThreadProfile* mPrimaryThreadProfile; |
michael@0 | 202 | bool mSaveRequested; |
michael@0 | 203 | bool mAddLeafAddresses; |
michael@0 | 204 | bool mUseStackWalk; |
michael@0 | 205 | bool mJankOnly; |
michael@0 | 206 | bool mProfileJS; |
michael@0 | 207 | bool mProfileThreads; |
michael@0 | 208 | bool mUnwinderThread; |
michael@0 | 209 | bool mProfileJava; |
michael@0 | 210 | bool mProfilePower; |
michael@0 | 211 | |
michael@0 | 212 | // Keep the thread filter to check against new thread that |
michael@0 | 213 | // are started while profiling |
michael@0 | 214 | char** mThreadNameFilters; |
michael@0 | 215 | uint32_t mFilterCount; |
michael@0 | 216 | bool mPrivacyMode; |
michael@0 | 217 | bool mAddMainThreadIO; |
michael@0 | 218 | #if defined(XP_WIN) |
michael@0 | 219 | IntelPowerGadget* mIntelPowerGadget; |
michael@0 | 220 | #endif |
michael@0 | 221 | }; |
michael@0 | 222 | |
michael@0 | 223 | #endif |
michael@0 | 224 |