#!/usr/bin/stap
/*
 * fslatency-nd.stp	Measure file system I/O latency distribution.
 *			For Linux, uses SystemTap (non-debuginfo).
 *
 * USAGE: ./fslatency-nd.stp [interval [count]]
 *
 * This script uses kernel dynamic tracing of two common file system functions:
 * do_sync_read() and do_sync_write(). This provides a view of just two file
 * system request types. There are typically many others: asynchronous I/O,
 * directory operations, file handle operations, etc, that this script does
 * not instrument.
 *
 * This current implementation also traces sock_aio_write(), to exclude that
 * write time from the report.
 *
 * From systemtap-lwtools: https://github.com/brendangregg/systemtap-lwtools
 *
 * See the corresponding man page (in systemtap-lwtools) for more info.
 *
 * Copyright (C) 2015 Brendan Gregg.
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 06-Feb-2015	Brendan Gregg	Created this.
 */

global ts[65536];	# 65536 is max concurrent I/O
global lat, interval = 0, secs = 0, count = 0, output = 0;

probe begin
{
	printf("Tracing FS sync reads and writes... ");
	if (argv_1 != "") {
		interval = strtol(argv_1, 10);
		if (argv_2 != "") {
			count = strtol(argv_2, 10);
		}
		printf("Output every %d secs.\n", interval);
	} else {
		printf("Hit Ctrl-C to end.\n");
	}
}

probe read_funcs, write_funcs
{
	ts[tid()] = gettimeofday_ns();
}

probe read_funcs = kprobe.function("do_sync_read") !, __vfs_read
{
}
probe __vfs_read = kprobe.function("__vfs_read")
{
	# Skip the call if new_sync_read() wouldn't be called.
	file = pointer_arg(1)
	if (!file || @cast(file, "file", "kernel")->f_op->read
	    || !@cast(file, "file", "kernel")->f_op->read_iter)
		next
}

probe write_funcs = kprobe.function("do_sync_write") !, __vfs_write
{
}
probe __vfs_write = kprobe.function("__vfs_write")
{
	# Skip the call if new_sync_write() wouldn't be called.
	file = pointer_arg(1)
	if (!file || @cast(file, "file", "kernel")->f_op->write
	    || !@cast(file, "file", "kernel")->f_op->write_iter)
		next
}

probe kprobe.function("sock_write_iter") !, kprobe.function("sock_aio_write")
{
	/* not a file system write */
	ts[tid()] = 0;
}

probe read_funcs.return, write_funcs.return
{
	if (ts[tid()]) {
		lat[ppfunc()] <<< gettimeofday_ns() - ts[tid()];
		delete ts[tid()];
	}
}

probe read_funcs.return = kprobe.function("do_sync_read").return !,
			  __vfs_read.return
{
}
probe __vfs_read.return = kprobe.function("__vfs_read").return
{
	# Skip the call if new_sync_read() wouldn't be called.
	file = @entry(pointer_arg(1))
	if (!file || @cast(file, "file", "kernel")->f_op->read
	    || !@cast(file, "file", "kernel")->f_op->read_iter)
		next
}

probe write_funcs.return = kprobe.function("do_sync_write").return !,
			   __vfs_write.return
{
}
probe __vfs_write.return = kprobe.function("__vfs_write")
{
	# Skip the call if new_sync_write() wouldn't be called.
	file = pointer_arg(1)
	if (!file || @cast(file, "file", "kernel")->f_op->write
	    || !@cast(file, "file", "kernel")->f_op->write_iter)
		next
}

function print_report()
{
	printf("\n");
	if (interval) { printf("%s ", ctime(gettimeofday_s())); }
	printf("FS latency (ns):\n\n");
	foreach (name in lat+) {
		printf("FS call: %s()\n", name);
		print(@hist_log(lat[name]));
	}
	delete lat;
}

probe timer.s(1)
{
	if (interval) {
		if (++secs == interval) {
			print_report();
			secs = 0;
			count && (++output == count) && exit();
		}
	}
}

probe end
{
	!interval && print_report();
	delete secs;
	delete output;
}
