facebook / hermes

A JavaScript engine optimized for running React Native.
https://hermesengine.dev/
MIT License
9.41k stars 596 forks source link

[Profiler] First sample sometimes has timestamp of zero #1368

Open Beanyy opened 1 month ago

Beanyy commented 1 month ago

Bug Description

When using the Profiler, sometimes the first sample has a timestamp of zero, where the other samples have a timestamp very far off from zero. When viewing the trace, the total duration ends up being very long (like 100s) even though the capture of the trace was just 5s long.

I've narrowed down the issue to right here: https://github.com/facebook/hermes/blob/main/lib/VM/Profiler/SamplingProfilerSampler.cpp#L74

If the code path reaches here for the first sample, sampleStorage_ is still in its initial state without being assigned a new sample, and then that is appended to the sampledStacks_ array. Locally, I have fixed this issue by just adding return true after sampledStackDepth_ = 0;, but I'm unsure if that's the right approach to resolving this issue and would like some guidance on how you think this should be resolved.

Thanks

Hermes git revision (if applicable): React Native version: OS: Platform (most likely one of arm64-v8a, armeabi-v7a, x86, x86_64):

Steps To Reproduce

1. 2.

code example:

The Expected Behavior

neildhar commented 1 month ago

Thanks for reporting this, it certainly seems like a bug when this codepath is hit, since the timestamp is uninitialised. However, I'm slightly surprised that you're hitting it at all, given that there aren't really any cases where the profiler is suspended but there is no recorded stack available. Could you share more about how you're getting into that state?