diff options
author | Olivier Dormond <odormond@users.noreply.github.com> | 2021-11-10 23:28:27 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-10 23:28:27 +0100 |
commit | 324b297de09feb1d5982db1145db2b6a6a4609b8 (patch) | |
tree | 8e2227587e25cd220e460b14d7e65a4bad35d871 | |
parent | 8fa1746685a86b8ddda454e2e2746d535e2b6d89 (diff) | |
download | psutil-324b297de09feb1d5982db1145db2b6a6a4609b8.tar.gz |
[macOS / M1] cpu_times(): convert mach tick units to nsecs (fixes #1956) (PR #2011)
Signed-off-by: Olivier Dormond <olivier.dormond@pix4d.com>
-rw-r--r-- | psutil/_psutil_common.c | 13 | ||||
-rw-r--r-- | psutil/_psutil_common.h | 10 | ||||
-rw-r--r-- | psutil/_psutil_osx.c | 11 |
3 files changed, 32 insertions, 2 deletions
diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index 94aa997b..683db9bd 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -170,6 +170,15 @@ convert_kvm_err(const char *syscall, char *errbuf) { } #endif +// ==================================================================== +// --- macOS +// ==================================================================== + +#ifdef PSUTIL_OSX +#include <mach/mach_time.h> + +struct mach_timebase_info PSUTIL_MACH_TIMEBASE_INFO; +#endif // ==================================================================== // --- Windows @@ -405,5 +414,9 @@ psutil_setup(void) { InitializeCriticalSection(&PSUTIL_CRITICAL_SECTION); #endif +#ifdef PSUTIL_OSX + mach_timebase_info(&PSUTIL_MACH_TIMEBASE_INFO); +#endif + return 0; } diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index 6aa7da6d..591f5521 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -119,6 +119,16 @@ int psutil_setup(void); void convert_kvm_err(const char *syscall, char *errbuf); // ==================================================================== +// --- macOS +// ==================================================================== + +#ifdef PSUTIL_OSX + #include <mach/mach_time.h> + + extern struct mach_timebase_info PSUTIL_MACH_TIMEBASE_INFO; +#endif + +// ==================================================================== // --- Windows // ==================================================================== diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 8c5990ae..87ec495b 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -279,16 +279,23 @@ static PyObject * psutil_proc_pidtaskinfo_oneshot(PyObject *self, PyObject *args) { pid_t pid; struct proc_taskinfo pti; + uint64_t total_user; + uint64_t total_system; if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) return NULL; if (psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti)) <= 0) return NULL; + total_user = pti.pti_total_user * PSUTIL_MACH_TIMEBASE_INFO.numer; + total_user /= PSUTIL_MACH_TIMEBASE_INFO.denom; + total_system = pti.pti_total_system * PSUTIL_MACH_TIMEBASE_INFO.numer; + total_system /= PSUTIL_MACH_TIMEBASE_INFO.denom; + return Py_BuildValue( "(ddKKkkkk)", - (float)pti.pti_total_user / 1000000000.0, // (float) cpu user time - (float)pti.pti_total_system / 1000000000.0, // (float) cpu sys time + (float)total_user / 1000000000.0, // (float) cpu user time + (float)total_system / 1000000000.0, // (float) cpu sys time // Note about memory: determining other mem stats on macOS is a mess: // http://www.opensource.apple.com/source/top/top-67/libtop.c?txt // I just give up. |