Skip to content

Commit

Permalink
Increase timer resolution for sampling profiler
Browse files Browse the repository at this point in the history
Summary:
When using the sampling profiler on Windows, it is apparent that the
frequency is not as high as it is on other platforms. Doing some local
testing, the cause of the low frequency sampling is Windows timer
resolution when calling std::condition_variable::wait_for. By default,
this resolution is approximately 64Hz, meaning the sampling profiler
maxes out at ~64Hz. Setting `timeBeginPeriod(1)` improves sampling
frequency, maxing out at ~500Hz.

Reviewed By: neildhar

Differential Revision: D69500253

fbshipit-source-id: 3108c4629e6f2853731171e92ddf6af89792154b
  • Loading branch information
rozele authored and facebook-github-bot committed Feb 14, 2025
1 parent f0998df commit da283d1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/VM/Profiler/SamplingProfilerSampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
#include <random>
#include <thread>

#if defined(_WINDOWS)
#include <windows.h>
// Must be included after windows.h
#include <mmsystem.h>
#include "llvh/ADT/ScopeExit.h"
#endif

namespace hermes {
namespace vm {
namespace sampling_profiler {
Expand Down Expand Up @@ -143,6 +150,14 @@ void Sampler::timerLoop(double meanHzFreq) {
std::normal_distribution<> distribution{interval, interval / 2};
std::unique_lock<std::mutex> uniqueLock(profilerLock_);

#if defined(_WINDOWS)
// By default, timer resolution is approximately 64Hz on Windows, so if the
// meanHzFreq parameter is greater than 64, sampling will occur at a lower
// frequency than desired. Setting the period to 1 is the minimum useful
// value, resulting in timer resolution of roughly 1 millsecond.
timeBeginPeriod(1);
auto restorePeriod = llvh::make_scope_exit([] { timeEndPeriod(1); });
#endif
while (enabled_) {
if (!sampleStacks()) {
return;
Expand Down

0 comments on commit da283d1

Please sign in to comment.