1#!/usr/sbin/dtrace -s
2/*
3 * seeksize.d - analyse disk head seek distance by process.
4 *              Written using DTrace (Solaris 10 3/05).
5 *
6 * Disk I/O events caused by processes will in turn cause the disk heads
7 * to seek. This program analyses those seeks, so that we can determine
8 * if processes are causing the disks to seek in a "random" or "sequential"
9 * manner.
10 *
11 * $Id: seeksize.d 3 2007-08-01 10:50:08Z brendan $
12 *
13 * USAGE:	seeksize.d		# wait several seconds, then hit Ctrl-C
14 *
15 * FIELDS:
16 *		PID	process ID
17 *		CMD	command and argument list
18 *		value	distance in disk blocks (sectors)
19 *		count	number of I/O operations
20 *
21 * SEE ALSO: bitesize.d, iosnoop
22 *
23 * COPYRIGHT: Copyright (c) 2006 Brendan Gregg.
24 *
25 * CDDL HEADER START
26 *
27 *  The contents of this file are subject to the terms of the
28 *  Common Development and Distribution License, Version 1.0 only
29 *  (the "License").  You may not use this file except in compliance
30 *  with the License.
31 *
32 *  You can obtain a copy of the license at Docs/cddl1.txt
33 *  or http://www.opensolaris.org/os/licensing.
34 *  See the License for the specific language governing permissions
35 *  and limitations under the License.
36 *
37 * CDDL HEADER END
38 *
39 * 11-Sep-2004	Brendan Gregg	Created this.
40 * 10-Oct-2004	   "      "	Rewrote to use the io provider.
41 * 20-Apr-2006	   "      "	Last update.
42 */
43
44#pragma D option quiet
45
46/*
47 * Print header
48 */
49dtrace:::BEGIN
50{
51	printf("Tracing... Hit Ctrl-C to end.\n");
52}
53
54self int last[dev_t];
55
56/*
57 * Process io start
58 */
59io:genunix::start
60/self->last[args[0]->b_edev] != 0/
61{
62	/* calculate seek distance */
63	this->last = self->last[args[0]->b_edev];
64	this->dist = (int)(args[0]->b_blkno - this->last) > 0 ?
65	    args[0]->b_blkno - this->last : this->last - args[0]->b_blkno;
66
67	/* store details */
68	@Size[pid, curpsinfo->pr_psargs] = quantize(this->dist);
69}
70
71io:genunix::start
72{
73	/* save last position of disk head */
74	self->last[args[0]->b_edev] = args[0]->b_blkno +
75	    args[0]->b_bcount / 512;
76}
77
78/*
79 * Print final report
80 */
81dtrace:::END
82{
83	printf("\n%8s  %s\n", "PID", "CMD");
84	printa("%8d  %S\n%@d\n", @Size);
85}
86