Skip to content

Commit

Permalink
fix(profiling): Dynamically adjust profiler sleep time (#1634)
Browse files Browse the repository at this point in the history
Because more time may have elapsed between 2 samples due to us calling the
sampling function and other threads executing, we need to account for it in the
sleep or the time between samples will often be greater than the expected
interval. This change ensures we account for this time elapsed and dynamically
adjust the amount of time we sleep for between samples.
  • Loading branch information
Zylphrex authored Sep 26, 2022
1 parent b6c0096 commit f71a8f4
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions sentry_sdk/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,23 @@ class _SleepScheduler(_ThreadScheduler):

def run(self):
# type: () -> None
last = time.perf_counter()

while True:
# some time may have elapsed since the last time
# we sampled, so we need to account for that and
# not sleep for too long
now = time.perf_counter()
elapsed = max(now - last, 0)

if elapsed < self._interval:
time.sleep(self._interval - elapsed)

last = time.perf_counter()

if self.event.is_set():
break
time.sleep(self._interval)

_sample_stack()


Expand All @@ -395,9 +408,11 @@ class _EventScheduler(_ThreadScheduler):
def run(self):
# type: () -> None
while True:
self.event.wait(timeout=self._interval)

if self.event.is_set():
break
self.event.wait(timeout=self._interval)

_sample_stack()


Expand Down

0 comments on commit f71a8f4

Please sign in to comment.