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