/* * Return information about a given process thread. */ static PyObject * psutil_proc_query_thread(PyObject *self, PyObject *args) { int pid, tid; char path[100]; lwpstatus_t info; if (! PyArg_ParseTuple(args, "ii", &pid, &tid)) return NULL; sprintf(path, "/proc/%i/lwp/%i/lwpstatus", pid, tid); if (! psutil_file_to_struct(path, (void *)&info, sizeof(info))) return NULL; return Py_BuildValue("dd", TV2DOUBLE(info.pr_utime), TV2DOUBLE(info.pr_stime)); }
/* * Return process user and system CPU times as a Python tuple. */ static PyObject * psutil_proc_cpu_times(PyObject *self, PyObject *args) { int pid; char path[100]; pstatus_t info; if (! PyArg_ParseTuple(args, "i", &pid)) return NULL; sprintf(path, "/proc/%i/status", pid); if (! psutil_file_to_struct(path, (void *)&info, sizeof(info))) return NULL; // results are more precise than os.times() return Py_BuildValue("dd", TV2DOUBLE(info.pr_utime), TV2DOUBLE(info.pr_stime)); }
/* * Return a Python tuple (user_time, kernel_time) */ static PyObject* get_cpu_times(PyObject* self, PyObject* args) { long pid; double user_t, sys_t; struct kinfo_proc kp; if (! PyArg_ParseTuple(args, "l", &pid)) { return NULL; } if (get_kinfo_proc(pid, &kp) == -1) { return NULL; } // convert from microseconds to seconds user_t = TV2DOUBLE(kp.ki_rusage.ru_utime); sys_t = TV2DOUBLE(kp.ki_rusage.ru_stime); return Py_BuildValue("(dd)", user_t, sys_t); }
/* * Return a Python float indicating the process create time expressed in * seconds since the epoch. */ static PyObject* get_process_create_time(PyObject* self, PyObject* args) { long pid; struct kinfo_proc kp; if (! PyArg_ParseTuple(args, "l", &pid)) { return NULL; } if (get_kinfo_proc(pid, &kp) == -1) { return NULL; } return Py_BuildValue("d", TV2DOUBLE(kp.ki_start)); }
static gint gdkevent_poll (GPollFD *ufds, guint nfds, gint timeout_) { GDBusMessage *message; GVariant *body; GError *error = NULL; GTimeVal now; long diff; gint ret; g_get_current_time(&now); diff = time_val_difference(&now, &last_time); /* Log if the delay was more than a tenth of a second */ if (diff > (G_USEC_PER_SEC / 10)) { message = g_dbus_message_new_method_call(NULL, "/", "org.perfkit.Agent.GdkEvent", "Delay"); body = g_variant_new("(dd)", TV2DOUBLE(last_time), TV2DOUBLE(now)); g_dbus_message_set_body(message, body); if (!g_dbus_connection_send_message(connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error)) { CRITICAL(Gdk, "Error sending message: %s", error->message); g_error_free(error); } } ret = poll_func(ufds, nfds, timeout_); g_get_current_time(&now); last_time = now; return ret; }
/* * Return process ppid, rss, vms, ctime, nice, nthreads, status and tty * as a Python tuple. */ static PyObject * psutil_proc_basic_info(PyObject *self, PyObject *args) { int pid; char path[100]; psinfo_t info; if (! PyArg_ParseTuple(args, "i", &pid)) return NULL; sprintf(path, "/proc/%i/psinfo", pid); if (! psutil_file_to_struct(path, (void *)&info, sizeof(info))) return NULL; return Py_BuildValue("ikkdiiik", info.pr_ppid, // parent pid info.pr_rssize, // rss info.pr_size, // vms TV2DOUBLE(info.pr_start), // create time info.pr_lwp.pr_nice, // nice info.pr_nlwp, // no. of threads info.pr_lwp.pr_state, // status code info.pr_ttydev // tty nr ); }
/* * Retrieves all threads used by process returning a list of tuples * including thread id, user time and system time. * Thanks to Robert N. M. Watson: * http://fxr.googlebit.com/source/usr.bin/procstat/procstat_threads.c?v=8-CURRENT */ static PyObject* get_process_threads(PyObject* self, PyObject* args) { long pid; int mib[4]; struct kinfo_proc *kip; struct kinfo_proc *kipp; int error; unsigned int i; size_t size; PyObject* retList = PyList_New(0); PyObject* pyTuple = NULL; if (! PyArg_ParseTuple(args, "l", &pid)) { return NULL; } /* * We need to re-query for thread information, so don't use *kipp. */ mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PID | KERN_PROC_INC_THREAD; mib[3] = pid; size = 0; error = sysctl(mib, 4, NULL, &size, NULL, 0); if (error == -1) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } if (size == 0) { return NoSuchProcess(); } kip = malloc(size); if (kip == NULL) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } error = sysctl(mib, 4, kip, &size, NULL, 0); if (error == -1) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } if (size == 0) { return NoSuchProcess(); } for (i = 0; i < size / sizeof(*kipp); i++) { kipp = &kip[i]; pyTuple = Py_BuildValue("Idd", kipp->ki_tid, TV2DOUBLE(kipp->ki_rusage.ru_utime), TV2DOUBLE(kipp->ki_rusage.ru_stime) ); PyList_Append(retList, pyTuple); Py_XDECREF(pyTuple); } free(kip); return retList; }