1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ProfilerThreadRegistrationData.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/FlowMarkers.h"
#include "mozilla/FOGIPC.h"
#include "mozilla/ProfilerMarkers.h"
#include "js/AllocationRecording.h"
#include "js/ProfilingStack.h"
#if defined(XP_WIN)
# include <windows.h>
#elif defined(XP_DARWIN)
# include <pthread.h>
#endif
#ifdef NIGHTLY_BUILD
namespace geckoprofiler::markers {
using namespace mozilla;
struct ThreadCpuUseMarker {
static constexpr Span<const char> MarkerTypeName() {
return MakeStringSpan("ThreadCpuUse");
}
static void StreamJSONMarkerData(baseprofiler::SpliceableJSONWriter& aWriter,
ProfilerThreadId aThreadId,
int64_t aCpuTimeMs, int64_t aWakeUps,
const ProfilerString8View& aThreadName) {
aWriter.IntProperty("threadId", static_cast<int64_t>(aThreadId.ToNumber()));
aWriter.IntProperty("time", aCpuTimeMs);
aWriter.IntProperty("wakeups", aWakeUps);
aWriter.StringProperty("label", aThreadName);
}
static MarkerSchema MarkerTypeDisplay() {
using MS = MarkerSchema;
MS schema{MS::Location::MarkerChart, MS::Location::MarkerTable};
schema.AddKeyLabelFormat("time", "CPU Time", MS::Format::Milliseconds);
schema.AddKeyLabelFormat("wakeups", "Wake ups", MS::Format::Integer);
schema.SetTooltipLabel("{marker.name} - {marker.data.label}");
schema.SetTableLabel(
"{marker.data.label}: {marker.data.time} of CPU time, "
"{marker.data.wakeups} wake ups");
return schema;
}
};
} // namespace geckoprofiler::markers
#endif
namespace mozilla::profiler {
ThreadRegistrationData::ThreadRegistrationData(const char* aName,
const void* aStackTop)
: mInfo(aName),
mPlatformData(mInfo.ThreadId()),
mStackTop(
#if defined(XP_WIN)
// We don't have to guess on Windows.
reinterpret_cast<const void*>(
reinterpret_cast<PNT_TIB>(NtCurrentTeb())->StackBase)
#elif defined(XP_DARWIN)
// We don't have to guess on Mac/Darwin.
reinterpret_cast<const void*>(
pthread_get_stackaddr_np(pthread_self()))
#else
// Otherwise use the given guess.
aStackTop
#endif
) {
}
// This is a simplified version of profiler_add_marker that can be easily passed
// into the JS engine.
static void profiler_add_js_marker(mozilla::MarkerCategory aCategory,
const char* aMarkerName,
const char* aMarkerText) {
#ifdef MOZ_GECKO_PROFILER
AUTO_PROFILER_STATS(js_marker);
profiler_add_marker(
mozilla::ProfilerString8View::WrapNullTerminatedString(aMarkerName),
aCategory, {}, ::geckoprofiler::markers::TextMarker{},
mozilla::ProfilerString8View::WrapNullTerminatedString(aMarkerText));
#endif
}
static void profiler_add_js_interval(mozilla::MarkerCategory aCategory,
const char* aMarkerName,
mozilla::TimeStamp aStartTime,
const char* aMarkerText) {
#ifdef MOZ_GECKO_PROFILER
AUTO_PROFILER_STATS(js_interval);
profiler_add_marker(
mozilla::ProfilerString8View::WrapNullTerminatedString(aMarkerName),
aCategory, mozilla::MarkerTiming::IntervalUntilNowFrom(aStartTime),
::geckoprofiler::markers::TextMarker{},
mozilla::ProfilerString8View::WrapNullTerminatedString(aMarkerText));
#endif
}
static void profiler_add_js_flow(mozilla::MarkerCategory aCategory,
const char* aMarkerName, uint64_t aFlowId) {
#ifdef MOZ_GECKO_PROFILER
if (!profiler_feature_active(ProfilerFeature::Flows)) {
return;
}
AUTO_PROFILER_STATS(js_flow);
profiler_add_marker(
mozilla::ProfilerString8View::WrapNullTerminatedString(aMarkerName),
aCategory, {}, ::geckoprofiler::markers::FlowMarker{},
Flow::ProcessScoped(aFlowId));
#endif
}
static void profiler_add_js_terminating_flow(mozilla::MarkerCategory aCategory,
const char* aMarkerName,
uint64_t aFlowId) {
#ifdef MOZ_GECKO_PROFILER
if (!profiler_feature_active(ProfilerFeature::Flows)) {
return;
}
AUTO_PROFILER_STATS(js_terminating_flow);
profiler_add_marker(
mozilla::ProfilerString8View::WrapNullTerminatedString(aMarkerName),
aCategory, {}, ::geckoprofiler::markers::TerminatingFlowMarker{},
Flow::ProcessScoped(aFlowId));
#endif
}
static void profiler_add_js_allocation_marker(JS::RecordAllocationInfo&& info) {
if (!profiler_thread_is_being_profiled_for_markers()) {
return;
}
struct JsAllocationMarker {
static constexpr mozilla::Span<const char> MarkerTypeName() {
return mozilla::MakeStringSpan("JS allocation");
}
static void StreamJSONMarkerData(
mozilla::baseprofiler::SpliceableJSONWriter& aWriter,
const mozilla::ProfilerString16View& aTypeName,
const mozilla::ProfilerString8View& aClassName,
const mozilla::ProfilerString16View& aDescriptiveTypeName,
const mozilla::ProfilerString8View& aCoarseType, uint64_t aSize,
bool aInNursery) {
if (aClassName.Length() != 0) {
aWriter.StringProperty("className", aClassName);
}
if (aTypeName.Length() != 0) {
aWriter.StringProperty("typeName", NS_ConvertUTF16toUTF8(aTypeName));
}
if (aDescriptiveTypeName.Length() != 0) {
aWriter.StringProperty("descriptiveTypeName",
NS_ConvertUTF16toUTF8(aDescriptiveTypeName));
}
aWriter.StringProperty("coarseType", aCoarseType);
aWriter.IntProperty("size", aSize);
aWriter.BoolProperty("inNursery", aInNursery);
}
static mozilla::MarkerSchema MarkerTypeDisplay() {
return mozilla::MarkerSchema::SpecialFrontendLocation{};
}
};
profiler_add_marker(
"JS allocation", geckoprofiler::category::JS,
mozilla::MarkerStack::Capture(), JsAllocationMarker{},
mozilla::ProfilerString16View::WrapNullTerminatedString(info.typeName),
mozilla::ProfilerString8View::WrapNullTerminatedString(info.className),
mozilla::ProfilerString16View::WrapNullTerminatedString(
info.descriptiveTypeName),
mozilla::ProfilerString8View::WrapNullTerminatedString(info.coarseType),
info.size, info.inNursery);
}
JSContext* ThreadRegistrationUnlockedReaderAndAtomicRWOnThread::GetJSContext()
const {
if (!mCCJSContext) {
return nullptr;
}
return mCCJSContext->Context();
}
void ThreadRegistrationLockedRWFromAnyThread::SetProfilingFeaturesAndData(
ThreadProfilingFeatures aProfilingFeatures,
ProfiledThreadData* aProfiledThreadData, const PSAutoLock&) {
MOZ_ASSERT(mProfilingFeatures == ThreadProfilingFeatures::NotProfiled);
mProfilingFeatures = aProfilingFeatures;
MOZ_ASSERT(!mProfiledThreadData);
MOZ_ASSERT(aProfiledThreadData);
mProfiledThreadData = aProfiledThreadData;
if (mCCJSContext) {
// The thread is now being profiled, and we already have a JSContext,
// allocate a JsFramesBuffer to allow profiler-unlocked on-thread sampling.
MOZ_ASSERT(!mJsFrameBuffer);
mJsFrameBuffer = new JsFrame[MAX_JS_FRAMES];
}
// Check invariants.
MOZ_ASSERT((mProfilingFeatures != ThreadProfilingFeatures::NotProfiled) ==
!!mProfiledThreadData);
MOZ_ASSERT((mCCJSContext &&
(mProfilingFeatures != ThreadProfilingFeatures::NotProfiled)) ==
!!mJsFrameBuffer);
}
void ThreadRegistrationLockedRWFromAnyThread::ClearProfilingFeaturesAndData(
const PSAutoLock&) {
mProfilingFeatures = ThreadProfilingFeatures::NotProfiled;
mProfiledThreadData = nullptr;
if (mJsFrameBuffer) {
delete[] mJsFrameBuffer;
mJsFrameBuffer = nullptr;
}
// Check invariants.
MOZ_ASSERT((mProfilingFeatures != ThreadProfilingFeatures::NotProfiled) ==
!!mProfiledThreadData);
MOZ_ASSERT((mCCJSContext &&
(mProfilingFeatures != ThreadProfilingFeatures::NotProfiled)) ==
!!mJsFrameBuffer);
}
void ThreadRegistrationLockedRWOnThread::SetCycleCollectedJSContext(
CycleCollectedJSContext* aCx) {
MOZ_ASSERT(aCx && !mCCJSContext);
MOZ_ASSERT(aCx->Context());
mCCJSContext = aCx;
if (mProfiledThreadData) {
MOZ_ASSERT((mProfilingFeatures != ThreadProfilingFeatures::NotProfiled) ==
!!mProfiledThreadData);
// We now have a JSContext, and the thread is already being profiled,
// allocate a JsFramesBuffer to allow profiler-unlocked on-thread sampling.
MOZ_ASSERT(!mJsFrameBuffer);
mJsFrameBuffer = new JsFrame[MAX_JS_FRAMES];
}
// We give the JS engine a non-owning reference to the ProfilingStack. It's
// important that the JS engine doesn't touch this once the thread dies.
js::SetContextProfilingStack(aCx->Context(), &ProfilingStackRef());
// Check invariants.
MOZ_ASSERT((mCCJSContext &&
(mProfilingFeatures != ThreadProfilingFeatures::NotProfiled)) ==
!!mJsFrameBuffer);
}
void ThreadRegistrationLockedRWOnThread::ClearCycleCollectedJSContext() {
mCCJSContext = nullptr;
if (mJsFrameBuffer) {
delete[] mJsFrameBuffer;
mJsFrameBuffer = nullptr;
}
// Check invariants.
MOZ_ASSERT((mCCJSContext &&
(mProfilingFeatures != ThreadProfilingFeatures::NotProfiled)) ==
!!mJsFrameBuffer);
}
void ThreadRegistrationLockedRWOnThread::PollJSSampling() {
// We can't start/stop profiling until we have the thread's JSContext.
if (mCCJSContext) {
// It is possible for mJSSampling to go through the following sequences.
//
// - INACTIVE, ACTIVE_REQUESTED, INACTIVE_REQUESTED, INACTIVE
//
// - ACTIVE, INACTIVE_REQUESTED, ACTIVE_REQUESTED, ACTIVE
//
// Therefore, the if and else branches here aren't always interleaved.
// This is ok because the JS engine can handle that.
//
JSContext* cx = mCCJSContext->Context();
if (mJSSampling == ACTIVE_REQUESTED) {
mJSSampling = ACTIVE;
js::EnableContextProfilingStack(cx, true);
if (JSAllocationsEnabled()) {
// TODO - This probability should not be hardcoded. See Bug 1547284.
JS::EnableRecordingAllocations(cx, profiler_add_js_allocation_marker,
0.01);
}
js::RegisterContextProfilerMarkers(
cx, profiler_add_js_marker, profiler_add_js_interval,
profiler_add_js_flow, profiler_add_js_terminating_flow);
} else if (mJSSampling == INACTIVE_REQUESTED) {
mJSSampling = INACTIVE;
js::EnableContextProfilingStack(cx, false);
if (JSAllocationsEnabled()) {
JS::DisableRecordingAllocations(cx);
}
}
}
}
#ifdef NIGHTLY_BUILD
void ThreadRegistrationUnlockedConstReaderAndAtomicRW::RecordWakeCount() const {
baseprofiler::detail::BaseProfilerAutoLock lock(mRecordWakeCountMutex);
uint64_t newWakeCount = mWakeCount - mAlreadyRecordedWakeCount;
if (newWakeCount == 0 && mSleep != AWAKE) {
// If no new wake-up was counted, and the thread is not marked awake,
// we can be pretty sure there is no CPU activity to record.
// Threads that are never annotated as asleep/awake (typically rust threads)
// start as awake.
return;
}
uint64_t cpuTimeNs;
if (!GetCpuTimeSinceThreadStartInNs(&cpuTimeNs, PlatformDataCRef())) {
cpuTimeNs = 0;
}
constexpr uint64_t NS_PER_MS = 1'000'000;
uint64_t cpuTimeMs = cpuTimeNs / NS_PER_MS;
uint64_t newCpuTimeMs = MOZ_LIKELY(cpuTimeMs > mAlreadyRecordedCpuTimeInMs)
? cpuTimeMs - mAlreadyRecordedCpuTimeInMs
: 0;
if (!newWakeCount && !newCpuTimeMs) {
// Nothing to report, avoid computing the Glean friendly thread name.
return;
}
nsAutoCString threadName(mInfo.Name());
// Trim the trailing number of threads that are part of a thread pool.
for (size_t length = threadName.Length(); length > 0; --length) {
const char c = threadName.CharAt(length - 1);
if ((c < '0' || c > '9') && c != '#' && c != ' ') {
if (length != threadName.Length()) {
threadName.SetLength(length);
}
break;
}
}
mozilla::glean::RecordThreadCpuUse(threadName, newCpuTimeMs, newWakeCount);
// The thread id is provided as part of the payload because this call is
// inside a ThreadRegistration data function, which could be invoked with
// the ThreadRegistry locked. We cannot call any function/option that could
// attempt to lock the ThreadRegistry again, like MarkerThreadId.
PROFILER_MARKER("Thread CPU use", OTHER, {}, ThreadCpuUseMarker,
mInfo.ThreadId(), newCpuTimeMs, newWakeCount, threadName);
mAlreadyRecordedCpuTimeInMs = cpuTimeMs;
mAlreadyRecordedWakeCount += newWakeCount;
}
#endif
} // namespace mozilla::profiler
|