retina_core/lcore/
monitor.rs

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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use crate::config::RuntimeConfig;
use crate::dpdk;
use crate::port::{statistics::PortStats, Port, PortId, RxQueue, RxQueueType};

use std::collections::{BTreeMap, HashMap};
use std::ffi::CString;
use std::fmt;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::{bail, Result};
use chrono::Local;
use csv::Writer;
use serde::Serialize;

/// Preamble + Start Frame Delimiter
const PSFD_SIZE: u64 = 8;
/// Interpacket Gap
const IPG_SIZE: u64 = 12;
/// Frame Checksum
const FCS_SIZE: u64 = 4;

/// A Monitor monitors throughput when running online, displays live statistics
#[derive(Debug)]
pub(crate) struct Monitor {
    duration: Option<Duration>,
    display: Option<Display>,
    logger: Option<Logger>,
    ports: BTreeMap<PortId, Vec<RxQueue>>,
    is_running: Arc<AtomicBool>,
}

impl Monitor {
    pub(crate) fn new(
        config: &RuntimeConfig,
        ports: &BTreeMap<PortId, Port>,
        is_running: Arc<AtomicBool>,
    ) -> Self {
        let date = Local::now();
        let online_cfg = config
            .online
            .as_ref()
            .expect("Not configured for online runtime");

        let duration = online_cfg.duration.map(Duration::from_secs);

        let display = (|| {
            if let Some(monitor_cfg) = &online_cfg.monitor {
                if let Some(display_cfg) = &monitor_cfg.display {
                    return Some(Display {
                        throughput: display_cfg.throughput,
                        keywords: display_cfg.port_stats.clone(),
                    });
                }
            }
            None
        })();

        let logger = (|| {
            if let Some(monitor_cfg) = &online_cfg.monitor {
                if let Some(log_cfg) = &monitor_cfg.log {
                    let path = Path::new(&log_cfg.directory)
                        .join(date.format("%Y-%m-%dT%H:%M:%S").to_string());
                    fs::create_dir_all(&path).expect("create log directory");
                    log::info!("Logging to {:?}", path);

                    let toml = toml::to_string(&config).expect("serialize config");
                    let mut config_file =
                        fs::File::create(path.join("config.toml")).expect("create config log");
                    config_file.write_all(toml.as_bytes()).expect("log config");

                    let mut port_wtrs = hashmap! {};
                    for port_id in ports.keys() {
                        let fname = path.join(format!("port{}.csv", port_id));
                        let wtr = Writer::from_path(&fname).expect("create portstat log");
                        port_wtrs.insert(*port_id, wtr);
                    }
                    return Some(Logger {
                        interval: Duration::from_millis(log_cfg.interval),
                        path,
                        port_wtrs,
                        keywords: log_cfg.port_stats.clone(),
                    });
                }
            }
            None
        })();

        let mut monitor_ports: BTreeMap<PortId, Vec<RxQueue>> = BTreeMap::new();
        for (port_id, port) in ports.iter() {
            monitor_ports.insert(*port_id, port.queue_map.keys().cloned().collect());
        }

        Monitor {
            duration,
            display,
            logger,
            ports: monitor_ports,
            is_running,
        }
    }

