Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
vmprof-python/src/_vmprof.c at remove-click · vmprof/vmprof-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
vmprof
/
vmprof-python
Public
Notifications
You must be signed in to change notification settings
Fork
55
Star
439
Code
Issues
54
Pull requests
5
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
remove-click
Breadcrumbs
vmprof-python
/
src
/
_vmprof.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
248 lines (214 loc) · 6.66 KB
remove-click
Breadcrumbs
vmprof-python
/
src
/
_vmprof.c
Copy path
Top
File metadata and controls
Code
Blame
248 lines (214 loc) · 6.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#define
_GNU_SOURCE
1
#include
<Python.h>
#include
<frameobject.h>
#include
<signal.h>
// for whatever reason python-dev decided to hide that one
#if
PY_MAJOR_VERSION
>=
3
&&
!defined(
_Py_atomic_load_relaxed
)
/* this was abruptly un-defined in 3.5.1 */
void
*
volatile
_PyThreadState_Current
;
/* XXX simple volatile access is assumed atomic */
# define
_Py_atomic_load_relaxed
(
pp
) (*(pp))
#endif
#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
;
double
interval
;
char
*
p_error
;
if
(!
PyArg_ParseTuple
(
args
,
"id"
,
&
fd
,
&
interval
))
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
,
"cpython"
);
if
(
p_error
) {
PyErr_SetString
(
PyExc_ValueError
,
p_error
);
return
NULL
;
}
if
(
vmprof_enable
()
<
0
) {
PyErr_SetFromErrno
(
PyExc_OSError
);
return
NULL
;
}
is_enabled
=
1
;
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
You can’t perform that action at this time.