qmassa 0.6.4

Terminal-based tool for displaying GPUs usage stats on Linux.
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
use std::collections::{VecDeque, HashMap};
use std::cell::{RefCell, RefMut};
use std::path::PathBuf;
use std::rc::{Rc, Weak};
use std::time;

use anyhow::{bail, Result};
use log::{debug, warn};
use serde::{Deserialize, Serialize};

use crate::proc_info::ProcInfo;
use crate::drm_fdinfo::{DrmEngine, DrmMemRegion, DrmFdinfo};
use crate::drm_drivers::DrmDriver;


#[derive(Debug)]
pub struct DrmEnginesAcum
{
    pub acum_time: u64,
    pub acum_cycles: u64,
    pub acum_total_cycles: u64,
}

impl DrmEnginesAcum
{
    pub fn new() -> DrmEnginesAcum
    {
        DrmEnginesAcum {
            acum_time: 0,
            acum_cycles: 0,
            acum_total_cycles: 0,
        }
    }
}

#[derive(Debug)]
pub struct DrmEngineDelta
{
    pub delta_time: u64,
    pub delta_cycles: u64,
    pub delta_total_cycles: u64,
}

impl DrmEngineDelta
{
    pub fn new() -> DrmEngineDelta
    {
        DrmEngineDelta {
            delta_time: 0,
            delta_cycles: 0,
            delta_total_cycles: 0,
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DrmClientMemInfo
{
    pub smem_used: u64,
    pub smem_rss: u64,
    pub vram_used: u64,
    pub vram_rss: u64,
}

impl DrmClientMemInfo
{
    pub fn new() -> DrmClientMemInfo
    {
        DrmClientMemInfo {
            smem_used: 0,
            smem_rss: 0,
            vram_used: 0,
            vram_rss: 0,
        }
    }
}

#[derive(Debug)]
#[allow(dead_code)]
pub struct DrmClientInfo
{
    pub pci_dev: String,
    pub drm_minor: u32,
    pub client_id: u32,
    pub proc: ProcInfo,
    pub fdinfo_path: PathBuf,
    pub shared_procs: Vec<(ProcInfo, PathBuf)>,
    engs_last: HashMap<String, DrmEngine>,
    engs_delta: HashMap<String, DrmEngineDelta>,
    engs_updates: HashMap<String, u64>,
    engs_acum: DrmEnginesAcum,
    mem_regions: HashMap<String, DrmMemRegion>,
    nr_updates: u64,
    ms_elapsed: u64,
    last_update: time::Instant,
    driver: Option<Weak<RefCell<dyn DrmDriver>>>,
}

impl Default for DrmClientInfo
{
    fn default() -> DrmClientInfo
    {
        DrmClientInfo {
            pci_dev: String::new(),
            drm_minor: 0,
            client_id: 0,
            proc: ProcInfo::default(),
            fdinfo_path: PathBuf::new(),
            shared_procs: Vec::new(),
            engs_last: HashMap::new(),
            engs_delta: HashMap::new(),
            engs_updates: HashMap::new(),
            engs_acum: DrmEnginesAcum::new(),
            mem_regions: HashMap::new(),
            nr_updates: 0,
            ms_elapsed: 0,
            last_update: time::Instant::now(),
            driver: None,
        }
    }
}

impl DrmClientInfo
{
    pub fn mem_info(&self) -> DrmClientMemInfo
    {
        if let Some(w_ref) = &self.driver {
            if let Some(drv_ref) = w_ref.upgrade() {
                let mut drv_b = drv_ref.borrow_mut();
                if let Ok(res) = drv_b.client_mem_info(&self.mem_regions) {
                    return res;
                }
            }
        } else if let Some(mrg) = self.mem_regions.get("memory") {
            let mut cmi = DrmClientMemInfo::new();
            cmi.smem_rss = mrg.resident;
            cmi.smem_used = mrg.total;
            return cmi;
        }

        DrmClientMemInfo::new()
    }

    pub fn eng_utilization(&self, eng: &String) -> f64
    {
        if !self.engs_last.contains_key(eng) {
            return 0.0;
        }
        if *self.engs_updates.get(eng).unwrap() < 2 {
            return 0.0;
        }

        let acum = &self.engs_acum;
        if acum.acum_time == 0 && acum.acum_cycles == 0 {
            return 0.0;
        }

        let ed = self.engs_delta.get(eng).unwrap();
        let cap = self.engs_last.get(eng).unwrap().capacity as f64;

        let mut res: f64 = 0.0;
        if acum.acum_cycles > 0 && ed.delta_total_cycles > 0 {
            res = (ed.delta_cycles as f64 * 100.0) /
                (ed.delta_total_cycles as f64 * cap);
        } else if acum.acum_time > 0 {
            res = ((ed.delta_time as f64 / 1000000.0) * 100.0) /
                (self.ms_elapsed as f64 * cap);
        }

        if res > 100.0 {
            warn!("Engine {:?} utilization at {:.1}%, clamped to 100%.",
                eng, res);
            res = 100.0;
        }
        res
    }

    pub fn engines(&self) -> Vec<&String>
    {
        let mut res: Vec<&String> = self.engs_delta.keys().collect::<Vec<&_>>();
        res.sort();

        res
    }

    fn total_mem(&self) -> u64
    {
        let mut tot: u64 = 0;
        for reg in self.mem_regions.values() {
            tot += reg.total;
        }

        tot
    }

    pub fn is_active(&self) -> bool
    {
        let acum = &self.engs_acum;
        if acum.acum_time > 0 ||
            (acum.acum_cycles > 0 && acum.acum_total_cycles > 0) {
            return true;
        }

        if self.total_mem() > 0 {
            return true;
        }

        false
    }

    pub fn update(&mut self, pinfo: ProcInfo, fdi: DrmFdinfo)
    {
        // handle process and fdinfo path updates
        if self.proc != pinfo {
            self.proc = pinfo;  // fd might be shared
        }
        if let Err(err) = self.proc.update() {
            debug!("ERR: failed to update process info for {:?}: {:?}",
                self.proc, err);
        }
        self.fdinfo_path = fdi.path;

        // handle new engines showing up in a client's DRM fdinfo
        // or the very unlikely (not possible?) removal of an engine
        for nm in fdi.engines.keys() {
            if !self.engs_last.contains_key(nm) {
                self.engs_last.insert(nm.clone(),
                    DrmEngine::from(&fdi.engines[nm]));
                self.engs_delta.insert(nm.clone(), DrmEngineDelta::new());
                self.engs_updates.insert(nm.clone(), 0);
            }
        }
        self.engs_last.retain(|k, _| fdi.engines.contains_key(k));
        self.engs_delta.retain(|k, _| fdi.engines.contains_key(k));
        self.engs_updates.retain(|k, _| fdi.engines.contains_key(k));

        // handle engines and mem regions updates
        self.engs_acum.acum_time = 0;
        self.engs_acum.acum_cycles = 0;
        self.engs_acum.acum_total_cycles = 0;

        for (nm, oeng) in self.engs_last.iter_mut() {
            let deng = self.engs_delta.get_mut(nm).unwrap();
            let neng = fdi.engines.get(nm).unwrap();

            if neng.time >= oeng.time {
                self.engs_acum.acum_time += neng.time;
                deng.delta_time = neng.time - oeng.time;
                oeng.time = neng.time;
            }

            if neng.cycles >= oeng.cycles {
                self.engs_acum.acum_cycles += neng.cycles;
                deng.delta_cycles = neng.cycles - oeng.cycles;
                oeng.cycles = neng.cycles;
            }

            if neng.total_cycles >= oeng.total_cycles {
                self.engs_acum.acum_total_cycles += neng.total_cycles;
                deng.delta_total_cycles = neng.total_cycles - oeng.total_cycles;
                oeng.total_cycles = neng.total_cycles;
            }

            self.engs_updates.entry(nm.clone()).and_modify(|nr| *nr += 1);
        }
        self.mem_regions = fdi.mem_regions;

        // one more update for this DRM client
        self.ms_elapsed = self.last_update.elapsed().as_millis() as u64;
        self.last_update = time::Instant::now();
        self.nr_updates += 1;
    }

    pub fn set_driver(&mut self, drv_wref: Weak<RefCell<dyn DrmDriver>>)
    {
        self.driver = Some(drv_wref);
    }

    pub fn from(pinfo: ProcInfo, fdi: DrmFdinfo) -> DrmClientInfo
    {
        let mut cli = DrmClientInfo {
            pci_dev: fdi.pci_dev.clone(),
            drm_minor: fdi.drm_minor,
            client_id: fdi.client_id,
            ..Default::default()
        };

        for nm in fdi.engines.keys() {
            cli.engs_last.insert(nm.clone(),
                DrmEngine::from(&fdi.engines[nm]));
            cli.engs_delta.insert(nm.clone(), DrmEngineDelta::new());
            cli.engs_updates.insert(nm.clone(), 0);
        }

        cli.update(pinfo, fdi);

        cli
    }
}

#[derive(Debug)]
pub struct DrmClients
{
    base_pid: String,
    infos: HashMap<String, Rc<RefCell<Vec<DrmClientInfo>>>>,
}

impl DrmClients
{
    pub fn set_dev_clients_driver(&mut self,
        dev: &String, drv_wref: Weak<RefCell<dyn DrmDriver>>)
    {
        if !self.infos.contains_key(dev) {
            return;
        }
        let mut vlst = self.infos.get_mut(dev).unwrap().borrow_mut();

        for cliref in vlst.iter_mut() {
            cliref.set_driver(Weak::clone(&drv_wref));
        }
    }

    pub fn device_clients(&self,
        dev: &String) -> Option<Rc<RefCell<Vec<DrmClientInfo>>>>
    {
        if let Some(vref) = self.infos.get(dev) {
            return Some(vref.clone());
        }

        None
    }

    fn map_has_client<'a>(map: &'a mut HashMap<String,
        Rc<RefCell<Vec<DrmClientInfo>>>>, dev: &'a String,
        minor: u32, id: u32) -> Option<RefMut<'a, DrmClientInfo>>
    {
        if !map.contains_key(dev) {
            return None;
        }
        let vlst = map.get_mut(dev).unwrap().borrow_mut();

        let mut idx = 0;
        for cliref in vlst.iter() {
            if cliref.drm_minor == minor && cliref.client_id == id {
                break;
            }
            idx += 1;
        }

        if idx >= vlst.len() {
            return None;
        }

        Some(RefMut::map(vlst, |v| &mut v[idx]))
    }

    fn map_remove_client(map: &mut HashMap<String,
        Rc<RefCell<Vec<DrmClientInfo>>>>, dev: &String,
        minor: u32, id: u32) -> Option<DrmClientInfo>
    {
        if !map.contains_key(dev) {
            return None
        }
        let mut vlst = map.get(dev).unwrap().borrow_mut();

        let mut idx = 0;
        for cliref in vlst.iter() {
            if cliref.drm_minor == minor && cliref.client_id == id {
                break;
            }
            idx += 1;
        }

        if idx >= vlst.len() {
            return None;
        }

        Some(vlst.swap_remove(idx))
    }

    fn map_insert_client(map: &mut HashMap<String,
        Rc<RefCell<Vec<DrmClientInfo>>>>, dev: String, cli: DrmClientInfo)
    {
        if !map.contains_key(&dev) {
            let mut vlst: Vec<DrmClientInfo> = Vec::new();
            vlst.push(cli);
            map.insert(dev, Rc::new(RefCell::new(vlst)));
        } else {
            let mut vref = map.get(&dev).unwrap().borrow_mut();
            vref.push(cli);
        }
    }

    fn process_fdinfos(&mut self,
        ninfos: &mut HashMap<String, Rc<RefCell<Vec<DrmClientInfo>>>>,
        nproc: &ProcInfo, fdinfos: Vec<DrmFdinfo>)
    {
        for fdi in fdinfos {
            if let Some(mut cliref) = DrmClients::map_has_client(ninfos,
                &fdi.pci_dev, fdi.drm_minor, fdi.client_id) {
                cliref.shared_procs.push((nproc.clone(), fdi.path));
                debug!("INF: repeated drm client/fd info: proc={:?}, drm-minor={:?}, drm-client-id={:?}", nproc, fdi.drm_minor, fdi.client_id);
                continue;
            }

            let pci_dev = fdi.pci_dev.clone();
            if let Some(mut cli) = DrmClients::map_remove_client(
                &mut self.infos, &fdi.pci_dev, fdi.drm_minor, fdi.client_id) {
                cli.update(nproc.clone(), fdi);
                DrmClients::map_insert_client(ninfos, pci_dev, cli);
            } else {
                let cli = DrmClientInfo::from(nproc.clone(), fdi);
                DrmClients::map_insert_client(ninfos, pci_dev, cli);
            }
        }
    }

    fn scan_all_pids(&mut self) -> Result<()>
    {
        let mut ninfos: HashMap<String,
            Rc<RefCell<Vec<DrmClientInfo>>>> = HashMap::new();

        let proc_iter = ProcInfo::iter_proc_pids();
        if let Err(err) = proc_iter {
            debug!("ERR: couldn't get pids info in /proc: {:?}", err);
        } else {
            let proc_iter = proc_iter.unwrap();
            for nproc in proc_iter {
                // got next process info
                if let Err(err) = nproc {
                    debug!("ERR: error iterating through /proc pids: {:?}",
                        err);
                    break;
                }
                let nproc = nproc.unwrap();

                // search and parse all DRM fdinfo from npid process
                let fdinfos = nproc.drm_fdinfos();
                if let Err(err) = fdinfos {
                    debug!("ERR: failed to get DRM fdinfos from {:?}: {:?}",
                        nproc.pid, err);
                    continue;
                }
                let fdinfos = fdinfos.unwrap();

                // sort out DRM client infos based on DRM fdinfos
                self.process_fdinfos(&mut ninfos, &nproc, fdinfos);
            }
        }

        // update DRM client infos
        self.infos = ninfos;

        Ok(())
    }

    fn scan_pid_tree(&mut self) -> Result<()>
    {
        let mut ninfos: HashMap<String,
            Rc<RefCell<Vec<DrmClientInfo>>>> = HashMap::new();
        let mut pidq = VecDeque::from([self.base_pid.clone(),]);

        while !pidq.is_empty() {
            let npid = pidq.pop_front().unwrap();

            // new process info
            let nproc = ProcInfo::from(&npid);
            if let Err(err) = nproc {
                debug!("ERR: Couldn't get proc info for {:?}: {:?}", npid, err);
                continue;
            }
            let nproc = nproc.unwrap();

            // search and parse all DRM fdinfo from npid process
            let fdinfos = nproc.drm_fdinfos();
            if let Err(err) = fdinfos {
                debug!("ERR: failed to get DRM fdinfos from {:?}: {:?}",
                    npid, err);
                continue;
            }
            let fdinfos = fdinfos.unwrap();

            // sort out DRM client infos based on DRM fdinfos
            self.process_fdinfos(&mut ninfos, &nproc, fdinfos);

            // add all child processes
            let chids = nproc.children_pids();
            if let Err(err) = chids {
                debug!("ERR: failed to get children pids for {:?}: {:?}",
                    npid, err);
            } else {
                let mut chids = chids.unwrap();
                pidq.append(&mut chids);
            }
        }

        // update DRM client infos
        self.infos = ninfos;

        Ok(())
    }

    pub fn refresh(&mut self) -> Result<()>
    {
        if self.base_pid.is_empty() {
            self.scan_all_pids()?;
        } else {
            self.scan_pid_tree()?;
        }

        for vref in self.infos.values_mut() {
            let mut vcli = vref.borrow_mut();
            vcli.sort_by(|a, b| {
                if a.drm_minor == b.drm_minor {
                    a.client_id.cmp(&b.client_id)
                } else {
                    a.drm_minor.cmp(&b.drm_minor)
                }
            });
        }

        Ok(())
    }

    pub fn from_pid_tree(at_pid: &str) -> Result<DrmClients>
    {
        if !at_pid.is_empty() && !ProcInfo::is_valid_pid(at_pid) {
            bail!("Not a valid PID: {}", at_pid);
        }

        Ok(DrmClients {
            base_pid: at_pid.to_string(),
            infos: HashMap::new(),
        })
    }
}