    pub(crate) async fn run(&mut self) {
        if let Some(logger) = &mut self.logger {
            logger.init_port_wtrs().expect("port logger init");
        }
        // ts of run start
        let start_ts = Instant::now();
        // initial data capture
        let mut init_rx = AggRxStats::default();
        // ts of initial data capture
        let mut init_ts = start_ts;

        let mut prev_rx = init_rx;
        let mut prev_ts = init_ts;
        let mut init = true;
        let mut display_ticker = tokio::time::interval(Duration::from_millis(1000));

        let mut logger_ticker = self
            .logger
            .as_ref()
            .map(|logger| tokio::time::interval(logger.interval));
        // Add a small delay to allow workers to start polling for packets
        tokio::time::sleep(Duration::from_millis(1000)).await;
        while self.is_running.load(Ordering::Relaxed) {
            if let Some(duration) = self.duration {
                if start_ts.elapsed() >= duration {
                    self.is_running.store(false, Ordering::Relaxed);
                }
            }

            if let Some(display) = &self.display {
                display_ticker.tick().await;
                let curr_ts = Instant::now();
                let delta = curr_ts - prev_ts;
                match AggRxStats::collect(&self.ports, &display.keywords) {
                    Ok(curr_rx) => {
                        #[cfg(feature = "prometheus")]
                        curr_rx.update_prometheus_stats();
                        let nms = delta.as_millis() as f64;
                        if init {
                            init_rx = curr_rx;
                            init_ts = curr_ts;
                            init = false;
                        }
                        if display.throughput {
                            let elapsed_ts = curr_ts - start_ts;
                            println!("----------------------------------------------");
                            println!("Current time: {}", pretty_print_duration(elapsed_ts));
                            display.mempool_usage(&self.ports);
                            AggRxStats::display_rates(curr_rx, prev_rx, nms);
                            AggRxStats::display_dropped(curr_rx, init_rx);
                        }
                        prev_rx = curr_rx;
                        prev_ts = curr_ts;
                    }
                    Err(error) => {
                        log::error!("Monitor display error: {}", error);
                    }
                }
            }

            if let Some(logger) = &mut self.logger {
                logger_ticker.as_mut().unwrap().tick().await;
                match logger.log_stats(init_ts.elapsed()) {
                    Ok(_) => (),
                    Err(error) => log::error!("Monitor log error: {}", error),
                }
            }
        }

        tokio::time::sleep(Duration::from_millis(1000)).await;
        println!("----------------------------------------------");
        let tputs = Throughputs::new(prev_rx, init_rx, (prev_ts - init_ts).as_millis() as f64);
        println!("{}", tputs);

        if let Some(logger) = &self.logger {
            let json_fname = logger.path.join("throughputs.json");
            tputs.dump_json(json_fname).expect("Unable to dump to json");
        }
    }
}

#[derive(Debug)]
struct Display {
    throughput: bool,
    keywords: Vec<String>,
}

impl Display {
    /// Display mempool usage
    fn mempool_usage(&self, ports: &BTreeMap<PortId, Vec<RxQueue>>) {
        for name in ports.keys().map(|id| format!("mempool_{}", id.socket_id())) {
            let cname = CString::new(name.clone()).expect("Invalid CString conversion");
            let mempool_raw = unsafe { dpdk::rte_mempool_lookup(cname.as_ptr()) };
            let avail_cnt = unsafe { dpdk::rte_mempool_avail_count(mempool_raw) };
            let inuse_cnt = unsafe { dpdk::rte_mempool_in_use_count(mempool_raw) };

            println!(
                "{} avail: {}, in use: {} ({:.3}%)",
                name,
                avail_cnt,
                inuse_cnt,
                100.0 * inuse_cnt as f64 / (inuse_cnt + avail_cnt) as f64
            );
        }
    }
}

#[derive(Debug)]
struct Logger {
    interval: Duration,
    path: PathBuf,
    port_wtrs: HashMap<PortId, Writer<std::fs::File>>,
    keywords: Vec<String>,
}

impl Logger {
    /// Initialize port statistic CSV writers. Must occur after ports have been started.
    fn init_port_wtrs(&mut self) -> Result<()> {
        for (port_id, wtr) in self.port_wtrs.iter_mut() {
            let port_stats = PortStats::collect(*port_id)?;
            wtr.write_field("ts")?;
            for label in port_stats.stats.keys() {
                if self.keywords.iter().any(|k| label.contains(k)) {
                    wtr.write_field(label)?;
                }
            }
            wtr.write_field("mempool_avail_cnt")?;
            wtr.write_field("mempool_inuse_cnt")?;
            wtr.write_record(None::<&[u8]>)?;
            wtr.flush()?;
        }
        Ok(())
    }

    /// Logs per-port statistics and mempool statistics (per-socket statistics).
    fn log_stats(&mut self, elapsed: Duration) -> Result<()> {
        for (port_id, wtr) in self.port_wtrs.iter_mut() {
            let port_stats = PortStats::collect(*port_id);
            match port_stats {
                Ok(port_stats) => {
                    wtr.write_field(elapsed.as_millis().to_string())?;
                    for label in port_stats.stats.keys() {
                        if self.keywords.iter().any(|k| label.contains(k)) {
                            if let Some(value) = port_stats.stats.get(label) {
                                wtr.write_field(value.to_string())?;
                            } else {
                                wtr.write_field("-")?;
                            }
                        }
                    }
                }
                Err(error) => log::error!("{}", error),
            }
            let name = format!("mempool_{}", port_id.socket_id());
            let cname = CString::new(name.clone()).expect("Invalid CString conversion");
            let mempool_raw = unsafe { dpdk::rte_mempool_lookup(cname.as_ptr()) };
            let avail_cnt = unsafe { dpdk::rte_mempool_avail_count(mempool_raw) };
            let inuse_cnt = unsafe { dpdk::rte_mempool_in_use_count(mempool_raw) };
            wtr.write_field(avail_cnt.to_string())?;
            wtr.write_field(inuse_cnt.to_string())?;
            wtr.write_record(None::<&[u8]>)?;
        }
        for wtr in self.port_wtrs.values_mut() {
            wtr.flush()?;
        }
        Ok(())
    }
}

