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

100 lines
3.3 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"
2026-05-28 15:58:08 +08:00
#include "common/strutil.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
2026-03-29 09:28:49 +08:00
static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* result, FFDiskIOOptions* options) {
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);
2026-03-29 09:28:49 +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
{
2026-03-29 09:28:49 +08:00
if (ffAppendFileBufferRelative(devfd, "vendor", &name)) {
2024-10-15 10:30:58 +08:00
ffStrbufTrimRightSpace(&name);
2026-03-29 09:28:49 +08:00
if (name.length > 0) {
2024-10-15 10:30:58 +08:00
ffStrbufAppendC(&name, ' ');
2026-03-29 09:28:49 +08:00
}
2024-10-15 10:30:58 +08:00
}
2025-08-25 09:26:04 +08:00
ffAppendFileBufferRelative(devfd, "model", &name);
ffStrbufTrimRightSpace(&name);
2026-03-29 09:28:49 +08:00
if (name.length == 0) {
2024-10-15 10:30:58 +08:00
ffStrbufSetS(&name, devName);
2026-03-29 09:28:49 +08:00
} else if (ffStrStartsWith(devName, "nvme")) {
2024-10-15 10:30:58 +08:00
int devid, nsid;
2026-03-29 09:28:49 +08:00
if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2) {
2024-10-15 10:30:58 +08:00
bool multiNs = nsid > 1;
2026-03-29 09:28:49 +08:00
if (!multiNs) {
2024-10-15 10:30:58 +08:00
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
}
2026-03-29 09:28:49 +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
}
2026-03-29 09:28:49 +08:00
if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
2025-03-26 16:42:39 +08:00
return "ignored";
2026-03-29 09:28:49 +08:00
}
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);
2026-03-29 09:28:49 +08:00
if (fileSize <= 0) {
return "failed to read stat file";
}
2024-10-15 10:30:58 +08:00
sysBlockStat[fileSize] = '\0';
2026-03-29 09:28:49 +08:00
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";
2026-03-29 09:28:49 +08:00
}
2024-10-15 10:30:58 +08:00
}
2026-04-14 10:41:18 +08:00
FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
2024-10-15 10:30:58 +08:00
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
}
2026-03-29 09:28:49 +08:00
const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
2024-10-15 10:30:58 +08:00
FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
2026-03-29 09:28:49 +08:00
if (sysBlockDirp == NULL) {
2024-10-15 10:30:58 +08:00
return "opendir(\"/sys/block/\") == NULL";
2026-03-29 09:28:49 +08:00
}
2024-10-15 10:30:58 +08:00
struct dirent* sysBlockEntry;
2026-03-29 09:28:49 +08:00
while ((sysBlockEntry = readdir(sysBlockDirp)) != NULL) {
2024-10-15 10:30:58 +08:00
const char* const devName = sysBlockEntry->d_name;
2026-03-29 09:28:49 +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);
2026-03-29 09:28:49 +08:00
if (dfd > 0) {
parseDiskIOCounters(dfd, devName, result, options);
}
2023-10-07 16:21:26 +08:00
}
return NULL;
}