Debian Codegraph @

rpython/jit/metainterp/test/test_counter.py

raw symbols SCIP
1 from rpython.jit.metainterp.counter import JitCounter
2
3
4 def test_get_index():
5 jc = JitCounter(size=128) # 7 bits
6 for i in range(10):
7 hash = 400000001 * i
8 index = jc._get_index(hash)
9 assert index == (hash >> (32 - 7))
10
11 def test_get_subhash():
12 assert JitCounter._get_subhash(0x518ebd) == 0x8ebd
13
14 def test_fetch_next_hash():
15 jc = JitCounter(size=2048)
16 # check the distribution of "fetch_next_hash() & ~7".
17 blocks = [[jc.fetch_next_hash() & ~7 for i in range(65536)]
18 for j in range(2)]
19 for block in blocks:
20 assert 0 <= jc._get_index(block[0]) < 2048
21 assert 0 <= jc._get_index(block[-1]) < 2048
22 assert 0 <= jc._get_index(block[2531]) < 2048
23 assert 0 <= jc._get_index(block[45981]) < 2048
24 # should be correctly distributed: ideally 2047 or 2048 different
25 # values
26 assert len(set([jc._get_index(x) for x in block])) >= 2040
27 # check that the subkeys are distinct for same-block entries
28 subkeys = {}
29 for block in blocks:
30 for x in block:
31 idx = jc._get_index(x)
32 subkeys.setdefault(idx, []).append(jc._get_subhash(x))
33 collisions = 0
34 for idx, sks in subkeys.items():
35 collisions += len(sks) - len(set(sks))
36 assert collisions < 5
37
38 def index2hash(jc, index, subhash=0):
39 assert 0 <= subhash < 65536
40 return (index << jc.shift) | subhash
41
42 def test_tick():
43 jc = JitCounter()
44 jc._tick_slowpath = "not callable in this test!"
45 incr = jc.compute_threshold(4)
46 for i in range(5):
47 r = jc.tick(index2hash(jc, 104), incr)
48 assert r is (i == 3)
49 for i in range(5):
50 r = jc.tick(index2hash(jc, 108), incr)
51 s = jc.tick(index2hash(jc, 109), incr)
52 assert r is (i == 3)
53 assert s is (i == 3)
54 jc.reset(index2hash(jc, 108))
55 for i in range(5):
56 r = jc.tick(index2hash(jc, 108), incr)
57 assert r is (i == 3)
58
59 def test_collisions():
60 jc = JitCounter(size=4) # 2 bits
61 incr = jc.compute_threshold(4)
62 for i in range(5):
63 for sk in range(100, 105):
64 r = jc.tick(index2hash(jc, 3, subhash=sk), incr)
65 assert r is (i == 3)
66
67 jc = JitCounter()
68 incr = jc.compute_threshold(4)
69 misses = 0
70 for i in range(5):
71 for sk in range(100, 106):
72 r = jc.tick(index2hash(jc, 3, subhash=sk), incr)
73 if r:
74 assert i == 3
75 elif i == 3:
76 misses += 1
77 assert misses < 5
78
79
80 def test_install_new_chain():
81 class Dead:
82 next = None
83 def should_remove_jitcell(self):
84 return True
85 class Alive:
86 next = None
87 def should_remove_jitcell(self):
88 return False
89 #
90 jc = JitCounter()
91 assert jc.lookup_chain(104) is None
92 d1 = Dead()
93 jc.install_new_cell(104, d1)
94 assert jc.lookup_chain(104) is d1
95 d2 = Dead()
96 jc.install_new_cell(104, d2)
97 assert jc.lookup_chain(104) is d2
98 assert d2.next is None
99 #
100 d3 = Alive()
101 jc.install_new_cell(104, d3)
102 assert jc.lookup_chain(104) is d3
103 assert d3.next is None
104 d4 = Alive()
105 jc.install_new_cell(104, d4)
106 assert jc.lookup_chain(104) is d3
107 assert d3.next is d4
108 assert d4.next is None
109
110
111 def test_change_current_fraction():
112 jc = JitCounter()
113 incr = jc.compute_threshold(8)
114 # change_current_fraction() with a fresh new hash
115 jc.change_current_fraction(index2hash(jc, 104), 0.95)
116 r = jc.tick(index2hash(jc, 104), incr)
117 assert r is True
118 # change_current_fraction() with an already-existing hash
119 r = jc.tick(index2hash(jc, 104), incr)
120 assert r is False
121 jc.change_current_fraction(index2hash(jc, 104), 0.95)
122 r = jc.tick(index2hash(jc, 104), incr)
123 assert r is True
124 # change_current_fraction() with a smaller incr
125 incr = jc.compute_threshold(32)
126 jc.change_current_fraction(index2hash(jc, 104), 0.95)
127 r = jc.tick(index2hash(jc, 104), incr)
128 assert r is False
129 r = jc.tick(index2hash(jc, 104), incr)
130 assert r is True