Debian Codegraph @

psutil/arch/openbsd/mem.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola', Landry Breuil.
3 * All rights reserved.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include <Python.h>
9 #include <sys/sysctl.h>
10 #include <sys/vmmeter.h>
11 #include <sys/mount.h>
12 #include <sys/swap.h>
13 #include <sys/param.h>
14
15 #include "../../arch/all/init.h"
16
17
18 PyObject *
19 psutil_virtual_mem(PyObject *self, PyObject *args) {
20 int64_t total_physmem;
21 int uvmexp_mib[] = {CTL_VM, VM_UVMEXP};
22 int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT};
23 int physmem_mib[] = {CTL_HW, HW_PHYSMEM64};
24 int vmmeter_mib[] = {CTL_VM, VM_METER};
25 size_t size;
26 struct uvmexp uvmexp;
27 struct bcachestats bcstats;
28 struct vmtotal vmdata;
29 long pagesize = psutil_getpagesize();
30
31 size = sizeof(total_physmem);
32 if (psutil_sysctl(physmem_mib, 2, &total_physmem, size) != 0)
33 return NULL;
34
35 size = sizeof(uvmexp);
36 if (psutil_sysctl(uvmexp_mib, 2, &uvmexp, size) != 0)
37 return NULL;
38
39 size = sizeof(bcstats);
40 if (psutil_sysctl(bcstats_mib, 3, &bcstats, size) != 0)
41 return NULL;
42
43 size = sizeof(vmdata);
44 if (psutil_sysctl(vmmeter_mib, 2, &vmdata, size) != 0)
45 return NULL;
46
47 return Py_BuildValue("KKKKKKKK",
48 // Note: many programs calculate total memory as
49 // "uvmexp.npages * pagesize" but this is incorrect and does not
50 // match "sysctl | grep hw.physmem".
51 (unsigned long long) total_physmem,
52 (unsigned long long) uvmexp.free * pagesize,
53 (unsigned long long) uvmexp.active * pagesize,
54 (unsigned long long) uvmexp.inactive * pagesize,
55 (unsigned long long) uvmexp.wired * pagesize,
56 // this is how "top" determines it
57 (unsigned long long) bcstats.numbufpages * pagesize, // cached
58 (unsigned long long) 0, // buffers
59 (unsigned long long) vmdata.t_vmshr + vmdata.t_rmshr // shared
60 );
61 }
62
63
64 PyObject *
65 psutil_swap_mem(PyObject *self, PyObject *args) {
66 uint64_t swap_total, swap_free;
67 struct swapent *swdev;
68 int nswap, i;
69
70 if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) == 0) {
71 PyErr_SetFromErrno(PyExc_OSError);
72 return NULL;
73 }
74
75 if ((swdev = calloc(nswap, sizeof(*swdev))) == NULL) {
76 PyErr_NoMemory();
77 return NULL;
78 }
79
80 if (swapctl(SWAP_STATS, swdev, nswap) == -1) {
81 PyErr_SetFromErrno(PyExc_OSError);
82 goto error;
83 }
84
85 // Total things up.
86 swap_total = swap_free = 0;
87 for (i = 0; i < nswap; i++) {
88 if (swdev[i].se_flags & SWF_ENABLE) {
89 swap_free += (swdev[i].se_nblks - swdev[i].se_inuse);
90 swap_total += swdev[i].se_nblks;
91 }
92 }
93
94 free(swdev);
95 return Py_BuildValue(
96 "(LLLII)",
97 swap_total * DEV_BSIZE,
98 (swap_total - swap_free) * DEV_BSIZE,
99 swap_free * DEV_BSIZE,
100 // swap in / swap out is not supported as the
101 // swapent struct does not provide any info
102 // about it.
103 0,
104 0
105 );
106
107 error:
108 free(swdev);
109 return NULL;
110 }