| 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/disk.h> |
| 11 |
|
| 12 |
#include "../../arch/all/init.h" |
| 13 |
|
| 14 |
|
| 15 |
PyObject * |
| 16 |
psutil_disk_io_counters(PyObject *self, PyObject *args) { |
| 17 |
int i, dk_ndrive, mib[3]; |
| 18 |
size_t len; |
| 19 |
struct diskstats *stats = NULL; |
| 20 |
|
| 21 |
PyObject *py_retdict = PyDict_New(); |
| 22 |
PyObject *py_disk_info = NULL; |
| 23 |
if (py_retdict == NULL) |
| 24 |
return NULL; |
| 25 |
|
| 26 |
mib[0] = CTL_HW; |
| 27 |
mib[1] = HW_DISKSTATS; |
| 28 |
|
| 29 |
if (psutil_sysctl_malloc(mib, 2, (char **)&stats, &len) != 0) |
| 30 |
goto error; |
| 31 |
|
| 32 |
dk_ndrive = (int)(len / sizeof(struct diskstats)); |
| 33 |
|
| 34 |
for (i = 0; i < dk_ndrive; i++) { |
| 35 |
py_disk_info = Py_BuildValue( |
| 36 |
"(KKKK)", |
| 37 |
stats[i].ds_rxfer, // num reads |
| 38 |
stats[i].ds_wxfer, // num writes |
| 39 |
stats[i].ds_rbytes, // read bytes |
| 40 |
stats[i].ds_wbytes // write bytes |
| 41 |
); |
| 42 |
if (!py_disk_info) |
| 43 |
goto error; |
| 44 |
if (PyDict_SetItemString(py_retdict, stats[i].ds_name, py_disk_info)) |
| 45 |
goto error; |
| 46 |
Py_DECREF(py_disk_info); |
| 47 |
} |
| 48 |
|
| 49 |
free(stats); |
| 50 |
return py_retdict; |
| 51 |
|
| 52 |
error: |
| 53 |
Py_XDECREF(py_disk_info); |
| 54 |
Py_DECREF(py_retdict); |
| 55 |
if (stats != NULL) |
| 56 |
free(stats); |
| 57 |
return NULL; |
| 58 |
} |