1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*
* Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
// System memory related functions. Original code was refactored and moved
// from psutil/_psutil_osx.c in 2023. This is the GIT blame before the move:
// https://github.com/giampaolo/psutil/blame/efd7ed3/psutil/_psutil_osx.c
#include <Python.h>
#include <mach/host_info.h>
#include <sys/sysctl.h>
#include <mach/mach.h>
#include "../../_psutil_posix.h"
static int
psutil_sys_vminfo(vm_statistics_data_t *vmstat) {
kern_return_t ret;
mach_msg_type_number_t count = sizeof(*vmstat) / sizeof(integer_t);
mach_port_t mport = mach_host_self();
ret = host_statistics(mport, HOST_VM_INFO, (host_info_t)vmstat, &count);
if (ret != KERN_SUCCESS) {
PyErr_Format(
PyExc_RuntimeError,
"host_statistics(HOST_VM_INFO) syscall failed: %s",
mach_error_string(ret));
return 0;
}
mach_port_deallocate(mach_task_self(), mport);
return 1;
}
/*
* Return system virtual memory stats.
* See:
* https://opensource.apple.com/source/system_cmds/system_cmds-790/
* vm_stat.tproj/vm_stat.c.auto.html
*/
PyObject *
psutil_virtual_mem(PyObject *self, PyObject *args) {
int mib[2];
uint64_t total;
size_t len = sizeof(total);
vm_statistics_data_t vm;
long pagesize = psutil_getpagesize();
// physical mem
mib[0] = CTL_HW;
mib[1] = HW_MEMSIZE;
// This is also available as sysctlbyname("hw.memsize").
if (sysctl(mib, 2, &total, &len, NULL, 0)) {
if (errno != 0)
PyErr_SetFromErrno(PyExc_OSError);
else
PyErr_Format(
PyExc_RuntimeError, "sysctl(HW_MEMSIZE) syscall failed");
return NULL;
}
// vm
if (!psutil_sys_vminfo(&vm))
return NULL;
return Py_BuildValue(
"KKKKKK",
total,
(unsigned long long) vm.active_count * pagesize, // active
(unsigned long long) vm.inactive_count * pagesize, // inactive
(unsigned long long) vm.wire_count * pagesize, // wired
(unsigned long long) vm.free_count * pagesize, // free
(unsigned long long) vm.speculative_count * pagesize // speculative
);
}
/*
* Return stats about swap memory.
*/
PyObject *
psutil_swap_mem(PyObject *self, PyObject *args) {
int mib[2];
size_t size;
struct xsw_usage totals;
vm_statistics_data_t vmstat;
long pagesize = psutil_getpagesize();
mib[0] = CTL_VM;
mib[1] = VM_SWAPUSAGE;
size = sizeof(totals);
if (sysctl(mib, 2, &totals, &size, NULL, 0) == -1) {
if (errno != 0)
PyErr_SetFromErrno(PyExc_OSError);
else
PyErr_Format(
PyExc_RuntimeError, "sysctl(VM_SWAPUSAGE) syscall failed");
return NULL;
}
if (!psutil_sys_vminfo(&vmstat))
return NULL;
return Py_BuildValue(
"LLLKK",
totals.xsu_total,
totals.xsu_used,
totals.xsu_avail,
(unsigned long long)vmstat.pageins * pagesize,
(unsigned long long)vmstat.pageouts * pagesize);
}
|