bench-malloc-simple.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Benchmark malloc and free functions.
  2. Copyright (C) 2019-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef TEST_FUNC
  16. # define TEST_FUNC(size) malloc(size)
  17. # define TEST_NAME "malloc"
  18. #endif
  19. #include <pthread.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <malloc.h>
  23. #include <sys/resource.h>
  24. #include "bench-timing.h"
  25. #include "json-lib.h"
  26. /* Benchmark the malloc/free performance of a varying number of blocks of a
  27. given size. This enables performance tracking of the t-cache.
  28. It tests 3 different scenarios: single-threaded using main arena,
  29. multi-threaded using thread-arena, and main arena with SINGLE_THREAD_P
  30. false. */
  31. #define NUM_ITERS 5000000
  32. #define NUM_ALLOCS 4
  33. #define MAX_ALLOCS 1600
  34. typedef struct
  35. {
  36. size_t iters;
  37. size_t size;
  38. int n;
  39. timing_t elapsed;
  40. } malloc_args;
  41. static void
  42. do_benchmark (malloc_args *args, int **arr)
  43. {
  44. timing_t start, stop;
  45. size_t iters = args->iters;
  46. size_t size = args->size;
  47. int n = args->n;
  48. TIMING_NOW (start);
  49. for (int j = 0; j < iters; j++)
  50. {
  51. for (int i = 0; i < n; i++)
  52. arr[i] = TEST_FUNC (size);
  53. for (int i = 0; i < n; i++)
  54. free (arr[i]);
  55. }
  56. TIMING_NOW (stop);
  57. TIMING_DIFF (args->elapsed, start, stop);
  58. }
  59. static malloc_args tests[3][NUM_ALLOCS];
  60. static int allocs[NUM_ALLOCS] = { 25, 100, 400, MAX_ALLOCS };
  61. static void *
  62. thread_test (void *p)
  63. {
  64. int **arr = (int**)p;
  65. /* Run benchmark multi-threaded. */
  66. for (int i = 0; i < NUM_ALLOCS; i++)
  67. do_benchmark (&tests[2][i], arr);
  68. return p;
  69. }
  70. void
  71. bench (unsigned long size)
  72. {
  73. size_t iters = NUM_ITERS;
  74. int **arr = (int**) malloc (MAX_ALLOCS * sizeof (void*));
  75. for (int t = 0; t < 3; t++)
  76. for (int i = 0; i < NUM_ALLOCS; i++)
  77. {
  78. tests[t][i].n = allocs[i];
  79. tests[t][i].size = size;
  80. tests[t][i].iters = iters / allocs[i];
  81. /* Do a quick warmup run. */
  82. if (t == 0)
  83. do_benchmark (&tests[0][i], arr);
  84. }
  85. /* Run benchmark single threaded in main_arena. */
  86. for (int i = 0; i < NUM_ALLOCS; i++)
  87. do_benchmark (&tests[0][i], arr);
  88. /* Run benchmark in a thread_arena. */
  89. pthread_t t;
  90. pthread_create (&t, NULL, thread_test, (void*)arr);
  91. pthread_join (t, NULL);
  92. /* Repeat benchmark in main_arena with SINGLE_THREAD_P == false. */
  93. for (int i = 0; i < NUM_ALLOCS; i++)
  94. do_benchmark (&tests[1][i], arr);
  95. free (arr);
  96. json_ctx_t json_ctx;
  97. json_init (&json_ctx, 0, stdout);
  98. json_document_begin (&json_ctx);
  99. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  100. json_attr_object_begin (&json_ctx, "functions");
  101. json_attr_object_begin (&json_ctx, TEST_NAME);
  102. char s[100];
  103. double iters2 = iters;
  104. json_attr_object_begin (&json_ctx, "");
  105. json_attr_double (&json_ctx, "malloc_block_size", size);
  106. struct rusage usage;
  107. getrusage (RUSAGE_SELF, &usage);
  108. json_attr_double (&json_ctx, "max_rss", usage.ru_maxrss);
  109. for (int i = 0; i < NUM_ALLOCS; i++)
  110. {
  111. sprintf (s, "main_arena_st_allocs_%04d_time", allocs[i]);
  112. json_attr_double (&json_ctx, s, tests[0][i].elapsed / iters2);
  113. }
  114. for (int i = 0; i < NUM_ALLOCS; i++)
  115. {
  116. sprintf (s, "main_arena_mt_allocs_%04d_time", allocs[i]);
  117. json_attr_double (&json_ctx, s, tests[1][i].elapsed / iters2);
  118. }
  119. for (int i = 0; i < NUM_ALLOCS; i++)
  120. {
  121. sprintf (s, "thread_arena__allocs_%04d_time", allocs[i]);
  122. json_attr_double (&json_ctx, s, tests[2][i].elapsed / iters2);
  123. }
  124. json_attr_object_end (&json_ctx);
  125. json_attr_object_end (&json_ctx);
  126. json_attr_object_end (&json_ctx);
  127. json_document_end (&json_ctx);
  128. }
  129. static void usage (const char *name)
  130. {
  131. fprintf (stderr, "%s: <alloc_size>\n", name);
  132. exit (1);
  133. }
  134. int
  135. main (int argc, char **argv)
  136. {
  137. long val = 16;
  138. if (argc == 2)
  139. val = strtol (argv[1], NULL, 0);
  140. if (argc > 2 || val <= 0)
  141. usage (argv[0]);
  142. bench (val);
  143. return 0;
  144. }