Skip to content

Commit 958a6a9

Browse files
dianpopaShadowCurse
authored andcommitted
devices: doc for the balloon device
Doc for the balloon device Signed-off-by: Diana Popa <dpopa@amazon.com>
1 parent 29eb073 commit 958a6a9

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

src/vmm/src/devices/virtio/balloon/device.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,40 +64,63 @@ struct BalloonStat {
6464
// SAFETY: Safe because BalloonStat only contains plain data.
6565
unsafe impl ByteValued for BalloonStat {}
6666

67-
// BalloonStats holds statistics returned from the stats_queue.
67+
/// Holds configuration details for the balloon device.
6868
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize)]
6969
pub struct BalloonConfig {
70+
/// Target size.
7071
pub amount_mib: u32,
72+
/// Whether or not to ask for pages back.
7173
pub deflate_on_oom: bool,
74+
/// Interval of time in seconds at which the balloon statistics are updated.
7275
pub stats_polling_interval_s: u16,
7376
}
7477

75-
// BalloonStats holds statistics returned from the stats_queue.
78+
/// BalloonStats holds statistics returned from the stats_queue.
7679
#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize)]
7780
#[serde(deny_unknown_fields)]
7881
pub struct BalloonStats {
82+
/// The target size of the balloon, in 4K pages.
7983
pub target_pages: u32,
84+
/// The number of 4K pages the device is currently holding.
8085
pub actual_pages: u32,
86+
/// The target size of the balloon, in MiB.
8187
pub target_mib: u32,
88+
/// The number of MiB the device is currently holding.
8289
pub actual_mib: u32,
90+
/// Amount of memory swapped in.
8391
#[serde(skip_serializing_if = "Option::is_none")]
8492
pub swap_in: Option<u64>,
93+
/// Amount of memory swapped out.
8594
#[serde(skip_serializing_if = "Option::is_none")]
8695
pub swap_out: Option<u64>,
96+
/// Number of major faults.
8797
#[serde(skip_serializing_if = "Option::is_none")]
8898
pub major_faults: Option<u64>,
99+
/// Number of minor faults.
89100
#[serde(skip_serializing_if = "Option::is_none")]
90101
pub minor_faults: Option<u64>,
102+
/// The amount of memory not being used for any
103+
/// purpose (in bytes).
91104
#[serde(skip_serializing_if = "Option::is_none")]
92105
pub free_memory: Option<u64>,
106+
/// Total amount of memory available (in bytes).
93107
#[serde(skip_serializing_if = "Option::is_none")]
94108
pub total_memory: Option<u64>,
109+
/// An estimate of how much memory is available (in
110+
/// bytes) for starting new applications, without pushing the system to swap.
95111
#[serde(skip_serializing_if = "Option::is_none")]
96112
pub available_memory: Option<u64>,
113+
/// The amount of memory, in bytes, that can be
114+
/// quickly reclaimed without additional I/O. Typically these pages are used for
115+
/// caching files from disk.
97116
#[serde(skip_serializing_if = "Option::is_none")]
98117
pub disk_caches: Option<u64>,
118+
/// The number of successful hugetlb page
119+
/// allocations in the guest.
99120
#[serde(skip_serializing_if = "Option::is_none")]
100121
pub hugetlb_allocations: Option<u64>,
122+
/// The number of failed hugetlb page allocations
123+
/// in the guest.
101124
#[serde(skip_serializing_if = "Option::is_none")]
102125
pub hugetlb_failures: Option<u64>,
103126
}
@@ -125,7 +148,7 @@ impl BalloonStats {
125148
}
126149
}
127150

