qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot
  2023-08-28 17:19 Hyman Huang
@ 2023-08-28 17:19 ` Hyman Huang
  2023-08-28 17:32   ` Hyman Huang
  0 siblings, 1 reply; 9+ messages in thread
From: Hyman Huang @ 2023-08-28 17:19 UTC (permalink / raw)
  To: qemu-devel; +Cc: Andrei Gudkov, Hyman Huang

From: Andrei Gudkov <gudkov.andrei@huawei.com>

Signed-off-by: Andrei Gudkov <gudkov.andrei@huawei.com>
Reviewed-by: Hyman Huang <yong.huang@smartx.com>
Message-Id: <8ddb0d40d143f77aab8f602bd494e01e5fa01614.1691161009.git.gudkov.andrei@huawei.com>
Signed-off-by: Hyman Huang <yong.huang@smartx.com>
---
 migration/dirtyrate.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
index 84f1b0fb20..bccb3515e3 100644
--- a/migration/dirtyrate.c
+++ b/migration/dirtyrate.c
@@ -57,6 +57,8 @@ static int64_t dirty_stat_wait(int64_t msec, int64_t initial_time)
         msec = current_time - initial_time;
     } else {
         g_usleep((msec + initial_time - current_time) * 1000);
+        /* g_usleep may overshoot */
+        msec = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - initial_time;
     }
 
     return msec;
@@ -77,9 +79,13 @@ static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages,
 {
     uint64_t increased_dirty_pages =
         dirty_pages.end_pages - dirty_pages.start_pages;
-    uint64_t memory_size_MiB = qemu_target_pages_to_MiB(increased_dirty_pages);
 
-    return memory_size_MiB * 1000 / calc_time_ms;
+    /*
+     * multiply by 1000ms/s _before_ converting down to megabytes
+     * to avoid losing precision
+     */
+    return qemu_target_pages_to_MiB(increased_dirty_pages * 1000) /
+        calc_time_ms;
 }
 
 void global_dirty_log_change(unsigned int flag, bool start)
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot
  2023-08-28 17:19 ` [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot Hyman Huang
@ 2023-08-28 17:32   ` Hyman Huang
  0 siblings, 0 replies; 9+ messages in thread
From: Hyman Huang @ 2023-08-28 17:32 UTC (permalink / raw)
  To: qemu-devel; +Cc: Andrei Gudkov, Hyman Huang

From: Andrei Gudkov <gudkov.andrei@huawei.com>

Signed-off-by: Andrei Gudkov <gudkov.andrei@huawei.com>
Reviewed-by: Hyman Huang <yong.huang@smartx.com>
Message-Id: <8ddb0d40d143f77aab8f602bd494e01e5fa01614.1691161009.git.gudkov.andrei@huawei.com>
Signed-off-by: Hyman Huang <yong.huang@smartx.com>
---
 migration/dirtyrate.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
index 84f1b0fb20..bccb3515e3 100644
--- a/migration/dirtyrate.c
+++ b/migration/dirtyrate.c
@@ -57,6 +57,8 @@ static int64_t dirty_stat_wait(int64_t msec, int64_t initial_time)
         msec = current_time - initial_time;
     } else {
         g_usleep((msec + initial_time - current_time) * 1000);
+        /* g_usleep may overshoot */
+        msec = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - initial_time;
     }
 
     return msec;
