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