Debian Codegraph @

Include/internal/pycore_gc.h

raw ◀ ▶
1 Debian patch touch this file
1 #ifndef Py_INTERNAL_GC_H
2 #define Py_INTERNAL_GC_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6
7 #ifndef Py_BUILD_CORE
8 # error "this header requires Py_BUILD_CORE define"
9 #endif
10
11 #include "pycore_freelist.h" // _PyFreeListState
12
13 /* GC information is stored BEFORE the object structure. */
14 typedef struct {
15 // Pointer to next object in the list.
16 // 0 means the object is not tracked
17 uintptr_t _gc_next;
18
19 // Pointer to previous object in the list.
20 // Lowest two bits are used for flags documented later.
•21 // Those bits are made available by the struct's minimum alignment.
22 uintptr_t _gc_prev;
•23 } PyGC_Head Py_ALIGNED(1 << _PyObject_ALIGNMENT_SHIFT);
24
25 #define _PyGC_Head_UNUSED PyGC_Head
26
27
28 /* Get an object's GC head */
29 static inline PyGC_Head* _Py_AS_GC(PyObject *op) {
30 char *gc = ((char*)op) - sizeof(PyGC_Head);
31 return (PyGC_Head*)gc;
32 }
33
34 /* Get the object given the GC head */
35 static inline PyObject* _Py_FROM_GC(PyGC_Head *gc) {
36 char *op = ((char *)gc) + sizeof(PyGC_Head);
37 return (PyObject *)op;
38 }
39
40
41 /* Bit flags for ob_gc_bits (in Py_GIL_DISABLED builds)
42 *
43 * Setting the bits requires a relaxed store. The per-object lock must also be
44 * held, except when the object is only visible to a single thread (e.g. during
45 * object initialization or destruction).
46 *
47 * Reading the bits requires using a relaxed load, but does not require holding
48 * the per-object lock.
49 */
50 #ifdef Py_GIL_DISABLED
51 # define _PyGC_BITS_TRACKED (1) // Tracked by the GC
52 # define _PyGC_BITS_FINALIZED (2) // tp_finalize was called
53 # define _PyGC_BITS_UNREACHABLE (4)
54 # define _PyGC_BITS_FROZEN (8)
55 # define _PyGC_BITS_SHARED (16)
56 # define _PyGC_BITS_SHARED_INLINE (32)
57 # define _PyGC_BITS_DEFERRED (64) // Use deferred reference counting
58 #endif
59
60 #ifdef Py_GIL_DISABLED
61
62 static inline void
63 _PyObject_SET_GC_BITS(PyObject *op, uint8_t new_bits)
64 {
65 uint8_t bits = _Py_atomic_load_uint8_relaxed(&op->ob_gc_bits);
66 _Py_atomic_store_uint8_relaxed(&op->ob_gc_bits, bits | new_bits);
67 }
68
69 static inline int
70 _PyObject_HAS_GC_BITS(PyObject *op, uint8_t bits)
71 {
72 return (_Py_atomic_load_uint8_relaxed(&op->ob_gc_bits) & bits) != 0;
73 }
74
75 static inline void
76 _PyObject_CLEAR_GC_BITS(PyObject *op, uint8_t bits_to_clear)
77 {
78 uint8_t bits = _Py_atomic_load_uint8_relaxed(&op->ob_gc_bits);
79 _Py_atomic_store_uint8_relaxed(&op->ob_gc_bits, bits & ~bits_to_clear);
80 }
81
82 #endif
83
84 /* True if the object is currently tracked by the GC. */
85 static inline int _PyObject_GC_IS_TRACKED(PyObject *op) {
86 #ifdef Py_GIL_DISABLED
87 return _PyObject_HAS_GC_BITS(op, _PyGC_BITS_TRACKED);
88 #else
89 PyGC_Head *gc = _Py_AS_GC(op);
90 return (gc->_gc_next != 0);
91 #endif
92 }
93 #define _PyObject_GC_IS_TRACKED(op) _PyObject_GC_IS_TRACKED(_Py_CAST(PyObject*, op))
94
95 /* True if the object may be tracked by the GC in the future, or already is.
96 This can be useful to implement some optimizations. */
97 static inline int _PyObject_GC_MAY_BE_TRACKED(PyObject *obj) {
98 if (!PyObject_IS_GC(obj)) {
99 return 0;
100 }
101 if (PyTuple_CheckExact(obj)) {
102 return _PyObject_GC_IS_TRACKED(obj);
103 }
104 return 1;
105 }
106
107 #ifdef Py_GIL_DISABLED
108
109 /* True if memory the object references is shared between
110 * multiple threads and needs special purpose when freeing
111 * those references due to the possibility of in-flight
112 * lock-free reads occurring. The object is responsible
113 * for calling _PyMem_FreeDelayed on the referenced
114 * memory. */
115 static inline int _PyObject_GC_IS_SHARED(PyObject *op) {
116 return _PyObject_HAS_GC_BITS(op, _PyGC_BITS_SHARED);
117 }
118 #define _PyObject_GC_IS_SHARED(op) _PyObject_GC_IS_SHARED(_Py_CAST(PyObject*, op))
119
120 static inline void _PyObject_GC_SET_SHARED(PyObject *op) {
121 _PyObject_SET_GC_BITS(op, _PyGC_BITS_SHARED);
122 }
123 #define _PyObject_GC_SET_SHARED(op) _PyObject_GC_SET_SHARED(_Py_CAST(PyObject*, op))
124
125 /* True if the memory of the object is shared between multiple
126 * threads and needs special purpose when freeing due to
127 * the possibility of in-flight lock-free reads occurring.
128 * Objects with this bit that are GC objects will automatically
129 * delay-freed by PyObject_GC_Del. */
130 static inline int _PyObject_GC_IS_SHARED_INLINE(PyObject *op) {
131 return _PyObject_HAS_GC_BITS(op, _PyGC_BITS_SHARED_INLINE);
132 }
133 #define _PyObject_GC_IS_SHARED_INLINE(op) \
134 _PyObject_GC_IS_SHARED_INLINE(_Py_CAST(PyObject*, op))
135
136 static inline void _PyObject_GC_SET_SHARED_INLINE(PyObject *op) {
137 _PyObject_SET_GC_BITS(op, _PyGC_BITS_SHARED_INLINE);
138 }
139 #define _PyObject_GC_SET_SHARED_INLINE(op) \
140 _PyObject_GC_SET_SHARED_INLINE(_Py_CAST(PyObject*, op))
141
142 #endif
143
144 /* Bit flags for _gc_prev */
145 /* Bit 0 is set when tp_finalize is called */
146 #define _PyGC_PREV_MASK_FINALIZED (1)
147 /* Bit 1 is set when the object is in generation which is GCed currently. */
148 #define _PyGC_PREV_MASK_COLLECTING (2)
149 /* The (N-2) most significant bits contain the real address. */
•150 #define _PyGC_PREV_SHIFT _PyObject_ALIGNMENT_SHIFT
151 #define _PyGC_PREV_MASK (((uintptr_t) -1) << _PyGC_PREV_SHIFT)
152
153 /* set for debugging information */
154 #define _PyGC_DEBUG_STATS (1<<0) /* print collection statistics */
155 #define _PyGC_DEBUG_COLLECTABLE (1<<1) /* print collectable objects */
156 #define _PyGC_DEBUG_UNCOLLECTABLE (1<<2) /* print uncollectable objects */
157 #define _PyGC_DEBUG_SAVEALL (1<<5) /* save all garbage in gc.garbage */
158 #define _PyGC_DEBUG_LEAK _PyGC_DEBUG_COLLECTABLE | \
159 _PyGC_DEBUG_UNCOLLECTABLE | \
160 _PyGC_DEBUG_SAVEALL
161
162 typedef enum {
163 // GC was triggered by heap allocation
164 _Py_GC_REASON_HEAP,
165
166 // GC was called during shutdown
167 _Py_GC_REASON_SHUTDOWN,
168
169 // GC was called by gc.collect() or PyGC_Collect()
170 _Py_GC_REASON_MANUAL
171 } _PyGC_Reason;
172
173 // Lowest bit of _gc_next is used for flags only in GC.
174 // But it is always 0 for normal code.
175 static inline PyGC_Head* _PyGCHead_NEXT(PyGC_Head *gc) {
176 uintptr_t next = gc->_gc_next;
177 return (PyGC_Head*)next;
178 }
179 static inline void _PyGCHead_SET_NEXT(PyGC_Head *gc, PyGC_Head *next) {
180 gc->_gc_next = (uintptr_t)next;
181 }
182
183 // Lowest two bits of _gc_prev is used for _PyGC_PREV_MASK_* flags.
184 static inline PyGC_Head* _PyGCHead_PREV(PyGC_Head *gc) {
185 uintptr_t prev = (gc->_gc_prev & _PyGC_PREV_MASK);
186 return (PyGC_Head*)prev;
187 }
188 static inline void _PyGCHead_SET_PREV(PyGC_Head *gc, PyGC_Head *prev) {
189 uintptr_t uprev = (uintptr_t)prev;
190 assert((uprev & ~_PyGC_PREV_MASK) == 0);
191 gc->_gc_prev = ((gc->_gc_prev & ~_PyGC_PREV_MASK) | uprev);
192 }
193
194 static inline int _PyGC_FINALIZED(PyObject *op) {
195 #ifdef Py_GIL_DISABLED
196 return _PyObject_HAS_GC_BITS(op, _PyGC_BITS_FINALIZED);
197 #else
198 PyGC_Head *gc = _Py_AS_GC(op);
199 return ((gc->_gc_prev & _PyGC_PREV_MASK_FINALIZED) != 0);
200 #endif
201 }
202 static inline void _PyGC_SET_FINALIZED(PyObject *op) {
203 #ifdef Py_GIL_DISABLED
204 _PyObject_SET_GC_BITS(op, _PyGC_BITS_FINALIZED);
205 #else
206 PyGC_Head *gc = _Py_AS_GC(op);
207 gc->_gc_prev |= _PyGC_PREV_MASK_FINALIZED;
208 #endif
209 }
210 static inline void _PyGC_CLEAR_FINALIZED(PyObject *op) {
211 #ifdef Py_GIL_DISABLED
212 _PyObject_CLEAR_GC_BITS(op, _PyGC_BITS_FINALIZED);
213 #else
214 PyGC_Head *gc = _Py_AS_GC(op);
215 gc->_gc_prev &= ~_PyGC_PREV_MASK_FINALIZED;
216 #endif
217 }
218
219
220 /* GC runtime state */
221
222 /* If we change this, we need to change the default value in the
223 signature of gc.collect. */
224 #define NUM_GENERATIONS 3
225 /*
226 NOTE: about untracking of mutable objects.
227
228 Certain types of container cannot participate in a reference cycle, and
229 so do not need to be tracked by the garbage collector. Untracking these
230 objects reduces the cost of garbage collections. However, determining
231 which objects may be untracked is not free, and the costs must be
232 weighed against the benefits for garbage collection.
233
234 There are two possible strategies for when to untrack a container:
235
236 i) When the container is created.
237 ii) When the container is examined by the garbage collector.
238
239 Tuples containing only immutable objects (integers, strings etc, and
240 recursively, tuples of immutable objects) do not need to be tracked.
241 The interpreter creates a large number of tuples, many of which will
242 not survive until garbage collection. It is therefore not worthwhile
243 to untrack eligible tuples at creation time.
244
245 Instead, all tuples except the empty tuple are tracked when created.
246 During garbage collection it is determined whether any surviving tuples
247 can be untracked. A tuple can be untracked if all of its contents are
248 already not tracked. Tuples are examined for untracking in all garbage
249 collection cycles. It may take more than one cycle to untrack a tuple.
250
251 Dictionaries containing only immutable objects also do not need to be
252 tracked. Dictionaries are untracked when created. If a tracked item is
253 inserted into a dictionary (either as a key or value), the dictionary
254 becomes tracked. During a full garbage collection (all generations),
255 the collector will untrack any dictionaries whose contents are not
256 tracked.
257
258 The module provides the python function is_tracked(obj), which returns
259 the CURRENT tracking status of the object. Subsequent garbage
260 collections may change the tracking status of the object.
261
262 Untracking of certain containers was introduced in issue #4688, and
263 the algorithm was refined in response to issue #14775.
264 */
265
266 struct gc_generation {
267 PyGC_Head head;
268 int threshold; /* collection threshold */
269 int count; /* count of allocations or collections of younger
270 generations */
271 };
272
273 /* Running stats per generation */
274 struct gc_generation_stats {
275 /* total number of collections */
276 Py_ssize_t collections;
277 /* total number of collected objects */
278 Py_ssize_t collected;
279 /* total number of uncollectable objects (put into gc.garbage) */
280 Py_ssize_t uncollectable;
281 };
282
283 struct _gc_runtime_state {
284 /* List of objects that still need to be cleaned up, singly linked
285 * via their gc headers' gc_prev pointers. */
286 PyObject *trash_delete_later;
287 /* Current call-stack depth of tp_dealloc calls. */
288 int trash_delete_nesting;
289
290 /* Is automatic collection enabled? */
291 int enabled;
292 int debug;
293 /* linked lists of container objects */
294 struct gc_generation generations[NUM_GENERATIONS];
295 PyGC_Head *generation0;
296 /* a permanent generation which won't be collected */
297 struct gc_generation permanent_generation;
298 struct gc_generation_stats generation_stats[NUM_GENERATIONS];
299 /* true if we are currently running the collector */
300 int collecting;
301 /* list of uncollectable objects */
302 PyObject *garbage;
303 /* a list of callbacks to be invoked when collection is performed */
304 PyObject *callbacks;
305
306 /* This is the number of objects that survived the last full
307 collection. It approximates the number of long lived objects
308 tracked by the GC.
309
310 (by "full collection", we mean a collection of the oldest
311 generation). */
312 Py_ssize_t long_lived_total;
313 /* This is the number of objects that survived all "non-full"
314 collections, and are awaiting to undergo a full collection for
315 the first time. */
316 Py_ssize_t long_lived_pending;
317
318 #ifdef Py_GIL_DISABLED
319 /* gh-117783: Deferred reference counting is not fully implemented yet, so
320 as a temporary measure we treat objects using deferred reference
321 counting as immortal. The value may be zero, one, or a negative number:
322 0: immortalize deferred RC objects once the first thread is created
323 1: immortalize all deferred RC objects immediately
324 <0: suppressed; don't immortalize objects */
325 int immortalize;
326 #endif
327 };
328
329 #ifdef Py_GIL_DISABLED
330 struct _gc_thread_state {
331 /* Thread-local allocation count. */
332 Py_ssize_t alloc_count;
333 };
334 #endif
335
336
337 extern void _PyGC_InitState(struct _gc_runtime_state *);
338
339 extern Py_ssize_t _PyGC_Collect(PyThreadState *tstate, int generation,
340 _PyGC_Reason reason);
341 extern void _PyGC_CollectNoFail(PyThreadState *tstate);
342
343 /* Freeze objects tracked by the GC and ignore them in future collections. */
344 extern void _PyGC_Freeze(PyInterpreterState *interp);
345 /* Unfreezes objects placing them in the oldest generation */
346 extern void _PyGC_Unfreeze(PyInterpreterState *interp);
347 /* Number of frozen objects */
348 extern Py_ssize_t _PyGC_GetFreezeCount(PyInterpreterState *interp);
349
350 extern PyObject *_PyGC_GetObjects(PyInterpreterState *interp, int generation);
351 extern PyObject *_PyGC_GetReferrers(PyInterpreterState *interp, PyObject *objs);
352
353 // Functions to clear types free lists
354 extern void _PyGC_ClearAllFreeLists(PyInterpreterState *interp);
355 extern void _Py_ScheduleGC(PyThreadState *tstate);
356 extern void _Py_RunGC(PyThreadState *tstate);
357
358 #ifdef Py_GIL_DISABLED
359 // gh-117783: Immortalize objects that use deferred reference counting
360 extern void _PyGC_ImmortalizeDeferredObjects(PyInterpreterState *interp);
361 #endif
362
363 #ifdef __cplusplus
364 }
365 #endif
366 #endif /* !Py_INTERNAL_GC_H */