mproved statistics on isam-d
[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 "recindex.h"
14 #include "../isamc/isamh-p.h"
15 #include "../isamc/isamd-p.h"
16
17 struct inv_stat_info {
18     ISAM isam;
19     ISAMC isamc;
20     ISAMS isams;
21     ISAMH isamh;
22     ISAMD isamd;
23     int no_isam_entries[9];
24     int no_dict_entries;
25     int no_dict_bytes;
26     int isam_bounds[20];
27     int isam_occurrences[20];
28     char tmp[128];
29 };
30
31 #define SINGLETON_TYPE 8 /* the type to use for singletons that */ 
32                          /* have no block and no block type */
33
34 static int inv_stat_handle (char *name, const char *info, int pos,
35                             void *client)
36 {
37     int occur = 0;
38     int i = 0;
39     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
40     ISAM_P isam_p;
41
42     stat_info->no_dict_entries++;
43     stat_info->no_dict_bytes += strlen(name);
44
45     assert (*info == sizeof(ISAM_P));
46     memcpy (&isam_p, info+1, sizeof(ISAM_P));
47
48     //printf ("---\n");
49     if (stat_info->isam)
50     {
51         ISPT ispt;
52
53         ispt = is_position (stat_info->isam, isam_p);
54         occur = is_numkeys (ispt);
55         is_pt_free (ispt);
56     }
57     if (stat_info->isamc)
58     {
59         ISAMC_PP pp;
60         int occurx = 0;
61         struct it_key key;
62
63         pp = isc_pp_open (stat_info->isamc, isam_p);
64         occur = isc_pp_num (pp);
65         while (isc_pp_read(pp, &key))
66         {
67             //printf ("sysno=%d seqno=%d\n", key.sysno, key.seqno);
68             occurx++;
69         }
70         assert (occurx == occur);
71         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
72         isc_pp_close (pp);
73     }
74     if (stat_info->isamh)
75     {
76         ISAMH_PP pp;
77         int occurx = 0;
78         struct it_key key;
79
80         pp = isamh_pp_open (stat_info->isamh, isam_p);
81         
82         occur = isamh_pp_num (pp);
83         while (isamh_pp_read(pp, &key))
84         {
85             occurx++;
86             //logf (LOG_LOG,"sysno=%d seqno=%d (%x/%x) oc=%d/%d ofs=%d ",
87             //       key.sysno, key.seqno,
88             //       key.sysno, key.seqno,
89             //       occur,occurx, pp->offset);
90         }
91         if (occurx != occur) {
92           logf(LOG_LOG,"Count error!!! read %d, counted %d", occur, occurx);
93           //isamh_pp_dump(stat_info->isamh, isam_p);
94           }
95         stat_info->no_isam_entries[isamh_type(isam_p)] += occur;
96         isamh_pp_close (pp);
97     }
98     if (stat_info->isamd)
99     {
100         ISAMD_PP pp;
101         int occurx = 0;
102         struct it_key key;
103
104         pp = isamd_pp_open (stat_info->isamd, isam_p);
105         
106         occur = isamd_pp_num (pp);
107         while (isamd_pp_read(pp, &key))
108         {
109             occurx++;
110             if ( pp->is->method->debug >8 )
111                logf (LOG_LOG,"sysno=%d seqno=%d (%x/%x) oc=%d/%d ofs=%d ",
112                    key.sysno, key.seqno,
113                    key.sysno, key.seqno,
114                    occur,occurx, pp->offset);
115         }
116         if ( pp->is->method->debug >7 )
117            logf(LOG_LOG,"item %d=%d:%d says %d keys, counted %d",
118               isam_p, isamd_type(isam_p), isamd_block(isam_p),
119               occur, occurx); 
120         if (occurx != occur) 
121           logf(LOG_LOG,"Count error!!! read %d, counted %d", occur, occurx);
122         assert (occurx == occur);
123         if ( is_singleton(isam_p) )
124             stat_info->no_isam_entries[SINGLETON_TYPE] += occur;
125         else
126             stat_info->no_isam_entries[isamd_type(isam_p)] += occur;
127         isamd_pp_close (pp);
128     }
129     if (stat_info->isams)
130     {
131         ISAMS_PP pp;
132         int occurx = 0;
133         struct it_key key;
134
135         pp = isams_pp_open (stat_info->isams, isam_p);
136         occur = isams_pp_num (pp);
137         while (isams_pp_read(pp, &key))
138         {
139             //printf ("sysno=%d seqno=%d\n", key.sysno, key.seqno);
140             occurx++;
141         }
142         assert (occurx == occur);
143         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
144         isams_pp_close (pp);
145     }
146
147     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
148         i++;
149     ++(stat_info->isam_occurrences[i]);
150     return 0;
151 }
152
153 void inv_prstat (BFiles bfs)
154 {
155     Dict dict;
156     ISAM  isam  = NULL;
157     ISAMC isamc = NULL;
158     ISAMS isams = NULL;
159     ISAMH isamh = NULL;
160     ISAMD isamd = NULL;
161     Records records;
162     int i, prev;
163     int before = 0;
164     int after = 1000000000;
165     struct inv_stat_info stat_info;
166     char term_dict[2*IT_MAX_WORD+2];
167     int blocks;
168     int size;
169     int count;
170         
171     term_dict[0] = 1;
172     term_dict[1] = 0;
173
174     dict = dict_open (bfs, FNAME_DICT, 100, 0, 0);
175     if (!dict)
176     {
177         logf (LOG_FATAL, "dict_open fail");
178         exit (1);
179     }
180     if (res_get_match (common_resource, "isam", "i", NULL))
181     {
182         isam = is_open (bfs, FNAME_ISAM, key_compare, 0,
183                         sizeof(struct it_key), common_resource);
184         if (!isam)
185         {
186             logf (LOG_FATAL, "is_open fail");
187             exit (1);
188         }
189     }
190     else if (res_get_match (common_resource, "isam", "s", NULL))
191     {
192         struct ISAMS_M_s isams_m;
193         isams = isams_open (bfs, FNAME_ISAMS, 0,
194                             key_isams_m(common_resource, &isams_m));
195         if (!isams)
196         {
197             logf (LOG_FATAL, "isams_open fail");
198             exit (1);
199         }
200     }
201     else if (res_get_match (common_resource, "isam", "h", NULL))
202     {
203         isamh = isamh_open (bfs, FNAME_ISAMH, 0, key_isamh_m(common_resource));
204         if (!isamh)
205         {
206             logf (LOG_FATAL, "isamh_open fail");
207             exit (1);
208         }
209     }
210     else if (res_get_match (common_resource, "isam", "d", NULL))
211     {
212         struct ISAMD_M_s isamd_m;
213         isamd = isamd_open (bfs, FNAME_ISAMD, 0, 
214                             key_isamd_m(common_resource,&isamd_m));
215         if (!isamd)
216         {
217             logf (LOG_FATAL, "isamd_open fail");
218             exit (1);
219         }
220     }
221     else
222     {
223         struct ISAMC_M_s isamc_m;
224         isamc = isc_open (bfs, FNAME_ISAMC, 0,
225                           key_isamc_m (common_resource, &isamc_m));
226         if (!isamc)
227         {
228             logf (LOG_FATAL, "isc_open fail");
229             exit (1);
230         }
231     }
232     records = rec_open (bfs, 0, 0);
233
234     for (i = 0; i<=SINGLETON_TYPE; i++)
235         stat_info.no_isam_entries[i] = 0;
236     stat_info.no_dict_entries = 0;
237     stat_info.no_dict_bytes = 0;
238     stat_info.isam = isam;
239     stat_info.isamc = isamc;
240     stat_info.isams = isams;
241     stat_info.isamh = isamh;
242     stat_info.isamd = isamd;
243     stat_info.isam_bounds[0] = 1;
244     stat_info.isam_bounds[1] = 2;
245     stat_info.isam_bounds[2] = 3;
246     stat_info.isam_bounds[3] = 6;
247     stat_info.isam_bounds[4] = 10;
248     stat_info.isam_bounds[5] = 20;
249     stat_info.isam_bounds[6] = 30;
250     stat_info.isam_bounds[7] = 50;
251     stat_info.isam_bounds[8] = 100;
252     stat_info.isam_bounds[9] = 200;
253     stat_info.isam_bounds[10] = 5000;
254     stat_info.isam_bounds[11] = 10000;
255     stat_info.isam_bounds[12] = 20000;
256     stat_info.isam_bounds[13] = 50000;
257     stat_info.isam_bounds[14] = 100000;
258     stat_info.isam_bounds[15] = 200000;
259     stat_info.isam_bounds[16] = 500000;
260     stat_info.isam_bounds[17] = 1000000;
261     stat_info.isam_bounds[18] = 0;
262
263     for (i = 0; i<20; i++)
264         stat_info.isam_occurrences[i] = 0;
265
266     dict_scan (dict, term_dict, &before, &after, &stat_info, inv_stat_handle);
267
268     if (isamc)
269     {
270         fprintf (stderr, "   Blocks    Occur  Size KB   Bytes/Entry\n");
271         for (i = 0; isc_block_used (isamc, i) >= 0; i++)
272         {
273             fprintf (stderr, " %8d %8d", isc_block_used (isamc, i),
274                      stat_info.no_isam_entries[i]);
275
276             if (stat_info.no_isam_entries[i])
277                 fprintf (stderr, " %8d   %f",
278                          (int) ((1023.0 + (double) isc_block_used(isamc, i) *
279                                  isc_block_size(isamc,i))/1024),
280                          ((double) isc_block_used(isamc, i) *
281                           isc_block_size(isamc,i))/
282                          stat_info.no_isam_entries[i]);
283             fprintf (stderr, "\n");
284         }
285     }
286     if (isamd)
287     {
288         fprintf (stderr, "   Blocks   Occur      KB Bytes/Entry\n");
289         if (isamd->method->debug >0) 
290             logf(LOG_LOG,"   Blocks   Occur      KB Bytes/Entry");
291         for (i = 0; i<=SINGLETON_TYPE; i++)
292         {
293             blocks= isamd_block_used(isamd,i);
294             size= isamd_block_size(isamd,i);
295             count=stat_info.no_isam_entries[i];
296             if (i==SINGLETON_TYPE) 
297                 blocks=size=0;
298             if (stat_info.no_isam_entries[i]) 
299             {
300                 fprintf (stderr, "%c %7d %7d %7d %5.2f\n",
301                          (i==SINGLETON_TYPE)?('z'):('A'+i),
302                          blocks,
303                          count,
304                          (int) ((1023.0 + (double) blocks * size)/1024),
305                          ((double) blocks * size)/count);
306                 if (isamd->method->debug >0) 
307                     logf(LOG_LOG, "%c %7d %7d %7d %5.2f",
308                          (i==SINGLETON_TYPE)?('z'):('A'+i),
309                          blocks,
310                          count,
311                          (int) ((1023.0 + (double) blocks * size)/1024),
312                          ((double) blocks * size)/count);
313             } /* entries */
314         } /* for */
315     } /* isamd */
316     if ( (isamd) && (isamd->method->debug>0))
317     fprintf (stderr, "\n%d words using %d bytes\n",
318              stat_info.no_dict_entries, stat_info.no_dict_bytes);
319     fprintf (stderr, "    Occurrences     Words\n");
320     if ( (isamd) && (isamd->method->debug>0) )
321     {
322         logf(LOG_LOG, "%d words using %d bytes",
323              stat_info.no_dict_entries, stat_info.no_dict_bytes);
324         logf(LOG_LOG, "    Occurrences     Words");
325     }
326     prev = 1;
327     for (i = 0; stat_info.isam_bounds[i]; i++)
328     {
329         int here = stat_info.isam_bounds[i];
330         fprintf (stderr, "%7d-%-7d %7d\n",
331                  prev, here, stat_info.isam_occurrences[i]);
332         if ( (isamd) && (isamd->method->debug>0) &&
333              stat_info.isam_occurrences[i] )
334         {
335             logf(LOG_LOG,"%7d-%-7d %7d",
336                  prev, here, stat_info.isam_occurrences[i]);
337         }
338         prev = here+1;
339     }
340     fprintf (stderr, "%7d-        %7d\n",
341              prev, stat_info.isam_occurrences[i]);
342     if ( (isamd) && (isamd->method->debug>0) &&
343          ( stat_info.isam_occurrences[i] ||
344            stat_info.isam_occurrences[i+1])  )
345         logf(LOG_LOG,"%7d-        %7d",
346         prev, stat_info.isam_occurrences[i]);
347     rec_close (&records);
348     dict_close (dict);
349
350     if (isam)
351         is_close (isam);
352     if (isamc)
353         isc_close (isamc);
354     if (isams)
355         isams_close (isams);
356     if (isamh)
357         isamh_close (isamh);
358     if (isamd)
359         isamd_close (isamd);
360
361     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
362 }
363
364
365 /*
366  *
367  * $Log: invstat.c,v $
368  * Revision 1.18  1999-10-06 11:46:36  heikki
369  * mproved statistics on isam-d
370  *
371  * Revision 1.17  1999/08/20 08:28:37  heikki
372  * Log levels
373  *
374  * Revision 1.16  1999/08/18 08:38:22  heikki
375  * Memory leak hunting
376  *
377  * Revision 1.15  1999/08/18 08:34:53  heikki
378  * isamd
379  *
380  * Revision 1.14  1999/07/14 10:59:26  adam
381  * Changed functions isc_getmethod, isams_getmethod.
382  * Improved fatal error handling (such as missing EXPLAIN schema).
383  *
384  * Revision 1.13  1999/07/08 14:23:27  heikki
385  * Fixed a bug in isamh_pp_read and cleaned up a bit
386  *
387  * Revision 1.12  1999/07/06 12:28:04  adam
388  * Updated record index structure. Format includes version ID. Compression
389  * algorithm ID is stored for each record block.
390  *
391  * Revision 1.11  1999/05/15 14:36:38  adam
392  * Updated dictionary. Implemented "compression" of dictionary.
393  *
394  * Revision 1.10  1999/05/12 13:08:06  adam
395  * First version of ISAMS.
396  *
397  * Revision 1.9  1999/02/12 13:29:23  adam
398  * Implemented position-flag for registers.
399  *
400  * Revision 1.8  1999/02/02 14:50:53  adam
401  * Updated WIN32 code specific sections. Changed header.
402  *
403  * Revision 1.7  1998/03/13 15:30:50  adam
404  * New functions isc_block_used and isc_block_size. Fixed 'leak'
405  * in isc_alloc_block.
406  *
407  * Revision 1.6  1998/03/06 13:54:02  adam
408  * Fixed two nasty bugs in isc_merge.
409  *
410  * Revision 1.5  1997/09/17 12:19:13  adam
411  * Zebra version corresponds to YAZ version 1.4.
412  * Changed Zebra server so that it doesn't depend on global common_resource.
413  *
414  * Revision 1.4  1996/11/08 11:10:21  adam
415  * Buffers used during file match got bigger.
416  * Compressed ISAM support everywhere.
417  * Bug fixes regarding masking characters in queries.
418  * Redesigned Regexp-2 queries.
419  *
420  * Revision 1.3  1996/06/04 10:18:58  adam
421  * Minor changes - removed include of ctype.h.
422  *
423  * Revision 1.2  1996/05/22  08:25:56  adam
424  * Minor change.
425  *
426  * Revision 1.1  1996/05/14 14:04:34  adam
427  * In zebraidx, the 'stat' command is improved. Statistics about ISAM/DICT
428  * is collected.
429  */