lh_stats.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 1995-2017 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. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. /*
  13. * If you wish to build this outside of OpenSSL, remove the following lines
  14. * and things should work as expected
  15. */
  16. #include "internal/cryptlib.h"
  17. #include <openssl/bio.h>
  18. #include <openssl/lhash.h>
  19. #include "lhash_local.h"
  20. # ifndef OPENSSL_NO_STDIO
  21. void OPENSSL_LH_stats(const OPENSSL_LHASH *lh, FILE *fp)
  22. {
  23. BIO *bp;
  24. bp = BIO_new(BIO_s_file());
  25. if (bp == NULL)
  26. return;
  27. BIO_set_fp(bp, fp, BIO_NOCLOSE);
  28. OPENSSL_LH_stats_bio(lh, bp);
  29. BIO_free(bp);
  30. }
  31. void OPENSSL_LH_node_stats(const OPENSSL_LHASH *lh, FILE *fp)
  32. {
  33. BIO *bp;
  34. bp = BIO_new(BIO_s_file());
  35. if (bp == NULL)
  36. return;
  37. BIO_set_fp(bp, fp, BIO_NOCLOSE);
  38. OPENSSL_LH_node_stats_bio(lh, bp);
  39. BIO_free(bp);
  40. }
  41. void OPENSSL_LH_node_usage_stats(const OPENSSL_LHASH *lh, FILE *fp)
  42. {
  43. BIO *bp;
  44. bp = BIO_new(BIO_s_file());
  45. if (bp == NULL)
  46. return;
  47. BIO_set_fp(bp, fp, BIO_NOCLOSE);
  48. OPENSSL_LH_node_usage_stats_bio(lh, bp);
  49. BIO_free(bp);
  50. }
  51. # endif
  52. void OPENSSL_LH_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
  53. {
  54. BIO_printf(out, "num_items = %lu\n", lh->num_items);
  55. BIO_printf(out, "num_nodes = %u\n", lh->num_nodes);
  56. BIO_printf(out, "num_alloc_nodes = %u\n", lh->num_alloc_nodes);
  57. BIO_printf(out, "num_expands = %lu\n", lh->num_expands);
  58. BIO_printf(out, "num_expand_reallocs = %lu\n", lh->num_expand_reallocs);
  59. BIO_printf(out, "num_contracts = %lu\n", lh->num_contracts);
  60. BIO_printf(out, "num_contract_reallocs = %lu\n", lh->num_contract_reallocs);
  61. BIO_printf(out, "num_hash_calls = %lu\n", lh->num_hash_calls);
  62. BIO_printf(out, "num_comp_calls = %lu\n", lh->num_comp_calls);
  63. BIO_printf(out, "num_insert = %lu\n", lh->num_insert);
  64. BIO_printf(out, "num_replace = %lu\n", lh->num_replace);
  65. BIO_printf(out, "num_delete = %lu\n", lh->num_delete);
  66. BIO_printf(out, "num_no_delete = %lu\n", lh->num_no_delete);
  67. BIO_printf(out, "num_retrieve = %lu\n", lh->num_retrieve);
  68. BIO_printf(out, "num_retrieve_miss = %lu\n", lh->num_retrieve_miss);
  69. BIO_printf(out, "num_hash_comps = %lu\n", lh->num_hash_comps);
  70. }
  71. void OPENSSL_LH_node_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
  72. {
  73. OPENSSL_LH_NODE *n;
  74. unsigned int i, num;
  75. for (i = 0; i < lh->num_nodes; i++) {
  76. for (n = lh->b[i], num = 0; n != NULL; n = n->next)
  77. num++;
  78. BIO_printf(out, "node %6u -> %3u\n", i, num);
  79. }
  80. }
  81. void OPENSSL_LH_node_usage_stats_bio(const OPENSSL_LHASH *lh, BIO *out)
  82. {
  83. OPENSSL_LH_NODE *n;
  84. unsigned long num;
  85. unsigned int i;
  86. unsigned long total = 0, n_used = 0;
  87. for (i = 0; i < lh->num_nodes; i++) {
  88. for (n = lh->b[i], num = 0; n != NULL; n = n->next)
  89. num++;
  90. if (num != 0) {
  91. n_used++;
  92. total += num;
  93. }
  94. }
  95. BIO_printf(out, "%lu nodes used out of %u\n", n_used, lh->num_nodes);
  96. BIO_printf(out, "%lu items\n", total);
  97. if (n_used == 0)
  98. return;
  99. BIO_printf(out, "load %d.%02d actual load %d.%02d\n",
  100. (int)(total / lh->num_nodes),
  101. (int)((total % lh->num_nodes) * 100 / lh->num_nodes),
  102. (int)(total / n_used), (int)((total % n_used) * 100 / n_used));
  103. }