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