Added dumpdict command line option to dump the
[idzebra-moved-to-github.git] / index / invstat.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss, Heikki Levanto
5  * log at eof
6  *
7  */
8 #include <stdio.h>
9 #include <assert.h>
10 #include <string.h>
11
12 #include "index.h"
13 #include "../isamc/isamd-p.h"
14
15 struct inv_stat_info {
16     ZebraHandle zh;
17     int no_isam_entries[9];
18     int no_dict_entries;
19     int no_dict_bytes;
20     int isam_bounds[20];
21     int isam_occurrences[20];
22     char tmp[128];
23     int isamb_levels[10][5];
24     int isamb_sizes[10];
25     int isamb_blocks[10];
26     unsigned long cksum;
27     int dumpwords;
28 };
29
30 #define SINGLETON_TYPE 8 /* the type to use for singletons that */ 
31                          /* have no block and no block type */
32
33 static void print_dict_item (ZebraMaps zm, const char *s, int count )
34 {
35     int reg_type = s[1];
36     char keybuf[IT_MAX_WORD+1];
37     char *to = keybuf;
38     const char *from = s + 2;
39
40     while (*from)
41     {
42         const char *res = zebra_maps_output (zm, reg_type, &from);
43         if (!res)
44             *to++ = *from++;
45         else
46             while (*res)
47                 *to++ = *res++;
48     }
49     *to = '\0';
50     /* yaz_log (LOG_LOG, "%s", keybuf); */
51     printf("%10d %s\n",count, keybuf);
52 }
53
54 static int inv_stat_handle (char *name, const char *info, int pos,
55                             void *client)
56 {
57     int occur = 0;
58     int i = 0;
59     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
60     ISAMS_P isam_p;
61
62     stat_info->no_dict_entries++;
63     stat_info->no_dict_bytes += strlen(name);
64
65     assert (*info == sizeof(ISAMS_P));
66     memcpy (&isam_p, info+1, sizeof(ISAMS_P));
67
68
69     if (stat_info->zh->reg->isams)
70     {
71         ISAMS_PP pp;
72         int occurx = 0;
73         struct it_key key;
74
75         pp = isams_pp_open (stat_info->zh->reg->isams, isam_p);
76         occur = isams_pp_num (pp);
77         while (isams_pp_read(pp, &key))
78         {
79             stat_info->cksum = stat_info->cksum * 65509 + 
80                 key.sysno + 11 * key.seqno;
81             occurx++;
82         }
83         assert (occurx == occur);
84         stat_info->no_isam_entries[0] += occur;
85         isams_pp_close (pp);
86     }
87     if (stat_info->zh->reg->isam)
88     {
89         ISPT ispt;
90
91         ispt = is_position (stat_info->zh->reg->isam, isam_p);
92         occur = is_numkeys (ispt);
93         stat_info->no_isam_entries[is_type(isam_p)] += occur;
94         is_pt_free (ispt);
95     }
96     if (stat_info->zh->reg->isamc)
97     {
98         ISAMC_PP pp;
99         int occurx = 0;
100         struct it_key key;
101
102         pp = isc_pp_open (stat_info->zh->reg->isamc, isam_p);
103         occur = isc_pp_num (pp);
104         while (isc_pp_read(pp, &key))
105         {
106             stat_info->cksum = stat_info->cksum * 65509 + 
107                 key.sysno + 11 * key.seqno;
108             occurx++;
109         }
110         assert (occurx == occur);
111         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
112         isc_pp_close (pp);
113     }
114     if (stat_info->zh->reg->isamd)
115     {
116         ISAMD_PP pp;
117         int occurx = 0;
118         struct it_key key;
119
120         pp = isamd_pp_open (stat_info->zh->reg->isamd, isam_p);
121         
122         occur = isamd_pp_num (pp);
123         while (isamd_pp_read(pp, &key))
124         {
125             stat_info->cksum = stat_info->cksum * 65509 + 
126                 key.sysno + 11 * key.seqno;
127             occurx++;
128             if ( pp->is->method->debug >8 )
129                logf (LOG_LOG,"sysno=%d seqno=%d (%x/%x) oc=%d/%d ofs=%d ",
130                    key.sysno, key.seqno,
131                    key.sysno, key.seqno,
132                    occur,occurx, pp->offset);
133         }
134         if ( pp->is->method->debug >7 )
135            logf(LOG_LOG,"item %d=%d:%d says %d keys, counted %d",
136               isam_p, isamd_type(isam_p), isamd_block(isam_p),
137               occur, occurx); 
138         if (occurx != occur) 
139           logf(LOG_LOG,"Count error!!! read %d, counted %d", occur, occurx);
140         assert (occurx == occur);
141         if ( is_singleton(isam_p) )
142             stat_info->no_isam_entries[SINGLETON_TYPE] += occur;
143         else
144             stat_info->no_isam_entries[isamd_type(isam_p)] += occur;
145         isamd_pp_close (pp);
146     }
147     if (stat_info->zh->reg->isamb)
148     {
149         ISAMB_PP pp;
150         struct it_key key;
151         int cat = isam_p & 3;
152         int level;
153         int size;
154         int blocks;
155         
156         pp = isamb_pp_open_x(stat_info->zh->reg->isamb, isam_p, &level);
157
158         while (isamb_pp_read(pp, &key))
159         {
160             stat_info->cksum = stat_info->cksum * 65509 + 
161                 key.sysno + 11 * key.seqno;
162             occur++;
163         }
164         isamb_pp_close_x (pp, &size, &blocks);
165         stat_info->isamb_blocks[cat] += blocks;
166         stat_info->isamb_sizes[cat] += size;
167         if (level > 4)
168             level = 4;
169         stat_info->isamb_levels[cat][level] ++;
170         stat_info->no_isam_entries[cat] += occur;
171     }
172
173     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
174         i++;
175     ++(stat_info->isam_occurrences[i]);
176     if (stat_info->dumpwords)
177        print_dict_item(stat_info->zh->reg->zebra_maps, name, occur);
178     return 0;
179 }
180
181 void zebra_register_statistics (ZebraHandle zh, int dumpdict)
182 {
183     int blocks;
184     int size;
185     int count;
186     int i, prev;
187     int before = 0;
188     int occur;
189     int after = 1000000000;
190     struct inv_stat_info stat_info;
191     char term_dict[2*IT_MAX_WORD+2];
192
193     if (zebra_begin_read (zh))
194         return;
195
196     stat_info.zh = zh;
197     stat_info.dumpwords=dumpdict;
198
199     term_dict[0] = 1;
200     term_dict[1] = 0;
201
202     for (i = 0; i<=SINGLETON_TYPE; i++)
203         stat_info.no_isam_entries[i] = 0;
204     stat_info.no_dict_entries = 0;
205     stat_info.no_dict_bytes = 0;
206     stat_info.isam_bounds[0] = 1;
207     stat_info.isam_bounds[1] = 2;
208     stat_info.isam_bounds[2] = 3;
209     stat_info.isam_bounds[3] = 6;
210     stat_info.isam_bounds[4] = 10;
211     stat_info.isam_bounds[5] = 20;
212     stat_info.isam_bounds[6] = 30;
213     stat_info.isam_bounds[7] = 50;
214     stat_info.isam_bounds[8] = 100;
215     stat_info.isam_bounds[9] = 200;
216     stat_info.isam_bounds[10] = 5000;
217     stat_info.isam_bounds[11] = 10000;
218     stat_info.isam_bounds[12] = 20000;
219     stat_info.isam_bounds[13] = 50000;
220     stat_info.isam_bounds[14] = 100000;
221     stat_info.isam_bounds[15] = 200000;
222     stat_info.isam_bounds[16] = 500000;
223     stat_info.isam_bounds[17] = 1000000;
224     stat_info.isam_bounds[18] = 0;
225
226     stat_info.cksum = 0;
227
228     for (i = 0; i<20; i++)
229         stat_info.isam_occurrences[i] = 0;
230
231     for (i = 0; i<10; i++)
232     {
233         int j;
234         for (j = 0; j<5; j++)
235             stat_info.isamb_levels[i][j] = 0;
236         stat_info.isamb_sizes[i] = 0;
237         stat_info.isamb_blocks[i] = 0;
238     }
239
240     dict_scan (zh->reg->dict, term_dict, &before, &after, &stat_info,
241                inv_stat_handle);
242
243     if (zh->reg->isamc)
244     {
245         fprintf (stdout, "   Blocks    Occur  Size KB   Bytes/Entry\n");
246         for (i = 0; isc_block_used (zh->reg->isamc, i) >= 0; i++)
247         {
248             fprintf (stdout, " %8d %8d", isc_block_used (zh->reg->isamc, i),
249                      stat_info.no_isam_entries[i]);
250
251             if (stat_info.no_isam_entries[i])
252                 fprintf (stdout, " %8d   %f",
253                          (int) ((1023.0 + (double)
254                                  isc_block_used(zh->reg->isamc, i) *
255                                  isc_block_size(zh->reg->isamc,i))/1024),
256                          ((double) isc_block_used(zh->reg->isamc, i) *
257                           isc_block_size(zh->reg->isamc,i))/
258                          stat_info.no_isam_entries[i]);
259             fprintf (stdout, "\n");
260         }
261     }
262     if (zh->reg->isamd)
263     {
264         fprintf (stdout, "   Blocks   Occur      KB Bytes/Entry\n");
265         if (zh->reg->isamd->method->debug >0) 
266             logf(LOG_LOG,"   Blocks   Occur      KB Bytes/Entry");
267         for (i = 0; i<=SINGLETON_TYPE; i++)
268         {
269             blocks= isamd_block_used(zh->reg->isamd,i);
270             size= isamd_block_size(zh->reg->isamd,i);
271             count=stat_info.no_isam_entries[i];
272             if (i==SINGLETON_TYPE) 
273                 blocks=size=0;
274             if (stat_info.no_isam_entries[i]) 
275             {
276                 fprintf (stdout, "%c %7d %7d %7d %5.2f\n",
277                          (i==SINGLETON_TYPE)?('z'):('A'+i),
278                          blocks,
279                          count,
280                          (int) ((1023.0 + (double) blocks * size)/1024),
281                          ((double) blocks * size)/count);
282                 if (zh->reg->isamd->method->debug >0) 
283                     logf(LOG_LOG, "%c %7d %7d %7d %5.2f",
284                          (i==SINGLETON_TYPE)?('z'):('A'+i),
285                          blocks,
286                          count,
287                          (int) ((1023.0 + (double) blocks * size)/1024),
288                          ((double) blocks * size)/count);
289             } /* entries */
290         } /* for */
291     } /* isamd */
292     if ( (zh->reg->isamd) && (zh->reg->isamd->method->debug>0))
293         fprintf (stdout, "\n%d words using %d bytes\n",
294              stat_info.no_dict_entries, stat_info.no_dict_bytes);
295
296     if (zh->reg->isamb)
297     {
298         for (i = 0; i<4; i++)
299         {
300             int j;
301             int bsize = isamb_block_info(zh->reg->isamb, i);
302             if (bsize < 0)
303                 break;
304             fprintf (stdout, "Category   %d\n", i);
305             fprintf (stdout, "Block size %d\n", bsize);
306             fprintf (stdout, "Blocks:    %d\n", stat_info.isamb_blocks[i]);
307             fprintf (stdout, "Size:      %d\n", stat_info.isamb_sizes[i]);
308             fprintf (stdout, "Entries:   %d\n", stat_info.no_isam_entries[i]);
309             fprintf (stdout, "Total      %d\n", stat_info.isamb_blocks[i]*
310                      bsize);
311             for (j = 0; j<5; j++)
312                 if (stat_info.isamb_levels[i][j])
313                     fprintf (stdout, "Level%d     %d\n", j,
314                              stat_info.isamb_levels[i][j]);
315             fprintf (stdout, "\n");
316         }
317     }
318     fprintf (stdout, "Checksum       %08lX\n", stat_info.cksum);
319
320     fprintf (stdout, "Distinct words %d\n", stat_info.no_dict_entries);
321     occur = 0;
322     for (i = 0; i<9; i++)
323         occur += stat_info.no_isam_entries[i];
324     fprintf (stdout, "Word pos       %d\n", occur);
325     fprintf (stdout, "    Occurrences     Words\n");
326     prev = 1;
327     for (i = 0; stat_info.isam_bounds[i]; i++)
328     {
329         int here = stat_info.isam_bounds[i];
330         fprintf (stdout, "%7d-%-7d %7d\n",
331                  prev, here, stat_info.isam_occurrences[i]);
332         prev = here+1;
333     }
334     fprintf (stdout, "%7d-        %7d\n",
335              prev, stat_info.isam_occurrences[i]);
336     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
337     zebra_end_read (zh);
338 }
339
340
341 /*
342  *
343  * $Log: invstat.c,v $
344  * Revision 1.30  2002-07-11 13:03:01  heikki
345  * Added dumpdict command line option to dump the
346  * dictionary before doing the usual stats
347  *
348  * Revision 1.29  2002/06/19 10:29:17  adam
349  * align block sizes for isam sys. Better plot for test
350  *
351  * Revision 1.28  2002/04/30 19:31:09  adam
352  * isamb delete; more statistics
353  *
354  * Revision 1.27  2002/04/30 08:28:37  adam
355  * isamb fixes for pp_read. Statistics
356  *
357  * Revision 1.26  2002/04/29 18:03:46  adam
358  * More isamb statistics
359  *
360  * Revision 1.25  2002/04/26 08:44:47  adam
361  * Index statistics working again
362  *
363  * Revision 1.24  2002/04/05 08:46:26  adam
364  * Zebra with full functionality
365  *
366  * Revision 1.23  2002/04/04 14:14:13  adam
367  * Multiple registers (alpha early)
368  *
369  * Revision 1.22  2002/02/20 17:30:01  adam
370  * Work on new API. Locking system re-implemented
371  *
372  * Revision 1.21  2000/07/13 10:14:20  heikki
373  * Removed compiler warnings when making zebra
374  *
375  * Revision 1.20  1999/12/01 13:30:30  adam
376  * Updated configure for Zmbol/Zebra dependent settings.
377  *
378  * Revision 1.19  1999/11/30 13:48:03  adam
379  * Improved installation. Updated for inclusion of YAZ header files.
380  *
381  * Revision 1.18  1999/10/06 11:46:36  heikki
382  * mproved statistics on isam-d
383  *
384  * Revision 1.17  1999/08/20 08:28:37  heikki
385  * Log levels
386  *
387  * Revision 1.16  1999/08/18 08:38:22  heikki
388  * Memory leak hunting
389  *
390  * Revision 1.15  1999/08/18 08:34:53  heikki
391  * isamd
392  *
393  * Revision 1.14  1999/07/14 10:59:26  adam
394  * Changed functions isc_getmethod, isams_getmethod.
395  * Improved fatal error handling (such as missing EXPLAIN schema).
396  *
397  * Revision 1.13  1999/07/08 14:23:27  heikki
398  * Fixed a bug in isamh_pp_read and cleaned up a bit
399  *
400  * Revision 1.12  1999/07/06 12:28:04  adam
401  * Updated record index structure. Format includes version ID. Compression
402  * algorithm ID is stored for each record block.
403  *
404  * Revision 1.11  1999/05/15 14:36:38  adam
405  * Updated dictionary. Implemented "compression" of dictionary.
406  *
407  * Revision 1.10  1999/05/12 13:08:06  adam
408  * First version of ISAMS.
409  *
410  * Revision 1.9  1999/02/12 13:29:23  adam
411  * Implemented position-flag for registers.
412  *
413  * Revision 1.8  1999/02/02 14:50:53  adam
414  * Updated WIN32 code specific sections. Changed header.
415  *
416  * Revision 1.7  1998/03/13 15:30:50  adam
417  * New functions isc_block_used and isc_block_size. Fixed 'leak'
418  * in isc_alloc_block.
419  *
420  * Revision 1.6  1998/03/06 13:54:02  adam
421  * Fixed two nasty bugs in isc_merge.
422  *
423  * Revision 1.5  1997/09/17 12:19:13  adam
424  * Zebra version corresponds to YAZ version 1.4.
425  * Changed Zebra server so that it doesn't depend on global common_resource.
426  *
427  * Revision 1.4  1996/11/08 11:10:21  adam
428  * Buffers used during file match got bigger.
429  * Compressed ISAM support everywhere.
430  * Bug fixes regarding masking characters in queries.
431  * Redesigned Regexp-2 queries.
432  *
433  * Revision 1.3  1996/06/04 10:18:58  adam
434  * Minor changes - removed include of ctype.h.
435  *
436  * Revision 1.2  1996/05/22  08:25:56  adam
437  * Minor change.
438  *
439  * Revision 1.1  1996/05/14 14:04:34  adam
440  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
441  * is collected.
442  */