summaryrefslogtreecommitdiff
path: root/psutil
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2022-01-16 19:25:29 +0000
committerGiampaolo Rodola <g.rodola@gmail.com>2022-01-16 19:25:29 +0000
commit11a1f34a7f250e2385b39b29e7fbc33e9357c392 (patch)
tree6d149e6ee52b6ca9ecd3a6c7784a343d4258f0c7 /psutil
parent088ccb23fc93c17b5f30bc544b45c94924cf48f3 (diff)
parent98b49486f81f085bceda60b01b0a8f69a7008290 (diff)
downloadpsutil-11a1f34a7f250e2385b39b29e7fbc33e9357c392.tar.gz
merge from master
Diffstat (limited to 'psutil')
-rw-r--r--psutil/_psbsd.py54
-rw-r--r--psutil/_psutil_bsd.c7
-rw-r--r--psutil/arch/openbsd/cpu.c18
-rw-r--r--psutil/arch/openbsd/cpu.h5
-rwxr-xr-xpsutil/tests/test_contracts.py2
5 files changed, 56 insertions, 30 deletions
diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py
index 52885065..c4200cce 100644
--- a/psutil/_psbsd.py
+++ b/psutil/_psbsd.py
@@ -310,6 +310,36 @@ def cpu_stats():
return _common.scpustats(ctxsw, intrs, soft_intrs, syscalls)
+if FREEBSD:
+ def cpu_freq():
+ """Return frequency metrics for CPUs. As of Dec 2018 only
+ CPU 0 appears to be supported by FreeBSD and all other cores
+ match the frequency of CPU 0.
+ """
+ ret = []
+ num_cpus = cpu_count_logical()
+ for cpu in range(num_cpus):
+ try:
+ current, available_freq = cext.cpu_freq(cpu)
+ except NotImplementedError:
+ continue
+ if available_freq:
+ try:
+ min_freq = int(available_freq.split(" ")[-1].split("/")[0])
+ except(IndexError, ValueError):
+ min_freq = None
+ try:
+ max_freq = int(available_freq.split(" ")[0].split("/")[0])
+ except(IndexError, ValueError):
+ max_freq = None
+ ret.append(_common.scpufreq(current, min_freq, max_freq))
+ return ret
+elif OPENBSD:
+ def cpu_freq():
+ curr = float(cext.cpu_freq())
+ return [_common.scpufreq(curr, 0.0, 0.0)]
+
+
# =====================================================================
# --- disks
# =====================================================================
@@ -439,30 +469,6 @@ if FREEBSD:
return ret
- def cpu_freq():
- """Return frequency metrics for CPUs. As of Dec 2018 only
- CPU 0 appears to be supported by FreeBSD and all other cores
- match the frequency of CPU 0.
- """
- ret = []
- num_cpus = cpu_count_logical()
- for cpu in range(num_cpus):
- try:
- current, available_freq = cext.cpu_frequency(cpu)
- except NotImplementedError:
- continue
- if available_freq:
- try:
- min_freq = int(available_freq.split(" ")[-1].split("/")[0])
- except(IndexError, ValueError):
- min_freq = None
- try:
- max_freq = int(available_freq.split(" ")[0].split("/")[0])
- except(IndexError, ValueError):
- max_freq = None
- ret.append(_common.scpufreq(current, min_freq, max_freq))
- return ret
-
# =====================================================================
# --- other system functions
diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c
index 6bd7eb08..3d92375d 100644
--- a/psutil/_psutil_bsd.c
+++ b/psutil/_psutil_bsd.c
@@ -1133,6 +1133,10 @@ static PyMethodDef mod_methods[] = {
"Return currently connected users as a list of tuples"},
{"cpu_stats", psutil_cpu_stats, METH_VARARGS,
"Return CPU statistics"},
+#if defined(PSUTIL_FREEBSD) || defined(PSUTIL_OPENBSD)
+ {"cpu_freq", psutil_cpu_freq, METH_VARARGS,
+ "Return CPU frequency"},
+#endif
#if defined(PSUTIL_FREEBSD) || defined(PSUTIL_NETBSD)
{"net_connections", psutil_net_connections, METH_VARARGS,
"Return system-wide open connections."},
@@ -1142,14 +1146,11 @@ static PyMethodDef mod_methods[] = {
"Return battery information."},
{"sensors_cpu_temperature", psutil_sensors_cpu_temperature, METH_VARARGS,
"Return temperature information for a given CPU core number."},
- {"cpu_frequency", psutil_cpu_freq, METH_VARARGS,
- "Return frequency of a given CPU"},
#endif
#if defined(PSUTIL_OPENBSD)
{"cpu_vendor", psutil_cpu_vendor, METH_VARARGS,
"Return the CPU vendor string."},
#endif
-
// --- others
{"set_debug", psutil_set_debug, METH_VARARGS,
"Enable or disable PSUTIL_DEBUG messages"},
diff --git a/psutil/arch/openbsd/cpu.c b/psutil/arch/openbsd/cpu.c
index 68a353b5..e2857a98 100644
--- a/psutil/arch/openbsd/cpu.c
+++ b/psutil/arch/openbsd/cpu.c
@@ -92,6 +92,24 @@ psutil_cpu_stats(PyObject *self, PyObject *args) {
PyObject *
+psutil_cpu_freq(PyObject *self, PyObject *args) {
+ int freq;
+ size_t size;
+ int mib[2] = {CTL_HW, HW_CPUSPEED};
+
+ // On VirtualBox I get "sysctl hw.cpuspeed=2593" (never changing),
+ // which appears to be expressed in Mhz.
+ size = sizeof(freq);
+ if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+
+ return Py_BuildValue("i", freq);
+}
+
+
+PyObject *
psutil_cpu_vendor(PyObject *self, PyObject *args) {
int mib[2];
char vendor[128];
diff --git a/psutil/arch/openbsd/cpu.h b/psutil/arch/openbsd/cpu.h
index f30f2e52..5d65e769 100644
--- a/psutil/arch/openbsd/cpu.h
+++ b/psutil/arch/openbsd/cpu.h
@@ -7,6 +7,7 @@
#include <Python.h>
-PyObject *psutil_cpu_stats(PyObject *self, PyObject *args);
-PyObject *psutil_per_cpu_times(PyObject *self, PyObject *args);
+PyObject *psutil_cpu_freq(PyObject* self, PyObject* args);
+PyObject *psutil_cpu_stats(PyObject* self, PyObject* args);
PyObject *psutil_cpu_vendor(PyObject *self, PyObject *args);
+PyObject *psutil_per_cpu_times(PyObject *self, PyObject *args);
diff --git a/psutil/tests/test_contracts.py b/psutil/tests/test_contracts.py
index d16a7066..73596856 100755
--- a/psutil/tests/test_contracts.py
+++ b/psutil/tests/test_contracts.py
@@ -135,7 +135,7 @@ class TestAvailSystemAPIs(PsutilTestCase):
def test_cpu_freq(self):
self.assertEqual(hasattr(psutil, "cpu_freq"),
- LINUX or MACOS or WINDOWS or FREEBSD)
+ LINUX or MACOS or WINDOWS or FREEBSD or OPENBSD)
def test_sensors_temperatures(self):
self.assertEqual(