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