Repository navigation
Expand file tree
/
Copy path_vmprof.c
More file actions
243 lines (210 loc) · 6.42 KB
/
Copy path_vmprof.c
File metadata and controls
243 lines (210 loc) · 6.42 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
#define _GNU_SOURCE 1
#include <Python.h>
#include <frameobject.h>
#include <signal.h>
#define RPY_EXTERN static
static PyObject* cpyprof_PyEval_EvalFrameEx(PyFrameObject *, int);
#define VMPROF_ADDR_OF_TRAMPOLINE(x) ((x) == &cpyprof_PyEval_EvalFrameEx)
#define CPYTHON_GET_CUSTOM_OFFSET
/* This returns the address of the code object
as the identifier. The mapping from identifiers to string
representations of the code object is done elsewhere, namely:
* If the code object dies while vmprof is enabled,
PyCode_Type.tp_dealloc will emit it. (We don't handle nicely
for now the case where several code objects are created and die
at the same memory address.)
* When _vmprof.disable() is called, then we look around the
process for code objects and emit all the ones that we can
find (which we hope is very close to 100% of them).
*/
#define CODE_ADDR_TO_UID(co) (((unsigned long)(co)))
static volatile int is_enabled = 0;
#define SINGLE_BUF_SIZE (8192 - 2 * sizeof(unsigned int))
#if defined(__unix__) || defined(__APPLE__)
#include "vmprof_main.h"
#else
#include "vmprof_main_win32.h"
#endif
static destructor Original_code_dealloc = 0;
static ptrdiff_t mainloop_sp_offset;
static void* get_virtual_ip(char* sp)
{
PyFrameObject *f = *(PyFrameObject **)(sp + mainloop_sp_offset);
return (void *)CODE_ADDR_TO_UID(f->f_code);
}
static int emit_code_object(PyCodeObject *co)
{
char buf[MAX_FUNC_NAME + 1];
char *co_name, *co_filename;
int co_firstlineno;
int sz;
#if PY_MAJOR_VERSION >= 3
co_name = PyUnicode_AsUTF8(co->co_name);
if (co_name == NULL)
return -1;
co_filename = PyUnicode_AsUTF8(co->co_filename);
if (co_filename == NULL)
return -1;
#else
co_name = PyString_AS_STRING(co->co_name);
co_filename = PyString_AS_STRING(co->co_filename);
#endif
co_firstlineno = co->co_firstlineno;
sz = snprintf(buf, MAX_FUNC_NAME / 2, "py:%s", co_name);
if (sz < 0) sz = 0;
if (sz > MAX_FUNC_NAME / 2) sz = MAX_FUNC_NAME / 2;
snprintf(buf + sz, MAX_FUNC_NAME / 2, ":%d:%s", co_firstlineno,
co_filename);
return vmprof_register_virtual_function(buf, CODE_ADDR_TO_UID(co), 500000);
}
static int _look_for_code_object(PyObject *o, void *all_codes)
{
if (PyCode_Check(o) && !PySet_Contains((PyObject *)all_codes, o)) {
Py_ssize_t i;
PyCodeObject *co = (PyCodeObject *)o;
if (emit_code_object(co) < 0)
return -1;
if (PySet_Add((PyObject *)all_codes, o) < 0)
return -1;
/* as a special case, recursively look for and add code
objects found in the co_consts. The problem is that code
objects are not created as GC-aware in CPython, so we need
to hack like this to hope to find most of them.
*/
i = PyTuple_Size(co->co_consts);
while (i > 0) {
--i;
if (_look_for_code_object(PyTuple_GET_ITEM(co->co_consts, i),
all_codes) < 0)
return -1;
}
}
return 0;
}
static void emit_all_code_objects(void)
{
PyObject *gc_module = NULL, *lst = NULL, *all_codes = NULL;
Py_ssize_t i, size;
gc_module = PyImport_ImportModuleNoBlock("gc");
if (gc_module == NULL)
goto error;
lst = PyObject_CallMethod(gc_module, "get_objects", "");
if (lst == NULL || !PyList_Check(lst))
goto error;
all_codes = PySet_New(NULL);
if (all_codes == NULL)
goto error;
size = PyList_GET_SIZE(lst);
for (i = 0; i < size; i++) {
PyObject *o = PyList_GET_ITEM(lst, i);
if (o->ob_type->tp_traverse &&
o->ob_type->tp_traverse(o, _look_for_code_object, (void *)all_codes)
< 0)
goto error;
}
error:
Py_XDECREF(all_codes);
Py_XDECREF(lst);
Py_XDECREF(gc_module);
}
static void cpyprof_code_dealloc(PyObject *co)
{
if (is_enabled) {
emit_code_object((PyCodeObject *)co);
/* xxx error return values are ignored */
}
Original_code_dealloc(co);
}
static void init_cpyprof(void)
{
if (!Original_code_dealloc) {
Original_code_dealloc = PyCode_Type.tp_dealloc;
PyCode_Type.tp_dealloc = &cpyprof_code_dealloc;
}
}
static PyObject *enable_vmprof(PyObject* self, PyObject *args)
{
int fd;
int memory = 0;
int lines = 0;
double interval;
char *p_error;
if (!PyArg_ParseTuple(args, "id|ii", &fd, &interval, &memory, &lines))
return NULL;
assert(fd >= 0);
if (is_enabled) {
PyErr_SetString(PyExc_ValueError, "vmprof is already enabled");
return NULL;
}
init_cpyprof();
p_error = vmprof_init(fd, interval, memory, lines, "cpython");
if (p_error) {
PyErr_SetString(PyExc_ValueError, p_error);
return NULL;
}
if (vmprof_enable(memory) < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
is_enabled = 1;
profile_lines = lines;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *disable_vmprof(PyObject* self, PyObject *noarg)
{
if (!is_enabled) {
PyErr_SetString(PyExc_ValueError, "vmprof is not enabled");
return NULL;
}
is_enabled = 0;
vmprof_ignore_signals(1);
emit_all_code_objects();
if (vmprof_disable() < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
if (PyErr_Occurred())
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyObject* write_all_code_objects(PyObject *self, PyObject *noarg)
{
if (!is_enabled) {
PyErr_SetString(PyExc_ValueError, "vmprof is not enabled");
return NULL;
}
emit_all_code_objects();
if (PyErr_Occurred())
return NULL;
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef VmprofMethods[] = {
{"enable", enable_vmprof, METH_VARARGS,
"Enable profiling."},
{"disable", disable_vmprof, METH_NOARGS,
"Disable profiling."},
{"write_all_code_objects", write_all_code_objects, METH_NOARGS,
"Write eagerly all the IDs of code objects"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef VmprofModule = {
PyModuleDef_HEAD_INIT,
"_vmprof",
"", // doc
-1, // size
VmprofMethods
};
PyMODINIT_FUNC PyInit__vmprof(void)
{
return PyModule_Create(&VmprofModule);
}
#else
PyMODINIT_FUNC init_vmprof(void)
{
Py_InitModule("_vmprof", VmprofMethods);
}
#endif