1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str::FromStr;
use std::time::{Duration, Instant};

use crossbeam_channel::Sender;
use serde_derive::Deserialize;

use crate::blocks::{Block, ConfigBlock, Update};
use crate::config::SharedConfig;
use crate::de::deserialize_duration;
use crate::errors::*;
use crate::formatting::value::Value;
use crate::formatting::FormatTemplate;
use crate::input::{I3BarEvent, MouseButton};
use crate::scheduler::Task;
use crate::widgets::text::TextWidget;
use crate::widgets::{I3BarWidget, State};

#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum Memtype {
    Swap,
    Memory,
}

#[derive(Clone, Copy, Debug)]
// Not following naming convention, because of naming in /proc/meminfo
struct Memstate {
    mem_total: (u64, bool),
    mem_free: (u64, bool),
    buffers: (u64, bool),
    cached: (u64, bool),
    s_reclaimable: (u64, bool),
    shmem: (u64, bool),
    swap_total: (u64, bool),
    swap_free: (u64, bool),
}

impl Memstate {
    fn mem_total(&self) -> u64 {
        self.mem_total.0
    }

    fn mem_free(&self) -> u64 {
        self.mem_free.0
    }

    fn buffers(&self) -> u64 {
        self.buffers.0
    }

    fn cached(&self) -> u64 {
        self.cached.0
    }

    fn s_reclaimable(&self) -> u64 {
        self.s_reclaimable.0
    }

    fn shmem(&self) -> u64 {
        self.shmem.0
    }

    fn swap_total(&self) -> u64 {
        self.swap_total.0
    }

    fn swap_free(&self) -> u64 {
        self.swap_free.0
    }

    fn new() -> Self {
        Memstate {
            mem_total: (0, false),
            mem_free: (0, false),
            buffers: (0, false),
            cached: (0, false),
            s_reclaimable: (0, false),
            shmem: (0, false),
            swap_total: (0, false),
            swap_free: (0, false),
        }
    }

    fn done(&self) -> bool {
        self.mem_total.1
            && self.mem_free.1
            && self.buffers.1
            && self.cached.1
            && self.s_reclaimable.1
            && self.shmem.1
            && self.swap_total.1
            && self.swap_free.1
    }
}

#[derive(Clone, Debug)]
pub struct Memory {
    id: usize,
    memtype: Memtype,
    output: (TextWidget, TextWidget),
    clickable: bool,
    format: (FormatTemplate, FormatTemplate),
    update_interval: Duration,
    tx_update_request: Sender<Task>,
    warning: (f64, f64),
    critical: (f64, f64),
}

#[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields, default)]
pub struct MemoryConfig {
    /// Format string for Memory view. All format values are described below.
    pub format_mem: String,

    /// Format string for Swap view.
    pub format_swap: String,

    /// Default view displayed on startup. Options are <br/> memory, swap
    pub display_type: Memtype,

    /// Whether the format string should be prepended with Icons. Options are <br/> true, false
    pub icons: bool,

    /// Whether the view should switch between memory and swap on click. Options are <br/> true, false
    pub clickable: bool,

    /// The delay in seconds between an update. If `clickable`, an update is triggered on click. Integer values only.
    #[serde(deserialize_with = "deserialize_duration")]
    pub interval: Duration,

    /// Percentage of memory usage, where state is set to warning
    pub warning_mem: f64,

    /// Percentage of swap usage, where state is set to warning
    pub warning_swap: f64,

    /// Percentage of memory usage, where state is set to critical
    pub critical_mem: f64,

    /// Percentage of swap usage, where state is set to critical
    pub critical_swap: f64,
}

impl Default for MemoryConfig {
    fn default() -> Self {
        Self {
            format_mem: "{mem_free;M}/{mem_total;M}({mem_total_used_percents})".to_string(),
            format_swap: "{swap_free;M}/{swap_total;M}({swap_used_percents})".to_string(),
            display_type: Memtype::Memory,
            icons: true,
            clickable: true,
            interval: Duration::from_secs(5),
            warning_mem: 80.,
            warning_swap: 80.,
            critical_mem: 95.,
            critical_swap: 95.,
        }
    }
}

impl Memory {
    fn format_insert_values(&mut self, mem_state: Memstate) -> Result<String> {
        let mem_total = mem_state.mem_total() as f64 * 1024.;
        let mem_free = mem_state.mem_free() as f64 * 1024.;
        let swap_total = mem_state.swap_total() as f64 * 1024.;
        let swap_free = mem_state.swap_free() as f64 * 1024.;
        let swap_used = swap_total - swap_free;
        let mem_total_used = mem_total - mem_free;
        let buffers = mem_state.buffers() as f64 * 1024.;
        let cached =
            // Why do we include shared memory to "cached"?
            (mem_state.cached() + mem_state.s_reclaimable() - mem_state.shmem()) as f64 * 1024.;
        let mem_used = mem_total_used - (buffers + cached);
        let mem_avail = mem_total - mem_used;

        let values = map!(
            "mem_total" => Value::from_float(mem_total).bytes(),
            "mem_free" => Value::from_float(mem_free).bytes(),
            "mem_free_percents" => Value::from_float(mem_free / mem_total * 100.).percents(),
            "mem_total_used" => Value::from_float(mem_total_used).bytes(),
            "mem_total_used_percents" => Value::from_float(mem_total_used / mem_total * 100.).percents(),
            "mem_used" => Value::from_float(mem_used).bytes(),
            "mem_used_percents" => Value::from_float(mem_used / mem_total * 100.).percents(),
            "mem_avail" => Value::from_float(mem_avail).bytes(),
            "mem_avail_percents" => Value::from_float(mem_avail / mem_total * 100.).percents(),
            "swap_total" => Value::from_float(swap_total).bytes(),
            "swap_free" => Value::from_float(swap_free).bytes(),
            "swap_free_percents" => Value::from_float(swap_free / swap_total * 100.).percents(),
            "swap_used" => Value::from_float(swap_used).bytes(),
            "swap_used_percents" => Value::from_float(swap_used / swap_total * 100.).percents(),
            "buffers" => Value::from_float(buffers).bytes(),
            "buffers_percent" => Value::from_float(buffers / mem_total * 100.).percents(),
            "cached" => Value::from_float(cached).bytes(),
            "cached_percent" => Value::from_float(cached / mem_total * 100.).percents(),
        );

        match self.memtype {
            Memtype::Memory => self.output.0.set_state(match mem_used / mem_total * 100. {
                x if x > self.critical.0 => State::Critical,
                x if x > self.warning.0 => State::Warning,
                _ => State::Idle,
            }),
            Memtype::Swap => self
                .output
                .1
                .set_state(match swap_used / swap_total * 100. {
                    x if x > self.critical.1 => State::Critical,
                    x if x > self.warning.1 => State::Warning,
                    _ => State::Idle,
                }),
        };

        Ok(match self.memtype {
            Memtype::Memory => self.format.0.render(&values)?,
            Memtype::Swap => self.format.1.render(&values)?,
        })
    }

