1 /* 2 * BUFQUEUES.C 3 * 4 * cc -I/usr/src/sys bufqueues.c -o /usr/local/bin/bufqueues -lkvm 5 * 6 * bufqueues 7 * 8 * Output buf(9) queues usages 9 * 10 * Copyright (c) 2015 The DragonFly Project. All rights reserved. 11 * 12 * This code is derived from software contributed to The DragonFly Project 13 * by Matthew Dillon <dillon@backplane.com> 14 * by Antonio Huete Jimenez <tuxillo@quantumachine.net> 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in 24 * the documentation and/or other materials provided with the 25 * distribution. 26 * 3. Neither the name of The DragonFly Project nor the names of its 27 * contributors may be used to endorse or promote products derived 28 * from this software without specific, prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 33 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 34 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 35 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 36 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 38 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 39 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 40 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 */ 43 44 #define _KERNEL_STRUCTURES_ 45 #include <sys/param.h> 46 #include <sys/user.h> 47 #include <sys/buf.h> 48 #include <sys/bio.h> 49 #include <sys/queue.h> 50 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <stddef.h> 55 #include <fcntl.h> 56 #include <kvm.h> 57 #include <nlist.h> 58 #include <getopt.h> 59 #include <math.h> 60 61 struct nlist Nl[] = { 62 { "_bufpcpu" }, 63 { "_ncpus" }, 64 { "_nbuf" }, 65 { NULL } 66 }; 67 68 TAILQ_HEAD(bqueues, buf); 69 70 /* 71 * Buffer queues. 72 */ 73 enum bufq_type { 74 BQUEUE_NONE, /* not on any queue */ 75 BQUEUE_LOCKED, /* locked buffers */ 76 BQUEUE_CLEAN, /* non-B_DELWRI buffers */ 77 BQUEUE_DIRTY, /* B_DELWRI buffers */ 78 BQUEUE_DIRTY_HW, /* B_DELWRI buffers - heavy weight */ 79 BQUEUE_EMPTYKVA, /* empty buffer headers with KVA assignment */ 80 BQUEUE_EMPTY, /* empty buffer headers */ 81 82 BUFFER_QUEUES /* number of buffer queues */ 83 }; 84 85 struct bufpcpu { 86 struct spinlock spin; 87 struct bqueues bufqueues[BUFFER_QUEUES]; 88 } __cachealign; 89 90 int verboseopt; 91 92 static int kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes, int out); 93 static void scan_queues(kvm_t *kd, int cpu, struct bufpcpu *bqp); 94 static void loaddelay(struct timespec *ts, const char *arg); 95 96 static const char *q2s(int queue); 97 98 /* Globals */ 99 int qcounter[BUFFER_QUEUES]; 100 int failcount; 101 int totalcount; 102 int nbuf; 103 104 int 105 main(int ac, char **av) 106 { 107 const char *corefile = NULL; 108 const char *sysfile = NULL; 109 struct bufpcpu bpcpu; 110 struct timespec delay = { 1, 0 }; 111 kvm_t *kd; 112 int count; 113 int ncpus; 114 int ch; 115 int cpu; 116 int q; 117 118 while ((ch = getopt(ac, av, "M:N:v")) != -1) { 119 switch(ch) { 120 case 'v': 121 ++verboseopt; 122 break; 123 case 'M': 124 corefile = optarg; 125 break; 126 case 'N': 127 sysfile = optarg; 128 break; 129 default: 130 fprintf(stderr, "%s [-M core] [-N system]\n", av[0]); 131 exit(1); 132 } 133 } 134 ac -= optind; 135 av += optind; 136 137 if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) { 138 perror("kvm_open"); 139 exit(1); 140 } 141 if (kvm_nlist(kd, Nl) != 0) { 142 perror("kvm_nlist"); 143 exit(1); 144 } 145 146 if (ac > 0) 147 loaddelay(&delay, av[0]); 148 149 kkread(kd, Nl[1].n_value, &ncpus, sizeof(ncpus), 1); 150 kkread(kd, Nl[2].n_value, &nbuf, sizeof(nbuf), 1); 151 152 for (count = 0; ; ++count) { 153 for (cpu = 0; cpu < ncpus; cpu++) { 154 kkread(kd, Nl[0].n_value + cpu * sizeof(struct bufpcpu), 155 &bpcpu, sizeof(struct bufpcpu), 1); 156 scan_queues(kd, cpu, &bpcpu); 157 } 158 159 if (count && !verboseopt) { 160 if ((count & 15) == 1) 161 printf(" NONE LOCKED CLEAN DIRTY " 162 "DIRTY_HW EMPTYKVA EMPTY OFF-QUEUE KVMFAIL\n"); 163 printf("%6d %7d %6d %6d %9d %9d %6d %10d %7d\n", 164 qcounter[0], qcounter[1], qcounter[2], 165 qcounter[3], qcounter[4], qcounter[5], 166 qcounter[6], (nbuf - totalcount), failcount); 167 } 168 169 /* If in verbose mode only output detailed bufs info once */ 170 if (verboseopt) 171 break; 172 nanosleep(&delay, NULL); 173 bzero(&qcounter, sizeof(qcounter)); 174 totalcount = 0; 175 failcount = 0; 176 } 177 return(0); 178 } 179 180 static const char *q2s(int queue) 181 { 182 switch(queue) { 183 case BQUEUE_NONE: 184 return "NONE"; 185 case BQUEUE_LOCKED: 186 return "LOCKED"; 187 case BQUEUE_CLEAN: 188 return "CLEAN"; 189 case BQUEUE_DIRTY: 190 return "DIRTY"; 191 case BQUEUE_DIRTY_HW: 192 return "DIRTY_HW"; 193 case BQUEUE_EMPTYKVA: 194 return "EMPTYKVA"; 195 case BQUEUE_EMPTY: 196 return "EMPTY"; 197 default: 198 return "INVALID"; 199 } 200 } 201 202 void 203 scan_queues(kvm_t *kd, int cpu, struct bufpcpu *bqp) 204 { 205 struct buf b, *tmp; 206 int q; 207 208 for (q = 0; q < BUFFER_QUEUES; q++) { 209 if (bqp->bufqueues[q].tqh_first == NULL) 210 continue; 211 kkread(kd, (u_long)bqp->bufqueues[q].tqh_first, &b, sizeof(b), 1); 212 tmp = bqp->bufqueues[q].tqh_first; 213 if (tmp != NULL) 214 qcounter[q]++; 215 while (tmp != NULL) { 216 if (verboseopt) 217 printf("cpu=%d queue=%8s buf=%p", cpu, q2s(q), tmp); 218 tmp = b.b_freelist.tqe_next; 219 if (kkread(kd, (u_long)tmp, &b, sizeof(b), 0) == -1) { 220 if (verboseopt) 221 printf(" [F] "); 222 failcount++; 223 } 224 if (verboseopt) 225 printf("\n"); 226 qcounter[q]++; 227 totalcount++; /* All scanned bufs */ 228 } 229 } 230 } 231 232 /* 233 * Convert a delay string (e.g. "0.1") into a timespec. 234 */ 235 static 236 void 237 loaddelay(struct timespec *ts, const char *arg) 238 { 239 double d; 240 241 d = strtod(arg, NULL); 242 if (d < 0.001) 243 d = 0.001; 244 ts->tv_sec = (int)d; 245 ts->tv_nsec = (int)(modf(d, &d) * 1000000000.0); 246 } 247 248 static int 249 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes, int out) 250 { 251 if (kvm_read(kd, addr, buf, nbytes) != nbytes) { 252 if (out) { 253 perror("kvm_read"); 254 exit(1); 255 } 256 return -1; 257 } 258 return 0; 259 } 260