Debian Codegraph @

rpython/translator/goal/gcbench.py

raw symbols SCIP
1 # Ported from a Java benchmark whose history is :
2 # This is adapted from a benchmark written by John Ellis and Pete Kovac
3 # of Post Communications.
4 # It was modified by Hans Boehm of Silicon Graphics.
5 #
6 # This is no substitute for real applications. No actual application
7 # is likely to behave in exactly this way. However, this benchmark was
8 # designed to be more representative of real applications than other
9 # Java GC benchmarks of which we are aware.
10 # It attempts to model those properties of allocation requests that
11 # are important to current GC techniques.
12 # It is designed to be used either to obtain a single overall performance
13 # number, or to give a more detailed estimate of how collector
14 # performance varies with object lifetimes. It prints the time
15 # required to allocate and collect balanced binary trees of various
16 # sizes. Smaller trees result in shorter object lifetimes. Each cycle
17 # allocates roughly the same amount of memory.
18 # Two data structures are kept around during the entire process, so
19 # that the measured performance is representative of applications
20 # that maintain some live in-memory data. One of these is a tree
21 # containing many pointers. The other is a large array containing
22 # double precision floating point numbers. Both should be of comparable
23 # size.
24 #
25 # The results are only really meaningful together with a specification
26 # of how much memory was used. It is possible to trade memory for
27 # better time performance. This benchmark should be run in a 32 MB
28 # heap, though we don't currently know how to enforce that uniformly.
29 #
30 # Unlike the original Ellis and Kovac benchmark, we do not attempt
31 # measure pause times. This facility should eventually be added back
32 # in. There are several reasons for omitting it for now. The original
33 # implementation depended on assumptions about the thread scheduler
34 # that don't hold uniformly. The results really measure both the
35 # scheduler and GC. Pause time measurements tend to not fit well with
36 # current benchmark suites. As far as we know, none of the current
37 # commercial Java implementations seriously attempt to minimize GC pause
38 # times.
39 #
40 # Known deficiencies:
41 # - No way to check on memory use
42 # - No cyclic data structures
43 # - No attempt to measure variation with object size
44 # - Results are sensitive to locking cost, but we dont
45 # check for proper locking
46 import time
47 import gc
48
49 USAGE = """gcbench [num_repetitions] [--depths=N,N,N..] [--threads=N] [--gc=off|--gc=manual]"""
50 ENABLE_THREADS = True
51
52
53 class Node(object):
54
55 def __init__(self, l=None, r=None):
56 self.left = l
57 self.right = r
58
59 kStretchTreeDepth = 18 # about 16Mb (for Java)
60 kLongLivedTreeDepth = 16 # about 4Mb (for Java)
61 kArraySize = 500000 # about 4Mb
62 kMinTreeDepth = 4
63 kMaxTreeDepth = 16
64
65 def tree_size(i):
66 "Nodes used by a tree of a given size"
67 return (1 << (i + 1)) - 1
68
69 def num_iters(i):
70 "Number of iterations to use for a given tree depth"
71 return 2 * tree_size(kStretchTreeDepth) / tree_size(i);
72
73 def populate(depth, node):
74 "Build tree top down, assigning to older objects."
75 if depth <= 0:
76 return
77 else:
78 depth -= 1
79 node.left = Node()
80 node.right = Node()
81 populate(depth, node.left)
82 populate(depth, node.right)
83
84 def make_tree(depth):
85 "Build tree bottom-up"
86 if depth <= 0:
87 return Node()
88 else:
89 return Node(make_tree(depth-1), make_tree(depth-1))
90
91 def print_diagnostics():
92 "ought to print free/total memory"
93 pass
94
95 def time_construction(depth):
96 niters = num_iters(depth)
97 print "Creating %d trees of depth %d" % (niters, depth)
98 t_start = time.time()
99 for i in range(niters):
100 temp_tree = Node()
101 populate(depth, temp_tree)
102 temp_tree = None
103 t_finish = time.time()
104 print "\tTop down constrution took %f ms" % ((t_finish-t_start)*1000.)
105 t_start = time.time()
106 for i in range(niters):
107 temp_tree = make_tree(depth)
108 temp_tree = None
109 t_finish = time.time()
110 print "\tBottom up constrution took %f ms" % ((t_finish-t_start)*1000.)
111
112 DEFAULT_DEPTHS = range(kMinTreeDepth, kMaxTreeDepth+1, 2)
113
114 def time_constructions(depths):
115 for d in depths:
116 time_construction(d)
117
118 def time_parallel_constructions(depths, nthreads):
119 import threading
120 threadlist = []
121 print "Starting %d parallel threads..." % (nthreads,)
122 for n in range(nthreads):
123 t = threading.Thread(target=time_constructions, args=(depths,))
124 t.start()
125 threadlist.append(t)
126 for t in threadlist:
127 t.join()
128 print "All %d threads finished" % (nthreads,)
129
130 def main(depths=DEFAULT_DEPTHS, threads=0):
131 print "Garbage Collector Test"
132 print " Stretching memory with a binary tree of depth %d" % kStretchTreeDepth
133 print_diagnostics()
134 t_start = time.time()
135 temp_tree = make_tree(kStretchTreeDepth)
136 temp_tree = None
137
138 # Create a long lived object
139 print " Creating a long-lived binary tree of depth %d" % kLongLivedTreeDepth
140 long_lived_tree = Node()
141 populate(kLongLivedTreeDepth, long_lived_tree)
142
143 # Create long-lived array, filling half of it
144 print " Creating a long-lived array of %d doubles" % kArraySize
145 array = [0.0] * kArraySize
146 i = 1
147 while i < kArraySize/2:
148 array[i] = 1.0/i
149 i += 1
150 print_diagnostics()
151
152 if threads:
153 time_parallel_constructions(depths, threads)
154 else:
155 time_constructions(depths)
156
157 if long_lived_tree is None or array[1024] != 1.0/1024:
158 raise Failed
159
160 t_finish = time.time()
161 print_diagnostics()
162 print "Completed in %f ms." % ((t_finish-t_start)*1000.)
163
164 class Failed(Exception):
165 pass
166
167
168 def argerror():
169 print "Usage:"
170 print " ", USAGE
171 return 2
172
173 def entry_point(argv):
174 depths = DEFAULT_DEPTHS
175 threads = 0
176 repeatcount = 1
177 gc_policy = 'on'
178 for arg in argv[1:]:
179 if arg.startswith('--threads='):
180 arg = arg[len('--threads='):]
181 if not ENABLE_THREADS:
182 print "threads disabled (they cannot be translated)"
183 return 1
184 try:
185 threads = int(arg)
186 except ValueError:
187 return argerror()
188 elif arg.startswith('--depths='):
189 arg = arg[len('--depths='):].split(',')
190 try:
191 depths = [int(s) for s in arg]
192 except ValueError:
193 return argerror()
194 elif arg.startswith('--gc=off'):
195 gc_policy = 'off'
196 elif arg.startswith('--gc=manual'):
197 gc_policy = 'manual'
198 else:
199 try:
200 repeatcount = int(arg)
201 except ValueError:
202 return argerror()
203 #
204 if gc_policy == 'off' or gc_policy == 'manual':
205 gc.disable()
206 for i in range(repeatcount):
207 main(depths, threads)
208 if gc_policy == 'manual':
209 gc.collect(1)
210 return 0
211
212
213 if __name__ == '__main__':
214 import sys
215 sys.exit(entry_point(sys.argv))