Commit 5167127c authored by Jerome Marchand's avatar Jerome Marchand Committed by yonghong-song

Tools: properly handle the keyboard interrupt (#2083)

Many tools rely on the user to type Ctrl-C to end, but don't actually
catch the keyboard interrupt and thus show an ugly backtrace when it
happens. Let's catch the interrupt.
parent c22ec6d4
...@@ -61,4 +61,7 @@ def print_event(cpu, data, size): ...@@ -61,4 +61,7 @@ def print_event(cpu, data, size):
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -187,4 +187,7 @@ def print_event(cpu, data, size): ...@@ -187,4 +187,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -352,4 +352,7 @@ else: ...@@ -352,4 +352,7 @@ else:
# read events # read events
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -216,4 +216,7 @@ def print_event(bpf, cpu, data, size): ...@@ -216,4 +216,7 @@ def print_event(bpf, cpu, data, size):
callback = partial(print_event, b) callback = partial(print_event, b)
b["events"].open_perf_buffer(callback) b["events"].open_perf_buffer(callback)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -328,4 +328,7 @@ print("Finding critical section with {} disabled for > {}us".format( ...@@ -328,4 +328,7 @@ print("Finding critical section with {} disabled for > {}us".format(
('preempt' if preemptoff else 'IRQ'), args.duration)) ('preempt' if preemptoff else 'IRQ'), args.duration))
while 1: while 1:
b.perf_buffer_poll(); try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -230,4 +230,7 @@ print("%-14s %-6s %8s %s" % ("TIME(s)", "PID", "MS", "QUERY")) ...@@ -230,4 +230,7 @@ print("%-14s %-6s %8s %s" % ("TIME(s)", "PID", "MS", "QUERY"))
bpf["events"].open_perf_buffer(print_event, page_cnt=64) bpf["events"].open_perf_buffer(print_event, page_cnt=64)
while True: while True:
bpf.perf_buffer_poll() try:
bpf.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -162,4 +162,7 @@ print("%-11s %-6s %-16s %1s %s" % ("TIME(s)", "PID", "COMM", "T", "FILE")) ...@@ -162,4 +162,7 @@ print("%-11s %-6s %-16s %1s %s" % ("TIME(s)", "PID", "COMM", "T", "FILE"))
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -246,4 +246,7 @@ def print_event(cpu, data, size): ...@@ -246,4 +246,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -356,4 +356,7 @@ else: ...@@ -356,4 +356,7 @@ else:
# read events # read events
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -141,4 +141,7 @@ def print_event(cpu, data, size): ...@@ -141,4 +141,7 @@ def print_event(cpu, data, size):
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -250,4 +250,7 @@ def print_event(cpu, data, size): ...@@ -250,4 +250,7 @@ def print_event(cpu, data, size):
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -330,4 +330,7 @@ def print_event(cpu, data, size): ...@@ -330,4 +330,7 @@ def print_event(cpu, data, size):
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while True: while True:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -135,4 +135,7 @@ def print_event(cpu, data, size): ...@@ -135,4 +135,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -507,7 +507,10 @@ struct pid_struct { ...@@ -507,7 +507,10 @@ struct pid_struct {
def _main_loop(self): def _main_loop(self):
while True: while True:
self.bpf.perf_buffer_poll() try:
self.bpf.perf_buffer_poll()
except KeyboardInterrupt:
exit()
def run(self): def run(self):
self._create_probes() self._create_probes()
......
...@@ -145,4 +145,7 @@ def print_event(cpu, data, size): ...@@ -145,4 +145,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -206,4 +206,7 @@ def print_event(cpu, data, size): ...@@ -206,4 +206,7 @@ def print_event(cpu, data, size):
bpf["calls"].open_perf_buffer(print_event) bpf["calls"].open_perf_buffer(print_event)
while 1: while 1:
bpf.perf_buffer_poll() try:
bpf.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -244,4 +244,7 @@ def print_event(cpu, data, size): ...@@ -244,4 +244,7 @@ def print_event(cpu, data, size):
bpf["gcs"].open_perf_buffer(print_event) bpf["gcs"].open_perf_buffer(print_event)
while 1: while 1:
bpf.perf_buffer_poll() try:
bpf.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -128,4 +128,7 @@ def print_event(cpu, data, size): ...@@ -128,4 +128,7 @@ def print_event(cpu, data, size):
bpf["threads"].open_perf_buffer(print_event) bpf["threads"].open_perf_buffer(print_event)
while 1: while 1:
bpf.perf_buffer_poll() try:
bpf.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -78,4 +78,7 @@ def print_event(cpu, data, size): ...@@ -78,4 +78,7 @@ def print_event(cpu, data, size):
# read events # read events
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -414,7 +414,11 @@ def main(): ...@@ -414,7 +414,11 @@ def main():
print('{:16} {:<7} {:<7} {:<11} {}'.format( print('{:16} {:<7} {:<7} {:<11} {}'.format(
'COMM', 'PID', 'TID', 'MNT_NS', 'CALL')) 'COMM', 'PID', 'TID', 'MNT_NS', 'CALL'))
while True: while True:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -130,4 +130,7 @@ def print_event(cpu, data, size): ...@@ -130,4 +130,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -325,4 +325,7 @@ else: ...@@ -325,4 +325,7 @@ else:
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
try:
b.perf_buffer_poll() b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -77,4 +77,7 @@ b = BPF(text=bpf_text) ...@@ -77,4 +77,7 @@ b = BPF(text=bpf_text)
print("Tracing OOM kills... Ctrl-C to stop.") print("Tracing OOM kills... Ctrl-C to stop.")
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -191,4 +191,7 @@ def print_event(cpu, data, size): ...@@ -191,4 +191,7 @@ def print_event(cpu, data, size):
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
start_time = datetime.now() start_time = datetime.now()
while not args.duration or datetime.now() - start_time < args.duration: while not args.duration or datetime.now() - start_time < args.duration:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -254,4 +254,7 @@ print("%-8s %-16s %-6s %14s" % ("TIME", "COMM", "PID", "LAT(us)")) ...@@ -254,4 +254,7 @@ print("%-8s %-16s %-6s %14s" % ("TIME", "COMM", "PID", "LAT(us)"))
# read events # read events
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -210,4 +210,7 @@ if __name__ == "__main__": ...@@ -210,4 +210,7 @@ if __name__ == "__main__":
# Read events # Read events
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -228,4 +228,7 @@ def print_event(cpu, data, size, rw): ...@@ -228,4 +228,7 @@ def print_event(cpu, data, size, rw):
b["perf_SSL_write"].open_perf_buffer(print_event_write) b["perf_SSL_write"].open_perf_buffer(print_event_write)
b["perf_SSL_read"].open_perf_buffer(print_event_read) b["perf_SSL_read"].open_perf_buffer(print_event_read)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -179,4 +179,7 @@ def print_event(cpu, data, size): ...@@ -179,4 +179,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -50,4 +50,7 @@ def print_event(cpu, data, size): ...@@ -50,4 +50,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -270,4 +270,7 @@ start_ts = 0 ...@@ -270,4 +270,7 @@ start_ts = 0
b["ipv4_events"].open_perf_buffer(print_ipv4_event) b["ipv4_events"].open_perf_buffer(print_ipv4_event)
b["ipv6_events"].open_perf_buffer(print_ipv6_event) b["ipv6_events"].open_perf_buffer(print_ipv6_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -237,4 +237,7 @@ start_ts = 0 ...@@ -237,4 +237,7 @@ start_ts = 0
b["ipv4_events"].open_perf_buffer(print_ipv4_event) b["ipv4_events"].open_perf_buffer(print_ipv4_event)
b["ipv6_events"].open_perf_buffer(print_ipv6_event) b["ipv6_events"].open_perf_buffer(print_ipv6_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -264,4 +264,7 @@ print("%-6s %-12s %-2s %-16s %-16s %-5s %s" % ("PID", "COMM", "IP", "SADDR", ...@@ -264,4 +264,7 @@ print("%-6s %-12s %-2s %-16s %-16s %-5s %s" % ("PID", "COMM", "IP", "SADDR",
b["ipv4_events"].open_perf_buffer(print_ipv4_event) b["ipv4_events"].open_perf_buffer(print_ipv4_event)
b["ipv6_events"].open_perf_buffer(print_ipv6_event) b["ipv6_events"].open_perf_buffer(print_ipv6_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -221,4 +221,7 @@ print("%-8s %-6s %-2s %-20s > %-20s %s (%s)" % ("TIME", "PID", "IP", ...@@ -221,4 +221,7 @@ print("%-8s %-6s %-2s %-20s > %-20s %s (%s)" % ("TIME", "PID", "IP",
b["ipv4_events"].open_perf_buffer(print_ipv4_event) b["ipv4_events"].open_perf_buffer(print_ipv4_event)
b["ipv6_events"].open_perf_buffer(print_ipv6_event) b["ipv6_events"].open_perf_buffer(print_ipv6_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -506,4 +506,7 @@ start_ts = 0 ...@@ -506,4 +506,7 @@ start_ts = 0
b["ipv4_events"].open_perf_buffer(print_ipv4_event, page_cnt=64) b["ipv4_events"].open_perf_buffer(print_ipv4_event, page_cnt=64)
b["ipv6_events"].open_perf_buffer(print_ipv6_event, page_cnt=64) b["ipv6_events"].open_perf_buffer(print_ipv6_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -303,4 +303,7 @@ else: ...@@ -303,4 +303,7 @@ else:
b["ipv4_events"].open_perf_buffer(print_ipv4_event) b["ipv4_events"].open_perf_buffer(print_ipv4_event)
b["ipv6_events"].open_perf_buffer(print_ipv6_event) b["ipv6_events"].open_perf_buffer(print_ipv6_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -386,4 +386,7 @@ start_ts = 0 ...@@ -386,4 +386,7 @@ start_ts = 0
b["ipv4_events"].open_perf_buffer(print_ipv4_event, page_cnt=64) b["ipv4_events"].open_perf_buffer(print_ipv4_event, page_cnt=64)
b["ipv6_events"].open_perf_buffer(print_ipv6_event, page_cnt=64) b["ipv6_events"].open_perf_buffer(print_ipv6_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -662,4 +662,7 @@ def inet_ntoa(addr): ...@@ -662,4 +662,7 @@ def inet_ntoa(addr):
b["tcp_ipv4_event"].open_perf_buffer(print_ipv4_event) b["tcp_ipv4_event"].open_perf_buffer(print_ipv4_event)
b["tcp_ipv6_event"].open_perf_buffer(print_ipv6_event) b["tcp_ipv6_event"].open_perf_buffer(print_ipv6_event)
while True: while True:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -121,4 +121,7 @@ def print_event(cpu, data, size): ...@@ -121,4 +121,7 @@ def print_event(cpu, data, size):
# loop with callback to print_event # loop with callback to print_event
b["events"].open_perf_buffer(print_event) b["events"].open_perf_buffer(print_event)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -303,4 +303,7 @@ else: ...@@ -303,4 +303,7 @@ else:
# read events # read events
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
...@@ -315,4 +315,7 @@ else: ...@@ -315,4 +315,7 @@ else:
# read events # read events
b["events"].open_perf_buffer(print_event, page_cnt=64) b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1: while 1:
b.perf_buffer_poll() try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment