mem.h 6.7 KB

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