| 1 |
/* |
| 2 |
* Copyright (c) 2009, 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 |
#include <Python.h> |
| 8 |
|
| 9 |
#include <kstat.h> |
| 10 |
#include <string.h> |
| 11 |
#include <sys/sysinfo.h> |
| 12 |
|
| 13 |
#include "mem.h" |
| 14 |
|
| 15 |
|
| 16 |
PyObject * |
| 17 |
psutil_swap_mem(PyObject *self, PyObject *args) { |
| 18 |
// XXX (arghhh!) |
| 19 |
// total/free swap mem: commented out as for some reason I can't |
| 20 |
// manage to get the same results shown by "swap -l", despite the |
| 21 |
// code below is exactly the same as: |
| 22 |
// http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/ |
| 23 |
// cmd/swap/swap.c |
| 24 |
// We're going to parse "swap -l" output from Python (sigh!) |
| 25 |
|
| 26 |
/* |
| 27 |
struct swaptable *st; |
| 28 |
struct swapent *swapent; |
| 29 |
int i; |
| 30 |
struct stat64 statbuf; |
| 31 |
char *path; |
| 32 |
char fullpath[MAXPATHLEN+1]; |
| 33 |
int num; |
| 34 |
|
| 35 |
if ((num = swapctl(SC_GETNSWP, NULL)) == -1) { |
| 36 |
PyErr_SetFromErrno(PyExc_OSError); |
| 37 |
return NULL; |
| 38 |
} |
| 39 |
if (num == 0) { |
| 40 |
PyErr_SetString(PyExc_RuntimeError, "no swap devices configured"); |
| 41 |
return NULL; |
| 42 |
} |
| 43 |
if ((st = malloc(num * sizeof(swapent_t) + sizeof (int))) == NULL) { |
| 44 |
PyErr_SetString(PyExc_RuntimeError, "malloc failed"); |
| 45 |
return NULL; |
| 46 |
} |
| 47 |
if ((path = malloc(num * MAXPATHLEN)) == NULL) { |
| 48 |
PyErr_SetString(PyExc_RuntimeError, "malloc failed"); |
| 49 |
return NULL; |
| 50 |
} |
| 51 |
swapent = st->swt_ent; |
| 52 |
for (i = 0; i < num; i++, swapent++) { |
| 53 |
swapent->ste_path = path; |
| 54 |
path += MAXPATHLEN; |
| 55 |
} |
| 56 |
st->swt_n = num; |
| 57 |
if ((num = swapctl(SC_LIST, st)) == -1) { |
| 58 |
PyErr_SetFromErrno(PyExc_OSError); |
| 59 |
return NULL; |
| 60 |
} |
| 61 |
|
| 62 |
swapent = st->swt_ent; |
| 63 |
long t = 0, f = 0; |
| 64 |
for (i = 0; i < num; i++, swapent++) { |
| 65 |
int diskblks_per_page =(int)(sysconf(_SC_PAGESIZE) >> DEV_BSHIFT); |
| 66 |
t += (long)swapent->ste_pages; |
| 67 |
f += (long)swapent->ste_free; |
| 68 |
} |
| 69 |
|
| 70 |
free(st); |
| 71 |
return Py_BuildValue("(kk)", t, f); |
| 72 |
*/ |
| 73 |
|
| 74 |
kstat_ctl_t *kc; |
| 75 |
kstat_t *k; |
| 76 |
cpu_stat_t *cpu; |
| 77 |
int cpu_count = 0; |
| 78 |
int flag = 0; |
| 79 |
uint_t sin = 0; |
| 80 |
uint_t sout = 0; |
| 81 |
|
| 82 |
kc = kstat_open(); |
| 83 |
if (kc == NULL) |
| 84 |
return PyErr_SetFromErrno(PyExc_OSError);; |
| 85 |
|
| 86 |
k = kc->kc_chain; |
| 87 |
while (k != NULL) { |
| 88 |
if ((strncmp(k->ks_name, "cpu_stat", 8) == 0) && \ |
| 89 |
(kstat_read(kc, k, NULL) != -1) ) |
| 90 |
{ |
| 91 |
flag = 1; |
| 92 |
cpu = (cpu_stat_t *) k->ks_data; |
| 93 |
sin += cpu->cpu_vminfo.pgswapin; // num pages swapped in |
| 94 |
sout += cpu->cpu_vminfo.pgswapout; // num pages swapped out |
| 95 |
} |
| 96 |
cpu_count += 1; |
| 97 |
k = k->ks_next; |
| 98 |
} |
| 99 |
kstat_close(kc); |
| 100 |
if (!flag) { |
| 101 |
PyErr_SetString(PyExc_RuntimeError, "no swap device was found"); |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
return Py_BuildValue("(II)", sin, sout); |
| 105 |
} |