Skip to content

Instantly share code, notes, and snippets.

@jimjh
Last active June 23, 2023 01:49
Show Gist options
  • Save jimjh/45014b5a4f2611ac7b1af03c98346b18 to your computer and use it in GitHub Desktop.
Save jimjh/45014b5a4f2611ac7b1af03c98346b18 to your computer and use it in GitHub Desktop.
gevent_tracer.py
def _callback(event, args):
# switch and throw both switch the active greenlet from origin to target
if event not in set(['switch', 'throw']):
return
origin, target = args
# record last switch (time and CPU tick)
global __last_switch_time_ms, __last_switch_cpu_tick
then_time, then_tick = __last_switch_time_ms, __last_switch_cpu_tick
now_time, now_tick = switch_timer()
__last_switch_time_ms, __last_switch_cpu_tick = now_time, now_tick
# first switch, nothing to record
if None in (then_time, then_tick):
return
if origin is gevent.hub.get_hub():
return
elapsed_ms = now_time - then_time
elapsed_ticks = now_tick - then_tick
if elapsed_ms <= threshold_ms:
return
log_fn(timing=elapsed_ms, value=elapsed_ticks, reason=trace_info) # log extra information e.g. stack frames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment