Skip to content

Commit

Permalink
Merge pull request #502 from mattico/linux-zfs-arc
Browse files Browse the repository at this point in the history
Linux: subtract ZFS ARC min size from available memory
  • Loading branch information
aristocratos committed Feb 23, 2023
2 parents af04de9 + 4d6e791 commit 3200de5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ mem_graphs = True
#* Show mem box below net box instead of above.
mem_below_net = False
#* Count ZFS ARC in cached and available memory.
zfs_arc_cached = True
#* If swap memory should be shown in memory box.
show_swap = True
Expand Down
19 changes: 11 additions & 8 deletions src/linux/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,6 @@ namespace Mem {
fs::file_time_type fstab_time;
int disk_ios{}; // defaults to 0
vector<string> last_found;
const std::regex zfs_size_regex("^size\\s+\\d\\s+(\\d+)");

//?* Find the filepath to the specified ZFS object's stat file
fs::path get_zfs_stat_file(const string& device_name, size_t dataset_name_start, bool zfs_hide_datasets);
Expand Down Expand Up @@ -851,15 +850,17 @@ namespace Mem {
mem.stats.at("swap_total") = 0;

//? Read ZFS ARC info from /proc/spl/kstat/zfs/arcstats
uint64_t arc_size = 0;
uint64_t arc_size = 0, arc_min_size = 0;
if (zfs_arc_cached) {
ifstream arcstats(Shared::procPath / "spl/kstat/zfs/arcstats");
if (arcstats.good()) {
std::string line;
while (std::getline(arcstats, line)) {
std::smatch match;
if (std::regex_match(line, match, zfs_size_regex) && match.size() == 2) {
arc_size = stoull(match.str(1));
for (string label; arcstats >> label;) {
if (label == "c_min") {
arcstats >> arc_min_size >> arc_min_size; // double read skips type column
}
else if (label == "size") {
arcstats >> arc_size >> arc_size;
break;
}
}
}
Expand Down Expand Up @@ -899,7 +900,9 @@ namespace Mem {
if (not got_avail) mem.stats.at("available") = mem.stats.at("free") + mem.stats.at("cached");
if (zfs_arc_cached) {
mem.stats.at("cached") += arc_size;
mem.stats.at("available") += arc_size;
// The ARC will not shrink below arc_min_size, so that memory is not available
if (arc_size > arc_min_size)
mem.stats.at("available") += arc_size - arc_min_size;
}
mem.stats.at("used") = totalMem - (mem.stats.at("available") <= totalMem ? mem.stats.at("available") : mem.stats.at("free"));

Expand Down

0 comments on commit 3200de5

Please sign in to comment.