From d966c7b1f32558a0e987e7483929ca2bf06cddd7 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sun, 16 Jan 2022 19:42:01 +0000 Subject: OpenBSD: expose CPU chipset --- psutil/_psutil_bsd.c | 2 ++ psutil/_virt.py | 21 +++++++++++++++++++-- psutil/arch/openbsd/cpu.c | 14 ++++++++++++++ psutil/arch/openbsd/cpu.h | 1 + 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 3d92375d..feefdd18 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -1150,6 +1150,8 @@ static PyMethodDef mod_methods[] = { #if defined(PSUTIL_OPENBSD) {"cpu_vendor", psutil_cpu_vendor, METH_VARARGS, "Return the CPU vendor string."}, + {"cpu_chipset", psutil_cpu_chipset, METH_VARARGS, + "Return the CPU chipset string."}, #endif // --- others {"set_debug", psutil_set_debug, METH_VARARGS, diff --git a/psutil/_virt.py b/psutil/_virt.py index f3268407..ebec5d6d 100644 --- a/psutil/_virt.py +++ b/psutil/_virt.py @@ -441,17 +441,34 @@ elif WINDOWS: elif OPENBSD: from . import _psutil_bsd as cext + def _find_dmi_or_cpuid_match(inputstr): + inputstr = inputstr.strip() + for k, v in CPUID_VENDORS_TABLE.items(): + if inputstr.startswith(k): + debug("found CPUID match") + return v + for k, v in DMI_VENDORS_TABLE.items(): + if inputstr.startswith(k): + debug("found DMI match") + return v + def ask_cpu_vendor(): # Vendor is determined via "sysctl hw.vendor". On VirtualBox # this returns "innotek GmbH", which is the same DMI value # returned on Linux. vendor = cext.cpu_vendor() - if vendor and vendor in DMI_VENDORS_TABLE: - return DMI_VENDORS_TABLE[vendor] + if vendor: + return _find_dmi_or_cpuid_match(vendor) + + def ask_cpu_chipset(): + chipset = cext.cpu_chipset() + if chipset: + return _find_dmi_or_cpuid_match(chipset) def get_functions(): return [ ask_cpu_vendor, + ask_cpu_chipset, ] # --- diff --git a/psutil/arch/openbsd/cpu.c b/psutil/arch/openbsd/cpu.c index 7f68e032..a4acf4fb 100644 --- a/psutil/arch/openbsd/cpu.c +++ b/psutil/arch/openbsd/cpu.c @@ -121,3 +121,17 @@ psutil_cpu_vendor(PyObject *self, PyObject *args) { } return Py_BuildValue("s", vendor); } + + +PyObject * +psutil_cpu_chipset(PyObject *self, PyObject *args) { + char product[128]; + size_t size = sizeof(product); + int mib[2] = {CTL_HW, HW_PRODUCT}; + + if (sysctl(mib, 2, product, &size, NULL, 0) < 0) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } + return Py_BuildValue("s", product); +} diff --git a/psutil/arch/openbsd/cpu.h b/psutil/arch/openbsd/cpu.h index 5d65e769..6b5d2696 100644 --- a/psutil/arch/openbsd/cpu.h +++ b/psutil/arch/openbsd/cpu.h @@ -7,6 +7,7 @@ #include +PyObject *psutil_cpu_chipset(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); -- cgit v1.2.1