Skip to content

Commit

Permalink
#799 / BSD: also include the name() in the oneshot() info; adds furth…
Browse files Browse the repository at this point in the history
…er speedup
  • Loading branch information
giampaolo committed Oct 8, 2016
1 parent 026afd5 commit 77b484c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ Process class
+------------------------------+-------------+------------------------------+------------------------------+--------------------------+
| :meth:`ppid` | | :meth:`num_ctx_switches` | :meth:`io_counters` | :meth:`memory_info` |
+------------------------------+-------------+------------------------------+------------------------------+--------------------------+
| :meth:`status` | | :meth:`num_threads` | | :meth:`memory_percent` |
| :meth:`status` | | :meth:`num_threads` | :meth:`name` | :meth:`memory_percent` |
+------------------------------+-------------+------------------------------+------------------------------+--------------------------+
| :meth:`terminal` | | | :meth:`memory_info` | :meth:`nice` |
+------------------------------+-------------+------------------------------+------------------------------+--------------------------+
Expand Down
4 changes: 3 additions & 1 deletion psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
memtext=20,
memdata=21,
memstack=22,
name=23,
)


Expand Down Expand Up @@ -493,7 +494,8 @@ def oneshot_exit(self):

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

@wrap_exceptions
def exe(self):
Expand Down
27 changes: 24 additions & 3 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,32 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
long memstack;
kinfo_proc kp;
long pagesize = sysconf(_SC_PAGESIZE);
char str[1000];
PyObject *py_name;

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

// Process
#ifdef __FreeBSD__
sprintf(str, "%s", kp.ki_comm);
#elif defined(__OpenBSD__) || defined(__NetBSD__)
sprintf(str, "%s", kp.p_comm);
#endif
#if PY_MAJOR_VERSION >= 3
py_name = PyUnicode_DecodeFSDefault(str);
#else
py_name = Py_BuildValue("s", str);
#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;
}

// Calculate memory.
#ifdef __FreeBSD__
rss = (long)kp.ki_rssize * pagesize;
Expand Down Expand Up @@ -233,7 +253,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {

// Return a single big tuple with all process info.
return Py_BuildValue(
"(lillllllidllllddddlllll)",
"(lillllllidllllddddlllllO)",
#ifdef __FreeBSD__
//
(long)kp.ki_ppid, // (long) ppid
Expand Down Expand Up @@ -265,7 +285,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
vms, // (long) vms
memtext, // (long) mem text
memdata, // (long) mem data
memstack // (long) mem stack
memstack, // (long) mem stack
#elif defined(__OpenBSD__) || defined(__NetBSD__)
//
(long)kp.p_ppid, // (long) ppid
Expand Down Expand Up @@ -299,8 +319,9 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) {
vms, // (long) vms
memtext, // (long) mem text
memdata, // (long) mem data
memstack // (long) mem stack
memstack, // (long) mem stack
#endif
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 @@ -52,6 +52,7 @@
'io_counters',
'memory_full_info',
'memory_info',
'name',
'num_ctx_switches',
'ppid',
'status',
Expand Down

0 comments on commit 77b484c

Please sign in to comment.