| 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 |
/* |
| 9 |
Memory related functions. Original code was refactored and moved from |
| 10 |
psutil/arch/netbsd/specific.c in 2023 (and was moved in there previously |
| 11 |
already) from cset 84219ad. For reference, here's the git history with |
| 12 |
original(ish) implementations: |
| 13 |
- virtual memory: 0749a69c01b374ca3e2180aaafc3c95e3b2d91b9 (Oct 2016) |
| 14 |
- swap memory: 312442ad2a5b5d0c608476c5ab3e267735c3bc59 (Jan 2016) |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <Python.h> |
| 18 |
#include <sys/swap.h> |
| 19 |
#include <sys/sysctl.h> |
| 20 |
#include <uvm/uvm_extern.h> |
| 21 |
|
| 22 |
#include "../../arch/all/init.h" |
| 23 |
|
| 24 |
|
| 25 |
// Virtual memory stats, taken from: |
| 26 |
// https://github.com/satterly/zabbix-stats/blob/master/src/libs/zbxsysinfo/ |
| 27 |
// netbsd/memory.c |
| 28 |
PyObject * |
| 29 |
psutil_virtual_mem(PyObject *self, PyObject *args) { |
| 30 |
size_t size; |
| 31 |
struct uvmexp_sysctl uv; |
| 32 |
int mib[] = {CTL_VM, VM_UVMEXP2}; |
| 33 |
long long cached; |
| 34 |
|
| 35 |
if (psutil_sysctl(mib, 2, &uv, sizeof(uv)) != 0) |
| 36 |
return NULL; |
| 37 |
|
| 38 |
// Note: zabbix does not include anonpages, but that doesn't match the |
| 39 |
// "Cached" value in /proc/meminfo. |
| 40 |
// https://github.com/zabbix/zabbix/blob/af5e0f8/src/libs/zbxsysinfo/netbsd/memory.c#L182 |
| 41 |
cached = (uv.filepages + uv.execpages + uv.anonpages) << uv.pageshift; |
| 42 |
return Py_BuildValue( |
| 43 |
"LLLLLL", |
| 44 |
(long long) uv.npages << uv.pageshift, // total |
| 45 |
(long long) uv.free << uv.pageshift, // free |
| 46 |
(long long) uv.active << uv.pageshift, // active |
| 47 |
(long long) uv.inactive << uv.pageshift, // inactive |
| 48 |
(long long) uv.wired << uv.pageshift, // wired |
| 49 |
cached // cached |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
PyObject * |
| 55 |
psutil_swap_mem(PyObject *self, PyObject *args) { |
| 56 |
uint64_t swap_total, swap_free; |
| 57 |
struct swapent *swdev; |
| 58 |
int nswap, i; |
| 59 |
long pagesize = psutil_getpagesize(); |
| 60 |
|
| 61 |
nswap = swapctl(SWAP_NSWAP, 0, 0); |
| 62 |
if (nswap == 0) { |
| 63 |
// This means there's no swap partition. |
| 64 |
return Py_BuildValue("(iiiii)", 0, 0, 0, 0, 0); |
| 65 |
} |
| 66 |
|
| 67 |
swdev = calloc(nswap, sizeof(*swdev)); |
| 68 |
if (swdev == NULL) { |
| 69 |
PyErr_SetFromErrno(PyExc_OSError); |
| 70 |
return NULL; |
| 71 |
} |
| 72 |
|
| 73 |
if (swapctl(SWAP_STATS, swdev, nswap) == -1) { |
| 74 |
PyErr_SetFromErrno(PyExc_OSError); |
| 75 |
goto error; |
| 76 |
} |
| 77 |
|
| 78 |
// Total things up. |
| 79 |
swap_total = swap_free = 0; |
| 80 |
for (i = 0; i < nswap; i++) { |
| 81 |
if (swdev[i].se_flags & SWF_ENABLE) { |
| 82 |
swap_total += (uint64_t)swdev[i].se_nblks * DEV_BSIZE; |
| 83 |
swap_free += (uint64_t)(swdev[i].se_nblks - swdev[i].se_inuse) * DEV_BSIZE; |
| 84 |
} |
| 85 |
} |
| 86 |
free(swdev); |
| 87 |
|
| 88 |
// Get swap in/out |
| 89 |
unsigned int total; |
| 90 |
size_t size = sizeof(total); |
| 91 |
struct uvmexp_sysctl uv; |
| 92 |
int mib[] = {CTL_VM, VM_UVMEXP2}; |
| 93 |
if (psutil_sysctl(mib, 2, &uv, sizeof(uv)) != 0) |
| 94 |
goto error; |
| 95 |
|
| 96 |
return Py_BuildValue( |
| 97 |
"(LLLll)", |
| 98 |
swap_total, |
| 99 |
(swap_total - swap_free), |
| 100 |
swap_free, |
| 101 |
(long) uv.pgswapin * pagesize, // swap in |
| 102 |
(long) uv.pgswapout * pagesize // swap out |
| 103 |
); |
| 104 |
|
| 105 |
error: |
| 106 |
free(swdev); |
| 107 |
return NULL; |
| 108 |
} |