File: hook.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (391 lines) | stat: -rw-r--r-- 13,813 bytes parent folder | download | duplicates (3)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from rpython.memory.gc.hook import GcHooks
from rpython.memory.gc import incminimark
from rpython.rlib import rgc
from rpython.rlib.nonconst import NonConstant
from rpython.rlib.rarithmetic import r_uint, r_longlong, longlongmax
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.typedef import TypeDef, interp_attrproperty, GetSetProperty
from pypy.interpreter.executioncontext import AsyncAction

inf = float("inf")

class LowLevelGcHooks(GcHooks):
    """
    These are the low-level hooks which are called directly from the GC.

    They can't do much, because the base class marks the methods as
    @rgc.no_collect.

    This is expected to be a singleton, created by space.fromcache, and it is
    integrated with the translation by targetpypystandalone.get_gchooks
    """

    def __init__(self, space):
        self.space = space
        self.w_hooks = space.fromcache(W_AppLevelHooks)

    def is_gc_minor_enabled(self):
        return self.w_hooks.gc_minor_enabled

    def is_gc_collect_step_enabled(self):
        return self.w_hooks.gc_collect_step_enabled

    def is_gc_collect_enabled(self):
        return self.w_hooks.gc_collect_enabled

    def on_gc_minor(self, duration, total_memory_used, pinned_objects):
        action = self.w_hooks.gc_minor
        action.count += 1
        action.duration += duration
        action.duration_min = min(action.duration_min, duration)
        action.duration_max = max(action.duration_max, duration)
        action.total_memory_used = total_memory_used
        action.pinned_objects = pinned_objects
        action.fire()

    def on_gc_collect_step(self, duration, oldstate, newstate):
        action = self.w_hooks.gc_collect_step
        action.count += 1
        action.duration += duration
        action.duration_min = min(action.duration_min, duration)
        action.duration_max = max(action.duration_max, duration)
        action.oldstate = oldstate
        action.newstate = newstate
        action.fire()

    def on_gc_collect(self, num_major_collects,
                      arenas_count_before, arenas_count_after,
                      arenas_bytes, rawmalloc_bytes_before,
                      rawmalloc_bytes_after, pinned_objects):
        action = self.w_hooks.gc_collect
        action.count += 1
        action.num_major_collects = num_major_collects
        action.arenas_count_before = arenas_count_before
        action.arenas_count_after = arenas_count_after
        action.arenas_bytes = arenas_bytes
        action.rawmalloc_bytes_before = rawmalloc_bytes_before
        action.rawmalloc_bytes_after = rawmalloc_bytes_after
        action.pinned_objects = pinned_objects
        action.fire()


class W_AppLevelHooks(W_Root):

    def __init__(self, space):
        self.space = space
        self.gc_minor_enabled = False
        self.gc_collect_step_enabled = False
        self.gc_collect_enabled = False
        self.gc_minor = GcMinorHookAction(space)
        self.gc_collect_step = GcCollectStepHookAction(space)
        self.gc_collect = GcCollectHookAction(space)

    def descr_get_on_gc_minor(self, space):
        return self.gc_minor.w_callable

    def descr_set_on_gc_minor(self, space, w_obj):
        self.gc_minor_enabled = not space.is_none(w_obj)
        self.gc_minor.w_callable = w_obj
        self.gc_minor.fix_annotation()

    def descr_get_on_gc_collect_step(self, space):
        return self.gc_collect_step.w_callable

    def descr_set_on_gc_collect_step(self, space, w_obj):
        self.gc_collect_step_enabled = not space.is_none(w_obj)
        self.gc_collect_step.w_callable = w_obj
        self.gc_collect_step.fix_annotation()

    def descr_get_on_gc_collect(self, space):
        return self.gc_collect.w_callable

    def descr_set_on_gc_collect(self, space, w_obj):
        self.gc_collect_enabled = not space.is_none(w_obj)
        self.gc_collect.w_callable = w_obj
        self.gc_collect.fix_annotation()

    def descr_set(self, space, w_obj):
        w_a = space.getattr(w_obj, space.newtext('on_gc_minor'))
        w_b = space.getattr(w_obj, space.newtext('on_gc_collect_step'))
        w_c = space.getattr(w_obj, space.newtext('on_gc_collect'))
        self.descr_set_on_gc_minor(space, w_a)
        self.descr_set_on_gc_collect_step(space, w_b)
        self.descr_set_on_gc_collect(space, w_c)

    def descr_reset(self, space):
        self.descr_set_on_gc_minor(space, space.w_None)
        self.descr_set_on_gc_collect_step(space, space.w_None)
        self.descr_set_on_gc_collect(space, space.w_None)


