ohmu
·
2026-10-15
1#include "diskio.h"
2#include "common/debug.h"
3#include "common/strutil.h"
4#include "common/mallocHelper.h"
5
6#include <errno.h>
7#include <sys/disk.h>
8#include <sys/sysctl.h>
9
10const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
11 int mib[] = { CTL_HW, HW_DISKSTATS };
12 size_t len;
13 if (sysctl(mib, ARRAY_SIZE(mib), nullptr, &len, nullptr, 0) < 0) {
14 FF_DEBUG("sysctl({HW_DISKSTATS}, nullptr) failed: %s", strerror(errno));
15 return "sysctl({HW_DISKSTATS}, nullptr) failed";
16 }
17 uint32_t nDrive = (uint32_t) (len / sizeof(struct diskstats));
18
19 FF_AUTO_FREE struct diskstats* stats = malloc(len);
20
21 if (sysctl(mib, ARRAY_SIZE(mib), stats, &len, nullptr, 0) < 0) {
22 FF_DEBUG("sysctl({HW_DISKSTATS}, stats) failed: %s", strerror(errno));
23 return "sysctl({HW_DISKSTATS}, stats) failed";
24 }
25
26 for (uint32_t i = 0; i < nDrive; ++i) {
27 struct diskstats* st = &stats[i];
28
29 if (options->namePrefix.length && strncmp(st->ds_name, options->namePrefix.chars, options->namePrefix.length) != 0) {
30 continue;
31 }
32
33 FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
34 ffStrbufInitF(&device->devPath, "/dev/%s", st->ds_name);
35 ffStrbufInitS(&device->name, st->ds_name);
36 device->bytesRead = st->ds_rbytes;
37 device->readCount = st->ds_rxfer;
38 device->bytesWritten = st->ds_wbytes;
39 device->writeCount = st->ds_wxfer;
40 }
41
42 return nullptr;
43}