| 1 |
from rpython.memory.gc.hook import GcHooks |
| 2 |
from rpython.memory.gc import incminimark |
| 3 |
from rpython.rlib import rgc |
| 4 |
from rpython.rlib.nonconst import NonConstant |
| 5 |
from rpython.rlib.rarithmetic import r_uint, r_longlong, longlongmax |
| 6 |
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault |
| 7 |
from pypy.interpreter.baseobjspace import W_Root |
| 8 |
from pypy.interpreter.typedef import TypeDef, interp_attrproperty, GetSetProperty |
| 9 |
from pypy.interpreter.executioncontext import AsyncAction |
| 10 |
|
| 11 |
inf = float("inf") |
| 12 |
|
| 13 |
class LowLevelGcHooks(GcHooks): |
| 14 |
""" |
| 15 |
These are the low-level hooks which are called directly from the GC. |
| 16 |
|
| 17 |
They can't do much, because the base class marks the methods as |
| 18 |
@rgc.no_collect. |
| 19 |
|
| 20 |
This is expected to be a singleton, created by space.fromcache, and it is |
| 21 |
integrated with the translation by targetpypystandalone.get_gchooks |
| 22 |
""" |
| 23 |
|
| 24 |
def __init__(self, space): |
| 25 |
self.space = space |
| 26 |
self.w_hooks = space.fromcache(W_AppLevelHooks) |
| 27 |
|
| 28 |
def is_gc_minor_enabled(self): |
| 29 |
return self.w_hooks.gc_minor_enabled |
| 30 |
|
| 31 |
def is_gc_collect_step_enabled(self): |
| 32 |
return self.w_hooks.gc_collect_step_enabled |
| 33 |
|
| 34 |
def is_gc_collect_enabled(self): |
| 35 |
return self.w_hooks.gc_collect_enabled |
| 36 |
|
| 37 |
def on_gc_minor(self, duration, total_memory_used, pinned_objects): |
| 38 |
action = self.w_hooks.gc_minor |
| 39 |
action.count += 1 |
| 40 |
action.duration += duration |
| 41 |
action.duration_min = min(action.duration_min, duration) |
| 42 |
action.duration_max = max(action.duration_max, duration) |
| 43 |
action.total_memory_used = total_memory_used |
| 44 |
action.pinned_objects = pinned_objects |
| 45 |
action.fire() |
| 46 |
|
| 47 |
def on_gc_collect_step(self, duration, oldstate, newstate): |
| 48 |
action = self.w_hooks.gc_collect_step |
| 49 |
action.count += 1 |
| 50 |
action.duration += duration |
| 51 |
action.duration_min = min(action.duration_min, duration) |
| 52 |
action.duration_max = max(action.duration_max, duration) |
| 53 |
action.oldstate = oldstate |
| 54 |
action.newstate = newstate |
| 55 |
action.fire() |
| 56 |
|
| 57 |
def on_gc_collect(self, num_major_collects, |
| 58 |
arenas_count_before, arenas_count_after, |
| 59 |
arenas_bytes, rawmalloc_bytes_before, |
| 60 |
rawmalloc_bytes_after, pinned_objects): |
| 61 |
action = self.w_hooks.gc_collect |
| 62 |
action.count += 1 |
| 63 |
action.num_major_collects = num_major_collects |
| 64 |
action.arenas_count_before = arenas_count_before |
| 65 |
action.arenas_count_after = arenas_count_after |
| 66 |
action.arenas_bytes = arenas_bytes |
| 67 |
action.rawmalloc_bytes_before = rawmalloc_bytes_before |
| 68 |
action.rawmalloc_bytes_after = rawmalloc_bytes_after |
| 69 |
action.pinned_objects = pinned_objects |
| 70 |
action.fire() |
| 71 |
|
| 72 |
|
| 73 |
class W_AppLevelHooks(W_Root): |
| 74 |
|
| 75 |
def __init__(self, space): |
| 76 |
self.space = space |
| 77 |
self.gc_minor_enabled = False |
| 78 |
self.gc_collect_step_enabled = False |
| 79 |
self.gc_collect_enabled = False |
| 80 |
self.gc_minor = GcMinorHookAction(space) |
| 81 |
self.gc_collect_step = GcCollectStepHookAction(space) |
| 82 |
self.gc_collect = GcCollectHookAction(space) |
| 83 |
|
| 84 |
def descr_get_on_gc_minor(self, space): |
| 85 |
return self.gc_minor.w_callable |
| 86 |
|
| 87 |
def descr_set_on_gc_minor(self, space, w_obj): |
| 88 |
self.gc_minor_enabled = not space.is_none(w_obj) |
| 89 |
self.gc_minor.w_callable = w_obj |
| 90 |
self.gc_minor.fix_annotation() |
| 91 |
|
| 92 |
def descr_get_on_gc_collect_step(self, space): |
| 93 |
return self.gc_collect_step.w_callable |
| 94 |
|
| 95 |
def descr_set_on_gc_collect_step(self, space, w_obj): |
| 96 |
self.gc_collect_step_enabled = not space.is_none(w_obj) |
| 97 |
self.gc_collect_step.w_callable = w_obj |
| 98 |
self.gc_collect_step.fix_annotation() |
| 99 |
|
| 100 |
def descr_get_on_gc_collect(self, space): |
| 101 |
return self.gc_collect.w_callable |
| 102 |
|
| 103 |
def descr_set_on_gc_collect(self, space, w_obj): |
| 104 |
self.gc_collect_enabled = not space.is_none(w_obj) |
| 105 |
self.gc_collect.w_callable = w_obj |
| 106 |
self.gc_collect.fix_annotation() |
| 107 |
|
| 108 |
def descr_set(self, space, w_obj): |
| 109 |
w_a = space.getattr(w_obj, space.newtext('on_gc_minor')) |
| 110 |
w_b = space.getattr(w_obj, space.newtext('on_gc_collect_step')) |
| 111 |
w_c = space.getattr(w_obj, space.newtext('on_gc_collect')) |
| 112 |
self.descr_set_on_gc_minor(space, w_a) |
| 113 |
self.descr_set_on_gc_collect_step(space, w_b) |
| 114 |
self.descr_set_on_gc_collect(space, w_c) |
| 115 |
|
| 116 |
def descr_reset(self, space): |
| 117 |
self.descr_set_on_gc_minor(space, space.w_None) |
| 118 |
self.descr_set_on_gc_collect_step(space, space.w_None) |
| 119 |
self.descr_set_on_gc_collect(space, space.w_None) |
| 120 |
|
| 121 |
|
| 122 |
class NoRecursiveAction(AsyncAction): |
| 123 |
depth = 0 |
| 124 |
|
| 125 |
def perform(self, ec, frame): |
| 126 |
if self.depth == 0: |
| 127 |
try: |
| 128 |
self.depth += 1 |
| 129 |
return self._do_perform(ec, frame) |
| 130 |
finally: |
| 131 |
self.depth -= 1 |
| 132 |
|
| 133 |
|
| 134 |
class GcMinorHookAction(NoRecursiveAction): |
| 135 |
total_memory_used = 0 |
| 136 |
pinned_objects = 0 |
| 137 |
|
| 138 |
def __init__(self, space): |
| 139 |
NoRecursiveAction.__init__(self, space) |
| 140 |
self.w_callable = space.w_None |
| 141 |
self.reset() |
| 142 |
|
| 143 |
def reset(self): |
| 144 |
self.count = 0 |
| 145 |
self.duration = 0.0 |
| 146 |
self.duration_min = inf |
| 147 |
self.duration_max = 0.0 |
| 148 |
|
| 149 |
def fix_annotation(self): |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
if NonConstant(False): |
| 154 |
self.count = NonConstant(-42) |
| 155 |
self.duration = NonConstant(-53.2) |
| 156 |
self.duration_min = NonConstant(-53.2) |
| 157 |
self.duration_max = NonConstant(-53.2) |
| 158 |
self.total_memory_used = NonConstant(r_uint(42)) |
| 159 |
self.pinned_objects = NonConstant(-42) |
| 160 |
self.fire() |
| 161 |
|
| 162 |
def _do_perform(self, ec, frame): |
| 163 |
w_stats = W_GcMinorStats( |
| 164 |
self.count, |
| 165 |
self.duration, |
| 166 |
self.duration_min, |
| 167 |
self.duration_max, |
| 168 |
self.total_memory_used, |
| 169 |
self.pinned_objects) |
| 170 |
self.reset() |
| 171 |
self.space.call_function(self.w_callable, w_stats) |
| 172 |
|
| 173 |
|
| 174 |
class GcCollectStepHookAction(NoRecursiveAction): |
| 175 |
oldstate = 0 |
| 176 |
newstate = 0 |
| 177 |
|
| 178 |
def __init__(self, space): |
| 179 |
NoRecursiveAction.__init__(self, space) |
| 180 |
self.w_callable = space.w_None |
| 181 |
self.reset() |
| 182 |
|
| 183 |
def reset(self): |
| 184 |
self.count = 0 |
| 185 |
self.duration = 0.0 |
| 186 |
self.duration_min = inf |
| 187 |
self.duration_max = 0.0 |
| 188 |
|
| 189 |
def fix_annotation(self): |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if NonConstant(False): |
| 194 |
self.count = NonConstant(-42) |
| 195 |
self.duration = NonConstant(-53.2) |
| 196 |
self.duration_min = NonConstant(-53.2) |
| 197 |
self.duration_max = NonConstant(-53.2) |
| 198 |
self.oldstate = NonConstant(-42) |
| 199 |
self.newstate = NonConstant(-42) |
| 200 |
self.fire() |
| 201 |
|
| 202 |
def _do_perform(self, ec, frame): |
| 203 |
w_stats = W_GcCollectStepStats( |
| 204 |
self.count, |
| 205 |
self.duration, |
| 206 |
self.duration_min, |
| 207 |
self.duration_max, |
| 208 |
self.oldstate, |
| 209 |
self.newstate, |
| 210 |
rgc.is_done__states(self.oldstate, self.newstate)) |
| 211 |
self.reset() |
| 212 |
self.space.call_function(self.w_callable, w_stats) |
| 213 |
|
| 214 |
|
| 215 |
class GcCollectHookAction(NoRecursiveAction): |
| 216 |
num_major_collects = 0 |
| 217 |
arenas_count_before = 0 |
| 218 |
arenas_count_after = 0 |
| 219 |
arenas_bytes = 0 |
| 220 |
rawmalloc_bytes_before = 0 |
| 221 |
rawmalloc_bytes_after = 0 |
| 222 |
pinned_objects = 0 |
| 223 |
|
| 224 |
def __init__(self, space): |
| 225 |
NoRecursiveAction.__init__(self, space) |
| 226 |
self.w_callable = space.w_None |
| 227 |
self.reset() |
| 228 |
|
| 229 |
def reset(self): |
| 230 |
self.count = 0 |
| 231 |
|
| 232 |
def fix_annotation(self): |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
if NonConstant(False): |
| 237 |
self.count = NonConstant(-42) |
| 238 |
self.num_major_collects = NonConstant(-42) |
| 239 |
self.arenas_count_before = NonConstant(-42) |
| 240 |
self.arenas_count_after = NonConstant(-42) |
| 241 |
self.arenas_bytes = NonConstant(r_uint(42)) |
| 242 |
self.rawmalloc_bytes_before = NonConstant(r_uint(42)) |
| 243 |
self.rawmalloc_bytes_after = NonConstant(r_uint(42)) |
| 244 |
self.pinned_objects = NonConstant(-42) |
| 245 |
self.fire() |
| 246 |
|
| 247 |
def _do_perform(self, ec, frame): |
| 248 |
w_stats = W_GcCollectStats(self.count, |
| 249 |
self.num_major_collects, |
| 250 |
self.arenas_count_before, |
| 251 |
self.arenas_count_after, |
| 252 |
self.arenas_bytes, |
| 253 |
self.rawmalloc_bytes_before, |
| 254 |
self.rawmalloc_bytes_after, |
| 255 |
self.pinned_objects, |
| 256 |
) |
| 257 |
self.reset() |
| 258 |
self.space.call_function(self.w_callable, w_stats) |
| 259 |
|
| 260 |
|
| 261 |
class W_GcMinorStats(W_Root): |
| 262 |
|
| 263 |
def __init__(self, count, duration, duration_min, duration_max, |
| 264 |
total_memory_used, pinned_objects): |
| 265 |
self.count = count |
| 266 |
self.duration = duration |
| 267 |
self.duration_min = duration_min |
| 268 |
self.duration_max = duration_max |
| 269 |
self.total_memory_used = total_memory_used |
| 270 |
self.pinned_objects = pinned_objects |
| 271 |
|
| 272 |
|
| 273 |
class W_GcCollectStepStats(W_Root): |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
STATE_SCANNING = incminimark.STATE_SCANNING |
| 285 |
STATE_MARKING = incminimark.STATE_MARKING |
| 286 |
STATE_SWEEPING = incminimark.STATE_SWEEPING |
| 287 |
STATE_FINALIZING = incminimark.STATE_FINALIZING |
| 288 |
STATE_USERDEL = incminimark.STATE_FINALIZING + 1 |
| 289 |
GC_STATES = tuple(incminimark.GC_STATES + ['USERDEL']) |
| 290 |
|
| 291 |
def __init__(self, count, duration, duration_min, duration_max, |
| 292 |
oldstate, newstate, major_is_done): |
| 293 |
self.count = count |
| 294 |
self.duration = duration |
| 295 |
self.duration_min = duration_min |
| 296 |
self.duration_max = duration_max |
| 297 |
self.oldstate = oldstate |
| 298 |
self.newstate = newstate |
| 299 |
self.major_is_done = major_is_done |
| 300 |
|
| 301 |
|
| 302 |
class W_GcCollectStats(W_Root): |
| 303 |
def __init__(self, count, num_major_collects, |
| 304 |
arenas_count_before, arenas_count_after, |
| 305 |
arenas_bytes, rawmalloc_bytes_before, |
| 306 |
rawmalloc_bytes_after, pinned_objects): |
| 307 |
self.count = count |
| 308 |
self.num_major_collects = num_major_collects |
| 309 |
self.arenas_count_before = arenas_count_before |
| 310 |
self.arenas_count_after = arenas_count_after |
| 311 |
self.arenas_bytes = arenas_bytes |
| 312 |
self.rawmalloc_bytes_before = rawmalloc_bytes_before |
| 313 |
self.rawmalloc_bytes_after = rawmalloc_bytes_after |
| 314 |
self.pinned_objects = pinned_objects |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
def wrap_many(cls, names): |
| 319 |
d = {} |
| 320 |
for name in names: |
| 321 |
if "duration" in name: |
| 322 |
wrapfn = "newfloat" |
| 323 |
else: |
| 324 |
wrapfn = "newint" |
| 325 |
d[name] = interp_attrproperty(name, cls=cls, wrapfn=wrapfn) |
| 326 |
return d |
| 327 |
|
| 328 |
|
| 329 |
W_AppLevelHooks.typedef = TypeDef( |
| 330 |
"GcHooks", |
| 331 |
on_gc_minor = GetSetProperty( |
| 332 |
W_AppLevelHooks.descr_get_on_gc_minor, |
| 333 |
W_AppLevelHooks.descr_set_on_gc_minor), |
| 334 |
|
| 335 |
on_gc_collect_step = GetSetProperty( |
| 336 |
W_AppLevelHooks.descr_get_on_gc_collect_step, |
| 337 |
W_AppLevelHooks.descr_set_on_gc_collect_step), |
| 338 |
|
| 339 |
on_gc_collect = GetSetProperty( |
| 340 |
W_AppLevelHooks.descr_get_on_gc_collect, |
| 341 |
W_AppLevelHooks.descr_set_on_gc_collect), |
| 342 |
|
| 343 |
set = interp2app(W_AppLevelHooks.descr_set), |
| 344 |
reset = interp2app(W_AppLevelHooks.descr_reset), |
| 345 |
) |
| 346 |
|
| 347 |
W_GcMinorStats.typedef = TypeDef( |
| 348 |
"GcMinorStats", |
| 349 |
**wrap_many(W_GcMinorStats, ( |
| 350 |
"count", |
| 351 |
"duration", |
| 352 |
"duration_min", |
| 353 |
"duration_max", |
| 354 |
"total_memory_used", |
| 355 |
"pinned_objects")) |
| 356 |
) |
| 357 |
|
| 358 |
W_GcCollectStepStats.typedef = TypeDef( |
| 359 |
"GcCollectStepStats", |
| 360 |
STATE_SCANNING = W_GcCollectStepStats.STATE_SCANNING, |
| 361 |
STATE_MARKING = W_GcCollectStepStats.STATE_MARKING, |
| 362 |
STATE_SWEEPING = W_GcCollectStepStats.STATE_SWEEPING, |
| 363 |
STATE_FINALIZING = W_GcCollectStepStats.STATE_FINALIZING, |
| 364 |
STATE_USERDEL = W_GcCollectStepStats.STATE_USERDEL, |
| 365 |
GC_STATES = tuple(W_GcCollectStepStats.GC_STATES), |
| 366 |
major_is_done = interp_attrproperty( |
| 367 |
"major_is_done", |
| 368 |
cls=W_GcCollectStepStats, |
| 369 |
wrapfn="newbool"), |
| 370 |
**wrap_many(W_GcCollectStepStats, ( |
| 371 |
"count", |
| 372 |
"duration", |
| 373 |
"duration_min", |
| 374 |
"duration_max", |
| 375 |
"oldstate", |
| 376 |
"newstate")) |
| 377 |
) |
| 378 |
|
| 379 |
W_GcCollectStats.typedef = TypeDef( |
| 380 |
"GcCollectStats", |
| 381 |
**wrap_many(W_GcCollectStats, ( |
| 382 |
"count", |
| 383 |
"num_major_collects", |
| 384 |
"arenas_count_before", |
| 385 |
"arenas_count_after", |
| 386 |
"arenas_bytes", |
| 387 |
"rawmalloc_bytes_before", |
| 388 |
"rawmalloc_bytes_after", |
| 389 |
"pinned_objects", |
| 390 |
)) |
| 391 |
) |