Skip to content

Commit

Permalink
Add cli argument to limit run time (iovisor#2428)
Browse files Browse the repository at this point in the history
If a user is granted sudo access only to btrfsslower.py and not kill or timeout, then user is unable to limit the runtime of btrfsslower.py. The optional cli argument stops execution of program after specified number of seconds passes.
  • Loading branch information
williamlw999 authored and yonghong-song committed Jun 30, 2019
1 parent fabb9a5 commit 8bd65dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
9 changes: 8 additions & 1 deletion man/man8/btrfsslower.8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.SH NAME
btrfsslower \- Trace slow btrfs file operations, with per-event details.
.SH SYNOPSIS
.B btrfsslower [\-h] [\-j] [\-p PID] [min_ms]
.B btrfsslower [\-h] [\-j] [\-p PID] [min_ms] [\-d DURATION]
.SH DESCRIPTION
This tool traces common btrfs file operations: reads, writes, opens, and
syncs. It measures the time spent in these operations, and prints details
Expand All @@ -25,6 +25,9 @@ Trace this PID only.
.TP
min_ms
Minimum I/O latency (duration) to trace, in milliseconds. Default is 10 ms.
.TP
\-d DURATION
Total duration of trace in seconds.
.SH EXAMPLES
.TP
Trace synchronous file reads and writes slower than 10 ms:
Expand All @@ -46,6 +49,10 @@ Trace all file reads and writes (warning: the output will be verbose):
Trace slower than 1 ms, for PID 181 only:
#
.B btrfsslower \-p 181 1
.TP
Trace for 10 seconds only:
#
.B btrfsslower \-d 10
.SH FIELDS
.TP
TIME(s)
Expand Down
11 changes: 8 additions & 3 deletions tools/btrfsslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# btrfsslower Trace slow btrfs operations.
# For Linux, uses BCC, eBPF.
#
# USAGE: btrfsslower [-h] [-j] [-p PID] [min_ms]
# USAGE: btrfsslower [-h] [-j] [-p PID] [min_ms] [-d DURATION]
#
# This script traces common btrfs file operations: reads, writes, opens, and
# syncs. It measures the time spent in these operations, and prints details
Expand All @@ -27,6 +27,7 @@
from __future__ import print_function
from bcc import BPF
import argparse
from datetime import datetime, timedelta
from time import strftime

# symbols
Expand All @@ -39,6 +40,7 @@
./btrfsslower -j 1 # ... 1 ms, parsable output (csv)
./btrfsslower 0 # trace all operations (warning: verbose)
./btrfsslower -p 185 # trace PID 185 only
./btrfsslower -d 10 # trace for 10 seconds only
"""
parser = argparse.ArgumentParser(
description="Trace common btrfs file operations slower than a threshold",
Expand All @@ -52,6 +54,8 @@
help="minimum I/O duration to trace, in ms (default 10)")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
parser.add_argument("-d", "--duration",
help="total duration of trace in seconds")
args = parser.parse_args()
min_ms = int(args.min_ms)
pid = args.pid
Expand Down Expand Up @@ -335,8 +339,9 @@ def print_event(cpu, data, size):

# read events
b["events"].open_perf_buffer(print_event, page_cnt=64)
while 1:
start_time = datetime.now()
while not args.duration or datetime.now() - start_time < args.duration:
try:
b.perf_buffer_poll()
b.perf_buffer_poll(timeout=1000)
except KeyboardInterrupt:
exit()
6 changes: 5 additions & 1 deletion tools/btrfsslower_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ patterns.
USAGE message:

# ./btrfsslower -h
usage: btrfsslower [-h] [-j] [-p PID] [min_ms]
usage: btrfsslower [-h] [-j] [-p PID] [min_ms] [-d DURATION]

Trace common btrfs file operations slower than a threshold

Expand All @@ -137,10 +137,14 @@ optional arguments:
-h, --help show this help message and exit
-j, --csv just print fields: comma-separated values
-p PID, --pid PID trace this PID only
-d DURATION, --duration DURATION
total duration of trace in seconds

examples:
./btrfsslower # trace operations slower than 10 ms (default)
./btrfsslower 1 # trace operations slower than 1 ms
./btrfsslower -j 1 # ... 1 ms, parsable output (csv)
./btrfsslower 0 # trace all operations (warning: verbose)
./btrfsslower -p 185 # trace PID 185 only
./btrfsslower -d 10 # trace for 10 seconds only

0 comments on commit 8bd65dd

Please sign in to comment.