Skip to content

Commit 739f7b6

Browse files
committed
recopy changes, did not work properly
1 parent 44231d9 commit 739f7b6

File tree

8 files changed

+79
-401
lines changed

8 files changed

+79
-401
lines changed

src/_vmprof.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
#include <signal.h>
1010

1111
#include "_vmprof.h"
12+
#include "vmprof_common.h"
1213

13-
static volatile int is_enabled = 0;
1414
static destructor Original_code_dealloc = 0;
1515
static PyObject* (*_default_eval_loop)(PyFrameObject *, int) = 0;
1616

1717
#if VMPROF_UNIX
1818
#include "trampoline.h"
1919
#include "machine.h"
2020
#include "symboltable.h"
21-
#include "vmprof_main.h"
21+
#include "vmprof_unix.h"
2222
#else
23-
#include "vmprof_main_win32.h"
23+
#include "vmprof_win.h"
2424
#endif
2525
#include "vmp_stack.h"
2626

@@ -156,7 +156,7 @@ void emit_all_code_objects(PyObject * seen_code_ids)
156156

157157
static void cpyprof_code_dealloc(PyObject *co)
158158
{
159-
if (is_enabled) {
159+
if (vmprof_is_enabled()) {
160160
emit_code_object((PyCodeObject *)co);
161161
/* xxx error return values are ignored */
162162
}
@@ -187,7 +187,7 @@ static PyObject *enable_vmprof(PyObject* self, PyObject *args)
187187
return NULL;
188188
}
189189

190-
if (is_enabled) {
190+
if (vmprof_is_enabled()) {
191191
PyErr_SetString(PyExc_ValueError, "vmprof is already enabled");
192192
return NULL;
193193
}
@@ -217,13 +217,13 @@ static PyObject *enable_vmprof(PyObject* self, PyObject *args)
217217
return NULL;
218218
}
219219

220-
is_enabled = 1;
220+
vmprof_set_enabled(1);
221221

222222
Py_RETURN_NONE;
223223
}
224224

225225
static PyObject * vmp_is_enabled(PyObject *module, PyObject *noargs) {
226-
if (is_enabled) {
226+
if (vmprof_is_enabled()) {
227227
Py_RETURN_TRUE;
228228
}
229229
Py_RETURN_FALSE;
@@ -237,7 +237,7 @@ disable_vmprof(PyObject *module, PyObject *noargs)
237237
return NULL;
238238
}
239239

240-
is_enabled = 0;
240+
vmprof_set_enabled(0);
241241

242242
if (PyErr_Occurred())
243243
return NULL;
@@ -362,7 +362,7 @@ start_sampling(PyObject *module, PyObject *noargs)
362362
#ifdef VMPROF_UNIX
363363
static PyObject * vmp_get_profile_path(PyObject *module, PyObject *noargs) {
364364
PyObject * o;
365-
if (is_enabled) {
365+
if (vmprof_is_enabled()) {
366366
char buffer[4096];
367367
buffer[0] = 0;
368368
ssize_t buffer_len = vmp_fd_to_path(vmp_profile_fileno(), buffer, 4096);
@@ -382,21 +382,19 @@ static PyObject *
382382
insert_real_time_thread(PyObject *module, PyObject * noargs) {
383383
ssize_t thread_count;
384384

385-
if (!is_enabled) {
385+
if (!vmprof_is_enabled()) {
386386
PyErr_SetString(PyExc_ValueError, "vmprof is not enabled");
387387
return NULL;
388388
}
389389

390-
if (signal_type != SIGALRM) {
390+
if (vmprof_get_signal_type() != SIGALRM) {
391391
PyErr_SetString(PyExc_ValueError, "vmprof is not in real time mode");
392392
return NULL;
393393
}
394394

395-
while (__sync_lock_test_and_set(&spinlock, 1)) {
396-
}
397-
395+
vmprof_aquire_lock();
398396
thread_count = insert_thread(pthread_self(), -1);
399-
__sync_lock_release(&spinlock);
397+
vmprof_release_lock();
400398

401399
return PyLong_FromSsize_t(thread_count);
402400
}
@@ -405,21 +403,19 @@ static PyObject *
405403
remove_real_time_thread(PyObject *module, PyObject * noargs) {
406404
ssize_t thread_count;
407405

408-
if (!is_enabled) {
406+
if (!vmprof_is_enabled()) {
409407
PyErr_SetString(PyExc_ValueError, "vmprof is not enabled");
410408
return NULL;
411409
}
412410

413-
if (signal_type != SIGALRM) {
411+
if (vmprof_get_signal_type() != SIGALRM) {
414412
PyErr_SetString(PyExc_ValueError, "vmprof is not in real time mode");
415413
return NULL;
416414
}
417415

418-
while (__sync_lock_test_and_set(&spinlock, 1)) {
419-
}
420-
416+
vmprof_aquire_lock();
421417
thread_count = remove_thread(pthread_self(), -1);
422-
__sync_lock_release(&spinlock);
418+
vmprof_release_lock();
423419

424420
return PyLong_FromSsize_t(thread_count);
425421
}

src/machine.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ const char * vmp_machine_os_name(void)
2727
#endif
2828
#elif __linux__
2929
return "linux";
30+
#elif __FreeBSD__
31+
return "freebsd"
3032
#else
3133
#error "Unknown compiler"
3234
#endif
@@ -38,7 +40,7 @@ long vmp_fd_to_path(int fd, char * buffer, long buffer_len)
3840
char proffs[24];
3941
(void)snprintf(proffs, 24, "/proc/self/fd/%d", fd);
4042
return readlink(proffs, buffer, buffer_len);
41-
#elif defined(VMPROF_UNIX)
43+
#elif defined(VMPROF_UNIX) && !defined(__FreeBSD__)
4244
fcntl(fd, F_GETPATH, buffer);
4345
return strlen(buffer);
4446
#endif

src/vmp_stack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static void * libhandle = NULL;
523523

524524
int vmp_native_enable(void) {
525525
#ifdef VMPROF_LINUX
526-
if (!unw_get_reg) {
526+
if (libhandle == NULL) {
527527
if ((libhandle = dlopen(LIBUNWIND, RTLD_LAZY | RTLD_LOCAL)) == NULL) {
528528
goto bail_out;
529529
}
@@ -570,6 +570,7 @@ void vmp_native_disable(void) {
570570
vmprof_error = dlerror();
571571
fprintf(stderr, "could not close libunwind at runtime. error: %s\n", vmprof_error);
572572
}
573+
libhandle = NULL;
573574
}
574575

575576
vmp_native_traces_enabled = 0;

src/vmprof.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#pragma once
22

3+
#define _GNU_SOURCE 1
4+
5+
#ifndef RPYTHON_VMPROF
6+
#include <Python.h>
7+
#endif
8+
39
#ifdef VMPROF_UNIX
410
#include <unistd.h>
511
#endif
@@ -79,3 +85,7 @@ int IS_VMPROF_EVAL(void * ptr);
7985

8086
#endif
8187

88+
void set_current_codes(void * to);
89+
int opened_profile(const char *interp_name, int memory, int proflines, int native, int real_time);
90+
void flush_codes(void);
91+

0 commit comments

Comments
 (0)