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