/// Aggregate RX port statistics at time of collection
#[derive(Debug, Default, Clone, Copy)]
struct AggRxStats {
    ingress_bits: u64,
    ingress_pkts: u64,
    good_bits: u64,
    good_pkts: u64,
    process_bits: u64,
    process_pkts: u64,
    hw_dropped_pkts: u64,
    sw_dropped_pkts: u64,
}

impl AggRxStats {
    /// Collect aggregate statistics, display keyword statistics if `keywords` is not `None`
    fn collect(ports: &BTreeMap<PortId, Vec<RxQueue>>, keywords: &[String]) -> Result<Self> {
        let mut ingress_bytes = 0;
        let mut ingress_pkts = 0;
        let mut good_bytes = 0;
        let mut good_pkts = 0;
        let mut process_bytes = 0;
        let mut process_pkts = 0;
        let mut hw_dropped_pkts = 0;
        let mut sw_dropped_pkts = 0;
        for (port_id, rx_queues) in ports.iter() {
            let mut sink_queue = None;
            for queue in rx_queues {
                if queue.ty == RxQueueType::Sink {
                    sink_queue = Some(queue.qid.raw());
                }
            }

            match PortStats::collect(*port_id) {
                Ok(port_stats) => {
                    // Ingress (reached NIC)
                    ingress_bytes += match port_stats.stats.get("rx_phy_bytes") {
                        Some(v) => *v,
                        None => {
                            log::warn!("Failed retrieving ingress_bytes, device does not support precise PHY count");
                            0
                        }
                    };
                    ingress_pkts += match port_stats.stats.get("rx_phy_packets") {
                        Some(v) => *v,
                        None => {
                            log::warn!("Failed retrieving ingress_pkts, device does not support precise PHY count");
                            0
                        }
                    };

                    // Good (reached software)
                    let good_bytes_temp = match port_stats.stats.get("rx_good_bytes") {
                        Some(v) => *v,
                        None => {
                            log::warn!("Failed retrieving good_bytes, device does not support precise PHY count");
                            0
                        }
                    };
                    let good_pkts_temp = match port_stats.stats.get("rx_good_packets") {
                        Some(v) => *v,
                        None => {
                            log::warn!("Failed retrieving good_pkts, device does not support precise PHY count");
                            0
                        }
                    };
                    good_bytes += good_bytes_temp;
                    good_pkts += good_pkts_temp;

                    // Process (reached workers)
                    process_bytes += if let Some(sink) = sink_queue {
                        let label = format!("rx_q{}_bytes", sink);
                        let sink_bytes = match port_stats.stats.get(&label) {
                            Some(v) => *v,
                            None => bail!("Failed retrieving sink_bytes"),
                        };
                        good_bytes_temp - sink_bytes
                    } else {
                        good_bytes_temp
                    };
                    process_pkts += if let Some(sink) = sink_queue {
                        let label = format!("rx_q{}_packets", sink);
                        let sink_pkts = match port_stats.stats.get(&label) {
                            Some(v) => *v,
                            None => bail!("Failed retrieving sink_pkts"),
                        };
                        good_pkts_temp - sink_pkts
                    } else {
                        good_pkts_temp
                    };

                    // dropped
                    hw_dropped_pkts += match port_stats.stats.get("rx_phy_discard_packets") {
                        Some(v) => *v,
                        None => {
                            log::warn!("Failed retrieving hw_dropped_pkts, device does not support precise packet dropped counter (no hardware drop will be accounted for).");
                            0
                        }
                    };
                    sw_dropped_pkts += match port_stats.stats.get("rx_missed_errors") {
                        Some(v) => *v,
                        None => bail!("Failed retrieving sw_dropped_pkts"),
                    };

                    port_stats.display(keywords);
                }
                Err(error) => bail!(error),
            }
        }
        Ok(AggRxStats {
            ingress_bits: (ingress_bytes + (PSFD_SIZE + IPG_SIZE) * ingress_pkts) * 8,
            ingress_pkts,
            good_bits: (good_bytes + (PSFD_SIZE + IPG_SIZE + FCS_SIZE) * good_pkts) * 8,
            good_pkts,
            process_bits: (process_bytes + (PSFD_SIZE + IPG_SIZE + FCS_SIZE) * process_pkts) * 8,
            process_pkts,
            hw_dropped_pkts,
            sw_dropped_pkts,
        })
    }

