Debian Codegraph @

psutil/arch/osx/mem.c

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