summaryrefslogtreecommitdiffstats
path: root/src/detection/diskio/diskio_obsd.c
blob: 8dbc67ebdec2da2129f1a0e5a387c183e08b0fa6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "diskio.h"
#include "common/strutil.h"
#include "common/mallocHelper.h"

#include <sys/disk.h>
#include <sys/sysctl.h>

const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
    int mib[] = { CTL_HW, HW_DISKSTATS };
    size_t len;
    if (sysctl(mib, ARRAY_SIZE(mib), NULL, &len, NULL, 0) < 0) {
        return "sysctl({HW_DISKSTATS}, NULL) failed";
    }
    uint32_t nDrive = (uint32_t) (len / sizeof(struct diskstats));

    FF_AUTO_FREE struct diskstats* stats = malloc(len);

    if (sysctl(mib, ARRAY_SIZE(mib), stats, &len, NULL, 0) < 0) {
        return "sysctl({HW_DISKSTATS}, stats) failed";
    }

    for (uint32_t i = 0; i < nDrive; ++i) {
        struct diskstats* st = &stats[i];

        if (options->namePrefix.length && strncmp(st->ds_name, options->namePrefix.chars, options->namePrefix.length) != 0) {
            continue;
        }

        FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
        ffStrbufInitF(&device->devPath, "/dev/%s", st->ds_name);
        ffStrbufInitS(&device->name, st->ds_name);
        device->bytesRead = st->ds_rbytes;
        device->readCount = st->ds_rxfer;
        device->bytesWritten = st->ds_wbytes;
        device->writeCount = st->ds_wxfer;
    }

    return NULL;
}