class NoRecursiveAction(AsyncAction):
    depth = 0

    def perform(self, ec, frame):
        if self.depth == 0:
            try:
                self.depth += 1
                return self._do_perform(ec, frame)
            finally:
                self.depth -= 1


class GcMinorHookAction(NoRecursiveAction):
    total_memory_used = 0
    pinned_objects = 0

    def __init__(self, space):
        NoRecursiveAction.__init__(self, space)
        self.w_callable = space.w_None
        self.reset()

    def reset(self):
        self.count = 0
        self.duration = 0.0
        self.duration_min = inf
        self.duration_max = 0.0

    def fix_annotation(self):
        # the annotation of the class and its attributes must be completed
        # BEFORE we do the gc transform; this makes sure that everything is
        # annotated with the correct types
        if NonConstant(False):
            self.count = NonConstant(-42)
            self.duration = NonConstant(-53.2)
            self.duration_min = NonConstant(-53.2)
            self.duration_max = NonConstant(-53.2)
            self.total_memory_used = NonConstant(r_uint(42))
            self.pinned_objects = NonConstant(-42)
            self.fire()

    def _do_perform(self, ec, frame):
        w_stats = W_GcMinorStats(
            self.count,
            self.duration,
            self.duration_min,
            self.duration_max,
            self.total_memory_used,
            self.pinned_objects)
        self.reset()
        self.space.call_function(self.w_callable, w_stats)


class GcCollectStepHookAction(NoRecursiveAction):
    oldstate = 0
    newstate = 0

    def __init__(self, space):
        NoRecursiveAction.__init__(self, space)
        self.w_callable = space.w_None
        self.reset()

    def reset(self):
        self.count = 0
        self.duration = 0.0
        self.duration_min = inf
        self.duration_max = 0.0

    def fix_annotation(self):
        # the annotation of the class and its attributes must be completed
        # BEFORE we do the gc transform; this makes sure that everything is
        # annotated with the correct types
        if NonConstant(False):
            self.count = NonConstant(-42)
            self.duration = NonConstant(-53.2)
            self.duration_min = NonConstant(-53.2)
            self.duration_max = NonConstant(-53.2)
            self.oldstate = NonConstant(-42)
            self.newstate = NonConstant(-42)
            self.fire()

    def _do_perform(self, ec, frame):
        w_stats = W_GcCollectStepStats(
            self.count,
            self.duration,
            self.duration_min,
            self.duration_max,
            self.oldstate,
            self.newstate,
            rgc.is_done__states(self.oldstate, self.newstate))
        self.reset()
        self.space.call_function(self.w_callable, w_stats)


class GcCollectHookAction(NoRecursiveAction):
    num_major_collects = 0
    arenas_count_before = 0
    arenas_count_after = 0
    arenas_bytes = 0
    rawmalloc_bytes_before = 0
    rawmalloc_bytes_after = 0
    pinned_objects = 0

    def __init__(self, space):
        NoRecursiveAction.__init__(self, space)
        self.w_callable = space.w_None
        self.reset()

    def reset(self):
        self.count = 0

    def fix_annotation(self):
        # the annotation of the class and its attributes must be completed
        # BEFORE we do the gc transform; this makes sure that everything is
        # annotated with the correct types
        if NonConstant(False):
            self.count = NonConstant(-42)
            self.num_major_collects = NonConstant(-42)
            self.arenas_count_before = NonConstant(-42)
            self.arenas_count_after = NonConstant(-42)
            self.arenas_bytes = NonConstant(r_uint(42))
            self.rawmalloc_bytes_before = NonConstant(r_uint(42))
            self.rawmalloc_bytes_after = NonConstant(r_uint(42))
            self.pinned_objects = NonConstant(-42)
            self.fire()

    def _do_perform(self, ec, frame):
        w_stats = W_GcCollectStats(self.count,
                                   self.num_major_collects,
                                   self.arenas_count_before,
                                   self.arenas_count_after,
                                   self.arenas_bytes,
                                   self.rawmalloc_bytes_before,
                                   self.rawmalloc_bytes_after,
                                   self.pinned_objects,
                                  )
        self.reset()
        self.space.call_function(self.w_callable, w_stats)


class W_GcMinorStats(W_Root):

    def __init__(self, count, duration, duration_min, duration_max,
                 total_memory_used, pinned_objects):
        self.count = count
        self.duration = duration
        self.duration_min = duration_min
        self.duration_max = duration_max
        self.total_memory_used = total_memory_used
        self.pinned_objects = pinned_objects


