Skip to content

Commit 74e153d

Browse files
schuayV8 LUCI CQ
authored andcommitted
[stack traces] Reduce stack frame summarization costs
During stack trace capture, Summarize() is the most expensive step — it creates a full TranslatedState for every optimized frame even though most frames are never inspected. This CL reduces that cost in two ways: 1. Lightweight Summarize() for optimized frames: instead of building a full TranslatedState, walk only the deopt translation frame headers and resolve function/receiver via ResolveTaggedValue(), falling back to the full TranslatedState path for wasm-inlined or unresolvable closures. 2. Deferred baseline frames: during CaptureSimpleStackTrace, baseline frames store the raw Code + PC offset and defer bytecode offset resolution to ExpandDeferredFrames(), which runs lazily before the stack trace is formatted or inspected. A new Torque bitfield flag (is_deferred_baseline_frame) marks entries in the raw capture array that still need resolution. All consumers (GetSimpleStackTrace, GetDetailedStackTraceFromCallSiteInfos, GetFormattedStack, PrintCurrentStackTrace) call ExpandDeferredFrames() before processing the array. Change-Id: I7bf44c5f29ee0f6b2ba45ef1acf4a1f15178ee93 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7714738 Commit-Queue: Jakob Linke <[email protected]> Auto-Submit: Jakob Linke <[email protected]> Reviewed-by: Leszek Swirski <[email protected]> Commit-Queue: Leszek Swirski <[email protected]> Cr-Commit-Position: refs/heads/main@{#106218}
1 parent 42fa832 commit 74e153d

9 files changed

Lines changed: 309 additions & 57 deletions

File tree

‎src/deoptimizer/translated-state.cc‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,27 @@ Address TranslatedState::DecompressIfNeeded(intptr_t value) {
18941894
}
18951895
}
18961896

