Skip to content

Commit

Permalink
tools/biolatency: Handle signals from user
Browse files Browse the repository at this point in the history
  • Loading branch information
kcalvinalvin authored and yonghong-song committed Oct 1, 2020
1 parent 21b810a commit 87792ce
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions tools/biolatency.py
Expand Up @@ -15,6 +15,7 @@
from bcc import BPF
from time import sleep, strftime
import argparse
import signal

# arguments
examples = """examples:
Expand Down Expand Up @@ -193,15 +194,7 @@ def flags_print(flags):
desc = "NoWait-" + desc
return desc

# output
exiting = 0 if args.interval else 1
dist = b.get_table("dist")
while (1):
try:
sleep(int(args.interval))
except KeyboardInterrupt:
exiting = 1

def print_hist():
print()
if args.timestamp:
print("%-8s\n" % strftime("%H:%M:%S"), end="")
Expand All @@ -212,6 +205,28 @@ def flags_print(flags):
dist.print_log2_hist(label, "disk")
dist.clear()

def exit_handler():
print_hist()
exit()

def signal_handler(sig, frame):
exit_handler()

signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGQUIT, signal_handler)

exiting = 0 if args.interval else 1
dist = b.get_table("dist")

while (1):
try:
sleep(int(args.interval))
except KeyboardInterrupt:
exiting = 1

print_hist()

countdown -= 1
if exiting or countdown == 0:
exit()
exit_handler()

0 comments on commit 87792ce

Please sign in to comment.