summaryrefslogtreecommitdiffstats
path: root/src/detection/diskio/diskio_bsd.c
blob: f5eb4b8b84bb01652650b9074acacbd7ddee2e19 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "diskio.h"

#if __has_include(<libgeom.h>)

    #include "common/strutil.h"

    #include <devstat.h>
    #include <memory.h>
    #include <fcntl.h>
    #include <sys/ioctl.h>
    #include <sys/disk.h>
    #include <libgeom.h>

const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
    FF_A_CLEANUP(geom_deletetree) struct gmesh geomTree = {};
    if (geom_gettree(&geomTree) < 0) {
        return "geom_gettree() failed";
    }

    if (geom_stats_open() < 0) {
        return "geom_stats_open() failed";
    }

    void* snap = geom_stats_snapshot_get();
    if (!snap) {
        return "geom_stats_snapshot_get() failed";
    }

    struct devstat* snapIter;
    while ((snapIter = geom_stats_snapshot_next(snap)) != NULL) {
        if (snapIter->device_type & DEVSTAT_TYPE_PASS) {
            continue;
        }
        struct gident* geomId = geom_lookupid(&geomTree, snapIter->id);
        if (geomId == NULL) {
            continue;
        }
        if (geomId->lg_what != ISPROVIDER) {
            continue;
        }
        struct gprovider* provider = (struct gprovider*) geomId->lg_ptr;
        if (provider->lg_geom->lg_rank != 1) {
            continue;
        }

        FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
        for (struct gconfig* ptr = provider->lg_config.lh_first; ptr; ptr = ptr->lg_config.le_next) {
            if (ffStrEquals(ptr->lg_name, "descr")) {
                ffStrbufSetS(&name, ptr->lg_val);
            }
        }
        if (name.length == 0) {
            ffStrbufSetS(&name, provider->lg_name);
        }

        if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
            continue;
        }

        FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
        ffStrbufInitF(&device->devPath, "/dev/%s", provider->lg_name);
        device->bytesRead = snapIter->bytes[DEVSTAT_READ];
        device->readCount = snapIter->operations[DEVSTAT_READ];
        device->bytesWritten = snapIter->bytes[DEVSTAT_WRITE];
        device->writeCount = snapIter->operations[DEVSTAT_WRITE];
        ffStrbufInitMove(&device->name, &name);
    }

    geom_stats_snapshot_free(snap);
    geom_stats_close();

    return NULL;
}

#else

    #include <devstat.h>
    #include <memory.h>

const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
    if (checkversion() < 0) {
        return "checkversion() failed";
    }

    struct statinfo stats = {
        .dinfo = (struct devinfo*) calloc(1, sizeof(struct devinfo)),
    };
    if (getdevs(&stats) < 0) {
        return "getdevs() failed";
    }

    for (int i = 0; i < stats.dinfo->numdevs; i++) {
        struct devstat* current = &stats.dinfo->devices[i];
        if (current->device_type & DEVSTAT_TYPE_PASS) {
            continue;
        }

        char deviceName[128];
        snprintf(deviceName, sizeof(deviceName), "%s%d", current->device_name, current->unit_number);

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

        FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
        ffStrbufInitS(&device->name, deviceName);
        ffStrbufInitF(&device->devPath, "/dev/%s", deviceName);
        device->bytesRead = current->bytes_read;
        device->readCount = current->num_reads;
        device->bytesWritten = current->bytes_written;
        device->writeCount = current->num_writes;
    }

    free(stats.dinfo->mem_ptr);
    free(stats.dinfo);

    return NULL;
}

#endif