    /// Display live bits per second and packets per second between `curr_rx` and `prev_rx`
    fn display_rates(curr_rx: AggRxStats, prev_rx: AggRxStats, nms: f64) {
        println!(
            "Ingress: {} / {}",
            pretty_print_unit(
                (curr_rx.ingress_bits - prev_rx.ingress_bits) as f64 / nms * 1000.0,
                "bps",
            ),
            pretty_print_unit(
                (curr_rx.ingress_pkts - prev_rx.ingress_pkts) as f64 / nms * 1000.0,
                "pps",
            ),
        );
        println!(
            "Good:    {} / {}",
            pretty_print_unit(
                (curr_rx.good_bits - prev_rx.good_bits) as f64 / nms * 1000.0,
                "bps",
            ),
            pretty_print_unit(
                (curr_rx.good_pkts - prev_rx.good_pkts) as f64 / nms * 1000.0,
                "pps",
            ),
        );
        println!(
            "Process: {} / {}",
            pretty_print_unit(
                (curr_rx.process_bits - prev_rx.process_bits) as f64 / nms * 1000.0,
                "bps",
            ),
            pretty_print_unit(
                (curr_rx.process_pkts - prev_rx.process_pkts) as f64 / nms * 1000.0,
                "pps",
            ),
        );
        println!(
            "Drop: {} ({}%)",
            pretty_print_unit(
                (curr_rx.dropped_pkts() - prev_rx.dropped_pkts()) as f64 / nms * 1000.0,
                "pps",
            ),
            100.0
                * ((curr_rx.dropped_pkts() - prev_rx.dropped_pkts()) as f64
                    / (curr_rx.ingress_pkts - prev_rx.ingress_pkts) as f64)
        );
    }

    fn display_dropped(curr_rx: AggRxStats, init_rx: AggRxStats) {
        println!(
            "HW Dropped: {} ({}%)",
            pretty_print_unit(
                (curr_rx.hw_dropped_pkts - init_rx.hw_dropped_pkts) as f64,
                "pkt",
            ),
            100.0
                * ((curr_rx.hw_dropped_pkts - init_rx.hw_dropped_pkts) as f64
                    / (curr_rx.ingress_pkts - init_rx.ingress_pkts) as f64)
        );
        println!(
            "SW Dropped: {} ({}%)",
            pretty_print_unit(
                (curr_rx.sw_dropped_pkts - init_rx.sw_dropped_pkts) as f64,
                "pkt",
            ),
            100.0
                * ((curr_rx.sw_dropped_pkts - init_rx.sw_dropped_pkts) as f64
                    / (curr_rx.ingress_pkts - init_rx.ingress_pkts) as f64)
        );
        println!(
            "Total Dropped: {} ({}%)",
            pretty_print_unit(
                (curr_rx.dropped_pkts() - init_rx.dropped_pkts()) as f64,
                "pkt",
            ),
            100.0
                * ((curr_rx.dropped_pkts() - init_rx.dropped_pkts()) as f64
                    / (curr_rx.ingress_pkts - init_rx.ingress_pkts) as f64)
        );
        println!(
            "Total Packets: {}",
            pretty_print_unit((curr_rx.ingress_pkts - init_rx.ingress_pkts) as f64, "pkt",),
        );
    }

    fn dropped_pkts(&self) -> u64 {
        self.hw_dropped_pkts + self.sw_dropped_pkts
    }

    #[cfg(feature = "prometheus")]
    fn update_prometheus_stats(&self) {
        use crate::stats::DPDK_STATS;
        DPDK_STATS
            .ingress_pkts
            .inc_by(self.ingress_pkts - DPDK_STATS.ingress_pkts.get());
        DPDK_STATS
            .ingress_bits
            .inc_by(self.ingress_bits - DPDK_STATS.ingress_bits.get());
        DPDK_STATS
            .good_pkts
            .inc_by(self.good_pkts - DPDK_STATS.good_pkts.get());
        DPDK_STATS
            .good_bits
            .inc_by(self.good_bits - DPDK_STATS.good_bits.get());
        DPDK_STATS
            .process_pkts
            .inc_by(self.process_pkts - DPDK_STATS.process_pkts.get());
        DPDK_STATS
            .process_bits
            .inc_by(self.process_bits - DPDK_STATS.process_bits.get());
        DPDK_STATS
            .hw_dropped_pkts
            .inc_by(self.hw_dropped_pkts - DPDK_STATS.hw_dropped_pkts.get());
        DPDK_STATS
            .sw_dropped_pkts
            .inc_by(self.sw_dropped_pkts - DPDK_STATS.sw_dropped_pkts.get());
    }
}

