[pypy-commit] pypy vmprof-0.4.10: update the src/shared files to vmprof==0.4.10

antocuni pypy.commits at gmail.com
Mon Nov 6 05:40:35 EST 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: vmprof-0.4.10
Changeset: r92954:e4158aeecc04
Date: 2017-11-06 11:39 +0100
http://bitbucket.org/pypy/pypy/changeset/e4158aeecc04/

Log:	update the src/shared files to vmprof==0.4.10

diff --git a/rpython/rlib/rvmprof/src/shared/vmprof_common.c b/rpython/rlib/rvmprof/src/shared/vmprof_common.c
--- a/rpython/rlib/rvmprof/src/shared/vmprof_common.c
+++ b/rpython/rlib/rvmprof/src/shared/vmprof_common.c
@@ -32,12 +32,21 @@
 static size_t threads_size = 0;
 static size_t thread_count = 0;
 static size_t threads_size_step = 8;
-#endif
 
 int vmprof_get_itimer_type(void) {
     return itimer_type;
 }
 
+int vmprof_get_signal_type(void) {
+    return signal_type;
+}
+#endif
+
+#ifdef VMPROF_WINDOWS
+#include "vmprof_win.h"
+#endif
+
+
 int vmprof_is_enabled(void) {
     return is_enabled;
 }
@@ -62,10 +71,6 @@
     profile_interval_usec = value;
 }
 
