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