| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef RegisteredThread_h |
| 8 |
#define RegisteredThread_h |
| 9 |
|
| 10 |
#include "platform.h" |
| 11 |
#include "ThreadInfo.h" |
| 12 |
|
| 13 |
#include "mozilla/UniquePtr.h" |
| 14 |
|
| 15 |
namespace mozilla { |
| 16 |
namespace baseprofiler { |
| 17 |
|
| 18 |
// This class contains the state for a single thread that is accessible without |
| 19 |
// protection from gPSMutex in platform.cpp. Because there is no external |
| 20 |
// protection against data races, it must provide internal protection. Hence |
| 21 |
// the "Racy" prefix. |
| 22 |
// |
| 23 |
class RacyRegisteredThread final { |
| 24 |
public: |
| 25 |
explicit RacyRegisteredThread(BaseProfilerThreadId aThreadId) |
| 26 |
: mThreadId(aThreadId), mSleep(AWAKE), mIsBeingProfiled(false) {} |
| 27 |
|
| 28 |
~RacyRegisteredThread() {} |
| 29 |
|
| 30 |
void SetIsBeingProfiled(bool aIsBeingProfiled) { |
| 31 |
mIsBeingProfiled = aIsBeingProfiled; |
| 32 |
} |
| 33 |
|
| 34 |
bool IsBeingProfiled() const { return mIsBeingProfiled; } |
| 35 |
|
| 36 |
// This is called on every profiler restart. Put things that should happen at |
| 37 |
// that time here. |
| 38 |
void ReinitializeOnResume() { |
| 39 |
// This is needed to cause an initial sample to be taken from sleeping |
| 40 |
// threads that had been observed prior to the profiler stopping and |
| 41 |
// restarting. Otherwise sleeping threads would not have any samples to |
| 42 |
// copy forward while sleeping. |
| 43 |
(void)mSleep.compareExchange(SLEEPING_OBSERVED, SLEEPING_NOT_OBSERVED); |
| 44 |
} |
| 45 |
|
| 46 |
// This returns true for the second and subsequent calls in each sleep cycle. |
| 47 |
bool CanDuplicateLastSampleDueToSleep() { |
| 48 |
if (mSleep == AWAKE) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
if (mSleep.compareExchange(SLEEPING_NOT_OBSERVED, SLEEPING_OBSERVED)) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
return true; |
| 57 |
} |
| 58 |
|
| 59 |
// Call this whenever the current thread sleeps. Calling it twice in a row |
| 60 |
// without an intervening setAwake() call is an error. |
| 61 |
void SetSleeping() { |
| 62 |
MOZ_ASSERT(mSleep == AWAKE); |
| 63 |
mSleep = SLEEPING_NOT_OBSERVED; |
| 64 |
} |
| 65 |
|
| 66 |
// Call this whenever the current thread wakes. Calling it twice in a row |
| 67 |
// without an intervening setSleeping() call is an error. |
| 68 |
void SetAwake() { |
| 69 |
MOZ_ASSERT(mSleep != AWAKE); |
| 70 |
mSleep = AWAKE; |
| 71 |
} |
| 72 |
|
| 73 |
bool IsSleeping() { return mSleep != AWAKE; } |
| 74 |
|
| 75 |
BaseProfilerThreadId ThreadId() const { return mThreadId; } |
| 76 |
|
| 77 |
class ProfilingStack& ProfilingStack() { return mProfilingStack; } |
| 78 |
const class ProfilingStack& ProfilingStack() const { return mProfilingStack; } |
| 79 |
|
| 80 |
private: |
| 81 |
class ProfilingStack mProfilingStack; |
| 82 |
|
| 83 |
// mThreadId contains the thread ID of the current thread. It is safe to read |
| 84 |
// this from multiple threads concurrently, as it will never be mutated. |
| 85 |
const BaseProfilerThreadId mThreadId; |
| 86 |
|
| 87 |
// mSleep tracks whether the thread is sleeping, and if so, whether it has |
| 88 |
// been previously observed. This is used for an optimization: in some cases, |
| 89 |
// when a thread is asleep, we duplicate the previous sample, which is |
| 90 |
// cheaper than taking a new sample. |
| 91 |
// |
| 92 |
// mSleep is atomic because it is accessed from multiple threads. |
| 93 |
// |
| 94 |
// - It is written only by this thread, via setSleeping() and setAwake(). |
| 95 |
// |
| 96 |
// - It is read by SamplerThread::Run(). |
| 97 |
// |
| 98 |
// There are two cases where racing between threads can cause an issue. |
| 99 |
// |
| 100 |
// - If CanDuplicateLastSampleDueToSleep() returns false but that result is |
| 101 |
// invalidated before being acted upon, we will take a full sample |
| 102 |
// unnecessarily. This is additional work but won't cause any correctness |
| 103 |
// issues. (In actual fact, this case is impossible. In order to go from |
| 104 |
// CanDuplicateLastSampleDueToSleep() returning false to it returning true |
| 105 |
// requires an intermediate call to it in order for mSleep to go from |
| 106 |
// SLEEPING_NOT_OBSERVED to SLEEPING_OBSERVED.) |
| 107 |
// |
| 108 |
// - If CanDuplicateLastSampleDueToSleep() returns true but that result is |
| 109 |
// invalidated before being acted upon -- i.e. the thread wakes up before |
| 110 |
// DuplicateLastSample() is called -- we will duplicate the previous |
| 111 |
// sample. This is inaccurate, but only slightly... we will effectively |
| 112 |
// treat the thread as having slept a tiny bit longer than it really did. |
| 113 |
// |
| 114 |
// This latter inaccuracy could be avoided by moving the |
| 115 |
// CanDuplicateLastSampleDueToSleep() check within the thread-freezing code, |
| 116 |
// e.g. the section where Tick() is called. But that would reduce the |
| 117 |
// effectiveness of the optimization because more code would have to be run |
| 118 |
// before we can tell that duplication is allowed. |
| 119 |
// |
| 120 |
static const int AWAKE = 0; |
| 121 |
static const int SLEEPING_NOT_OBSERVED = 1; |
| 122 |
static const int SLEEPING_OBSERVED = 2; |
| 123 |
Atomic<int> mSleep; |
| 124 |
|
| 125 |
// Is this thread being profiled? (e.g., should markers be recorded?) |
| 126 |
Atomic<bool, MemoryOrdering::Relaxed> mIsBeingProfiled; |
| 127 |
}; |
| 128 |
|
| 129 |
// This class contains information that's relevant to a single thread only |
| 130 |
// while that thread is running and registered with the profiler, but |
| 131 |
// regardless of whether the profiler is running. All accesses to it are |
| 132 |
// protected by the profiler state lock. |
| 133 |
class RegisteredThread final { |
| 134 |
public: |
| 135 |
RegisteredThread(ThreadInfo* aInfo, void* aStackTop); |
| 136 |
~RegisteredThread(); |
| 137 |
|
| 138 |
class RacyRegisteredThread& RacyRegisteredThread() { |
| 139 |
return mRacyRegisteredThread; |
| 140 |
} |
| 141 |
const class RacyRegisteredThread& RacyRegisteredThread() const { |
| 142 |
return mRacyRegisteredThread; |
| 143 |
} |
| 144 |
|
| 145 |
PlatformData* GetPlatformData() const { return mPlatformData.get(); } |
| 146 |
const void* StackTop() const { return mStackTop; } |
| 147 |
|
| 148 |
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const; |
| 149 |
|
| 150 |
const RefPtr<ThreadInfo> Info() const { return mThreadInfo; } |
| 151 |
|
| 152 |
private: |
| 153 |
class RacyRegisteredThread mRacyRegisteredThread; |
| 154 |
|
| 155 |
const UniquePlatformData mPlatformData; |
| 156 |
const void* mStackTop; |
| 157 |
|
| 158 |
const RefPtr<ThreadInfo> mThreadInfo; |
| 159 |
}; |
| 160 |
|
| 161 |
} // namespace baseprofiler |
| 162 |
} // namespace mozilla |
| 163 |
|
| 164 |
#endif // RegisteredThread_h |