lh_stats.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #define OPENSSL_SUPPRESS_DEPRECATED
  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. #include <openssl/bio.h>
  19. #include <openssl/lhash.h>
  20. #include "lhash_local.h"
  21. # ifndef OPENSSL_NO_STDIO
  22. # ifndef OPENSSL_NO_DEPRECATED_3_1
  23. void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp)
  24. {
  25. BIO *bp;
  26. bp = BIO_new(BIO_s_file());
  27. if (bp == NULL)
  28. return;
  29. BIO_set_fp(bp, fp, BIO_NOCLOSE);
  30. OPENSSL_LH_stats_bio(lh, bp);
  31. BIO_free(bp);
  32. }
  33. void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp)
  34. {
  35. BIO *bp;
  36. bp = BIO_new(BIO_s_file());
  37. if (bp == NULL)
  38. return;
  39. BIO_set_fp(bp, fp, BIO_NOCLOSE);
  40. OPENSSL_LH_node_stats_bio(lh, bp);
  41. BIO_free(bp);
  42. }
  43. void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp)
  44. {
  45. BIO *bp;
  46. bp = BIO_new(BIO_s_file());
  47. if (bp == NULL)
  48. return;
  49. BIO_set_fp(bp, fp, BIO_NOCLOSE);
  50. OPENSSL_LH_node_usage_stats_bio(lh, bp);
  51. BIO_free(bp);
  52. }
  53. # endif
  54. # endif
  55. # ifndef OPENSSL_NO_DEPRECATED_3_1
  56. /*
  57. * These functions are implemented as separate static functions as they are
  58. * called from the stdio functions above and calling deprecated functions will
  59. * generate a warning.
  60. */
  61. void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
  62. {
  63. BIO_printf(out, "num_items = %lu\n", lh->num_items);
  64. BIO_printf(out, "num_nodes = %u\n", lh->num_nodes);
  65. BIO_printf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes);
  66. BIO_printf(out, "num_expands = 0\n");
  67. BIO_printf(out, "num_expand_reallocs = 0\n");
  68. BIO_printf(out, "num_contracts = 0\n");
  69. BIO_printf(out, "num_contract_reallocs = 0\n");
  70. BIO_printf(out, "num_hash_calls = 0\n");
  71. BIO_printf(out, "num_comp_calls = 0\n");
  72. BIO_printf(out, "num_insert = 0\n");
  73. BIO_printf(out, "num_replace = 0\n");
  74. BIO_printf(out, "num_delete = 0\n");
  75. BIO_printf(out, "num_no_delete = 0\n");
  76. BIO_printf(out, "num_retrieve = 0\n");
  77. BIO_printf(out, "num_retrieve_miss = 0\n");
  78. BIO_printf(out, "num_hash_comps = 0\n");
  79. }
  80. void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
  81. {
  82. OPENSSL_LH_NODE *n;
  83. unsigned int i, num;
  84. for (i = 0; i < lh->num_nodes; i++) {
  85. for (n = lh->b[i], num = 0; n != NULL; n = n->next)
  86. num++;
  87. BIO_printf(out, "node %6u -> %3u\n", i, num);
  88. }
  89. }
  90. void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
  91. {
  92. OPENSSL_LH_NODE *n;
  93. unsigned long num;
  94. unsigned int i;
  95. unsigned long total = 0, n_used = 0;
  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. if (num != 0) {
  100. n_used++;
  101. total += num;
  102. }
  103. }
  104. BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
  105. BIO_printf(out, "%lu items\n", total);
  106. if (n_used == 0)
  107. return;
  108. BIO_printf(out, "load %d.%02d actual load %d.%02d\n",
  109. (int)(total / lh->num_nodes),
  110. (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
  111. (int)(total / n_used), (int)((total % n_used) * 100 / n_used));
  112. }
  113. # endif