Initial import from FreeBSD RELENG_4:
[dragonfly.git] / tools / diag / dumpvfscache / dumpvfscache.c
1 /* $FreeBSD: src/tools/diag/dumpvfscache/dumpvfscache.c,v 1.1.6.1 2001/03/05 12:17:00 kris Exp $ */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <fcntl.h>
6 #include <kvm.h>
7 #include <nlist.h>
8 #include <sys/uio.h>
9 #include <sys/namei.h>
10 #include <sys/param.h>
11 #include <sys/time.h>
12 #include <sys/vnode.h>
13 /*----------------------------------*/
14 static u_int crc16_table[16] = { 
15     0x0000, 0xCC01, 0xD801, 0x1400,
16     0xF001, 0x3C00, 0x2800, 0xE401,
17     0xA001, 0x6C00, 0x7800, 0xB401,
18     0x5000, 0x9C01, 0x8801, 0x4400 
19 };
20
21 static u_short
22 wlpsacrc(u_char *buf, u_int len)
23 {
24     u_short     crc = 0;
25     int         i, r1;
26     
27     for (i = 0; i < len; i++, buf++) {
28         /* lower 4 bits */
29         r1 = crc16_table[crc & 0xF];
30         crc = (crc >> 4) & 0x0FFF;
31         crc = crc ^ r1 ^ crc16_table[*buf & 0xF];
32         
33         /* upper 4 bits */
34         r1 = crc16_table[crc & 0xF];
35         crc = (crc >> 4) & 0x0FFF;
36         crc = crc ^ r1 ^ crc16_table[(*buf >> 4) & 0xF];
37     }
38     return(crc);
39 }
40
41 /*----------------------------------*/
42 struct nlist nl[] = {
43         { "_nchash", 0},
44         { "_nchashtbl", 0},
45         { 0, 0 },
46 };
47
48 int histo[2047];
49 int histn[2047];
50 int *newbucket;
51
52 int
53 main(int argc, char **argv)
54 {
55         int nchash, i, j, k, kn;
56         int nb, p1, p2;
57         u_long p;
58         LIST_HEAD(nchashhead, namecache) *nchashtbl;
59         struct namecache *nc;
60         struct vnode vn;
61
62         kvm_t *kvm = kvm_open(0, 0, 0, O_RDONLY, 0);
63
64         printf("kvm: %p\n", kvm);
65         printf("kvm_nlist: %d\n", kvm_nlist(kvm, nl));
66         kvm_read(kvm, nl[0].n_value, &nchash, sizeof nchash);
67         nchash++;
68         nchashtbl = malloc(nchash * sizeof *nchashtbl);
69         nc = malloc(sizeof *nc + 400);
70         newbucket = malloc(nchash * sizeof (int));
71         memset(newbucket, 0, nchash * sizeof (int));
72         kvm_read(kvm, nl[1].n_value, &p, sizeof p);
73         kvm_read(kvm, p, nchashtbl, nchash * sizeof *nchashtbl);
74         for (i=0; i < nchash; i++) {
75 #if 0
76                 printf("%d\n", i);
77 #endif
78                 nb=0;
79                 p = (u_long)LIST_FIRST(nchashtbl+i);
80                 while (p) {
81                         nb++;
82                         kvm_read(kvm, p, nc, sizeof *nc + 400);
83                         kvm_read(kvm, (u_long)nc->nc_dvp, &vn, sizeof vn);
84                         nc->nc_name[nc->nc_nlen] = '\0';
85                         for (j=k=kn=0;nc->nc_name[j];j++) {
86                                 k+= nc->nc_name[j];
87                                 kn <<= 1;
88                                 kn+= nc->nc_name[j];
89                         }
90                         /*
91                         kn = k;
92                         */
93                         kn = wlpsacrc(nc->nc_name,nc->nc_nlen);
94
95                         /* kn += (u_long)vn.v_data >> 8;  */
96                         /* kn += (u_long)nc->nc_dvp >> 7;    */
97                         kn += vn.v_id; 
98                         kn &= (nchash - 1);
99                         newbucket[kn]++;
100 #if 1
101                         printf("%4d  dvp %08x  hash %08x  vp %08x  id %08x  name <%s>\n",
102                                 i,nc->nc_dvp, k, nc->nc_vp, vn.v_id, nc->nc_name);
103 #endif
104                         p = (u_long)LIST_NEXT(nc, nc_hash);
105                 }
106                 histo[nb]++;
107         }
108         for (i=0; i < nchash; i++) {
109                 histn[newbucket[i]]++;
110         }
111         p1=p2 = 0;
112         for (i=0;i<30;i++) {
113                 p1 += histo[i] * i;
114                 p2 += histn[i] * i;
115                 if (histo[i] || histn[i])
116                         printf("H%02d %4d %4d / %4d %4d\n",i,histo[i], p1 , histn[i], p2);
117         }
118                 
119         return (0);
120 }
121