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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
/* -*- Mode: C++; tab-width: 2; 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 "ProfiledThreadData.h"
#include "platform.h"
#include "ProfileBuffer.h"
#include "mozilla/OriginAttributes.h"
#include "mozilla/Span.h"
#include "nsXULAppAPI.h"
#if defined(GP_OS_darwin)
# include <pthread.h>
#endif
using namespace mozilla::literals::ProportionValue_literals;
ProfiledThreadData::ProfiledThreadData(
const mozilla::profiler::ThreadRegistrationInfo& aThreadInfo)
: mThreadInfo(aThreadInfo.Name(), aThreadInfo.ThreadId(),
aThreadInfo.IsMainThread(), aThreadInfo.RegisterTime()) {
MOZ_COUNT_CTOR(ProfiledThreadData);
}
ProfiledThreadData::ProfiledThreadData(
mozilla::profiler::ThreadRegistrationInfo&& aThreadInfo)
: mThreadInfo(std::move(aThreadInfo)) {
MOZ_COUNT_CTOR(ProfiledThreadData);
}
ProfiledThreadData::~ProfiledThreadData() {
MOZ_COUNT_DTOR(ProfiledThreadData);
}
static void StreamTables(UniqueStacks&& aUniqueStacks, JSContext* aCx,
SpliceableJSONWriter& aWriter,
SpliceableChunkedJSONWriter* aShapesWriter,
const mozilla::TimeStamp& aProcessStartTime,
mozilla::ProgressLogger aProgressLogger) {
aWriter.StartObjectProperty("stackTable");
{
{
JSONSchemaWriter schema(aWriter);
schema.WriteField("prefix");
schema.WriteField("frame");
}
aWriter.StartArrayProperty("data");
{
aProgressLogger.SetLocalProgress(1_pc, "Splicing stack table...");
aUniqueStacks.SpliceStackTableElements(aWriter);
aProgressLogger.SetLocalProgress(30_pc, "Spliced stack table");
}
aWriter.EndArray();
}
aWriter.EndObject();
aWriter.StartObjectProperty("frameTable");
{
{
JSONSchemaWriter schema(aWriter);
schema.WriteField("location");
schema.WriteField("relevantForJS");
schema.WriteField("innerWindowID");
schema.WriteField("implementation");
schema.WriteField("line");
schema.WriteField("column");
schema.WriteField("category");
schema.WriteField("subcategory");
}
aWriter.StartArrayProperty("data");
{
aProgressLogger.SetLocalProgress(30_pc, "Splicing frame table...");
aUniqueStacks.SpliceFrameTableElements(aWriter);
aProgressLogger.SetLocalProgress(60_pc, "Spliced frame table");
}
aWriter.EndArray();
}
aWriter.EndObject();
aWriter.StartArrayProperty("stringTable");
{
aProgressLogger.SetLocalProgress(60_pc, "Splicing string table...");
std::move(aUniqueStacks.UniqueStrings()).SpliceStringTableElements(aWriter);
aProgressLogger.SetLocalProgress(90_pc, "Spliced string table");
}
aWriter.EndArray();
if (aShapesWriter) {
aWriter.StartArrayProperty("tracedObjectShapes");
aWriter.TakeAndSplice(aShapesWriter->TakeChunkedWriteFunc());
aWriter.EndArray();
}
aWriter.StringProperty("tracedValues", aUniqueStacks.TracedValues());
}
mozilla::NotNull<mozilla::UniquePtr<UniqueStacks>>
ProfiledThreadData::PrepareUniqueStacks(
const ProfileBuffer& aBuffer, JSContext* aCx,
mozilla::FailureLatch& aFailureLatch, ProfilerCodeAddressService* aService,
mozilla::ProgressLogger aProgressLogger,
const nsTHashMap<SourceId, IndexIntoSourceTable>* aSourceIdToIndexMap) {
if (mJITFrameInfoForPreviousJSContexts &&
mJITFrameInfoForPreviousJSContexts->HasExpired(
aBuffer.BufferRangeStart())) {
mJITFrameInfoForPreviousJSContexts = nullptr;
}
aProgressLogger.SetLocalProgress(1_pc, "Checked JIT frame info presence");
// If we have an existing JITFrameInfo in mJITFrameInfoForPreviousJSContexts,
// copy the data from it.
JITFrameInfo jitFrameInfo =
mJITFrameInfoForPreviousJSContexts
? JITFrameInfo(*mJITFrameInfoForPreviousJSContexts,
aProgressLogger.CreateSubLoggerTo(
"Retrieving JIT frame info...", 10_pc,
"Retrieved JIT frame info"))
: JITFrameInfo();
if (aCx && mBufferPositionWhenReceivedJSContext) {
aBuffer.AddJITInfoForRange(
*mBufferPositionWhenReceivedJSContext, mThreadInfo.ThreadId(), aCx,
jitFrameInfo,
aProgressLogger.CreateSubLoggerTo("Adding JIT info...", 90_pc,
"Added JIT info"),
aSourceIdToIndexMap);
} else {
aProgressLogger.SetLocalProgress(90_pc, "No JIT info");
}
return mozilla::MakeNotNull<mozilla::UniquePtr<UniqueStacks>>(
aFailureLatch, std::move(jitFrameInfo), aService, aSourceIdToIndexMap);
}
void ProfiledThreadData::StreamJSON(
const ProfileBuffer& aBuffer, JSContext* aCx, SpliceableJSONWriter& aWriter,
const nsACString& aProcessName, const nsACString& aETLDplus1,
const mozilla::TimeStamp& aProcessStartTime, double aSinceTime,
ProfilerCodeAddressService* aService,
mozilla::ProgressLogger aProgressLogger) {
mozilla::NotNull<mozilla::UniquePtr<UniqueStacks>> uniqueStacks =
PrepareUniqueStacks(aBuffer, aCx, aWriter.SourceFailureLatch(), aService,
aProgressLogger.CreateSubLoggerFromTo(
0_pc, "Preparing unique stacks...", 10_pc,
"Prepared Unique stacks"));
aWriter.SetUniqueStrings(uniqueStacks->UniqueStrings());
aWriter.Start();
{
StreamSamplesAndMarkers(
mThreadInfo.Name(), mThreadInfo.ThreadId(), aBuffer, aWriter,
aProcessName, aETLDplus1, aProcessStartTime, mThreadInfo.RegisterTime(),
mUnregisterTime, aSinceTime, *uniqueStacks,
aProgressLogger.CreateSubLoggerTo(
90_pc,
"ProfiledThreadData::StreamJSON: Streamed samples and markers"));
StreamTables(std::move(*uniqueStacks), aCx, aWriter, nullptr,
aProcessStartTime,
aProgressLogger.CreateSubLoggerTo(
99_pc, "Streamed tables and trace logger"));
}
aWriter.End();
aWriter.ResetUniqueStrings();
}
void ProfiledThreadData::StreamJSON(
ThreadStreamingContext&& aThreadStreamingContext,
SpliceableJSONWriter& aWriter, const nsACString& aProcessName,
const nsACString& aETLDplus1, const mozilla::TimeStamp& aProcessStartTime,
ProfilerCodeAddressService* aService,
mozilla::ProgressLogger aProgressLogger) {
aWriter.Start();
{
StreamSamplesAndMarkers(
mThreadInfo.Name(), aThreadStreamingContext, aWriter, aProcessName,
aETLDplus1, aProcessStartTime, mThreadInfo.RegisterTime(),
mUnregisterTime,
aProgressLogger.CreateSubLoggerFromTo(
1_pc, "ProfiledThreadData::StreamJSON(context): Streaming...",
90_pc,
"ProfiledThreadData::StreamJSON(context): Streamed samples and "
"markers"));
StreamTables(
std::move(*aThreadStreamingContext.mUniqueStacks),
aThreadStreamingContext.mJSContext, aWriter,
&aThreadStreamingContext.mShapesDataWriter, aProcessStartTime,
aProgressLogger.CreateSubLoggerTo(
"ProfiledThreadData::StreamJSON(context): Streaming tables...",
99_pc, "ProfiledThreadData::StreamJSON(context): Streamed tables"));
}
aWriter.End();
}
// StreamSamplesDataCallback: (ProgressLogger) -> ProfilerThreadId
// StreamMarkersDataCallback: (ProgressLogger) -> void
// Returns the ProfilerThreadId returned by StreamSamplesDataCallback, which
// should be the thread id of the last sample that was processed (if any;
// otherwise it is left unspecified). This is mostly useful when the caller
// doesn't know where the sample comes from, e.g., when it's a backtrace in a
// marker.
template <typename StreamSamplesDataCallback,
typename StreamMarkersDataCallback>
ProfilerThreadId DoStreamSamplesAndMarkers(
const char* aName, SpliceableJSONWriter& aWriter,
const nsACString& aProcessName, const nsACString& aETLDplus1,
const mozilla::TimeStamp& aProcessStartTime,
const mozilla::TimeStamp& aRegisterTime,
const mozilla::TimeStamp& aUnregisterTime,
mozilla::ProgressLogger aProgressLogger,
StreamSamplesDataCallback&& aStreamSamplesDataCallback,
StreamMarkersDataCallback&& aStreamMarkersDataCallback) {
ProfilerThreadId processedThreadId;
aWriter.StringProperty("processType",
mozilla::MakeStringSpan(XRE_GetProcessTypeString()));
aWriter.StringProperty("name", mozilla::MakeStringSpan(aName));
// Use given process name (if any), unless we're the parent process.
if (XRE_IsParentProcess()) {
aWriter.StringProperty("processName", "Parent Process");
} else if (!aProcessName.IsEmpty()) {
aWriter.StringProperty("processName", aProcessName);
}
if (!aETLDplus1.IsEmpty()) {
nsAutoCString originNoSuffix;
mozilla::OriginAttributes attrs;
if (!attrs.PopulateFromOrigin(aETLDplus1, originNoSuffix)) {
aWriter.StringProperty("eTLD+1", aETLDplus1);
} else {
aWriter.StringProperty("eTLD+1", originNoSuffix);
aWriter.BoolProperty("isPrivateBrowsing", attrs.IsPrivateBrowsing());
aWriter.IntProperty("userContextId", attrs.mUserContextId);
}
}
if (aRegisterTime) {
aWriter.DoubleProperty(
"registerTime", (aRegisterTime - aProcessStartTime).ToMilliseconds());
} else {
aWriter.NullProperty("registerTime");
}
if (aUnregisterTime) {
aWriter.DoubleProperty(
"unregisterTime",
(aUnregisterTime - aProcessStartTime).ToMilliseconds());
} else {
aWriter.NullProperty("unregisterTime");
}
aWriter.StartObjectProperty("samples");
{
{
JSONSchemaWriter schema(aWriter);
schema.WriteField("stack");
schema.WriteField("time");
schema.WriteField("eventDelay");
schema.WriteField("argumentValues");
#define RUNNING_TIME_FIELD(index, name, unit, jsonProperty) \
schema.WriteField(#jsonProperty);
PROFILER_FOR_EACH_RUNNING_TIME(RUNNING_TIME_FIELD)
#undef RUNNING_TIME_FIELD
}
aWriter.StartArrayProperty("data");
{
processedThreadId = std::forward<StreamSamplesDataCallback>(
aStreamSamplesDataCallback)(aProgressLogger.CreateSubLoggerFromTo(
1_pc, "Streaming samples...", 49_pc, "Streamed samples"));
}
aWriter.EndArray();
}
aWriter.EndObject();
aWriter.StartObjectProperty("markers");
{
{
JSONSchemaWriter schema(aWriter);
schema.WriteField("name");
schema.WriteField("startTime");
schema.WriteField("endTime");
schema.WriteField("phase");
schema.WriteField("category");
schema.WriteField("data");
}
aWriter.StartArrayProperty("data");
{
std::forward<StreamMarkersDataCallback>(aStreamMarkersDataCallback)(
aProgressLogger.CreateSubLoggerFromTo(50_pc, "Streaming markers...",
99_pc, "Streamed markers"));
}
aWriter.EndArray();
}
aWriter.EndObject();
// Tech note: If `ToNumber()` returns a uint64_t, the conversion to int64_t is
// "implementation-defined" before C++20. This is acceptable here, because
// this is a one-way conversion to a unique identifier that's used to visually
// separate data by thread on the front-end.
aWriter.IntProperty(
"pid", static_cast<int64_t>(profiler_current_process_id().ToNumber()));
aWriter.IntProperty("tid",
static_cast<int64_t>(processedThreadId.ToNumber()));
return processedThreadId;
}
ProfilerThreadId StreamSamplesAndMarkers(
const char* aName, ProfilerThreadId aThreadId, const ProfileBuffer& aBuffer,
SpliceableJSONWriter& aWriter, const nsACString& aProcessName,
const nsACString& aETLDplus1, const mozilla::TimeStamp& aProcessStartTime,
const mozilla::TimeStamp& aRegisterTime,
const mozilla::TimeStamp& aUnregisterTime, double aSinceTime,
UniqueStacks& aUniqueStacks, mozilla::ProgressLogger aProgressLogger) {
return DoStreamSamplesAndMarkers(
aName, aWriter, aProcessName, aETLDplus1, aProcessStartTime,
aRegisterTime, aUnregisterTime, std::move(aProgressLogger),
[&](mozilla::ProgressLogger aSubProgressLogger) {
ProfilerThreadId processedThreadId = aBuffer.StreamSamplesToJSON(
aWriter, aThreadId, aSinceTime, aUniqueStacks,
std::move(aSubProgressLogger));
return aThreadId.IsSpecified() ? aThreadId : processedThreadId;
},
[&](mozilla::ProgressLogger aSubProgressLogger) {
aBuffer.StreamMarkersToJSON(aWriter, aThreadId, aProcessStartTime,
aSinceTime, aUniqueStacks,
std::move(aSubProgressLogger));
});
}
void StreamSamplesAndMarkers(const char* aName,
ThreadStreamingContext& aThreadData,
SpliceableJSONWriter& aWriter,
const nsACString& aProcessName,
const nsACString& aETLDplus1,
const mozilla::TimeStamp& aProcessStartTime,
const mozilla::TimeStamp& aRegisterTime,
const mozilla::TimeStamp& aUnregisterTime,
mozilla::ProgressLogger aProgressLogger) {
(void)DoStreamSamplesAndMarkers(
aName, aWriter, aProcessName, aETLDplus1, aProcessStartTime,
aRegisterTime, aUnregisterTime, std::move(aProgressLogger),
[&](mozilla::ProgressLogger aSubProgressLogger) {
aWriter.TakeAndSplice(
aThreadData.mSamplesDataWriter.TakeChunkedWriteFunc());
return aThreadData.mProfiledThreadData.Info().ThreadId();
},
[&](mozilla::ProgressLogger aSubProgressLogger) {
aWriter.TakeAndSplice(
aThreadData.mMarkersDataWriter.TakeChunkedWriteFunc());
});
}
void ProfiledThreadData::NotifyAboutToLoseJSContext(
JSContext* aContext, const mozilla::TimeStamp& aProcessStartTime,
ProfileBuffer& aBuffer) {
if (!mBufferPositionWhenReceivedJSContext) {
return;
}
MOZ_RELEASE_ASSERT(aContext);
if (mJITFrameInfoForPreviousJSContexts &&
mJITFrameInfoForPreviousJSContexts->HasExpired(
aBuffer.BufferRangeStart())) {
mJITFrameInfoForPreviousJSContexts = nullptr;
}
mozilla::UniquePtr<JITFrameInfo> jitFrameInfo =
mJITFrameInfoForPreviousJSContexts
? std::move(mJITFrameInfoForPreviousJSContexts)
: mozilla::MakeUnique<JITFrameInfo>();
aBuffer.AddJITInfoForRange(*mBufferPositionWhenReceivedJSContext,
mThreadInfo.ThreadId(), aContext, *jitFrameInfo,
mozilla::ProgressLogger{});
mJITFrameInfoForPreviousJSContexts = std::move(jitFrameInfo);
mBufferPositionWhenReceivedJSContext = mozilla::Nothing();
}
ThreadStreamingContext::ThreadStreamingContext(
ProfiledThreadData& aProfiledThreadData, const ProfileBuffer& aBuffer,
JSContext* aCx, mozilla::FailureLatch& aFailureLatch,
ProfilerCodeAddressService* aService,
mozilla::ProgressLogger aProgressLogger,
const nsTHashMap<SourceId, IndexIntoSourceTable>* aSourceIdToIndexMap)
: mProfiledThreadData(aProfiledThreadData),
mJSContext(aCx),
mSamplesDataWriter(aFailureLatch),
mMarkersDataWriter(aFailureLatch),
mShapesDataWriter(aFailureLatch),
mUniqueStacks(mProfiledThreadData.PrepareUniqueStacks(
aBuffer, aCx, aFailureLatch, aService,
aProgressLogger.CreateSubLoggerFromTo(
0_pc, "Preparing thread streaming context unique stacks...",
99_pc, "Prepared thread streaming context Unique stacks"),
aSourceIdToIndexMap)) {
if (aFailureLatch.Failed()) {
return;
}
mSamplesDataWriter.SetUniqueStrings(mUniqueStacks->UniqueStrings());
mSamplesDataWriter.StartBareList();
mMarkersDataWriter.SetUniqueStrings(mUniqueStacks->UniqueStrings());
mMarkersDataWriter.StartBareList();
mShapesDataWriter.StartBareList();
}
void ThreadStreamingContext::FinalizeWriter() {
mSamplesDataWriter.EndBareList();
mMarkersDataWriter.EndBareList();
mShapesDataWriter.EndBareList();
}
ProcessStreamingContext::ProcessStreamingContext(
size_t aThreadCount, mozilla::FailureLatch& aFailureLatch,
const mozilla::TimeStamp& aProcessStartTime, double aSinceTime)
: mFailureLatch(aFailureLatch),
mProcessStartTime(aProcessStartTime),
mSinceTime(aSinceTime) {
if (mFailureLatch.Failed()) {
return;
}
if (!mTIDList.initCapacity(aThreadCount)) {
mFailureLatch.SetFailure(
"OOM in ProcessStreamingContext allocating TID list");
return;
}
if (!mThreadStreamingContextList.initCapacity(aThreadCount)) {
mFailureLatch.SetFailure(
"OOM in ProcessStreamingContext allocating context list");
mTIDList.clear();
return;
}
}
ProcessStreamingContext::~ProcessStreamingContext() {
if (mFailureLatch.Failed()) {
return;
}
MOZ_ASSERT(mTIDList.length() == mThreadStreamingContextList.length());
MOZ_ASSERT(mTIDList.length() == mTIDList.capacity(),
"Didn't pre-allocate exactly right");
}
void ProcessStreamingContext::AddThreadStreamingContext(
ProfiledThreadData& aProfiledThreadData, const ProfileBuffer& aBuffer,
JSContext* aCx, ProfilerCodeAddressService* aService,
mozilla::ProgressLogger aProgressLogger,
const nsTHashMap<SourceId, IndexIntoSourceTable>* aSourceIdToIndexMap) {
if (mFailureLatch.Failed()) {
return;
}
MOZ_ASSERT(mTIDList.length() == mThreadStreamingContextList.length());
MOZ_ASSERT(mTIDList.length() < mTIDList.capacity(),
"Didn't pre-allocate enough");
mTIDList.infallibleAppend(aProfiledThreadData.Info().ThreadId());
mThreadStreamingContextList.infallibleEmplaceBack(
aProfiledThreadData, aBuffer, aCx, mFailureLatch, aService,
aProgressLogger.CreateSubLoggerFromTo(
1_pc, "Prepared streaming thread id", 100_pc,
"Added thread streaming context"),
aSourceIdToIndexMap);
}
|