blob: 4a41b77734ac41aee1c303640c1bfa7a6f487973 [file] [log] [blame]
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001#ifndef Py_INTERNAL_MEM_H
2#define Py_INTERNAL_MEM_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Victor Stinner2be00d92018-10-31 20:19:24 +01007#ifndef Py_BUILD_CORE
8# error "Py_BUILD_CORE must be defined to include this header"
9#endif
10
Eric Snow2ebc5ce2017-09-07 23:51:28 -060011#include "objimpl.h"
12#include "pymem.h"
13
Eric Snow2ebc5ce2017-09-07 23:51:28 -060014
15/* GC runtime state */
16
17/* If we change this, we need to change the default value in the
18 signature of gc.collect. */
19#define NUM_GENERATIONS 3
20
21/*
22 NOTE: about the counting of long-lived objects.
23
24 To limit the cost of garbage collection, there are two strategies;
25 - make each collection faster, e.g. by scanning fewer objects
26 - do less collections
27 This heuristic is about the latter strategy.
28
29 In addition to the various configurable thresholds, we only trigger a
30 full collection if the ratio
31 long_lived_pending / long_lived_total
32 is above a given value (hardwired to 25%).
33
34 The reason is that, while "non-full" collections (i.e., collections of
35 the young and middle generations) will always examine roughly the same
36 number of objects -- determined by the aforementioned thresholds --,
37 the cost of a full collection is proportional to the total number of
38 long-lived objects, which is virtually unbounded.
39
40 Indeed, it has been remarked that doing a full collection every
41 <constant number> of object creations entails a dramatic performance
42 degradation in workloads which consist in creating and storing lots of
43 long-lived objects (e.g. building a large list of GC-tracked objects would
44 show quadratic performance, instead of linear as expected: see issue #4074).
45
46 Using the above ratio, instead, yields amortized linear performance in
47 the total number of objects (the effect of which can be summarized
48 thusly: "each full garbage collection is more and more costly as the
49 number of objects grows, but we do fewer and fewer of them").
50
51 This heuristic was suggested by Martin von Löwis on python-dev in
52 June 2008. His original analysis and proposal can be found at:
53 http://mail.python.org/pipermail/python-dev/2008-June/080579.html
54*/
55
56/*
57 NOTE: about untracking of mutable objects.
58
59 Certain types of container cannot participate in a reference cycle, and
60 so do not need to be tracked by the garbage collector. Untracking these
61 objects reduces the cost of garbage collections. However, determining
62 which objects may be untracked is not free, and the costs must be
63 weighed against the benefits for garbage collection.
64
65 There are two possible strategies for when to untrack a container:
66
67 i) When the container is created.
68 ii) When the container is examined by the garbage collector.
69
70 Tuples containing only immutable objects (integers, strings etc, and
71 recursively, tuples of immutable objects) do not need to be tracked.
72 The interpreter creates a large number of tuples, many of which will
73 not survive until garbage collection. It is therefore not worthwhile
74 to untrack eligible tuples at creation time.
75
76 Instead, all tuples except the empty tuple are tracked when created.
77 During garbage collection it is determined whether any surviving tuples
78 can be untracked. A tuple can be untracked if all of its contents are
79 already not tracked. Tuples are examined for untracking in all garbage
80 collection cycles. It may take more than one cycle to untrack a tuple.
81
82 Dictionaries containing only immutable objects also do not need to be
83 tracked. Dictionaries are untracked when created. If a tracked item is
84 inserted into a dictionary (either as a key or value), the dictionary
85 becomes tracked. During a full garbage collection (all generations),
86 the collector will untrack any dictionaries whose contents are not
87 tracked.
88
89 The module provides the python function is_tracked(obj), which returns
90 the CURRENT tracking status of the object. Subsequent garbage
91 collections may change the tracking status of the object.
92
93 Untracking of certain containers was introduced in issue #4688, and
94 the algorithm was refined in response to issue #14775.
95*/
96
97struct gc_generation {
98 PyGC_Head head;
99 int threshold; /* collection threshold */
100 int count; /* count of allocations or collections of younger
101 generations */
102};
103
104/* Running stats per generation */
105struct gc_generation_stats {
106 /* total number of collections */
107 Py_ssize_t collections;
108 /* total number of collected objects */
109 Py_ssize_t collected;
110 /* total number of uncollectable objects (put into gc.garbage) */
111 Py_ssize_t uncollectable;
112};
113
114struct _gc_runtime_state {
115 /* List of objects that still need to be cleaned up, singly linked
116 * via their gc headers' gc_prev pointers. */
117 PyObject *trash_delete_later;
118 /* Current call-stack depth of tp_dealloc calls. */
119 int trash_delete_nesting;
120
121 int enabled;
122 int debug;
123 /* linked lists of container objects */
124 struct gc_generation generations[NUM_GENERATIONS];
125 PyGC_Head *generation0;
brainfvckc75edab2017-10-16 12:49:41 -0700126 /* a permanent generation which won't be collected */
127 struct gc_generation permanent_generation;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600128 struct gc_generation_stats generation_stats[NUM_GENERATIONS];
129 /* true if we are currently running the collector */
130 int collecting;
131 /* list of uncollectable objects */
132 PyObject *garbage;
133 /* a list of callbacks to be invoked when collection is performed */
134 PyObject *callbacks;
135 /* This is the number of objects that survived the last full
136 collection. It approximates the number of long lived objects
137 tracked by the GC.
138
139 (by "full collection", we mean a collection of the oldest
140 generation). */
141 Py_ssize_t long_lived_total;
142 /* This is the number of objects that survived all "non-full"
143 collections, and are awaiting to undergo a full collection for
144 the first time. */
145 Py_ssize_t long_lived_pending;
146};
147
148PyAPI_FUNC(void) _PyGC_Initialize(struct _gc_runtime_state *);
149
150#define _PyGC_generation0 _PyRuntime.gc.generation0
151
Victor Stinner2be00d92018-10-31 20:19:24 +0100152
153/* Set the memory allocator of the specified domain to the default.
154 Save the old allocator into *old_alloc if it's non-NULL.
155 Return on success, or return -1 if the domain is unknown. */
156PyAPI_FUNC(int) _PyMem_SetDefaultAllocator(
157 PyMemAllocatorDomain domain,
158 PyMemAllocatorEx *old_alloc);
159
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600160#ifdef __cplusplus
161}
162#endif
163#endif /* !Py_INTERNAL_MEM_H */