• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 use serde::Deserialize;
6 use serde::Serialize;
7 
8 // Balloon commands that are send on the balloon command tube.
9 #[derive(Serialize, Deserialize, Debug)]
10 pub 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. The ID can be used to discard stale states
25     // if any previous stats requests failed or timed out.
26     Stats {
27         id: u64,
28     },
29     // Fetch balloon wss. The ID can be used to discard stale states if any
30     // previous wss request failed or timed out.
31     WorkingSetSize {
32         id: u64,
33     },
34 }
35 
36 // BalloonStats holds stats returned from the stats_queue.
37 #[derive(Default, Serialize, Deserialize, Debug, Clone)]
38 pub 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 
53 // TODO(b/276353613): remove and refactor bins into Vec
54 pub const VIRTIO_BALLOON_WSS_NUM_BINS: usize = 4;
55 pub const VIRTIO_BALLOON_WSS_CONFIG_SIZE: usize = 5;
56 
57 // WSSBucket stores information about a bucket (or bin) of the working set size.
58 #[derive(Default, Serialize, Deserialize, Debug, Clone, Copy)]
59 pub struct WSSBucket {
60     pub age: u64,
61     pub bytes: [u64; 2],
62 }
63 
64 // BalloonWSS holds WSS returned from the wss_queue.
65 #[derive(Default, Serialize, Deserialize, Debug, Clone)]
66 pub struct BalloonWSS {
67     pub wss: [WSSBucket; VIRTIO_BALLOON_WSS_NUM_BINS],
68 }
69 
70 impl BalloonWSS {
new() -> Self71     pub fn new() -> Self {
72         BalloonWSS {
73             wss: [WSSBucket {
74                 ..Default::default()
75             }; VIRTIO_BALLOON_WSS_NUM_BINS],
76         }
77     }
78 }
79 
80 // BalloonTubeResult are results to BalloonTubeCommand defined above.
81 #[derive(Serialize, Deserialize, Debug)]
82 pub enum BalloonTubeResult {
83     Stats {
84         stats: BalloonStats,
85         balloon_actual: u64,
86         id: u64,
87     },
88     Adjusted {
89         num_bytes: u64,
90     },
91     WorkingSetSize {
92         wss: BalloonWSS,
93         id: u64,
94     },
95 }
96