    pub fn switch(&mut self) {
        let old: Memtype = self.memtype.clone();
        self.memtype = match old {
            Memtype::Memory => Memtype::Swap,
            _ => Memtype::Memory,
        };
    }
}

impl ConfigBlock for Memory {
    type Config = MemoryConfig;

    fn new(
        id: usize,
        block_config: Self::Config,
        shared_config: SharedConfig,
        tx: Sender<Task>,
    ) -> Result<Self> {
        let widget = TextWidget::new(id, 0, shared_config);
        Ok(Memory {
            id,
            memtype: block_config.display_type,
            output: if block_config.icons {
                (
                    widget.clone().with_icon("memory_mem")?,
                    widget.with_icon("memory_swap")?,
                )
            } else {
                (widget.clone(), widget)
            },
            clickable: block_config.clickable,
            format: (
                FormatTemplate::from_string(&block_config.format_mem)?,
                FormatTemplate::from_string(&block_config.format_swap)?,
            ),
            update_interval: block_config.interval,
            tx_update_request: tx,
            warning: (block_config.warning_mem, block_config.warning_swap),
            critical: (block_config.critical_mem, block_config.critical_swap),
        })
    }
}

impl Block for Memory {
    fn id(&self) -> usize {
        self.id
    }

    fn update(&mut self) -> Result<Option<Update>> {
        let f =
            File::open("/proc/meminfo").block_error("memory", "/proc/meminfo does not exist")?;
        let f = BufReader::new(f);

        let mut mem_state = Memstate::new();

        for line in f.lines() {
            // stop reading if all values are already present
            if mem_state.done() {
                break;
            }

            let line = match line {
                Ok(s) => s,
                _ => continue,
            };
            let line = line.split_whitespace().collect::<Vec<&str>>();

            match line.get(0) {
                Some(&"MemTotal:") => {
                    mem_state.mem_total = (
                        u64::from_str(line[1])
                            .block_error("memory", "failed to parse mem_total")?,
                        true,
                    );
                    continue;
                }
                Some(&"MemFree:") => {
                    mem_state.mem_free = (
                        u64::from_str(line[1]).block_error("memory", "failed to parse mem_free")?,
                        true,
                    );
                    continue;
                }
                Some(&"Buffers:") => {
                    mem_state.buffers = (
                        u64::from_str(line[1]).block_error("memory", "failed to parse buffers")?,
                        true,
                    );
                    continue;
                }
                Some(&"Cached:") => {
                    mem_state.cached = (
                        u64::from_str(line[1]).block_error("memory", "failed to parse cached")?,
                        true,
                    );
                    continue;
                }
                Some(&"SReclaimable:") => {
                    mem_state.s_reclaimable = (
                        u64::from_str(line[1])
                            .block_error("memory", "failed to parse s_reclaimable")?,
                        true,
                    );
                    continue;
                }
                Some(&"Shmem:") => {
                    mem_state.shmem = (
                        u64::from_str(line[1]).block_error("memory", "failed to parse shmem")?,
                        true,
                    );
                    continue;
                }
                Some(&"SwapTotal:") => {
                    mem_state.swap_total = (
                        u64::from_str(line[1])
                            .block_error("memory", "failed to parse swap_total")?,
                        true,
                    );
                    continue;
                }
                Some(&"SwapFree:") => {
                    mem_state.swap_free = (
                        u64::from_str(line[1])
                            .block_error("memory", "failed to parse swap_free")?,
                        true,
                    );
                    continue;
                }
                _ => {
                    continue;
                }
            }
        }

        // Now, create the string to be shown
        let output_text = self.format_insert_values(mem_state)?;

        match self.memtype {
            Memtype::Memory => self.output.0.set_text(output_text),
            Memtype::Swap => self.output.1.set_text(output_text),
        }

        Ok(Some(self.update_interval.into()))
    }

    fn click(&mut self, event: &I3BarEvent) -> Result<()> {
        if event.button == MouseButton::Left && self.clickable {
            self.switch();
            self.update()?;
            self.tx_update_request.send(Task {
                id: self.id,
                update_time: Instant::now(),
            })?;
        }

        Ok(())
    }

    fn view(&self) -> Vec<&dyn I3BarWidget> {
        vec![match self.memtype {
            Memtype::Memory => &self.output.0,
            Memtype::Swap => &self.output.1,
        }]
    }
}