xref: /netbsd-src/sys/kern/subr_iostat.c (revision 7e30e94394d0994ab9534f68a8f91665045c91ce)
1 /*	$NetBSD: subr_iostat.c,v 1.22 2017/03/05 23:07:12 mlelstv 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.22 2017/03/05 23:07:12 mlelstv 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 static void sysctl_io_stats_setup(struct sysctllog **);
100 
101 /*
102  * Initialise the iostat subsystem.
103  */
104 void
105 iostat_init(void)
106 {
107 
108 	rw_init(&iostatlist_lock);
109 	sysctl_io_stats_setup(NULL);
110 }
111 
112 /*
113  * Searches the iostatlist for the iostat corresponding to the
114  * name provided.
115  */
116 struct io_stats *
117 iostat_find(const char *name)
118 {
119 	struct io_stats *iostatp;
120 
121 	KASSERT(name != NULL);
122 
123 	rw_enter(&iostatlist_lock, RW_READER);
124 	TAILQ_FOREACH(iostatp, &iostatlist, io_link) {
125 		if (strcmp(iostatp->io_name, name) == 0) {
126 			break;
127 		}
128 	}
129 	rw_exit(&iostatlist_lock);
130 
131 	return iostatp;
132 }
133 
134 /*
135  * Allocate and initialise memory for the i/o statistics.
136  */
137 struct io_stats *
138 iostat_alloc(int32_t type, void *parent, const char *name)
139 {
140 	struct io_stats *stats;
141 
142 	stats = kmem_zalloc(sizeof(*stats), KM_SLEEP);
143 	if (stats == NULL)
144 		panic("iostat_alloc: cannot allocate memory for stats buffer");
145 
146 	stats->io_type = type;
147 	stats->io_parent = parent;
148 	(void)strlcpy(stats->io_name, name, sizeof(stats->io_name));
149 
150 	/*
151 	 * Set the attached timestamp.
152 	 */
153 	getmicrouptime(&stats->io_attachtime);
154 
155 	/*
156 	 * Link into the drivelist.
157 	 */
158 	rw_enter(&iostatlist_lock, RW_WRITER);
159 	TAILQ_INSERT_TAIL(&iostatlist, stats, io_link);
160 	iostat_count++;
161 	rw_exit(&iostatlist_lock);
162 
163 	return stats;
164 }
165 
166 /*
167  * Remove i/o from stats collection.
168  */
169 void
170 iostat_free(struct io_stats *stats)
171 {
172 
173 	/*
174 	 * Remove from the iostat list.
175 	 */
176 	if (iostat_count == 0)
177 		panic("iostat_free: iostat_count == 0");
178 	rw_enter(&iostatlist_lock, RW_WRITER);
179 	TAILQ_REMOVE(&iostatlist, stats, io_link);
180 	iostat_count--;
181 	rw_exit(&iostatlist_lock);
182 	kmem_free(stats, sizeof(*stats));
183 }
184 
185 /*
186  * multiply timeval by unsigned integer and add to result
187  */
188 static void
189 timermac(struct timeval *a, uint64_t count, struct timeval *res)
190 {
191 	struct timeval part = *a;
192 
193 	while (count) {
194 		if (count & 1)
195 			timeradd(res, &part, res);
196 		timeradd(&part, &part, &part);
197 		count >>= 1;
198 	}
199 }
200 
201 /*
202  * Increment the iostat wait counter.
203  * Accumulate wait time and timesum.
204  *
205  * Wait time is spent in the device bufq.
206  */
207 void
208 iostat_wait(struct io_stats *stats)
209 {
210 	struct timeval dv_time, diff_time;
211 	int32_t count;
212 
213 	KASSERT(stats->io_wait >= 0);
214 
215 	getmicrouptime(&dv_time);
216 
217 	timersub(&dv_time, &stats->io_waitstamp, &diff_time);
218 	count = stats->io_wait++;
219 	if (count != 0) {
220 		timermac(&diff_time, count, &stats->io_waitsum);
221 		timeradd(&stats->io_waittime, &diff_time, &stats->io_waittime);
222 	}
223 	stats->io_waitstamp = dv_time;
224 }
225 
226 /*
227  * Decrement the iostat wait counter.
228  * Increment the iostat busy counter.
229  * Accumulate wait and busy times and timesums.
230  *
231  * Busy time is spent being processed by the device.
232  *
233  * Old devices do not yet measure wait time, so skip
234  * processing it if the counter is still zero.
235  */
236 void
237 iostat_busy(struct io_stats *stats)
238 {
239 	struct timeval dv_time, diff_time;
240 	int32_t count;
241 
242 	KASSERT(stats->io_wait >= 0); /* > 0 when iostat_wait is used */
243 	KASSERT(stats->io_busy >= 0);
244 
245 	getmicrouptime(&dv_time);
246 
247 	timersub(&dv_time, &stats->io_waitstamp, &diff_time);
248 	if (stats->io_wait != 0) {
249 		count = stats->io_wait--;
250 		timermac(&diff_time, count, &stats->io_waitsum);
251 		timeradd(&stats->io_waittime, &diff_time, &stats->io_waittime);
252 	}
253 	stats->io_waitstamp = dv_time;
254 
255 	timersub(&dv_time, &stats->io_busystamp, &diff_time);
256 	count = stats->io_busy++;
257 	if (count != 0) {
258 		timermac(&diff_time, count, &stats->io_busysum);
259 		timeradd(&stats->io_busytime, &diff_time, &stats->io_busytime);
260 	}
261 	stats->io_busystamp = dv_time;
262 }
263 
264 /*
265  * Decrement the iostat busy counter, increment the byte count.
266  * Accumulate busy time and timesum.
267  */
268 void
269 iostat_unbusy(struct io_stats *stats, long bcount, int read)
270 {
271 	struct timeval dv_time, diff_time;
272 	int32_t count;
273 
274 	KASSERT(stats->io_busy > 0);
275 
276 	getmicrouptime(&dv_time);
277 	stats->io_timestamp = dv_time;
278 
279 	/* any op */
280 	timersub(&dv_time, &stats->io_busystamp, &diff_time);
281 	count = stats->io_busy--;
282 	timermac(&diff_time, count, &stats->io_busysum);
283 	timeradd(&stats->io_busytime, &diff_time, &stats->io_busytime);
284 	stats->io_busystamp = dv_time;
285 
286 	if (bcount > 0) {
287 		if (read) {
288 			stats->io_rbytes += bcount;
289 			stats->io_rxfer++;
290 		} else {
291 			stats->io_wbytes += bcount;
292 			stats->io_wxfer++;
293 		}
294 	}
295 }
296 
297 /*
298  * Return non-zero if a device has an I/O request in flight.
299  */
300 bool
301 iostat_isbusy(struct io_stats *stats)
302 {
303 
304 	return stats->io_busy != 0;
305 }
306 
307 /*
308  * Increment the seek counter.  This does look almost redundant but it
309  * abstracts the stats gathering.
310  */
311 void
312 iostat_seek(struct io_stats *stats)
313 {
314 
315 	stats->io_seek++;
316 }
317 
318 static int
319 sysctl_hw_disknames(SYSCTLFN_ARGS)
320 {
321 
322 	return iostati_getnames(1, oldp, oldlenp, newp, namelen);
323 }
324 
325 static int
326 sysctl_hw_iostatnames(SYSCTLFN_ARGS)
327 {
328 
329 	return iostati_getnames(0, oldp, oldlenp, newp, namelen);
330 }
331 
332 static int
333 iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
334 		 u_int namelen)
335 {
336 	char bf[IOSTATNAMELEN + 1];
337 	char *where = oldp;
338 	struct io_stats *stats;
339 	size_t needed, left, slen;
340 	int error, first;
341 
342 	if (newp != NULL)
343 		return (EPERM);
344 	if (namelen != 0)
345 		return (EINVAL);
346 
347 	first = 1;
348 	error = 0;
349 	needed = 0;
350 	left = *oldlenp;
351 
352 	rw_enter(&iostatlist_lock, RW_READER);
353 	for (stats = TAILQ_FIRST(&iostatlist); stats != NULL;
354 	    stats = TAILQ_NEXT(stats, io_link)) {
355 		if ((disk_only == 1) && (stats->io_type != IOSTAT_DISK))
356 			continue;
357 
358 		if (where == NULL)
359 			needed += strlen(stats->io_name) + 1;
360 		else {
361 			memset(bf, 0, sizeof(bf));
362 			if (first) {
363 				strncpy(bf, stats->io_name, sizeof(bf));
364 				first = 0;
365 			} else {
366 				bf[0] = ' ';
367 				strncpy(bf + 1, stats->io_name,
368 				    sizeof(bf) - 1);
369 			}
370 			bf[IOSTATNAMELEN] = '\0';
371 			slen = strlen(bf);
372 			if (left < slen + 1)
373 				break;
374 			/* +1 to copy out the trailing NUL byte */
375 			error = copyout(bf, where, slen + 1);
376 			if (error)
377 				break;
378 			where += slen;
379 			needed += slen;
380 			left -= slen;
381 		}
382 	}
383 	rw_exit(&iostatlist_lock);
384 	*oldlenp = needed;
385 	return (error);
386 }
387 
388 static int
389 sysctl_hw_iostats(SYSCTLFN_ARGS)
390 {
391 	struct io_sysctl sdrive;
392 	struct io_stats *stats;
393 	char *where = oldp;
394 	size_t tocopy, left;
395 	int error;
396 
397 	if (newp != NULL)
398 		return (EPERM);
399 
400 	/*
401 	 * The original hw.diskstats call was broken and did not require
402 	 * the userland to pass in its size of struct disk_sysctl.  This
403 	 * was fixed after NetBSD 1.6 was released.
404 	 */
405 	if (namelen == 0)
406 		tocopy = offsetof(struct io_sysctl, busy);
407 	else
408 		tocopy = name[0];
409 
410 	if (where == NULL) {
411 		*oldlenp = iostat_count * tocopy;
412 		return (0);
413 	}
414 
415 	error = 0;
416 	left = *oldlenp;
417 	memset(&sdrive, 0, sizeof(sdrive));
418 	*oldlenp = 0;
419 
420 	rw_enter(&iostatlist_lock, RW_READER);
421 	TAILQ_FOREACH(stats, &iostatlist, io_link) {
422 		if (left < tocopy)
423 			break;
424 
425 		strncpy(sdrive.name, stats->io_name, sizeof(sdrive.name));
426 		sdrive.attachtime_sec = stats->io_attachtime.tv_sec;
427 		sdrive.attachtime_usec = stats->io_attachtime.tv_usec;
428 		sdrive.timestamp_sec = stats->io_busystamp.tv_sec;
429 		sdrive.timestamp_usec = stats->io_busystamp.tv_usec;
430 
431 		sdrive.time_sec = stats->io_busytime.tv_sec;
432 		sdrive.time_usec = stats->io_busytime.tv_usec;
433 
434 		sdrive.seek = stats->io_seek;
435 
436 		sdrive.rxfer = stats->io_rxfer;
437 		sdrive.wxfer = stats->io_wxfer;
438 		sdrive.xfer = stats->io_rxfer + stats->io_wxfer;
439 
440 		sdrive.rbytes = stats->io_rbytes;
441 		sdrive.wbytes = stats->io_wbytes;
442 		sdrive.bytes = stats->io_rbytes + stats->io_wbytes;
443 
444 		sdrive.wait_sec = stats->io_waittime.tv_sec;
445 		sdrive.wait_usec = stats->io_waittime.tv_usec;
446 
447 		sdrive.time_sec = stats->io_busytime.tv_sec;
448 		sdrive.time_usec = stats->io_busytime.tv_usec;
449 
450 		sdrive.waitsum_sec = stats->io_waitsum.tv_sec;
451 		sdrive.waitsum_usec = stats->io_waitsum.tv_usec;
452 
453 		sdrive.busysum_sec = stats->io_busysum.tv_sec;
454 		sdrive.busysum_usec = stats->io_busysum.tv_usec;
455 
456 		sdrive.busy = stats->io_busy;
457 
458 		error = copyout(&sdrive, where, min(tocopy, sizeof(sdrive)));
459 		if (error)
460 			break;
461 		where += tocopy;
462 		*oldlenp += tocopy;
463 		left -= tocopy;
464 	}
465 	rw_exit(&iostatlist_lock);
466 	return (error);
467 }
468 
469 static void
470 sysctl_io_stats_setup(struct sysctllog **clog)
471 {
472 
473 	sysctl_createv(clog, 0, NULL, NULL,
474 		       CTLFLAG_PERMANENT,
475 		       CTLTYPE_STRING, "disknames",
476 		       SYSCTL_DESCR("List of disk drives present"),
477 		       sysctl_hw_disknames, 0, NULL, 0,
478 		       CTL_HW, HW_DISKNAMES, CTL_EOL);
479 	sysctl_createv(clog, 0, NULL, NULL,
480 		       CTLFLAG_PERMANENT,
481 		       CTLTYPE_STRING, "iostatnames",
482 		       SYSCTL_DESCR("I/O stats are being collected for these"
483 				    " devices"),
484 		       sysctl_hw_iostatnames, 0, NULL, 0,
485 		       CTL_HW, HW_IOSTATNAMES, CTL_EOL);
486 	sysctl_createv(clog, 0, NULL, NULL,
487 		       CTLFLAG_PERMANENT,
488 		       CTLTYPE_STRUCT, "iostats",
489 		       SYSCTL_DESCR("Statistics on device I/O operations"),
490 		       sysctl_hw_iostats, 0, NULL, 0,
491 		       CTL_HW, HW_IOSTATS, CTL_EOL);
492 }
493