mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-09 06:30:10 +01:00
Chore: run clang-format
```bash find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.hpp" \) ! -path "src/3rdparty/*" -print0 | xargs -0 clang-format -i ```
This commit is contained in:
@@ -7,65 +7,68 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options);
|
||||
static FFlist ioCounters1;
|
||||
static uint64_t time1;
|
||||
|
||||
void ffPrepareDiskIO(FFDiskIOOptions* options)
|
||||
{
|
||||
if (options->detectTotal) return;
|
||||
void ffPrepareDiskIO(FFDiskIOOptions* options) {
|
||||
if (options->detectTotal) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (time1 != 0) return; // Already prepared
|
||||
if (time1 != 0) {
|
||||
return; // Already prepared
|
||||
}
|
||||
|
||||
ffListInit(&ioCounters1, sizeof(FFDiskIOResult));
|
||||
ffDiskIOGetIoCounters(&ioCounters1, options);
|
||||
time1 = ffTimeGetNow();
|
||||
}
|
||||
|
||||
const char* ffDetectDiskIO(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
const char* ffDetectDiskIO(FFlist* result, FFDiskIOOptions* options) {
|
||||
const char* error = NULL;
|
||||
|
||||
if (options->detectTotal)
|
||||
{
|
||||
if (options->detectTotal) {
|
||||
error = ffDiskIOGetIoCounters(result, options);
|
||||
if (error)
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (time1 == 0)
|
||||
{
|
||||
if (time1 == 0) {
|
||||
ffListInit(&ioCounters1, sizeof(FFDiskIOResult));
|
||||
error = ffDiskIOGetIoCounters(&ioCounters1, options);
|
||||
if (error)
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
time1 = ffTimeGetNow();
|
||||
}
|
||||
|
||||
if (ioCounters1.length == 0)
|
||||
if (ioCounters1.length == 0) {
|
||||
return "No physical disk found";
|
||||
}
|
||||
|
||||
uint64_t time2 = ffTimeGetNow();
|
||||
while (time2 - time1 < options->waitTime)
|
||||
{
|
||||
while (time2 - time1 < options->waitTime) {
|
||||
ffTimeSleep((uint32_t) (options->waitTime - (time2 - time1)));
|
||||
time2 = ffTimeGetNow();
|
||||
}
|
||||
|
||||
error = ffDiskIOGetIoCounters(result, options);
|
||||
if (error)
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (result->length != ioCounters1.length)
|
||||
if (result->length != ioCounters1.length) {
|
||||
return "Different number of physical disks. Hardware change?";
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < result->length; ++i)
|
||||
{
|
||||
for (uint32_t i = 0; i < result->length; ++i) {
|
||||
FFDiskIOResult* icPrev = FF_LIST_GET(FFDiskIOResult, ioCounters1, i);
|
||||
FFDiskIOResult* icCurr = FF_LIST_GET(FFDiskIOResult, *result, i);
|
||||
if (!ffStrbufEqual(&icPrev->devPath, &icCurr->devPath))
|
||||
if (!ffStrbufEqual(&icPrev->devPath, &icCurr->devPath)) {
|
||||
return "Physical disk device path changed";
|
||||
}
|
||||
|
||||
static_assert(sizeof(FFDiskIOResult) - offsetof(FFDiskIOResult, bytesRead) == sizeof(uint64_t) * 4, "Unexpected struct FFDiskIOResult layout");
|
||||
for (size_t off = offsetof(FFDiskIOResult, bytesRead); off < sizeof(FFDiskIOResult); off += sizeof(uint64_t))
|
||||
{
|
||||
for (size_t off = offsetof(FFDiskIOResult, bytesRead); off < sizeof(FFDiskIOResult); off += sizeof(uint64_t)) {
|
||||
uint64_t* prevValue = (uint64_t*) ((uint8_t*) icPrev + off);
|
||||
uint64_t* currValue = (uint64_t*) ((uint8_t*) icCurr + off);
|
||||
uint64_t temp = *currValue;
|
||||
|
||||
@@ -3,8 +3,7 @@
|
||||
#include "fastfetch.h"
|
||||
#include "modules/diskio/option.h"
|
||||
|
||||
typedef struct FFDiskIOResult
|
||||
{
|
||||
typedef struct FFDiskIOResult {
|
||||
FFstrbuf name;
|
||||
FFstrbuf devPath;
|
||||
uint64_t bytesRead;
|
||||
|
||||
@@ -8,34 +8,38 @@
|
||||
#include <IOKit/storage/IOStorageDeviceCharacteristics.h>
|
||||
#include <IOKit/storage/IOStorageProtocolCharacteristics.h>
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
||||
FF_IOOBJECT_AUTO_RELEASE io_iterator_t iterator = 0;
|
||||
if (IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching(kIOMediaClass), &iterator) != KERN_SUCCESS)
|
||||
if (IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching(kIOMediaClass), &iterator) != KERN_SUCCESS) {
|
||||
return "IOServiceGetMatchingServices() failed";
|
||||
}
|
||||
|
||||
io_registry_entry_t registryEntry;
|
||||
while ((registryEntry = IOIteratorNext(iterator)) != IO_OBJECT_NULL)
|
||||
{
|
||||
while ((registryEntry = IOIteratorNext(iterator)) != IO_OBJECT_NULL) {
|
||||
FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t entryPartition = registryEntry;
|
||||
|
||||
io_name_t deviceName;
|
||||
if (IORegistryEntryGetName(registryEntry, deviceName) != KERN_SUCCESS)
|
||||
if (IORegistryEntryGetName(registryEntry, deviceName) != KERN_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (options->namePrefix.length && strncmp(deviceName, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
if (options->namePrefix.length && strncmp(deviceName, options->namePrefix.chars, options->namePrefix.length) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t entryDriver = 0;
|
||||
if (IORegistryEntryGetParentEntry(entryPartition, kIOServicePlane, &entryDriver) != KERN_SUCCESS)
|
||||
if (IORegistryEntryGetParentEntry(entryPartition, kIOServicePlane, &entryDriver) != KERN_SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!IOObjectConformsTo(entryDriver, kIOBlockStorageDriverClass)) // physical disk only
|
||||
if (!IOObjectConformsTo(entryDriver, kIOBlockStorageDriverClass)) { // physical disk only
|
||||
continue;
|
||||
}
|
||||
|
||||
FF_CFTYPE_AUTO_RELEASE CFDictionaryRef statistics = IORegistryEntryCreateCFProperty(entryDriver, CFSTR(kIOBlockStorageDriverStatisticsKey), kCFAllocatorDefault, kNilOptions);
|
||||
if (!statistics)
|
||||
if (!statistics) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
ffStrbufInitS(&device->name, deviceName);
|
||||
@@ -47,8 +51,7 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsWritesKey), (int64_t*) &device->writeCount);
|
||||
|
||||
FF_CFTYPE_AUTO_RELEASE CFStringRef bsdName = IORegistryEntryCreateCFProperty(entryPartition, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, kNilOptions);
|
||||
if (bsdName)
|
||||
{
|
||||
if (bsdName) {
|
||||
ffCfStrGetString(bsdName, &device->devPath);
|
||||
ffStrbufPrependS(&device->devPath, "/dev/");
|
||||
}
|
||||
|
||||
@@ -2,54 +2,60 @@
|
||||
|
||||
#if __has_include(<libgeom.h>)
|
||||
|
||||
#include "common/stringUtils.h"
|
||||
# include "common/stringUtils.h"
|
||||
|
||||
#include <devstat.h>
|
||||
#include <memory.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/disk.h>
|
||||
#include <libgeom.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)
|
||||
{
|
||||
__attribute__((__cleanup__(geom_deletetree)))
|
||||
struct gmesh geomTree = {};
|
||||
if (geom_gettree(&geomTree) < 0)
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
||||
__attribute__((__cleanup__(geom_deletetree))) struct gmesh geomTree = {};
|
||||
if (geom_gettree(&geomTree) < 0) {
|
||||
return "geom_gettree() failed";
|
||||
}
|
||||
|
||||
if (geom_stats_open() < 0)
|
||||
if (geom_stats_open() < 0) {
|
||||
return "geom_stats_open() failed";
|
||||
}
|
||||
|
||||
void* snap = geom_stats_snapshot_get();
|
||||
if (!snap)
|
||||
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)
|
||||
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)
|
||||
if (geomId == NULL) {
|
||||
continue;
|
||||
if (geomId->lg_what != ISPROVIDER)
|
||||
}
|
||||
if (geomId->lg_what != ISPROVIDER) {
|
||||
continue;
|
||||
}
|
||||
struct gprovider* provider = (struct gprovider*) geomId->lg_ptr;
|
||||
if (provider->lg_geom->lg_rank != 1)
|
||||
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"))
|
||||
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)
|
||||
if (name.length == 0) {
|
||||
ffStrbufSetS(&name, provider->lg_name);
|
||||
}
|
||||
|
||||
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix))
|
||||
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
ffStrbufInitF(&device->devPath, "/dev/%s", provider->lg_name);
|
||||
@@ -68,31 +74,33 @@ const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
|
||||
#else
|
||||
|
||||
#include <devstat.h>
|
||||
#include <memory.h>
|
||||
# include <devstat.h>
|
||||
# include <memory.h>
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
if (checkversion() < 0)
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
||||
if (checkversion() < 0) {
|
||||
return "checkversion() failed";
|
||||
}
|
||||
|
||||
struct statinfo stats = {
|
||||
.dinfo = (struct devinfo *)calloc(1, sizeof(struct devinfo)),
|
||||
.dinfo = (struct devinfo*) calloc(1, sizeof(struct devinfo)),
|
||||
};
|
||||
if (getdevs(&stats) < 0)
|
||||
if (getdevs(&stats) < 0) {
|
||||
return "getdevs() failed";
|
||||
}
|
||||
|
||||
for (int i = 0; i < stats.dinfo->numdevs; i++)
|
||||
{
|
||||
for (int i = 0; i < stats.dinfo->numdevs; i++) {
|
||||
struct devstat* current = &stats.dinfo->devices[i];
|
||||
if (current->device_type & DEVSTAT_TYPE_PASS)
|
||||
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)
|
||||
if (options->namePrefix.length && strncmp(deviceName, options->namePrefix.chars, options->namePrefix.length) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
ffStrbufInitS(&device->name, deviceName);
|
||||
|
||||
@@ -8,48 +8,46 @@
|
||||
#include <inttypes.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* result, FFDiskIOOptions* options) {
|
||||
FF_AUTO_CLOSE_FD int devfd = openat(dfd, "device", O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
|
||||
if (devfd < 0) return "virtual device";
|
||||
if (devfd < 0) {
|
||||
return "virtual device";
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
|
||||
|
||||
{
|
||||
if (ffAppendFileBufferRelative(devfd, "vendor", &name))
|
||||
{
|
||||
if (ffAppendFileBufferRelative(devfd, "vendor", &name)) {
|
||||
ffStrbufTrimRightSpace(&name);
|
||||
if (name.length > 0)
|
||||
if (name.length > 0) {
|
||||
ffStrbufAppendC(&name, ' ');
|
||||
}
|
||||
}
|
||||
|
||||
ffAppendFileBufferRelative(devfd, "model", &name);
|
||||
ffStrbufTrimRightSpace(&name);
|
||||
|
||||
if (name.length == 0)
|
||||
if (name.length == 0) {
|
||||
ffStrbufSetS(&name, devName);
|
||||
else if (ffStrStartsWith(devName, "nvme"))
|
||||
{
|
||||
} else if (ffStrStartsWith(devName, "nvme")) {
|
||||
int devid, nsid;
|
||||
if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2)
|
||||
{
|
||||
if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2) {
|
||||
bool multiNs = nsid > 1;
|
||||
if (!multiNs)
|
||||
{
|
||||
if (!multiNs) {
|
||||
char pathSysBlock[16];
|
||||
snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "nvme%dn2", devid);
|
||||
multiNs = faccessat(devfd, pathSysBlock, F_OK, 0) == 0;
|
||||
}
|
||||
if (multiNs)
|
||||
{
|
||||
if (multiNs) {
|
||||
// In Asahi Linux, there are multiple namespaces for the same NVMe drive.
|
||||
ffStrbufAppendF(&name, " - %d", nsid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix))
|
||||
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
|
||||
return "ignored";
|
||||
}
|
||||
}
|
||||
|
||||
// I/Os merges sectors ticks ...
|
||||
@@ -57,10 +55,13 @@ static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* res
|
||||
{
|
||||
char sysBlockStat[PROC_FILE_BUFFSIZ];
|
||||
ssize_t fileSize = ffReadFileDataRelative(dfd, "stat", ARRAY_SIZE(sysBlockStat) - 1, sysBlockStat);
|
||||
if (fileSize <= 0) return "failed to read stat file";
|
||||
if (fileSize <= 0) {
|
||||
return "failed to read stat file";
|
||||
}
|
||||
sysBlockStat[fileSize] = '\0';
|
||||
if (sscanf(sysBlockStat, "%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u", &nRead, §orRead, &nWritten, §orWritten) <= 0)
|
||||
if (sscanf(sysBlockStat, "%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u", &nRead, §orRead, &nWritten, §orWritten) <= 0) {
|
||||
return "invalid stat file format";
|
||||
}
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
@@ -74,21 +75,24 @@ static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* res
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
||||
FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
|
||||
if(sysBlockDirp == NULL)
|
||||
if (sysBlockDirp == NULL) {
|
||||
return "opendir(\"/sys/block/\") == NULL";
|
||||
}
|
||||
|
||||
struct dirent* sysBlockEntry;
|
||||
while ((sysBlockEntry = readdir(sysBlockDirp)) != NULL)
|
||||
{
|
||||
while ((sysBlockEntry = readdir(sysBlockDirp)) != NULL) {
|
||||
const char* const devName = sysBlockEntry->d_name;
|
||||
|
||||
if (devName[0] == '.') continue;
|
||||
if (devName[0] == '.') {
|
||||
continue;
|
||||
}
|
||||
|
||||
FF_AUTO_CLOSE_FD int dfd = openat(dirfd(sysBlockDirp), devName, O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
|
||||
if (dfd > 0) parseDiskIOCounters(dfd, devName, result, options);
|
||||
if (dfd > 0) {
|
||||
parseDiskIOCounters(dfd, devName, result, options);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
@@ -5,25 +5,26 @@
|
||||
#include <sys/iostat.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
||||
int mib[] = {CTL_HW, HW_IOSTATS, sizeof(struct io_sysctl)};
|
||||
size_t len;
|
||||
if (sysctl(mib, ARRAY_SIZE(mib), NULL, &len, NULL, 0) < 0)
|
||||
if (sysctl(mib, ARRAY_SIZE(mib), NULL, &len, NULL, 0) < 0) {
|
||||
return "sysctl({HW_IOSTATS}, NULL) failed";
|
||||
}
|
||||
uint32_t nDrive = (uint32_t) (len / sizeof(struct io_sysctl));
|
||||
|
||||
FF_AUTO_FREE struct io_sysctl* stats = malloc(len);
|
||||
|
||||
if (sysctl(mib, ARRAY_SIZE(mib), stats, &len, NULL, 0) < 0)
|
||||
if (sysctl(mib, ARRAY_SIZE(mib), stats, &len, NULL, 0) < 0) {
|
||||
return "sysctl({HW_IOSTATS}, stats) failed";
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < nDrive; ++i)
|
||||
{
|
||||
for (uint32_t i = 0; i < nDrive; ++i) {
|
||||
struct io_sysctl* st = &stats[i];
|
||||
|
||||
if (options->namePrefix.length && strncmp(st->name, options->namePrefix.chars, options->namePrefix.length) != 0)
|
||||
if (options->namePrefix.length && strncmp(st->name, options->namePrefix.chars, options->namePrefix.length) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
ffStrbufInitF(&device->devPath, "/dev/%s", st->name);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "diskio.h"
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FF_MAYBE_UNUSED FFlist* result, FF_MAYBE_UNUSED FFDiskIOOptions* options)
|
||||
{
|
||||
const char* ffDiskIOGetIoCounters(FF_MAYBE_UNUSED FFlist* result, FF_MAYBE_UNUSED FFDiskIOOptions* options) {
|
||||
return "Not supported on this platform";
|
||||
}
|
||||
|
||||
@@ -5,25 +5,26 @@
|
||||
#include <sys/disk.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
{
|
||||
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)
|
||||
if (options->namePrefix.length && strncmp(st->ds_name, options->namePrefix.chars, options->namePrefix.length) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
ffStrbufInitF(&device->devPath, "/dev/%s", st->ds_name);
|
||||
|
||||
@@ -2,29 +2,32 @@
|
||||
#include "common/stringUtils.h"
|
||||
#include <kstat.h>
|
||||
|
||||
static inline void kstatFreeWrap(kstat_ctl_t** pkc)
|
||||
{
|
||||
static inline void kstatFreeWrap(kstat_ctl_t** pkc) {
|
||||
assert(pkc);
|
||||
if (*pkc)
|
||||
if (*pkc) {
|
||||
kstat_close(*pkc);
|
||||
}
|
||||
}
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
||||
__attribute__((__cleanup__(kstatFreeWrap))) kstat_ctl_t* kc = kstat_open();
|
||||
if (!kc)
|
||||
if (!kc) {
|
||||
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)
|
||||
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)
|
||||
if (kstat_read(kc, ks, &kio) < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
ffStrbufInit(&device->devPath); // unlike other platforms, `/dev/ks_name` is not available
|
||||
|
||||
@@ -5,67 +5,64 @@
|
||||
#include <windows.h>
|
||||
#include <winioctl.h>
|
||||
|
||||
static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFDiskIOOptions* options) {
|
||||
FF_AUTO_CLOSE_FD HANDLE hDevice = CreateFileW(szDevice, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if (hDevice == INVALID_HANDLE_VALUE)
|
||||
if (hDevice == INVALID_HANDLE_VALUE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD retSize;
|
||||
char sddBuffer[4096];
|
||||
if(!DeviceIoControl(
|
||||
hDevice,
|
||||
IOCTL_STORAGE_QUERY_PROPERTY,
|
||||
&(STORAGE_PROPERTY_QUERY) {
|
||||
.PropertyId = StorageDeviceProperty,
|
||||
.QueryType = PropertyStandardQuery,
|
||||
},
|
||||
sizeof(STORAGE_PROPERTY_QUERY),
|
||||
&sddBuffer,
|
||||
ARRAY_SIZE(sddBuffer),
|
||||
&retSize,
|
||||
NULL
|
||||
) || retSize == 0)
|
||||
if (!DeviceIoControl(
|
||||
hDevice,
|
||||
IOCTL_STORAGE_QUERY_PROPERTY,
|
||||
&(STORAGE_PROPERTY_QUERY) {
|
||||
.PropertyId = StorageDeviceProperty,
|
||||
.QueryType = PropertyStandardQuery,
|
||||
},
|
||||
sizeof(STORAGE_PROPERTY_QUERY),
|
||||
&sddBuffer,
|
||||
ARRAY_SIZE(sddBuffer),
|
||||
&retSize,
|
||||
NULL) ||
|
||||
retSize == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
|
||||
STORAGE_DEVICE_DESCRIPTOR* sdd = (STORAGE_DEVICE_DESCRIPTOR*) sddBuffer;
|
||||
|
||||
ffStrbufInit(&device->name);
|
||||
if (sdd->VendorIdOffset != 0)
|
||||
{
|
||||
if (sdd->VendorIdOffset != 0) {
|
||||
ffStrbufSetS(&device->name, (const char*) sddBuffer + sdd->VendorIdOffset);
|
||||
ffStrbufTrim(&device->name, ' ');
|
||||
}
|
||||
if (sdd->ProductIdOffset != 0)
|
||||
{
|
||||
if (device->name.length)
|
||||
if (sdd->ProductIdOffset != 0) {
|
||||
if (device->name.length) {
|
||||
ffStrbufAppendC(&device->name, ' ');
|
||||
}
|
||||
|
||||
ffStrbufAppendS(&device->name, (const char*) sddBuffer + sdd->ProductIdOffset);
|
||||
ffStrbufTrimRight(&device->name, ' ');
|
||||
}
|
||||
|
||||
if (!device->name.length)
|
||||
if (!device->name.length) {
|
||||
ffStrbufSetWS(&device->name, szDevice);
|
||||
}
|
||||
|
||||
if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix))
|
||||
{
|
||||
if (options->namePrefix.length && !ffStrbufStartsWith(&device->name, &options->namePrefix)) {
|
||||
ffStrbufDestroy(&device->name);
|
||||
result->length--;
|
||||
return true;
|
||||
}
|
||||
|
||||
DISK_PERFORMANCE dp = {};
|
||||
if (DeviceIoControl(hDevice, IOCTL_DISK_PERFORMANCE, NULL, 0, &dp, sizeof(dp), &retSize, NULL))
|
||||
{
|
||||
if (DeviceIoControl(hDevice, IOCTL_DISK_PERFORMANCE, NULL, 0, &dp, sizeof(dp), &retSize, NULL)) {
|
||||
device->bytesRead = (uint64_t) dp.BytesRead.QuadPart;
|
||||
device->readCount = (uint64_t) dp.ReadCount;
|
||||
device->bytesWritten = (uint64_t) dp.BytesWritten.QuadPart;
|
||||
device->writeCount = (uint64_t) dp.WriteCount;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ffStrbufDestroy(&device->name);
|
||||
result->length--;
|
||||
}
|
||||
@@ -75,29 +72,28 @@ static bool detectPhysicalDisk(const wchar_t* szDevice, FFlist* result, FFDiskIO
|
||||
return true;
|
||||
}
|
||||
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
|
||||
{
|
||||
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
|
||||
{
|
||||
wchar_t szPhysicalDrive[32] = L"\\\\.\\PhysicalDrive";
|
||||
wchar_t* pNum = szPhysicalDrive + strlen("\\\\.\\PhysicalDrive");
|
||||
for (uint32_t idev = 0; ; ++idev)
|
||||
{
|
||||
for (uint32_t idev = 0;; ++idev) {
|
||||
_ultow(idev, pNum, 10);
|
||||
|
||||
if (!detectPhysicalDisk(szPhysicalDrive, result, options))
|
||||
if (!detectPhysicalDisk(szPhysicalDrive, result, options)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
wchar_t szCdrom[32] = L"\\\\.\\CDROM";
|
||||
wchar_t* pNum = szCdrom + strlen("\\\\.\\CDROM");
|
||||
for (uint32_t idev = 0; ; ++idev)
|
||||
{
|
||||
for (uint32_t idev = 0;; ++idev) {
|
||||
_ultow(idev, pNum, 10);
|
||||
|
||||
if (!detectPhysicalDisk(szCdrom, result, options))
|
||||
if (!detectPhysicalDisk(szCdrom, result, options)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user