disclaim_bench.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2011 by Hewlett-Packard Company. All rights reserved.
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. *
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #ifdef HAVE_CONFIG_H
  18. # include "config.h"
  19. #endif
  20. #include "gc_disclaim.h"
  21. /* Include gc_priv.h is done after including GC public headers, so */
  22. /* that GC_BUILD has no effect on the public prototypes. */
  23. #include "private/gc_priv.h" /* for CLOCK_TYPE, COVERT_DATAFLOW, GC_random */
  24. #ifdef LINT2
  25. # undef rand
  26. # define rand() (int)GC_random()
  27. #endif
  28. #define my_assert(e) \
  29. if (!(e)) { \
  30. fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \
  31. exit(-1); \
  32. }
  33. static int free_count = 0;
  34. struct testobj_s {
  35. struct testobj_s *keep_link;
  36. int i;
  37. };
  38. typedef struct testobj_s *testobj_t;
  39. void GC_CALLBACK testobj_finalize(void *obj, void *carg)
  40. {
  41. ++*(int *)carg;
  42. my_assert(((testobj_t)obj)->i == 109);
  43. ((testobj_t)obj)->i = 110;
  44. }
  45. static const struct GC_finalizer_closure fclos = {
  46. testobj_finalize,
  47. &free_count
  48. };
  49. testobj_t testobj_new(int model)
  50. {
  51. testobj_t obj;
  52. switch (model) {
  53. case 0:
  54. obj = GC_NEW(struct testobj_s);
  55. if (obj != NULL)
  56. GC_REGISTER_FINALIZER_NO_ORDER(obj, testobj_finalize,
  57. &free_count, NULL, NULL);
  58. break;
  59. case 1:
  60. obj = (testobj_t)GC_finalized_malloc(sizeof(struct testobj_s),
  61. &fclos);
  62. break;
  63. case 2:
  64. obj = GC_NEW(struct testobj_s);
  65. break;
  66. default:
  67. exit(-1);
  68. }
  69. if (obj == NULL) {
  70. fprintf(stderr, "Out of memory!\n");
  71. exit(3);
  72. }
  73. my_assert(obj->i == 0 && obj->keep_link == NULL);
  74. obj->i = 109;
  75. return obj;
  76. }
  77. #define ALLOC_CNT (4*1024*1024)
  78. #define KEEP_CNT (32*1024)
  79. static char const *model_str[3] = {
  80. "regular finalization",
  81. "finalize on reclaim",
  82. "no finalization"
  83. };
  84. int main(int argc, char **argv)
  85. {
  86. int i;
  87. int model, model_min, model_max;
  88. testobj_t *keep_arr;
  89. GC_INIT();
  90. GC_init_finalized_malloc();
  91. if (argc == 2 && strcmp(argv[1], "--help") == 0) {
  92. fprintf(stderr,
  93. "Usage: %s [FINALIZATION_MODEL]\n"
  94. "\t0 -- original finalization\n"
  95. "\t1 -- finalization on reclaim\n"
  96. "\t2 -- no finalization\n", argv[0]);
  97. return 1;
  98. }
  99. if (argc == 2) {
  100. model_min = model_max = (int)COVERT_DATAFLOW(atoi(argv[1]));
  101. if (model_min < 0 || model_max > 2)
  102. exit(2);
  103. }
  104. else {
  105. model_min = 0;
  106. model_max = 2;
  107. }
  108. keep_arr = (testobj_t *)GC_MALLOC(sizeof(void *) * KEEP_CNT);
  109. if (NULL == keep_arr) {
  110. fprintf(stderr, "Out of memory!\n");
  111. exit(3);
  112. }
  113. printf("\t\t\tfin. ratio time/s time/fin.\n");
  114. for (model = model_min; model <= model_max; ++model) {
  115. double t = 0.0;
  116. # ifndef NO_CLOCK
  117. CLOCK_TYPE tI, tF;
  118. GET_TIME(tI);
  119. # endif
  120. free_count = 0;
  121. for (i = 0; i < ALLOC_CNT; ++i) {
  122. int k = rand() % KEEP_CNT;
  123. keep_arr[k] = testobj_new(model);
  124. }
  125. GC_gcollect();
  126. # ifndef NO_CLOCK
  127. GET_TIME(tF);
  128. t = MS_TIME_DIFF(tF, tI)*1e-3;
  129. # endif
  130. if (model < 2 && free_count > 0)
  131. printf("%20s: %12.4f %12g %12g\n", model_str[model],
  132. free_count/(double)ALLOC_CNT, t, t/free_count);
  133. else
  134. printf("%20s: %12.4f %12g %12s\n",
  135. model_str[model], 0.0, t, "N/A");
  136. }
  137. return 0;
  138. }