-int vmprof_get_signal_type(void) {
-    return signal_type;
-}
-
 char *vmprof_init(int fd, double interval, int memory,
                   int proflines, const char *interp_name, int native, int real_time)
 {
diff --git a/rpython/rlib/rvmprof/src/shared/vmprof_common.h b/rpython/rlib/rvmprof/src/shared/vmprof_common.h
--- a/rpython/rlib/rvmprof/src/shared/vmprof_common.h
+++ b/rpython/rlib/rvmprof/src/shared/vmprof_common.h
@@ -15,7 +15,9 @@
 #include <pthread.h>
 #endif
 
+#ifdef VMPROF_UNIX
 #include "vmprof_getpc.h"
+#endif
 
 #ifdef VMPROF_LINUX
 #include <syscall.h>
diff --git a/rpython/rlib/rvmprof/src/shared/vmprof_memory.c b/rpython/rlib/rvmprof/src/shared/vmprof_memory.c
--- a/rpython/rlib/rvmprof/src/shared/vmprof_memory.c
+++ b/rpython/rlib/rvmprof/src/shared/vmprof_memory.c
@@ -8,7 +8,7 @@
 #include <mach/task_info.h>
 
 static mach_port_t mach_task;
-#else
+#elif defined(VMPROF_UNIX)
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
diff --git a/rpython/rlib/rvmprof/src/shared/vmprof_unix.c b/rpython/rlib/rvmprof/src/shared/vmprof_unix.c
--- a/rpython/rlib/rvmprof/src/shared/vmprof_unix.c
+++ b/rpython/rlib/rvmprof/src/shared/vmprof_unix.c
@@ -41,8 +41,6 @@
 void vmprof_ignore_signals(int ignored)
 {
     if (ignored) {
-        /* set the last bit, and wait until concurrently-running signal
-           handlers finish */
         __sync_add_and_fetch(&signal_handler_ignore, 1L);
         while (signal_handler_entries != 0L) {
             usleep(1);
@@ -370,7 +368,7 @@
         goto error;
     if (install_sigprof_timer() == -1)
         goto error;
-    vmprof_ignore_signals(0);
+    signal_handler_ignore = 0;
     return 0;
 
  error:
@@ -394,7 +392,7 @@
 
 int vmprof_disable(void)
 {
-    vmprof_ignore_signals(1);
+    signal_handler_ignore = 1;
     vmprof_set_profile_interval_usec(0);
 #ifdef VMP_SUPPORTS_NATIVE_PROFILING
     disable_cpyprof();
diff --git a/rpython/rlib/rvmprof/src/shared/vmprof_win.c b/rpython/rlib/rvmprof/src/shared/vmprof_win.c
--- a/rpython/rlib/rvmprof/src/shared/vmprof_win.c
+++ b/rpython/rlib/rvmprof/src/shared/vmprof_win.c
@@ -1,7 +1,7 @@
-// cannot include this header because it also has definitions
-#include "windows.h"
-#include "compat.h"
-#include "vmp_stack.h"
+#include "vmprof_win.h"
+
+volatile int thread_started = 0;
+volatile int enabled = 0;
 
 HANDLE write_mutex;
 
@@ -12,7 +12,20 @@
     return 0;
 }
 
-#include <tlhelp32.h>
+int vmprof_register_virtual_function(char *code_name, intptr_t code_uid,
+                                     int auto_retry)
+{
+    char buf[2048];
+    long namelen;
+
+    namelen = (long)strnlen(code_name, 1023);
+    buf[0] = MARKER_VIRTUAL_IP;
+    *(intptr_t*)(buf + 1) = code_uid;
+    *(long*)(buf + 1 + sizeof(intptr_t)) = namelen;
+    memcpy(buf + 1 + sizeof(intptr_t) + sizeof(long), code_name, namelen);
+    vmp_write_all(buf, 1 + sizeof(intptr_t) + sizeof(long) + namelen);
+    return 0;
+}
 
 int vmp_write_all(const char *buf, size_t bufsize)
 {
@@ -40,3 +53,168 @@
     return 0;
 }
 
+HANDLE write_mutex;
+
+#include "vmprof_common.h"
+
+int vmprof_snapshot_thread(DWORD thread_id, PY_WIN_THREAD_STATE *tstate, prof_stacktrace_s *stack)
+{
+    HRESULT result;
+    HANDLE hThread;
+    int depth;
+    CONTEXT ctx;
+#ifdef RPYTHON_LL2CTYPES
+    return 0; // not much we can do
+#else
+#if !defined(RPY_TLOFS_thread_ident) && defined(RPYTHON_VMPROF)
+    return 0; // we can't freeze threads, unsafe
+#else
+    hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread_id);
+    if (!hThread) {
+        return -1;
+    }
+    result = SuspendThread(hThread);
+    if(result == 0xffffffff)
+        return -1; // possible, e.g. attached debugger or thread alread suspended
+    // find the correct thread
+#ifdef RPYTHON_VMPROF
+    ctx.ContextFlags = CONTEXT_FULL;
+    if (!GetThreadContext(hThread, &ctx))
+        return -1;
+    depth = get_stack_trace(tstate->vmprof_tl_stack,
+                     stack->stack, MAX_STACK_DEPTH-2, ctx.Eip);
+    stack->depth = depth;
+    stack->stack[depth++] = thread_id;
+    stack->count = 1;
+    stack->marker = MARKER_STACKTRACE;
+    ResumeThread(hThread);
+    return depth;
+#else
+    depth = vmp_walk_and_record_stack(tstate->frame, stack->stack,
+                                      MAX_STACK_DEPTH, 0, 0);
+    stack->depth = depth;
+    stack->stack[depth++] = (void*)((ULONG_PTR)thread_id);
+    stack->count = 1;
+    stack->marker = MARKER_STACKTRACE;
+    ResumeThread(hThread);
+    return depth;
+#endif
+
+#endif
+#endif
+}
+
+#ifndef RPYTHON_VMPROF
+static
+PY_WIN_THREAD_STATE * get_current_thread_state(void)
+{
+#if PY_MAJOR_VERSION < 3
+    return _PyThreadState_Current;
+#elif PY_VERSION_HEX < 0x03050200
+    return (PyThreadState*) _Py_atomic_load_relaxed(&_PyThreadState_Current);
+#else
+    return _PyThreadState_UncheckedGet();
+#endif
+}
+#endif
+
+long __stdcall vmprof_mainloop(void *arg)
+{
+#ifdef RPYTHON_LL2CTYPES
+    // for tests only
+    return 0;
+#else
+    // it is not a test case!
+    PY_WIN_THREAD_STATE *tstate;
+    HANDLE hThreadSnap = INVALID_HANDLE_VALUE; 
+    prof_stacktrace_s *stack = (prof_stacktrace_s*)malloc(SINGLE_BUF_SIZE);
+    int depth;
+#ifndef RPYTHON_VMPROF
+    // cpython version
+    while (1) {
+        Sleep(vmprof_get_profile_interval_usec() * 1000);
+        if (!enabled) {
+            continue;
+        }
+        tstate = get_current_thread_state();
+        if (!tstate)
+            continue;
+        depth = vmprof_snapshot_thread(tstate->thread_id, tstate, stack);
+        if (depth > 0) {
+            vmp_write_all((char*)stack + offsetof(prof_stacktrace_s, marker),
+                          SIZEOF_PROF_STACKTRACE + depth * sizeof(void*));
+        }
+    }
+#else
+    // pypy version
+    while (1) {
+        //Sleep(vmprof_get_profile_interval_usec() * 1000);
+        Sleep(10);
+        if (!enabled) {
+            continue;
+        }
+        _RPython_ThreadLocals_Acquire();
+        tstate = _RPython_ThreadLocals_Head(); // the first one is one behind head
+        tstate = _RPython_ThreadLocals_Enum(tstate);
+        while (tstate) {
+            if (tstate->ready == 42) {
+                depth = vmprof_snapshot_thread(tstate->thread_ident, tstate, stack);
+                if (depth > 0) {
+                    vmp_write_all((char*)stack + offsetof(prof_stacktrace_s, marker),
+                         depth * sizeof(void *) +
+                         sizeof(struct prof_stacktrace_s) -
+                         offsetof(struct prof_stacktrace_s, marker));
+                }
+            }
+            tstate = _RPython_ThreadLocals_Enum(tstate);
+        }
+        _RPython_ThreadLocals_Release();
+    }
+#endif
+#endif
+}
+
+RPY_EXTERN
+int vmprof_enable(int memory, int native, int real_time)
+{
+    if (!thread_started) {
+        if (!CreateThread(NULL, 0, vmprof_mainloop, NULL, 0, NULL)) {
+            return -1;
+        }
+        thread_started = 1;
+    }
+    enabled = 1;
+    return 0;
+}
+
+RPY_EXTERN
+int vmprof_disable(void)
+{
+    char marker = MARKER_TRAILER;
+    (void)vmp_write_time_now(MARKER_TRAILER);
+
+    enabled = 0;
+    vmp_set_profile_fileno(-1);
+    return 0;
+}
+
+RPY_EXTERN
+void vmprof_ignore_signals(int ignored)
+{
+    enabled = !ignored;
+}
+
+int vmp_native_enable(void)
+{
+    return 0;
+}
+
+void vmp_native_disable(void)
+{
+}
+
+int get_stack_trace(PY_WIN_THREAD_STATE * current, void** result,
+                    int max_depth, intptr_t pc)
+{
+    return 0;
+}
diff --git a/rpython/rlib/rvmprof/src/shared/vmprof_win.h b/rpython/rlib/rvmprof/src/shared/vmprof_win.h
--- a/rpython/rlib/rvmprof/src/shared/vmprof_win.h
+++ b/rpython/rlib/rvmprof/src/shared/vmprof_win.h
@@ -3,20 +3,13 @@
 #include "windows.h"
 #include "compat.h"
 #include "vmp_stack.h"
-
-HANDLE write_mutex;
+#include <tlhelp32.h>
 
 int prepare_concurrent_bufs(void);
 
-#include "vmprof_common.h"
-#include <tlhelp32.h>
-
 // This file has been inspired (but not copied from since the LICENSE
 // would not allow it) from verysleepy profiler
 
-volatile int thread_started = 0;
-volatile int enabled = 0;
-
 int vmp_write_all(const char *buf, size_t bufsize);
 
 #ifdef RPYTHON_VMPROF
@@ -26,178 +19,14 @@
 #endif
 
 
-RPY_EXTERN
 int vmprof_register_virtual_function(char *code_name, intptr_t code_uid,
-                                     int auto_retry)
-{
-    char buf[2048];
-    long namelen;
+                                     int auto_retry);
 
-    namelen = (long)strnlen(code_name, 1023);
-    buf[0] = MARKER_VIRTUAL_IP;
-    *(intptr_t*)(buf + 1) = code_uid;
-    *(long*)(buf + 1 + sizeof(intptr_t)) = namelen;
-    memcpy(buf + 1 + sizeof(intptr_t) + sizeof(long), code_name, namelen);
-    vmp_write_all(buf, 1 + sizeof(intptr_t) + sizeof(long) + namelen);
-    return 0;
-}
-
-int vmprof_snapshot_thread(DWORD thread_id, PY_WIN_THREAD_STATE *tstate, prof_stacktrace_s *stack)
-{
-    HRESULT result;
-    HANDLE hThread;
-    int depth;
-    CONTEXT ctx;
-#ifdef RPYTHON_LL2CTYPES
-    return 0; // not much we can do
-#else
-#if !defined(RPY_TLOFS_thread_ident) && defined(RPYTHON_VMPROF)
-    return 0; // we can't freeze threads, unsafe
-#else
-    hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread_id);
-    if (!hThread) {
-        return -1;
-    }
-    result = SuspendThread(hThread);
-    if(result == 0xffffffff)
-        return -1; // possible, e.g. attached debugger or thread alread suspended
-    // find the correct thread
-#ifdef RPYTHON_VMPROF
-    ctx.ContextFlags = CONTEXT_FULL;
-    if (!GetThreadContext(hThread, &ctx))
-        return -1;
-    depth = get_stack_trace(tstate->vmprof_tl_stack,
-                     stack->stack, MAX_STACK_DEPTH-2, ctx.Eip);
-    stack->depth = depth;
-    stack->stack[depth++] = thread_id;
-    stack->count = 1;
-    stack->marker = MARKER_STACKTRACE;
-    ResumeThread(hThread);
-    return depth;
-#else
-    depth = vmp_walk_and_record_stack(tstate->frame, stack->stack,
-                                      MAX_STACK_DEPTH, 0, 0);
-    stack->depth = depth;
-    stack->stack[depth++] = (void*)((ULONG_PTR)thread_id);
-    stack->count = 1;
-    stack->marker = MARKER_STACKTRACE;
-    ResumeThread(hThread);
-    return depth;
-#endif
-
-#endif
-#endif
-}
-
-#ifndef RPYTHON_VMPROF
-static
-PY_WIN_THREAD_STATE * get_current_thread_state(void)
-{
-#if PY_MAJOR_VERSION < 3
-    return _PyThreadState_Current;
-#elif PY_VERSION_HEX < 0x03050200
-    return (PyThreadState*) _Py_atomic_load_relaxed(&_PyThreadState_Current);
-#else
-    return _PyThreadState_UncheckedGet();
-#endif
-}
-#endif
-
-long __stdcall vmprof_mainloop(void *arg)
-{
-#ifdef RPYTHON_LL2CTYPES
-    // for tests only
-    return 0;
-#else
-    // it is not a test case!
-    PY_WIN_THREAD_STATE *tstate;
-    HANDLE hThreadSnap = INVALID_HANDLE_VALUE; 
-    prof_stacktrace_s *stack = (prof_stacktrace_s*)malloc(SINGLE_BUF_SIZE);
-    int depth;
-#ifndef RPYTHON_VMPROF
-    // cpython version
-    while (1) {
-        Sleep(profile_interval_usec * 1000);
-        if (!enabled) {
-            continue;
-        }
-        tstate = get_current_thread_state();
-        if (!tstate)
-            continue;
-        depth = vmprof_snapshot_thread(tstate->thread_id, tstate, stack);
-        if (depth > 0) {
-            vmp_write_all((char*)stack + offsetof(prof_stacktrace_s, marker),
-                          SIZEOF_PROF_STACKTRACE + depth * sizeof(void*));
-        }
-    }
-#else
-    // pypy version
-    while (1) {
-        //Sleep(profile_interval_usec * 1000);
-        Sleep(10);
-        if (!enabled) {
-            continue;
-        }
-        _RPython_ThreadLocals_Acquire();
-        tstate = _RPython_ThreadLocals_Head(); // the first one is one behind head
-        tstate = _RPython_ThreadLocals_Enum(tstate);
-        while (tstate) {
-            if (tstate->ready == 42) {
-                depth = vmprof_snapshot_thread(tstate->thread_ident, tstate, stack);
-                if (depth > 0) {
-                    vmp_write_all((char*)stack + offsetof(prof_stacktrace_s, marker),
-                         depth * sizeof(void *) +
-                         sizeof(struct prof_stacktrace_s) -
-                         offsetof(struct prof_stacktrace_s, marker));
-                }
-            }
-            tstate = _RPython_ThreadLocals_Enum(tstate);
-        }
-        _RPython_ThreadLocals_Release();
-    }
-#endif
-#endif
-}
-
-RPY_EXTERN
-int vmprof_enable(int memory, int native, int real_time)
-{
-    if (!thread_started) {
-        if (!CreateThread(NULL, 0, vmprof_mainloop, NULL, 0, NULL)) {
-            return -1;
-        }
-        thread_started = 1;
-    }
-    enabled = 1;
-    return 0;
-}
-
-RPY_EXTERN
-int vmprof_disable(void)
-{
-    char marker = MARKER_TRAILER;
-    (void)vmp_write_time_now(MARKER_TRAILER);
-
-    enabled = 0;
-    vmp_set_profile_fileno(-1);
-    return 0;
-}
-
-RPY_EXTERN
-void vmprof_ignore_signals(int ignored)
-{
-    enabled = !ignored;
-}
-
-int vmp_native_enable(void) {
-    return 0;
-}
-
-void vmp_native_disable(void) {
-}
-
+PY_WIN_THREAD_STATE * get_current_thread_state(void);
+int vmprof_enable(int memory, int native, int real_time);
+int vmprof_disable(void);
+void vmprof_ignore_signals(int ignored);
+int vmp_native_enable(void);
+void vmp_native_disable(void);
 int get_stack_trace(PY_WIN_THREAD_STATE * current, void** result,
-		    int max_depth, intptr_t pc)
-{
-    return 0;
-}
+                    int max_depth, intptr_t pc);


More information about the pypy-commit mailing list