1897+
// static
1898+
Tagged<Object> TranslatedState::ResolveTaggedValue(
1899+
DeoptTranslationIterator* it, Address fp,
1900+
Tagged<DeoptimizationLiteralArray> literals) {
1901+
TranslationOpcode opcode = it->NextOpcode();
1902+
switch (opcode) {
1903+
case TranslationOpcode::LITERAL: {
1904+
int literal_index = it->NextOperand();
1905+
return literals->get(literal_index);
1906+
}
1907+
case TranslationOpcode::TAGGED_STACK_SLOT: {
1908+
int slot_offset =
1909+
OptimizedJSFrame::StackSlotOffsetRelativeToFp(it->NextOperand());
1910+
intptr_t value = *reinterpret_cast<intptr_t*>(fp + slot_offset);
1911+
return Tagged<Object>(DecompressIfNeeded(value));
1912+
}
1913+
default:
1914+
UNREACHABLE();
1915+
}
1916+
}
1917+
18971918
TranslatedState::TranslatedState(const JavaScriptFrame* frame)
18981919
: purpose_(kFrameInspection) {
18991920
int deopt_index = SafepointEntry::kNoDeoptIndex;

‎src/deoptimizer/translated-state.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,15 @@ class TranslatedState {
511511
void VerifyMaterializedObjects();
512512
bool DoUpdateFeedback(DeoptimizeReason reason);
513513

514+
// Resolves one deopt translation value opcode to a raw Tagged<Object>,
515+
// reading from the live frame if needed. Only LITERAL and
516+
// TAGGED_STACK_SLOT are expected; other opcodes are UNREACHABLE.
517+
static Tagged<Object> ResolveTaggedValue(
518+
DeoptTranslationIterator* it, Address fp,
519+
Tagged<DeoptimizationLiteralArray> literals);
520+
521+
static Address DecompressIfNeeded(intptr_t value);
522+
514523
private:
515524
friend TranslatedValue;
516525

@@ -530,7 +539,6 @@ class TranslatedState {
530539
int frame_index, DeoptTranslationIterator* iterator,
531540
const DeoptimizationLiteralProvider& literal_array, Address fp,
532541
RegisterValues* registers, FILE* trace_file);
533-
Address DecompressIfNeeded(intptr_t value);
534542
void CreateArgumentsElementsTranslatedValues(int frame_index,
535543
Address input_frame_pointer,
536544
CreateArgumentsType type,

‎src/execution/frames.cc‎

Lines changed: 116 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3099,7 +3099,108 @@ FrameSummaries OptimizedJSFrame::Summarize(bool never_allocate) const {
30993099
"Missing deoptimization information for OptimizedJSFrame::Summarize.");
31003100
}
31013101

3102-
// Prepare iteration over translation. We must not materialize values here
3102+
// Lightweight walk: iterate frame headers only, resolving just the
3103+
// function and receiver from the live frame via ResolveTaggedValue.
3104+
// This avoids the expensive TranslatedState::Init + Prepare path that
3105+
// would parse every value in every inlined frame.
3106+
3107+
Tagged<DeoptimizationLiteralArray> literal_array = data->LiteralArray();
3108+
3109+
// Lightweight walk: resolve function and receiver from live frame headers
3110+
// and build JavaScriptFrameSummary objects directly.
3111+
bool needs_full_walk = false;
3112+
{
3113+
DisallowGarbageCollection no_gc;
3114+
DeoptimizationFrameTranslation::Iterator it(
3115+
data->FrameTranslation(), data->TranslationIndex(deopt_index).value());
3116+
bool is_constructor = IsConstructor();
3117+
int remaining = it.EnterBeginOpcode().total_frame_count;
3118+
3119+
while (remaining > 0) {
3120+
TranslationOpcode opcode = it.SeekNextFrame();
3121+
remaining--;
3122+
3123+
if (opcode == TranslationOpcode::CONSTRUCT_CREATE_STUB_FRAME ||
3124+
opcode == TranslationOpcode::CONSTRUCT_INVOKE_STUB_FRAME) {
3125+
is_constructor = true;
3126+
it.SkipOperands(TranslationOpcodeOperandCount(opcode));
3127+
continue;
3128+
}
3129+
3130+
if (!IsTranslationJsFrameOpcode(opcode)) {
3131+
#if V8_ENABLE_WEBASSEMBLY
3132+
// Wasm-inlined-into-JS frames need the full TranslatedState
3133+
// machinery to produce WasmFrameSummary entries.
3134+
if (opcode == TranslationOpcode::WASM_INLINED_INTO_JS_FRAME) {
3135+
needs_full_walk = true;
3136+
break;
3137+
}
3138+
#endif
3139+
it.SkipOperands(TranslationOpcodeOperandCount(opcode));
3140+
continue;
3141+
}
3142+
3143+
bool is_builtin_cont =
3144+
(opcode == TranslationOpcode::JAVASCRIPT_BUILTIN_CONTINUATION_FRAME ||
3145+
opcode == TranslationOpcode::
3146+
JAVASCRIPT_BUILTIN_CONTINUATION_WITH_CATCH_FRAME);
3147+
3148+
int bytecode_offset = it.NextOperand();
3149+
int sfi_id = it.NextOperand();
3150+
Tagged<SharedFunctionInfo> sfi =
3151+
Cast<SharedFunctionInfo>(literal_array->get(sfi_id));
3152+
3153+
// Skip remaining header operands to reach the values.
3154+
it.SkipOperands(TranslationOpcodeOperandCount(opcode) - 2);
3155+
3156+
// Resolve closure and receiver from the live frame. Both are always
3157+
// encoded as LITERAL or TAGGED_STACK_SLOT in the deopt translation.
3158+
Tagged<Object> function_obj =
3159+
TranslatedState::ResolveTaggedValue(&it, fp(), literal_array);
3160+
DCHECK(IsJSFunction(function_obj));
3161+
Tagged<Object> receiver_obj =
3162+
TranslatedState::ResolveTaggedValue(&it, fp(), literal_array);
3163+
3164+
Tagged<AbstractCode> abstract_code;
3165+
int code_offset;
3166+
if (is_builtin_cont) {
3167+
code_offset = 0;
3168+
abstract_code = Cast<AbstractCode>(
3169+
isolate()->builtins()->code(Builtins::GetBuiltinFromBytecodeOffset(
3170+
BytecodeOffset(bytecode_offset))));
3171+
} else {
3172+
code_offset = bytecode_offset;
3173+
abstract_code = Cast<AbstractCode>(sfi->GetBytecodeArray(isolate()));
3174+
}
3175+
3176+
FrameSummary::JavaScriptFrameSummary summary(
3177+
isolate(), receiver_obj, Cast<JSFunction>(function_obj),
3178+
abstract_code, code_offset, is_constructor);
3179+
summaries.frames.push_back(summary);
3180+
is_constructor = false;
3181+
}
3182+
3183+
if (!needs_full_walk && is_constructor) {
3184+
summaries.top_frame_is_construct_call = true;
3185+
}
3186+
} // no_gc scope ends.
3187+
3188+
if (needs_full_walk) {
3189+
return SummarizeFull(data, deopt_index, never_allocate);
3190+
}
3191+
3192+
return summaries;
3193+
}
3194+
3195+
FrameSummaries OptimizedJSFrame::SummarizeFull(Tagged<DeoptimizationData> data,
3196+
int deopt_index,
3197+
bool never_allocate) const {
3198+
FrameSummaries summaries;
3199+
3200+
DCHECK_NE(deopt_index, SafepointEntry::kNoDeoptIndex);
3201+
DCHECK(!data.is_null());
3202+
3203+
// Prepare iteration over translation. We must not materialize values here
31033204
// because we do not deoptimize the function.
31043205
TranslatedState translated(this);
31053206
translated.Prepare(fp());
@@ -3141,7 +3242,6 @@ FrameSummaries OptimizedJSFrame::Summarize(bool never_allocate) const {
31413242
} else {
31423243
receiver = translated_values->GetValue();
31433244
}
3144-
31453245
translated_values++;
31463246

31473247
// Determine the underlying code object and the position within it from
@@ -3247,24 +3347,20 @@ int TurbofanJSFrame::FindReturnPCForTrampoline(Tagged<Code> code,
32473347
return safepoints.FindReturnPC(trampoline_pc);
32483348
}
32493349

3250-
Tagged<DeoptimizationData> OptimizedJSFrame::GetDeoptimizationData(
3251-
Tagged<Code> code, int* deopt_index) const {
3252-
DCHECK(is_optimized());
3253-
3254-
Address pc = maybe_unauthenticated_pc();
3255-
3256-
DCHECK(code->contains(isolate(), pc));
3350+
// static
3351+
Tagged<DeoptimizationData> OptimizedJSFrame::GetDeoptimizationDataForPC(
3352+
Isolate* isolate, Tagged<Code> code, Address pc, int* deopt_index) {
3353+
DCHECK(code->contains(isolate, pc));
32573354
DCHECK(CodeKindCanDeoptimize(code->kind()));
3258-
32593355
if (code->is_maglevved()) {
3260-
MaglevSafepointTable table(isolate(), pc, code);
3356+
MaglevSafepointTable table(isolate, pc, code);
32613357
MaglevSafepointEntry safepoint_entry = table.FindEntry(pc);
32623358
if (safepoint_entry.has_deoptimization_index()) {
32633359
*deopt_index = safepoint_entry.deoptimization_index();
32643360
return code->deoptimization_data();
32653361
}
32663362
} else {
3267-
SafepointTable table(isolate(), pc, code);
3363+
SafepointTable table(isolate, pc, code);
32683364
SafepointEntry& safepoint_entry = table.FindEntry_NoStackSlots(pc);
32693365
if (safepoint_entry.has_deoptimization_index()) {
32703366
*deopt_index = safepoint_entry.deoptimization_index();
@@ -3275,6 +3371,14 @@ Tagged<DeoptimizationData> OptimizedJSFrame::GetDeoptimizationData(
32753371
return {};
32763372
}
32773373

3374+
Tagged<DeoptimizationData> OptimizedJSFrame::GetDeoptimizationData(
3375+
Tagged<Code> code, int* deopt_index) const {
3376+
DCHECK(is_optimized());
3377+
Address pc = maybe_unauthenticated_pc();
3378+
DCHECK(code->contains(isolate(), pc));
3379+
return GetDeoptimizationDataForPC(isolate(), code, pc, deopt_index);
3380+
}
3381+
32783382
void OptimizedJSFrame::GetFunctions(
32793383
std::vector<Tagged<SharedFunctionInfo>>* functions) const {
32803384
DCHECK(functions->empty());

‎src/execution/frames.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,11 @@ class OptimizedJSFrame : public JavaScriptFrame {
11671167
Tagged<DeoptimizationData> GetDeoptimizationData(Tagged<Code> code,
11681168
int* deopt_index) const;
11691169

1170+
// Like GetDeoptimizationData, but takes an explicit PC instead of reading
1171+
// it from the frame. Can be used without a live frame.
1172+
static Tagged<DeoptimizationData> GetDeoptimizationDataForPC(
1173+
Isolate* isolate, Tagged<Code> code, Address pc, int* deopt_index);
1174+
11701175
static int StackSlotOffsetRelativeToFp(int slot_index);
11711176

11721177
// Lookup exception handler for current {pc}, returns -1 if none found.
@@ -1176,6 +1181,13 @@ class OptimizedJSFrame : public JavaScriptFrame {
11761181
virtual int FindReturnPCForTrampoline(Tagged<Code> code,
11771182
int trampoline_pc) const = 0;
11781183

1184+
private:
1185+
// Full TranslatedState-based walk, used as fallback when the lightweight
1186+
// path in Summarize() encounters frames it cannot handle (e.g.
1187+
// wasm-inlined-into-JS frames).
1188+
FrameSummaries SummarizeFull(Tagged<DeoptimizationData> data, int deopt_index,
1189+
bool never_allocate) const;
1190+
11791191
protected:
11801192
inline explicit OptimizedJSFrame(StackFrameIteratorBase* iterator);
11811193
};

0 commit comments

Comments
 (0)