Fix term counters to be of type zint. Fix several printfs of zint.
[idzebra-moved-to-github.git] / index / invstat.c
1 /* $Id: invstat.c,v 1.38 2004-08-06 12:55:01 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->isam)
118     {
119         ISPT ispt;
120
121         ispt = is_position (stat_info->zh->reg->isam, (int) isam_p);
122         occur = is_numkeys (ispt);
123         stat_info->no_isam_entries[is_type(isam_p)] += occur;
124         is_pt_free (ispt);
125     }
126     if (stat_info->zh->reg->isamc)
127     {
128         ISAMC_PP pp;
129         zint occurx = 0;
130         struct it_key key;
131
132         pp = isc_pp_open (stat_info->zh->reg->isamc, isam_p);
133         occur = isc_pp_num (pp);
134         while (isc_pp_read(pp, &key))
135         {
136             occurx++;
137 #if IT_KEY_NEW
138 #else
139             stat_info->cksum = stat_info->cksum * 65509 + 
140                 key.sysno + 11 * key.seqno;
141             if (-1==firstsys)
142             {
143                 firstseq=key.seqno;
144                 firstsys=key.sysno;
145             }
146             lastsys=key.sysno;
147             lastseq=key.seqno;
148 #endif
149         }
150         assert (occurx == occur);
151         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
152         isc_pp_close (pp);
153     }
154     if (stat_info->zh->reg->isamb)
155     {
156         ISAMB_PP pp;
157         struct it_key key;
158         int cat = (int) (isam_p & 3);
159         int level;
160         int size;
161         int blocks;
162         
163         pp = isamb_pp_open_x(stat_info->zh->reg->isamb, isam_p, &level);
164
165         while (isamb_pp_read(pp, &key))
166         {
167             occur++;
168 #if IT_KEY_NEW
169 #else
170             stat_info->cksum = stat_info->cksum * 65509 + 
171                 key.sysno + 11 * key.seqno;
172             if (-1==firstsys)
173             {
174                 firstseq=key.seqno;
175                 firstsys=key.sysno;
176             }
177             lastsys=key.sysno;
178             lastseq=key.seqno;
179 #endif
180         }
181         isamb_pp_close_x (pp, &size, &blocks);
182         stat_info->isamb_blocks[cat] += blocks;
183         stat_info->isamb_sizes[cat] += size;
184         if (level > 4)
185             level = 4;
186         stat_info->isamb_levels[cat][level] ++;
187         stat_info->no_isam_entries[cat] += occur;
188     }
189     i=0;
190     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
191         i++;
192     ++(stat_info->isam_occurrences[i]);
193     if (stat_info->dumpwords)
194        print_dict_item(stat_info->zh->reg->zebra_maps, name, occur,
195           firstsys,firstseq, lastsys, lastseq);
196     return 0;
197 }
198
199 int zebra_register_statistics (ZebraHandle zh, int dumpdict)
200 {
201     int i, prev;
202     int before = 0;
203     zint occur;
204     int after = 1000000000;
205     struct inv_stat_info stat_info;
206     char term_dict[2*IT_MAX_WORD+2];
207
208     if (zebra_begin_read (zh))
209         return 1;
210
211     stat_info.zh = zh;
212     stat_info.dumpwords=dumpdict;
213
214     term_dict[0] = 1;
215     term_dict[1] = 0;
216
217     for (i = 0; i<=SINGLETON_TYPE; i++)
218         stat_info.no_isam_entries[i] = 0;
219     stat_info.no_dict_entries = 0;
220     stat_info.no_dict_bytes = 0;
221     stat_info.isam_bounds[0] = 1;
222     stat_info.isam_bounds[1] = 2;
223     stat_info.isam_bounds[2] = 3;
224     stat_info.isam_bounds[3] = 6;
225     stat_info.isam_bounds[4] = 10;
226     stat_info.isam_bounds[5] = 20;
227     stat_info.isam_bounds[6] = 30;
228     stat_info.isam_bounds[7] = 50;
229     stat_info.isam_bounds[8] = 100;
230     stat_info.isam_bounds[9] = 200;
231     stat_info.isam_bounds[10] = 5000;
232     stat_info.isam_bounds[11] = 10000;
233     stat_info.isam_bounds[12] = 20000;
234     stat_info.isam_bounds[13] = 50000;
235     stat_info.isam_bounds[14] = 100000;
236     stat_info.isam_bounds[15] = 200000;
237     stat_info.isam_bounds[16] = 500000;
238     stat_info.isam_bounds[17] = 1000000;
239     stat_info.isam_bounds[18] = 0;
240
241     stat_info.cksum = 0;
242
243     for (i = 0; i<20; i++)
244         stat_info.isam_occurrences[i] = 0;
245
246     for (i = 0; i<10; i++)
247     {
248         int j;
249         for (j = 0; j<5; j++)
250             stat_info.isamb_levels[i][j] = 0;
251         stat_info.isamb_sizes[i] = 0;
252         stat_info.isamb_blocks[i] = 0;
253     }
254
255     dict_scan (zh->reg->dict, term_dict, &before, &after, &stat_info,
256                inv_stat_handle);
257
258     if (zh->reg->isamc)
259     {
260         fprintf (stdout, "   Blocks    Occur  Size KB   Bytes/Entry\n");
261         for (i = 0; isc_block_used (zh->reg->isamc, i) >= 0; i++)
262         {
263             fprintf (stdout, " %8" ZINT_FORMAT0 " %8" ZINT_FORMAT0,
264                      isc_block_used (zh->reg->isamc, i),
265                      stat_info.no_isam_entries[i]);
266
267             if (stat_info.no_isam_entries[i])
268                 fprintf (stdout, " %8d   %f",
269                          (int) ((1023.0 + (double)
270                                  isc_block_used(zh->reg->isamc, i) *
271                                  isc_block_size(zh->reg->isamc,i))/1024),
272                          ((double) isc_block_used(zh->reg->isamc, i) *
273                           isc_block_size(zh->reg->isamc,i))/
274                          stat_info.no_isam_entries[i]);
275             fprintf (stdout, "\n");
276         }
277     }
278
279     if (zh->reg->isamb)
280     {
281         for (i = 0; i<4; i++)
282         {
283             int j;
284             int bsize = isamb_block_info(zh->reg->isamb, i);
285             if (bsize < 0)
286                 break;
287             fprintf (stdout, "Category   %d\n", i);
288             fprintf (stdout, "Block size %d\n", bsize);
289             fprintf (stdout, "Blocks:    %d\n", stat_info.isamb_blocks[i]);
290             fprintf (stdout, "Size:      %d\n", stat_info.isamb_sizes[i]);
291             fprintf (stdout, "Entries:   " ZINT_FORMAT "\n",
292                      stat_info.no_isam_entries[i]);
293             fprintf (stdout, "Total      %d\n", stat_info.isamb_blocks[i]*
294                      bsize);
295             for (j = 0; j<5; j++)
296                 if (stat_info.isamb_levels[i][j])
297                     fprintf (stdout, "Level%d     %d\n", j,
298                              stat_info.isamb_levels[i][j]);
299             fprintf (stdout, "\n");
300         }
301     }
302     fprintf (stdout, "Checksum       %08lX\n", stat_info.cksum);
303
304     fprintf (stdout, "Distinct words %d\n", stat_info.no_dict_entries);
305     occur = 0;
306     for (i = 0; i<9; i++)
307         occur += stat_info.no_isam_entries[i];
308     fprintf (stdout, "Word pos       " ZINT_FORMAT "\n", occur);
309     fprintf (stdout, "    Occurrences     Words\n");
310     prev = 1;
311     for (i = 0; stat_info.isam_bounds[i]; i++)
312     {
313         int here = stat_info.isam_bounds[i];
314         fprintf (stdout, "%7d-%-7d %7d\n",
315                  prev, here, stat_info.isam_occurrences[i]);
316         prev = here+1;
317     }
318     fprintf (stdout, "%7d-        %7d\n",
319              prev, stat_info.isam_occurrences[i]);
320     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
321     zebra_end_read (zh);
322     return 0;
323 }
324