Skip to content

Commit d4c10d3

Browse files
committed
remove python dump_native_symbols. it does not work to callback into python in e.g. atexit,
filter native symbols for line profiling
1 parent 4b27051 commit d4c10d3

6 files changed

Lines changed: 30 additions & 70 deletions

File tree

src/_vmprof.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,6 @@ static void cpyprof_code_dealloc(PyObject *co)
150150
Original_code_dealloc(co);
151151
}
152152

153-
154-
void dump_native_symbols(int fileno)
155-
{
156-
PyObject * mod = NULL;
157-
158-
mod = PyImport_ImportModuleNoBlock("vmprof");
159-
if (mod == NULL)
160-
goto error;
161-
162-
PyObject_CallMethod(mod, "dump_native_symbols", "(l)", fileno);
163-
164-
error:
165-
Py_XDECREF(mod);
166-
}
167-
168-
169-
170153
static PyObject *enable_vmprof(PyObject* self, PyObject *args)
171154
{
172155
int fd;

src/symboltable.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,6 @@ int vmp_resolve_addr(void * addr, char * name, int name_len, int * lineno, char
243243
return 0;
244244
}
245245

246-
#ifdef RPYTHON_VMPROF
247-
248246
#define WORD_SIZE sizeof(long)
249247
#define ADDR_SIZE sizeof(void*)
250248
#define MAXLEN 1024
@@ -254,10 +252,12 @@ void _dump_native_symbol(int fileno, void * addr, char * sym, int linenumber, ch
254252
off_t pos_before;
255253
struct str {
256254
void * addr;
257-
// NOTE windows 64, not supported yet
255+
// NOTE windows, not supported yet would be a problem for 64 bit
256+
// hint: alignment
258257
long size;
259258
char str[1024];
260259
} s;
260+
fsync(fileno);
261261
pos_before = lseek(fileno, 0, SEEK_CUR);
262262
lseek(fileno, 0, SEEK_END);
263263

@@ -286,7 +286,7 @@ int _skip_string(int fileno)
286286
{
287287
long chars;
288288
int count = read(fileno, &chars, sizeof(long));
289-
LOG("reading string of %d chars\n", chars);
289+
//LOG("reading string of %d chars\n", chars);
290290
if (count <= 0) {
291291
return 1;
292292
}
@@ -309,14 +309,14 @@ int _skip_header(int fileno, int * version, int * flags)
309309
long _read_word(int fileno)
310310
{
311311
long w;
312-
read(fileno, &w, WORD_SIZE);
312+
(void)read(fileno, &w, WORD_SIZE);
313313
return w;
314314
}
315315

316316
void * _read_addr(int fileno)
317317
{
318318
void * a;
319-
read(fileno, &a, ADDR_SIZE);
319+
(void)read(fileno, &a, ADDR_SIZE);
320320
return a;
321321
}
322322

@@ -341,21 +341,19 @@ int _skip_time_and_zone(int fileno)
341341

342342
void dump_native_symbols(int fileno)
343343
{
344-
// only call this function
345344
off_t orig_pos, cur_pos;
346345
char marker;
347346
ssize_t count;
348347
int version;
349348
int flags;
350349
int memory = 0, lines = 0, native = 0;
350+
fsync(fileno);
351351
orig_pos = lseek(fileno, 0, SEEK_CUR);
352352

353353
lseek(fileno, 5*WORD_SIZE, SEEK_SET);
354354

355355
while (1) {
356-
LOG("pre read\n");
357356
count = read(fileno, &marker, 1);
358-
LOG("post read\n");
359357
if (count <= 0) {
360358
break;
361359
}
@@ -383,7 +381,7 @@ void dump_native_symbols(int fileno)
383381
break;
384382
} case MARKER_VIRTUAL_IP:
385383
case MARKER_NATIVE_SYMBOLS: {
386-
LOG("virtip 0x%llx\n", cur_pos);
384+
//LOG("virtip 0x%llx\n", cur_pos);
387385
if (_skip_addr(fileno) != 0) { return; }
388386
if (_skip_string(fileno) != 0) { return; }
389387
break;
@@ -394,10 +392,16 @@ void dump_native_symbols(int fileno)
394392

395393
LOG("stack 0x%llx %d %d\n", cur_pos, trace_count, depth);
396394

395+
#ifdef RPYTHON_VMPROF
397396
for (i = depth/2-1; i >= 0; i--) {
398397
long kind = (long)_read_addr(fileno);
399398
void * addr = _read_addr(fileno);
400399
if (kind == VMPROF_NATIVE_TAG) {
400+
#else
401+
for (i = 0; i < depth; i++) {
402+
void * addr = _read_addr(fileno);
403+
if (((intptr_t)addr & 0x1) == 1) {
404+
#endif
401405
LOG("found kind %p\n", addr);
402406
char name[MAXLEN];
403407
char srcfile[MAXLEN];
@@ -412,14 +416,17 @@ void dump_native_symbols(int fileno)
412416
}
413417
LOG("passed memory %d \n", memory);
414418

415-
if (_skip_addr(fileno) != 0) { return; } // thread id
419+
if (version >= VERSION_THREAD_ID) {
420+
if (_skip_addr(fileno) != 0) { return; } // thread id
421+
}
416422
if (memory) {
417423
if (_skip_addr(fileno) != 0) { return; } // profile memory
418424
}
419425

420426
break;
421427
} default: {
422428
fprintf(stderr, "unknown marker 0x%x\n", marker);
429+
lseek(fileno, 0, SEEK_END);
423430
return;
424431
}
425432
}
@@ -432,4 +439,3 @@ void dump_native_symbols(int fileno)
432439

433440
lseek(fileno, 0, SEEK_END);
434441
}
435-
#endif

src/vmp_stack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static PY_STACK_FRAME_T * _write_python_stack_entry(PY_STACK_FRAME_T * frame, vo
126126
}
127127

128128
int vmp_walk_and_record_python_stack_only(PY_STACK_FRAME_T *frame, void ** result,
129-
int max_depth, int depth, intptr_t pc)
129+
int max_depth, int depth, intptr_t pc)
130130
{
131131
while (depth < max_depth && frame) {
132132
frame = _write_python_stack_entry(frame, result, &depth, max_depth);

src/vmprof_main.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ void init_cpyprof(int native)
343343
static void disable_cpyprof(void)
344344
{
345345
vmp_native_disable();
346-
dump_native_symbols(vmp_profile_fileno());
347346
}
348347
#endif
349348

@@ -376,9 +375,15 @@ int vmprof_enable(int memory, int native)
376375

377376
int close_profile(void)
378377
{
378+
int fileno = vmp_profile_fileno();
379+
fsync(fileno);
380+
dump_native_symbols(fileno);
381+
379382
(void)vmp_write_time_now(MARKER_TRAILER);
380383

381384
teardown_rss();
385+
386+
382387
/* don't close() the file descriptor from here */
383388
vmp_set_profile_fileno(-1);
384389
return 0;

vmprof/__init__.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,40 +73,6 @@ def enable(fileno, period=DEFAULT_PERIOD, memory=False, lines=False, native=None
7373
native = _is_native_enabled(native)
7474
_vmprof.enable(gz_fileno, period, memory, lines, native)
7575

76-
def dump_native_symbols(fileno):
77-
# native symbols cannot be resolved in the signal handler.
78-
# it would take far too long. Thus this method should be called
79-
# just after the sampling finished and before the file descriptor
80-
# is closed.
81-
82-
# called from C with the fileno that has been used for this profile
83-
# duplicates are avoided if this function is only called once for a profile
84-
fileobj = io.open(fileno, mode='rb', closefd=False)
85-
fileobj.seek(0)
86-
_, profiles, _, _, _, _, _ = read_prof(fileobj)
87-
88-
duplicates = set()
89-
fileobj = io.open(fileno, mode='ab', closefd=False)
90-
91-
for profile in profiles:
92-
addrs = profile[0]
93-
for addr in addrs:
94-
if addr in duplicates:
95-
continue
96-
duplicates.add(addr)
97-
if addr & 0x1 and addr > 1:
98-
name, lineno, srcfile = _vmprof.resolve_addr(addr)
99-
if name == "" and srcfile == '-':
100-
name = "<native symbol 0x%x>" % addr
101-
102-
str = "n:%s:%d:%s" % (name, lineno, srcfile)
103-
if PY3:
104-
str = str.encode()
105-
out = [MARKER_NATIVE_SYMBOLS, struct.pack("l", addr),
106-
struct.pack("l", len(str)),
107-
str]
108-
fileobj.write(b''.join(out))
109-
11076
def sample_stack_now(skip=0):
11177
""" Helper utility mostly for tests, this is considered
11278
private API.

vmprof/show.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ def walk(node, d):
193193
parts = node.name.count(':')
194194
if parts == 3:
195195
block_type, funname, funline, filename = node.name.split(':')
196-
197-
lines = d.setdefault((filename, int(funline), funname), {})
198-
199-
for l, cnt in six.iteritems(node.lines):
200-
lines[l] = lines.get(l, 0) + cnt
196+
# only python supported for line profiling
197+
if block_type == 'py':
198+
lines = d.setdefault((filename, int(funline), funname), {})
199+
for l, cnt in six.iteritems(node.lines):
200+
lines[l] = lines.get(l, 0) + cnt
201201

202202
for c in six.itervalues(node.children):
203203
walk(c, d)

0 commit comments

Comments
 (0)