-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
Expand file tree
/
Copy pathgc_stats.h
More file actions
143 lines (121 loc) · 4.16 KB
/
gc_stats.h
File metadata and controls
143 lines (121 loc) · 4.16 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
/******************************************************************************
* Remote Debugging Module - GC Stats Functions
*
* This file contains function for read GC stats from interpreter state.
******************************************************************************/
#ifndef Py_REMOTE_DEBUGGING_GC_STATS_H
#define Py_REMOTE_DEBUGGING_GC_STATS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "_remote_debugging.h"
static int
read_gc_stats(struct gc_stats *stats, unsigned long iid, PyObject *result)
{
#define ADD_LOCAL_ULONG(name) do { \
val = PyLong_FromUnsignedLong(name); \
if (!val || PyDict_SetItemString(item, #name, val) < 0) { \
goto error; \
} \
Py_DECREF(val); \
} while(0)
#define ADD_STATS_SSIZE(name) do { \
val = PyLong_FromSsize_t(stats_item->name); \
if (!val || PyDict_SetItemString(item, #name, val) < 0) { \
goto error; \
} \
Py_DECREF(val); \
} while(0)
#define ADD_STATS_INT64(name) do { \
val = PyLong_FromInt64(stats_item->name); \
if (!val || PyDict_SetItemString(item, #name, val) < 0) { \
goto error; \
} \
Py_DECREF(val); \
} while(0)
#define ADD_STATS_DOUBLE(name) do { \
val = PyFloat_FromDouble(stats_item->name); \
if (!val || PyDict_SetItemString(item, #name, val) < 0) { \
goto error; \
} \
Py_DECREF(val); \
} while(0)
PyObject *item = NULL;
PyObject *val = NULL;
for(unsigned long gen = 0; gen < NUM_GENERATIONS; gen++) {
struct gc_generation_stats *items;
int size;
if (gen == 0) {
items = (struct gc_generation_stats *)stats->young.items;
size = GC_YOUNG_STATS_SIZE;
}
else {
items = (struct gc_generation_stats *)stats->old[gen-1].items;
size = GC_OLD_STATS_SIZE;
}
for(int i = 0; i < size; i++, items++) {
struct gc_generation_stats *stats_item = items;
item = PyDict_New();
if (item == NULL) {
goto error;
}
ADD_LOCAL_ULONG(gen);
ADD_LOCAL_ULONG(iid);
ADD_STATS_INT64(ts_start);
ADD_STATS_INT64(ts_stop);
ADD_STATS_SSIZE(heap_size);
ADD_STATS_SSIZE(work_to_do);
ADD_STATS_SSIZE(collections);
ADD_STATS_SSIZE(object_visits);
ADD_STATS_SSIZE(collected);
ADD_STATS_SSIZE(uncollectable);
ADD_STATS_SSIZE(candidates);
ADD_STATS_SSIZE(objects_transitively_reachable);
ADD_STATS_SSIZE(objects_not_transitively_reachable);
ADD_STATS_DOUBLE(duration);
val = NULL;
int rc = PyList_Append(result, item);
Py_CLEAR(item);
if (rc < 0) {
goto error;
}
}
}
#undef ADD_LOCAL_ULONG
#undef ADD_STATS_SSIZE
#undef ADD_STATS_INT64
#undef ADD_STATS_DOUBLE
return 0;
error:
Py_XDECREF(val);
Py_XDECREF(item);
return -1;
}
static int
get_gc_stats_from_interpreter_state(RuntimeOffsets *offsets,
uintptr_t interpreter_state_addr,
unsigned long iid,
void *context)
{
struct gc_stats stats;
uintptr_t gc_stats_address = interpreter_state_addr
+ offsets->debug_offsets.interpreter_state.gc
+ offsets->debug_offsets.gc.generation_stats;
uint64_t gc_stats_size = offsets->debug_offsets.gc.generation_stats_size;
if (_Py_RemoteDebug_ReadRemoteMemory(&offsets->handle,
gc_stats_address,
gc_stats_size,
&stats) < 0) {
PyErr_SetString(PyExc_RuntimeError, "Failed to read GC state");
return -1;
}
PyObject *result = context;
if (read_gc_stats(&stats, iid, result) < 0) {
return -1;
}
return 0;
}
#ifdef __cplusplus
}
#endif
#endif /* Py_REMOTE_DEBUGGING_GC_STATS_H */