xref: /netbsd-src/sys/kern/subr_iostat.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: subr_iostat.c,v 1.17 2009/04/04 07:30:10 ad Exp $	*/
2 /*	NetBSD: subr_disk.c,v 1.69 2005/05/29 22:24:15 christos Exp	*/
3 
4 /*-
5  * Copyright (c) 1996, 1997, 1999, 2000, 2009 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  * (c) UNIX System Laboratories, Inc.
38  * All or some portions of this file are derived from material licensed
39  * to the University of California by American Telephone and Telegraph
40  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41  * the permission of UNIX System Laboratories, Inc.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	@(#)ufs_disksubr.c	8.5 (Berkeley) 1/21/94
68  */
69 
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: subr_iostat.c,v 1.17 2009/04/04 07:30:10 ad Exp $");
72 
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/kmem.h>
76 #include <sys/iostat.h>
77 #include <sys/sysctl.h>
78 #include <sys/rwlock.h>
79 
80 /*
81  * Function prototypes for sysctl nodes
82  */
83 static int	sysctl_hw_disknames(SYSCTLFN_PROTO);
84 static int	sysctl_hw_iostatnames(SYSCTLFN_PROTO);
85 static int	sysctl_hw_iostats(SYSCTLFN_PROTO);
86 
87 static int
88 iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
89 		u_int namelen);
90 
91 /*
92  * A global list of all drives attached to the system.  May grow or
93  * shrink over time.
94  */
95 struct iostatlist_head iostatlist = TAILQ_HEAD_INITIALIZER(iostatlist);
96 int iostat_count;		/* number of drives in global drivelist */
97 krwlock_t iostatlist_lock;
98 
99 /*
100  * Initialise the iostat subsystem.
101  */
102 void
103 iostat_init(void)
104 {
105 
106 	rw_init(&iostatlist_lock);
107 }
108 
109 /*
110  * Searches the iostatlist for the iostat corresponding to the
111  * name provided.
112  */
113 struct io_stats *
114 iostat_find(const char *name)
115 {
116 	struct io_stats *iostatp;
117 
118 	KASSERT(name != NULL);
119 
120 	rw_enter(&iostatlist_lock, RW_READER);
121 	TAILQ_FOREACH(iostatp, &iostatlist, io_link) {
122 		if (strcmp(iostatp->io_name, name) == 0) {
123 			break;
124 		}
125 	}
126 	rw_exit(&iostatlist_lock);
127 
128 	return iostatp;
129 }
130 
131 /*
132  * Allocate and initialise memory for the i/o statistics.
133  */
134 struct io_stats *
135 iostat_alloc(int32_t type, void *parent, const char *name)
136 {
137 	struct io_stats *stats;
138 
139 	stats = kmem_zalloc(sizeof(*stats), KM_SLEEP);
140 	if (stats == NULL)
141 		panic("iostat_alloc: cannot allocate memory for stats buffer");
142 
143 	stats->io_type = type;
144 	stats->io_parent = parent;
145 	(void)strlcpy(stats->io_name, name, sizeof(stats->io_name));
146 
147 	/*
148 	 * Set the attached timestamp.
149 	 */
150 	getmicrouptime(&stats->io_attachtime);
151 
152 	/*
153 	 * Link into the drivelist.
154 	 */
155 	rw_enter(&iostatlist_lock, RW_WRITER);
156 	TAILQ_INSERT_TAIL(&iostatlist, stats, io_link);
157 	iostat_count++;
158 	rw_exit(&iostatlist_lock);
159 
160 	return stats;
161 }
162 
163 /*
164  * Remove i/o from stats collection.
165  */
166 void
167 iostat_free(struct io_stats *stats)
168 {
169 
170 	/*
171 	 * Remove from the iostat list.
172 	 */
173 	if (iostat_count == 0)
174 		panic("iostat_free: iostat_count == 0");
175 	rw_enter(&iostatlist_lock, RW_WRITER);
176 	TAILQ_REMOVE(&iostatlist, stats, io_link);
177 	iostat_count--;
178 	rw_exit(&iostatlist_lock);
179 	kmem_free(stats, sizeof(*stats));
180 }
181 
182 /*
183  * Increment a iostat busy counter.  If the counter is going from
184  * 0 to 1, set the timestamp.
185  */
186 void
187 iostat_busy(struct io_stats *stats)
188 {
189 
190 	if (stats->io_busy++ == 0)
191 		getmicrouptime(&stats->io_timestamp);
192 }
193 
194 /*
195  * Decrement a iostat busy counter, increment the byte count, total busy
196  * time, and reset the timestamp.
197  */
198 void
199 iostat_unbusy(struct io_stats *stats, long bcount, int read)
200 {
201 	struct timeval dv_time, diff_time;
202 
203 	if (stats->io_busy-- == 0) {
204 		printf("%s: busy < 0\n", stats->io_name);
205 		panic("iostat_unbusy");
206 	}
207 
208 	getmicrouptime(&dv_time);
209 
210 	timersub(&dv_time, &stats->io_timestamp, &diff_time);
211 	timeradd(&stats->io_time, &diff_time, &stats->io_time);
212 
213 	stats->io_timestamp = dv_time;
214 	if (bcount > 0) {
215 		if (read) {
216 			stats->io_rbytes += bcount;
217 			stats->io_rxfer++;
218 		} else {
219 			stats->io_wbytes += bcount;
220 			stats->io_wxfer++;
221 		}
222 	}
223 }
224 
225 /*
226  * Return non-zero if a device has an I/O request in flight.
227  */
228 bool
229 iostat_isbusy(struct io_stats *stats)
230 {
231 
232 	return stats->io_busy != 0;
233 }
234 
235 /*
236  * Increment the seek counter.  This does look almost redundant but it
237  * abstracts the stats gathering.
238  */
239 void
240 iostat_seek(struct io_stats *stats)
241 {
242 
243 	stats->io_seek++;
244 }
245 
246 static int
247 sysctl_hw_disknames(SYSCTLFN_ARGS)
248 {
249 
250 	return iostati_getnames(1, oldp, oldlenp, newp, namelen);
251 }
252 
253 static int
254 sysctl_hw_iostatnames(SYSCTLFN_ARGS)
255 {
256 
257 	return iostati_getnames(0, oldp, oldlenp, newp, namelen);
258 }
259 
260 static int
261 iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
262 		 u_int namelen)
263 {
264 	char bf[IOSTATNAMELEN + 1];
265 	char *where = oldp;
266 	struct io_stats *stats;
267 	size_t needed, left, slen;
268 	int error, first;
269 
270 	if (newp != NULL)
271 		return (EPERM);
272 	if (namelen != 0)
273 		return (EINVAL);
274 
275 	first = 1;
276 	error = 0;
277 	needed = 0;
278 	left = *oldlenp;
279 
280 	rw_enter(&iostatlist_lock, RW_READER);
281 	for (stats = TAILQ_FIRST(&iostatlist); stats != NULL;
282 	    stats = TAILQ_NEXT(stats, io_link)) {
283 		if ((disk_only == 1) && (stats->io_type != IOSTAT_DISK))
284 			continue;
285 
286 		if (where == NULL)
287 			needed += strlen(stats->io_name) + 1;
288 		else {
289 			memset(bf, 0, sizeof(bf));
290 			if (first) {
291 				strncpy(bf, stats->io_name, sizeof(bf));
292 				first = 0;
293 			} else {
294 				bf[0] = ' ';
295 				strncpy(bf + 1, stats->io_name,
296 				    sizeof(bf) - 1);
297 			}
298 			bf[IOSTATNAMELEN] = '\0';
299 			slen = strlen(bf);
300 			if (left < slen + 1)
301 				break;
302 			/* +1 to copy out the trailing NUL byte */
303 			error = copyout(bf, where, slen + 1);
304 			if (error)
305 				break;
306 			where += slen;
307 			needed += slen;
308 			left -= slen;
309 		}
310 	}
311 	rw_exit(&iostatlist_lock);
312 	*oldlenp = needed;
313 	return (error);
314 }
315 
316 static int
317 sysctl_hw_iostats(SYSCTLFN_ARGS)
318 {
319 	struct io_sysctl sdrive;
320 	struct io_stats *stats;
321 	char *where = oldp;
322 	size_t tocopy, left;
323 	int error;
324 
325 	if (newp != NULL)
326 		return (EPERM);
327 
328 	/*
329 	 * The original hw.diskstats call was broken and did not require
330 	 * the userland to pass in it's size of struct disk_sysctl.  This
331 	 * was fixed after NetBSD 1.6 was released.
332 	 */
333 	if (namelen == 0)
334 		tocopy = offsetof(struct io_sysctl, busy);
335 	else
336 		tocopy = name[0];
337 
338 	if (where == NULL) {
339 		*oldlenp = iostat_count * tocopy;
340 		return (0);
341 	}
342 
343 	error = 0;
344 	left = *oldlenp;
345 	memset(&sdrive, 0, sizeof(sdrive));
346 	*oldlenp = 0;
347 
348 	rw_enter(&iostatlist_lock, RW_READER);
349 	TAILQ_FOREACH(stats, &iostatlist, io_link) {
350 		if (left < tocopy)
351 			break;
352 		strncpy(sdrive.name, stats->io_name, sizeof(sdrive.name));
353 		sdrive.xfer = stats->io_rxfer + stats->io_wxfer;
354 		sdrive.rxfer = stats->io_rxfer;
355 		sdrive.wxfer = stats->io_wxfer;
356 		sdrive.seek = stats->io_seek;
357 		sdrive.bytes = stats->io_rbytes + stats->io_wbytes;
358 		sdrive.rbytes = stats->io_rbytes;
359 		sdrive.wbytes = stats->io_wbytes;
360 		sdrive.attachtime_sec = stats->io_attachtime.tv_sec;
361 		sdrive.attachtime_usec = stats->io_attachtime.tv_usec;
362 		sdrive.timestamp_sec = stats->io_timestamp.tv_sec;
363 		sdrive.timestamp_usec = stats->io_timestamp.tv_usec;
364 		sdrive.time_sec = stats->io_time.tv_sec;
365 		sdrive.time_usec = stats->io_time.tv_usec;
366 		sdrive.busy = stats->io_busy;
367 
368 		error = copyout(&sdrive, where, min(tocopy, sizeof(sdrive)));
369 		if (error)
370 			break;
371 		where += tocopy;
372 		*oldlenp += tocopy;
373 		left -= tocopy;
374 	}
375 	rw_exit(&iostatlist_lock);
376 	return (error);
377 }
378 
379 SYSCTL_SETUP(sysctl_io_stats_setup, "sysctl i/o stats setup")
380 {
381 
382 	sysctl_createv(clog, 0, NULL, NULL,
383 		       CTLFLAG_PERMANENT,
384 		       CTLTYPE_STRING, "disknames",
385 		       SYSCTL_DESCR("List of disk drives present"),
386 		       sysctl_hw_disknames, 0, NULL, 0,
387 		       CTL_HW, HW_DISKNAMES, CTL_EOL);
388 	sysctl_createv(clog, 0, NULL, NULL,
389 		       CTLFLAG_PERMANENT,
390 		       CTLTYPE_STRING, "iostatnames",
391 		       SYSCTL_DESCR("I/O stats are being collected for these"
392 				    " devices"),
393 		       sysctl_hw_iostatnames, 0, NULL, 0,
394 		       CTL_HW, HW_IOSTATNAMES, CTL_EOL);
395 	sysctl_createv(clog, 0, NULL, NULL,
396 		       CTLFLAG_PERMANENT,
397 		       CTLTYPE_STRUCT, "iostats",
398 		       SYSCTL_DESCR("Statistics on device I/O operations"),
399 		       sysctl_hw_iostats, 0, NULL, 0,
400 		       CTL_HW, HW_IOSTATS, CTL_EOL);
401 }
402