0644c756e83216c5c56d5495df32cad3b965de3c
[openssl.git] / crypto / lhash / lh_stats.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 /*
14  * If you wish to build this outside of OpenSSL, remove the following lines
15  * and things should work as expected
16  */
17 #include "internal/cryptlib.h"
18
19 #include <openssl/bio.h>
20 #include <openssl/lhash.h>
21
22 # ifndef OPENSSL_NO_STDIO
23 void lh_stats(const _LHASH *lh, FILE *fp)
24 {
25     BIO *bp;
26
27     bp = BIO_new(BIO_s_file());
28     if (bp == NULL)
29         goto end;
30     BIO_set_fp(bp, fp, BIO_NOCLOSE);
31     lh_stats_bio(lh, bp);
32     BIO_free(bp);
33  end:;
34 }
35
36 void lh_node_stats(const _LHASH *lh, FILE *fp)
37 {
38     BIO *bp;
39
40     bp = BIO_new(BIO_s_file());
41     if (bp == NULL)
42         goto end;
43     BIO_set_fp(bp, fp, BIO_NOCLOSE);
44     lh_node_stats_bio(lh, bp);
45     BIO_free(bp);
46  end:;
47 }
48
49 void lh_node_usage_stats(const _LHASH *lh, FILE *fp)
50 {
51     BIO *bp;
52
53     bp = BIO_new(BIO_s_file());
54     if (bp == NULL)
55         goto end;
56     BIO_set_fp(bp, fp, BIO_NOCLOSE);
57     lh_node_usage_stats_bio(lh, bp);
58     BIO_free(bp);
59  end:;
60 }
61
62 # endif
63
64 void lh_stats_bio(const _LHASH *lh, BIO *out)
65 {
66     BIO_printf(out, "num_items             = %lu\n", lh->num_items);
67     BIO_printf(out, "num_nodes             = %u\n", lh->num_nodes);
68     BIO_printf(out, "num_alloc_nodes       = %u\n", lh->num_alloc_nodes);
69     BIO_printf(out, "num_expands           = %lu\n", lh->num_expands);
70     BIO_printf(out, "num_expand_reallocs   = %lu\n", lh->num_expand_reallocs);
71     BIO_printf(out, "num_contracts         = %lu\n", lh->num_contracts);
72     BIO_printf(out, "num_contract_reallocs = %lu\n",
73                lh->num_contract_reallocs);
74     BIO_printf(out, "num_hash_calls        = %lu\n", lh->num_hash_calls);
75     BIO_printf(out, "num_comp_calls        = %lu\n", lh->num_comp_calls);
76     BIO_printf(out, "num_insert            = %lu\n", lh->num_insert);
77     BIO_printf(out, "num_replace           = %lu\n", lh->num_replace);
78     BIO_printf(out, "num_delete            = %lu\n", lh->num_delete);
79     BIO_printf(out, "num_no_delete         = %lu\n", lh->num_no_delete);
80     BIO_printf(out, "num_retrieve          = %lu\n", lh->num_retrieve);
81     BIO_printf(out, "num_retrieve_miss     = %lu\n", lh->num_retrieve_miss);
82     BIO_printf(out, "num_hash_comps        = %lu\n", lh->num_hash_comps);
83 }
84
85 void lh_node_stats_bio(const _LHASH *lh, BIO *out)
86 {
87     LHASH_NODE *n;
88     unsigned int i, num;
89
90     for (i = 0; i < lh->num_nodes; i++) {
91         for (n = lh->b[i], num = 0; n != NULL; n = n->next)
92             num++;
93         BIO_printf(out, "node %6u -> %3u\n", i, num);
94     }
95 }
96
97 void lh_node_usage_stats_bio(const _LHASH *lh, BIO *out)
98 {
99     LHASH_NODE *n;
100     unsigned long num;
101     unsigned int i;
102     unsigned long total = 0, n_used = 0;
103
104     for (i = 0; i < lh->num_nodes; i++) {
105         for (n = lh->b[i], num = 0; n != NULL; n = n->next)
106             num++;
107         if (num != 0) {
108             n_used++;
109             total += num;
110         }
111     }
112     BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
113     BIO_printf(out, "%lu items\n", total);
114     if (n_used == 0)
115         return;
116     BIO_printf(out, "load %d.%02d  actual load %d.%02d\n",
117                (int)(total / lh->num_nodes),
118                (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
119                (int)(total / n_used), (int)((total % n_used) * 100 / n_used));
120 }