xref: /netbsd-src/sys/kern/subr_iostat.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: subr_iostat.c,v 1.15 2008/05/20 16:03:31 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 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.15 2008/05/20 16:03:31 ad Exp $");
72 
73 #include <sys/param.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.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 = malloc(sizeof(struct io_stats), M_DEVBUF, M_WAITOK|M_ZERO);
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 	free(stats, M_DEVBUF);
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  * Increment the seek counter.  This does look almost redundant but it
227  * abstracts the stats gathering.
228  */
229 void
230 iostat_seek(struct io_stats *stats)
231 {
232 
233 	stats->io_seek++;
234 }
235 
236 static int
237 sysctl_hw_disknames(SYSCTLFN_ARGS)
238 {
239 
240 	return iostati_getnames(1, oldp, oldlenp, newp, namelen);
241 }
242 
243 static int
244 sysctl_hw_iostatnames(SYSCTLFN_ARGS)
245 {
246 
247 	return iostati_getnames(0, oldp, oldlenp, newp, namelen);
248 }
249 
250 static int
251 iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
252 		 u_int namelen)
253 {
254 	char bf[IOSTATNAMELEN + 1];
255 	char *where = oldp;
256 	struct io_stats *stats;
257 	size_t needed, left, slen;
258 	int error, first;
259 
260 	if (newp != NULL)
261 		return (EPERM);
262 	if (namelen != 0)
263 		return (EINVAL);
264 
265 	first = 1;
266 	error = 0;
267 	needed = 0;
268 	left = *oldlenp;
269 
270 	rw_enter(&iostatlist_lock, RW_READER);
271 	for (stats = TAILQ_FIRST(&iostatlist); stats != NULL;
272 	    stats = TAILQ_NEXT(stats, io_link)) {
273 		if ((disk_only == 1) && (stats->io_type != IOSTAT_DISK))
274 			continue;
275 
276 		if (where == NULL)
277 			needed += strlen(stats->io_name) + 1;
278 		else {
279 			memset(bf, 0, sizeof(bf));
280 			if (first) {
281 				strncpy(bf, stats->io_name, sizeof(bf));
282 				first = 0;
283 			} else {
284 				bf[0] = ' ';
285 				strncpy(bf + 1, stats->io_name,
286 				    sizeof(bf) - 1);
287 			}
288 			bf[IOSTATNAMELEN] = '\0';
289 			slen = strlen(bf);
290 			if (left < slen + 1)
291 				break;
292 			/* +1 to copy out the trailing NUL byte */
293 			error = copyout(bf, where, slen + 1);
294 			if (error)
295 				break;
296 			where += slen;
297 			needed += slen;
298 			left -= slen;
299 		}
300 	}
301 	rw_exit(&iostatlist_lock);
302 	*oldlenp = needed;
303 	return (error);
304 }
305 
306 static int
307 sysctl_hw_iostats(SYSCTLFN_ARGS)
308 {
309 	struct io_sysctl sdrive;
310 	struct io_stats *stats;
311 	char *where = oldp;
312 	size_t tocopy, left;
313 	int error;
314 
315 	if (newp != NULL)
316 		return (EPERM);
317 
318 	/*
319 	 * The original hw.diskstats call was broken and did not require
320 	 * the userland to pass in it's size of struct disk_sysctl.  This
321 	 * was fixed after NetBSD 1.6 was released.
322 	 */
323 	if (namelen == 0)
324 		tocopy = offsetof(struct io_sysctl, busy);
325 	else
326 		tocopy = name[0];
327 
328 	if (where == NULL) {
329 		*oldlenp = iostat_count * tocopy;
330 		return (0);
331 	}
332 
333 	error = 0;
334 	left = *oldlenp;
335 	memset(&sdrive, 0, sizeof(sdrive));
336 	*oldlenp = 0;
337 
338 	rw_enter(&iostatlist_lock, RW_READER);
339 	TAILQ_FOREACH(stats, &iostatlist, io_link) {
340 		if (left < tocopy)
341 			break;
342 		strncpy(sdrive.name, stats->io_name, sizeof(sdrive.name));
343 		sdrive.xfer = stats->io_rxfer + stats->io_wxfer;
344 		sdrive.rxfer = stats->io_rxfer;
345 		sdrive.wxfer = stats->io_wxfer;
346 		sdrive.seek = stats->io_seek;
347 		sdrive.bytes = stats->io_rbytes + stats->io_wbytes;
348 		sdrive.rbytes = stats->io_rbytes;
349 		sdrive.wbytes = stats->io_wbytes;
350 		sdrive.attachtime_sec = stats->io_attachtime.tv_sec;
351 		sdrive.attachtime_usec = stats->io_attachtime.tv_usec;
352 		sdrive.timestamp_sec = stats->io_timestamp.tv_sec;
353 		sdrive.timestamp_usec = stats->io_timestamp.tv_usec;
354 		sdrive.time_sec = stats->io_time.tv_sec;
355 		sdrive.time_usec = stats->io_time.tv_usec;
356 		sdrive.busy = stats->io_busy;
357 
358 		error = copyout(&sdrive, where, min(tocopy, sizeof(sdrive)));
359 		if (error)
360 			break;
361 		where += tocopy;
362 		*oldlenp += tocopy;
363 		left -= tocopy;
364 	}
365 	rw_exit(&iostatlist_lock);
366 	return (error);
367 }
368 
369 SYSCTL_SETUP(sysctl_io_stats_setup, "sysctl i/o stats setup")
370 {
371 
372 	sysctl_createv(clog, 0, NULL, NULL,
373 		       CTLFLAG_PERMANENT,
374 		       CTLTYPE_STRING, "disknames",
375 		       SYSCTL_DESCR("List of disk drives present"),
376 		       sysctl_hw_disknames, 0, NULL, 0,
377 		       CTL_HW, HW_DISKNAMES, CTL_EOL);
378 	sysctl_createv(clog, 0, NULL, NULL,
379 		       CTLFLAG_PERMANENT,
380 		       CTLTYPE_STRING, "iostatnames",
381 		       SYSCTL_DESCR("I/O stats are being collected for these"
382 				    " devices"),
383 		       sysctl_hw_iostatnames, 0, NULL, 0,
384 		       CTL_HW, HW_IOSTATNAMES, CTL_EOL);
385 	sysctl_createv(clog, 0, NULL, NULL,
386 		       CTLFLAG_PERMANENT,
387 		       CTLTYPE_STRUCT, "iostats",
388 		       SYSCTL_DESCR("Statistics on device I/O operations"),
389 		       sysctl_hw_iostats, 0, NULL, 0,
390 		       CTL_HW, HW_IOSTATS, CTL_EOL);
391 }
392