This commit implements two methods to get kernel memory using CGroup V1.
One of them get the Hard Limit of kernel memory and the other one get
the current kernel memory usage from that current container. For V2, it
will return error because it is not defined.
Signed-off-by: Julio Faracco <jcfaracco(a)gmail.com>
---
src/libvirt_private.syms | 2 ++
src/util/vircgroup.c | 32 ++++++++++++++++++++++++++++++++
src/util/vircgroup.h | 2 ++
src/util/vircgroupbackend.h | 10 ++++++++++
src/util/vircgroupv1.c | 36 ++++++++++++++++++++++++++++++++++++
5 files changed, 82 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 212adf53c1..0c2328de0e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1584,6 +1584,8 @@ virCgroupGetMemoryStat;
virCgroupGetMemoryUsage;
virCgroupGetMemSwapHardLimit;
virCgroupGetMemSwapUsage;
+virCgroupGetKMemHardLimit;
+virCgroupGetKMemUsage;
virCgroupGetPercpuStats;
virCgroupHasController;
virCgroupHasEmptyTasks;
diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 34c970f292..2712ae32e6 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -1784,6 +1784,38 @@ virCgroupGetMemSwapUsage(virCgroupPtr group, unsigned long long
*kb)
}
+/**
+ * virCgroupGetKMemHardLimit:
+ *
+ * @group: The cgroup to get mem+kmem hard limit for
+ * @kb: The mem+kmem amount in kilobytes
+ *
+ * Returns: 0 on success
+ */
+int
+virCgroupGetKMemHardLimit(virCgroupPtr group, unsigned long long *kb)
+{
+ VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
+ getKMemHardLimit, -1, kb);
+}
+
+
+/**
+ * virCgroupGetKMemUsage:
+ *
+ * @group: The cgroup to get mem+kmem usage for
+ * @kb: The mem+kmem amount in kilobytes
+ *
+ * Returns: 0 on success
+ */
+int
+virCgroupGetKMemUsage(virCgroupPtr group, unsigned long long *kb)
+{
+ VIR_CGROUP_BACKEND_CALL(group, VIR_CGROUP_CONTROLLER_MEMORY,
+ getKMemUsage, -1, kb);
+}
+
+
/**
* virCgroupSetCpusetMems:
*
diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h
index 372009de4a..edd13742d2 100644
--- a/src/util/vircgroup.h
+++ b/src/util/vircgroup.h
@@ -191,6 +191,8 @@ int virCgroupGetMemorySoftLimit(virCgroupPtr group, unsigned long long
*kb);
int virCgroupSetMemSwapHardLimit(virCgroupPtr group, unsigned long long kb);
int virCgroupGetMemSwapHardLimit(virCgroupPtr group, unsigned long long *kb);
int virCgroupGetMemSwapUsage(virCgroupPtr group, unsigned long long *kb);
+int virCgroupGetKMemHardLimit(virCgroupPtr group, unsigned long long *kb);
+int virCgroupGetKMemUsage(virCgroupPtr group, unsigned long long *kb);
enum {
VIR_CGROUP_DEVICE_READ = 1,
diff --git a/src/util/vircgroupbackend.h b/src/util/vircgroupbackend.h
index 24b45be9bb..db4369325b 100644
--- a/src/util/vircgroupbackend.h
+++ b/src/util/vircgroupbackend.h
@@ -263,6 +263,14 @@ typedef int
(*virCgroupGetMemSwapUsageCB)(virCgroupPtr group,
unsigned long long *kb);
+typedef int
+(*virCgroupGetKMemHardLimitCB)(virCgroupPtr group,
+ unsigned long long *kb);
+
+typedef int
+(*virCgroupGetKMemUsageCB)(virCgroupPtr group,
+ unsigned long long *kb);
+
typedef int
(*virCgroupAllowDeviceCB)(virCgroupPtr group,
char type,
@@ -406,6 +414,8 @@ struct _virCgroupBackend {
virCgroupSetMemSwapHardLimitCB setMemSwapHardLimit;
virCgroupGetMemSwapHardLimitCB getMemSwapHardLimit;
virCgroupGetMemSwapUsageCB getMemSwapUsage;
+ virCgroupGetKMemHardLimitCB getKMemHardLimit;
+ virCgroupGetKMemUsageCB getKMemUsage;
virCgroupAllowDeviceCB allowDevice;
virCgroupDenyDeviceCB denyDevice;
diff --git a/src/util/vircgroupv1.c b/src/util/vircgroupv1.c
index c172eb8b96..268088ff0c 100644
--- a/src/util/vircgroupv1.c
+++ b/src/util/vircgroupv1.c
@@ -1690,6 +1690,40 @@ virCgroupV1GetMemSwapUsage(virCgroupPtr group,
}
+static int
+virCgroupV1GetKMemHardLimit(virCgroupPtr group,
+ unsigned long long *kb)
+{
+ long long unsigned int limit_in_bytes;
+
+ if (virCgroupGetValueU64(group,
+ VIR_CGROUP_CONTROLLER_MEMORY,
+ "memory.kmem.limit_in_bytes", &limit_in_bytes)
< 0)
+ return -1;
+
+ *kb = limit_in_bytes >> 10;
+ if (*kb >= virCgroupV1GetMemoryUnlimitedKB())
+ *kb = VIR_DOMAIN_MEMORY_PARAM_UNLIMITED;
+
+ return 0;
+}
+
+
+static int
+virCgroupV1GetKMemUsage(virCgroupPtr group,
+ unsigned long long *kb)
+{
+ long long unsigned int usage_in_bytes;
+ int ret;
+ ret = virCgroupGetValueU64(group,
+ VIR_CGROUP_CONTROLLER_MEMORY,
+ "memory.kmem.usage_in_bytes",
&usage_in_bytes);
+ if (ret == 0)
+ *kb = usage_in_bytes >> 10;
+ return ret;
+}
+
+
static int
virCgroupV1AllowDevice(virCgroupPtr group,
char type,
@@ -2085,6 +2119,8 @@ virCgroupBackend virCgroupV1Backend = {
.setMemSwapHardLimit = virCgroupV1SetMemSwapHardLimit,
.getMemSwapHardLimit = virCgroupV1GetMemSwapHardLimit,
.getMemSwapUsage = virCgroupV1GetMemSwapUsage,
+ .getKMemHardLimit = virCgroupV1GetKMemHardLimit,
+ .getKMemUsage = virCgroupV1GetKMemUsage,
.allowDevice = virCgroupV1AllowDevice,
.denyDevice = virCgroupV1DenyDevice,
--
2.19.1