Debian Codegraph @

external/bdwgc/tests/disclaim_bench.c

raw
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
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "gc_disclaim.h"
24
25 /* Include gc_priv.h is done after including GC public headers, so */
26 /* that GC_BUILD has no effect on the public prototypes. */
27 #include "private/gc_priv.h" /* for CLOCK_TYPE, COVERT_DATAFLOW, GC_random */
28
29 #ifdef LINT2
30 # undef rand
31 # define rand() (int)GC_random()
32 #endif
33
34 #define my_assert(e) \
35 if (!(e)) { \
36 fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \
37 exit(-1); \
38 }
39
40 static int free_count = 0;
41
42 struct testobj_s {
43 struct testobj_s *keep_link;
44 int i;
45 };
46
47 typedef struct testobj_s *testobj_t;
48
49 void GC_CALLBACK testobj_finalize(void *obj, void *carg)
50 {
51 ++*(int *)carg;
52 my_assert(((testobj_t)obj)->i == 109);
53 ((testobj_t)obj)->i = 110;
54 }
55
56 static const struct GC_finalizer_closure fclos = {
57 testobj_finalize,
58 &free_count
59 };
60
61 testobj_t testobj_new(int model)
62 {
63 testobj_t obj;
64 switch (model) {
65 case 0:
66 obj = GC_NEW(struct testobj_s);
67 if (obj != NULL)
68 GC_REGISTER_FINALIZER_NO_ORDER(obj, testobj_finalize,
69 &free_count, NULL, NULL);
70 break;
71 case 1:
72 obj = (testobj_t)GC_finalized_malloc(sizeof(struct testobj_s),
73 &fclos);
74 break;
75 case 2:
76 obj = GC_NEW(struct testobj_s);
77 break;
78 default:
79 exit(-1);
80 }
81 if (obj == NULL) {
82 fprintf(stderr, "Out of memory!\n");
83 exit(3);
84 }
85 my_assert(obj->i == 0 && obj->keep_link == NULL);
86 obj->i = 109;
87 return obj;
88 }
89
90 #define ALLOC_CNT (4*1024*1024)
91 #define KEEP_CNT (32*1024)
92
93 static char const *model_str[3] = {
94 "regular finalization",
95 "finalize on reclaim",
96 "no finalization"
97 };
98
99 int main(int argc, char **argv)
100 {
101 int i;
102 int model, model_min, model_max;
103 testobj_t *keep_arr;
104
105 GC_INIT();
106 GC_init_finalized_malloc();
107 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
108 fprintf(stderr,
109 "Usage: %s [FINALIZATION_MODEL]\n"
110 "\t0 -- original finalization\n"
111 "\t1 -- finalization on reclaim\n"
112 "\t2 -- no finalization\n", argv[0]);
113 return 1;
114 }
115 if (argc == 2) {
116 model_min = model_max = (int)COVERT_DATAFLOW(atoi(argv[1]));
117 if (model_min < 0 || model_max > 2)
118 exit(2);
119 }
120 else {
121 model_min = 0;
122 model_max = 2;
123 }
124
125 keep_arr = (testobj_t *)GC_MALLOC(sizeof(void *) * KEEP_CNT);
126 if (NULL == keep_arr) {
127 fprintf(stderr, "Out of memory!\n");
128 exit(3);
129 }
130
131 printf("\t\t\tfin. ratio time/s time/fin.\n");
132 for (model = model_min; model <= model_max; ++model) {
133 double t = 0.0;
134 # ifndef NO_CLOCK
135 CLOCK_TYPE tI, tF;
136
137 GET_TIME(tI);
138 # endif
139 free_count = 0;
140 for (i = 0; i < ALLOC_CNT; ++i) {
141 int k = rand() % KEEP_CNT;
142 keep_arr[k] = testobj_new(model);
143 }
144 GC_gcollect();
145 # ifndef NO_CLOCK
146 GET_TIME(tF);
147 t = MS_TIME_DIFF(tF, tI)*1e-3;
148 # endif
149
150 if (model < 2 && free_count > 0)
151 printf("%20s: %12.4f %12g %12g\n", model_str[model],
152 free_count/(double)ALLOC_CNT, t, t/free_count);
153 else
154 printf("%20s: %12.4f %12g %12s\n",
155 model_str[model], 0.0, t, "N/A");
156 }
157 return 0;
158 }