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