1/* $NetBSD: subr_iostat.c,v 1.21 2014/10/18 08:33:29 snj 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.21 2014/10/18 08:33:29 snj 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 */
83static int sysctl_hw_disknames(SYSCTLFN_PROTO);
84static int sysctl_hw_iostatnames(SYSCTLFN_PROTO);
85static int sysctl_hw_iostats(SYSCTLFN_PROTO);
86
87static int
88iostati_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 */
95struct iostatlist_head iostatlist = TAILQ_HEAD_INITIALIZER(iostatlist);
96int iostat_count; /* number of drives in global drivelist */
97krwlock_t iostatlist_lock;
98
99static void sysctl_io_stats_setup(struct sysctllog **);
100
101/*
102 * Initialise the iostat subsystem.
103 */
104void
105iostat_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 */
116struct io_stats *
117iostat_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 */
137struct io_stats *
138iostat_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 */
169void
170iostat_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 * Increment a iostat busy counter. If the counter is going from
187 * 0 to 1, set the timestamp.
188 */
189void
190iostat_busy(struct io_stats *stats)
191{
192
193 if (stats->io_busy++ == 0)
194 getmicrouptime(&stats->io_timestamp);
195}
196
197/*
198 * Decrement a iostat busy counter, increment the byte count, total busy
199 * time, and reset the timestamp.
200 */
201void
202iostat_unbusy(struct io_stats *stats, long bcount, int read)
203{
204 struct timeval dv_time, diff_time;
205
206 if (stats->io_busy-- == 0) {
207 printf("%s: busy < 0\n", stats->io_name);
208 panic("iostat_unbusy");
209 }
210
211 getmicrouptime(&dv_time);
212
213 timersub(&dv_time, &stats->io_timestamp, &diff_time);
214 timeradd(&stats->io_time, &diff_time, &stats->io_time);
215
216 stats->io_timestamp = dv_time;
217 if (bcount > 0) {
218 if (read) {
219 stats->io_rbytes += bcount;
220 stats->io_rxfer++;
221 } else {
222 stats->io_wbytes += bcount;
223 stats->io_wxfer++;
224 }
225 }
226}
227
228/*
229 * Return non-zero if a device has an I/O request in flight.
230 */
231bool
232iostat_isbusy(struct io_stats *stats)
233{
234
235 return stats->io_busy != 0;
236}
237
238/*
239 * Increment the seek counter. This does look almost redundant but it
240 * abstracts the stats gathering.
241 */
242void
243iostat_seek(struct io_stats *stats)
244{
245
246 stats->io_seek++;
247}
248
249static int
250sysctl_hw_disknames(SYSCTLFN_ARGS)
251{
252
253 return iostati_getnames(1, oldp, oldlenp, newp, namelen);
254}
255
256static int
257sysctl_hw_iostatnames(SYSCTLFN_ARGS)
258{
259
260 return iostati_getnames(0, oldp, oldlenp, newp, namelen);
261}
262
263static int
264iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
265 u_int namelen)
266{
267 char bf[IOSTATNAMELEN + 1];
268 char *where = oldp;
269 struct io_stats *stats;
270 size_t needed, left, slen;
271 int error, first;
272
273 if (newp != NULL)
274 return (EPERM);
275 if (namelen != 0)
276 return (EINVAL);
277
278 first = 1;
279 error = 0;
280 needed = 0;
281 left = *oldlenp;
282
283 rw_enter(&iostatlist_lock, RW_READER);
284 for (stats = TAILQ_FIRST(&iostatlist); stats != NULL;
285 stats = TAILQ_NEXT(stats, io_link)) {
286 if ((disk_only == 1) && (stats->io_type != IOSTAT_DISK))
287 continue;
288
289 if (where == NULL)
290 needed += strlen(stats->io_name) + 1;
291 else {
292 memset(bf, 0, sizeof(bf));
293 if (first) {
294 strncpy(bf, stats->io_name, sizeof(bf));
295 first = 0;
296 } else {
297 bf[0] = ' ';
298 strncpy(bf + 1, stats->io_name,
299 sizeof(bf) - 1);
300 }
301 bf[IOSTATNAMELEN] = '\0';
302 slen = strlen(bf);
303 if (left < slen + 1)
304 break;
305 /* +1 to copy out the trailing NUL byte */
306 error = copyout(bf, where, slen + 1);
307 if (error)
308 break;
309 where += slen;
310 needed += slen;
311 left -= slen;
312 }
313 }
314 rw_exit(&iostatlist_lock);
315 *oldlenp = needed;
316 return (error);
317}
318
319static int
320sysctl_hw_iostats(SYSCTLFN_ARGS)
321{
322 struct io_sysctl sdrive;
323 struct io_stats *stats;
324 char *where = oldp;
325 size_t tocopy, left;
326 int error;
327
328 if (newp != NULL)
329 return (EPERM);
330
331 /*
332 * The original hw.diskstats call was broken and did not require
333 * the userland to pass in its size of struct disk_sysctl. This
334 * was fixed after NetBSD 1.6 was released.
335 */
336 if (namelen == 0)
337 tocopy = offsetof(struct io_sysctl, busy);
338 else
339 tocopy = name[0];
340
341 if (where == NULL) {
342 *oldlenp = iostat_count * tocopy;
343 return (0);
344 }
345
346 error = 0;
347 left = *oldlenp;
348 memset(&sdrive, 0, sizeof(sdrive));
349 *oldlenp = 0;
350
351 rw_enter(&iostatlist_lock, RW_READER);
352 TAILQ_FOREACH(stats, &iostatlist, io_link) {
353 if (left < tocopy)
354 break;
355 strncpy(sdrive.name, stats->io_name, sizeof(sdrive.name));
356 sdrive.xfer = stats->io_rxfer + stats->io_wxfer;
357 sdrive.rxfer = stats->io_rxfer;
358 sdrive.wxfer = stats->io_wxfer;
359 sdrive.seek = stats->io_seek;
360 sdrive.bytes = stats->io_rbytes + stats->io_wbytes;
361 sdrive.rbytes = stats->io_rbytes;
362 sdrive.wbytes = stats->io_wbytes;
363 sdrive.attachtime_sec = stats->io_attachtime.tv_sec;
364 sdrive.attachtime_usec = stats->io_attachtime.tv_usec;
365 sdrive.timestamp_sec = stats->io_timestamp.tv_sec;
366 sdrive.timestamp_usec = stats->io_timestamp.tv_usec;
367 sdrive.time_sec = stats->io_time.tv_sec;
368 sdrive.time_usec = stats->io_time.tv_usec;
369 sdrive.busy = stats->io_busy;
370
371 error = copyout(&sdrive, where, min(tocopy, sizeof(sdrive)));
372 if (error)
373 break;
374 where += tocopy;
375 *oldlenp += tocopy;
376 left -= tocopy;
377 }
378 rw_exit(&iostatlist_lock);
379 return (error);
380}
381
382static void
383sysctl_io_stats_setup(struct sysctllog **clog)
384{
385
386 sysctl_createv(clog, 0, NULL, NULL,
387 CTLFLAG_PERMANENT,
388 CTLTYPE_STRING, "disknames",
389 SYSCTL_DESCR("List of disk drives present"),
390 sysctl_hw_disknames, 0, NULL, 0,
391 CTL_HW, HW_DISKNAMES, CTL_EOL);
392 sysctl_createv(clog, 0, NULL, NULL,
393 CTLFLAG_PERMANENT,
394 CTLTYPE_STRING, "iostatnames",
395 SYSCTL_DESCR("I/O stats are being collected for these"
396 " devices"),
397 sysctl_hw_iostatnames, 0, NULL, 0,
398 CTL_HW, HW_IOSTATNAMES, CTL_EOL);
399 sysctl_createv(clog, 0, NULL, NULL,
400 CTLFLAG_PERMANENT,
401 CTLTYPE_STRUCT, "iostats",
402 SYSCTL_DESCR("Statistics on device I/O operations"),
403 sysctl_hw_iostats, 0, NULL, 0,
404 CTL_HW, HW_IOSTATS, CTL_EOL);
405}
406