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