subr_iostat.c revision 1.23 1 /* $NetBSD: subr_iostat.c,v 1.23 2017/06/01 02:45:13 chs 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.23 2017/06/01 02:45:13 chs 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 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 * multiply timeval by unsigned integer and add to result
184 */
185 static void
186 timermac(struct timeval *a, uint64_t count, struct timeval *res)
187 {
188 struct timeval part = *a;
189
190 while (count) {
191 if (count & 1)
192 timeradd(res, &part, res);
193 timeradd(&part, &part, &part);
194 count >>= 1;
195 }
196 }
197
198 /*
199 * Increment the iostat wait counter.
200 * Accumulate wait time and timesum.
201 *
202 * Wait time is spent in the device bufq.
203 */
204 void
205 iostat_wait(struct io_stats *stats)
206 {
207 struct timeval dv_time, diff_time;
208 int32_t count;
209
210 KASSERT(stats->io_wait >= 0);
211
212 getmicrouptime(&dv_time);
213
214 timersub(&dv_time, &stats->io_waitstamp, &diff_time);
215 count = stats->io_wait++;
216 if (count != 0) {
217 timermac(&diff_time, count, &stats->io_waitsum);
218 timeradd(&stats->io_waittime, &diff_time, &stats->io_waittime);
219 }
220 stats->io_waitstamp = dv_time;
221 }
222
223 /*
224 * Decrement the iostat wait counter.
225 * Increment the iostat busy counter.
226 * Accumulate wait and busy times and timesums.
227 *
228 * Busy time is spent being processed by the device.
229 *
230 * Old devices do not yet measure wait time, so skip
231 * processing it if the counter is still zero.
232 */
233 void
234 iostat_busy(struct io_stats *stats)
235 {
236 struct timeval dv_time, diff_time;
237 int32_t count;
238
239 KASSERT(stats->io_wait >= 0); /* > 0 when iostat_wait is used */
240 KASSERT(stats->io_busy >= 0);
241
242 getmicrouptime(&dv_time);
243
244 timersub(&dv_time, &stats->io_waitstamp, &diff_time);
245 if (stats->io_wait != 0) {
246 count = stats->io_wait--;
247 timermac(&diff_time, count, &stats->io_waitsum);
248 timeradd(&stats->io_waittime, &diff_time, &stats->io_waittime);
249 }
250 stats->io_waitstamp = dv_time;
251
252 timersub(&dv_time, &stats->io_busystamp, &diff_time);
253 count = stats->io_busy++;
254 if (count != 0) {
255 timermac(&diff_time, count, &stats->io_busysum);
256 timeradd(&stats->io_busytime, &diff_time, &stats->io_busytime);
257 }
258 stats->io_busystamp = dv_time;
259 }
260
261 /*
262 * Decrement the iostat busy counter, increment the byte count.
263 * Accumulate busy time and timesum.
264 */
265 void
266 iostat_unbusy(struct io_stats *stats, long bcount, int read)
267 {
268 struct timeval dv_time, diff_time;
269 int32_t count;
270
271 KASSERT(stats->io_busy > 0);
272
273 getmicrouptime(&dv_time);
274 stats->io_timestamp = dv_time;
275
276 /* any op */
277 timersub(&dv_time, &stats->io_busystamp, &diff_time);
278 count = stats->io_busy--;
279 timermac(&diff_time, count, &stats->io_busysum);
280 timeradd(&stats->io_busytime, &diff_time, &stats->io_busytime);
281 stats->io_busystamp = dv_time;
282
283 if (bcount > 0) {
284 if (read) {
285 stats->io_rbytes += bcount;
286 stats->io_rxfer++;
287 } else {
288 stats->io_wbytes += bcount;
289 stats->io_wxfer++;
290 }
291 }
292 }
293
294 /*
295 * Return non-zero if a device has an I/O request in flight.
296 */
297 bool
298 iostat_isbusy(struct io_stats *stats)
299 {
300
301 return stats->io_busy != 0;
302 }
303
304 /*
305 * Increment the seek counter. This does look almost redundant but it
306 * abstracts the stats gathering.
307 */
308 void
309 iostat_seek(struct io_stats *stats)
310 {
311
312 stats->io_seek++;
313 }
314
315 static int
316 sysctl_hw_disknames(SYSCTLFN_ARGS)
317 {
318
319 return iostati_getnames(1, oldp, oldlenp, newp, namelen);
320 }
321
322 static int
323 sysctl_hw_iostatnames(SYSCTLFN_ARGS)
324 {
325
326 return iostati_getnames(0, oldp, oldlenp, newp, namelen);
327 }
328
329 static int
330 iostati_getnames(int disk_only, char *oldp, size_t *oldlenp, const void *newp,
331 u_int namelen)
332 {
333 char bf[IOSTATNAMELEN + 1];
334 char *where = oldp;
335 struct io_stats *stats;
336 size_t needed, left, slen;
337 int error, first;
338
339 if (newp != NULL)
340 return (EPERM);
341 if (namelen != 0)
342 return (EINVAL);
343
344 first = 1;
345 error = 0;
346 needed = 0;
347 left = *oldlenp;
348
349 rw_enter(&iostatlist_lock, RW_READER);
350 for (stats = TAILQ_FIRST(&iostatlist); stats != NULL;
351 stats = TAILQ_NEXT(stats, io_link)) {
352 if ((disk_only == 1) && (stats->io_type != IOSTAT_DISK))
353 continue;
354
355 if (where == NULL)
356 needed += strlen(stats->io_name) + 1;
357 else {
358 memset(bf, 0, sizeof(bf));
359 if (first) {
360 strncpy(bf, stats->io_name, sizeof(bf));
361 first = 0;
362 } else {
363 bf[0] = ' ';
364 strncpy(bf + 1, stats->io_name,
365 sizeof(bf) - 1);
366 }
367 bf[IOSTATNAMELEN] = '\0';
368 slen = strlen(bf);
369 if (left < slen + 1)
370 break;
371 /* +1 to copy out the trailing NUL byte */
372 error = copyout(bf, where, slen + 1);
373 if (error)
374 break;
375 where += slen;
376 needed += slen;
377 left -= slen;
378 }
379 }
380 rw_exit(&iostatlist_lock);
381 *oldlenp = needed;
382 return (error);
383 }
384
385 static int
386 sysctl_hw_iostats(SYSCTLFN_ARGS)
387 {
388 struct io_sysctl sdrive;
389 struct io_stats *stats;
390 char *where = oldp;
391 size_t tocopy, left;
392 int error;
393
394 if (newp != NULL)
395 return (EPERM);
396
397 /*
398 * The original hw.diskstats call was broken and did not require
399 * the userland to pass in its size of struct disk_sysctl. This
400 * was fixed after NetBSD 1.6 was released.
401 */
402 if (namelen == 0)
403 tocopy = offsetof(struct io_sysctl, busy);
404 else
405 tocopy = name[0];
406
407 if (where == NULL) {
408 *oldlenp = iostat_count * tocopy;
409 return (0);
410 }
411
412 error = 0;
413 left = *oldlenp;
414 memset(&sdrive, 0, sizeof(sdrive));
415 *oldlenp = 0;
416
417 rw_enter(&iostatlist_lock, RW_READER);
418 TAILQ_FOREACH(stats, &iostatlist, io_link) {
419 if (left < tocopy)
420 break;
421
422 strncpy(sdrive.name, stats->io_name, sizeof(sdrive.name));
423 sdrive.attachtime_sec = stats->io_attachtime.tv_sec;
424 sdrive.attachtime_usec = stats->io_attachtime.tv_usec;
425 sdrive.timestamp_sec = stats->io_busystamp.tv_sec;
426 sdrive.timestamp_usec = stats->io_busystamp.tv_usec;
427
428 sdrive.time_sec = stats->io_busytime.tv_sec;
429 sdrive.time_usec = stats->io_busytime.tv_usec;
430
431 sdrive.seek = stats->io_seek;
432
433 sdrive.rxfer = stats->io_rxfer;
434 sdrive.wxfer = stats->io_wxfer;
435 sdrive.xfer = stats->io_rxfer + stats->io_wxfer;
436
437 sdrive.rbytes = stats->io_rbytes;
438 sdrive.wbytes = stats->io_wbytes;
439 sdrive.bytes = stats->io_rbytes + stats->io_wbytes;
440
441 sdrive.wait_sec = stats->io_waittime.tv_sec;
442 sdrive.wait_usec = stats->io_waittime.tv_usec;
443
444 sdrive.time_sec = stats->io_busytime.tv_sec;
445 sdrive.time_usec = stats->io_busytime.tv_usec;
446
447 sdrive.waitsum_sec = stats->io_waitsum.tv_sec;
448 sdrive.waitsum_usec = stats->io_waitsum.tv_usec;
449
450 sdrive.busysum_sec = stats->io_busysum.tv_sec;
451 sdrive.busysum_usec = stats->io_busysum.tv_usec;
452
453 sdrive.busy = stats->io_busy;
454
455 error = copyout(&sdrive, where, min(tocopy, sizeof(sdrive)));
456 if (error)
457 break;
458 where += tocopy;
459 *oldlenp += tocopy;
460 left -= tocopy;
461 }
462 rw_exit(&iostatlist_lock);
463 return (error);
464 }
465
466 static void
467 sysctl_io_stats_setup(struct sysctllog **clog)
468 {
469
470 sysctl_createv(clog, 0, NULL, NULL,
471 CTLFLAG_PERMANENT,
472 CTLTYPE_STRING, "disknames",
473 SYSCTL_DESCR("List of disk drives present"),
474 sysctl_hw_disknames, 0, NULL, 0,
475 CTL_HW, HW_DISKNAMES, CTL_EOL);
476 sysctl_createv(clog, 0, NULL, NULL,
477 CTLFLAG_PERMANENT,
478 CTLTYPE_STRING, "iostatnames",
479 SYSCTL_DESCR("I/O stats are being collected for these"
480 " devices"),
481 sysctl_hw_iostatnames, 0, NULL, 0,
482 CTL_HW, HW_IOSTATNAMES, CTL_EOL);
483 sysctl_createv(clog, 0, NULL, NULL,
484 CTLFLAG_PERMANENT,
485 CTLTYPE_STRUCT, "iostats",
486 SYSCTL_DESCR("Statistics on device I/O operations"),
487 sysctl_hw_iostats, 0, NULL, 0,
488 CTL_HW, HW_IOSTATS, CTL_EOL);
489 }
490