1 /*
2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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 #define OPENSSL_SUPPRESS_DEPRECATED
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 /*
16 * If you wish to build this outside of OpenSSL, remove the following lines
17 * and things should work as expected
18 */
19 #include "internal/cryptlib.h"
20
21 #include <openssl/bio.h>
22 #include <openssl/lhash.h>
23 #include "lhash_local.h"
24
25 # ifndef OPENSSL_NO_STDIO
26 # ifndef OPENSSL_NO_DEPRECATED_3_1
OPENSSL_LH_stats(const OPENSSL_LHASH * lh,FILE * fp)27 void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp)
28 {
29 BIO *bp;
30
31 bp = BIO_new(BIO_s_file());
32 if (bp == NULL)
33 return;
34 BIO_set_fp(bp, fp, BIO_NOCLOSE);
35 OPENSSL_LH_stats_bio(lh, bp);
36 BIO_free(bp);
37 }
38
OPENSSL_LH_node_stats(const OPENSSL_LHASH * lh,FILE * fp)39 void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp)
40 {
41 BIO *bp;
42
43 bp = BIO_new(BIO_s_file());
44 if (bp == NULL)
45 return;
46 BIO_set_fp(bp, fp, BIO_NOCLOSE);
47 OPENSSL_LH_node_stats_bio(lh, bp);
48 BIO_free(bp);
49 }
50
OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH * lh,FILE * fp)51 void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp)
52 {
53 BIO *bp;
54
55 bp = BIO_new(BIO_s_file());
56 if (bp == NULL)
57 return;
58 BIO_set_fp(bp, fp, BIO_NOCLOSE);
59 OPENSSL_LH_node_usage_stats_bio(lh, bp);
60 BIO_free(bp);
61 }
62 # endif
63 # endif
64
65 # ifndef OPENSSL_NO_DEPRECATED_3_1
66 /*
67 * These functions are implemented as separate static functions as they are
68 * called from the stdio functions above and calling deprecated functions will
69 * generate a warning.
70 */
OPENSSL_LH_stats_bio(const OPENSSL_LHASH * lh,BIO * out)71 void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
72 {
73 BIO_printf(out, "num_items = %lu\n", lh->num_items);
74 BIO_printf(out, "num_nodes = %u\n", lh->num_nodes);
75 BIO_printf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes);
76 BIO_printf(out, "num_expands = 0\n");
77 BIO_printf(out, "num_expand_reallocs = 0\n");
78 BIO_printf(out, "num_contracts = 0\n");
79 BIO_printf(out, "num_contract_reallocs = 0\n");
80 BIO_printf(out, "num_hash_calls = 0\n");
81 BIO_printf(out, "num_comp_calls = 0\n");
82 BIO_printf(out, "num_insert = 0\n");
83 BIO_printf(out, "num_replace = 0\n");
84 BIO_printf(out, "num_delete = 0\n");
85 BIO_printf(out, "num_no_delete = 0\n");
86 BIO_printf(out, "num_retrieve = 0\n");
87 BIO_printf(out, "num_retrieve_miss = 0\n");
88 BIO_printf(out, "num_hash_comps = 0\n");
89 }
90
OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH * lh,BIO * out)91 void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
92 {
93 OPENSSL_LH_NODE *n;
94 unsigned int i, num;
95
96 for (i = 0; i < lh->num_nodes; i++) {
97 for (n = lh->b[i], num = 0; n != NULL; n = n->next)
98 num++;
99 BIO_printf(out, "node %6u -> %3u\n", i, num);
100 }
101 }
102
OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH * lh,BIO * out)103 void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
104 {
105 OPENSSL_LH_NODE *n;
106 unsigned long num;
107 unsigned int i;
108 unsigned long total = 0, n_used = 0;
109
110 for (i = 0; i < lh->num_nodes; i++) {
111 for (n = lh->b[i], num = 0; n != NULL; n = n->next)
112 num++;
113 if (num != 0) {
114 n_used++;
115 total += num;
116 }
117 }
118 BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
119 BIO_printf(out, "%lu items\n", total);
120 if (n_used == 0)
121 return;
122 BIO_printf(out, "load %d.%02d actual load %d.%02d\n",
123 (int)(total / lh->num_nodes),
124 (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
125 (int)(total / n_used), (int)((total % n_used) * 100 / n_used));
126 }
127 # endif
128