@@ -77,9 +79,13 @@ static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages,
 {
     uint64_t increased_dirty_pages =
         dirty_pages.end_pages - dirty_pages.start_pages;
-    uint64_t memory_size_MiB = qemu_target_pages_to_MiB(increased_dirty_pages);
 
-    return memory_size_MiB * 1000 / calc_time_ms;
+    /*
+     * multiply by 1000ms/s _before_ converting down to megabytes
+     * to avoid losing precision
+     */
+    return qemu_target_pages_to_MiB(increased_dirty_pages * 1000) /
+        calc_time_ms;
 }
 
 void global_dirty_log_change(unsigned int flag, bool start)
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches
@ 2023-08-29 15:29 Hyman Huang
  2023-08-29 15:29 ` [PULL 1/3] softmmu: Fix dirtylimit memory leak Hyman Huang
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Hyman Huang @ 2023-08-29 15:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: Hyman Huang

The following changes since commit f5fe7c17ac4e309e47e78f0f9761aebc8d2f2c81:

  Merge tag 'pull-tcg-20230823-2' of https://gitlab.com/rth7680/qemu into staging (2023-08-28 16:07:04 -0400)

are available in the Git repository at:

  https://github.com/newfriday/qemu.git tags/dirtylimit-dirtyrate-pull-request

for you to fetch changes up to 3eb82637fbf8c0471990b59e6733fd4beb1f9939:

  migration/dirtyrate: Fix precision losses and g_usleep overshoot (2023-08-29 10:19:03 +0800)

----------------------------------------------------------------
Dirtylimit and dirtyrate 20230829 patches PULL request

Correct memory leaks in dirtylimit and accuracy losses in
dirtyrate, respectively; make minor corrections to overshoot
and memory deallocation.

This is v3 with a signed tag. Please apply.

Thanks, Yong.

----------------------------------------------------------------
Andrei Gudkov (1):
      migration/dirtyrate: Fix precision losses and g_usleep overshoot

alloc.young (2):
      softmmu: Fix dirtylimit memory leak
      softmmu/dirtylimit: Convert free to g_free

 migration/dirtyrate.c | 10 ++++++++--
 softmmu/dirtylimit.c  | 26 ++++++++++++--------------
 2 files changed, 20 insertions(+), 16 deletions(-)

-- 
2.39.1



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PULL 1/3] softmmu: Fix dirtylimit memory leak
  2023-08-29 15:29 [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Hyman Huang
@ 2023-08-29 15:29 ` Hyman Huang
  2023-08-30 16:55   ` Michael Tokarev
  2023-08-29 15:29 ` [PULL 2/3] softmmu/dirtylimit: Convert free to g_free Hyman Huang
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Hyman Huang @ 2023-08-29 15:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: alloc.young, Hyman Huang

From: "alloc.young" <alloc.young@outlook.com>

Fix memory leak in hmp_info_vcpu_dirty_limit,use g_autoptr
to handle memory deallocation.

Signed-off-by: alloc.young <alloc.young@outlook.com>
Reviewed-by: Hyman Huang <yong.huang@smartx.com>
Message-Id: <SA1PR11MB6760B9AB7EAFBDAFB524ED06F5E3A@SA1PR11MB6760.namprd11.prod.outlook.com>
Signed-off-by: Hyman Huang <yong.huang@smartx.com>
---
 softmmu/dirtylimit.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/softmmu/dirtylimit.c b/softmmu/dirtylimit.c
index 3c275ee55b..e3ff53b8fc 100644
--- a/softmmu/dirtylimit.c
+++ b/softmmu/dirtylimit.c
@@ -653,7 +653,8 @@ struct DirtyLimitInfoList *qmp_query_vcpu_dirty_limit(Error **errp)
 
 void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict)
 {
-    DirtyLimitInfoList *limit, *head, *info = NULL;
+    DirtyLimitInfoList *info;
+    g_autoptr(DirtyLimitInfoList) head = NULL;
     Error *err = NULL;
 
     if (!dirtylimit_in_service()) {
@@ -661,20 +662,17 @@ void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict)
         return;
     }
 
-    info = qmp_query_vcpu_dirty_limit(&err);
+    head = qmp_query_vcpu_dirty_limit(&err);
     if (err) {
         hmp_handle_error(mon, err);
         return;
     }
 
-    head = info;
-    for (limit = head; limit != NULL; limit = limit->next) {
+    for (info = head; info != NULL; info = info->next) {
         monitor_printf(mon, "vcpu[%"PRIi64"], limit rate %"PRIi64 " (MB/s),"
                             " current rate %"PRIi64 " (MB/s)\n",
-                            limit->value->cpu_index,
-                            limit->value->limit_rate,
-                            limit->value->current_rate);
+                            info->value->cpu_index,
+                            info->value->limit_rate,
+                            info->value->current_rate);
     }
