Skip to content

Commit

Permalink
Add filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
everdrone committed Jun 29, 2021
1 parent 5b20462 commit bffc50b
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ EXTRA_DIST = readme.md license
jnfo_SOURCES = \
include/cpu.h \
include/defines.h \
include/filesystem.h \
include/formatter.h \
include/gpu.h \
include/mem.h \
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([jnfo], [1.0.0], [])
AC_INIT([jnfo], [1.0.1], [])
AM_INIT_AUTOMAKE([-Wall foreign subdir-objects])

AX_CXX_COMPILE_STDCXX_17
Expand Down
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
jnfo (1.0.1-1) unstable; urgency=medium

* Add filesystem statistics

-- Giorgio Tropiano <[email protected]> Sun, 13 Jun 2021 13:25:54 +0200

jnfo (1.0.0-1) unstable; urgency=medium

* Initial release
Expand Down
1 change: 1 addition & 0 deletions include/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ vector<cpu_t> get_cpus() {

void pretty_print(vector<cpu_t> info, bool summary = false) {
fmt_measure_t fmt;

unsigned avg_cur_freq = 0;
unsigned avg_max_freq = 0;

Expand Down
3 changes: 3 additions & 0 deletions include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
// memory
#define IRAM_SIZE_PATH TEST_PATH "/sys/kernel/debug/nvmap/iram/size"

// filesystem
#define MTAB_PATH TEST_PATH "/etc/mtab"

// power
#define POWER_INPUT_NAME_GLOB \
TEST_PATH "/sys/bus/i2c/drivers/ina3221x/6-0040/iio:device0/rail_name_[0-9]"
Expand Down
118 changes: 118 additions & 0 deletions include/filesystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#pragma once

#include <string.h>
#include <sys/statvfs.h>

#include "defines.h"
#include "formatter.h"

typedef struct {
string name;
string path;
unsigned long capacity;
unsigned long free;
unsigned long available;
unsigned long used;
unsigned long nonroot_total;
} fs_t;

vector<fs_t> get_filesystem() {
vector<fs_t> result;

FILE* fd = fopen(MTAB_PATH, "r");

if (fd == NULL) {
std::cerr << "cannot open " << MTAB_PATH << std::endl;
exit(EXIT_FAILURE);
}

char line[128];
char part_name[32];
char mount_path[32];

while (fgets(line, 128, fd) != NULL) {
// parse line
if (sscanf(line, "%s %s", part_name, mount_path) == 2) {
int num_slashes = 0;
char* ptr = mount_path;

// count forward slashes in path
while ((ptr = strchr(ptr, '/')) != NULL) {
num_slashes++, ptr++;
}

if (num_slashes == 1) {
// consider filesystem
fs_t filesystem;
filesystem.path = mount_path;
#if __APPLE__
if (filesystem.path == "/") {
#endif
filesystem.name = part_name;

struct statvfs vfs;

int err = statvfs(filesystem.path.c_str(), &vfs);
if (err != 0) {
std::cerr << "Cannot stat filesystem." << std::endl;
std::cerr << err << std::endl;
fclose(fd);
exit(EXIT_FAILURE);
}

filesystem.capacity = vfs.f_blocks * vfs.f_frsize;
filesystem.free = vfs.f_bfree * vfs.f_frsize;
filesystem.available = vfs.f_bavail * vfs.f_frsize;

filesystem.used = filesystem.capacity - filesystem.free;
filesystem.nonroot_total = filesystem.used + filesystem.available;

result.push_back(filesystem);
}

#if __APPLE__
}
#endif
} else {
std::cerr << "cannot parse " << MTAB_PATH << std::endl;
fclose(fd);
exit(EXIT_FAILURE);
}
}

fclose(fd);

return result;
}

void pretty_print(vector<fs_t> info, bool summary = false) {
fmt_measure_t fmt;

pretty("Filesystem", "", 0);

for (const auto& fs : info) {
if (summary) {
if (fs.path == "/") {
fmt = format_storage(fs.capacity, 2);
pretty("Capacity", fmt.value.c_str(), 1, NUMBER, 0, fmt.unit.c_str());

pretty("Used", format_percent(fs.used, fs.nonroot_total, 0).c_str(), 1, PERCENT, 4);
break;
}
} else {
pretty("Name", fs.name.c_str(), 2, STRING, 5, "", true);
pretty("Path", fs.path.c_str(), 2, STRING, 5);

fmt = format_storage(fs.capacity, 2);
pretty("Capacity", fmt.value.c_str(), 2, NUMBER, 1, fmt.unit.c_str());

fmt = format_storage(fs.free, 2);
pretty("Free", fmt.value.c_str(), 2, NUMBER, 5, fmt.unit.c_str());

fmt = format_storage(fs.available, 2);
pretty("Available", fmt.value.c_str(), 2, NUMBER, 0, fmt.unit.c_str());

pretty("Used", format_percent(fs.used, fs.nonroot_total, 0).c_str(), 2, PERCENT, 5);
}
}
}
11 changes: 7 additions & 4 deletions include/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ fmt_measure_t format_frequency(unsigned hertz, int decimals) {
return out;
}

fmt_measure_t format_storage(unsigned bytes, int decimals,
bool use_iec = false) {
fmt_measure_t format_storage(unsigned long bytes, int decimals, bool use_iec = false) {
fmt_measure_t out;
char buffer[16];
float result = float(bytes);
Expand Down Expand Up @@ -74,7 +73,7 @@ fmt_measure_t format_storage(unsigned bytes, int decimals,
out.unit = "kB";
} else {
// return bytes
sprintf(buffer, "%d", bytes);
sprintf(buffer, "%lu", bytes);
out.value = buffer;
out.unit = "B";
return out;
Expand All @@ -90,7 +89,11 @@ string format_percent(float x, float y, int decimals) {
string out;
char buffer[16];

sprintf(buffer, "%.*f%%", decimals, x / y * 100.0);
if (y == 0) {
sprintf(buffer, "0%%");
} else {
sprintf(buffer, "%.*f%%", decimals, x / y * 100.0);
}

out = buffer;
return out;
Expand Down
34 changes: 34 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Memory:
RAM: 33%
Swap: 0%
NVMap: 0%
Filesystem:
Capacity: 125.72 GB
Used: 11%
Power:
Total: 2078 mW
```
Expand Down Expand Up @@ -170,6 +173,37 @@ Memory:
Free: 258.05 kB
Used: 0 B
Usage: 0%
Filesystem:
- Name: /dev/mmcblk0p1
Path: /
Capacity: 125.72 GB
Free: 112.47 GB
Available: 107.20 GB
Used: 11%
- Name: proc
Path: /proc
Capacity: 0 B
Free: 0 B
Available: 0 B
Used: 0%
- Name: sysfs
Path: /sys
Capacity: 0 B
Free: 0 B
Available: 0 B
Used: 0%
- Name: none
Path: /dev
Capacity: 1.83 GB
Free: 1.83 GB
Available: 1.83 GB
Used: 0%
- Name: tmpfs
Path: /run
Capacity: 2.08 GB
Free: 2.06 GB
Available: 2.06 GB
Used: 1%
Power:
- Name: POM_5V_IN
Critical Current Limit: 32760 mA
Expand Down
18 changes: 16 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "config.h"
#include "cpu.h"
#include "defines.h"
#include "filesystem.h"
#include "formatter.h"
#include "gpu.h"
#include "mem.h"
Expand Down Expand Up @@ -43,6 +44,7 @@ typedef struct {
power_t power;

memory_t memory;
vector<fs_t> filesystem;

// TODO: ape_t ape
// TODO: vic_t vic
Expand All @@ -60,6 +62,7 @@ typedef struct options_struct {
bool gpu = false;
bool thermal = false;
bool memory = false;
bool storage = false;
bool power = false;
} options_t;

Expand All @@ -79,6 +82,7 @@ void print_help_exit() {
" -g --gpu Show GPU information and clients\n"
" -t --thermal Show thermal sensors\n"
" -m --memory Show RAM, swap and NVMap\n"
" -f --storage Show filesystems\n"
" -p --power Show power and current information\n"
" -C --color Enable colored output\n",
progname);
Expand All @@ -104,13 +108,14 @@ int main(int argc, char* argv[]) {
{"gpu", no_argument, NULL, 'g'},
{"thermal", no_argument, NULL, 't'},
{"memory", no_argument, NULL, 'm'},
{"storage", no_argument, NULL, 'f'},
{"power", no_argument, NULL, 'p'},
{"color", no_argument, NULL, 'C'},
{NULL, 0, NULL, 0}};
// clang-format on

int opt;
while ((opt = getopt_long(argc, argv, "hvscgtmpC", long_options, NULL)) >= 0) {
while ((opt = getopt_long(argc, argv, "hvscgtmfpC", long_options, NULL)) >= 0) {
switch (opt) {
case 'h':
oobj.help = true;
Expand All @@ -136,6 +141,9 @@ int main(int argc, char* argv[]) {
case 'm':
oobj.memory = true;
break;
case 'f':
oobj.storage = true;
break;
case 'p':
oobj.power = true;
break;
Expand Down Expand Up @@ -163,7 +171,8 @@ int main(int argc, char* argv[]) {

jetson_info_t result;

if ((oobj.gpu || oobj.cpu || oobj.thermal || oobj.memory || oobj.power) == false) {
if ((oobj.gpu || oobj.cpu || oobj.thermal || oobj.memory || oobj.storage || oobj.power) ==
false) {
oobj.all = true;
}

Expand Down Expand Up @@ -194,6 +203,11 @@ int main(int argc, char* argv[]) {
pretty_print(result.memory, oobj.summary);
}

if (oobj.all || oobj.storage) {
result.filesystem = get_filesystem();
pretty_print(result.filesystem, oobj.summary);
}

if (oobj.all || oobj.power) {
result.power = get_power();
pretty_print(result.power, oobj.summary);
Expand Down
31 changes: 31 additions & 0 deletions test/etc/mtab
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/dev/mmcblk0p1 / ext4 rw,relatime,data=ordered 0 0
proc /proc proc rw,relatime 0 0
sysfs /sys sysfs rw,relatime 0 0
none /dev devtmpfs rw,relatime,size=1784284k,nr_inodes=446071,mode=755 0 0
tmpfs /dev/shm tmpfs rw,nosuid,nodev 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,nodev,mode=755 0 0
tmpfs /run/lock tmpfs rw,nosuid,nodev,noexec,relatime,size=5120k 0 0
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0
cgroup /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,name=systemd 0 0
pstore /sys/fs/pstore pstore rw,nosuid,nodev,noexec,relatime 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/debug cgroup rw,nosuid,nodev,noexec,relatime,debug 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=27,pgrp=1,timeout=0,minproto=5,maxproto=5,direct 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
sunrpc /run/rpc_pipefs rpc_pipefs rw,relatime 0 0
mqueue /dev/mqueue mqueue rw,relatime 0 0
debugfs /sys/kernel/debug debugfs rw,relatime 0 0
configfs /sys/kernel/config configfs rw,relatime 0 0
tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=405924k,mode=700,uid=1000,gid=1000 0 0
binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,relatime 0 0

0 comments on commit bffc50b

Please sign in to comment.