Skip to content

Instantly share code, notes, and snippets.

@sphr2k
Created March 28, 2024 17:56
Show Gist options
  • Save sphr2k/73a97085c30cece1d90cdfcd2004fc4e to your computer and use it in GitHub Desktop.
Save sphr2k/73a97085c30cece1d90cdfcd2004fc4e to your computer and use it in GitHub Desktop.
Linux I/O tracing / performance analysis
#! /usr/bin/env bash
# Check if a device argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 /dev/<device>"
exit 1
fi
# Assign the first argument to DEVICE variable
DEVICE=$1
# Start blktrace to monitor block I/O for the specified device for 300 seconds
echo "Starting blktrace on ${DEVICE} for 300 seconds..."
sudo blktrace -w 300 "$DEVICE"
# Extract the device name from the full path (e.g., nvme0n1 from /dev/nvme0n1)
DEV_NAME=$(basename "$DEVICE")
# Process the blktrace binary output with blkparse
echo "Processing blktrace output with blkparse..."
blkparse -d "${DEV_NAME}.bin" -i "$DEV_NAME" -o
# btt is a block trace analysis tool
# -i specifies the blktrace binary input file
# -B specifies the output file for block offsets
echo "Analyzing block trace with btt..."
btt -i "${DEV_NAME}.bin" -B blk_offsets
# Run a custom Python script to further analyze the block offsets
# The filenames for analysis must be adjusted based on actual output files
echo "Running custom analysis with bt_analyze.py..."
./bt_analyze.py "b1k_offsets_259,4_r.dat" "blk_offsets_259,4_w.dat"
echo "Analysis complete."
with open(tracefile) as f:
for line in f:
start, end = line.split()[1:]
start = int(start)
end = int(end)
num_ios += 1
# keep track of sequential segments
if start != prev_segment:
num_segments += 1
prev_segment = end
# convert sectors to bytes
SZ = (end - start) / 2
iosize[SZ] += 1
total_size += SZ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment