| 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 ProfiledThreadData_h |
| 8 |
#define ProfiledThreadData_h |
| 9 |
|
| 10 |
#include "BaseProfilingStack.h" |
| 11 |
#include "platform.h" |
| 12 |
#include "ProfileBufferEntry.h" |
| 13 |
#include "ThreadInfo.h" |
| 14 |
|
| 15 |
#include "mozilla/RefPtr.h" |
| 16 |
#include "mozilla/TimeStamp.h" |
| 17 |
#include "mozilla/UniquePtr.h" |
| 18 |
|
| 19 |
#include <string> |
| 20 |
|
| 21 |
namespace mozilla { |
| 22 |
namespace baseprofiler { |
| 23 |
|
| 24 |
class ProfileBuffer; |
| 25 |
|
| 26 |
// This class contains information about a thread that is only relevant while |
| 27 |
// the profiler is running, for any threads (both alive and dead) whose thread |
| 28 |
// name matches the "thread filter" in the current profiler run. |
| 29 |
// ProfiledThreadData objects may be kept alive even after the thread is |
| 30 |
// unregistered, as long as there is still data for that thread in the profiler |
| 31 |
// buffer. |
| 32 |
// |
| 33 |
// Accesses to this class are protected by the profiler state lock. |
| 34 |
// |
| 35 |
// Created as soon as the following are true for the thread: |
| 36 |
// - The profiler is running, and |
| 37 |
// - the thread matches the profiler's thread filter, and |
| 38 |
// - the thread is registered with the profiler. |
| 39 |
// So it gets created in response to either (1) the profiler being started (for |
| 40 |
// an existing registered thread) or (2) the thread being registered (if the |
| 41 |
// profiler is already running). |
| 42 |
// |
| 43 |
// The thread may be unregistered during the lifetime of ProfiledThreadData. |
| 44 |
// If that happens, NotifyUnregistered() is called. |
| 45 |
// |
| 46 |
// This class is the right place to store buffer positions. Profiler buffer |
| 47 |
// positions become invalid if the profiler buffer is destroyed, which happens |
| 48 |
// when the profiler is stopped. |
| 49 |
class ProfiledThreadData final { |
| 50 |
public: |
| 51 |
explicit ProfiledThreadData(ThreadInfo* aThreadInfo); |
| 52 |
~ProfiledThreadData(); |
| 53 |
|
| 54 |
void NotifyUnregistered(uint64_t aBufferPosition) { |
| 55 |
mLastSample = Nothing(); |
| 56 |
MOZ_ASSERT(!mBufferPositionWhenReceivedJSContext, |
| 57 |
"JSContext should have been cleared before the thread was " |
| 58 |
"unregistered"); |
| 59 |
mUnregisterTime = TimeStamp::Now(); |
| 60 |
mBufferPositionWhenUnregistered = Some(aBufferPosition); |
| 61 |
} |
| 62 |
Maybe<uint64_t> BufferPositionWhenUnregistered() { |
| 63 |
return mBufferPositionWhenUnregistered; |
| 64 |
} |
| 65 |
|
| 66 |
Maybe<uint64_t>& LastSample() { return mLastSample; } |
| 67 |
|
| 68 |
void StreamJSON(const ProfileBuffer& aBuffer, SpliceableJSONWriter& aWriter, |
| 69 |
const std::string& aProcessName, |
| 70 |
const std::string& aETLDplus1, |
| 71 |
const TimeStamp& aProcessStartTime, double aSinceTime); |
| 72 |
|
| 73 |
const RefPtr<ThreadInfo> Info() const { return mThreadInfo; } |
| 74 |
|
| 75 |
void NotifyReceivedJSContext(uint64_t aCurrentBufferPosition) { |
| 76 |
mBufferPositionWhenReceivedJSContext = Some(aCurrentBufferPosition); |
| 77 |
} |
| 78 |
|
| 79 |
private: |
| 80 |
// Group A: |
| 81 |
// The following fields are interesting for the entire lifetime of a |
| 82 |
// ProfiledThreadData object. |
| 83 |
|
| 84 |
// This thread's thread info. |
| 85 |
const RefPtr<ThreadInfo> mThreadInfo; |
| 86 |
|
| 87 |
// Group B: |
| 88 |
// The following fields are only used while this thread is alive and |
| 89 |
// registered. They become Nothing() once the thread is unregistered. |
| 90 |
|
| 91 |
// When sampling, this holds the position in ActivePS::mBuffer of the most |
| 92 |
// recent sample for this thread, or Nothing() if there is no sample for this |
| 93 |
// thread in the buffer. |
| 94 |
Maybe<uint64_t> mLastSample; |
| 95 |
|
| 96 |
// Only non-Nothing() if the thread currently has a JSContext. |
| 97 |
Maybe<uint64_t> mBufferPositionWhenReceivedJSContext; |
| 98 |
|
| 99 |
// Group C: |
| 100 |
// The following fields are only used once this thread has been unregistered. |
| 101 |
|
| 102 |
Maybe<uint64_t> mBufferPositionWhenUnregistered; |
| 103 |
TimeStamp mUnregisterTime; |
| 104 |
}; |
| 105 |
|
| 106 |
// Stream all samples and markers from aBuffer with the given aThreadId (or 0 |
| 107 |
// for everything, which is assumed to be a single backtrace sample.) |
| 108 |
// Returns the thread id of the output sample(s), or 0 if none was present. |
| 109 |
BaseProfilerThreadId StreamSamplesAndMarkers( |
| 110 |
const char* aName, BaseProfilerThreadId aThreadId, |
| 111 |
const ProfileBuffer& aBuffer, SpliceableJSONWriter& aWriter, |
| 112 |
const std::string& aProcessName, const std::string& aETLDplus1, |
| 113 |
const TimeStamp& aProcessStartTime, const TimeStamp& aRegisterTime, |
| 114 |
const TimeStamp& aUnregisterTime, double aSinceTime, |
| 115 |
UniqueStacks& aUniqueStacks); |
| 116 |
|
| 117 |
} // namespace baseprofiler |
| 118 |
} // namespace mozilla |
| 119 |
|
| 120 |
#endif // ProfiledThreadData_h |