diff mbox

migration: cache memory region ram ptr

Message ID 1399719075-11517-1-git-send-email-pl@kamp.de
State New
Headers show

Commit Message

Peter Lieven May 10, 2014, 10:51 a.m. UTC
we currently look up the ram ptr for each single page. Cache
the pointer while we operate on the same block.

Signed-off-by: Peter Lieven <pl@kamp.de>
---
 arch_init.c |   23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)

Comments

陈梁 May 10, 2014, 11:38 a.m. UTC | #1
Hi,
The patch is correct. There is a small improved point.
> 
>             /* In doubt sent page as normal */
>             bytes_sent = -1;
> @@ -990,16 +996,17 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>                                             int flags)
> {
>     static RAMBlock *block = NULL;
       RAMBlock *block = NULL;

> +    static uint8_t *ram_ptr;
>     char id[256];
>     uint8_t len;
> 
>     if (flags & RAM_SAVE_FLAG_CONTINUE) {
> -        if (!block) {
> +        if (!block || !ram_ptr) {

             if (!ram_ptr) {

Best regards
ChenLiang

>             fprintf(stderr, "Ack, bad migration stream!\n");
>             return NULL;
>         }
> 
> -        return memory_region_get_ram_ptr(block->mr) + offset;
> +        return ram_ptr + offset;
>     }
> 
>     len = qemu_get_byte(f);
> @@ -1007,8 +1014,10 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>     id[len] = 0;
> 
>     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
> -        if (!strncmp(id, block->idstr, sizeof(id)))
> -            return memory_region_get_ram_ptr(block->mr) + offset;
> +        if (!strncmp(id, block->idstr, sizeof(id))) {
> +            ram_ptr = memory_region_get_ram_ptr(block->mr);
> +            return ram_ptr + offset;
> +        }
>     }
> 
>     fprintf(stderr, "Can't find block %s!\n", id);
> -- 
> 1.7.9.5
> 
>
Peter Lieven May 10, 2014, 1:13 p.m. UTC | #2
Am 10.05.2014 13:38, schrieb 陈梁:
> Hi,
> The patch is correct. There is a small improved point.
>>             /* In doubt sent page as normal */
>>             bytes_sent = -1;
>> @@ -990,16 +996,17 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>>                                             int flags)
>> {
>>     static RAMBlock *block = NULL;
>        RAMBlock *block = NULL;

This one has to be static as well. Why do you think it should not?

Peter

>
>> +    static uint8_t *ram_ptr;
>>     char id[256];
>>     uint8_t len;
>>
>>     if (flags & RAM_SAVE_FLAG_CONTINUE) {
>> -        if (!block) {
>> +        if (!block || !ram_ptr) {
>              if (!ram_ptr) {
>
> Best regards
> ChenLiang
>
>>             fprintf(stderr, "Ack, bad migration stream!\n");
>>             return NULL;
>>         }
>>
>> -        return memory_region_get_ram_ptr(block->mr) + offset;
>> +        return ram_ptr + offset;
>>     }
>>
>>     len = qemu_get_byte(f);
>> @@ -1007,8 +1014,10 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>>     id[len] = 0;
>>
>>     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
>> -        if (!strncmp(id, block->idstr, sizeof(id)))
>> -            return memory_region_get_ram_ptr(block->mr) + offset;
>> +        if (!strncmp(id, block->idstr, sizeof(id))) {
>> +            ram_ptr = memory_region_get_ram_ptr(block->mr);
>> +            return ram_ptr + offset;
>> +        }
>>     }
>>
>>     fprintf(stderr, "Can't find block %s!\n", id);
>> -- 
>> 1.7.9.5
>>
>>
Paolo Bonzini May 10, 2014, 3:33 p.m. UTC | #3
Il 10/05/2014 12:51, Peter Lieven ha scritto:
> we currently look up the ram ptr for each single page. Cache
> the pointer while we operate on the same block.

Why don't you instead cache the result in the MemoryRegion, so that 
memory_region_get_ram_ptr becomes a simple, inline field access?

Paolo

> Signed-off-by: Peter Lieven <pl@kamp.de>
> ---
>  arch_init.c |   23 ++++++++++++++++-------
>  1 file changed, 16 insertions(+), 7 deletions(-)
>
> diff --git a/arch_init.c b/arch_init.c
> index 582b716..ce338aa 100644
> --- a/arch_init.c
> +++ b/arch_init.c
> @@ -594,13 +594,19 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
>                  ram_bulk_stage = false;
>              }
>          } else {
> +            static uint8_t *ram_ptr;
>              int ret;
>              uint8_t *p;
>              bool send_async = true;
> -            int cont = (block == last_sent_block) ?
> -                RAM_SAVE_FLAG_CONTINUE : 0;
> +            int cont = 0;
>
> -            p = memory_region_get_ram_ptr(mr) + offset;
> +            if (block != last_sent_block) {
> +                ram_ptr = memory_region_get_ram_ptr(mr);
> +            } else {
> +                cont = RAM_SAVE_FLAG_CONTINUE;
> +            }
> +
> +            p = ram_ptr + offset;
>
>              /* In doubt sent page as normal */
>              bytes_sent = -1;
> @@ -990,16 +996,17 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>                                              int flags)
>  {
>      static RAMBlock *block = NULL;
> +    static uint8_t *ram_ptr;
>      char id[256];
>      uint8_t len;
>
>      if (flags & RAM_SAVE_FLAG_CONTINUE) {
> -        if (!block) {
> +        if (!block || !ram_ptr) {
>              fprintf(stderr, "Ack, bad migration stream!\n");
>              return NULL;
>          }
>
> -        return memory_region_get_ram_ptr(block->mr) + offset;
> +        return ram_ptr + offset;
>      }
>
>      len = qemu_get_byte(f);
> @@ -1007,8 +1014,10 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>      id[len] = 0;
>
>      QTAILQ_FOREACH(block, &ram_list.blocks, next) {
> -        if (!strncmp(id, block->idstr, sizeof(id)))
> -            return memory_region_get_ram_ptr(block->mr) + offset;
> +        if (!strncmp(id, block->idstr, sizeof(id))) {
> +            ram_ptr = memory_region_get_ram_ptr(block->mr);
> +            return ram_ptr + offset;
> +        }
>      }
>
>      fprintf(stderr, "Can't find block %s!\n", id);
>
Peter Lieven May 10, 2014, 4:07 p.m. UTC | #4
You mean,

a) extent the MemoryRegion stuct with a pointer?

b) on the first call to memory_region_get_ram_ptr cache the result in the struct?

Peter

Am 10.05.2014 17:33, schrieb Paolo Bonzini:
> Il 10/05/2014 12:51, Peter Lieven ha scritto:
>> we currently look up the ram ptr for each single page. Cache
>> the pointer while we operate on the same block.
>
> Why don't you instead cache the result in the MemoryRegion, so that memory_region_get_ram_ptr becomes a simple, inline field access?
>
> Paolo
>
>> Signed-off-by: Peter Lieven <pl@kamp.de>
>> ---
>>  arch_init.c |   23 ++++++++++++++++-------
>>  1 file changed, 16 insertions(+), 7 deletions(-)
>>
>> diff --git a/arch_init.c b/arch_init.c
>> index 582b716..ce338aa 100644
>> --- a/arch_init.c
>> +++ b/arch_init.c
>> @@ -594,13 +594,19 @@ static int ram_save_block(QEMUFile *f, bool last_stage)
>>                  ram_bulk_stage = false;
>>              }
>>          } else {
>> +            static uint8_t *ram_ptr;
>>              int ret;
>>              uint8_t *p;
>>              bool send_async = true;
>> -            int cont = (block == last_sent_block) ?
>> -                RAM_SAVE_FLAG_CONTINUE : 0;
>> +            int cont = 0;
>>
>> -            p = memory_region_get_ram_ptr(mr) + offset;
>> +            if (block != last_sent_block) {
>> +                ram_ptr = memory_region_get_ram_ptr(mr);
>> +            } else {
>> +                cont = RAM_SAVE_FLAG_CONTINUE;
>> +            }
>> +
>> +            p = ram_ptr + offset;
>>
>>              /* In doubt sent page as normal */
>>              bytes_sent = -1;
>> @@ -990,16 +996,17 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>>                                              int flags)
>>  {
>>      static RAMBlock *block = NULL;
>> +    static uint8_t *ram_ptr;
>>      char id[256];
>>      uint8_t len;
>>
>>      if (flags & RAM_SAVE_FLAG_CONTINUE) {
>> -        if (!block) {
>> +        if (!block || !ram_ptr) {
>>              fprintf(stderr, "Ack, bad migration stream!\n");
>>              return NULL;
>>          }
>>
>> -        return memory_region_get_ram_ptr(block->mr) + offset;
>> +        return ram_ptr + offset;
>>      }
>>
>>      len = qemu_get_byte(f);
>> @@ -1007,8 +1014,10 @@ static inline void *host_from_stream_offset(QEMUFile *f,
>>      id[len] = 0;
>>
>>      QTAILQ_FOREACH(block, &ram_list.blocks, next) {
>> -        if (!strncmp(id, block->idstr, sizeof(id)))
>> -            return memory_region_get_ram_ptr(block->mr) + offset;
>> +        if (!strncmp(id, block->idstr, sizeof(id))) {
>> +            ram_ptr = memory_region_get_ram_ptr(block->mr);
>> +            return ram_ptr + offset;
>> +        }
>>      }
>>
>>      fprintf(stderr, "Can't find block %s!\n", id);
>>
>
diff mbox

Patch

diff --git a/arch_init.c b/arch_init.c
index 582b716..ce338aa 100644
--- a/arch_init.c
+++ b/arch_init.c
@@ -594,13 +594,19 @@  static int ram_save_block(QEMUFile *f, bool last_stage)
                 ram_bulk_stage = false;
             }
         } else {
+            static uint8_t *ram_ptr;
             int ret;
             uint8_t *p;
             bool send_async = true;
-            int cont = (block == last_sent_block) ?
-                RAM_SAVE_FLAG_CONTINUE : 0;
+            int cont = 0;
 
-            p = memory_region_get_ram_ptr(mr) + offset;
+            if (block != last_sent_block) {
+                ram_ptr = memory_region_get_ram_ptr(mr);
+            } else {
+                cont = RAM_SAVE_FLAG_CONTINUE;
+            }
+
+            p = ram_ptr + offset;
 
             /* In doubt sent page as normal */
             bytes_sent = -1;
@@ -990,16 +996,17 @@  static inline void *host_from_stream_offset(QEMUFile *f,
                                             int flags)
 {
     static RAMBlock *block = NULL;
+    static uint8_t *ram_ptr;
     char id[256];
     uint8_t len;
 
     if (flags & RAM_SAVE_FLAG_CONTINUE) {
-        if (!block) {
+        if (!block || !ram_ptr) {
             fprintf(stderr, "Ack, bad migration stream!\n");
             return NULL;
         }
 
-        return memory_region_get_ram_ptr(block->mr) + offset;
+        return ram_ptr + offset;
     }
 
     len = qemu_get_byte(f);
@@ -1007,8 +1014,10 @@  static inline void *host_from_stream_offset(QEMUFile *f,
     id[len] = 0;
 
     QTAILQ_FOREACH(block, &ram_list.blocks, next) {
-        if (!strncmp(id, block->idstr, sizeof(id)))
-            return memory_region_get_ram_ptr(block->mr) + offset;
+        if (!strncmp(id, block->idstr, sizeof(id))) {
+            ram_ptr = memory_region_get_ram_ptr(block->mr);
+            return ram_ptr + offset;
+        }
     }
 
     fprintf(stderr, "Can't find block %s!\n", id);