mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 03:00:30 +01:00
45 lines
1.3 KiB
C
45 lines
1.3 KiB
C
#include "diskio.h"
|
|
#include "common/debug.h"
|
|
#include "common/strutil.h"
|
|
#include <kstat.h>
|
|
|
|
static inline void kstatFreeWrap(kstat_ctl_t** pkc) {
|
|
assert(pkc);
|
|
if (*pkc) {
|
|
kstat_close(*pkc);
|
|
}
|
|
}
|
|
|
|
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
|
[[gnu::cleanup(kstatFreeWrap)]] kstat_ctl_t* kc = kstat_open();
|
|
if (!kc) {
|
|
FF_DEBUG("kstat_open() failed");
|
|
return "kstat_open() failed";
|
|
}
|
|
|
|
for (kstat_t* ks = kc->kc_chain; ks; ks = ks->ks_next) {
|
|
if (ks->ks_type != KSTAT_TYPE_IO || !ffStrEquals(ks->ks_class, "disk")) {
|
|
continue;
|
|
}
|
|
|
|
if (options->namePrefix.length && strncmp(ks->ks_name, options->namePrefix.chars, options->namePrefix.length) != 0) {
|
|
continue;
|
|
}
|
|
|
|
kstat_io_t kio;
|
|
if (kstat_read(kc, ks, &kio) < 0) {
|
|
continue;
|
|
}
|
|
|
|
FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
|
|
ffStrbufInit(&device->devPath); // unlike other platforms, `/dev/ks_name` is not available
|
|
ffStrbufInitS(&device->name, ks->ks_name);
|
|
device->bytesRead = kio.nread;
|
|
device->readCount = kio.reads;
|
|
device->bytesWritten = kio.nwritten;
|
|
device->writeCount = kio.writes;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|