perf_malloc.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @author Christian Grothoff
  18. * @file util/perf_malloc.c
  19. * @brief measure performance of allocation functions
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include <gauger.h>
  24. static uint64_t
  25. perf_malloc ()
  26. {
  27. uint64_t ret;
  28. ret = 0;
  29. for (size_t i = 1; i < 1024 * 1024; i += 1024)
  30. {
  31. ret += i;
  32. GNUNET_free (GNUNET_malloc (i));
  33. }
  34. return ret;
  35. }
  36. static uint64_t
  37. perf_realloc ()
  38. {
  39. uint64_t ret;
  40. ret = 0;
  41. for (size_t i = 10; i < 1024 * 1024 / 5; i += 1024)
  42. {
  43. char *ptr;
  44. ret += i;
  45. ptr = GNUNET_malloc (i);
  46. memset (ptr, 1, i);
  47. ptr = GNUNET_realloc (ptr, i + 5);
  48. for (size_t j = 0; j<i; j++)
  49. GNUNET_assert (1 == ptr[j]);
  50. memset (ptr, 6, i + 5);
  51. ptr = GNUNET_realloc (ptr, i - 5);
  52. for (size_t j = 0; j<i - 5; j++)
  53. GNUNET_assert (6 == ptr[j]);
  54. GNUNET_free (ptr);
  55. }
  56. return ret;
  57. }
  58. int
  59. main (int argc, char *argv[])
  60. {
  61. struct GNUNET_TIME_Absolute start;
  62. uint64_t kb;
  63. start = GNUNET_TIME_absolute_get ();
  64. kb = perf_malloc ();
  65. printf ("Malloc perf took %s\n",
  66. GNUNET_STRINGS_relative_time_to_string (
  67. GNUNET_TIME_absolute_get_duration (start),
  68. GNUNET_YES));
  69. GAUGER ("UTIL", "Allocation",
  70. kb / 1024 / (1
  71. + GNUNET_TIME_absolute_get_duration
  72. (start).rel_value_us / 1000LL), "kb/ms");
  73. start = GNUNET_TIME_absolute_get ();
  74. kb = perf_realloc ();
  75. printf ("Realloc perf took %s\n",
  76. GNUNET_STRINGS_relative_time_to_string (
  77. GNUNET_TIME_absolute_get_duration (start),
  78. GNUNET_YES));
  79. return 0;
  80. }
  81. /* end of perf_malloc.c */