128-
// Virtio balloon device.
151+
/// Virtio balloon device.
129152
pub struct Balloon {
130153
// Virtio fields.
131154
pub(crate) avail_features: u64,
@@ -175,6 +198,7 @@ impl fmt::Debug for Balloon {
175198
}
176199

177200
impl Balloon {
201+
/// Instantiate a new balloon device.
178202
pub fn new(
179203
amount_mib: u32,
180204
deflate_on_oom: bool,
@@ -419,6 +443,7 @@ impl Balloon {
419443
let _ = self.process_deflate_queue();
420444
}
421445

446+
/// Provides the ID of this balloon device.
422447
pub fn id(&self) -> &str {
423448
BALLOON_DEV_ID
424449
}
@@ -440,6 +465,7 @@ impl Balloon {
440465
}
441466
}
442467

468+
/// Update the target size of the balloon.
443469
pub fn update_size(&mut self, amount_mib: u32) -> Result<(), BalloonError> {
444470
if self.is_activated() {
445471
self.config_space.num_pages = mib_to_pages(amount_mib)?;
@@ -451,6 +477,7 @@ impl Balloon {
451477
}
452478
}
453479

480+
/// Update the the statistics polling interval.
454481
pub fn update_stats_polling_interval(&mut self, interval_s: u16) -> Result<(), BalloonError> {
455482
if self.stats_polling_interval_s == interval_s {
456483
return Ok(());
@@ -476,10 +503,12 @@ impl Balloon {
476503
.set_state(timer_state, SetTimeFlags::Default);
477504
}
478505

506+
/// Obtain the number of 4K pages the device is currently holding.
479507
pub fn num_pages(&self) -> u32 {
480508
self.config_space.num_pages
481509
}
482510

511+
/// Obtain the size of 4K pages the device is currently holding in MIB.
483512
pub fn size_mb(&self) -> u32 {
484513
pages_to_mib(self.config_space.num_pages)
485514
}
@@ -492,6 +521,7 @@ impl Balloon {
492521
self.stats_polling_interval_s
493522
}
494523

524+
/// Retrieve latest stats for the balloon device.
495525
pub fn latest_stats(&mut self) -> Option<&BalloonStats> {
496526
if self.stats_enabled() {
497527
self.latest_stats.target_pages = self.config_space.num_pages;
@@ -504,6 +534,7 @@ impl Balloon {
504534
}
505535
}
506536

537+
/// Return the config of the balloon device.
507538
pub fn config(&self) -> BalloonConfig {
508539
BalloonConfig {
509540
amount_mib: self.size_mb(),

src/vmm/src/devices/virtio/balloon/mod.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
//! Implements a virtio balloon device.
5+
46
pub mod device;
57
mod event_handler;
68
pub mod persist;
@@ -14,25 +16,30 @@ pub use self::device::{Balloon, BalloonConfig, BalloonStats};
1416
/// Device ID used in MMIO device identification.
1517
/// Because Balloon is unique per-vm, this ID can be hardcoded.
1618
pub const BALLOON_DEV_ID: &str = "balloon";
19+
/// The size of the config space.
1720
pub const BALLOON_CONFIG_SPACE_SIZE: usize = 8;
21+
/// Max size of virtio queues.
1822
pub const BALLOON_QUEUE_SIZE: u16 = 256;
23+
/// Number of virtio queues.
1924
pub const BALLOON_NUM_QUEUES: usize = 3;
25+
/// Virtio queue sizes, in number of descriptor chain heads.
26+
// There are 3 queues for a virtio device (in this order): RX, TX, Event
2027
pub const BALLOON_QUEUE_SIZES: [u16; BALLOON_NUM_QUEUES] =
2128
[BALLOON_QUEUE_SIZE, BALLOON_QUEUE_SIZE, BALLOON_QUEUE_SIZE];
2229
// Number of 4K pages in a MiB.
2330
pub const MIB_TO_4K_PAGES: u32 = 256;
24-
// The maximum number of pages that can be received in a single descriptor.
31+
/// The maximum number of pages that can be received in a single descriptor.
2532
pub const MAX_PAGES_IN_DESC: usize = 256;
26-
// The maximum number of pages that can be compacted into ranges during process_inflate().
27-
// Needs to be a multiple of MAX_PAGES_IN_DESC.
33+
/// The maximum number of pages that can be compacted into ranges during process_inflate().
34+
/// Needs to be a multiple of MAX_PAGES_IN_DESC.
2835
pub const MAX_PAGE_COMPACT_BUFFER: usize = 2048;
29-
// The addresses given by the driver are divided by 4096.
36+
/// The addresses given by the driver are divided by 4096.
3037
pub const VIRTIO_BALLOON_PFN_SHIFT: u32 = 12;
31-
// The index of the deflate queue from Balloon device queues/queues_evts vector.
38+
/// The index of the deflate queue from Balloon device queues/queues_evts vector.
3239
pub const INFLATE_INDEX: usize = 0;
33-
// The index of the deflate queue from Balloon device queues/queues_evts vector.
40+
/// The index of the deflate queue from Balloon device queues/queues_evts vector.
3441
pub const DEFLATE_INDEX: usize = 1;
35-
// The index of the deflate queue from Balloon device queues/queues_evts vector.
42+
/// The index of the deflate queue from Balloon device queues/queues_evts vector.
3643
pub const STATS_INDEX: usize = 2;
3744

3845
// The feature bitmap for virtio balloon.
@@ -51,6 +58,7 @@ const VIRTIO_BALLOON_S_CACHES: u16 = 7;
5158
const VIRTIO_BALLOON_S_HTLB_PGALLOC: u16 = 8;
5259
const VIRTIO_BALLOON_S_HTLB_PGFAIL: u16 = 9;
5360

61+
/// Balloon device related errors.
5462
#[derive(Debug)]
5563
pub enum BalloonError {
5664
/// Activation error.

src/vmm/src/devices/virtio/balloon/persist.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ use crate::devices::virtio::balloon::device::{BalloonStats, ConfigSpace};
1818
use crate::devices::virtio::persist::VirtioDeviceState;
1919
use crate::devices::virtio::{DeviceState, TYPE_BALLOON};
2020

21+
/// Information about the balloon config's that are saved
22+
/// at snapshot.
2123
// NOTICE: Any changes to this structure require a snapshot version bump.
2224
#[derive(Debug, Clone, Versionize)]
2325
pub struct BalloonConfigSpaceState {
2426
num_pages: u32,
2527
actual_pages: u32,
2628
}
2729

30+
/// Information about the balloon stats that are saved
31+
/// at snapshot.
2832
// NOTICE: Any changes to this structure require a snapshot version bump.
2933
#[derive(Debug, Clone, Versionize)]
3034
pub struct BalloonStatsState {
@@ -76,6 +80,8 @@ impl BalloonStatsState {
7680
}
7781
}
7882

83+
/// Information about the balloon that are saved
84+
/// at snapshot.
7985
// NOTICE: Any changes to this structure require a snapshot version bump.
8086
#[derive(Debug, Clone, Versionize)]
8187
pub struct BalloonState {
@@ -86,8 +92,10 @@ pub struct BalloonState {
8692
virtio_state: VirtioDeviceState,
8793
}
8894

95+
/// Auxiliary structure for creating a device when resuming from a snapshot.
8996
#[derive(Debug)]
9097
pub struct BalloonConstructorArgs {
98+
/// Pointer to guest memory.
9199
pub mem: GuestMemoryMmap,
92100
}
93101

0 commit comments

Comments
 (0)