i3status_rs/blocks/
memory.rs

1//! Memory and swap usage
2//!
3//! # Configuration
4//!
5//! Key | Values | Default
6//! ----|--------|--------
7//! `format` | A string to customise the output of this block when in "Memory" view. See below for available placeholders. | `" $icon $mem_used.eng(prefix:Mi)/$mem_total.eng(prefix:Mi)($mem_used_percents.eng(w:2)) "`
8//! `format_alt` | If set, block will switch between `format` and `format_alt` on every click | `None`
9//! `interval` | Update interval in seconds | `5`
10//! `warning_mem` | Percentage of memory usage, where state is set to warning | `80.0`
11//! `warning_swap` | Percentage of swap usage, where state is set to warning | `80.0`
12//! `critical_mem` | Percentage of memory usage, where state is set to critical | `95.0`
13//! `critical_swap` | Percentage of swap usage, where state is set to critical | `95.0`
14//!
15//! Placeholder               | Value                                                                           | Type   | Unit
16//! --------------------------|---------------------------------------------------------------------------------|--------|-------
17//! `icon`                    | Memory icon                                                                     | Icon   | -
18//! `icon_swap`               | Swap icon                                                                       | Icon   | -
19//! `mem_total`               | Total physical ram available                                                    | Number | Bytes
20//! `mem_free`                | Free memory not yet used by the kernel or userspace (in general you should use mem_avail) | Number | Bytes
21//! `mem_free_percents`       | as above but as a percentage of total memory                                    | Number | Percents
22//! `mem_avail`               | Kernel estimate of usable free memory which includes cached memory and buffers  | Number | Bytes
23//! `mem_avail_percents`      | as above but as a percentage of total memory                                    | Number | Percents
24//! `mem_total_used`          | mem_total - mem_free                                                            | Number | Bytes
25//! `mem_total_used_percents` | as above but as a percentage of total memory                                    | Number | Percents
26//! `mem_used`                | Memory used, excluding cached memory and buffers; same as htop's green bar      | Number | Bytes
27//! `mem_used_percents`       | as above but as a percentage of total memory                                    | Number | Percents
28//! `buffers`                 | Buffers, similar to htop's blue bar                                             | Number | Bytes
29//! `buffers_percent`         | as above but as a percentage of total memory                                    | Number | Percents
30//! `cached`                  | Cached memory (taking into account ZFS ARC cache), similar to htop's yellow bar | Number | Bytes
31//! `cached_percent`          | as above but as a percentage of total memory                                    | Number | Percents
32//! `swap_total`              | Swap total                                                                      | Number | Bytes
33//! `swap_free`               | Swap free                                                                       | Number | Bytes
34//! `swap_free_percents`      | as above but as a percentage of total memory                                    | Number | Percents
35//! `swap_used`               | Swap used                                                                       | Number | Bytes
36//! `swap_used_percents`      | as above but as a percentage of total memory                                    | Number | Percents
37//! `zram_compressed`         | Compressed zram memory usage                                                    | Number | Bytes
38//! `zram_decompressed`       | Decompressed zram memory usage                                                  | Number | Bytes
39//! 'zram_comp_ratio'         | Ratio of the decompressed/compressed zram memory                                | Number | -
40//! `zswap_compressed`        | Compressed zswap memory usage (>=Linux 5.19)                                    | Number | Bytes
41//! `zswap_decompressed`      | Decompressed zswap memory usage (>=Linux 5.19)                                  | Number | Bytes
42//! `zswap_decompressed_percents` | as above but as a percentage of total zswap memory  (>=Linux 5.19)          | Number | Percents
43//! 'zswap_comp_ratio'        | Ratio of the decompressed/compressed zswap memory (>=Linux 5.19)                | Number | -
44//!
45//! Action          | Description                               | Default button
46//! ----------------|-------------------------------------------|---------------
47//! `toggle_format` | Toggles between `format` and `format_alt` | Left
48//!
49//! # Examples
50//!
51//! ```toml
52//! [[block]]
53//! block = "memory"
54//! format = " $icon $mem_used_percents.eng(w:1) "
55//! format_alt = " $icon_swap $swap_free.eng(w:3,u:B,p:Mi)/$swap_total.eng(w:3,u:B,p:Mi)($swap_used_percents.eng(w:2)) "
56//! interval = 30
57//! warning_mem = 70
58//! critical_mem = 90
59//! ```
60//!
61//! Show swap and hide if it is zero:
62//!
63//! ```toml
64//! [[block]]
65//! block = "memory"
66//! format = " $icon $swap_used.eng(range:1..) |"
67//! ```
68//!
69//! # Icons Used
70//! - `memory_mem`
71//! - `memory_swap`
72
73use std::cmp::min;
74use std::str::FromStr as _;
75use tokio::fs::{File, read_dir};
76use tokio::io::{AsyncBufReadExt as _, BufReader};
77
78use super::prelude::*;
79use crate::util::read_file;
80
81#[derive(Deserialize, Debug, SmartDefault)]
82#[serde(deny_unknown_fields, default)]
83pub struct Config {
84    pub format: FormatConfig,
85    pub format_alt: Option<FormatConfig>,
86    #[default(5.into())]
87    pub interval: Seconds,
88    #[default(80.0)]
89    pub warning_mem: f64,
90    #[default(80.0)]
91    pub warning_swap: f64,
92    #[default(95.0)]
93    pub critical_mem: f64,
94    #[default(95.0)]
95    pub critical_swap: f64,
96}
97
98pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
99    let mut actions = api.get_actions()?;
100    api.set_default_actions(&[(MouseButton::Left, None, "toggle_format")])?;
101
102    let mut format = config.format.with_default(
103        " $icon $mem_used.eng(prefix:Mi)/$mem_total.eng(prefix:Mi)($mem_used_percents.eng(w:2)) ",
104    )?;
105    let mut format_alt = match &config.format_alt {
106        Some(f) => Some(f.with_default("")?),
107        None => None,
108    };
109
110    let mut timer = config.interval.timer();
111
112    loop {
113        let mem_state = Memstate::new().await?;
114
115        let mem_total = mem_state.mem_total as f64;
116        let mem_free = mem_state.mem_free as f64;
117
118        // TODO: possibly remove this as it is confusing to have `mem_total_used` and `mem_used`
119        // htop and such only display equivalent of `mem_used`
120        let mem_total_used = mem_total - mem_free;
121
122        // dev note: difference between avail and free:
123        // https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
124        // same logic as htop
125        let mem_avail = if mem_state.mem_available != 0 {
126            min(mem_state.mem_available, mem_state.mem_total)
127        } else {
128            mem_state.mem_free
129        } as f64;
130
131        // While zfs_arc_cache can be considered "available" memory,
132        // it can only free a maximum of (zfs_arc_cache - zfs_arc_min) amount.
133        // see https://github.com/htop-dev/htop/pull/1003
134        let zfs_shrinkable_size = mem_state
135            .zfs_arc_cache
136            .saturating_sub(mem_state.zfs_arc_min) as f64;
137        let mem_avail = mem_avail + zfs_shrinkable_size;
138
139        let pagecache = mem_state.pagecache as f64;
140        let reclaimable = mem_state.s_reclaimable as f64;
141        let shmem = mem_state.shmem as f64;
142
143        // See https://lore.kernel.org/lkml/1455827801-13082-1-git-send-email-hannes@cmpxchg.org/
144        let cached = pagecache + reclaimable - shmem + zfs_shrinkable_size;
145
146        let buffers = mem_state.buffers as f64;
147
148        // Userspace should use `mem_avail` for estimating the memory that is available.
149        // See: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
150        let mem_used = mem_total - mem_avail;
151
152        // account for ZFS ARC cache
153        let mem_used = mem_used - zfs_shrinkable_size;
154
155        let swap_total = mem_state.swap_total as f64;
156        let swap_free = mem_state.swap_free as f64;
157        let swap_cached = mem_state.swap_cached as f64;
158        let swap_used = swap_total - swap_free - swap_cached;
159
160        // Zswap usage
161        let zswap_compressed = mem_state.zswap_compressed as f64;
162        let zswap_decompressed = mem_state.zswap_decompressed as f64;
163
164        let zswap_comp_ratio = if zswap_compressed != 0.0 {
165            zswap_decompressed / zswap_compressed
166        } else {
167            0.0
168        };
169        let zswap_decompressed_percents = if (swap_used + swap_cached) != 0.0 {
170            zswap_decompressed / (swap_used + swap_cached) * 100.0
171        } else {
172            0.0
173        };
174
175        // Zram usage
176        let zram_compressed = mem_state.zram_compressed as f64;
177        let zram_decompressed = mem_state.zram_decompressed as f64;
178
179        let zram_comp_ratio = if zram_compressed != 0.0 {
180            zram_decompressed / zram_compressed
181        } else {
182            0.0
183        };
184
185        let mut widget = Widget::new().with_format(format.clone());
186        widget.set_values(map! {
187            "icon" => Value::icon("memory_mem"),
188            "icon_swap" => Value::icon("memory_swap"),
189            "mem_total" => Value::bytes(mem_total),
190            "mem_free" => Value::bytes(mem_free),
191            "mem_free_percents" => Value::percents(mem_free / mem_total * 100.),
192            "mem_total_used" => Value::bytes(mem_total_used),
193            "mem_total_used_percents" => Value::percents(mem_total_used / mem_total * 100.),
194            "mem_used" => Value::bytes(mem_used),
195            "mem_used_percents" => Value::percents(mem_used / mem_total * 100.),
196            "mem_avail" => Value::bytes(mem_avail),
197            "mem_avail_percents" => Value::percents(mem_avail / mem_total * 100.),
198            "swap_total" => Value::bytes(swap_total),
199            "swap_free" => Value::bytes(swap_free),
200            "swap_free_percents" => Value::percents(swap_free / swap_total * 100.),
201            "swap_used" => Value::bytes(swap_used),
202            "swap_used_percents" => Value::percents(swap_used / swap_total * 100.),
203            "buffers" => Value::bytes(buffers),
204            "buffers_percent" => Value::percents(buffers / mem_total * 100.),
205            "cached" => Value::bytes(cached),
206            "cached_percent" => Value::percents(cached / mem_total * 100.),
207            "zram_compressed" => Value::bytes(zram_compressed),
208            "zram_decompressed" => Value::bytes(zram_decompressed),
209            "zram_comp_ratio" => Value::number(zram_comp_ratio),
210            "zswap_compressed" => Value::bytes(zswap_compressed),
211            "zswap_decompressed" => Value::bytes(zswap_decompressed),
212            "zswap_decompressed_percents" => Value::percents(zswap_decompressed_percents),
213            "zswap_comp_ratio" => Value::number(zswap_comp_ratio),
214        });
215
216        let mem_state = match mem_used / mem_total * 100. {
217            x if x > config.critical_mem => State::Critical,
218            x if x > config.warning_mem => State::Warning,
219            _ => State::Idle,
220        };
221
222        let swap_state = match swap_used / swap_total * 100. {
223            x if x > config.critical_swap => State::Critical,
224            x if x > config.warning_swap => State::Warning,
225            _ => State::Idle,
226        };
227
228        widget.state = if mem_state == State::Critical || swap_state == State::Critical {
229            State::Critical
230        } else if mem_state == State::Warning || swap_state == State::Warning {
231            State::Warning
232        } else {
233            State::Idle
234        };
235
236        api.set_widget(widget)?;
237
238        loop {
239            select! {
240                _ = timer.tick() => break,
241                _ = api.wait_for_update_request() => break,
242                Some(action) = actions.recv() => match action.as_ref() {
243                    "toggle_format" => {
244                        if let Some(ref mut format_alt) = format_alt {
245                            std::mem::swap(format_alt, &mut format);
246                            break;
247                        }
248                    }
249                    _ => (),
250                }
251            }
252        }
253    }
254}
255
256#[derive(Clone, Copy, Debug, Default)]
257struct Memstate {
258    mem_total: u64,
259    mem_free: u64,
260    mem_available: u64,
261    buffers: u64,
262    pagecache: u64,
263    s_reclaimable: u64,
264    shmem: u64,
265    swap_total: u64,
266    swap_free: u64,
267    swap_cached: u64,
268    zram_compressed: u64,
269    zram_decompressed: u64,
270    zswap_compressed: u64,
271    zswap_decompressed: u64,
272    zfs_arc_cache: u64,
273    zfs_arc_min: u64,
274}
275
276impl Memstate {
277    async fn new() -> Result<Self> {
278        // Reference: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
279        let mut file = BufReader::new(
280            File::open("/proc/meminfo")
281                .await
282                .error("/proc/meminfo does not exist")?,
283        );
284
285        let mut mem_state = Memstate::default();
286        let mut line = String::new();
287
288        while file
289            .read_line(&mut line)
290            .await
291            .error("failed to read /proc/meminfo")?
292            != 0
293        {
294            let mut words = line.split_whitespace();
295
296            let name = match words.next() {
297                Some(name) => name,
298                None => {
299                    line.clear();
300                    continue;
301                }
302            };
303            let val = words
304                .next()
305                .and_then(|x| u64::from_str(x).ok())
306                .error("failed to parse /proc/meminfo")?;
307
308            // These values are reported as “kB” but are actually “kiB”.
309            // Convert them into bytes to avoid having to handle this later.
310            // Source: https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/6/html/deployment_guide/s2-proc-meminfo#s2-proc-meminfo
311            const KIB: u64 = 1024;
312            match name {
313                "MemTotal:" => mem_state.mem_total = val * KIB,
314                "MemFree:" => mem_state.mem_free = val * KIB,
315                "MemAvailable:" => mem_state.mem_available = val * KIB,
316                "Buffers:" => mem_state.buffers = val * KIB,
317                "Cached:" => mem_state.pagecache = val * KIB,
318                "SReclaimable:" => mem_state.s_reclaimable = val * KIB,
319                "Shmem:" => mem_state.shmem = val * KIB,
320                "SwapTotal:" => mem_state.swap_total = val * KIB,
321                "SwapFree:" => mem_state.swap_free = val * KIB,
322                "SwapCached:" => mem_state.swap_cached = val * KIB,
323                "Zswap:" => mem_state.zswap_compressed = val * KIB,
324                "Zswapped:" => mem_state.zswap_decompressed = val * KIB,
325                _ => (),
326            }
327
328            line.clear();
329        }
330
331        // For ZRAM
332        let mut entries = read_dir("/sys/block/")
333            .await
334            .error("Could not read /sys/block")?;
335        while let Some(entry) = entries
336            .next_entry()
337            .await
338            .error("Could not get next file /sys/block")?
339        {
340            let Ok(file_name) = entry.file_name().into_string() else {
341                continue;
342            };
343            if !file_name.starts_with("zram") {
344                continue;
345            }
346
347            let zram_file_path = entry.path().join("mm_stat");
348            let Ok(file) = File::open(zram_file_path).await else {
349                continue;
350            };
351
352            let mut buf = BufReader::new(file);
353            let mut line = String::new();
354            if buf.read_to_string(&mut line).await.is_err() {
355                continue;
356            }
357
358            let mut values = line.split_whitespace().map(|s| s.parse::<u64>());
359            if let Some(Ok(zram_swap_size)) = values.next() && let Some(Ok(zram_comp_size)) = values.next()
360                // zram initializes with small amount by default, return 0 then
361                && zram_swap_size >= 65_536
362            {
363                mem_state.zram_decompressed += zram_swap_size;
364                mem_state.zram_compressed += zram_comp_size;
365            }
366        }
367
368        // For ZFS
369        if let Ok(arcstats) = read_file("/proc/spl/kstat/zfs/arcstats").await {
370            let size_re = regex!(r"size\s+\d+\s+(\d+)");
371            let size = &size_re
372                .captures(&arcstats)
373                .error("failed to find zfs_arc_cache size")?[1];
374            mem_state.zfs_arc_cache = size.parse().error("failed to parse zfs_arc_cache size")?;
375            let c_min_re = regex!(r"c_min\s+\d+\s+(\d+)");
376            let c_min = &c_min_re
377                .captures(&arcstats)
378                .error("failed to find zfs_arc_min size")?[1];
379            mem_state.zfs_arc_min = c_min.parse().error("failed to parse zfs_arc_min size")?;
380        }
381
382        Ok(mem_state)
383    }
384}