ohmu
·
2026-10-15
1#include "diskio.h"
2#include "common/debug.h"
3#include "common/io.h"
4#include "common/properties.h"
5#include "common/strutil.h"
6
7#include <ctype.h>
8#include <limits.h>
9#include <inttypes.h>
10#include <fcntl.h>
11
12static const char* parseDiskIOCounters(int dfd, const char* devName, FFlist* result, FFDiskIOOptions* options) {
13 FF_AUTO_CLOSE_FD int devfd = openat(dfd, "device", O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
14 if (devfd < 0) {
15 return "virtual device";
16 }
17
18 FF_STRBUF_AUTO_DESTROY name = ffStrbufCreate();
19
20 {
21 if (ffAppendFileBufferRelative(devfd, "vendor", &name)) {
22 ffStrbufTrimRightSpace(&name);
23 if (name.length > 0) {
24 ffStrbufAppendC(&name, ' ');
25 }
26 }
27
28 ffAppendFileBufferRelative(devfd, "model", &name);
29 ffStrbufTrimRightSpace(&name);
30
31 if (name.length == 0) {
32 ffStrbufSetS(&name, devName);
33 } else if (ffStrStartsWith(devName, "nvme")) {
34 int devid, nsid;
35 if (sscanf(devName, "nvme%dn%d", &devid, &nsid) == 2) {
36 bool multiNs = nsid > 1;
37 if (!multiNs) {
38 char pathSysBlock[16];
39 snprintf(pathSysBlock, ARRAY_SIZE(pathSysBlock), "nvme%dn2", devid);
40 multiNs = faccessat(devfd, pathSysBlock, F_OK, 0) == 0;
41 }
42 if (multiNs) {
43 // In Asahi Linux, there are multiple namespaces for the same NVMe drive.
44 ffStrbufAppendF(&name, " - %d", nsid);
45 }
46 }
47 }
48
49 if (options->namePrefix.length && !ffStrbufStartsWith(&name, &options->namePrefix)) {
50 return "ignored";
51 }
52 }
53
54 // I/Os merges sectors ticks ...
55 uint64_t nRead, sectorRead, nWritten, sectorWritten;
56 {
57 char sysBlockStat[PROC_FILE_BUFFSIZ];
58 ssize_t fileSize = ffReadFileDataRelative(dfd, "stat", ARRAY_SIZE(sysBlockStat) - 1, sysBlockStat);
59 if (fileSize <= 0) {
60 FF_DEBUG("failed to read stat file");
61 return "failed to read stat file";
62 }
63 sysBlockStat[fileSize] = '\0';
64 if (sscanf(sysBlockStat, "%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u%" PRIu64 "%*u", &nRead, §orRead, &nWritten, §orWritten) <= 0) {
65 FF_DEBUG("invalid stat file format");
66 return "invalid stat file format";
67 }
68 }
69
70 FFDiskIOResult* device = FF_LIST_ADD(FFDiskIOResult, *result);
71 ffStrbufInitMove(&device->name, &name);
72 ffStrbufInitF(&device->devPath, "/dev/%s", devName);
73 device->bytesRead = sectorRead * 512;
74 device->bytesWritten = sectorWritten * 512;
75 device->readCount = nRead;
76 device->writeCount = nWritten;
77
78 return nullptr;
79}
80
81const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) {
82 FF_AUTO_CLOSE_DIR DIR* sysBlockDirp = opendir("/sys/block/");
83 if (sysBlockDirp == nullptr) {
84 FF_DEBUG("opendir(\"/sys/block/\") failed: %s", strerror(errno));
85 return "opendir(\"/sys/block/\") == nullptr";
86 }
87
88 struct dirent* sysBlockEntry;
89 while ((sysBlockEntry = readdir(sysBlockDirp)) != nullptr) {
90 const char* const devName = sysBlockEntry->d_name;
91
92 if (devName[0] == '.') {
93 continue;
94 }
95
96 FF_AUTO_CLOSE_FD int dfd = openat(dirfd(sysBlockDirp), devName, O_RDONLY | O_CLOEXEC | O_PATH | O_DIRECTORY);
97 if (dfd > 0) {
98 parseDiskIOCounters(dfd, devName, result, options);
99 }
100 }
101
102 return nullptr;
103}