/src/openssl/crypto/lhash/lh_stats.c
Line | Count | Source (jump to first uncovered line) |
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 | | #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 | | #include "lhash_local.h" |
22 | | |
23 | | # ifndef OPENSSL_NO_STDIO |
24 | | void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp) |
25 | 0 | { |
26 | 0 | BIO *bp; |
27 | |
|
28 | 0 | bp = BIO_new(BIO_s_file()); |
29 | 0 | if (bp == NULL) |
30 | 0 | return; |
31 | 0 | BIO_set_fp(bp, fp, BIO_NOCLOSE); |
32 | 0 | OPENSSL_LH_stats_bio(lh, bp); |
33 | 0 | BIO_free(bp); |
34 | 0 | } |
35 | | |
36 | | void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp) |
37 | 0 | { |
38 | 0 | BIO *bp; |
39 | |
|
40 | 0 | bp = BIO_new(BIO_s_file()); |
41 | 0 | if (bp == NULL) |
42 | 0 | return; |
43 | 0 | BIO_set_fp(bp, fp, BIO_NOCLOSE); |
44 | 0 | OPENSSL_LH_node_stats_bio(lh, bp); |
45 | 0 | BIO_free(bp); |
46 | 0 | } |
47 | | |
48 | | void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp) |
49 | 0 | { |
50 | 0 | BIO *bp; |
51 | |
|
52 | 0 | bp = BIO_new(BIO_s_file()); |
53 | 0 | if (bp == NULL) |
54 | 0 | return; |
55 | 0 | BIO_set_fp(bp, fp, BIO_NOCLOSE); |
56 | 0 | OPENSSL_LH_node_usage_stats_bio(lh, bp); |
57 | 0 | BIO_free(bp); |
58 | 0 | } |
59 | | |
60 | | # endif |
61 | | |
62 | | void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out) |
63 | 0 | { |
64 | 0 | BIO_printf(out, "num_items = %lu\n", lh->num_items); |
65 | 0 | BIO_printf(out, "num_nodes = %u\n", lh->num_nodes); |
66 | 0 | BIO_printf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes); |
67 | 0 | BIO_printf(out, "num_expands = 0\n"); |
68 | 0 | BIO_printf(out, "num_expand_reallocs = 0\n"); |
69 | 0 | BIO_printf(out, "num_contracts = 0\n"); |
70 | 0 | BIO_printf(out, "num_contract_reallocs = 0\n"); |
71 | 0 | BIO_printf(out, "num_hash_calls = 0\n"); |
72 | 0 | BIO_printf(out, "num_comp_calls = 0\n"); |
73 | 0 | BIO_printf(out, "num_insert = 0\n"); |
74 | 0 | BIO_printf(out, "num_replace = 0\n"); |
75 | 0 | BIO_printf(out, "num_delete = 0\n"); |
76 | 0 | BIO_printf(out, "num_no_delete = 0\n"); |
77 | 0 | BIO_printf(out, "num_retrieve = 0\n"); |
78 | 0 | BIO_printf(out, "num_retrieve_miss = 0\n"); |
79 | 0 | BIO_printf(out, "num_hash_comps = 0\n"); |
80 | 0 | } |
81 | | |
82 | | void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out) |
83 | 0 | { |
84 | 0 | OPENSSL_LH_NODE *n; |
85 | 0 | unsigned int i, num; |
86 | |
|
87 | 0 | for (i = 0; i < lh->num_nodes; i++) { |
88 | 0 | for (n = lh->b[i], num = 0; n != NULL; n = n->next) |
89 | 0 | num++; |
90 | 0 | BIO_printf(out, "node %6u -> %3u\n", i, num); |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | | void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out) |
95 | 0 | { |
96 | 0 | OPENSSL_LH_NODE *n; |
97 | 0 | unsigned long num; |
98 | 0 | unsigned int i; |
99 | 0 | unsigned long total = 0, n_used = 0; |
100 | |
|
101 | 0 | for (i = 0; i < lh->num_nodes; i++) { |
102 | 0 | for (n = lh->b[i], num = 0; n != NULL; n = n->next) |
103 | 0 | num++; |
104 | 0 | if (num != 0) { |
105 | 0 | n_used++; |
106 | 0 | total += num; |
107 | 0 | } |
108 | 0 | } |
109 | 0 | BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes); |
110 | 0 | BIO_printf(out, "%lu items\n", total); |
111 | 0 | if (n_used == 0) |
112 | 0 | return; |
113 | 0 | BIO_printf(out, "load %d.%02d actual load %d.%02d\n", |
114 | 0 | (int)(total / lh->num_nodes), |
115 | 0 | (int)((total % lh->num_nodes) * 100 / lh->num_nodes), |
116 | 0 | (int)(total / n_used), (int)((total % n_used) * 100 / n_used)); |
117 | 0 | } |