1 a0d146ed 2005-07-12 devnull #include "stdinc.h"
2 a0d146ed 2005-07-12 devnull #include "dat.h"
3 a0d146ed 2005-07-12 devnull #include "fns.h"
6 a0d146ed 2005-07-12 devnull usage(void)
8 a0d146ed 2005-07-12 devnull fprint(2, "usage: fmtindex [-a] config\n");
9 a0d146ed 2005-07-12 devnull threadexitsall(0);
13 a0d146ed 2005-07-12 devnull threadmain(int argc, char *argv[])
15 a0d146ed 2005-07-12 devnull Config conf;
16 a0d146ed 2005-07-12 devnull Index *ix;
17 a0d146ed 2005-07-12 devnull ArenaPart *ap;
18 a0d146ed 2005-07-12 devnull Arena **arenas;
19 a0d146ed 2005-07-12 devnull AMap *amap;
20 a0d146ed 2005-07-12 devnull u64int addr;
21 a0d146ed 2005-07-12 devnull char *file;
22 a0d146ed 2005-07-12 devnull u32int i, j, n, narenas;
25 a0d146ed 2005-07-12 devnull ventifmtinstall();
26 a0d146ed 2005-07-12 devnull statsinit();
29 a0d146ed 2005-07-12 devnull ARGBEGIN{
30 a0d146ed 2005-07-12 devnull case 'a':
38 a0d146ed 2005-07-12 devnull if(argc != 1)
41 a0d146ed 2005-07-12 devnull file = argv[0];
43 a0d146ed 2005-07-12 devnull if(runconfig(file, &conf) < 0)
44 a0d146ed 2005-07-12 devnull sysfatal("can't initialize config %s: %r", file);
45 a0d146ed 2005-07-12 devnull if(conf.index == nil)
46 a0d146ed 2005-07-12 devnull sysfatal("no index specified in %s", file);
47 a0d146ed 2005-07-12 devnull if(nameok(conf.index) < 0)
48 a0d146ed 2005-07-12 devnull sysfatal("illegal index name %s", conf.index);
50 a0d146ed 2005-07-12 devnull narenas = 0;
51 a0d146ed 2005-07-12 devnull for(i = 0; i < conf.naparts; i++){
52 a0d146ed 2005-07-12 devnull ap = conf.aparts[i];
53 a0d146ed 2005-07-12 devnull narenas += ap->narenas;
57 a0d146ed 2005-07-12 devnull ix = initindex(conf.index, conf.sects, conf.nsects);
58 a0d146ed 2005-07-12 devnull if(ix == nil)
59 a0d146ed 2005-07-12 devnull sysfatal("can't initialize index %s: %r", conf.index);
61 a0d146ed 2005-07-12 devnull ix = newindex(conf.index, conf.sects, conf.nsects);
62 a0d146ed 2005-07-12 devnull if(ix == nil)
63 a0d146ed 2005-07-12 devnull sysfatal("can't create new index %s: %r", conf.index);
66 a0d146ed 2005-07-12 devnull for(i = 0; i < ix->nsects; i++)
67 a0d146ed 2005-07-12 devnull n += ix->sects[i]->blocks;
69 a0d146ed 2005-07-12 devnull if(0) fprint(2, "using %ud buckets of %ud; div=%d\n", ix->buckets, n, ix->div);
71 a0d146ed 2005-07-12 devnull amap = MKNZ(AMap, narenas);
72 a0d146ed 2005-07-12 devnull arenas = MKNZ(Arena*, narenas);
74 a0d146ed 2005-07-12 devnull addr = IndexBase;
76 a0d146ed 2005-07-12 devnull for(i = 0; i < conf.naparts; i++){
77 a0d146ed 2005-07-12 devnull ap = conf.aparts[i];
78 a0d146ed 2005-07-12 devnull for(j = 0; j < ap->narenas; j++){
79 a0d146ed 2005-07-12 devnull if(n >= narenas)
80 a0d146ed 2005-07-12 devnull sysfatal("too few slots in index's arena set");
82 a0d146ed 2005-07-12 devnull arenas[n] = ap->arenas[j];
83 a0d146ed 2005-07-12 devnull if(n < ix->narenas){
84 a0d146ed 2005-07-12 devnull if(arenas[n] != ix->arenas[n])
85 a0d146ed 2005-07-12 devnull sysfatal("mismatched arenas %s and %s at slot %d\n",
86 a0d146ed 2005-07-12 devnull arenas[n]->name, ix->arenas[n]->name, n);
87 a0d146ed 2005-07-12 devnull amap[n] = ix->amap[n];
88 a0d146ed 2005-07-12 devnull if(amap[n].start != addr)
89 a0d146ed 2005-07-12 devnull sysfatal("mis-located arena %s in index %s\n", arenas[n]->name, ix->name);
90 a0d146ed 2005-07-12 devnull addr = amap[n].stop;
92 a0d146ed 2005-07-12 devnull amap[n].start = addr;
93 a0d146ed 2005-07-12 devnull addr += ap->arenas[j]->size;
94 a0d146ed 2005-07-12 devnull amap[n].stop = addr;
95 a0d146ed 2005-07-12 devnull namecp(amap[n].name, ap->arenas[j]->name);
96 a0d146ed 2005-07-12 devnull if(0) fprint(2, "add arena %s at [%lld,%lld)\n",
97 a0d146ed 2005-07-12 devnull amap[n].name, amap[n].start, amap[n].stop);
104 a0d146ed 2005-07-12 devnull fprint(2, "configured index=%s with arenas=%d and storage=%lld\n",
105 a0d146ed 2005-07-12 devnull ix->name, n, addr - IndexBase);
106 a0d146ed 2005-07-12 devnull fprint(2, "\tbitblocks=%d maxdepth=%d buckets=%d\n",
107 a0d146ed 2005-07-12 devnull ix->bitblocks, ix->maxdepth, ix->buckets);
109 a0d146ed 2005-07-12 devnull fprint(2, "fmtindex: %,d arenas, %,d index buckets, %,lld bytes storage\n",
110 a0d146ed 2005-07-12 devnull n, ix->buckets, addr-IndexBase);
112 a0d146ed 2005-07-12 devnull ix->amap = amap;
113 a0d146ed 2005-07-12 devnull ix->arenas = arenas;
114 a0d146ed 2005-07-12 devnull ix->narenas = narenas;
116 a0d146ed 2005-07-12 devnull if(wbindex(ix) < 0)
117 a0d146ed 2005-07-12 devnull fprint(2, "can't write back arena partition header for %s: %r\n", file);
119 a0d146ed 2005-07-12 devnull threadexitsall(0);