Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/src/vmm/benches/main.rs
1958 views
1
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
// SPDX-License-Identifier: Apache-2.0
3
//
4
// Benchmark testing
5
//
6
// Test serialization and deserialzation of a MicrovmState for a default VMM:
7
// - 1 VCPU
8
// - 128 MB memory size
9
// - no devices
10
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
12
use snapshot::Snapshot;
13
use std::path::Path;
14
use std::thread;
15
use std::time::Duration;
16
use utils::tempfile::TempFile;
17
use versionize::VersionMap;
18
use vmm::persist::MicrovmState;
19
use vmm::utilities::mock_resources::NOISY_KERNEL_IMAGE;
20
use vmm::utilities::test_utils::create_vmm;
21
use vmm::version_map::VERSION_MAP;
22
use vmm::vmm_config::snapshot::{CreateSnapshotParams, SnapshotType};
23
use vmm::{persist, FC_EXIT_CODE_OK};
24
25
#[inline]
26
pub fn bench_restore_snapshot(
27
mut snapshot_reader: &[u8],
28
snapshot_len: usize,
29
vm: VersionMap,
30
crc: bool,
31
) {
32
if crc {
33
Snapshot::load::<&[u8], MicrovmState>(&mut snapshot_reader, snapshot_len, vm).unwrap();
34
} else {
35
Snapshot::unchecked_load::<&[u8], MicrovmState>(&mut snapshot_reader, vm).unwrap();
36
}
37
}
38
39
#[inline]
40
pub fn bench_create_snapshot<W: std::io::Write>(
41
mut snapshot_writer: &mut W,
42
vm: VersionMap,
43
crc: bool,
44
state: &mut MicrovmState,
45
) {
46
let mut snapshot = Snapshot::new(vm.clone(), vm.latest_version());
47
48
if crc {
49
snapshot.save(&mut snapshot_writer, state).unwrap();
50
} else {
51
snapshot
52
.save_without_crc(&mut snapshot_writer, state)
53
.unwrap();
54
}
55
}
56
57
fn create_microvm_state(is_diff: bool) -> MicrovmState {
58
let snapshot_file = TempFile::new().unwrap();
59
let memory_file = TempFile::new().unwrap();
60
61
let (vmm, _) = create_vmm(Some(NOISY_KERNEL_IMAGE), is_diff);
62
63
// Be sure that the microVM is running.
64
thread::sleep(Duration::from_millis(200));
65
66
// Pause microVM.
67
vmm.lock().unwrap().pause_vm().unwrap();
68
69
// Create snapshot.
70
let snapshot_type = match is_diff {
71
true => SnapshotType::Diff,
72
false => SnapshotType::Full,
73
};
74
let snapshot_params = CreateSnapshotParams {
75
snapshot_type,
76
snapshot_path: snapshot_file.as_path().to_path_buf(),
77
mem_file_path: memory_file.as_path().to_path_buf(),
78
version: None,
79
};
80
81
{
82
let mut locked_vmm = vmm.lock().unwrap();
83
persist::create_snapshot(&mut locked_vmm, &snapshot_params, VERSION_MAP.clone()).unwrap();
84
}
85
86
vmm.lock().unwrap().stop(FC_EXIT_CODE_OK);
87
88
// Deserialize the microVM state from `snapshot_file`.
89
let snapshot_path = snapshot_file.as_path().to_path_buf();
90
let snapshot_file_metadata = std::fs::metadata(snapshot_path).unwrap();
91
let snapshot_len = snapshot_file_metadata.len() as usize;
92
let microvm_state: MicrovmState = Snapshot::load(
93
&mut snapshot_file.as_file(),
94
snapshot_len,
95
VERSION_MAP.clone(),
96
)
97
.unwrap();
98
99
microvm_state
100
}
101
102
pub fn criterion_benchmark(c: &mut Criterion) {
103
let version_map = VERSION_MAP.clone();
104
105
// Create the microvm state
106
let mut state = create_microvm_state(false);
107
108
// Setup benchmarking with CRC
109
let mut snapshot_state_with_crc = vec![0u8; 1024 * 1024 * 128];
110
let mut slice = &mut snapshot_state_with_crc.as_mut_slice();
111
bench_create_snapshot(&mut slice, version_map.clone(), true, &mut state);
112
let snapshot_len =
113
slice.as_ptr() as usize - snapshot_state_with_crc.as_slice().as_ptr() as usize;
114
println!("Snapshot length with CRC: {} bytes.", snapshot_len);
115
116
c.bench_function("Serialize MicrovmState CRC", |b| {
117
b.iter(|| {
118
bench_create_snapshot(
119
&mut snapshot_state_with_crc.as_mut_slice(),
120
black_box(version_map.clone()),
121
black_box(true),
122
black_box(&mut state),
123
)
124
})
125
});
126
127
c.bench_function("Deserialize MicrovmState CRC", |b| {
128
b.iter(|| {
129
bench_restore_snapshot(
130
&mut snapshot_state_with_crc.as_mut_slice(),
131
black_box(snapshot_len),
132
black_box(version_map.clone()),
133
black_box(true),
134
)
135
})
136
});
137
138
// Setup benchmarking without CRC
139
let mut snapshot_state_without_crc = vec![0u8; 1024 * 1024 * 128];
140
let mut slice = &mut snapshot_state_without_crc.as_mut_slice();
141
bench_create_snapshot(&mut slice, version_map.clone(), false, &mut state);
142
let snapshot_len =
143
slice.as_ptr() as usize - snapshot_state_without_crc.as_slice().as_ptr() as usize;
144
println!("Snapshot length without CRC: {} bytes.", snapshot_len);
145
146
c.bench_function("Serialize MicrovmState", |b| {
147
b.iter(|| {
148
bench_create_snapshot(
149
black_box(&mut snapshot_state_without_crc.as_mut_slice()),
150
black_box(version_map.clone()),
151
black_box(false),
152
black_box(&mut state),
153
)
154
})
155
});
156
157
c.bench_function("Deserialize MicrovmState", |b| {
158
b.iter(|| {
159
bench_restore_snapshot(
160
black_box(&mut snapshot_state_without_crc.as_mut_slice()),
161
black_box(snapshot_len),
162
black_box(version_map.clone()),
163
black_box(false),
164
)
165
})
166
});
167
}
168
169
criterion_group! {
170
name = benches;
171
config = Criterion::default().sample_size(200).output_directory(Path::new("../../build/vmm_benchmark"));
172
targets = criterion_benchmark
173
}
174
175
criterion_main! {
176
benches
177
}
178
179