#[derive(Debug, Serialize)]
struct Throughputs {
    avg_ingress_bps: f64,
    avg_ingress_pps: f64,
    avg_good_bps: f64,
    avg_good_pps: f64,
    avg_process_bps: f64,
    avg_process_pps: f64,
    hw_dropped_pkts: u64,
    sw_dropped_pkts: u64,
    tot_dropped_pkts: u64,
    percent_dropped: f64,
}

impl Throughputs {
    /// Compute average rates over elapsed time
    fn new(curr_rx: AggRxStats, init_rx: AggRxStats, ems: f64) -> Self {
        Throughputs {
            avg_ingress_bps: (curr_rx.ingress_bits - init_rx.ingress_bits) as f64 / ems * 1000.0,
            avg_ingress_pps: (curr_rx.ingress_pkts - init_rx.ingress_pkts) as f64 / ems * 1000.0,
            avg_good_bps: (curr_rx.good_bits - init_rx.good_bits) as f64 / ems * 1000.0,
            avg_good_pps: (curr_rx.good_pkts - init_rx.good_pkts) as f64 / ems * 1000.0,
            avg_process_bps: (curr_rx.process_bits - init_rx.process_bits) as f64 / ems * 1000.0,
            avg_process_pps: (curr_rx.process_pkts - init_rx.process_pkts) as f64 / ems * 1000.0,
            hw_dropped_pkts: (curr_rx.hw_dropped_pkts - init_rx.hw_dropped_pkts),
            sw_dropped_pkts: (curr_rx.sw_dropped_pkts - init_rx.sw_dropped_pkts),
            tot_dropped_pkts: (curr_rx.dropped_pkts() - init_rx.dropped_pkts()),
            percent_dropped: 100.0
                * ((curr_rx.dropped_pkts() - init_rx.dropped_pkts()) as f64
                    / (curr_rx.ingress_pkts - init_rx.ingress_pkts) as f64),
        }
    }

    fn dump_json(&self, path: PathBuf) -> Result<()> {
        let file = std::fs::File::create(path)?;
        serde_json::to_writer(&file, self)?;
        Ok(())
    }
}

impl fmt::Display for Throughputs {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(
            f,
            "AVERAGE Ingress: {} / {}",
            pretty_print_unit(self.avg_ingress_bps, "bps"),
            pretty_print_unit(self.avg_ingress_pps, "pps"),
        )?;
        writeln!(
            f,
            "AVERAGE Good:    {} / {}",
            pretty_print_unit(self.avg_good_bps, "bps"),
            pretty_print_unit(self.avg_good_pps, "pps"),
        )?;
        writeln!(
            f,
            "AVERAGE Process: {} / {}",
            pretty_print_unit(self.avg_process_bps, "bps"),
            pretty_print_unit(self.avg_process_pps, "pps"),
        )?;
        writeln!(
            f,
            "DROPPED: {} ({}%)",
            pretty_print_unit(self.tot_dropped_pkts as f64, "pkt"),
            self.percent_dropped,
        )?;
        Ok(())
    }
}

fn pretty_print_unit(mut value: f64, unit: &str) -> String {
    let kilo_coef = 1000.;
    let mut unit_prefix = "";
    if value > kilo_coef {
        value /= kilo_coef;
        unit_prefix = "k";
        if value > kilo_coef {
            value /= kilo_coef;
            unit_prefix = "M";
            if value > kilo_coef {
                value /= kilo_coef;
                unit_prefix = "G";
            }
        }
    }
    format!("{value:.4} {unit_prefix}{unit}")
}

fn pretty_print_duration(duration: Duration) -> String {
    let total_seconds = duration.as_secs();
    let days = total_seconds / 86_400;
    let hours = (total_seconds % 86_400) / 3_600;
    let minutes = (total_seconds % 3_600) / 60;
    let seconds = total_seconds % 60;

    if days == 0 {
        if hours == 0 {
            if minutes == 0 {
                format!("{seconds}s")
            } else {
                format!("{minutes}m {seconds}s")
            }
        } else {
            format!("{hours}h {minutes}m {seconds}s")
        }
    } else {
        format!("{days}d {hours}h {minutes}m {seconds}s")
    }
}