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
|
/*
* Copyright (C) 2016-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#if ENABLE(SAMPLING_PROFILER)
#include "CallFrame.h"
#include "CodeBlockHash.h"
#include "JITCode.h"
#include "MachineStackMarker.h"
#include "NativeCallee.h"
#include "PCToCodeOriginMap.h"
#include "WasmCompilationMode.h"
#include "WasmIndexOrName.h"
#include <wtf/Box.h>
#include <wtf/HashSet.h>
#include <wtf/Lock.h>
#include <wtf/Stopwatch.h>
#include <wtf/TZoneMalloc.h>
#include <wtf/Vector.h>
#include <wtf/WeakRandom.h>
namespace JSC {
class VM;
class ExecutableBase;
class SamplingProfiler : public ThreadSafeRefCounted<SamplingProfiler> {
WTF_MAKE_TZONE_ALLOCATED(SamplingProfiler);
public:
struct UnprocessedStackFrame {
UnprocessedStackFrame(CodeBlock* codeBlock, CalleeBits callee, CallSiteIndex callSiteIndex)
: unverifiedCallee(callee)
, verifiedCodeBlock(codeBlock)
, callSiteIndex(callSiteIndex)
{ }
UnprocessedStackFrame(const void* pc)
: cCodePC(pc)
{ }
UnprocessedStackFrame() = default;
const void* cCodePC { nullptr };
CalleeBits unverifiedCallee;
CodeBlock* verifiedCodeBlock { nullptr };
CallSiteIndex callSiteIndex;
NativeCallee::Category nativeCalleeCategory { NativeCallee::Category::InlineCache };
#if ENABLE(WEBASSEMBLY)
std::optional<Wasm::IndexOrName> wasmIndexOrName;
#endif
std::optional<Wasm::CompilationMode> wasmCompilationMode;
#if ENABLE(JIT)
Box<PCToCodeOriginMap> wasmPCMap;
#endif
};
enum class FrameType {
Executable,
Wasm,
Host,
RegExp,
C,
Unknown,
};
struct StackFrame {
StackFrame(ExecutableBase* executable)
: frameType(FrameType::Executable)
, executable(executable)
{ }
StackFrame()
{ }
FrameType frameType { FrameType::Unknown };
const void* cCodePC { nullptr };
ExecutableBase* executable { nullptr };
JSObject* callee { nullptr };
RegExp* regExp { nullptr };
#if ENABLE(WEBASSEMBLY)
std::optional<Wasm::IndexOrName> wasmIndexOrName;
#endif
std::optional<Wasm::CompilationMode> wasmCompilationMode;
BytecodeIndex wasmOffset;
struct CodeLocation {
bool hasCodeBlockHash() const
{
return codeBlockHash.isSet();
}
bool hasBytecodeIndex() const
{
return !!bytecodeIndex;
}
bool hasExpressionInfo() const
{
return lineColumn.line != std::numeric_limits<unsigned>::max()
&& lineColumn.column != std::numeric_limits<unsigned>::max();
}
// These attempt to be expression-level line and column number.
LineColumn lineColumn { std::numeric_limits<unsigned>::max(), std::numeric_limits<unsigned>::max() };
BytecodeIndex bytecodeIndex;
CodeBlockHash codeBlockHash;
JITType jitType { JITType::None };
bool isRegExp { false };
};
CodeLocation semanticLocation;
std::optional<std::pair<CodeLocation, CodeBlock*>> machineLocation; // This is non-null if we were inlined. It represents the machine frame we were inlined into.
bool hasExpressionInfo() const { return semanticLocation.hasExpressionInfo(); }
unsigned lineNumber() const
{
ASSERT(hasExpressionInfo());
return semanticLocation.lineColumn.line;
}
unsigned columnNumber() const
{
ASSERT(hasExpressionInfo());
return semanticLocation.lineColumn.column;
}
// These are function-level data.
String nameFromCallee(VM&);
String displayName(VM&);
int functionStartLine();
unsigned functionStartColumn();
std::tuple<SourceProvider*, SourceID> sourceProviderAndID();
String url();
};
struct UnprocessedStackTrace {
MonotonicTime timestamp;
Seconds stopwatchTimestamp;
void* topPC;
bool topFrameIsLLInt;
void* llintPC;
RegExp* regExp;
Vector<UnprocessedStackFrame> frames;
};
struct StackTrace {
MonotonicTime timestamp;
Seconds stopwatchTimestamp;
Vector<StackFrame> frames;
StackTrace()
{ }
StackTrace(StackTrace&& other)
: timestamp(other.timestamp)
, frames(WTFMove(other.frames))
{ }
};
SamplingProfiler(VM&, Ref<Stopwatch>&&);
JS_EXPORT_PRIVATE ~SamplingProfiler();
void noticeJSLockAcquisition();
void noticeVMEntry();
void shutdown();
template<typename Visitor> void visit(Visitor&) WTF_REQUIRES_LOCK(m_lock);
Lock& getLock() WTF_RETURNS_LOCK(m_lock) { return m_lock; }
void setTimingInterval(Seconds interval) { m_timingInterval = interval; }
JS_EXPORT_PRIVATE void start();
void startWithLock() WTF_REQUIRES_LOCK(m_lock);
Vector<StackTrace> releaseStackTraces() WTF_REQUIRES_LOCK(m_lock);
JS_EXPORT_PRIVATE Ref<JSON::Value> stackTracesAsJSON();
JS_EXPORT_PRIVATE void noticeCurrentThreadAsJSCExecutionThread();
void noticeCurrentThreadAsJSCExecutionThreadWithLock() WTF_REQUIRES_LOCK(m_lock);
void processUnverifiedStackTraces() WTF_REQUIRES_LOCK(m_lock);
void setStopWatch(Ref<Stopwatch>&& stopwatch) WTF_REQUIRES_LOCK(m_lock) { m_stopwatch = WTFMove(stopwatch); }
void pause() WTF_REQUIRES_LOCK(m_lock);
void clearData() WTF_REQUIRES_LOCK(m_lock);
// Used for debugging in the JSC shell/DRT.
void registerForReportAtExit();
void reportDataToOptionFile();
JS_EXPORT_PRIVATE void reportTopFunctions();
JS_EXPORT_PRIVATE void reportTopFunctions(PrintStream&);
JS_EXPORT_PRIVATE void reportTopBytecodes();
JS_EXPORT_PRIVATE void reportTopBytecodes(PrintStream&);
JS_EXPORT_PRIVATE Thread* thread() const;
private:
void createThreadIfNecessary() WTF_REQUIRES_LOCK(m_lock);
void timerLoop();
void takeSample(Seconds& stackTraceProcessingTime) WTF_REQUIRES_LOCK(m_lock);
Lock m_lock;
bool m_isPaused WTF_GUARDED_BY_LOCK(m_lock);
bool m_isShutDown WTF_GUARDED_BY_LOCK(m_lock);
bool m_needsReportAtExit { false };
VM& m_vm;
WeakRandom m_weakRandom;
Ref<Stopwatch> m_stopwatch WTF_GUARDED_BY_LOCK(m_lock);
Vector<StackTrace> m_stackTraces WTF_GUARDED_BY_LOCK(m_lock);
Vector<UnprocessedStackTrace> m_unprocessedStackTraces WTF_GUARDED_BY_LOCK(m_lock);
Seconds m_timingInterval;
RefPtr<Thread> m_thread;
RefPtr<Thread> m_jscExecutionThread WTF_GUARDED_BY_LOCK(m_lock);
UncheckedKeyHashSet<JSCell*> m_liveCellPointers WTF_GUARDED_BY_LOCK(m_lock);
Vector<UnprocessedStackFrame> m_currentFrames WTF_GUARDED_BY_LOCK(m_lock);
};
} // namespace JSC
namespace WTF {
void printInternal(PrintStream&, JSC::SamplingProfiler::FrameType);
} // namespace WTF
#endif // ENABLE(SAMPLING_PROFILER)
|