[libvirt] [PATCH 31/53] vircgroup: introduce virCgroupV2GetMemoryUsage

Pavel Hrdina posted 53 patches 6 years, 7 months ago
There is a newer version of this series
[libvirt] [PATCH 31/53] vircgroup: introduce virCgroupV2GetMemoryUsage
Posted by Pavel Hrdina 6 years, 7 months ago
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 src/util/vircgroupv2.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
index da3b3a984c..9f37ff5be5 100644
--- a/src/util/vircgroupv2.c
+++ b/src/util/vircgroupv2.c
@@ -1118,6 +1118,20 @@ virCgroupV2GetMemoryStat(virCgroupPtr group,
 }
 
 
+static int
+virCgroupV2GetMemoryUsage(virCgroupPtr group,
+                          unsigned long *kb)
+{
+    long long unsigned int usage_in_bytes;
+    int ret = virCgroupGetValueU64(group,
+                                   VIR_CGROUP_CONTROLLER_MEMORY,
+                                   "memory.current", &usage_in_bytes);
+    if (ret == 0)
+        *kb = (unsigned long) usage_in_bytes >> 10;
+    return ret;
+}
+
+
 virCgroupBackend virCgroupV2Backend = {
     .type = VIR_CGROUP_BACKEND_TYPE_V2,
 
@@ -1157,6 +1171,7 @@ virCgroupBackend virCgroupV2Backend = {
 
     .setMemory = virCgroupV2SetMemory,
     .getMemoryStat = virCgroupV2GetMemoryStat,
+    .getMemoryUsage = virCgroupV2GetMemoryUsage,
 };
 
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 31/53] vircgroup: introduce virCgroupV2GetMemoryUsage
Posted by Michal Privoznik 6 years, 7 months ago
On 10/02/2018 10:44 AM, Pavel Hrdina wrote:
> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> ---
>  src/util/vircgroupv2.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/src/util/vircgroupv2.c b/src/util/vircgroupv2.c
> index da3b3a984c..9f37ff5be5 100644
> --- a/src/util/vircgroupv2.c
> +++ b/src/util/vircgroupv2.c
> @@ -1118,6 +1118,20 @@ virCgroupV2GetMemoryStat(virCgroupPtr group,
>  }
>  
>  
> +static int
> +virCgroupV2GetMemoryUsage(virCgroupPtr group,
> +                          unsigned long *kb)
> +{
> +    long long unsigned int usage_in_bytes;

or unsigned long long usage_in_bytes;

> +    int ret = virCgroupGetValueU64(group,
> +                                   VIR_CGROUP_CONTROLLER_MEMORY,
> +                                   "memory.current", &usage_in_bytes);
> +    if (ret == 0)
> +        *kb = (unsigned long) usage_in_bytes >> 10;

I wonder if this is safe. Does ULONG_MAX << 10 = ULLONG_MAX?
On the other hand, on 32bit machine you won't have more than 4GiB of RAM
anyway and on 64bit UL and ULL are going to be probably the same.

> +    return ret;
> +}
> +
> +
>  virCgroupBackend virCgroupV2Backend = {
>      .type = VIR_CGROUP_BACKEND_TYPE_V2,
>  
> @@ -1157,6 +1171,7 @@ virCgroupBackend virCgroupV2Backend = {
>  
>      .setMemory = virCgroupV2SetMemory,
>      .getMemoryStat = virCgroupV2GetMemoryStat,
> +    .getMemoryUsage = virCgroupV2GetMemoryUsage,
>  };
>  
>  
> 


ACK

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list