Eine aufbereitete Darstellung der Quelle

 
     
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung
 
 
 
 

Benutzer

Quelle  uptime.rs

  Sprache: Rust
 

use crate::{expect, ProcResult};

use std::io::Read;
use std::str::FromStr;
use std::time::Duration;

/// The uptime of the system, based on the `/proc/uptime` file.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Uptime {
    /// The uptime of the system (including time spent in suspend).
    pub uptime: f64,

    /// The sum of how much time each core has spent idle.
    pub idle: f64,
}

impl super::FromRead for Uptime {
    fn from_read<R: Read>(mut r: R) -> ProcResult<Self> {
        let mut buf = Vec::with_capacity(128);
        r.read_to_end(&mut buf)?;

        let line = String::from_utf8_lossy(&buf);
        let buf = line.trim();

        let mut s = buf.split(' ');
        let uptime = expect!(f64::from_str(expect!(s.next())));
        let idle = expect!(f64::from_str(expect!(s.next())));

        Ok(Uptime { uptime, idle })
    }
}

impl Uptime {
    /// The uptime of the system (including time spent in suspend).
    pub fn uptime_duration(&self) -> Duration {
        let secs = self.uptime.trunc() as u64;
        let csecs = (self.uptime.fract() * 100.0).round() as u32;
        let nsecs = csecs * 10_000_000;

        Duration::new(secs, nsecs)
    }

    /// The sum of how much time each core has spent idle.
    pub fn idle_duration(&self) -> Duration {
        let secs = self.idle.trunc() as u64;
        let csecs = (self.idle.fract() * 100.0).round() as u32;
        let nsecs = csecs * 10_000_000;

        Duration::new(secs, nsecs)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::FromRead;
    use std::io::Cursor;

    #[test]
    fn test_uptime() {
        let reader = Cursor::new(b"2578790.61 1999230.98\n");
        let uptime = Uptime::from_read(reader).unwrap();

        assert_eq!(uptime.uptime_duration(), Duration::new(2578790610_000_000));
        assert_eq!(uptime.idle_duration(), Duration::new(1999230980_000_000));
    }
}

Messung V0.5 in Prozent
C=84 H=95 G=89

¤ Dauer der Verarbeitung: 0.9 Sekunden  (vorverarbeitet am  2026-06-19) ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

PVS Prover

Isabelle Prover

NIST Cobol Testsuite

Cephes Mathematical Library

Vienna Development Method

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.






                                                                                                                                                                                                                                                                                                                                                                                                     


Neuigkeiten

     Aktuelles
     Motto des Tages

Software

     Quellcodebibliothek
     Eigene Quellcodes
     Fremde Quellcodes
     Suchen

Aktivitäten

     Artikel über Sicherheit
     Anleitung zur Aktivierung von SSL

Muße

     Gedichte
     Musik
     Bilder

Jenseits des Üblichen ....

Besucherstatistik

Besucherstatistik