| 1 |
/* -*- Mode: C++; tab-width: 8; 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 jit_PerfSpewer_h |
| 8 |
#define jit_PerfSpewer_h |
| 9 |
|
| 10 |
#ifdef JS_ION_PERF |
| 11 |
# include <stdio.h> |
| 12 |
#endif |
| 13 |
#include "js/AllocPolicy.h" |
| 14 |
#include "js/Vector.h" |
| 15 |
|
| 16 |
class JSScript; |
| 17 |
enum class JSOp : uint8_t; |
| 18 |
|
| 19 |
namespace js { |
| 20 |
|
| 21 |
namespace wasm { |
| 22 |
struct OpBytes; |
| 23 |
} |
| 24 |
|
| 25 |
namespace jit { |
| 26 |
|
| 27 |
class JitCode; |
| 28 |
class CompilerFrameInfo; |
| 29 |
class MacroAssembler; |
| 30 |
class MBasicBlock; |
| 31 |
class LInstruction; |
| 32 |
enum class CacheOp : uint16_t; |
| 33 |
|
| 34 |
struct AutoLockPerfSpewer { |
| 35 |
AutoLockPerfSpewer(); |
| 36 |
~AutoLockPerfSpewer(); |
| 37 |
}; |
| 38 |
|
| 39 |
bool PerfEnabled(); |
| 40 |
|
| 41 |
class PerfSpewer { |
| 42 |
protected: |
| 43 |
struct OpcodeEntry { |
| 44 |
uint32_t offset = 0; |
| 45 |
uint32_t opcode = 0; |
| 46 |
jsbytecode* bytecodepc = nullptr; |
| 47 |
|
| 48 |
// This string is used to replace the opcode, to define things like |
| 49 |
// Prologue/Epilogue, or to add operand info. |
| 50 |
JS::UniqueChars str; |
| 51 |
|
| 52 |
explicit OpcodeEntry(uint32_t offset_, uint32_t opcode_, |
| 53 |
JS::UniqueChars& str_, jsbytecode* pc) |
| 54 |
: offset(offset_), opcode(opcode_), bytecodepc(pc) { |
| 55 |
str = std::move(str_); |
| 56 |
} |
| 57 |
|
| 58 |
explicit OpcodeEntry(uint32_t offset_, uint32_t opcode_, |
| 59 |
JS::UniqueChars& str_) |
| 60 |
: offset(offset_), opcode(opcode_) { |
| 61 |
str = std::move(str_); |
| 62 |
} |
| 63 |
explicit OpcodeEntry(uint32_t offset_, JS::UniqueChars& str_) |
| 64 |
: offset(offset_) { |
| 65 |
str = std::move(str_); |
| 66 |
} |
| 67 |
explicit OpcodeEntry(uint32_t offset_, uint32_t opcode_) |
| 68 |
: offset(offset_), opcode(opcode_) {} |
| 69 |
|
| 70 |
explicit OpcodeEntry(jsbytecode* pc) : bytecodepc(pc) {} |
| 71 |
|
| 72 |
OpcodeEntry(OpcodeEntry&& copy) { |
| 73 |
offset = copy.offset; |
| 74 |
opcode = copy.opcode; |
| 75 |
bytecodepc = copy.bytecodepc; |
| 76 |
str = std::move(copy.str); |
| 77 |
} |
| 78 |
|
| 79 |
// Do not copy the UniqueChars member. |
| 80 |
OpcodeEntry(OpcodeEntry& copy) = delete; |
| 81 |
}; |
| 82 |
Vector<OpcodeEntry, 0, SystemAllocPolicy> opcodes_; |
| 83 |
uint32_t startOffset_ = 0; |
| 84 |
|
| 85 |
virtual const char* CodeName(uint32_t op) = 0; |
| 86 |
|
| 87 |
virtual void saveJitCodeSourceInfo(JSScript* script, JitCode* code, |
| 88 |
AutoLockPerfSpewer& lock); |
| 89 |
|
| 90 |
void saveJitCodeDebugInfo(JSScript* script, JitCode* code, |
| 91 |
AutoLockPerfSpewer& lock); |
| 92 |
void saveWasmCodeDebugInfo(uintptr_t codeBase, AutoLockPerfSpewer& lock); |
| 93 |
|
| 94 |
void saveJSProfile(JitCode* code, JS::UniqueChars& desc, JSScript* script); |
| 95 |
void saveWasmProfile(uintptr_t codeBase, size_t codeSize, |
| 96 |
JS::UniqueChars& desc); |
| 97 |
|
| 98 |
void saveIRInfo(uintptr_t codeBase, AutoLockPerfSpewer& lock); |
| 99 |
|
| 100 |
public: |
| 101 |
PerfSpewer() = default; |
| 102 |
PerfSpewer(PerfSpewer&&) = default; |
| 103 |
PerfSpewer& operator=(PerfSpewer&&) = default; |
| 104 |
|
| 105 |
void markStartOffset(uint32_t offset) { startOffset_ = offset; } |
| 106 |
void recordOffset(MacroAssembler& masm, const char*); |
| 107 |
|
| 108 |
static void Init(); |
| 109 |
|
| 110 |
static void CollectJitCodeInfo(JS::UniqueChars& function_name, JitCode* code, |
| 111 |
AutoLockPerfSpewer& lock); |
| 112 |
static void CollectJitCodeInfo(JS::UniqueChars& function_name, |
| 113 |
void* code_addr, uint64_t code_size, |
| 114 |
AutoLockPerfSpewer& lock); |
| 115 |
}; |
| 116 |
|
| 117 |
void CollectPerfSpewerJitCodeProfile(JitCode* code, const char* msg); |
| 118 |
void CollectPerfSpewerJitCodeProfile(uintptr_t base, uint64_t size, |
| 119 |
const char* msg); |
| 120 |
|
| 121 |
void CollectPerfSpewerWasmMap(uintptr_t base, uintptr_t size, |
| 122 |
JS::UniqueChars&& desc); |
| 123 |
|
| 124 |
class IonPerfSpewer : public PerfSpewer { |
| 125 |
const char* CodeName(uint32_t op) override; |
| 126 |
|
| 127 |
public: |
| 128 |
IonPerfSpewer() = default; |
| 129 |
IonPerfSpewer(IonPerfSpewer&&) = default; |
| 130 |
IonPerfSpewer& operator=(IonPerfSpewer&&) = default; |
| 131 |
|
| 132 |
void recordInstruction(MacroAssembler& masm, LInstruction* ins); |
| 133 |
void saveJSProfile(JSContext* cx, JSScript* script, JitCode* code); |
| 134 |
void saveWasmProfile(uintptr_t codeBase, size_t codeSize, |
| 135 |
JS::UniqueChars& desc); |
| 136 |
}; |
| 137 |
|
| 138 |
class WasmBaselinePerfSpewer : public PerfSpewer { |
| 139 |
const char* CodeName(uint32_t op) override; |
| 140 |
|
| 141 |
public: |
| 142 |
WasmBaselinePerfSpewer() = default; |
| 143 |
WasmBaselinePerfSpewer(WasmBaselinePerfSpewer&&) = default; |
| 144 |
WasmBaselinePerfSpewer& operator=(WasmBaselinePerfSpewer&&) = default; |
| 145 |
|
| 146 |
[[nodiscard]] bool needsToRecordInstruction() const; |
| 147 |
void recordInstruction(MacroAssembler& masm, const wasm::OpBytes& op); |
| 148 |
void saveProfile(uintptr_t codeBase, size_t codeSize, JS::UniqueChars& desc); |
| 149 |
}; |
| 150 |
|
| 151 |
class BaselineInterpreterPerfSpewer : public PerfSpewer { |
| 152 |
const char* CodeName(uint32_t op) override; |
| 153 |
|
| 154 |
// Do nothing, BaselineInterpreter has no source to reference. |
| 155 |
void saveJitCodeSourceInfo(JSScript* script, JitCode* code, |
| 156 |
AutoLockPerfSpewer& lock) override {} |
| 157 |
|
| 158 |
public: |
| 159 |
void recordOffset(MacroAssembler& masm, const JSOp& op); |
| 160 |
void recordOffset(MacroAssembler& masm, const char* name); |
| 161 |
void saveProfile(JitCode* code); |
| 162 |
}; |
| 163 |
|
| 164 |
class BaselinePerfSpewer : public PerfSpewer { |
| 165 |
const char* CodeName(uint32_t op) override; |
| 166 |
|
| 167 |
public: |
| 168 |
void recordInstruction(MacroAssembler& masm, jsbytecode* pc, |
| 169 |
CompilerFrameInfo& frame); |
| 170 |
void saveProfile(JSContext* cx, JSScript* script, JitCode* code); |
| 171 |
}; |
| 172 |
|
| 173 |
class InlineCachePerfSpewer : public PerfSpewer { |
| 174 |
const char* CodeName(uint32_t op) override; |
| 175 |
|
| 176 |
public: |
| 177 |
void recordInstruction(MacroAssembler& masm, const CacheOp& op); |
| 178 |
}; |
| 179 |
|
| 180 |
class BaselineICPerfSpewer : public InlineCachePerfSpewer { |
| 181 |
void saveJitCodeSourceInfo(JSScript* script, JitCode* code, |
| 182 |
AutoLockPerfSpewer& lock) override { |
| 183 |
// Baseline IC stubs are shared and have no source code to reference. |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
public: |
| 188 |
void saveProfile(JitCode* code, const char* stubName); |
| 189 |
}; |
| 190 |
|
| 191 |
class IonICPerfSpewer : public InlineCachePerfSpewer { |
| 192 |
public: |
| 193 |
explicit IonICPerfSpewer(jsbytecode* pc); |
| 194 |
|
| 195 |
void saveJitCodeSourceInfo(JSScript* script, JitCode* code, |
| 196 |
AutoLockPerfSpewer& lock) override; |
| 197 |
|
| 198 |
void saveProfile(JSContext* cx, JSScript* script, JitCode* code, |
| 199 |
const char* stubName); |
| 200 |
}; |
| 201 |
|
| 202 |
class PerfSpewerRangeRecorder { |
| 203 |
using OffsetPair = std::tuple<uint32_t, JS::UniqueChars>; |
| 204 |
Vector<OffsetPair, 0, js::SystemAllocPolicy> ranges; |
| 205 |
|
| 206 |
MacroAssembler& masm; |
| 207 |
|
| 208 |
void appendEntry(JS::UniqueChars& desc); |
| 209 |
|
| 210 |
public: |
| 211 |
explicit PerfSpewerRangeRecorder(MacroAssembler& masm_) : masm(masm_) {}; |
| 212 |
void recordOffset(const char* name); |
| 213 |
void recordOffset(const char* name, JSContext* cx, JSScript* script); |
| 214 |
void recordVMWrapperOffset(const char* name); |
| 215 |
void collectRangesForJitCode(JitCode* code); |
| 216 |
}; |
| 217 |
|
| 218 |
} // namespace jit |
| 219 |
} // namespace js |
| 220 |
|
| 221 |
#endif /* jit_PerfSpewer_h */ |