5001186938e671bce5428b6d76e20eb9805a98a4
[idzebra-moved-to-github.git] / index / invstat.c
1 /* $Id: invstat.c,v 1.39 2004-08-06 13:14:46 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27
28 #include "index.h"
29
30 struct inv_stat_info {
31     ZebraHandle zh;
32     zint no_isam_entries[9];
33     int no_dict_entries;
34     int no_dict_bytes;
35     int isam_bounds[20];
36     int isam_occurrences[20];
37     char tmp[128];
38     int isamb_levels[10][5];
39     int isamb_sizes[10];
40     int isamb_blocks[10];
41     unsigned long cksum;
42     int dumpwords;
43 };
44
45 #define SINGLETON_TYPE 8 /* the type to use for singletons that */ 
46                          /* have no block and no block type */
47
48 static void print_dict_item (ZebraMaps zm, const char *s, zint count,
49             int firstsys, int firstseq, int lastsys, int lastseq )
50 {
51     int reg_type = s[1];
52     char keybuf[IT_MAX_WORD+1];
53     char *to = keybuf;
54     const char *from = s + 2;
55
56     while (*from)
57     {
58         const char *res = zebra_maps_output (zm, reg_type, &from);
59         if (!res)
60             *to++ = *from++;
61         else
62             while (*res)
63                 *to++ = *res++;
64     }
65     *to = '\0';
66     /* yaz_log (LOG_LOG, "%s", keybuf); */
67     printf("%10" ZINT_FORMAT0 " %s %d.%d - %d.%d\n", count, keybuf,
68               firstsys,firstseq, lastsys,lastseq);
69 }
70
71 static int inv_stat_handle (char *name, const char *info, int pos,
72                             void *client)
73 {
74     zint occur = 0;
75     int i = 0;
76     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
77     ISAMS_P isam_p;
78     int firstsys=-1;
79     int firstseq=-1;
80     int lastsys=-1;
81     int lastseq=-1;
82
83     stat_info->no_dict_entries++;
84     stat_info->no_dict_bytes += strlen(name);
85
86     assert (*info == sizeof(ISAMS_P));
87     memcpy (&isam_p, info+1, sizeof(ISAMS_P));
88
89     if (stat_info->zh->reg->isams)
90     {
91         ISAMS_PP pp;
92         int occurx = 0;
93         struct it_key key;
94
95         pp = isams_pp_open (stat_info->zh->reg->isams, isam_p);
96         occur = isams_pp_num (pp);
97         while (isams_pp_read(pp, &key))
98         {
99             occurx++;
100 #if IT_KEY_NEW
101 #else
102             stat_info->cksum = stat_info->cksum * 65509 + 
103                 key.sysno + 11 * key.seqno;
104             if (-1==firstsys)
105             {
106                 firstseq=key.seqno;
107                 firstsys=key.sysno;
108             }
109             lastsys=key.sysno;
110             lastseq=key.seqno;
111 #endif
112         }
113         assert (occurx == occur);
114         stat_info->no_isam_entries[0] += occur;
115         isams_pp_close (pp);
116     }
117     if (stat_info->zh->reg->isamc)
118     {
119         ISAMC_PP pp;
120         zint occurx = 0;
121         struct it_key key;
122
123         pp = isc_pp_open (stat_info->zh->reg->isamc, isam_p);
124         occur = isc_pp_num (pp);
125         while (isc_pp_read(pp, &key))
126         {
127             occurx++;
128 #if IT_KEY_NEW
129 #else
130             stat_info->cksum = stat_info->cksum * 65509 + 
131                 key.sysno + 11 * key.seqno;
132             if (-1==firstsys)
133             {
134                 firstseq=key.seqno;
135                 firstsys=key.sysno;
136             }
137             lastsys=key.sysno;
138             lastseq=key.seqno;
139 #endif
140         }
141         assert (occurx == occur);
142         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
143         isc_pp_close (pp);
144     }
145     if (stat_info->zh->reg->isamb)
146     {
147         ISAMB_PP pp;
148         struct it_key key;
149         int cat = (int) (isam_p & 3);
150         int level;
151         int size;
152         int blocks;
153         
154         pp = isamb_pp_open_x(stat_info->zh->reg->isamb, isam_p, &level);
155
156         while (isamb_pp_read(pp, &key))
157         {
158             occur++;
159 #if IT_KEY_NEW
160 #else
161             stat_info->cksum = stat_info->cksum * 65509 + 
162                 key.sysno + 11 * key.seqno;
163             if (-1==firstsys)
164             {
165                 firstseq=key.seqno;
166                 firstsys=key.sysno;
167             }
168             lastsys=key.sysno;
169             lastseq=key.seqno;
170 #endif
171         }
172         isamb_pp_close_x (pp, &size, &blocks);
173         stat_info->isamb_blocks[cat] += blocks;
174         stat_info->isamb_sizes[cat] += size;
175         if (level > 4)
176             level = 4;
177         stat_info->isamb_levels[cat][level] ++;
178         stat_info->no_isam_entries[cat] += occur;
179     }
180     i=0;
181     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
182         i++;
183     ++(stat_info->isam_occurrences[i]);
184     if (stat_info->dumpwords)
185        print_dict_item(stat_info->zh->reg->zebra_maps, name, occur,
186           firstsys,firstseq, lastsys, lastseq);
187     return 0;
188 }
189
190 int zebra_register_statistics (ZebraHandle zh, int dumpdict)
191 {
192     int i, prev;
193     int before = 0;
194     zint occur;
195     int after = 1000000000;
196     struct inv_stat_info stat_info;
197     char term_dict[2*IT_MAX_WORD+2];
198
199     if (zebra_begin_read (zh))
200         return 1;
201
202     stat_info.zh = zh;
203     stat_info.dumpwords=dumpdict;
204
205     term_dict[0] = 1;
206     term_dict[1] = 0;
207
208     for (i = 0; i<=SINGLETON_TYPE; i++)
209         stat_info.no_isam_entries[i] = 0;
210     stat_info.no_dict_entries = 0;
211     stat_info.no_dict_bytes = 0;
212     stat_info.isam_bounds[0] = 1;
213     stat_info.isam_bounds[1] = 2;
214     stat_info.isam_bounds[2] = 3;
215     stat_info.isam_bounds[3] = 6;
216     stat_info.isam_bounds[4] = 10;
217     stat_info.isam_bounds[5] = 20;
218     stat_info.isam_bounds[6] = 30;
219     stat_info.isam_bounds[7] = 50;
220     stat_info.isam_bounds[8] = 100;
221     stat_info.isam_bounds[9] = 200;
222     stat_info.isam_bounds[10] = 5000;
223     stat_info.isam_bounds[11] = 10000;
224     stat_info.isam_bounds[12] = 20000;
225     stat_info.isam_bounds[13] = 50000;
226     stat_info.isam_bounds[14] = 100000;
227     stat_info.isam_bounds[15] = 200000;
228     stat_info.isam_bounds[16] = 500000;
229     stat_info.isam_bounds[17] = 1000000;
230     stat_info.isam_bounds[18] = 0;
231
232     stat_info.cksum = 0;
233
234     for (i = 0; i<20; i++)
235         stat_info.isam_occurrences[i] = 0;
236
237     for (i = 0; i<10; i++)
238     {
239         int j;
240         for (j = 0; j<5; j++)
241             stat_info.isamb_levels[i][j] = 0;
242         stat_info.isamb_sizes[i] = 0;
243         stat_info.isamb_blocks[i] = 0;
244     }
245
246     dict_scan (zh->reg->dict, term_dict, &before, &after, &stat_info,
247                inv_stat_handle);
248
249     if (zh->reg->isamc)
250     {
251         fprintf (stdout, "   Blocks    Occur  Size KB   Bytes/Entry\n");
252         for (i = 0; isc_block_used (zh->reg->isamc, i) >= 0; i++)
253         {
254             fprintf (stdout, " %8" ZINT_FORMAT0 " %8" ZINT_FORMAT0,
255                      isc_block_used (zh->reg->isamc, i),
256                      stat_info.no_isam_entries[i]);
257
258             if (stat_info.no_isam_entries[i])
259                 fprintf (stdout, " %8d   %f",
260                          (int) ((1023.0 + (double)
261                                  isc_block_used(zh->reg->isamc, i) *
262                                  isc_block_size(zh->reg->isamc,i))/1024),
263                          ((double) isc_block_used(zh->reg->isamc, i) *
264                           isc_block_size(zh->reg->isamc,i))/
265                          stat_info.no_isam_entries[i]);
266             fprintf (stdout, "\n");
267         }
268     }
269
270     if (zh->reg->isamb)
271     {
272         for (i = 0; i<4; i++)
273         {
274             int j;
275             int bsize = isamb_block_info(zh->reg->isamb, i);
276             if (bsize < 0)
277                 break;
278             fprintf (stdout, "Category   %d\n", i);
279             fprintf (stdout, "Block size %d\n", bsize);
280             fprintf (stdout, "Blocks:    %d\n", stat_info.isamb_blocks[i]);
281             fprintf (stdout, "Size:      %d\n", stat_info.isamb_sizes[i]);
282             fprintf (stdout, "Entries:   " ZINT_FORMAT "\n",
283                      stat_info.no_isam_entries[i]);
284             fprintf (stdout, "Total      %d\n", stat_info.isamb_blocks[i]*
285                      bsize);
286             for (j = 0; j<5; j++)
287                 if (stat_info.isamb_levels[i][j])
288                     fprintf (stdout, "Level%d     %d\n", j,
289                              stat_info.isamb_levels[i][j]);
290             fprintf (stdout, "\n");
291         }
292     }
293     fprintf (stdout, "Checksum       %08lX\n", stat_info.cksum);
294
295     fprintf (stdout, "Distinct words %d\n", stat_info.no_dict_entries);
296     occur = 0;
297     for (i = 0; i<9; i++)
298         occur += stat_info.no_isam_entries[i];
299     fprintf (stdout, "Word pos       " ZINT_FORMAT "\n", occur);
300     fprintf (stdout, "    Occurrences     Words\n");
301     prev = 1;
302     for (i = 0; stat_info.isam_bounds[i]; i++)
303     {
304         int here = stat_info.isam_bounds[i];
305         fprintf (stdout, "%7d-%-7d %7d\n",
306                  prev, here, stat_info.isam_occurrences[i]);
307         prev = here+1;
308     }
309     fprintf (stdout, "%7d-        %7d\n",
310              prev, stat_info.isam_occurrences[i]);
311     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
312     zebra_end_read (zh);
313     return 0;
314 }
315