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