Files
fastfetch/src/detection/diskio/diskio_linux.c
T

96 lines
3.2 KiB
C
Raw Normal View History

2023-10-07 16:21:26 +08:00
#include "diskio.h"
2026-01-06 09:48:38 +08:00
#include "common/io.h"
#include "common/properties.h"
#include "common/stringUtils.h"
2023-10-07 16:21:26 +08:00
#include <ctype.h>
#include <limits.h>
#include <inttypes.h>
2024-10-15 10:30:58 +08:00
#include <fcntl.h>
2023-10-07 16:21:26 +08:00
2025-03-26 16:42:39 +08:00
static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* result, FFDiskIOOptions* options)
2023-10-07 16:21:26 +08:00
{
2024-10-15 10:30:58 +08:00
FF_AUTO_CLOSE_FD int devfd = openat(dfd, "device", O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
2025-03-26 16:42:39 +08:00
if (devfd < 0) return "virtual device";
2024-10-15 10:30:58 +08:00
FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
2023-12-17 10:38:16 +08:00
2024-10-15 10:30:58 +08:00
{
if (ffAppendFileBufferRelative(devfd, "vendor", &name))
2023-10-07 16:21:26 +08:00
{
2024-10-15 10:30:58 +08:00
ffStrbufTrimRightSpace(&name);
if (name.length > 0)
ffStrbufAppendC(&name, ' ');
}
2025-08-25 09:26:04 +08:00
ffAppendFileBufferRelative(devfd, "model", &name);
ffStrbufTrimRightSpace(&name);
2024-10-15 10:30:58 +08:00
if (name.length == 0)
ffStrbufSetS(&name, devName);
else if (ffStrStartsWith(devName, "nvme"))
{
int devid, nsid;
if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2)
{
2024-10-15 10:30:58 +08:00
bool multiNs = nsid > 1;
if (!multiNs)
{
char pathSysBlock[16];
2024-10-18 16:15:48 +08:00
snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "nvme%dn2", devid);
multiNs = faccessat(devfd, pathSysBlock, F_OK, 0) == 0;
2024-10-15 10:30:58 +08:00
}
if (multiNs)
{
2024-10-15 10:30:58 +08:00
// In Asahi Linux, there are multiple namespaces for the same NVMe drive.
ffStrbufAppendF(&name, " - %d", nsid);
}
}
2023-10-07 16:21:26 +08:00
}
2024-10-15 10:30:58 +08:00
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix))
2025-03-26 16:42:39 +08:00
return "ignored";
2024-10-15 10:30:58 +08:00
}
// I/Os merges sectors ticks ...
uint64_t nRead, sectorRead, nWritten, sectorWritten;
{
char sysBlockStat[PROC_FILE_BUFFSIZ];
2024-10-18 16:15:48 +08:00
ssize_t fileSize = ffReadFileDataRelative(dfd, "stat", ARRAY_SIZE(sysBlockStat) - 1, sysBlockStat);
2025-03-26 16:42:39 +08:00
if (fileSize <= 0) return "failed to read stat file";
2024-10-15 10:30:58 +08:00
sysBlockStat[fileSize] = '\0';
if (sscanf(sysBlockStat, "%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u", &nRead, &sectorRead, &nWritten, &sectorWritten) <= 0)
2025-03-26 16:42:39 +08:00
return "invalid stat file format";
2024-10-15 10:30:58 +08:00
}
FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result);
ffStrbufInitMove(&device->name, &name);
ffStrbufInitF(&device->devPath, "/dev/%s", devName);
device->bytesRead = sectorRead * 512;
device->bytesWritten = sectorWritten * 512;
device->readCount = nRead;
device->writeCount = nWritten;
2025-03-26 16:42:39 +08:00
return NULL;
2024-10-15 10:30:58 +08:00
}
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options)
{
FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
if(sysBlockDirp == NULL)
return "opendir(\"/sys/block/\") == NULL";
struct dirent* sysBlockEntry;
while ((sysBlockEntry = readdir(sysBlockDirp)) != NULL)
{
const char* const devName = sysBlockEntry->d_name;
2025-03-26 16:42:39 +08:00
if (devName[0] == '.') continue;
2023-12-17 10:38:16 +08:00
2024-10-15 10:30:58 +08:00
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);
2023-10-07 16:21:26 +08:00
}
return NULL;
}