forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgc.rs
More file actions
305 lines (264 loc) · 9.9 KB
/
Copy pathgc.rs
File metadata and controls
305 lines (264 loc) · 9.9 KB
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
pub(crate) use gc::module_def;
#[pymodule]
mod gc {
use crate::{
PyObjectRef, PyResult, VirtualMachine,
builtins::PyListRef,
function::{FuncArgs, OptionalArg},
gc_state,
};
// Debug flag constants
#[pyattr]
const DEBUG_STATS: u32 = gc_state::GcDebugFlags::STATS.bits();
#[pyattr]
const DEBUG_COLLECTABLE: u32 = gc_state::GcDebugFlags::COLLECTABLE.bits();
#[pyattr]
const DEBUG_UNCOLLECTABLE: u32 = gc_state::GcDebugFlags::UNCOLLECTABLE.bits();
#[pyattr]
const DEBUG_SAVEALL: u32 = gc_state::GcDebugFlags::SAVEALL.bits();
#[pyattr]
const DEBUG_LEAK: u32 = gc_state::GcDebugFlags::LEAK.bits();
/// Enable automatic garbage collection.
#[pyfunction]
fn enable() {
gc_state::gc_state().enable();
}
/// Disable automatic garbage collection.
#[pyfunction]
fn disable() {
gc_state::gc_state().disable();
}
/// Return true if automatic gc is enabled.
#[pyfunction]
fn isenabled() -> bool {
gc_state::gc_state().is_enabled()
}
/// Run a garbage collection. Returns the number of unreachable objects found.
#[derive(FromArgs)]
struct CollectArgs {
#[pyarg(any, optional)]
generation: OptionalArg<i32>,
}
#[pyfunction]
fn collect(args: CollectArgs, vm: &VirtualMachine) -> PyResult<i32> {
let generation = args.generation;
let generation_num = generation.unwrap_or(2);
if !(0..=2).contains(&generation_num) {
return Err(vm.new_value_error("invalid generation"));
}
// Invoke callbacks with "start" phase
invoke_callbacks(vm, "start", generation_num as usize, &Default::default());
// Manual gc.collect() should run even if GC is disabled
let gc = gc_state::gc_state();
let result = gc.collect_force(generation_num as usize);
// Move objects from gc_state.garbage to vm.ctx.gc_garbage (for DEBUG_SAVEALL)
{
let mut state_garbage = gc.garbage.lock();
if !state_garbage.is_empty() {
let py_garbage = &vm.ctx.gc_garbage;
let mut garbage_vec = py_garbage.borrow_vec_mut();
for obj in state_garbage.drain(..) {
garbage_vec.push(obj);
}
}
}
// Invoke callbacks with "stop" phase
invoke_callbacks(vm, "stop", generation_num as usize, &result);
Ok((result.collected + result.uncollectable) as i32)
}
/// Return the current collection thresholds as a tuple.
#[pyfunction]
fn get_threshold(vm: &VirtualMachine) -> PyObjectRef {
let (t0, t1, t2) = gc_state::gc_state().get_threshold();
vm.ctx
.new_tuple(vec![
vm.ctx.new_int(t0).into(),
vm.ctx.new_int(t1).into(),
vm.ctx.new_int(t2).into(),
])
.into()
}
/// Set the collection thresholds.
#[pyfunction]
fn set_threshold(threshold0: u32, threshold1: OptionalArg<u32>, threshold2: OptionalArg<u32>) {
gc_state::gc_state().set_threshold(
threshold0,
threshold1.into_option(),
threshold2.into_option(),
);
}
/// Return the current collection counts as a tuple.
#[pyfunction]
fn get_count(vm: &VirtualMachine) -> PyObjectRef {
let (c0, c1, c2) = gc_state::gc_state().get_count();
vm.ctx
.new_tuple(vec![
vm.ctx.new_int(c0).into(),
vm.ctx.new_int(c1).into(),
vm.ctx.new_int(c2).into(),
])
.into()
}
/// Return the current debugging flags.
#[pyfunction]
fn get_debug() -> u32 {
gc_state::gc_state().get_debug().bits()
}
/// Set the debugging flags.
#[pyfunction]
fn set_debug(flags: u32) {
gc_state::gc_state().set_debug(gc_state::GcDebugFlags::from_bits_truncate(flags));
}
/// Return a list of per-generation gc stats.
#[pyfunction]
fn get_stats(vm: &VirtualMachine) -> PyResult<PyListRef> {
let stats = gc_state::gc_state().get_stats();
let mut result = Vec::with_capacity(3);
for stat in &stats {
let dict = vm.ctx.new_dict();
dict.set_item("collections", vm.ctx.new_int(stat.collections).into(), vm)?;
dict.set_item("collected", vm.ctx.new_int(stat.collected).into(), vm)?;
dict.set_item(
"uncollectable",
vm.ctx.new_int(stat.uncollectable).into(),
vm,
)?;
dict.set_item("candidates", vm.ctx.new_int(stat.candidates).into(), vm)?;
dict.set_item("duration", vm.ctx.new_float(stat.duration).into(), vm)?;
result.push(dict.into());
}
Ok(vm.ctx.new_list(result))
}
/// Return the list of objects tracked by the collector.
#[derive(FromArgs)]
struct GetObjectsArgs {
#[pyarg(any, optional)]
generation: OptionalArg<Option<i32>>,
}
#[pyfunction]
fn get_objects(args: GetObjectsArgs, vm: &VirtualMachine) -> PyResult<PyListRef> {
let generation_opt = args.generation.flatten();
if let Some(g) = generation_opt
&& !(0..=2).contains(&g)
{
return Err(vm.new_value_error(format!("generation must be in range(0, 3), not {g}")));
}
let objects = gc_state::gc_state().get_objects(generation_opt);
Ok(vm.ctx.new_list(objects))
}
/// Return the list of objects directly referred to by any of the arguments.
#[pyfunction]
fn get_referents(args: FuncArgs, vm: &VirtualMachine) -> PyListRef {
let mut result = Vec::new();
for obj in args.args {
// Use the gc_get_referents method to get references
result.extend(obj.gc_get_referents());
}
vm.ctx.new_list(result)
}
/// Return the list of objects that directly refer to any of the arguments.
#[pyfunction]
fn get_referrers(args: FuncArgs, vm: &VirtualMachine) -> PyListRef {
use std::collections::HashSet;
// Build a set of target object pointers for fast lookup
let targets: HashSet<usize> = args
.args
.iter()
.map(|obj| obj.as_ref() as *const crate::PyObject as usize)
.collect();
// Collect pointers of frames currently on the execution stack.
// In CPython, executing frames (_PyInterpreterFrame) are not GC-tracked
// PyObjects, so they never appear in get_referrers results. Since
// RustPython materializes every frame as a PyObject, we must exclude
// them manually to match the expected behavior.
let stack_frames: HashSet<usize> = vm
.frames
.borrow()
.iter()
.map(|fp| {
let frame: &crate::PyObject = unsafe { fp.as_ref() }.as_ref();
frame as *const crate::PyObject as usize
})
.collect();
let mut result = Vec::new();
// Scan all tracked objects across all generations
let all_objects = gc_state::gc_state().get_objects(None);
for obj in all_objects {
let obj_ptr = obj.as_ref() as *const crate::PyObject as usize;
if stack_frames.contains(&obj_ptr) {
continue;
}
let referent_ptrs = unsafe { obj.gc_get_referent_ptrs() };
for child_ptr in referent_ptrs {
if targets.contains(&(child_ptr.as_ptr() as usize)) {
result.push(obj.clone());
break;
}
}
}
vm.ctx.new_list(result)
}
/// Return True if the object is tracked by the garbage collector.
#[pyfunction]
fn is_tracked(obj: PyObjectRef) -> bool {
// An object is tracked if it has IS_TRACE = true (has a trace function)
obj.is_gc_tracked()
}
/// Return True if the object has been finalized by the garbage collector.
#[pyfunction]
fn is_finalized(obj: PyObjectRef) -> bool {
obj.gc_finalized()
}
/// Freeze all objects tracked by gc.
#[pyfunction]
fn freeze() {
gc_state::gc_state().freeze();
}
/// Unfreeze all objects in the permanent generation.
#[pyfunction]
fn unfreeze() {
gc_state::gc_state().unfreeze();
}
/// Return the number of objects in the permanent generation.
#[pyfunction]
fn get_freeze_count() -> usize {
gc_state::gc_state().get_freeze_count()
}
/// gc.garbage - list of uncollectable objects
#[pyattr]
fn garbage(vm: &VirtualMachine) -> PyListRef {
vm.ctx.gc_garbage.clone()
}
/// gc.callbacks - list of callbacks to be invoked
#[pyattr]
fn callbacks(vm: &VirtualMachine) -> PyListRef {
vm.ctx.gc_callbacks.clone()
}
/// Helper function to invoke GC callbacks
fn invoke_callbacks(
vm: &VirtualMachine,
phase: &str,
generation: usize,
result: &gc_state::CollectResult,
) {
let callbacks_list = &vm.ctx.gc_callbacks;
let callbacks: Vec<PyObjectRef> = callbacks_list.borrow_vec().to_vec();
if callbacks.is_empty() {
return;
}
let phase_str: PyObjectRef = vm.ctx.new_str(phase).into();
let info = vm.ctx.new_dict();
let _ = info.set_item("generation", vm.ctx.new_int(generation).into(), vm);
let _ = info.set_item("collected", vm.ctx.new_int(result.collected).into(), vm);
let _ = info.set_item(
"uncollectable",
vm.ctx.new_int(result.uncollectable).into(),
vm,
);
let _ = info.set_item("candidates", vm.ctx.new_int(result.candidates).into(), vm);
let _ = info.set_item("duration", vm.ctx.new_float(result.duration).into(), vm);
for callback in callbacks {
let _ = callback.call((phase_str.clone(), info.clone()), vm);
}
}
}