[PATCH] Add MemAvailable to node memory stats

Takashi Kajinami posted 1 patch 3 days, 17 hours ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20260415171757.2146769-1-kajinamit@oss.nttdata.com
include/libvirt/libvirt-host.h | 10 ++++++++++
src/libvirt-host.c             |  2 ++
src/util/virhostmem.c          |  3 ++-
3 files changed, 14 insertions(+), 1 deletion(-)
[PATCH] Add MemAvailable to node memory stats
Posted by Takashi Kajinami 3 days, 17 hours ago
MemAvailable was introduced in kernel version 3.10 (and it was even
backported to older kernels in some distributions) and has been
a quite popular method to estimate the available method (totally fully
amount + reclaimable amount).

Signed-off-by: Takashi Kajinami <kajinamit@oss.nttdata.com>
---
 include/libvirt/libvirt-host.h | 10 ++++++++++
 src/libvirt-host.c             |  2 ++
 src/util/virhostmem.c          |  3 ++-
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h
index 398b40c35a..5b448e7954 100644
--- a/include/libvirt/libvirt-host.h
+++ b/include/libvirt/libvirt-host.h
@@ -364,6 +364,16 @@ typedef enum {
  */
 # define VIR_NODE_MEMORY_STATS_CACHED "cached"
 
+/**
+ * VIR_NODE_MEMORY_STATS_AVAILABLE:
+ *
+ * Macro for the available memory: On Linux, it is only returned in case of
+ * VIR_NODE_MEMORY_STATS_ALL_CELLS.
+ *
+ * Since: 12.3.0
+ */
+# define VIR_NODE_MEMORY_STATS_AVAILABLE "available"
+
 /**
  * virNodeMemoryStats:
  *
diff --git a/src/libvirt-host.c b/src/libvirt-host.c
index da75f5f30b..6b4345b09d 100644
--- a/src/libvirt-host.c
+++ b/src/libvirt-host.c
@@ -633,6 +633,8 @@ virNodeGetCPUStats(virConnectPtr conn,
  *     The buffers memory usage.(KB)
  * VIR_NODE_MEMORY_STATS_CACHED:
  *     The cached memory usage.(KB)
+ * VIR_NODE_MEMORY_STATS_AVAILABLE:
+ *     The available memory usage.(KB)
  *
  * Returns -1 in case of error, 0 in case of success.
  *
diff --git a/src/util/virhostmem.c b/src/util/virhostmem.c
index a7027af835..09a322fdea 100644
--- a/src/util/virhostmem.c
+++ b/src/util/virhostmem.c
@@ -136,7 +136,7 @@ virHostMemGetStatsFreeBSD(virNodeMemoryStatsPtr params,
 # define SYSFS_MEMORY_SHARED_PATH "/sys/kernel/mm/ksm"
 # define SYSFS_THREAD_SIBLINGS_LIST_LENGTH_MAX 8192
 
-# define LINUX_NB_MEMORY_STATS_ALL 4
+# define LINUX_NB_MEMORY_STATS_ALL 5
 # define LINUX_NB_MEMORY_STATS_CELL 2
 
 static int
@@ -159,6 +159,7 @@ virHostMemGetStatsLinux(FILE *meminfo,
         {"MemFree:",  VIR_NODE_MEMORY_STATS_FREE},
         {"Buffers:",  VIR_NODE_MEMORY_STATS_BUFFERS},
         {"Cached:",   VIR_NODE_MEMORY_STATS_CACHED},
+        {"MemAvailable:",   VIR_NODE_MEMORY_STATS_AVAILABLE},
         {NULL,        NULL}
     };
 
-- 
2.49.0
Re: [PATCH] Add MemAvailable to node memory stats
Posted by Peter Krempa via Devel 2 days, 2 hours ago
On Thu, Apr 16, 2026 at 02:17:56 +0900, Takashi Kajinami wrote:
> MemAvailable was introduced in kernel version 3.10 (and it was even
> backported to older kernels in some distributions) and has been
> a quite popular method to estimate the available method (totally fully
> amount + reclaimable amount).
> 
> Signed-off-by: Takashi Kajinami <kajinamit@oss.nttdata.com>
> ---
>  include/libvirt/libvirt-host.h | 10 ++++++++++
>  src/libvirt-host.c             |  2 ++
>  src/util/virhostmem.c          |  3 ++-
>  3 files changed, 14 insertions(+), 1 deletion(-)

The new field is too long for the formating strings in virsh:

 $ virsh nodememstats
 total  :             63393452 KiB
 free   :              5009296 KiB
 available:             35725324 KiB
 buffers:              2279316 KiB
 cached :             23792344 KiB


Reviewed-by: Peter Krempa <pkrempa@redhat.com>

and I'll push this shortly. I'll see if it makes sense to refactor the
virsh printing via vshTable, leave it as is or just add 2 to the spacing
value, but that's outside of the scope of this patch.