-
-    g_free(info);
 }
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PULL 2/3] softmmu/dirtylimit: Convert free to g_free
  2023-08-29 15:29 [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Hyman Huang
  2023-08-29 15:29 ` [PULL 1/3] softmmu: Fix dirtylimit memory leak Hyman Huang
@ 2023-08-29 15:29 ` Hyman Huang
  2023-08-29 15:29 ` [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot Hyman Huang
  2023-08-30 16:22 ` [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Stefan Hajnoczi
  3 siblings, 0 replies; 9+ messages in thread
From: Hyman Huang @ 2023-08-29 15:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: alloc.young, Hyman Huang, Philippe Mathieu-Daudé

From: "alloc.young" <alloc.young@outlook.com>

Convert free to g_free to match g_new and g_malloc functions.

Fixes: cc2b33eab0 ("softmmu/dirtylimit: Implement vCPU dirtyrate calculation periodically")
Fixes: baa609832e ("softmmu/dirtylimit: Implement virtual CPU throttle")
Signed-off-by: alloc.young <alloc.young@outlook.com>
Reviewed-by: Hyman Huang <yong.huang@smartx.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Message-Id: <SA1PR11MB67604ECD85AFF34BEB3072F7F5E3A@SA1PR11MB6760.namprd11.prod.outlook.com>
Signed-off-by: Hyman Huang <yong.huang@smartx.com>
---
 softmmu/dirtylimit.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/softmmu/dirtylimit.c b/softmmu/dirtylimit.c
index e3ff53b8fc..fa959d7743 100644
--- a/softmmu/dirtylimit.c
+++ b/softmmu/dirtylimit.c
@@ -100,7 +100,7 @@ static void vcpu_dirty_rate_stat_collect(void)
             stat.rates[i].dirty_rate;
     }
 
-    free(stat.rates);
+    g_free(stat.rates);
 }
 
 static void *vcpu_dirty_rate_stat_thread(void *opaque)
@@ -171,10 +171,10 @@ void vcpu_dirty_rate_stat_initialize(void)
 
 void vcpu_dirty_rate_stat_finalize(void)
 {
-    free(vcpu_dirty_rate_stat->stat.rates);
+    g_free(vcpu_dirty_rate_stat->stat.rates);
     vcpu_dirty_rate_stat->stat.rates = NULL;
 
-    free(vcpu_dirty_rate_stat);
+    g_free(vcpu_dirty_rate_stat);
     vcpu_dirty_rate_stat = NULL;
 }
 
