| 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 |
Disk 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 |
- disk IO counters: 312442ad2a5b5d0c608476c5ab3e267735c3bc59 (Jan 2016) |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <Python.h> |
| 17 |
#include <sys/sysctl.h> |
| 18 |
#include <sys/disk.h> |
| 19 |
|
| 20 |
#include "../../arch/all/init.h" |
| 21 |
|
| 22 |
|
| 23 |
PyObject * |
| 24 |
psutil_disk_io_counters(PyObject *self, PyObject *args) { |
| 25 |
int i, dk_ndrive; |
| 26 |
int mib[3]; |
| 27 |
struct io_sysctl *stats = NULL; |
| 28 |
size_t stats_len; |
| 29 |
PyObject *py_disk_info = NULL; |
| 30 |
PyObject *py_retdict = PyDict_New(); |
| 31 |
|
| 32 |
if (py_retdict == NULL) |
| 33 |
return NULL; |
| 34 |
|
| 35 |
mib[0] = CTL_HW; |
| 36 |
mib[1] = HW_IOSTATS; |
| 37 |
mib[2] = sizeof(struct io_sysctl); |
| 38 |
|
| 39 |
// Use helper to allocate and fill buffer |
| 40 |
if (psutil_sysctl_malloc(mib, 3, (char **)&stats, &stats_len) == -1) |
| 41 |
goto error; |
| 42 |
|
| 43 |
dk_ndrive = (int)(stats_len / sizeof(struct io_sysctl)); |
| 44 |
|
| 45 |
for (i = 0; i < dk_ndrive; i++) { |
| 46 |
py_disk_info = Py_BuildValue( |
| 47 |
"(KKKK)", |
| 48 |
stats[i].rxfer, |
| 49 |
stats[i].wxfer, |
| 50 |
stats[i].rbytes, |
| 51 |
stats[i].wbytes |
| 52 |
); |
| 53 |
if (!py_disk_info) |
| 54 |
goto error; |
| 55 |
if (PyDict_SetItemString(py_retdict, stats[i].name, py_disk_info)) |
| 56 |
goto error; |
| 57 |
Py_DECREF(py_disk_info); |
| 58 |
} |
| 59 |
|
| 60 |
free(stats); |
| 61 |
return py_retdict; |
| 62 |
|
| 63 |
error: |
| 64 |
Py_XDECREF(py_disk_info); |
| 65 |
Py_DECREF(py_retdict); |
| 66 |
if (stats != NULL) |
| 67 |
free(stats); |
| 68 |
return NULL; |
| 69 |
} |