| 1 |
#include "vmprof_win.h" |
| 2 |
|
| 3 |
#ifndef RPYTHON_VMPROF |
| 4 |
#if PY_VERSION_HEX >= 0x030b00f0 |
| 5 |
#include "internal/pycore_frame.h" |
| 6 |
#include "populate_frames.h" |
| 7 |
#endif |
| 8 |
#endif |
| 9 |
|
| 10 |
volatile int thread_started = 0; |
| 11 |
volatile int enabled = 0; |
| 12 |
|
| 13 |
HANDLE write_mutex; |
| 14 |
|
| 15 |
int prepare_concurrent_bufs(void) |
| 16 |
{ |
| 17 |
if (!(write_mutex = CreateMutex(NULL, FALSE, NULL))) |
| 18 |
return -1; |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
int vmprof_register_virtual_function(char *code_name, intptr_t code_uid, |
| 23 |
int auto_retry) |
| 24 |
{ |
| 25 |
char buf[2048]; |
| 26 |
long namelen; |
| 27 |
|
| 28 |
namelen = (long)strnlen(code_name, 1023); |
| 29 |
buf[0] = MARKER_VIRTUAL_IP; |
| 30 |
*(intptr_t*)(buf + 1) = code_uid; |
| 31 |
*(long*)(buf + 1 + sizeof(intptr_t)) = namelen; |
| 32 |
memcpy(buf + 1 + sizeof(intptr_t) + sizeof(long), code_name, namelen); |
| 33 |
vmp_write_all(buf, 1 + sizeof(intptr_t) + sizeof(long) + namelen); |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
int vmp_write_all(const char *buf, size_t bufsize) |
| 38 |
{ |
| 39 |
int res; |
| 40 |
int fd; |
| 41 |
int count; |
| 42 |
|
| 43 |
res = WaitForSingleObject(write_mutex, INFINITE); |
| 44 |
fd = vmp_profile_fileno(); |
| 45 |
|
| 46 |
if (fd == -1) { |
| 47 |
ReleaseMutex(write_mutex); |
| 48 |
return -1; |
| 49 |
} |
| 50 |
while (bufsize > 0) { |
| 51 |
count = _write(fd, buf, (long)bufsize); |
| 52 |
if (count <= 0) { |
| 53 |
ReleaseMutex(write_mutex); |
| 54 |
return -1; |
| 55 |
} |
| 56 |
buf += count; |
| 57 |
bufsize -= count; |
| 58 |
} |
| 59 |
ReleaseMutex(write_mutex); |
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
#if PY_VERSION_HEX < 0x030900B1 && ! defined(RPYTHON_VMPROF) |
| 64 |
static inline PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate) |
| 65 |
{ |
| 66 |
Py_XINCREF(tstate->frame); |
| 67 |
return tstate->frame; |
| 68 |
} |
| 69 |
#endif |
| 70 |
|
| 71 |
HANDLE write_mutex; |
| 72 |
|
| 73 |
#include "vmprof_common.h" |
| 74 |
|
| 75 |
int vmprof_snapshot_thread(DWORD thread_id, PY_WIN_THREAD_STATE *tstate, prof_stacktrace_s *stack) |
| 76 |
{ |
| 77 |
HRESULT result; |
| 78 |
HANDLE hThread; |
| 79 |
int depth; |
| 80 |
CONTEXT ctx; |
| 81 |
#ifdef RPYTHON_LL2CTYPES |
| 82 |
return 0; |
| 83 |
#else |
| 84 |
#if !defined(RPY_TLOFS_thread_ident) && defined(RPYTHON_VMPROF) |
| 85 |
return 0; |
| 86 |
#else |
| 87 |
hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, thread_id); |
| 88 |
if (!hThread) { |
| 89 |
return -1; |
| 90 |
} |
| 91 |
result = SuspendThread(hThread); |
| 92 |
if(result == 0xffffffff) |
| 93 |
return -1; |
| 94 |
|
| 95 |
#ifdef RPYTHON_VMPROF |
| 96 |
ctx.ContextFlags = CONTEXT_FULL; |
| 97 |
if (!GetThreadContext(hThread, &ctx)) |
| 98 |
return -1; |
| 99 |
depth = get_stack_trace(tstate->vmprof_tl_stack, |
| 100 |
stack->stack, MAX_STACK_DEPTH-2, ctx.Eip); |
| 101 |
stack->depth = depth; |
| 102 |
stack->stack[depth++] = thread_id; |
| 103 |
stack->count = 1; |
| 104 |
stack->marker = MARKER_STACKTRACE; |
| 105 |
ResumeThread(hThread); |
| 106 |
return depth; |
| 107 |
#else |
| 108 |
|
| 109 |
#if PY_VERSION_HEX >= 0x030B0000 |
| 110 |
_PyInterpreterFrame * frame = unsafe_PyThreadState_GetInterpreterFrame(tstate); |
| 111 |
#else |
| 112 |
PY_STACK_FRAME_T * frame = PyThreadState_GetFrame(tstate); |
| 113 |
#endif |
| 114 |
|
| 115 |
depth = vmp_walk_and_record_stack(frame, stack->stack, |
| 116 |
MAX_STACK_DEPTH, 0, 0); |
| 117 |
|
| 118 |
#if PY_VERSION_HEX < 0x030B0000 |
| 119 |
Py_XDECREF(frame); |
| 120 |
#endif |
| 121 |
stack->depth = depth; |
| 122 |
stack->stack[depth++] = (void*)((ULONG_PTR)thread_id); |
| 123 |
stack->count = 1; |
| 124 |
stack->marker = MARKER_STACKTRACE; |
| 125 |
ResumeThread(hThread); |
| 126 |
return depth; |
| 127 |
#endif |
| 128 |
|
| 129 |
#endif |
| 130 |
#endif |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
#if PY_MAJOR_VERSION >= 3 && !defined(_Py_atomic_load_relaxed) |
| 138 |
|
| 139 |
void *volatile _PyThreadState_Current; |
| 140 |
|
| 141 |
# define _Py_atomic_load_relaxed(pp) (*(pp)) |
| 142 |
#endif |
| 143 |
|
| 144 |
#ifndef RPYTHON_VMPROF |
| 145 |
static |
| 146 |
PY_WIN_THREAD_STATE * get_current_thread_state(void) |
| 147 |
{ |
| 148 |
#if PY_MAJOR_VERSION < 3 |
| 149 |
return _PyThreadState_Current; |
| 150 |
#elif PY_VERSION_HEX < 0x03050200 |
| 151 |
return (PyThreadState*) _Py_atomic_load_relaxed(&_PyThreadState_Current); |
| 152 |
#else |
| 153 |
return _PyThreadState_UncheckedGet(); |
| 154 |
#endif |
| 155 |
} |
| 156 |
#endif |
| 157 |
|
| 158 |
long __stdcall vmprof_mainloop(void *arg) |
| 159 |
{ |
| 160 |
#ifdef RPYTHON_LL2CTYPES |
| 161 |
|
| 162 |
return 0; |
| 163 |
#else |
| 164 |
|
| 165 |
PY_WIN_THREAD_STATE *tstate; |
| 166 |
HANDLE hThreadSnap = INVALID_HANDLE_VALUE; |
| 167 |
prof_stacktrace_s *stack = (prof_stacktrace_s*)malloc(SINGLE_BUF_SIZE); |
| 168 |
int depth; |
| 169 |
#ifndef RPYTHON_VMPROF |
| 170 |
|
| 171 |
while (1) { |
| 172 |
Sleep(vmprof_get_profile_interval_usec() * 1000); |
| 173 |
if (!enabled) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
tstate = get_current_thread_state(); |
| 177 |
if (!tstate) |
| 178 |
continue; |
| 179 |
depth = vmprof_snapshot_thread(tstate->thread_id, tstate, stack); |
| 180 |
if (depth > 0) { |
| 181 |
vmp_write_all((char*)stack + offsetof(prof_stacktrace_s, marker), |
| 182 |
SIZEOF_PROF_STACKTRACE + depth * sizeof(void*)); |
| 183 |
} |
| 184 |
} |
| 185 |
#else |
| 186 |
|
| 187 |
while (1) { |
| 188 |
|
| 189 |
Sleep(10); |
| 190 |
if (!enabled) { |
| 191 |
continue; |
| 192 |
} |
| 193 |
_RPython_ThreadLocals_Acquire(); |
| 194 |
tstate = _RPython_ThreadLocals_Head(); |
| 195 |
tstate = _RPython_ThreadLocals_Enum(tstate); |
| 196 |
while (tstate) { |
| 197 |
if (tstate->ready == 42) { |
| 198 |
depth = vmprof_snapshot_thread(tstate->thread_ident, tstate, stack); |
| 199 |
if (depth > 0) { |
| 200 |
vmp_write_all((char*)stack + offsetof(prof_stacktrace_s, marker), |
| 201 |
depth * sizeof(void *) + |
| 202 |
sizeof(struct prof_stacktrace_s) - |
| 203 |
offsetof(struct prof_stacktrace_s, marker)); |
| 204 |
} |
| 205 |
} |
| 206 |
tstate = _RPython_ThreadLocals_Enum(tstate); |
| 207 |
} |
| 208 |
_RPython_ThreadLocals_Release(); |
| 209 |
} |
| 210 |
#endif |
| 211 |
#endif |
| 212 |
} |
| 213 |
|
| 214 |
RPY_EXTERN |
| 215 |
int vmprof_enable(int memory, int native, int real_time) |
| 216 |
{ |
| 217 |
if (!thread_started) { |
| 218 |
if (!CreateThread(NULL, 0, vmprof_mainloop, NULL, 0, NULL)) { |
| 219 |
return -1; |
| 220 |
} |
| 221 |
thread_started = 1; |
| 222 |
} |
| 223 |
enabled = 1; |
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
RPY_EXTERN |
| 228 |
int vmprof_disable(void) |
| 229 |
{ |
| 230 |
char marker = MARKER_TRAILER; |
| 231 |
(void)vmp_write_time_now(MARKER_TRAILER); |
| 232 |
|
| 233 |
enabled = 0; |
| 234 |
vmp_set_profile_fileno(-1); |
| 235 |
return 0; |
| 236 |
} |
| 237 |
|
| 238 |
RPY_EXTERN |
| 239 |
void vmprof_ignore_signals(int ignored) |
| 240 |
{ |
| 241 |
enabled = !ignored; |
| 242 |
} |
| 243 |
|
| 244 |
int vmp_native_enable(void) |
| 245 |
{ |
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
void vmp_native_disable(void) |
| 250 |
{ |
| 251 |
} |
| 252 |
|
| 253 |
int get_stack_trace(PY_WIN_THREAD_STATE * current, void** result, |
| 254 |
int max_depth, intptr_t pc) |
| 255 |
{ |
| 256 |
return 0; |
| 257 |
} |