diff --git a/REUSE.toml b/REUSE.toml index 7d4dc5f..3a169b5 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -9,10 +9,11 @@ SPDX-PackageDownloadLocation = "https://github.com/tiiuae/ghaf" [[annotations]] SPDX-License-Identifier = "Apache-2.0" -SPDX-FileCopyrightText = "2022-2024 TII (SSRC) and the Ghaf contributors" +SPDX-FileCopyrightText = "2022-2025 TII (SSRC) and the Ghaf contributors" precedence = "closest" path = [ "flake.lock", + "packages/ghaf-mem-manager/Cargo.lock", "packages/ghaf-artwork/**/*.png", "packages/ghaf-artwork/**/*.jpg", "packages/ghaf-artwork/**/*.svg", diff --git a/flake.lock b/flake.lock index b5c86bc..394fef3 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "crane": { + "locked": { + "lastModified": 1737689766, + "narHash": "sha256-ivVXYaYlShxYoKfSo5+y5930qMKKJ8CLcAoIBPQfJ6s=", + "owner": "ipetkov", + "repo": "crane", + "rev": "6fe74265bbb6d016d663b1091f015e2976c4a527", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { @@ -117,6 +132,7 @@ }, "root": { "inputs": { + "crane": "crane", "flake-compat": "flake-compat", "flake-parts": "flake-parts", "flake-root": "flake-root", diff --git a/flake.nix b/flake.nix index 697259d..4954652 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,11 @@ inputs.nixpkgs.follows = "nixpkgs"; }; + # Rust / Cargo building + crane = { + url = "github:ipetkov/crane"; + }; + pre-commit-hooks-nix = { url = "github:cachix/pre-commit-hooks.nix"; inputs = { diff --git a/nix/treefmt.nix b/nix/treefmt.nix index 615d299..b463bd6 100644 --- a/nix/treefmt.nix +++ b/nix/treefmt.nix @@ -35,6 +35,9 @@ # C++ clang-format.enable = true; + + # Rust + rustfmt.enable = true; }; settings.global.excludes = [ diff --git a/packages/flake-module.nix b/packages/flake-module.nix index a2612ff..f147240 100644 --- a/packages/flake-module.nix +++ b/packages/flake-module.nix @@ -1,6 +1,6 @@ # Copyright 2024 TII (SSRC) and the Ghaf contributors # SPDX-License-Identifier: Apache-2.0 -_: +{ inputs, ... }: let ghafpkgs = pkgs: @@ -8,9 +8,9 @@ let callPackage = pkgs.lib.callPackageWith pkgs; in { - ghaf-artwork = callPackage ./ghaf-artwork { }; ghaf-audio-control = callPackage ./ghaf-audio-control { }; + ghaf-mem-manager = callPackage ./ghaf-mem-manager { inherit (inputs) crane; }; ghaf-theme = callPackage ./ghaf-theme { }; }; in diff --git a/packages/ghaf-mem-manager/Cargo.lock b/packages/ghaf-mem-manager/Cargo.lock new file mode 100644 index 0000000..b7c2316 --- /dev/null +++ b/packages/ghaf-mem-manager/Cargo.lock @@ -0,0 +1,597 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys 0.59.0", +] + +[[package]] +name = "anyhow" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + +[[package]] +name = "bytes" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb3b4b9e5a7c7514dfa52869339ee98b3156b0bfb4e8a77c4ff4babb64b1604f" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b17a95aa67cc7b5ebd32aa5370189aa0d79069ef1c64ce893bd30fb24bff20ec" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afb84c814227b90d6895e01398aee0d8033c00e7466aca416fb6a8e0eb19d8a7" + +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + +[[package]] +name = "ghaf-mem-manager" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "serde", + "serde_json", + "tokio", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.164" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi", + "libc", + "wasi", + "windows-sys 0.52.0", +] + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "pin-project-lite" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.215" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.133" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "socket2" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tokio" +version = "1.41.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/packages/ghaf-mem-manager/Cargo.toml b/packages/ghaf-mem-manager/Cargo.toml new file mode 100644 index 0000000..cfa3f5e --- /dev/null +++ b/packages/ghaf-mem-manager/Cargo.toml @@ -0,0 +1,18 @@ +# Copyright 2025 TII (SSRC) and the Ghaf contributors +# SPDX-License-Identifier: Apache-2.0 + +[package] +name = "ghaf-mem-manager" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.93" +clap = { version = "4.5.21", features = ["derive"] } +serde = { version = "1.0.215", features = ["derive"] } +serde_json = "1.0.133" +tokio = { version = "1.41.1", features = ["rt", "net", "macros", "fs", "time", "io-util", "sync"] } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" diff --git a/packages/ghaf-mem-manager/default.nix b/packages/ghaf-mem-manager/default.nix new file mode 100644 index 0000000..88cc78c --- /dev/null +++ b/packages/ghaf-mem-manager/default.nix @@ -0,0 +1,28 @@ +# Copyright 2025 TII (SSRC) and the Ghaf contributors +# SPDX-License-Identifier: Apache-2.0 +{ + pkgs, + crane, + src, +}: +let + craneLib = crane.mkLib pkgs; + + # Common arguments can be set here to avoid repeating them later + # Note: changes here will rebuild all dependency crates + commonArgs = { + src = ./.; + + strictDeps = true; + }; + + mem-monitor = craneLib.buildPackage ( + commonArgs + // { + outputs = [ "out" ]; + cargoArtifacts = craneLib.buildDepsOnly commonArgs; + + } + ); +in +mem-monitor diff --git a/packages/ghaf-mem-manager/src/main.rs b/packages/ghaf-mem-manager/src/main.rs new file mode 100644 index 0000000..febf9a3 --- /dev/null +++ b/packages/ghaf-mem-manager/src/main.rs @@ -0,0 +1,387 @@ +/* + * Copyright 2025 TII (SSRC) and the Ghaf contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +use anyhow::{bail, Context, Result}; +use clap::Parser; +use serde::{Deserialize, Serialize}; +use std::{ + cell::RefCell, + collections::HashMap, + path::PathBuf, + time::{Duration, Instant}, +}; +use tokio::{ + io::{AsyncBufReadExt, AsyncWriteExt, BufStream}, + net::UnixStream, + sync::mpsc, +}; +use tracing::{info, warn}; + +#[derive(Parser)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Path to QMP socket + #[arg(short, long)] + socket: Vec, + + /// Monitoring interval in seconds + #[arg(short, long, default_value_t = 1)] + interval: u64, + + /// Minimum ballooning interval + #[arg(short, long, default_value_t = 3)] + balloon_interval: u64, + + /// Minimum memory size + #[arg(short, long, default_value_t = usize::MIN)] + minimum: usize, + + /// Maximum memory size + #[arg(short = 'M', long, default_value_t = usize::MAX)] + maximum: usize, + + /// Low memory presure + #[arg(short, long, default_value_t = 70)] + low: u8, + + /// High memory pressure + #[arg(short, long, default_value_t = 80)] + high: u8, +} + +#[derive(Serialize, Debug)] +#[serde(rename_all = "kebab-case")] +struct QmpCommand { + execute: &'static str, + #[serde(skip_serializing_if = "HashMap::is_empty")] + arguments: HashMap<&'static str, serde_json::Value>, +} + +impl QmpCommand { + pub fn new(cmd: &'static str) -> Self { + Self { + execute: cmd, + arguments: HashMap::new(), + } + } + + pub fn arg>(self, key: &'static str, v: T) -> Self { + let Self { + execute, + mut arguments, + } = self; + arguments.insert(key, v.into()); + Self { execute, arguments } + } +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "kebab-case")] +struct BalloonInfo { + actual: usize, +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "kebab-case")] +struct MemoryInfo { + base_memory: usize, + plugged_memory: usize, +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "kebab-case")] +struct GuestMemoryStats { + stat_available_memory: usize, + stat_free_memory: usize, +} + +#[derive(Deserialize, Debug)] +#[serde(rename_all = "kebab-case")] +struct GuestMemoryInfo { + last_update: usize, + stats: GuestMemoryStats, +} + +#[derive(Deserialize, Debug)] +struct Empty {} + +type ReplyChannel = mpsc::Sender; +type CommandChannel = mpsc::Sender<(QmpCommand, ReplyChannel)>; + +struct QmpConnection { + path: PathBuf, + channel: RefCell>, + last_balloon: RefCell, +} + +impl QmpConnection { + fn new>(path: P) -> Self { + Self { + path: path.into(), + channel: RefCell::new(None), + last_balloon: RefCell::new(Instant::now()), + } + } + + async fn connect( + &self, + ) -> Result<( + impl std::future::Future, + mpsc::Receiver, + )> { + let mut stream = BufStream::new( + UnixStream::connect(&self.path) + .await + .context("Failed to connect to QMP socket")?, + ); + info!("Connected to {}", self.path.display()); + let mut buf = vec![]; + stream.read_until(b'\n', &mut buf).await?; + buf.clear(); + stream + .write_all(&serde_json::to_vec(&QmpCommand::new("qmp_capabilities"))?) + .await?; + stream.write_all(b"\n").await?; + stream.flush().await?; + stream.read_until(b'\n', &mut buf).await?; + + let (sender, mut receiver) = mpsc::channel(16); + let (evsender, evreceiver) = mpsc::channel(16); + *self.channel.borrow_mut() = Some(sender); + let mut tx: Option = None; + let task = async move { + loop { + if let Some(curtx) = tx.take() { + buf.clear(); + while let Ok(len) = stream.read_until(b'\n', &mut buf).await { + if len == 0 { + return; + } + let Ok(serde_json::Value::Object(mut data)) = serde_json::from_slice(&buf) + else { + continue; + }; + if let Some(reply) = data.remove("return") { + let _ = curtx.send(reply).await; + break; + } else if evsender + .send(serde_json::Value::Object(data)) + .await + .is_err() + { + return; + } + buf.clear(); + } + } else { + buf.clear(); + tokio::select! { + cmd = receiver.recv() => { + let Some((cmd, newtx)) = cmd else { break; }; + if let Ok(vec) = serde_json::to_vec(&cmd) { + if stream.write_all(&vec).await.is_err() || + stream.write_all(b"\n").await.is_err() || + stream.flush().await.is_err() { + return; + } + tx = Some(newtx); + } else { + warn!("Command serialization failed"); + } + }, + Ok(len) = stream.read_until(b'\n', &mut buf) => { + if len == 0 { + return; + } + let Ok(data) = serde_json::from_slice(&buf) else { continue; }; + let serde_json::Value::Object(data) = data else { continue; }; + if evsender.send(serde_json::Value::Object(data)).await.is_err() { + return; + } + }, + } + } + } + }; + + Ok((task, evreceiver)) + } + + pub async fn disconnect(&self) -> Result<()> { + self.channel.borrow_mut().take(); + Ok(()) + } + + async fn send_command Deserialize<'a>>(&self, cmd: QmpCommand) -> Result { + let (tx, mut rx) = mpsc::channel(1); + let Some(channel) = self.channel.borrow().as_ref().cloned() else { + bail!("Not connected"); + }; + channel.send((cmd, tx)).await?; + Ok(serde_json::from_value( + rx.recv().await.context("Invalid response")?, + )?) + } + + pub async fn query_balloon(&self) -> Result { + let cmd = QmpCommand::new("query-balloon"); + self.send_command(cmd).await + } + + pub async fn balloon(&self, size: usize) -> Result<()> { + let cmd = QmpCommand::new("balloon").arg("value", size); + self.send_command::(cmd) + .await + .map(|_| ()) + .inspect(|_| *self.last_balloon.borrow_mut() = Instant::now()) + } + + pub async fn query_memory(&self) -> Result { + let cmd = QmpCommand::new("query-memory-size-summary"); + self.send_command(cmd).await + } + + pub async fn set_stats_interval(&self, ival: std::time::Duration) -> Result<()> { + let cmd = QmpCommand::new("qom-set") + .arg("path", "/machine/peripheral/balloon0") + .arg("property", "guest-stats-polling-interval") + .arg("value", ival.as_secs()); + self.send_command::(cmd).await.map(|_| ()) + } + + pub async fn query_stats(&self) -> Result { + let cmd = QmpCommand::new("qom-get") + .arg("path", "/machine/peripheral/balloon0") + .arg("property", "guest-stats"); + self.send_command(cmd).await + } +} + +#[derive(Debug)] +struct MemoryStats { + balloon_size: usize, + base_memory: usize, + plugged_memory: usize, + total_memory: usize, + free_memory: usize, + available_memory: usize, +} + +impl MemoryStats { + pub fn pressure(&self) -> u8 { + ((self.balloon_size - self.available_memory) as f64 * 100. / self.balloon_size as f64) + .round() as u8 + } + + pub fn reserved(&self) -> usize { + self.balloon_size - self.available_memory + } +} + +impl std::fmt::Display for MemoryStats { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + write!( + f, + "Memory stats:\n\ + Balloon size: {} MiB\n\ + Base memory: {} MiB\n\ + Plugged memory: {} MiB\n\ + Total memory: {} MiB\n\ + Free memory: {} MiB\n\ + Available memory: {} MiB", + self.balloon_size / 1024 / 1024, + self.base_memory / 1024 / 1024, + self.plugged_memory / 1024 / 1024, + self.total_memory / 1024 / 1024, + self.free_memory / 1024 / 1024, + self.available_memory / 1024 / 1024 + ) + } +} + +async fn monitor_memory(args: Args) -> Result<()> { + let qmps: Vec<_> = args.socket.iter().map(QmpConnection::new).collect(); + let dur = Duration::from_secs(args.interval); + let mut ival = tokio::time::interval(dur); + let mut last = None; + + loop { + ival.tick().await; + for qmp in &qmps { + let (task, mut receiver) = match qmp.connect().await { + Ok(a) => a, + Err(e) => { + warn!( + "Connection to {} failed: {e}, trying again later", + qmp.path.display() + ); + continue; + } + }; + tokio::select! { + e = async { + qmp.set_stats_interval(dur).await?; + let balloon = qmp.query_balloon().await?; + let memory = qmp.query_memory().await?; + let guest_stats = qmp.query_stats().await?; + + #[allow(clippy::nonminimal_bool)] + if !last.is_some_and(|last| last == guest_stats.last_update) { + last = Some(guest_stats.last_update); + let stats = MemoryStats { + balloon_size: balloon.actual, + base_memory: memory.base_memory, + plugged_memory: memory.plugged_memory, + total_memory: memory.base_memory + memory.plugged_memory, + free_memory: guest_stats.stats.stat_free_memory, + available_memory: guest_stats.stats.stat_available_memory, + }; + + let pressure = stats.pressure(); + if let Some(target) = if pressure < args.low { + if qmp.last_balloon.borrow().elapsed().as_secs() > args.balloon_interval { + info!("Pressure below limit, inflating balloon"); + Some(stats.reserved() * 100 / args.low as usize) + } else { + info!("Pressure below limit, waiting for stabilisation"); + None + } + } else if pressure > args.high { + if qmp.last_balloon.borrow().elapsed().as_secs() > args.balloon_interval { + info!("Pressure above limit, deflating balloon"); + Some(stats.total_memory.min(stats.reserved() * 100 / (args.high as usize - 2))) + } else { + info!("Pressure above limit, waiting for stabilisation"); + None + } + } else { + None + } { + let target = target.clamp(args.minimum, args.maximum); + if target != stats.balloon_size { + qmp.balloon(target).await?; + } + } + } + + qmp.disconnect().await + } => e, + _ = task => Ok(()), + _ = async move { + while let Some(e) = receiver.recv().await { + info!("Got event: {e:?}"); + } + } => Ok(()), + }?; + } + } +} + +#[tokio::main(flavor = "current_thread")] +async fn main() -> Result<()> { + tracing_subscriber::fmt::init(); + let args = Args::parse(); + monitor_memory(args).await +}