@@ -220,10 +220,10 @@ void dirtylimit_state_initialize(void)
 
 void dirtylimit_state_finalize(void)
 {
-    free(dirtylimit_state->states);
+    g_free(dirtylimit_state->states);
     dirtylimit_state->states = NULL;
 
-    free(dirtylimit_state);
+    g_free(dirtylimit_state);
     dirtylimit_state = NULL;
 
     trace_dirtylimit_state_finalize();
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot
  2023-08-29 15:29 [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Hyman Huang
  2023-08-29 15:29 ` [PULL 1/3] softmmu: Fix dirtylimit memory leak Hyman Huang
  2023-08-29 15:29 ` [PULL 2/3] softmmu/dirtylimit: Convert free to g_free Hyman Huang
@ 2023-08-29 15:29 ` Hyman Huang
  2023-08-30 16:22 ` [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Stefan Hajnoczi
  3 siblings, 0 replies; 9+ messages in thread
From: Hyman Huang @ 2023-08-29 15:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: Andrei Gudkov, Hyman Huang

From: Andrei Gudkov <gudkov.andrei@huawei.com>

Signed-off-by: Andrei Gudkov <gudkov.andrei@huawei.com>
Reviewed-by: Hyman Huang <yong.huang@smartx.com>
Message-Id: <8ddb0d40d143f77aab8f602bd494e01e5fa01614.1691161009.git.gudkov.andrei@huawei.com>
Signed-off-by: Hyman Huang <yong.huang@smartx.com>
---
 migration/dirtyrate.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c
index 84f1b0fb20..bccb3515e3 100644
--- a/migration/dirtyrate.c
+++ b/migration/dirtyrate.c
@@ -57,6 +57,8 @@ static int64_t dirty_stat_wait(int64_t msec, int64_t initial_time)
         msec = current_time - initial_time;
     } else {
         g_usleep((msec + initial_time - current_time) * 1000);
+        /* g_usleep may overshoot */
+        msec = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - initial_time;
     }
 
     return msec;
@@ -77,9 +79,13 @@ static int64_t do_calculate_dirtyrate(DirtyPageRecord dirty_pages,
 {
     uint64_t increased_dirty_pages =
         dirty_pages.end_pages - dirty_pages.start_pages;
-    uint64_t memory_size_MiB = qemu_target_pages_to_MiB(increased_dirty_pages);
 
-    return memory_size_MiB * 1000 / calc_time_ms;
+    /*
+     * multiply by 1000ms/s _before_ converting down to megabytes
+     * to avoid losing precision
+     */
+    return qemu_target_pages_to_MiB(increased_dirty_pages * 1000) /
+        calc_time_ms;
 }
 
 void global_dirty_log_change(unsigned int flag, bool start)
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches
  2023-08-29 15:29 [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Hyman Huang
                   ` (2 preceding siblings ...)
  2023-08-29 15:29 ` [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot Hyman Huang
@ 2023-08-30 16:22 ` Stefan Hajnoczi
  3 siblings, 0 replies; 9+ messages in thread
From: Stefan Hajnoczi @ 2023-08-30 16:22 UTC (permalink / raw)
  To: Hyman Huang; +Cc: qemu-devel, Hyman Huang

[-- Attachment #1: Type: text/plain, Size: 115 bytes --]

Applied, thanks.

Please update the changelog at https://wiki.qemu.org/ChangeLog/8.2 for any user-visible changes.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PULL 1/3] softmmu: Fix dirtylimit memory leak
  2023-08-29 15:29 ` [PULL 1/3] softmmu: Fix dirtylimit memory leak Hyman Huang
@ 2023-08-30 16:55   ` Michael Tokarev
  2023-08-31  1:01     ` Yong Huang
  0 siblings, 1 reply; 9+ messages in thread
From: Michael Tokarev @ 2023-08-30 16:55 UTC (permalink / raw)
  To: Hyman Huang, qemu-devel; +Cc: alloc.young

29.08.2023 18:29, Hyman Huang wrote:
> From: "alloc.young" <alloc.young@outlook.com>
> 
> Fix memory leak in hmp_info_vcpu_dirty_limit,use g_autoptr
> to handle memory deallocation.

It does not feel like -stable-worthy, or am I wrong and it should be picked up
for -stable?

Thanks,

/mjt


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PULL 1/3] softmmu: Fix dirtylimit memory leak
  2023-08-30 16:55   ` Michael Tokarev
@ 2023-08-31  1:01     ` Yong Huang
  0 siblings, 0 replies; 9+ messages in thread
From: Yong Huang @ 2023-08-31  1:01 UTC (permalink / raw)
  To: Michael Tokarev; +Cc: qemu-devel, alloc.young

[-- Attachment #1: Type: text/plain, Size: 800 bytes --]

On Thu, Aug 31, 2023 at 12:55 AM Michael Tokarev <mjt@tls.msk.ru> wrote:

> 29.08.2023 18:29, Hyman Huang wrote:
> > From: "alloc.young" <alloc.young@outlook.com>
> >
> > Fix memory leak in hmp_info_vcpu_dirty_limit,use g_autoptr
> > to handle memory deallocation.
>
> It does not feel like -stable-worthy, or am I wrong and it should be
> picked up
> for -stable?


Since the leak occurs when using the hmp command instead of qmp, which
lowers the frequency, it is, in my opinion, somewhat tolerable for the
dirtylimit feature.
It is undoubtedly not -stable-worthy for the memory leak issue by itself,
which is regrettable. :( And the valgrind tool would be applied as much as
possible for my further work.

Thanks,

Yong


> Thanks,
>
> /mjt
>


-- 
Best regards

[-- Attachment #2: Type: text/html, Size: 2629 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2023-08-31  1:04 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-29 15:29 [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Hyman Huang
2023-08-29 15:29 ` [PULL 1/3] softmmu: Fix dirtylimit memory leak Hyman Huang
2023-08-30 16:55   ` Michael Tokarev
2023-08-31  1:01     ` Yong Huang
2023-08-29 15:29 ` [PULL 2/3] softmmu/dirtylimit: Convert free to g_free Hyman Huang
2023-08-29 15:29 ` [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot Hyman Huang
2023-08-30 16:22 ` [PULL 0/3] Dirty page rate and dirty page limit 20230829 patches Stefan Hajnoczi
  -- strict thread matches above, loose matches on Subject: below --
2023-08-28 17:19 Hyman Huang
2023-08-28 17:19 ` [PULL 3/3] migration/dirtyrate: Fix precision losses and g_usleep overshoot Hyman Huang
2023-08-28 17:32   ` Hyman Huang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).