Added a few casts from zint to double/int where appropriate.
[idzebra-moved-to-github.git] / index / invstat.c
1 /* $Id: invstat.c,v 1.44 2005-03-08 14:02:09 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27
28 #include "index.h"
29
30 struct inv_stat_info {
31     ZebraHandle zh;
32     zint no_isam_entries[9];
33     int no_dict_entries;
34     int no_dict_bytes;
35     int isam_bounds[20];
36     int isam_occurrences[20];
37     char tmp[128];
38     int isamb_levels[10][5];
39     zint isamb_sizes[10];
40     zint isamb_blocks[10];
41     unsigned long cksum;
42     int dumpwords;
43 };
44
45 #define SINGLETON_TYPE 8 /* the type to use for singletons that */ 
46                          /* have no block and no block type */
47
48 static void print_dict_item (ZebraMaps zm, const char *s, zint count,
49             int firstsys, int firstseq, int lastsys, int lastseq )
50 {
51     int reg_type = s[1];
52     char keybuf[IT_MAX_WORD+1];
53     char *to = keybuf;
54     const char *from = s + 2;
55
56     while (*from)
57     {
58         const char *res = zebra_maps_output (zm, reg_type, &from);
59         if (!res)
60             *to++ = *from++;
61         else
62             while (*res)
63                 *to++ = *res++;
64     }
65     *to = '\0';
66     /* yaz_log (YLOG_LOG, "%s", keybuf); */
67     printf("%10" ZINT_FORMAT0 " %s %d.%d - %d.%d\n", count, keybuf,
68               firstsys,firstseq, lastsys,lastseq);
69 }
70
71 static int inv_stat_handle (char *name, const char *info, int pos,
72                             void *client)
73 {
74     zint occur = 0;
75     int i = 0;
76     struct inv_stat_info *stat_info = (struct inv_stat_info*) client;
77     ISAMS_P isam_p;
78     int firstsys=-1;
79     int firstseq=-1;
80     int lastsys=-1;
81     int lastseq=-1;
82
83     stat_info->no_dict_entries++;
84     stat_info->no_dict_bytes += strlen(name);
85
86     assert (*info == sizeof(ISAMS_P));
87     memcpy (&isam_p, info+1, sizeof(ISAMS_P));
88
89     if (stat_info->zh->reg->isams)
90     {
91         ISAMS_PP pp;
92         int occurx = 0;
93         struct it_key key;
94
95         pp = isams_pp_open (stat_info->zh->reg->isams, isam_p);
96         occur = isams_pp_num (pp);
97         while (isams_pp_read(pp, &key))
98         {
99             occurx++;
100         }
101         assert (occurx == occur);
102         stat_info->no_isam_entries[0] += occur;
103         isams_pp_close (pp);
104     }
105     if (stat_info->zh->reg->isamc)
106     {
107         ISAMC_PP pp;
108         zint occurx = 0;
109         struct it_key key;
110
111         pp = isc_pp_open (stat_info->zh->reg->isamc, isam_p);
112         occur = isc_pp_num (pp);
113         while (isc_pp_read(pp, &key))
114         {
115             occurx++;
116         }
117         assert (occurx == occur);
118         stat_info->no_isam_entries[isc_type(isam_p)] += occur;
119         isc_pp_close (pp);
120     }
121     if (stat_info->zh->reg->isamb)
122     {
123         ISAMB_PP pp;
124         struct it_key key;
125         int cat = (int) (isam_p & 3);
126         int level;
127         zint size;
128         zint blocks;
129         
130         pp = isamb_pp_open_x(stat_info->zh->reg->isamb, isam_p, &level, 0);
131
132         while (isamb_pp_read(pp, &key))
133         {
134             occur++;
135         }
136         isamb_pp_close_x (pp, &size, &blocks);
137         stat_info->isamb_blocks[cat] += blocks;
138         stat_info->isamb_sizes[cat] += size;
139         if (level > 4)
140             level = 4;
141         stat_info->isamb_levels[cat][level] ++;
142         stat_info->no_isam_entries[cat] += occur;
143     }
144     i=0;
145     while (occur > stat_info->isam_bounds[i] && stat_info->isam_bounds[i])
146         i++;
147     ++(stat_info->isam_occurrences[i]);
148     if (stat_info->dumpwords)
149        print_dict_item(stat_info->zh->reg->zebra_maps, name, occur,
150           firstsys,firstseq, lastsys, lastseq);
151     return 0;
152 }
153
154 int zebra_register_statistics (ZebraHandle zh, int dumpdict)
155 {
156     int i, prev;
157     int before = 0;
158     zint occur;
159     int after = 1000000000;
160     struct inv_stat_info stat_info;
161     char term_dict[2*IT_MAX_WORD+2];
162
163     if (zebra_begin_read (zh))
164         return 1;
165
166     stat_info.zh = zh;
167     stat_info.dumpwords=dumpdict;
168
169     term_dict[0] = 1;
170     term_dict[1] = 0;
171
172     for (i = 0; i<=SINGLETON_TYPE; i++)
173         stat_info.no_isam_entries[i] = 0;
174     stat_info.no_dict_entries = 0;
175     stat_info.no_dict_bytes = 0;
176     stat_info.isam_bounds[0] = 1;
177     stat_info.isam_bounds[1] = 2;
178     stat_info.isam_bounds[2] = 3;
179     stat_info.isam_bounds[3] = 6;
180     stat_info.isam_bounds[4] = 10;
181     stat_info.isam_bounds[5] = 20;
182     stat_info.isam_bounds[6] = 30;
183     stat_info.isam_bounds[7] = 50;
184     stat_info.isam_bounds[8] = 100;
185     stat_info.isam_bounds[9] = 200;
186     stat_info.isam_bounds[10] = 5000;
187     stat_info.isam_bounds[11] = 10000;
188     stat_info.isam_bounds[12] = 20000;
189     stat_info.isam_bounds[13] = 50000;
190     stat_info.isam_bounds[14] = 100000;
191     stat_info.isam_bounds[15] = 200000;
192     stat_info.isam_bounds[16] = 500000;
193     stat_info.isam_bounds[17] = 1000000;
194     stat_info.isam_bounds[18] = 0;
195
196     stat_info.cksum = 0;
197
198     for (i = 0; i<20; i++)
199         stat_info.isam_occurrences[i] = 0;
200
201     for (i = 0; i<10; i++)
202     {
203         int j;
204         for (j = 0; j<5; j++)
205             stat_info.isamb_levels[i][j] = 0;
206         stat_info.isamb_sizes[i] = 0;
207         stat_info.isamb_blocks[i] = 0;
208     }
209
210     dict_scan (zh->reg->dict, term_dict, &before, &after, &stat_info,
211                inv_stat_handle);
212
213     if (zh->reg->isamc)
214     {
215         fprintf (stdout, "   Blocks    Occur  Size KB   Bytes/Entry\n");
216         for (i = 0; isc_block_used (zh->reg->isamc, i) >= 0; i++)
217         {
218             fprintf (stdout, " %8" ZINT_FORMAT0 " %8" ZINT_FORMAT0,
219                      isc_block_used (zh->reg->isamc, i),
220                      stat_info.no_isam_entries[i]);
221
222             if (stat_info.no_isam_entries[i])
223                 fprintf (stdout, " %8d   %f",
224                          (int) ((1023.0 + (double)
225                                  isc_block_used(zh->reg->isamc, i) *
226                                  isc_block_size(zh->reg->isamc,i))/1024),
227                          ((double) isc_block_used(zh->reg->isamc, i) *
228                           isc_block_size(zh->reg->isamc,i))/
229                          stat_info.no_isam_entries[i]);
230             fprintf (stdout, "\n");
231         }
232     }
233
234     if (zh->reg->isamb)
235     {
236         for (i = 0; i<4; i++)
237         {
238             int j;
239             int bsize = isamb_block_info(zh->reg->isamb, i);
240             if (bsize < 0)
241                 break;
242             fprintf (stdout, "Category   %d\n", i);
243             fprintf (stdout, "Block size %d\n", bsize);
244             fprintf (stdout, "Blocks:    %d\n", stat_info.isamb_blocks[i]);
245             fprintf (stdout, "Size:      %d\n", stat_info.isamb_sizes[i]);
246             fprintf (stdout, "Entries:   " ZINT_FORMAT "\n",
247                      stat_info.no_isam_entries[i]);
248             fprintf (stdout, "Total      %d\n", stat_info.isamb_blocks[i]*
249                      bsize);
250             for (j = 0; j<5; j++)
251                 if (stat_info.isamb_levels[i][j])
252                     fprintf (stdout, "Level%d     %d\n", j,
253                              stat_info.isamb_levels[i][j]);
254             fprintf (stdout, "\n");
255         }
256     }
257     fprintf (stdout, "Checksum       %08lX\n", stat_info.cksum);
258
259     fprintf (stdout, "Distinct words %d\n", stat_info.no_dict_entries);
260     occur = 0;
261     for (i = 0; i<9; i++)
262         occur += stat_info.no_isam_entries[i];
263     fprintf (stdout, "Word pos       " ZINT_FORMAT "\n", occur);
264     fprintf (stdout, "    Occurrences     Words\n");
265     prev = 1;
266     for (i = 0; stat_info.isam_bounds[i]; i++)
267     {
268         int here = stat_info.isam_bounds[i];
269         fprintf (stdout, "%7d-%-7d %7d\n",
270                  prev, here, stat_info.isam_occurrences[i]);
271         prev = here+1;
272     }
273     fprintf (stdout, "%7d-        %7d\n",
274              prev, stat_info.isam_occurrences[i]);
275     xmalloc_trav("unfreed"); /*! while hunting memory leaks */    
276     zebra_end_read (zh);
277     return 0;
278 }
279