Merge branch 'vendor/OPENRESOLV' with the following changes:
[dragonfly.git] / test / debug / bufqueues.c
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
117     while ((ch = getopt(ac, av, "M:N:v")) != -1) {
118         switch(ch) {
119         case 'v':
120             ++verboseopt;
121             break;
122         case 'M':
123             corefile = optarg;
124             break;
125         case 'N':
126             sysfile = optarg;
127             break;
128         default:
129             fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
130             exit(1);
131         }
132     }
133     ac -= optind;
134     av += optind;
135
136     if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
137         perror("kvm_open");
138         exit(1);
139     }
140     if (kvm_nlist(kd, Nl) != 0) {
141         perror("kvm_nlist");
142         exit(1);
143     }
144
145     if (ac > 0)
146             loaddelay(&delay, av[0]);
147
148     kkread(kd, Nl[1].n_value, &ncpus, sizeof(ncpus), 1);
149     kkread(kd, Nl[2].n_value, &nbuf, sizeof(nbuf), 1);
150
151     for (count = 0; ; ++count) {
152             for (cpu = 0; cpu < ncpus; cpu++) {
153                     kkread(kd, Nl[0].n_value + cpu * sizeof(struct bufpcpu),
154                         &bpcpu, sizeof(struct bufpcpu), 1);
155                     scan_queues(kd, cpu, &bpcpu);
156             }
157
158             if (count && !verboseopt) {
159                     if ((count & 15) == 1)
160                             printf("  NONE  LOCKED  CLEAN  DIRTY  "
161                                 "DIRTY_HW  EMPTYKVA  EMPTY  OFF-QUEUE KVMFAIL\n");
162                     printf("%6d %7d %6d %6d %9d %9d %6d %10d %7d\n",
163                         qcounter[0], qcounter[1], qcounter[2],
164                         qcounter[3], qcounter[4], qcounter[5],
165                         qcounter[6], (nbuf - totalcount), failcount);
166             }
167
168             /* If in verbose mode only output detailed bufs info once */
169             if (verboseopt)
170                     break;
171             nanosleep(&delay, NULL);
172             bzero(&qcounter, sizeof(qcounter));
173             totalcount = 0;
174             failcount = 0;
175     }
176     return(0);
177 }
178
179 static const char *q2s(int queue)
180 {
181         switch(queue) {
182         case BQUEUE_NONE:
183                 return "NONE";
184         case BQUEUE_LOCKED:
185                 return "LOCKED";
186         case BQUEUE_CLEAN:
187                 return "CLEAN";
188         case BQUEUE_DIRTY:
189                 return "DIRTY";
190         case BQUEUE_DIRTY_HW:
191                 return "DIRTY_HW";
192         case BQUEUE_EMPTYKVA:
193                 return "EMPTYKVA";
194         case BQUEUE_EMPTY:
195                 return "EMPTY";
196         default:
197                 return "INVALID";
198         }
199 }
200
201 void
202 scan_queues(kvm_t *kd, int cpu, struct bufpcpu *bqp)
203 {
204         struct buf b, *tmp;
205         int q;
206
207         for (q = 0; q < BUFFER_QUEUES; q++) {
208                 if (bqp->bufqueues[q].tqh_first == NULL)
209                         continue;
210                 kkread(kd, (u_long)bqp->bufqueues[q].tqh_first, &b, sizeof(b), 1);
211                 tmp = bqp->bufqueues[q].tqh_first;
212                 if (tmp != NULL)
213                         qcounter[q]++;
214                 while (tmp != NULL) {
215                         if (verboseopt)
216                                 printf("cpu=%d queue=%8s buf=%p", cpu, q2s(q), tmp);
217                         tmp = b.b_freelist.tqe_next;
218                         if (kkread(kd, (u_long)tmp, &b, sizeof(b), 0) == -1) {
219                                 if (verboseopt)
220                                         printf(" [F] ");
221                                 failcount++;
222                         }
223                         if (verboseopt)
224                                 printf("\n");
225                         qcounter[q]++;
226                         totalcount++;   /* All scanned bufs */
227                 }
228         }
229 }
230
231 /*
232  * Convert a delay string (e.g. "0.1") into a timespec.
233  */
234 static
235 void
236 loaddelay(struct timespec *ts, const char *arg)
237 {
238         double d;
239
240         d = strtod(arg, NULL);
241         if (d < 0.001)
242                 d = 0.001;
243         ts->tv_sec = (int)d;
244         ts->tv_nsec = (int)(modf(d, &d) * 1000000000.0);
245 }
246
247 static int
248 kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes, int out)
249 {
250     if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
251             if (out) {
252                     perror("kvm_read");
253                     exit(1);
254             }
255             return -1;
256     }
257     return 0;
258 }