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