-
-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make subprocess child shows different process name #391
Changes from 10 commits
c8ab52e
07d92c0
38c75b6
6a5037e
4b7d72d
fb827cb
502208a
0687708
4b476bd
71af9c0
aabb8b3
76b26d6
aa598d6
3dc0f72
a7bc758
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -650,20 +650,27 @@ snaptrace_load(TracerObject* self, PyObject* args) | |
PyObject* dict = PyDict_New(); | ||
PyObject* args = PyDict_New(); | ||
PyObject* process_name_string = PyUnicode_FromString("process_name"); | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* process_name = PyObject_GetAttrString(current_process, "name"); | ||
PyObject* process_name = NULL; | ||
|
||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
if (self->process_name) { | ||
process_name = self->process_name; | ||
Py_INCREF(process_name); | ||
} else { | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
process_name = PyObject_GetAttrString(current_process, "name"); | ||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
} | ||
|
||
PyDict_SetItemString(dict, "ph", ph_M); | ||
PyDict_SetItemString(dict, "pid", pid); | ||
PyDict_SetItemString(dict, "tid", pid); | ||
|
@@ -898,22 +905,30 @@ snaptrace_dump(TracerObject* self, PyObject* args) | |
// == Load the metadata first == | ||
// Process Name | ||
{ | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
PyObject* process_name = NULL; | ||
if (self->process_name) { | ||
process_name = self->process_name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are losing reference here. You need to increment the reference because you are decreasing it later. |
||
Py_INCREF(process_name); | ||
} else { | ||
PyObject* current_process_method = PyObject_GetAttrString(multiprocessing_module, "current_process"); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
PyObject* current_process = PyObject_CallObject(current_process_method, NULL); | ||
if (!current_process_method) { | ||
perror("Failed to access multiprocessing.current_process()"); | ||
exit(-1); | ||
} | ||
process_name = PyObject_GetAttrString(current_process, "name"); | ||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
} | ||
PyObject* process_name = PyObject_GetAttrString(current_process, "name"); | ||
|
||
Py_DECREF(current_process_method); | ||
Py_DECREF(current_process); | ||
fprintf(fptr, "{\"ph\":\"M\",\"pid\":%lu,\"tid\":%lu,\"name\":\"process_name\",\"args\":{\"name\":\"%s\"}},", | ||
pid, pid, PyUnicode_AsUTF8(process_name)); | ||
fprintf(fptr, "{\"ph\":\"M\",\"pid\":%lu,\"tid\":%lu,\"name\":\"process_name\",\"args\":{\"name\":\"", | ||
pid, pid); | ||
fprint_escape(fptr, PyUnicode_AsUTF8(process_name)); | ||
fprintf(fptr, "\"}},"); | ||
Py_DECREF(process_name); | ||
} | ||
|
||
|
@@ -1140,11 +1155,12 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
static char* kwlist[] = {"verbose", "lib_file_path", "max_stack_depth", | ||
"include_files", "exclude_files", "ignore_c_function", "ignore_frozen", | ||
"log_func_retval", "log_func_args", "log_async", "trace_self", | ||
"min_duration", | ||
"min_duration", "process_name", | ||
NULL}; | ||
int kw_verbose = -1; | ||
int kw_max_stack_depth = 0; | ||
char* kw_lib_file_path = NULL; | ||
PyObject* kw_process_name = NULL; | ||
PyObject* kw_include_files = NULL; | ||
PyObject* kw_exclude_files = NULL; | ||
int kw_ignore_c_function = -1; | ||
|
@@ -1154,7 +1170,7 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
int kw_log_async = -1; | ||
int kw_trace_self = -1; | ||
double kw_min_duration = 0; | ||
if (!PyArg_ParseTupleAndKeywords(args, kw, "|isiOOppppppd", kwlist, | ||
if (!PyArg_ParseTupleAndKeywords(args, kw, "|isiOOppppppdO", kwlist, | ||
&kw_verbose, | ||
&kw_lib_file_path, | ||
&kw_max_stack_depth, | ||
|
@@ -1166,7 +1182,8 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
&kw_log_func_args, | ||
&kw_log_async, | ||
&kw_trace_self, | ||
&kw_min_duration)) { | ||
&kw_min_duration, | ||
&kw_process_name)) { | ||
return NULL; | ||
} | ||
|
||
|
@@ -1190,6 +1207,15 @@ snaptrace_config(TracerObject* self, PyObject* args, PyObject* kw) | |
strcpy(self->lib_file_path, kw_lib_file_path); | ||
} | ||
|
||
if (kw_process_name && !Py_IsNone(kw_process_name)) { | ||
if (!PyUnicode_CheckExact(kw_process_name)) { | ||
PyErr_SetString(PyExc_TypeError, "process_name must be a string"); | ||
return NULL; | ||
} | ||
Py_INCREF(kw_process_name); | ||
Py_XSETREF(self->process_name, kw_process_name); | ||
} | ||
|
||
if (kw_ignore_c_function == 1) { | ||
SET_FLAG(self->check_flags, SNAPTRACE_IGNORE_C_FUNCTION); | ||
} else if (kw_ignore_c_function == 0) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import sys | ||
import tempfile | ||
import unittest | ||
import json | ||
|
||
from .cmdline_tmpl import CmdlineTmpl | ||
|
||
|
@@ -44,6 +45,11 @@ def fib(n): | |
print(subprocess.call(["python", "-m", "timeit", "-n", "100", "'1+1'"])) | ||
""" | ||
|
||
file_subprocess_code = """ | ||
import subprocess | ||
p = subprocess.Popen(["python", "-c", "print('test')"]) | ||
""" | ||
|
||
file_fork = """ | ||
import os | ||
import time | ||
|
@@ -240,13 +246,26 @@ def test_child_process(self): | |
self.template(["viztracer", "-o", os.path.join(tmpdir, "result.json"), "--subprocess_child", "child.py"], | ||
expected_output_file=None) | ||
self.assertEqual(len(os.listdir(tmpdir)), 1) | ||
with open(os.path.join(tmpdir, os.listdir(tmpdir)[0])) as f: | ||
trace_events = json.load(f)["traceEvents"] | ||
for entry in trace_events: | ||
if entry["name"] == "process_name": | ||
self.assertNotEqual(entry["args"]["name"], "MainProcess") | ||
break | ||
else: | ||
self.fail("no process_name event found") | ||
|
||
def test_module(self): | ||
self.template(["viztracer", "-o", "result.json", "cmdline_test.py"], | ||
expected_output_file="result.json", | ||
expected_stdout=".*100 loops.*", | ||
script=file_subprocess_module) | ||
|
||
def test_code(self): | ||
self.template(["viztracer", "-o", "result.json", "cmdline_test.py"], | ||
expected_output_file="result.json", | ||
script=file_subprocess_code) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also check the process name of the subprocess on this, as it's known - should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the current process_name is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a process with a name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah |
||
|
||
def test_nested(self): | ||
def check_func(data): | ||
pids = set() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New reference here as well