Skip to content

Commit

Permalink
#799 / OSX: also include proc.name() in the list of grouped oneshot info
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 7, 2016
1 parent cf21849 commit 1e8cef9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
4 changes: 3 additions & 1 deletion psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
ttynr=7,
ctime=8,
status=9,
name=10,
)

pidtaskinfo_map = dict(
Expand Down Expand Up @@ -313,7 +314,8 @@ def oneshot_exit(self):

@wrap_exceptions
def name(self):
return cext.proc_name(self.pid)
name = self._get_kinfo_proc()[kinfo_proc_map['name']]
return name if name is not None else cext.proc_name(self.pid)

@wrap_exceptions
def exe(self):
Expand Down
18 changes: 16 additions & 2 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,27 @@ static PyObject *
psutil_proc_kinfo_oneshot(PyObject *self, PyObject *args) {
long pid;
struct kinfo_proc kp;
PyObject *py_name;

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;
if (psutil_get_kinfo_proc(pid, &kp) == -1)
return NULL;

#if PY_MAJOR_VERSION >= 3
py_name = PyUnicode_DecodeFSDefault(kp.kp_proc.p_comm);
#else
py_name = Py_BuildValue("s", kp.kp_proc.p_comm);
#endif
if (! py_name) {
// Likely a decoding error. We don't want to fail the whole
// operation. The python module may retry with proc_name().
PyErr_Clear();
py_name = Py_None;
}

return Py_BuildValue(
"lllllllidi",
"lllllllidiO",
(long)kp.kp_eproc.e_ppid, // (long) ppid
(long)kp.kp_eproc.e_pcred.p_ruid, // (long) real uid
(long)kp.kp_eproc.e_ucred.cr_uid, // (long) effective uid
Expand All @@ -141,7 +154,8 @@ psutil_proc_kinfo_oneshot(PyObject *self, PyObject *args) {
(long)kp.kp_eproc.e_pcred.p_svgid, // (long) saved gid
kp.kp_eproc.e_tdev, // (int) tty nr
PSUTIL_TV2DOUBLE(kp.kp_proc.p_starttime), // (double) create time
(int)kp.kp_proc.p_stat // (int) status
(int)kp.kp_proc.p_stat, // (int) status
py_name // (pystr) name
);
}

Expand Down
1 change: 1 addition & 0 deletions scripts/internal/bench_oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
'create_time',
'gids',
'memory_info',
'name',
'num_ctx_switches',
'num_threads',
'ppid',
Expand Down

0 comments on commit 1e8cef9

Please sign in to comment.