{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathvmprof_main.h
More file actions
411 lines (352 loc) · 11.1 KB
/
Copy pathvmprof_main.h
File metadata and controls
411 lines (352 loc) · 11.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* VMPROF
*
* statistical sampling profiler specifically designed to profile programs
* which run on a Virtual Machine and/or bytecode interpreter, such as Python,
* etc.
*
* The logic to dump the C stack traces is partly stolen from the code in
* gperftools.
* The file "getpc.h" has been entirely copied from gperftools.
*
* Tested only on gcc, linux, x86_64.
*
* Copyright (C) 2014-2015
* Antonio Cuni - anto.cuni@gmail.com
* Maciej Fijalkowski - fijall@gmail.com
* Armin Rigo - arigo@tunes.org
*
*/
#define _GNU_SOURCE 1
#include <dlfcn.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include "vmprof_getpc.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "vmprof_mt.h"
#include "vmprof_common.h"
/************************************************************/
static void *(*mainloop_get_virtual_ip)(char *) = 0;
static int opened_profile(char *interp_name);
static void flush_codes(void);
/************************************************************/
/* value: last bit is 1 if signals must be ignored; all other bits
are a counter for how many threads are currently in a signal handler */
static long volatile signal_handler_value = 1;
static int _write_all(const char *buf, size_t bufsize)
{
if (profile_file == -1) {
return -1;
}
while (bufsize > 0) {
ssize_t count = write(profile_file, buf, bufsize);
if (count <= 0)
return -1; /* failed */
buf += count;
bufsize -= count;
}
return 0;
}
RPY_EXTERN
void vmprof_ignore_signals(int ignored)
{
if (!ignored) {
__sync_fetch_and_and(&signal_handler_value, ~1L);
}
else {
/* set the last bit, and wait until concurrently-running signal
handlers finish */
while (__sync_or_and_fetch(&signal_handler_value, 1L) != 1L) {
usleep(1);
}
}
}
/* *************************************************************
* functions to write a profile file compatible with gperftools
* *************************************************************
*/
static char atfork_hook_installed = 0;
/* *************************************************************
* functions to dump the stack trace
* *************************************************************
*/
PyThreadState* get_current_thread_state(void)
{
#ifdef _Py_atomic_load_relaxed
return (PyThreadState*)_Py_atomic_load_relaxed(&_PyThreadState_Current);
#else
return _PyThreadState_Current;
#endif
}
static int get_stack_trace(void** result, int max_depth, ucontext_t *ucontext)
{
PyThreadState* current = get_current_thread_state();
if (!current)
return 0;
PyFrameObject *frame = current->frame;
return read_trace_from_cpy_frame(current->frame, result, max_depth);
}
static void *get_current_thread_id(void)
{
/* xxx This function is a hack on two fronts:
- It assumes that pthread_self() is async-signal-safe. This
should be true on Linux and OS X. I hope it is also true elsewhere.
- It abuses pthread_self() by assuming it just returns an
integer. According to comments in CPython's source code, the
platforms where it is not the case are rare nowadays.
An alternative would be to try to look if the information is
available in the ucontext_t in the caller.
*/
#ifdef __APPLE__
return (void *)get_current_thread_state();
#else
return (void *)pthread_self();
#endif
}
/* *************************************************************
* the signal handler
* *************************************************************
*/
#include <setjmp.h>
volatile int spinlock;
jmp_buf restore_point;
static void segfault_handler(int arg)
{
longjmp(restore_point, SIGSEGV);
}
static void sigprof_handler(int sig_nr, siginfo_t* info, void *ucontext)
{
#ifdef __APPLE__
// TERRIBLE HACK AHEAD
// on OS X, the thread local storage is sometimes uninitialized
// when the signal handler runs - it means it's impossible to read errno
// or call any syscall or read PyThread_Current or pthread_self. Additionally,
// it seems impossible to read the register gs.
// here we register segfault handler (all guarded by a spinlock) and call
// longjmp in case segfault happens while reading a thread local
while (__sync_lock_test_and_set(&spinlock, 1)) {
}
signal(SIGSEGV, &segfault_handler);
int fault_code = setjmp(restore_point);
if (fault_code == 0) {
pthread_self();
get_current_thread_state();
} else {
signal(SIGSEGV, SIG_DFL);
__sync_synchronize();
spinlock = 0;
return;
}
signal(SIGSEGV, SIG_DFL);
__sync_synchronize();
spinlock = 0;
#endif
long val = __sync_fetch_and_add(&signal_handler_value, 2L);
if ((val & 1) == 0) {
int saved_errno = errno;
int fd = profile_file;
assert(fd >= 0);
struct profbuf_s *p = reserve_buffer(fd);
if (p == NULL) {
/* ignore this signal: there are no free buffers right now */
}
else {
int depth;
struct prof_stacktrace_s *st = (struct prof_stacktrace_s *)p->data;
st->marker = MARKER_STACKTRACE;
st->count = 1;
depth = get_stack_trace(st->stack, MAX_STACK_DEPTH-1, ucontext);
//st->stack[0] = GetPC((ucontext_t*)ucontext);
// we gonna need that for pypy
st->depth = depth;
st->stack[depth++] = get_current_thread_id();
p->data_offset = offsetof(struct prof_stacktrace_s, marker);
p->data_size = (depth * sizeof(void *) +
sizeof(struct prof_stacktrace_s) -
offsetof(struct prof_stacktrace_s, marker));
commit_buffer(fd, p);
}
errno = saved_errno;
}
__sync_sub_and_fetch(&signal_handler_value, 2L);
}
/* *************************************************************
* the setup and teardown functions
* *************************************************************
*/
static int install_sigprof_handler(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = sigprof_handler;
sa.sa_flags = SA_RESTART | SA_SIGINFO;
if (sigemptyset(&sa.sa_mask) == -1 ||
sigaction(SIGPROF, &sa, NULL) == -1)
return -1;
return 0;
}
static int remove_sigprof_handler(void)
{
if (signal(SIGPROF, SIG_DFL) == SIG_ERR)
return -1;
return 0;
}
static int install_sigprof_timer(void)
{
static struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = profile_interval_usec;
timer.it_value = timer.it_interval;
if (setitimer(ITIMER_PROF, &timer, NULL) != 0)
return -1;
return 0;
}
static int remove_sigprof_timer(void) {
static struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 0;
if (setitimer(ITIMER_PROF, &timer, NULL) != 0)
return -1;
return 0;
}
static void atfork_disable_timer(void) {
if (profile_interval_usec > 0) {
remove_sigprof_timer();
is_enabled = 0;
}
}
static void atfork_enable_timer(void) {
if (profile_interval_usec > 0) {
install_sigprof_timer();
is_enabled = 1;
}
}
static int install_pthread_atfork_hooks(void) {
/* this is needed to prevent the problems described there:
- http://code.google.com/p/gperftools/issues/detail?id=278
- http://lists.debian.org/debian-glibc/2010/03/msg00161.html
TL;DR: if the RSS of the process is large enough, the clone() syscall
will be interrupted by the SIGPROF before it can complete, then
retried, interrupted again and so on, in an endless loop. The
solution is to disable the timer around the fork, and re-enable it
only inside the parent.
*/
if (atfork_hook_installed)
return 0;
int ret = pthread_atfork(atfork_disable_timer, atfork_enable_timer, NULL);
if (ret != 0)
return -1;
atfork_hook_installed = 1;
return 0;
}
RPY_EXTERN
int vmprof_enable(void)
{
assert(profile_file >= 0);
assert(prepare_interval_usec > 0);
profile_interval_usec = prepare_interval_usec;
if (install_pthread_atfork_hooks() == -1)
goto error;
if (install_sigprof_handler() == -1)
goto error;
if (install_sigprof_timer() == -1)
goto error;
vmprof_ignore_signals(0);
return 0;
error:
profile_file = -1;
profile_interval_usec = 0;
return -1;
}
static int close_profile(void)
{
char marker = MARKER_TRAILER;
if (_write_all(&marker, 1) < 0)
return -1;
/* don't close() the file descriptor from here */
profile_file = -1;
return 0;
}
RPY_EXTERN
int vmprof_disable(void)
{
vmprof_ignore_signals(1);
profile_interval_usec = 0;
if (remove_sigprof_timer() == -1)
return -1;
if (remove_sigprof_handler() == -1)
return -1;
flush_codes();
if (shutdown_concurrent_bufs(profile_file) < 0)
return -1;
return close_profile();
}
RPY_EXTERN
int vmprof_register_virtual_function(char *code_name, long code_uid,
int auto_retry)
{
long namelen = strnlen(code_name, 1023);
long blocklen = 1 + 2 * sizeof(long) + namelen;
struct profbuf_s *p;
char *t;
retry:
p = current_codes;
if (p != NULL) {
if (__sync_bool_compare_and_swap(¤t_codes, p, NULL)) {
/* grabbed 'current_codes': we will append the current block
to it if it contains enough room */
size_t freesize = SINGLE_BUF_SIZE - p->data_size;
if (freesize < (size_t)blocklen) {
/* full: flush it */
commit_buffer(profile_file, p);
p = NULL;
}
}
else {
/* compare-and-swap failed, don't try again */
p = NULL;
}
}
if (p == NULL) {
p = reserve_buffer(profile_file);
if (p == NULL) {
/* can't get a free block; should almost never be the
case. Spin loop if allowed, or return a failure code
if not (e.g. we're in a signal handler) */
if (auto_retry > 0) {
auto_retry--;
usleep(1);
goto retry;
}
return -1;
}
}
t = p->data + p->data_size;
p->data_size += blocklen;
assert(p->data_size <= SINGLE_BUF_SIZE);
*t++ = MARKER_VIRTUAL_IP;
memcpy(t, &code_uid, sizeof(long)); t += sizeof(long);
memcpy(t, &namelen, sizeof(long)); t += sizeof(long);
memcpy(t, code_name, namelen);
/* try to reattach 'p' to 'current_codes' */
if (!__sync_bool_compare_and_swap(¤t_codes, NULL, p)) {
/* failed, flush it */
commit_buffer(profile_file, p);
}
return 0;
}
static void flush_codes(void)
{
struct profbuf_s *p = current_codes;
if (p != NULL) {
current_codes = NULL;
commit_buffer(profile_file, p);
}
}
You can’t perform that action at this time.
