Repository: firecracker-microvm/firecracker
File: src/vmm/src/devices/virtio/balloon/device.rs (Rust)

Code before:
    pub direct_reclaim: Option<u64>,
}

impl BalloonStats {
    fn update_with_stat(&mut self, stat: &BalloonStat) -> Result<(), BalloonError> {
        let val = Some(stat.val);
        match stat.tag {
            VIRTIO_BALLOON_S_SWAP_IN => self.swap_in = val,
            VIRTIO_BALLOON_S_SWAP_OUT => self.swap_out = val,
            VIRTIO_BALLOON_S_MAJFLT => self.major_faults = val,
            VIRTIO_BALLOON_S_MINFLT => self.minor_faults = val,
            VIRTIO_BALLOON_S_MEMFREE => self.free_memory = val,
            VIRTIO_BALLOON_S_MEMTOT => self.total_memory = val,
            VIRTIO_BALLOON_S_AVAIL => self.available_memory = val,
            VIRTIO_BALLOON_S_CACHES => self.disk_caches = val,
            VIRTIO_BALLOON_S_HTLB_PGALLOC => self.hugetlb_allocations = val,
            VIRTIO_BALLOON_S_HTLB_PGFAIL => self.hugetlb_failures = val,
            VIRTIO_BALLOON_S_OOM_KILL => self.oom_kill = val,
            VIRTIO_BALLOON_S_ALLOC_STALL => self.alloc_stall = val,
            VIRTIO_BALLOON_S_ASYNC_SCAN => self.async_scan = val,
            VIRTIO_BALLOON_S_DIRECT_SCAN => self.direct_scan = val,
            VIRTIO_BALLOON_S_ASYNC_RECLAIM => self.async_reclaim = val,
            VIRTIO_BALLOON_S_DIRECT_RECLAIM => self.direct_reclaim = val,
            _ => {
                let tag = stat.tag;
                warn!("balloon: unknown stats update tag: {}", tag);
            }
        }

        Ok(())
    }
}

/// Virtio balloon device.
#[derive(Debug)]
pub struct Balloon {
    // Virtio fields.
    pub(crate) avail_features: u64,
    pub(crate) acked_features: u64,
    pub(crate) config_space: ConfigSpace,
    pub(crate) activate_evt: EventFd,

    // Transport related fields.
    pub(crate) queues: Vec<Queue>,
    pub(crate) queue_evts: Vec<EventFd>,
    pub(crate) device_state: DeviceState,

    // Implementation specific fields.
    pub(crate) stats_polling_interval_s: u16,
    pub(crate) stats_timer: TimerFd,
    // The index of the previous stats descriptor is saved because

Review comment (suggestion):
```suggestion
            tag => warn!("balloon: unknown stats update tag: {tag}")
```

nit: we can just do this

Code after:
    pub direct_reclaim: Option<u64>,
}

impl BalloonStats {
    fn update_with_stat(&mut self, stat: &BalloonStat) {
        let val = Some(stat.val);
        match stat.tag {
            VIRTIO_BALLOON_S_SWAP_IN => self.swap_in = val,
            VIRTIO_BALLOON_S_SWAP_OUT => self.swap_out = val,
            VIRTIO_BALLOON_S_MAJFLT => self.major_faults = val,
            VIRTIO_BALLOON_S_MINFLT => self.minor_faults = val,
            VIRTIO_BALLOON_S_MEMFREE => self.free_memory = val,
            VIRTIO_BALLOON_S_MEMTOT => self.total_memory = val,
            VIRTIO_BALLOON_S_AVAIL => self.available_memory = val,
            VIRTIO_BALLOON_S_CACHES => self.disk_caches = val,
            VIRTIO_BALLOON_S_HTLB_PGALLOC => self.hugetlb_allocations = val,
            VIRTIO_BALLOON_S_HTLB_PGFAIL => self.hugetlb_failures = val,
            VIRTIO_BALLOON_S_OOM_KILL => self.oom_kill = val,
            VIRTIO_BALLOON_S_ALLOC_STALL => self.alloc_stall = val,
            VIRTIO_BALLOON_S_ASYNC_SCAN => self.async_scan = val,
            VIRTIO_BALLOON_S_DIRECT_SCAN => self.direct_scan = val,
            VIRTIO_BALLOON_S_ASYNC_RECLAIM => self.async_reclaim = val,
            VIRTIO_BALLOON_S_DIRECT_RECLAIM => self.direct_reclaim = val,
            tag => {
                METRICS.stats_update_fails.inc();
                debug!("balloon: unknown stats update tag: {tag}");
            }
        }
    }
}

/// Virtio balloon device.
#[derive(Debug)]
pub struct Balloon {
    // Virtio fields.
    pub(crate) avail_features: u64,
    pub(crate) acked_features: u64,
    pub(crate) config_space: ConfigSpace,
    pub(crate) activate_evt: EventFd,

    // Transport related fields.
    pub(crate) queues: Vec<Queue>,
    pub(crate) queue_evts: Vec<EventFd>,
    pub(crate) device_state: DeviceState,

    // Implementation specific fields.
    pub(crate) stats_polling_interval_s: u16,
    pub(crate) stats_timer: TimerFd,
    // The index of the previous stats descriptor is saved because
    // it is acknowledged after the stats queue is processed.
    pub(crate) stats_desc_index: Option<u16>,