| 1 |
/* |
| 2 |
* Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <Python.h> |
| 8 |
#include <sys/sysctl.h> |
| 9 |
#include <sys/resource.h> |
| 10 |
#include <sys/sched.h> |
| 11 |
#include <sys/types.h> |
| 12 |
|
| 13 |
#include "../../arch/all/init.h" |
| 14 |
|
| 15 |
|
| 16 |
PyObject * |
| 17 |
psutil_cpu_count_logical(PyObject *self, PyObject *args) { |
| 18 |
int mib[2]; |
| 19 |
int ncpu; |
| 20 |
size_t len; |
| 21 |
|
| 22 |
mib[0] = CTL_HW; |
| 23 |
mib[1] = HW_NCPU; |
| 24 |
len = sizeof(ncpu); |
| 25 |
|
| 26 |
if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) |
| 27 |
Py_RETURN_NONE; // mimic os.cpu_count() |
| 28 |
else |
| 29 |
return Py_BuildValue("i", ncpu); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
PyObject * |
| 34 |
psutil_cpu_times(PyObject *self, PyObject *args) { |
| 35 |
#ifdef PSUTIL_NETBSD |
| 36 |
u_int64_t cpu_time[CPUSTATES]; |
| 37 |
#else |
| 38 |
long cpu_time[CPUSTATES]; |
| 39 |
#endif |
| 40 |
size_t size = sizeof(cpu_time); |
| 41 |
int ret; |
| 42 |
|
| 43 |
#if defined(PSUTIL_FREEBSD) || defined(PSUTIL_NETBSD) |
| 44 |
ret = psutil_sysctlbyname("kern.cp_time", &cpu_time, size); |
| 45 |
#elif PSUTIL_OPENBSD |
| 46 |
int mib[] = {CTL_KERN, KERN_CPTIME}; |
| 47 |
ret = psutil_sysctl(mib, 2, &cpu_time, size); |
| 48 |
#endif |
| 49 |
if (ret != 0) |
| 50 |
return NULL; |
| 51 |
return Py_BuildValue( |
| 52 |
"(ddddd)", |
| 53 |
(double)cpu_time[CP_USER] / CLOCKS_PER_SEC, |
| 54 |
(double)cpu_time[CP_NICE] / CLOCKS_PER_SEC, |
| 55 |
(double)cpu_time[CP_SYS] / CLOCKS_PER_SEC, |
| 56 |
(double)cpu_time[CP_IDLE] / CLOCKS_PER_SEC, |
| 57 |
(double)cpu_time[CP_INTR] / CLOCKS_PER_SEC |
| 58 |
); |
| 59 |
} |