155714Skris/* crypto/lhash/lh_stats.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8280297Sjkim *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280297Sjkim *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22280297Sjkim *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37280297Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40280297Sjkim *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52280297Sjkim *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include <string.h>
6155714Skris#include <stdlib.h>
62280297Sjkim/*
63280297Sjkim * If you wish to build this outside of SSLeay, remove the following lines
64280297Sjkim * and things should work as expected
65280297Sjkim */
6655714Skris#include "cryptlib.h"
6755714Skris
68109998Smarkm#ifndef OPENSSL_NO_BIO
69280297Sjkim# include <openssl/bio.h>
7068651Skris#endif
7155714Skris#include <openssl/lhash.h>
7255714Skris
73109998Smarkm#ifdef OPENSSL_NO_BIO
7455714Skris
7555714Skrisvoid lh_stats(LHASH *lh, FILE *out)
76280297Sjkim{
77280297Sjkim    fprintf(out, "num_items             = %lu\n", lh->num_items);
78280297Sjkim    fprintf(out, "num_nodes             = %u\n", lh->num_nodes);
79280297Sjkim    fprintf(out, "num_alloc_nodes       = %u\n", lh->num_alloc_nodes);
80280297Sjkim    fprintf(out, "num_expands           = %lu\n", lh->num_expands);
81280297Sjkim    fprintf(out, "num_expand_reallocs   = %lu\n", lh->num_expand_reallocs);
82280297Sjkim    fprintf(out, "num_contracts         = %lu\n", lh->num_contracts);
83280297Sjkim    fprintf(out, "num_contract_reallocs = %lu\n", lh->num_contract_reallocs);
84280297Sjkim    fprintf(out, "num_hash_calls        = %lu\n", lh->num_hash_calls);
85280297Sjkim    fprintf(out, "num_comp_calls        = %lu\n", lh->num_comp_calls);
86280297Sjkim    fprintf(out, "num_insert            = %lu\n", lh->num_insert);
87280297Sjkim    fprintf(out, "num_replace           = %lu\n", lh->num_replace);
88280297Sjkim    fprintf(out, "num_delete            = %lu\n", lh->num_delete);
89280297Sjkim    fprintf(out, "num_no_delete         = %lu\n", lh->num_no_delete);
90280297Sjkim    fprintf(out, "num_retrieve          = %lu\n", lh->num_retrieve);
91280297Sjkim    fprintf(out, "num_retrieve_miss     = %lu\n", lh->num_retrieve_miss);
92280297Sjkim    fprintf(out, "num_hash_comps        = %lu\n", lh->num_hash_comps);
93280297Sjkim# if 0
94280297Sjkim    fprintf(out, "p                     = %u\n", lh->p);
95280297Sjkim    fprintf(out, "pmax                  = %u\n", lh->pmax);
96280297Sjkim    fprintf(out, "up_load               = %lu\n", lh->up_load);
97280297Sjkim    fprintf(out, "down_load             = %lu\n", lh->down_load);
98280297Sjkim# endif
99280297Sjkim}
10055714Skris
10155714Skrisvoid lh_node_stats(LHASH *lh, FILE *out)
102280297Sjkim{
103280297Sjkim    LHASH_NODE *n;
104280297Sjkim    unsigned int i, num;
10555714Skris
106280297Sjkim    for (i = 0; i < lh->num_nodes; i++) {
107280297Sjkim        for (n = lh->b[i], num = 0; n != NULL; n = n->next)
108280297Sjkim            num++;
109280297Sjkim        fprintf(out, "node %6u -> %3u\n", i, num);
110280297Sjkim    }
111280297Sjkim}
11255714Skris
11355714Skrisvoid lh_node_usage_stats(LHASH *lh, FILE *out)
114280297Sjkim{
115280297Sjkim    LHASH_NODE *n;
116280297Sjkim    unsigned long num;
117280297Sjkim    unsigned int i;
118280297Sjkim    unsigned long total = 0, n_used = 0;
11955714Skris
120280297Sjkim    for (i = 0; i < lh->num_nodes; i++) {
121280297Sjkim        for (n = lh->b[i], num = 0; n != NULL; n = n->next)
122280297Sjkim            num++;
123280297Sjkim        if (num != 0) {
124280297Sjkim            n_used++;
125280297Sjkim            total += num;
126280297Sjkim        }
127280297Sjkim    }
128280297Sjkim    fprintf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
129280297Sjkim    fprintf(out, "%lu items\n", total);
130280297Sjkim    if (n_used == 0)
131280297Sjkim        return;
132280297Sjkim    fprintf(out, "load %d.%02d  actual load %d.%02d\n",
133280297Sjkim            (int)(total / lh->num_nodes),
134280297Sjkim            (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
135280297Sjkim            (int)(total / n_used), (int)((total % n_used) * 100 / n_used));
136280297Sjkim}
13755714Skris
13855714Skris#else
13955714Skris
140280297Sjkim# ifndef OPENSSL_NO_FP_API
141238405Sjkimvoid lh_stats(const _LHASH *lh, FILE *fp)
142280297Sjkim{
143280297Sjkim    BIO *bp;
14455714Skris
145280297Sjkim    bp = BIO_new(BIO_s_file());
146280297Sjkim    if (bp == NULL)
147280297Sjkim        goto end;
148280297Sjkim    BIO_set_fp(bp, fp, BIO_NOCLOSE);
149280297Sjkim    lh_stats_bio(lh, bp);
150280297Sjkim    BIO_free(bp);
151280297Sjkim end:;
152280297Sjkim}
15355714Skris
154238405Sjkimvoid lh_node_stats(const _LHASH *lh, FILE *fp)
155280297Sjkim{
156280297Sjkim    BIO *bp;
15755714Skris
158280297Sjkim    bp = BIO_new(BIO_s_file());
159280297Sjkim    if (bp == NULL)
160280297Sjkim        goto end;
161280297Sjkim    BIO_set_fp(bp, fp, BIO_NOCLOSE);
162280297Sjkim    lh_node_stats_bio(lh, bp);
163280297Sjkim    BIO_free(bp);
164280297Sjkim end:;
165280297Sjkim}
16655714Skris
167238405Sjkimvoid lh_node_usage_stats(const _LHASH *lh, FILE *fp)
168280297Sjkim{
169280297Sjkim    BIO *bp;
17055714Skris
171280297Sjkim    bp = BIO_new(BIO_s_file());
172280297Sjkim    if (bp == NULL)
173280297Sjkim        goto end;
174280297Sjkim    BIO_set_fp(bp, fp, BIO_NOCLOSE);
175280297Sjkim    lh_node_usage_stats_bio(lh, bp);
176280297Sjkim    BIO_free(bp);
177280297Sjkim end:;
178280297Sjkim}
17955714Skris
180280297Sjkim# endif
18155714Skris
182238405Sjkimvoid lh_stats_bio(const _LHASH *lh, BIO *out)
183280297Sjkim{
184280297Sjkim    BIO_printf(out, "num_items             = %lu\n", lh->num_items);
185280297Sjkim    BIO_printf(out, "num_nodes             = %u\n", lh->num_nodes);
186280297Sjkim    BIO_printf(out, "num_alloc_nodes       = %u\n", lh->num_alloc_nodes);
187280297Sjkim    BIO_printf(out, "num_expands           = %lu\n", lh->num_expands);
188280297Sjkim    BIO_printf(out, "num_expand_reallocs   = %lu\n", lh->num_expand_reallocs);
189280297Sjkim    BIO_printf(out, "num_contracts         = %lu\n", lh->num_contracts);
190280297Sjkim    BIO_printf(out, "num_contract_reallocs = %lu\n",
191280297Sjkim               lh->num_contract_reallocs);
192280297Sjkim    BIO_printf(out, "num_hash_calls        = %lu\n", lh->num_hash_calls);
193280297Sjkim    BIO_printf(out, "num_comp_calls        = %lu\n", lh->num_comp_calls);
194280297Sjkim    BIO_printf(out, "num_insert            = %lu\n", lh->num_insert);
195280297Sjkim    BIO_printf(out, "num_replace           = %lu\n", lh->num_replace);
196280297Sjkim    BIO_printf(out, "num_delete            = %lu\n", lh->num_delete);
197280297Sjkim    BIO_printf(out, "num_no_delete         = %lu\n", lh->num_no_delete);
198280297Sjkim    BIO_printf(out, "num_retrieve          = %lu\n", lh->num_retrieve);
199280297Sjkim    BIO_printf(out, "num_retrieve_miss     = %lu\n", lh->num_retrieve_miss);
200280297Sjkim    BIO_printf(out, "num_hash_comps        = %lu\n", lh->num_hash_comps);
201280297Sjkim# if 0
202280297Sjkim    BIO_printf(out, "p                     = %u\n", lh->p);
203280297Sjkim    BIO_printf(out, "pmax                  = %u\n", lh->pmax);
204280297Sjkim    BIO_printf(out, "up_load               = %lu\n", lh->up_load);
205280297Sjkim    BIO_printf(out, "down_load             = %lu\n", lh->down_load);
206280297Sjkim# endif
207280297Sjkim}
20855714Skris
209238405Sjkimvoid lh_node_stats_bio(const _LHASH *lh, BIO *out)
210280297Sjkim{
211280297Sjkim    LHASH_NODE *n;
212280297Sjkim    unsigned int i, num;
21355714Skris
214280297Sjkim    for (i = 0; i < lh->num_nodes; i++) {
215280297Sjkim        for (n = lh->b[i], num = 0; n != NULL; n = n->next)
216280297Sjkim            num++;
217280297Sjkim        BIO_printf(out, "node %6u -> %3u\n", i, num);
218280297Sjkim    }
219280297Sjkim}
22055714Skris
221238405Sjkimvoid lh_node_usage_stats_bio(const _LHASH *lh, BIO *out)
222280297Sjkim{
223280297Sjkim    LHASH_NODE *n;
224280297Sjkim    unsigned long num;
225280297Sjkim    unsigned int i;
226280297Sjkim    unsigned long total = 0, n_used = 0;
22755714Skris
228280297Sjkim    for (i = 0; i < lh->num_nodes; i++) {
229280297Sjkim        for (n = lh->b[i], num = 0; n != NULL; n = n->next)
230280297Sjkim            num++;
231280297Sjkim        if (num != 0) {
232280297Sjkim            n_used++;
233280297Sjkim            total += num;
234280297Sjkim        }
235280297Sjkim    }
236280297Sjkim    BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
237280297Sjkim    BIO_printf(out, "%lu items\n", total);
238280297Sjkim    if (n_used == 0)
239280297Sjkim        return;
240280297Sjkim    BIO_printf(out, "load %d.%02d  actual load %d.%02d\n",
241280297Sjkim               (int)(total / lh->num_nodes),
242280297Sjkim               (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
243280297Sjkim               (int)(total / n_used), (int)((total % n_used) * 100 / n_used));
244280297Sjkim}
24555714Skris
24655714Skris#endif
247