balloon_control/
lib.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use serde::Deserialize;
6use serde::Serialize;
7
8// Balloon commands that are send on the balloon command tube.
9#[derive(Serialize, Deserialize, Debug)]
10pub enum BalloonTubeCommand {
11    // Set the size of the VM's balloon.
12    Adjust {
13        num_bytes: u64,
14        // When this flag is set, adjust attempts can fail. After adjustment, the final
15        // size of the balloon is returned via a BalloonTubeResult::Adjust message.
16        //
17        // The flag changes the semantics of inflating the balloon. By default, the driver
18        // will indefinitely retry if it fails to allocate pages when inflating the
19        // balloon.  However, when this flag is set, the balloon device responds to page
20        // allocation failures in the guest by stopping inflation at the balloon's current
21        // size.
22        allow_failure: bool,
23    },
24    // Fetch balloon stats.
25    Stats,
26    // Fetch balloon ws.
27    WorkingSet,
28    // Send balloon ws config to guest.
29    WorkingSetConfig {
30        bins: Vec<u32>,
31        refresh_threshold: u32,
32        report_threshold: u32,
33    },
34}
35
36// BalloonStats holds stats returned from the stats_queue.
37#[derive(Default, Serialize, Deserialize, Debug, Clone)]
38pub struct BalloonStats {
39    pub swap_in: Option<u64>,
40    pub swap_out: Option<u64>,
41    pub major_faults: Option<u64>,
42    pub minor_faults: Option<u64>,
43    pub free_memory: Option<u64>,
44    pub total_memory: Option<u64>,
45    pub available_memory: Option<u64>,
46    pub disk_caches: Option<u64>,
47    pub hugetlb_allocations: Option<u64>,
48    pub hugetlb_failures: Option<u64>,
49    pub shared_memory: Option<u64>,
50    pub unevictable_memory: Option<u64>,
51}
52
53pub const VIRTIO_BALLOON_WS_MIN_NUM_BINS: usize = 2;
54pub const VIRTIO_BALLOON_WS_MAX_NUM_BINS: usize = 16;
55
56// WSBucket stores information about a bucket (or bin) of the working set.
57#[derive(Default, Serialize, Deserialize, Debug, Clone, Copy)]
58pub struct WSBucket {
59    pub age: u64,
60    pub bytes: [u64; 2],
61}
62
63// BalloonWS holds WS returned from the ws_queue.
64#[derive(Default, Serialize, Deserialize, Debug, Clone)]
65pub struct BalloonWS {
66    /// working set, separated per histogram bucket.
67    pub ws: Vec<WSBucket>,
68}
69
70impl BalloonWS {
71    pub fn new() -> Self {
72        BalloonWS { ws: vec![] }
73    }
74}
75
76// BalloonTubeResult are results to BalloonTubeCommand defined above.
77#[derive(Serialize, Deserialize, Debug)]
78pub enum BalloonTubeResult {
79    Stats {
80        stats: BalloonStats,
81        balloon_actual: u64,
82    },
83    Adjusted {
84        num_bytes: u64,
85    },
86    WorkingSet {
87        ws: BalloonWS,
88        /// size of the balloon in bytes.
89        balloon_actual: u64,
90    },
91}