| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
| 60 |
kLongLivedTreeDepth = 16 |
| 61 |
kArraySize = 500000 |
| 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 |
|
| 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 |
|
| 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)) |