Skip to content

Commit c6140fe

Browse files
authored
Add ballooning monitor for VMs (#945)
* Add ballooning monitor Add ghaf-memory-monitor and start it for every VM that has a balloon size defined. Signed-off-by: Santtu Lakkala <santtu.lakkala@unikie.com> * Increase guest balloon size Signed-off-by: Santtu Lakkala <santtu.lakkala@unikie.com> * Make appvm balloon size configurable (default to 2 * ramMb) Signed-off-by: Santtu Lakkala <santtu.lakkala@unikie.com> * Only run mem-manager on VMs with balloon Signed-off-by: Santtu Lakkala <santtu.lakkala@unikie.com> * Review fixes Signed-off-by: Santtu Lakkala <santtu.lakkala@unikie.com> --------- Signed-off-by: Santtu Lakkala <santtu.lakkala@unikie.com>
1 parent 209f8e4 commit c6140fe

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

modules/microvm/appvm.nix

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ let
171171
microvm = {
172172
optimize.enable = false;
173173
mem = vm.ramMb;
174+
balloonMem = builtins.ceil (vm.ramMb * vm.balloonRatio);
175+
deflateOnOOM = false;
174176
vcpu = vm.cores;
175177
hypervisor = "qemu";
176178
shares = [
@@ -320,10 +322,17 @@ in
320322
};
321323
ramMb = mkOption {
322324
description = ''
323-
Amount of RAM for this AppVM
325+
Minimum amount of RAM for this AppVM
324326
'';
325327
type = types.int;
326328
};
329+
balloonRatio = mkOption {
330+
description = ''
331+
Amount of dynamic RAM for this AppVM as a multiple of ramMb
332+
'';
333+
type = types.number;
334+
default = 2;
335+
};
327336
cores = mkOption {
328337
description = ''
329338
Amount of processor cores for this AppVM

modules/microvm/flake-module.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@
1818
./power-control.nix
1919
../hardware/common/shared-mem.nix
2020
];
21+
22+
mem-manager.imports = [
23+
./mem-manager.nix
24+
];
2125
};
2226
}

modules/microvm/mem-manager.nix

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2025 TII (SSRC) and the Ghaf contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# VM memory usage manager on host
5+
#
6+
{
7+
pkgs,
8+
config,
9+
...
10+
}:
11+
let
12+
balloonvms = builtins.filter (
13+
name: (config.microvm.vms.${name}.config.config.microvm.balloonMem or 0) > 0
14+
) (builtins.attrNames (config.microvm.vms or { }));
15+
in
16+
{
17+
systemd.services =
18+
builtins.foldl'
19+
(
20+
result: name:
21+
result
22+
// (
23+
let
24+
microvmConfig = config.microvm.vms.${name}.config.config.microvm;
25+
in
26+
{
27+
"ghaf-mem-manager-${name}" = {
28+
description = "Manage MicroVM '${name}' memory levels";
29+
after = [ "microvm@${name}.service" ];
30+
requires = [ "microvm@${name}.service" ];
31+
serviceConfig = {
32+
Type = "simple";
33+
WorkingDirectory = "${config.microvm.stateDir}/${name}";
34+
ExecStart = "${pkgs.ghaf-mem-manager}/bin/ghaf-mem-manager -s ${name}.sock -m ${
35+
builtins.toString (microvmConfig.mem * 1024 * 1024)
36+
} -M ${builtins.toString ((microvmConfig.mem + microvmConfig.balloonMem) * 1024 * 1024)}";
37+
};
38+
};
39+
}
40+
)
41+
)
42+
{
43+
balloon-manager =
44+
let
45+
balloonvmnames = builtins.map (name: "ghaf-mem-manager-" + name + ".service") balloonvms;
46+
in
47+
{
48+
description = "Manage MicroVM balloons";
49+
after = balloonvmnames;
50+
requires = balloonvmnames;
51+
wantedBy = [ "microvms.target" ];
52+
script = ":";
53+
};
54+
}
55+
balloonvms;
56+
}

targets/laptop/laptop-configuration-builder.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ let
2222
self.nixosModules.profiles-laptop
2323
self.nixosModules.laptop
2424
self.nixosModules.microvm
25+
self.nixosModules.mem-manager
2526

2627
{
2728
ghaf = {

0 commit comments

Comments
 (0)