Skip to contentSkip to conversation

libxl: Wait for ballooning if free memory is increasing

PATCH Applied Mike Latimer 3 participants 7 messages Jan 30, 2015 – Feb 13, 2015

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 libxl: Wait for ballooning if free memory is increasing

During domain startup, all required memory ballooning must complete within a maximum window of 33 seconds (3 retries, 11 seconds of delay). If not, domain creation is aborted with a 'failed to free memory' error.

In order to accommodate large domains or slower hardware (which require substantially longer to balloon memory) the free memory process should continue retrying if the amount of free memory is increasing on each iteration of the loop.

---
 tools/libxl/xl_cmdimpl.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +14 -3
index 0b02a6c..9ff3c4f 100644
@@ -2194,8 +2194,9 @@ static int preserve_domain(uint32_t *r_domid, libxl_event *event,
 
 static int freemem(uint32_t domid, libxl_domain_build_info *b_info)
 {
-    int rc, retries = 3;
-    uint32_t need_memkb, free_memkb;
+    int rc, retries;
+    const int MAX_RETRIES = 3;
+    uint32_t need_memkb, free_memkb, free_memkb_prev = 0;
 
     if (!autoballoon)
         return 0;
@@ -2204,6 +2205,7 @@ static int freemem(uint32_t domid, libxl_domain_build_info *b_info)
     if (rc < 0)
         return rc;
 
+    retries = MAX_RETRIES;
     do {
         rc = libxl_get_free_memory(ctx, &free_memkb);
         if (rc < 0)
@@ -2228,7 +2230,16 @@ static int freemem(uint32_t domid, libxl_domain_build_info *b_info)
         if (rc < 0)
             return rc;
 
-        retries--;
+        /*
+         * If the amount of free mem has increased on this iteration (i.e.
+         * some progress has been made) then reset the retry counter.
+         */
+        if (free_memkb > free_memkb_prev) {
+            retries = MAX_RETRIES;
+            free_memkb_prev = free_memkb;
+        } else {
+            retries--;
+        }
     } while (retries > 0);
 
     return ERROR_NOMEM;
Signature · 1 line
1.8.4.5
#1 lore raw
Quoted 8 lines · Mike Latimer

During domain startup, all required memory ballooning must complete within a maximum window of 33 seconds (3 retries, 11 seconds of delay). If not, domain creation is aborted with a 'failed to free memory' error.

In order to accommodate large domains or slower hardware (which require substantially longer to balloon memory) the free memory process should continue retrying if the amount of free memory is increasing on each iteration of the loop.

Sorry for not spotting this earlier, but to accept a patch into the Xen code base we need a Signed-off-by:
http://wiki.xen.org/wiki/Submitting_Xen_Patches#Signing_off_a_patch

You can provide one in this thread rather than resending if that is more convenient.

For my part: Acked-by: Ian Campbell <[email protected]>

Quoted 47 lines
---
 tools/libxl/xl_cmdimpl.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +14 -3
index 0b02a6c..9ff3c4f 100644
@@ -2194,8 +2194,9 @@ static int preserve_domain(uint32_t *r_domid, libxl_event *event,
 
 static int freemem(uint32_t domid, libxl_domain_build_info *b_info)
 {
-    int rc, retries = 3;
-    uint32_t need_memkb, free_memkb;
+    int rc, retries;
+    const int MAX_RETRIES = 3;
+    uint32_t need_memkb, free_memkb, free_memkb_prev = 0;
 
     if (!autoballoon)
         return 0;
@@ -2204,6 +2205,7 @@ static int freemem(uint32_t domid, libxl_domain_build_info *b_info)
     if (rc < 0)
         return rc;
 
+    retries = MAX_RETRIES;
     do {
         rc = libxl_get_free_memory(ctx, &free_memkb);
         if (rc < 0)
@@ -2228,7 +2230,16 @@ static int freemem(uint32_t domid, libxl_domain_build_info *b_info)
         if (rc < 0)
             return rc;
 
-        retries--;
+        /*
+         * If the amount of free mem has increased on this iteration (i.e.
+         * some progress has been made) then reset the retry counter.
+         */
+        if (free_memkb > free_memkb_prev) {
+            retries = MAX_RETRIES;
+            free_memkb_prev = free_memkb;
+        } else {
+            retries--;
+        }
     } while (retries > 0);
 
     return ERROR_NOMEM;
#2 lore raw
Quoted 10 lines · Ian Campbell
Quoted 8 lines · Mike Latimer

During domain startup, all required memory ballooning must complete within a maximum window of 33 seconds (3 retries, 11 seconds of delay). If not, domain creation is aborted with a 'failed to free memory' error.

In order to accommodate large domains or slower hardware (which require substantially longer to balloon memory) the free memory process should continue retrying if the amount of free memory is increasing on each iteration of the loop.

Quoted 6 lines

Sorry for not spotting this earlier, but to accept a patch into the Xen code base we need a Signed-off-by:
http://wiki.xen.org/wiki/Submitting_Xen_Patches#Signing_off_a_patch

You can provide one in this thread rather than resending if that is more convenient.

Sorry, I should have caught that myself. If's it's okay to provide it here:

Quoted 1 line

For my part: Acked-by: Ian Campbell <[email protected]>

Thanks,
Mike

Quoted 59 lines
Quoted 59 lines

---

 tools/libxl/xl_cmdimpl.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/libxl/xl_cmdimpl.c +0 -0
index 0b02a6c..9ff3c4f 100644
@@ -2194,8 +2194,9 @@ static int preserve_domain(uint32_t *r_domid,

libxl_event *event,>
static int freemem(uint32_t domid, libxl_domain_build_info *b_info) {

tools/libxl/xl_cmdimpl.c +3 -2
-    int rc, retries = 3;
-    uint32_t need_memkb, free_memkb;
+    int rc, retries;
+    const int MAX_RETRIES = 3;
+    uint32_t need_memkb, free_memkb, free_memkb_prev = 0;
 
     if (!autoballoon)
 
         return 0;
@@ -2204,6 +2205,7 @@ static int freemem(uint32_t domid,

libxl_domain_build_info *b_info)>

     if (rc < 0)
     
         return rc;
tools/libxl/xl_cmdimpl.c +1 -0
+    retries = MAX_RETRIES;
 
     do {
 
         rc = libxl_get_free_memory(ctx, &free_memkb);
         if (rc < 0)
@@ -2228,7 +2230,16 @@ static int freemem(uint32_t domid,

libxl_domain_build_info *b_info)>

         if (rc < 0)
         
             return rc;
tools/libxl/xl_cmdimpl.c +2 -1
-        retries--;
+        /*
+         * If the amount of free mem has increased on this iteration

(i.e.

tools/libxl/xl_cmdimpl.c +8 -0
+         * some progress has been made) then reset the retry counter.
+         */
+        if (free_memkb > free_memkb_prev) {
+            retries = MAX_RETRIES;
+            free_memkb_prev = free_memkb;
+        } else {
+            retries--;
+        }
 
     } while (retries > 0);
 
     return ERROR_NOMEM;
Applied
#3 lore raw
Quoted 15 lines · Mike Latimer
Quoted 14 lines · Ian Campbell
Quoted 8 lines · Mike Latimer

During domain startup, all required memory ballooning must complete within a maximum window of 33 seconds (3 retries, 11 seconds of delay). If not, domain creation is aborted with a 'failed to free memory' error.

In order to accommodate large domains or slower hardware (which require substantially longer to balloon memory) the free memory process should continue retrying if the amount of free memory is increasing on each iteration of the loop.

Sorry for not spotting this earlier, but to accept a patch into the Xen code base we need a Signed-off-by:
http://wiki.xen.org/wiki/Submitting_Xen_Patches#Signing_off_a_patch

Quoted 6 lines
Quoted 2 lines

You can provide one in this thread rather than resending if that is more convenient.

Sorry, I should have caught that myself. If's it's okay to provide it here:

Thanks, applied.

#4 lore raw
Quoted 8 lines · Ian Campbell
Quoted 7 lines · Mike Latimer
Quoted 6 lines · Ian Campbell
Quoted 5 lines · Mike Latimer

During domain startup, all required memory ballooning must complete within a maximum window of 33 seconds (3 retries, 11 seconds of delay).
If not, domain creation is aborted with a 'failed to free memory' error.

...

Quoted 1 line

Thanks, applied.

Unfortunately, I just found an issue with this codepath...

In tools/libxl/xl_cmdimpl.c:freemem, the following call sets the memory target for dom0:

  rc = libxl_set_memory_target(ctx, 0, free_memkb - need_memkb, 1, 0);

This reduces the memory target of dom0 by the amount of memory still needed - relative to the amount of memory currently free. In other words, during each iteration of the loop, dom0's target memory is set lower, and lower, and lower...

Prior to my changes, this issue would only be noticed when starting very large domains - due to the loop being limited to 3 iterations. (For example, when ballooning 512G, dom0 memory could be reduced up to 1.5T.) With my changes, this loop can take several more iterations (10 or many more in testing). With each iteration lowering dom0's target, the end result can be dom0 ballooning down 100's of gigabytes, just to satisfy a much smaller request. (On one machine, a 64G guest can result in dom0 ballooning down 500G.)

It seems like a proper fix would be to use libxl_get_memory_target to first check dom0's target and see if:

(free_memory + (dom0_current_mem - dom0_target_mem)) >= needed_memory

If so, there will be sufficient memory when dom0 reaches the target, so don't change it. Before going down that road, I'd like a sanity check on that theory, and any advice on the overall picture (including my thread on freemem- slack).

I'll leave it up to you if my patch should be reverted until a new patch is created.

Thanks!
Mike

#5 lore raw
Quoted 24 lines · Mike Latimer
Quoted 8 lines · Ian Campbell
Quoted 7 lines · Mike Latimer
Quoted 6 lines · Ian Campbell
Quoted 5 lines · Mike Latimer

During domain startup, all required memory ballooning must complete within a maximum window of 33 seconds (3 retries, 11 seconds of delay).
If not, domain creation is aborted with a 'failed to free memory' error.

...

Quoted 1 line

Thanks, applied.

Unfortunately, I just found an issue with this codepath...

In tools/libxl/xl_cmdimpl.c:freemem, the following call sets the memory target for dom0:

  rc = libxl_set_memory_target(ctx, 0, free_memkb - need_memkb, 1, 0);

This reduces the memory target of dom0 by the amount of memory still needed - relative to the amount of memory currently free. In other words, during each iteration of the loop, dom0's target memory is set lower, and lower, and lower...

Quoted 8 lines

Prior to my changes, this issue would only be noticed when starting very large domains - due to the loop being limited to 3 iterations. (For example, when ballooning 512G, dom0 memory could be reduced up to 1.5T.) With my changes, this loop can take several more iterations (10 or many more in testing). With each iteration lowering dom0's target, the end result can be dom0 ballooning down 100's of gigabytes, just to satisfy a much smaller request. (On one machine, a 64G guest can result in dom0 ballooning down 500G.)

I'm curious why freemem doesn't not return when dom0 is ballooned down 64G? I.e. I think libxl_wait_for_free_memory should return 0 in that case and freeme should just return. Does it suggest there are other domains competing for free memory and dom0 is doing the right thing?

Wei.

Quoted 15 lines

It seems like a proper fix would be to use libxl_get_memory_target to first check dom0's target and see if:

(free_memory + (dom0_current_mem - dom0_target_mem)) >= needed_memory

If so, there will be sufficient memory when dom0 reaches the target, so don't change it. Before going down that road, I'd like a sanity check on that theory, and any advice on the overall picture (including my thread on freemem- slack).

I'll leave it up to you if my patch should be reverted until a new patch is created.

Thanks!
Mike

#6 lore raw

Hi Wei,

Quoted 10 lines · Wei Liu
Quoted 8 lines · Mike Latimer

Prior to my changes, this issue would only be noticed when starting very large domains - due to the loop being limited to 3 iterations. (For example, when ballooning 512G, dom0 memory could be reduced up to 1.5T.) With my changes, this loop can take several more iterations (10 or many more in testing). With each iteration lowering dom0's target, the end result can be dom0 ballooning down 100's of gigabytes, just to satisfy a much smaller request. (On one machine, a 64G guest can result in dom0 ballooning down 500G.)

Quoted 4 lines

I'm curious why freemem doesn't not return when dom0 is ballooned down 64G? I.e. I think libxl_wait_for_free_memory should return 0 in that case and freeme should just return. Does it suggest there are other domains competing for free memory and dom0 is doing the right thing?

I was only testing with a single domain. freemem returns 0 until freemem-slack is free, then the difference between freemem and freemem-slack is returned.

However, there are actually a couple of different problems here - and the whole situation is closely related to freemem_slack. As the patch submitted with this thread is only a victim here, maybe it's best to move the discussion over to the freemem-slack thread. I'll reply in detail there.

Thanks!
Mike