Debian Codegraph @

rpython/rlib/rvmprof/src/shared/vmprof_common.c

raw symbols SCIP
1 #include "vmprof_common.h"
2
3 #include <assert.h>
4 #include <errno.h>
5
6 #ifdef RPYTHON_VMPROF
7
8 int get_stack_trace(PY_THREAD_STATE_T * current, void** result, int max_depth, intptr_t pc);
9
10 #ifdef RPYTHON_LL2CTYPES
11 /* only for testing: ll2ctypes sets RPY_EXTERN from the command-line */
12
13 #else
14 # include "common_header.h"
15 # include "structdef.h"
16 # include "src/threadlocal.h"
17 # include "rvmprof.h"
18 # include "forwarddecl.h"
19 #endif
20 #endif
21
22 #ifdef VMP_SUPPORTS_NATIVE_PROFILING
23 #include "vmp_stack.h" // reduces warings
24 #endif
25
26
27 static volatile int is_enabled = 0;
28 static long prepare_interval_usec = 0;
29 static long profile_interval_usec = 0;
30
31 #ifdef VMPROF_UNIX
32 static int signal_type = SIGPROF;
33 static int itimer_type = ITIMER_PROF;
34 static pthread_t *threads = NULL;
35 static size_t threads_size = 0;
36 static size_t thread_count = 0;
37 static size_t threads_size_step = 8;
38
39 int vmprof_get_itimer_type(void) {
40 return itimer_type;
41 }
42
43 int vmprof_get_signal_type(void) {
44 return signal_type;
45 }
46 #endif
47
48 #ifdef VMPROF_WINDOWS
49 #include "vmprof_win.h"
50 #endif
51
52
53 int vmprof_is_enabled(void) {
54 return is_enabled;
55 }
56
57 void vmprof_set_enabled(int value) {
58 is_enabled = value;
59 }
60
61 long vmprof_get_prepare_interval_usec(void) {
62 return prepare_interval_usec;
63 }
64
65 long vmprof_get_profile_interval_usec(void) {
66 return profile_interval_usec;
67 }
68
69 void vmprof_set_prepare_interval_usec(long value) {
70 prepare_interval_usec = value;
71 }
72
73 void vmprof_set_profile_interval_usec(long value) {
74 profile_interval_usec = value;
75 }
76
77 char *vmprof_init(int fd, double interval, int memory,
78 int proflines, const char *interp_name, int native, int real_time)
79 {
80 if (!(interval >= 1e-6 && interval < 1.0)) { /* also if it is NaN */
81 return "bad value for 'interval'";
82 }
83 prepare_interval_usec = (int)(interval * 1000000.0);
84
85 if (prepare_concurrent_bufs() < 0)
86 return "out of memory";
87 #if VMPROF_UNIX
88 if (real_time) {
89 signal_type = SIGALRM;
90 itimer_type = ITIMER_REAL;
91 } else {
92 signal_type = SIGPROF;
93 itimer_type = ITIMER_PROF;
94 }
95 set_current_codes(NULL);
96 assert(fd >= 0);
97 #else
98 if (memory) {
99 return "memory tracking only supported on unix";
100 }
101 if (native) {
102 return "native profiling only supported on unix";
103 }
104 #endif
105 vmp_set_profile_fileno(fd);
106 if (opened_profile(interp_name, memory, proflines, native, real_time) < 0) {
107 vmp_set_profile_fileno(0);
108 return strerror(errno);
109 }
110 return NULL;
111 }
112
113 int opened_profile(const char *interp_name, int memory, int proflines, int native, int real_time)
114 {
115 int success;
116 int bits;
117 struct {
118 long hdr[5];
119 char interp_name[259];
120 } header;
121
122 const char * machine;
123 size_t namelen = strnlen(interp_name, 255);
124
125 machine = vmp_machine_os_name();
126
127 header.hdr[0] = 0;
128 header.hdr[1] = 3;
129 header.hdr[2] = 0;
130 header.hdr[3] = prepare_interval_usec;
131 if (strstr(machine, "win64") != 0) {
132 header.hdr[4] = 1;
133 } else {
134 header.hdr[4] = 0;
135 }
136 header.interp_name[0] = MARKER_HEADER;
137 header.interp_name[1] = '\x00';
138 header.interp_name[2] = VERSION_TIMESTAMP;
139 header.interp_name[3] = memory*PROFILE_MEMORY + proflines*PROFILE_LINES + \
140 native*PROFILE_NATIVE + real_time*PROFILE_REAL_TIME;
141 #ifdef RPYTHON_VMPROF
142 header.interp_name[3] += PROFILE_RPYTHON;
143 #endif
144 header.interp_name[4] = (char)namelen;
145
146 memcpy(&header.interp_name[5], interp_name, namelen);
147 success = vmp_write_all((char*)&header, 5 * sizeof(long) + 5 + namelen);
148 if (success < 0) {
149 return success;
150 }
151
152 /* Write the time and the zone to the log file, profiling will start now */
153 (void)vmp_write_time_now(MARKER_TIME_N_ZONE);
154
155 /* write some more meta information */
156 vmp_write_meta("os", machine);
157 bits = vmp_machine_bits();
158 if (bits == 64) {
159 vmp_write_meta("bits", "64");
160 } else if (bits == 32) {
161 vmp_write_meta("bits", "32");
162 }
163
164 return success;
165 }
166
167
168 #ifdef RPYTHON_VMPROF
169 #ifndef RPYTHON_LL2CTYPES
170 PY_STACK_FRAME_T *get_vmprof_stack(void)
171 {
172 struct pypy_threadlocal_s *tl;
173 _OP_THREADLOCALREF_ADDR_SIGHANDLER(tl);
174 if (tl == NULL) {
175 return NULL;
176 } else {
177 return tl->vmprof_tl_stack;
178 }
179 }
180 #else
181 PY_STACK_FRAME_T *get_vmprof_stack(void)
182 {
183 return 0;
184 }
185 #endif
186
187 intptr_t vmprof_get_traceback(void *stack, void *ucontext,
188 void **result_p, intptr_t result_length)
189 {
190 int n;
191 int enabled;
192 #ifdef VMPROF_WINDOWS
193 intptr_t pc = 0; /* XXX implement me */
194 #else
195 intptr_t pc = ucontext ? (intptr_t)GetPC((ucontext_t *)ucontext) : 0;
196 #endif
197 if (stack == NULL) {
198 stack = get_vmprof_stack();
199 }
200 #ifdef VMP_SUPPORTS_NATIVE_PROFILING
201 enabled = vmp_native_enabled();
202 vmp_native_disable();
203 #endif
204 n = get_stack_trace(stack, result_p, result_length - 2, pc);
205 #ifdef VMP_SUPPORTS_NATIVE_PROFILING
206 if (enabled) {
207 vmp_native_enable();
208 }
209 #endif
210 return (intptr_t)n;
211 }
212 #endif
213
214 #ifdef VMPROF_UNIX
215
216 ssize_t search_thread(pthread_t tid, ssize_t i)
217 {
218 if (i < 0)
219 i = 0;
220 while ((size_t)i < thread_count) {
221 if (pthread_equal(threads[i], tid))
222 return i;
223 i++;
224 }
225 return -1;
226 }
227
228 ssize_t insert_thread(pthread_t tid, ssize_t i)
229 {
230 assert(signal_type == SIGALRM);
231 i = search_thread(tid, i);
232 if (i > 0)
233 return -1;
234 if (thread_count == threads_size) {
235 threads_size += threads_size_step;
236 threads = realloc(threads, sizeof(pthread_t) * threads_size);
237 assert(threads != NULL);
238 memset(threads + thread_count, 0, sizeof(pthread_t) * threads_size_step);
239 }
240 threads[thread_count++] = tid;
241 return thread_count;
242 }
243
244 ssize_t remove_thread(pthread_t tid, ssize_t i)
245 {
246 assert(signal_type == SIGALRM);
247 if (thread_count == 0)
248 return -1;
249 if (threads == NULL)
250 return -1;
251 i = search_thread(tid, i);
252 if (i < 0)
253 return -1;
254 threads[i] = threads[--thread_count];
255 threads[thread_count] = 0;
256 return thread_count;
257 }
258
259 ssize_t remove_threads(void)
260 {
261 assert(signal_type == SIGALRM);
262 if (threads != NULL) {
263 free(threads);
264 threads = NULL;
265 }
266 thread_count = 0;
267 threads_size = 0;
268 return 0;
269 }
270
271 int broadcast_signal_for_threads(void)
272 {
273 int done = 1;
274 size_t i = 0;
275 pthread_t self = pthread_self();
276 pthread_t tid;
277 while (i < thread_count) {
278 tid = threads[i];
279 if (pthread_equal(tid, self)) {
280 done = 0;
281 } else if (pthread_kill(tid, SIGALRM)) {
282 remove_thread(tid, i);
283 }
284 i++;
285 }
286 return done;
287 }
288
289 int is_main_thread(void)
290 {
291 #ifdef VMPROF_LINUX
292 pid_t pid = getpid();
293 pid_t tid = (pid_t) syscall(SYS_gettid);
294 return (pid == tid);
295 #elif defined(VMPROF_APPLE)
296 return pthread_main_np();
297 #endif
298 }
299
300 #endif