|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "HermesRuntimeSamplingProfileSerializer.h" |
| 9 | + |
| 10 | +namespace facebook::react::jsinspector_modern::tracing { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +/// Fallback script ID for call frames, when Hermes didn't provide one or when |
| 15 | +/// this frame is part of the VM, like native functions, used for parity with |
| 16 | +/// Chromium + V8. |
| 17 | +const uint32_t FALLBACK_SCRIPT_ID = 0; |
| 18 | +/// Garbage collector frame name, used for parity with Chromium + V8. |
| 19 | +const std::string GARBAGE_COLLECTOR_FRAME_NAME = "(garbage collector)"; |
| 20 | + |
| 21 | +/// Filters out Hermes Suspend frames related to Debugger. |
| 22 | +/// Even though Debugger domain is expected to be disabled, Hermes might run |
| 23 | +/// Debugger loop while recording sampling profile. We only allow GC frames. |
| 24 | +bool shouldIgnoreHermesFrame( |
| 25 | + hermes::sampling_profiler::ProfileSampleCallStackFrame* hermesFrame) { |
| 26 | + if (hermesFrame->getKind() != |
| 27 | + hermes::sampling_profiler::ProfileSampleCallStackFrame::Kind::Suspend) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + auto* suspendFrame = static_cast< |
| 32 | + hermes::sampling_profiler::ProfileSampleCallStackSuspendFrame*>( |
| 33 | + hermesFrame); |
| 34 | + auto suspendFrameKind = suspendFrame->getSuspendFrameKind(); |
| 35 | + return suspendFrameKind != |
| 36 | + hermes::sampling_profiler::ProfileSampleCallStackSuspendFrame:: |
| 37 | + SuspendFrameKind::GC; |
| 38 | +} |
| 39 | + |
| 40 | +RuntimeSamplingProfile::SampleCallStackFrame convertHermesFrameToTracingFrame( |
| 41 | + hermes::sampling_profiler::ProfileSampleCallStackFrame* hermesFrame) { |
| 42 | + switch (hermesFrame->getKind()) { |
| 43 | + case hermes::sampling_profiler::ProfileSampleCallStackFrame::Kind:: |
| 44 | + JSFunction: { |
| 45 | + auto* jsFunctionFrame = static_cast< |
| 46 | + hermes::sampling_profiler::ProfileSampleCallStackJSFunctionFrame*>( |
| 47 | + hermesFrame); |
| 48 | + return RuntimeSamplingProfile::SampleCallStackFrame{ |
| 49 | + RuntimeSamplingProfile::SampleCallStackFrame::Kind::JSFunction, |
| 50 | + jsFunctionFrame->hasScriptId() ? jsFunctionFrame->getScriptId() |
| 51 | + : FALLBACK_SCRIPT_ID, |
| 52 | + jsFunctionFrame->getFunctionName(), |
| 53 | + jsFunctionFrame->hasUrl() |
| 54 | + ? std::optional<std::string>{jsFunctionFrame->getUrl()} |
| 55 | + : std::nullopt, |
| 56 | + jsFunctionFrame->hasLineNumber() |
| 57 | + ? std::optional<uint32_t>{jsFunctionFrame->getLineNumber() - 1} |
| 58 | + // Hermes VM keeps line numbers as 1-based. Convert to |
| 59 | + // 0-based. |
| 60 | + : std::nullopt, |
| 61 | + jsFunctionFrame->hasColumnNumber() |
| 62 | + ? std::optional<uint32_t>{jsFunctionFrame->getColumnNumber() - 1} |
| 63 | + // Hermes VM keeps column numbers as 1-based. Convert to |
| 64 | + // 0-based. |
| 65 | + : std::nullopt, |
| 66 | + }; |
| 67 | + } |
| 68 | + case hermes::sampling_profiler::ProfileSampleCallStackFrame::Kind:: |
| 69 | + NativeFunction: { |
| 70 | + auto* nativeFunctionFrame = |
| 71 | + static_cast<hermes::sampling_profiler:: |
| 72 | + ProfileSampleCallStackNativeFunctionFrame*>( |
| 73 | + hermesFrame); |
| 74 | + |
| 75 | + return RuntimeSamplingProfile::SampleCallStackFrame{ |
| 76 | + RuntimeSamplingProfile::SampleCallStackFrame::Kind::NativeFunction, |
| 77 | + FALLBACK_SCRIPT_ID, // JavaScript Runtime defines the implementation |
| 78 | + // for native function, no script ID to reference. |
| 79 | + nativeFunctionFrame->getFunctionName(), |
| 80 | + }; |
| 81 | + } |
| 82 | + case hermes::sampling_profiler::ProfileSampleCallStackFrame::Kind:: |
| 83 | + HostFunction: { |
| 84 | + auto* hostFunctionFrame = static_cast< |
| 85 | + hermes::sampling_profiler::ProfileSampleCallStackHostFunctionFrame*>( |
| 86 | + hermesFrame); |
| 87 | + |
| 88 | + return RuntimeSamplingProfile::SampleCallStackFrame{ |
| 89 | + RuntimeSamplingProfile::SampleCallStackFrame::Kind::HostFunction, |
| 90 | + FALLBACK_SCRIPT_ID, // JavaScript Runtime defines the implementation |
| 91 | + // for host function, no script ID to reference. |
| 92 | + hostFunctionFrame->getFunctionName(), |
| 93 | + }; |
| 94 | + } |
| 95 | + case hermes::sampling_profiler::ProfileSampleCallStackFrame::Kind:: |
| 96 | + Suspend: { |
| 97 | + auto* suspendFrame = static_cast< |
| 98 | + hermes::sampling_profiler::ProfileSampleCallStackSuspendFrame*>( |
| 99 | + hermesFrame); |
| 100 | + auto suspendFrameKind = suspendFrame->getSuspendFrameKind(); |
| 101 | + if (suspendFrameKind == |
| 102 | + hermes::sampling_profiler::ProfileSampleCallStackSuspendFrame:: |
| 103 | + SuspendFrameKind::GC) { |
| 104 | + return RuntimeSamplingProfile::SampleCallStackFrame{ |
| 105 | + RuntimeSamplingProfile::SampleCallStackFrame::Kind:: |
| 106 | + GarbageCollector, |
| 107 | + FALLBACK_SCRIPT_ID, // GC frames are part of the VM, no script ID to |
| 108 | + // reference. |
| 109 | + GARBAGE_COLLECTOR_FRAME_NAME, |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + // We should have filtered out Debugger Suspend frames before in |
| 114 | + // shouldFilterOutHermesFrame(). |
| 115 | + throw std::logic_error{ |
| 116 | + "Unexpected Suspend frame found in Hermes call stack"}; |
| 117 | + } |
| 118 | + |
| 119 | + default: |
| 120 | + throw std::logic_error{"Unknown Hermes stack frame kind"}; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +RuntimeSamplingProfile::Sample convertHermesSampleToTracingSample( |
| 125 | + hermes::sampling_profiler::ProfileSample& hermesSample) { |
| 126 | + uint64_t reconciledTimestamp = hermesSample.getTimestamp(); |
| 127 | + std::vector<hermes::sampling_profiler::ProfileSampleCallStackFrame*> |
| 128 | + hermesSampleCallStack = hermesSample.getCallStack(); |
| 129 | + |
| 130 | + std::vector<RuntimeSamplingProfile::SampleCallStackFrame> |
| 131 | + reconciledSampleCallStack; |
| 132 | + reconciledSampleCallStack.reserve(hermesSampleCallStack.size()); |
| 133 | + |
| 134 | + for (auto* hermesFrame : hermesSampleCallStack) { |
| 135 | + if (shouldIgnoreHermesFrame(hermesFrame)) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + RuntimeSamplingProfile::SampleCallStackFrame reconciledFrame = |
| 139 | + convertHermesFrameToTracingFrame(hermesFrame); |
| 140 | + reconciledSampleCallStack.push_back(std::move(reconciledFrame)); |
| 141 | + } |
| 142 | + |
| 143 | + return RuntimeSamplingProfile::Sample{ |
| 144 | + reconciledTimestamp, |
| 145 | + hermesSample.getThreadId(), |
| 146 | + std::move(reconciledSampleCallStack)}; |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace |
| 150 | + |
| 151 | +/* static */ RuntimeSamplingProfile |
| 152 | +HermesRuntimeSamplingProfileSerializer::serializeToTracingSamplingProfile( |
| 153 | + const hermes::sampling_profiler::Profile& hermesProfile) { |
| 154 | + std::vector<hermes::sampling_profiler::ProfileSample> hermesSamples = |
| 155 | + hermesProfile.getSamples(); |
| 156 | + std::vector<RuntimeSamplingProfile::Sample> reconciledSamples; |
| 157 | + reconciledSamples.reserve(hermesSamples.size()); |
| 158 | + |
| 159 | + for (auto& hermesSample : hermesSamples) { |
| 160 | + RuntimeSamplingProfile::Sample reconciledSample = |
| 161 | + convertHermesSampleToTracingSample(hermesSample); |
| 162 | + reconciledSamples.push_back(std::move(reconciledSample)); |
| 163 | + } |
| 164 | + |
| 165 | + return RuntimeSamplingProfile{"Hermes", std::move(reconciledSamples)}; |
| 166 | +} |
| 167 | + |
| 168 | +} // namespace facebook::react::jsinspector_modern::tracing |
0 commit comments