| 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 |
#include "ProfileBuffer.h" |
| 8 |
|
| 9 |
#include "mozilla/MathAlgorithms.h" |
| 10 |
|
| 11 |
#include "BaseProfiler.h" |
| 12 |
|
| 13 |
namespace mozilla { |
| 14 |
namespace baseprofiler { |
| 15 |
|
| 16 |
ProfileBuffer::ProfileBuffer(ProfileChunkedBuffer& aBuffer) |
| 17 |
: mEntries(aBuffer) { |
| 18 |
// Assume the given buffer is in-session. |
| 19 |
MOZ_ASSERT(mEntries.IsInSession()); |
| 20 |
} |
| 21 |
|
| 22 |
/* static */ |
| 23 |
ProfileBufferBlockIndex ProfileBuffer::AddEntry( |
| 24 |
ProfileChunkedBuffer& aProfileChunkedBuffer, |
| 25 |
const ProfileBufferEntry& aEntry) { |
| 26 |
switch (aEntry.GetKind()) { |
| 27 |
#define SWITCH_KIND(KIND, TYPE, SIZE) \ |
| 28 |
case ProfileBufferEntry::Kind::KIND: { \ |
| 29 |
return aProfileChunkedBuffer.PutFrom(&aEntry, 1 + (SIZE)); \ |
| 30 |
break; \ |
| 31 |
} |
| 32 |
|
| 33 |
FOR_EACH_PROFILE_BUFFER_ENTRY_KIND(SWITCH_KIND) |
| 34 |
|
| 35 |
#undef SWITCH_KIND |
| 36 |
default: |
| 37 |
MOZ_ASSERT(false, "Unhandled baseprofiler::ProfilerBuffer entry KIND"); |
| 38 |
return ProfileBufferBlockIndex{}; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// Called from signal, call only reentrant functions |
| 43 |
uint64_t ProfileBuffer::AddEntry(const ProfileBufferEntry& aEntry) { |
| 44 |
return AddEntry(mEntries, aEntry).ConvertToProfileBufferIndex(); |
| 45 |
} |
| 46 |
|
| 47 |
/* static */ |
| 48 |
ProfileBufferBlockIndex ProfileBuffer::AddThreadIdEntry( |
| 49 |
ProfileChunkedBuffer& aProfileChunkedBuffer, |
| 50 |
BaseProfilerThreadId aThreadId) { |
| 51 |
return AddEntry(aProfileChunkedBuffer, |
| 52 |
ProfileBufferEntry::ThreadId(aThreadId)); |
| 53 |
} |
| 54 |
|
| 55 |
uint64_t ProfileBuffer::AddThreadIdEntry(BaseProfilerThreadId aThreadId) { |
| 56 |
return AddThreadIdEntry(mEntries, aThreadId).ConvertToProfileBufferIndex(); |
| 57 |
} |
| 58 |
|
| 59 |
void ProfileBuffer::CollectCodeLocation( |
| 60 |
const char* aLabel, const char* aStr, uint32_t aFrameFlags, |
| 61 |
uint64_t aInnerWindowID, const Maybe<uint32_t>& aLineNumber, |
| 62 |
const Maybe<uint32_t>& aColumnNumber, |
| 63 |
const Maybe<ProfilingCategoryPair>& aCategoryPair) { |
| 64 |
AddEntry(ProfileBufferEntry::Label(aLabel)); |
| 65 |
AddEntry(ProfileBufferEntry::FrameFlags(uint64_t(aFrameFlags))); |
| 66 |
|
| 67 |
if (aStr) { |
| 68 |
// Store the string using one or more DynamicStringFragment entries. |
| 69 |
size_t strLen = strlen(aStr) + 1; // +1 for the null terminator |
| 70 |
// If larger than the prescribed limit, we will cut the string and end it |
| 71 |
// with an ellipsis. |
| 72 |
const bool tooBig = strLen > kMaxFrameKeyLength; |
| 73 |
if (tooBig) { |
| 74 |
strLen = kMaxFrameKeyLength; |
| 75 |
} |
| 76 |
char chars[ProfileBufferEntry::kNumChars]; |
| 77 |
for (size_t j = 0;; j += ProfileBufferEntry::kNumChars) { |
| 78 |
// Store up to kNumChars characters in the entry. |
| 79 |
size_t len = ProfileBufferEntry::kNumChars; |
| 80 |
const bool last = j + len >= strLen; |
| 81 |
if (last) { |
| 82 |
// Only the last entry may be smaller than kNumChars. |
| 83 |
len = strLen - j; |
| 84 |
if (tooBig) { |
| 85 |
// That last entry is part of a too-big string, replace the end |
| 86 |
// characters with an ellipsis "...". |
| 87 |
len = std::max(len, size_t(4)); |
| 88 |
chars[len - 4] = '.'; |
| 89 |
chars[len - 3] = '.'; |
| 90 |
chars[len - 2] = '.'; |
| 91 |
chars[len - 1] = '\0'; |
| 92 |
// Make sure the memcpy will not overwrite our ellipsis! |
| 93 |
len -= 4; |
| 94 |
} |
| 95 |
} |
| 96 |
memcpy(chars, &aStr[j], len); |
| 97 |
AddEntry(ProfileBufferEntry::DynamicStringFragment(chars)); |
| 98 |
if (last) { |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if (aInnerWindowID) { |
| 105 |
AddEntry(ProfileBufferEntry::InnerWindowID(aInnerWindowID)); |
| 106 |
} |
| 107 |
|
| 108 |
if (aLineNumber) { |
| 109 |
AddEntry(ProfileBufferEntry::LineNumber(*aLineNumber)); |
| 110 |
} |
| 111 |
|
| 112 |
if (aColumnNumber) { |
| 113 |
AddEntry(ProfileBufferEntry::ColumnNumber(*aColumnNumber)); |
| 114 |
} |
| 115 |
|
| 116 |
if (aCategoryPair.isSome()) { |
| 117 |
AddEntry(ProfileBufferEntry::CategoryPair(int(*aCategoryPair))); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
size_t ProfileBuffer::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const { |
| 122 |
// Measurement of the following members may be added later if DMD finds it |
| 123 |
// is worthwhile: |
| 124 |
// - memory pointed to by the elements within mEntries |
| 125 |
return mEntries.SizeOfExcludingThis(aMallocSizeOf); |
| 126 |
} |
| 127 |
|
| 128 |
size_t ProfileBuffer::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const { |
| 129 |
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
| 130 |
} |
| 131 |
|
| 132 |
void ProfileBuffer::CollectOverheadStats(TimeDuration aSamplingTime, |
| 133 |
TimeDuration aLocking, |
| 134 |
TimeDuration aCleaning, |
| 135 |
TimeDuration aCounters, |
| 136 |
TimeDuration aThreads) { |
| 137 |
double timeUs = aSamplingTime.ToMilliseconds() * 1000.0; |
| 138 |
if (mFirstSamplingTimeUs == 0.0) { |
| 139 |
mFirstSamplingTimeUs = timeUs; |
| 140 |
} else { |
| 141 |
// Note that we'll have 1 fewer interval than other numbers (because |
| 142 |
// we need both ends of an interval to know its duration). The final |
| 143 |
// difference should be insignificant over the expected many thousands |
| 144 |
// of iterations. |
| 145 |
mIntervalsUs.Count(timeUs - mLastSamplingTimeUs); |
| 146 |
} |
| 147 |
mLastSamplingTimeUs = timeUs; |
| 148 |
// Time to take the lock before sampling. |
| 149 |
double lockingUs = aLocking.ToMilliseconds() * 1000.0; |
| 150 |
// Time to discard expired data. |
| 151 |
double cleaningUs = aCleaning.ToMilliseconds() * 1000.0; |
| 152 |
// Time to gather all counters. |
| 153 |
double countersUs = aCounters.ToMilliseconds() * 1000.0; |
| 154 |
// Time to sample all threads. |
| 155 |
double threadsUs = aThreads.ToMilliseconds() * 1000.0; |
| 156 |
|
| 157 |
// Add to our gathered stats. |
| 158 |
mOverheadsUs.Count(lockingUs + cleaningUs + countersUs + threadsUs); |
| 159 |
mLockingsUs.Count(lockingUs); |
| 160 |
mCleaningsUs.Count(cleaningUs); |
| 161 |
mCountersUs.Count(countersUs); |
| 162 |
mThreadsUs.Count(threadsUs); |
| 163 |
|
| 164 |
// Record details in buffer, if requested. |
| 165 |
static const bool sRecordSamplingOverhead = []() { |
| 166 |
const char* recordOverheads = getenv("MOZ_PROFILER_RECORD_OVERHEADS"); |
| 167 |
return recordOverheads && recordOverheads[0] != '\0'; |
| 168 |
}(); |
| 169 |
if (sRecordSamplingOverhead) { |
| 170 |
AddEntry(ProfileBufferEntry::ProfilerOverheadTime(timeUs)); |
| 171 |
AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(lockingUs)); |
| 172 |
AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(cleaningUs)); |
| 173 |
AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(countersUs)); |
| 174 |
AddEntry(ProfileBufferEntry::ProfilerOverheadDuration(threadsUs)); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
ProfilerBufferInfo ProfileBuffer::GetProfilerBufferInfo() const { |
| 179 |
return {BufferRangeStart(), |
| 180 |
BufferRangeEnd(), |
| 181 |
static_cast<uint32_t>(*mEntries.BufferLength() / |
| 182 |
8), // 8 bytes per entry. |
| 183 |
mIntervalsUs, |
| 184 |
mOverheadsUs, |
| 185 |
mLockingsUs, |
| 186 |
mCleaningsUs, |
| 187 |
mCountersUs, |
| 188 |
mThreadsUs}; |
| 189 |
} |
| 190 |
|
| 191 |
/* ProfileBufferCollector */ |
| 192 |
|
| 193 |
void ProfileBufferCollector::CollectNativeLeafAddr(void* aAddr) { |
| 194 |
mBuf.AddEntry(ProfileBufferEntry::NativeLeafAddr(aAddr)); |
| 195 |
} |
| 196 |
|
| 197 |
void ProfileBufferCollector::CollectProfilingStackFrame( |
| 198 |
const ProfilingStackFrame& aFrame) { |
| 199 |
// WARNING: this function runs within the profiler's "critical section". |
| 200 |
|
| 201 |
MOZ_ASSERT(aFrame.isLabelFrame() || |
| 202 |
(aFrame.isJsFrame() && !aFrame.isOSRFrame())); |
| 203 |
|
| 204 |
const char* label = aFrame.label(); |
| 205 |
const char* dynamicString = aFrame.dynamicString(); |
| 206 |
Maybe<uint32_t> line; |
| 207 |
Maybe<uint32_t> column; |
| 208 |
|
| 209 |
MOZ_ASSERT(aFrame.isLabelFrame()); |
| 210 |
|
| 211 |
mBuf.CollectCodeLocation(label, dynamicString, aFrame.flags(), |
| 212 |
aFrame.realmID(), line, column, |
| 213 |
Some(aFrame.categoryPair())); |
| 214 |
} |
| 215 |
|
| 216 |
} // namespace baseprofiler |
| 217 |
} // namespace mozilla |