1 /* 2 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/kernel.h> 33 #include <sys/systm.h> 34 #include <sys/bio.h> 35 #include <sys/sysctl.h> 36 37 #include <sys/devicestat.h> 38 39 static int devstat_num_devs; 40 static long devstat_generation; 41 static int devstat_version = DEVSTAT_VERSION; 42 static int devstat_current_devnumber; 43 44 static struct devstatlist device_statq; 45 46 /* 47 * Take a malloced and zeroed devstat structure given to us, fill it in 48 * and add it to the queue of devices. 49 */ 50 void 51 devstat_add_entry(struct devstat *ds, const char *dev_name, 52 int unit_number, u_int32_t block_size, 53 devstat_support_flags flags, 54 devstat_type_flags device_type, 55 devstat_priority priority) 56 { 57 struct devstatlist *devstat_head; 58 struct devstat *ds_tmp; 59 60 if (ds == NULL) 61 return; 62 63 if (devstat_num_devs == 0) 64 STAILQ_INIT(&device_statq); 65 66 devstat_generation++; 67 devstat_num_devs++; 68 69 devstat_head = &device_statq; 70 71 /* 72 * Priority sort. Each driver passes in its priority when it adds 73 * its devstat entry. Drivers are sorted first by priority, and 74 * then by probe order. 75 * 76 * For the first device, we just insert it, since the priority 77 * doesn't really matter yet. Subsequent devices are inserted into 78 * the list using the order outlined above. 79 */ 80 if (devstat_num_devs == 1) 81 STAILQ_INSERT_TAIL(devstat_head, ds, dev_links); 82 else { 83 STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) { 84 struct devstat *ds_next; 85 86 ds_next = STAILQ_NEXT(ds_tmp, dev_links); 87 88 /* 89 * If we find a break between higher and lower 90 * priority items, and if this item fits in the 91 * break, insert it. This also applies if the 92 * "lower priority item" is the end of the list. 93 */ 94 if ((priority <= ds_tmp->priority) 95 && ((ds_next == NULL) 96 || (priority > ds_next->priority))) { 97 STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds, 98 dev_links); 99 break; 100 } else if (priority > ds_tmp->priority) { 101 /* 102 * If this is the case, we should be able 103 * to insert ourselves at the head of the 104 * list. If we can't, something is wrong. 105 */ 106 if (ds_tmp == STAILQ_FIRST(devstat_head)) { 107 STAILQ_INSERT_HEAD(devstat_head, 108 ds, dev_links); 109 break; 110 } else { 111 STAILQ_INSERT_TAIL(devstat_head, 112 ds, dev_links); 113 printf("devstat_add_entry: HELP! " 114 "sorting problem detected " 115 "for %s%d\n", dev_name, 116 unit_number); 117 break; 118 } 119 } 120 } 121 } 122 123 ds->device_number = devstat_current_devnumber++; 124 ds->unit_number = unit_number; 125 strncpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN); 126 ds->device_name[DEVSTAT_NAME_LEN - 1] = '\0'; 127 ds->block_size = block_size; 128 ds->flags = flags; 129 ds->device_type = device_type; 130 ds->priority = priority; 131 getmicrotime(&ds->dev_creation_time); 132 } 133 134 /* 135 * Remove a devstat structure from the list of devices. 136 */ 137 void 138 devstat_remove_entry(struct devstat *ds) 139 { 140 struct devstatlist *devstat_head; 141 142 if (ds == NULL) 143 return; 144 145 devstat_generation++; 146 devstat_num_devs--; 147 148 devstat_head = &device_statq; 149 150 /* Remove this entry from the devstat queue */ 151 STAILQ_REMOVE(devstat_head, ds, devstat, dev_links); 152 } 153 154 /* 155 * Record a transaction start. 156 */ 157 void 158 devstat_start_transaction(struct devstat *ds) 159 { 160 /* sanity check */ 161 if (ds == NULL) 162 return; 163 164 /* 165 * We only want to set the start time when we are going from idle 166 * to busy. The start time is really the start of the latest busy 167 * period. 168 */ 169 if (ds->busy_count == 0) 170 getmicrouptime(&ds->start_time); 171 ds->busy_count++; 172 } 173 174 /* 175 * Record the ending of a transaction, and incrment the various counters. 176 */ 177 void 178 devstat_end_transaction(struct devstat *ds, u_int32_t bytes, 179 devstat_tag_type tag_type, devstat_trans_flags flags) 180 { 181 struct timeval busy_time; 182 183 /* sanity check */ 184 if (ds == NULL) 185 return; 186 187 getmicrouptime(&ds->last_comp_time); 188 ds->busy_count--; 189 190 /* 191 * There might be some transactions (DEVSTAT_NO_DATA) that don't 192 * transfer any data. 193 */ 194 if (flags == DEVSTAT_READ) { 195 ds->bytes_read += bytes; 196 ds->num_reads++; 197 } else if (flags == DEVSTAT_WRITE) { 198 ds->bytes_written += bytes; 199 ds->num_writes++; 200 } else if (flags == DEVSTAT_FREE) { 201 ds->bytes_freed += bytes; 202 ds->num_frees++; 203 } else 204 ds->num_other++; 205 206 /* 207 * Keep a count of the various tag types sent. 208 */ 209 if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 && 210 tag_type != DEVSTAT_TAG_NONE) 211 ds->tag_types[tag_type]++; 212 213 /* 214 * We only update the busy time when we go idle. Otherwise, this 215 * calculation would require many more clock cycles. 216 */ 217 if (ds->busy_count == 0) { 218 /* Calculate how long we were busy */ 219 busy_time = ds->last_comp_time; 220 timevalsub(&busy_time, &ds->start_time); 221 222 /* Add our busy time to the total busy time. */ 223 timevaladd(&ds->busy_time, &busy_time); 224 } else if (ds->busy_count < 0) 225 printf("devstat_end_transaction: HELP!! busy_count " 226 "for %s%d is < 0 (%d)!\n", ds->device_name, 227 ds->unit_number, ds->busy_count); 228 } 229 230 void 231 devstat_end_transaction_bio(struct devstat *ds, struct bio *bp) 232 { 233 devstat_trans_flags flg; 234 235 if (bp->bio_cmd == BIO_DELETE) 236 flg = DEVSTAT_FREE; 237 else if (bp->bio_cmd == BIO_READ) 238 flg = DEVSTAT_READ; 239 else 240 flg = DEVSTAT_WRITE; 241 242 devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid, 243 (bp->bio_flags & BIO_ORDERED) ? 244 DEVSTAT_TAG_ORDERED : DEVSTAT_TAG_SIMPLE, flg); 245 } 246 247 /* 248 * This is the sysctl handler for the devstat package. The data pushed out 249 * on the kern.devstat.all sysctl variable consists of the current devstat 250 * generation number, and then an array of devstat structures, one for each 251 * device in the system. 252 * 253 * I'm really not too fond of this method of doing things, but there really 254 * aren't that many alternatives. We must have some method of making sure 255 * that the generation number the user gets corresponds with the data the 256 * user gets. If the user makes a separate sysctl call to get the 257 * generation, and then a sysctl call to get the device statistics, the 258 * device list could have changed in that brief period of time. By 259 * supplying the generation number along with the statistics output, we can 260 * guarantee that the generation number and the statistics match up. 261 */ 262 static int 263 sysctl_devstat(SYSCTL_HANDLER_ARGS) 264 { 265 int error, i; 266 struct devstat *nds; 267 struct devstatlist *devstat_head; 268 269 if (devstat_num_devs == 0) 270 return(EINVAL); 271 272 error = 0; 273 devstat_head = &device_statq; 274 275 /* 276 * First push out the generation number. 277 */ 278 error = SYSCTL_OUT(req, &devstat_generation, sizeof(long)); 279 280 /* 281 * Now push out all the devices. 282 */ 283 for (i = 0, nds = STAILQ_FIRST(devstat_head); 284 (nds != NULL) && (i < devstat_num_devs) && (error == 0); 285 nds = STAILQ_NEXT(nds, dev_links), i++) 286 error = SYSCTL_OUT(req, nds, sizeof(struct devstat)); 287 288 return(error); 289 } 290 291 /* 292 * Sysctl entries for devstat. The first one is a node that all the rest 293 * hang off of. 294 */ 295 SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, 0, "Device Statistics"); 296 297 SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE, 298 0, 0, sysctl_devstat, "S,devstat", "All devices in the devstat list"); 299 /* 300 * Export the number of devices in the system so that userland utilities 301 * can determine how much memory to allocate to hold all the devices. 302 */ 303 SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD, 304 &devstat_num_devs, 0, "Number of devices in the devstat list"); 305 SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD, 306 &devstat_generation, 0, "Devstat list generation"); 307 SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD, 308 &devstat_version, 0, "Devstat list version number"); 309