Debian Codegraph @

rpython/jit/metainterp/memmgr.py

raw symbols SCIP
1 import math
2 from rpython.rlib.rarithmetic import r_int64
3 from rpython.rlib.debug import debug_start, debug_print, debug_stop
4 from rpython.rlib.objectmodel import we_are_translated
5
6 #
7 # Logic to decide which loops are old and not used any more.
8 #
9 # All the long-lived references to LoopToken are weakrefs (see JitCell
10 # in warmstate.py), apart from the 'alive_loops' set in MemoryManager,
11 # which is the only (long-living) place that keeps them alive. If a
12 # loop was not called for long enough, then it is removed from
13 # 'alive_loops'. It will soon be freed by the GC. LoopToken.__del__
14 # calls the method cpu.free_loop_and_bridges().
15 #
16 # The alive_loops set is maintained using the notion of a global
17 # 'current generation' which is, in practice, the total number of loops
18 # and bridges produced so far. A LoopToken is declared "old" if its
19 # 'generation' field is much smaller than the current generation, and
20 # removed from the set.
21 #
22
23 class MemoryManager(object):
24
25 def __init__(self):
26 self.check_frequency = -1
27 # NB. use of r_int64 to be extremely far on the safe side:
28 # this is increasing by one after each loop or bridge is
29 # compiled, and it must not overflow. If the backend implements
30 # complete freeing in cpu.free_loop_and_bridges(), then it may
31 # be possible to get arbitrary many of them just by waiting long
32 # enough. But in this day and age, you'd still never have the
33 # patience of waiting for a slowly-increasing 64-bit number to
34 # overflow :-)
35
36 # According to my estimates it's about 5e9 years given 1000 loops
37 # per second
38 self.current_generation = r_int64(1)
39 self.next_check = r_int64(-1)
40 self.alive_loops = {}
41
42 def set_max_age(self, max_age, check_frequency=0):
43 if max_age <= 0:
44 self.next_check = r_int64(-1)
45 else:
46 self.max_age = max_age
47 if check_frequency <= 0:
48 check_frequency = int(math.sqrt(max_age))
49 self.check_frequency = check_frequency
50 self.next_check = self.current_generation + 1
51
52 def next_generation(self):
53 self.current_generation += 1
54 if self.current_generation == self.next_check:
55 self._kill_old_loops_now()
56 self.next_check = self.current_generation + self.check_frequency
57
58 def keep_loop_alive(self, looptoken):
59 if looptoken.generation != self.current_generation:
60 looptoken.generation = self.current_generation
61 self.alive_loops[looptoken] = None
62
63 def _kill_old_loops_now(self):
64 debug_start("jit-mem-collect")
65 oldtotal = len(self.alive_loops)
66 #print self.alive_loops.keys()
67 debug_print("Current generation:", self.current_generation)
68 debug_print("Loop tokens before:", oldtotal)
69 max_generation = self.current_generation - (self.max_age-1)
70 for looptoken in self.alive_loops.keys():
71 if (0 <= looptoken.generation < max_generation or
72 looptoken.invalidated):
73 del self.alive_loops[looptoken]
74 newtotal = len(self.alive_loops)
75 debug_print("Loop tokens freed: ", oldtotal - newtotal)
76 debug_print("Loop tokens left: ", newtotal)
77 #print self.alive_loops.keys()
78 if not we_are_translated() and oldtotal != newtotal:
79 looptoken = None
80 from rpython.rlib import rgc
81 # a single one is not enough for all tests :-(
82 rgc.collect(); rgc.collect(); rgc.collect()
83 debug_stop("jit-mem-collect")
84
85 def release_all_loops(self):
86 debug_start("jit-mem-releaseall")
87 debug_print("Loop tokens cleared:", len(self.alive_loops))
88 self.alive_loops.clear()
89 debug_stop("jit-mem-releaseall")