tessera-command 0.4.17

The tesserax command: opens a recipe, then serves, fetches or runs tasks
Documentation
//! `machine`: the device and memory of this machine.

use crate::size::specs;
use serde_json::json;
use tessera_frontend::tasks::Error;
use tessera_frontend::tasks::task::{Run, Sink, Task};

/// Print this machine's device, memory and free memory.
#[derive(Task)]
#[task(example = "show the hardware and memory of this machine")]
pub struct Machine {}

/// Bytes as GiB, to one decimal place.
fn gb(bytes: u64) -> f64 {
    (bytes as f64 * 10.0 / (1u64 << 30) as f64).round() / 10.0
}

impl Run<()> for Machine {
    fn run(self, _: (), sink: &mut dyn Sink) -> Result<(), Error> {
        let specs = specs(&tessera_frontend::frontend_settings::read());
        // A device on host memory shares the host's free bytes.
        let free = match specs.integrated {
            true => Some(specs.free.unwrap_or(specs.memory).min(specs.host[1])),
            false => specs.free,
        };
        sink.record(json!({
            "os": std::env::consts::OS,
            "arch": std::env::consts::ARCH,
            "device": specs.name,
            "backend": specs.backend,
            "shares_host_memory": specs.integrated,
            "device_memory_gb": gb(specs.memory),
            "device_free_gb": free.map(gb),
            "host_memory_gb": gb(specs.host[0]),
            "host_available_gb": gb(specs.host[1]),
        }));
        Ok(())
    }
}