Skip to content

Commit

Permalink
[macOS / M1] cpu_times(): convert mach tick units to nsecs (fixes #1956
Browse files Browse the repository at this point in the history
…) (PR #2011)

Signed-off-by: Olivier Dormond <[email protected]>
  • Loading branch information
odormond authored Nov 10, 2021
1 parent 8fa1746 commit 324b297
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
13 changes: 13 additions & 0 deletions psutil/_psutil_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions psutil/_psutil_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,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
// ====================================================================
Expand Down
11 changes: 9 additions & 2 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 324b297

Please sign in to comment.