class W_GcCollectStepStats(W_Root):
    # NOTE: this is specific to incminimark: if we want to integrate the
    # applevel gc module with another gc, we probably need a more general
    # approach to this.
    #
    # incminimark has 4 GC states: scanning, marking, sweeping and
    # finalizing. However, from the user point of view, we have an additional
    # "virtual" state: USERDEL, which represent when we run applevel
    # finalizers after having completed a GC major collection. This state is
    # never explicitly visible when using hooks, but it is used for the return
    # value of gc.collect_step (see interp_gc.py)
    STATE_SCANNING = incminimark.STATE_SCANNING
    STATE_MARKING = incminimark.STATE_MARKING
    STATE_SWEEPING = incminimark.STATE_SWEEPING
    STATE_FINALIZING = incminimark.STATE_FINALIZING
    STATE_USERDEL = incminimark.STATE_FINALIZING + 1 # used by StepCollector
    GC_STATES = tuple(incminimark.GC_STATES + ['USERDEL'])

    def __init__(self, count, duration, duration_min, duration_max,
                 oldstate, newstate, major_is_done):
        self.count = count
        self.duration = duration
        self.duration_min = duration_min
        self.duration_max = duration_max
        self.oldstate = oldstate
        self.newstate = newstate
        self.major_is_done = major_is_done


class W_GcCollectStats(W_Root):
    def __init__(self, count, num_major_collects,
                 arenas_count_before, arenas_count_after,
                 arenas_bytes, rawmalloc_bytes_before,
                 rawmalloc_bytes_after, pinned_objects):
        self.count = count
        self.num_major_collects = num_major_collects
        self.arenas_count_before = arenas_count_before
        self.arenas_count_after = arenas_count_after
        self.arenas_bytes = arenas_bytes
        self.rawmalloc_bytes_before = rawmalloc_bytes_before
        self.rawmalloc_bytes_after = rawmalloc_bytes_after
        self.pinned_objects = pinned_objects


# just a shortcut to make the typedefs shorter
def wrap_many(cls, names):
    d = {}
    for name in names:
        if "duration" in name:
            wrapfn = "newfloat"
        else:
            wrapfn = "newint"
        d[name] = interp_attrproperty(name, cls=cls, wrapfn=wrapfn)
    return d


W_AppLevelHooks.typedef = TypeDef(
    "GcHooks",
    on_gc_minor = GetSetProperty(
        W_AppLevelHooks.descr_get_on_gc_minor,
        W_AppLevelHooks.descr_set_on_gc_minor),

    on_gc_collect_step = GetSetProperty(
        W_AppLevelHooks.descr_get_on_gc_collect_step,
        W_AppLevelHooks.descr_set_on_gc_collect_step),

    on_gc_collect = GetSetProperty(
        W_AppLevelHooks.descr_get_on_gc_collect,
        W_AppLevelHooks.descr_set_on_gc_collect),

    set = interp2app(W_AppLevelHooks.descr_set),
    reset = interp2app(W_AppLevelHooks.descr_reset),
    )

W_GcMinorStats.typedef = TypeDef(
    "GcMinorStats",
    **wrap_many(W_GcMinorStats, (
        "count",
        "duration",
        "duration_min",
        "duration_max",
        "total_memory_used",
        "pinned_objects"))
    )

W_GcCollectStepStats.typedef = TypeDef(
    "GcCollectStepStats",
    STATE_SCANNING = W_GcCollectStepStats.STATE_SCANNING,
    STATE_MARKING = W_GcCollectStepStats.STATE_MARKING,
    STATE_SWEEPING = W_GcCollectStepStats.STATE_SWEEPING,
    STATE_FINALIZING = W_GcCollectStepStats.STATE_FINALIZING,
    STATE_USERDEL = W_GcCollectStepStats.STATE_USERDEL,
    GC_STATES = tuple(W_GcCollectStepStats.GC_STATES),
    major_is_done = interp_attrproperty(
        "major_is_done",
        cls=W_GcCollectStepStats,
        wrapfn="newbool"),
    **wrap_many(W_GcCollectStepStats, (
        "count",
        "duration",
        "duration_min",
        "duration_max",
        "oldstate",
        "newstate"))
    )

W_GcCollectStats.typedef = TypeDef(
    "GcCollectStats",
    **wrap_many(W_GcCollectStats, (
        "count",
        "num_major_collects",
        "arenas_count_before",
        "arenas_count_after",
        "arenas_bytes",
        "rawmalloc_bytes_before",
        "rawmalloc_bytes_after",
        "pinned_objects",
     ))
    )