-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
420 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include <Python.h> | ||
#include <sys/sysctl.h> | ||
#include <devstat.h> | ||
|
||
#include "../../_psutil_common.h" | ||
#include "../../_psutil_posix.h" | ||
|
||
|
||
// convert a bintime struct to milliseconds | ||
#define PSUTIL_BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * (uint32_t) \ | ||
(bt.frac >> 32) ) >> 32 ) / 1000000) | ||
|
||
|
||
PyObject * | ||
psutil_disk_io_counters(PyObject *self, PyObject *args) { | ||
int i; | ||
struct statinfo stats; | ||
|
||
PyObject *py_retdict = PyDict_New(); | ||
PyObject *py_disk_info = NULL; | ||
|
||
if (py_retdict == NULL) | ||
return NULL; | ||
if (devstat_checkversion(NULL) < 0) { | ||
PyErr_Format(PyExc_RuntimeError, | ||
"devstat_checkversion() syscall failed"); | ||
goto error; | ||
} | ||
|
||
stats.dinfo = (struct devinfo *)malloc(sizeof(struct devinfo)); | ||
if (stats.dinfo == NULL) { | ||
PyErr_NoMemory(); | ||
goto error; | ||
} | ||
bzero(stats.dinfo, sizeof(struct devinfo)); | ||
|
||
if (devstat_getdevs(NULL, &stats) == -1) { | ||
PyErr_Format(PyExc_RuntimeError, "devstat_getdevs() syscall failed"); | ||
goto error; | ||
} | ||
|
||
for (i = 0; i < stats.dinfo->numdevs; i++) { | ||
py_disk_info = NULL; | ||
struct devstat current; | ||
char disk_name[128]; | ||
current = stats.dinfo->devices[i]; | ||
snprintf(disk_name, sizeof(disk_name), "%s%d", | ||
current.device_name, | ||
current.unit_number); | ||
|
||
py_disk_info = Py_BuildValue( | ||
"(KKKKLLL)", | ||
current.operations[DEVSTAT_READ], // no reads | ||
current.operations[DEVSTAT_WRITE], // no writes | ||
current.bytes[DEVSTAT_READ], // bytes read | ||
current.bytes[DEVSTAT_WRITE], // bytes written | ||
(long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_READ]), // r time | ||
(long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_WRITE]), // w time | ||
(long long) PSUTIL_BT2MSEC(current.busy_time) // busy time | ||
); // finished transactions | ||
if (!py_disk_info) | ||
goto error; | ||
if (PyDict_SetItemString(py_retdict, disk_name, py_disk_info)) | ||
goto error; | ||
Py_DECREF(py_disk_info); | ||
} | ||
|
||
if (stats.dinfo->mem_ptr) | ||
free(stats.dinfo->mem_ptr); | ||
free(stats.dinfo); | ||
return py_retdict; | ||
|
||
error: | ||
Py_XDECREF(py_disk_info); | ||
Py_DECREF(py_retdict); | ||
if (stats.dinfo != NULL) | ||
free(stats.dinfo); | ||
return NULL; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include <Python.h> | ||
|
||
PyObject *psutil_disk_io_counters(PyObject* self, PyObject* args); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
|
||
#include <Python.h> | ||
#include <sys/sysctl.h> | ||
#include <sys/vmmeter.h> | ||
#include <vm/vm_param.h> | ||
#include <devstat.h> | ||
#include <paths.h> | ||
#include <fcntl.h> | ||
|
||
#include "../../_psutil_common.h" | ||
#include "../../_psutil_posix.h" | ||
|
||
|
||
#ifndef _PATH_DEVNULL | ||
#define _PATH_DEVNULL "/dev/null" | ||
#endif | ||
|
||
|
||
PyObject * | ||
psutil_virtual_mem(PyObject *self, PyObject *args) { | ||
unsigned long total; | ||
unsigned int active, inactive, wired, cached, free; | ||
size_t size = sizeof(total); | ||
struct vmtotal vm; | ||
int mib[] = {CTL_VM, VM_METER}; | ||
long pagesize = psutil_getpagesize(); | ||
#if __FreeBSD_version > 702101 | ||
long buffers; | ||
#else | ||
int buffers; | ||
#endif | ||
size_t buffers_size = sizeof(buffers); | ||
|
||
if (sysctlbyname("hw.physmem", &total, &size, NULL, 0)) { | ||
return PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('hw.physmem')"); | ||
} | ||
if (sysctlbyname("vm.stats.vm.v_active_count", &active, &size, NULL, 0)) { | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_active_count')"); | ||
} | ||
if (sysctlbyname("vm.stats.vm.v_inactive_count", &inactive, &size, NULL, 0)) | ||
{ | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_inactive_count')"); | ||
} | ||
if (sysctlbyname("vm.stats.vm.v_wire_count", &wired, &size, NULL, 0)) { | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_wire_count')"); | ||
} | ||
// https://github.com/giampaolo/psutil/issues/997 | ||
if (sysctlbyname("vm.stats.vm.v_cache_count", &cached, &size, NULL, 0)) { | ||
cached = 0; | ||
} | ||
if (sysctlbyname("vm.stats.vm.v_free_count", &free, &size, NULL, 0)) { | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_free_count')"); | ||
} | ||
if (sysctlbyname("vfs.bufspace", &buffers, &buffers_size, NULL, 0)) { | ||
return PyErr_SetFromOSErrnoWithSyscall("sysctlbyname('vfs.bufspace')"); | ||
} | ||
|
||
size = sizeof(vm); | ||
if (sysctl(mib, 2, &vm, &size, NULL, 0) != 0) { | ||
return PyErr_SetFromOSErrnoWithSyscall("sysctl(CTL_VM | VM_METER)"); | ||
} | ||
|
||
return Py_BuildValue("KKKKKKKK", | ||
(unsigned long long) total, | ||
(unsigned long long) free * pagesize, | ||
(unsigned long long) active * pagesize, | ||
(unsigned long long) inactive * pagesize, | ||
(unsigned long long) wired * pagesize, | ||
(unsigned long long) cached * pagesize, | ||
(unsigned long long) buffers, | ||
(unsigned long long) (vm.t_vmshr + vm.t_rmshr) * pagesize // shared | ||
); | ||
} | ||
|
||
|
||
PyObject * | ||
psutil_swap_mem(PyObject *self, PyObject *args) { | ||
// Return swap memory stats (see 'swapinfo' cmdline tool) | ||
kvm_t *kd; | ||
struct kvm_swap kvmsw[1]; | ||
unsigned int swapin, swapout, nodein, nodeout; | ||
size_t size = sizeof(unsigned int); | ||
long pagesize = psutil_getpagesize(); | ||
|
||
kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, "kvm_open failed"); | ||
if (kd == NULL) { | ||
PyErr_SetString(PyExc_RuntimeError, "kvm_open() syscall failed"); | ||
return NULL; | ||
} | ||
|
||
if (kvm_getswapinfo(kd, kvmsw, 1, 0) < 0) { | ||
kvm_close(kd); | ||
PyErr_SetString(PyExc_RuntimeError, | ||
"kvm_getswapinfo() syscall failed"); | ||
return NULL; | ||
} | ||
|
||
kvm_close(kd); | ||
|
||
if (sysctlbyname("vm.stats.vm.v_swapin", &swapin, &size, NULL, 0) == -1) { | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_swapin)'"); | ||
} | ||
if (sysctlbyname("vm.stats.vm.v_swapout", &swapout, &size, NULL, 0) == -1){ | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_swapout)'"); | ||
} | ||
if (sysctlbyname("vm.stats.vm.v_vnodein", &nodein, &size, NULL, 0) == -1) { | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_vnodein)'"); | ||
} | ||
if (sysctlbyname("vm.stats.vm.v_vnodeout", &nodeout, &size, NULL, 0) == -1) { | ||
return PyErr_SetFromOSErrnoWithSyscall( | ||
"sysctlbyname('vm.stats.vm.v_vnodeout)'"); | ||
} | ||
|
||
return Py_BuildValue( | ||
"(KKKII)", | ||
(unsigned long long)kvmsw[0].ksw_total * pagesize, // total | ||
(unsigned long long)kvmsw[0].ksw_used * pagesize, // used | ||
(unsigned long long)kvmsw[0].ksw_total * pagesize - // free | ||
kvmsw[0].ksw_used * pagesize, | ||
swapin + swapout, // swap in | ||
nodein + nodeout // swap out | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#include <Python.h> | ||
|
||
PyObject *psutil_swap_mem(PyObject* self, PyObject* args); | ||
PyObject *psutil_virtual_mem(PyObject* self, PyObject* args); |
Oops, something went wrong.