diff mbox

[v5,10/11] Dump: add qmp command "query-dump"

Message ID 1449467796-15856-11-git-send-email-peterx@redhat.com
State New
Headers show

Commit Message

Peter Xu Dec. 7, 2015, 5:56 a.m. UTC
When dump-guest-memory is requested with detach flag, after its
return, user could query its status using "query-dump" command (with
no argument). The result contains:

- status: current dump status
- completed: bytes written in the latest dump
- total: bytes to write in the latest dump

From completed and total, we could know how much work
finished by calculating:

  100.0 * completed / total (%)

Also, enrich DUMP_COMPLETED event to contain dump results when
finished.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 docs/qmp-events.txt |  5 ++++-
 dump.c              | 28 ++++++++++++++++++++++++----
 qapi-schema.json    | 34 ++++++++++++++++++++++++++++++++--
 qapi/event.json     |  4 +++-
 qmp-commands.hx     | 29 +++++++++++++++++++++++++++--
 5 files changed, 90 insertions(+), 10 deletions(-)

Comments

Fam Zheng Dec. 7, 2015, 6:21 a.m. UTC | #1
On Mon, 12/07 13:56, Peter Xu wrote:
> When dump-guest-memory is requested with detach flag, after its
> return, user could query its status using "query-dump" command (with
> no argument). The result contains:
> 
> - status: current dump status
> - completed: bytes written in the latest dump
> - total: bytes to write in the latest dump
> 
> From completed and total, we could know how much work
> finished by calculating:
> 
>   100.0 * completed / total (%)
> 
> Also, enrich DUMP_COMPLETED event to contain dump results when
> finished.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  docs/qmp-events.txt |  5 ++++-
>  dump.c              | 28 ++++++++++++++++++++++++----
>  qapi-schema.json    | 34 ++++++++++++++++++++++++++++++++--
>  qapi/event.json     |  4 +++-
>  qmp-commands.hx     | 29 +++++++++++++++++++++++++++--
>  5 files changed, 90 insertions(+), 10 deletions(-)
> 
> diff --git a/docs/qmp-events.txt b/docs/qmp-events.txt
> index 1f79588..60af2ab 100644
> --- a/docs/qmp-events.txt
> +++ b/docs/qmp-events.txt
> @@ -227,6 +227,8 @@ Emitted when the guest has finished one memory dump.
>  
>  Data:
>  
> +- "result": DumpQueryResult type as described in qapi-schema.json
> +
>  - "error": Error message when dump failed. This is only a
>    human-readable string provided when dump failed. It should not be
>    parsed in any way (json-string, optional)
> @@ -234,7 +236,8 @@ Data:
>  Example:
>  
>  { "event": "DUMP_COMPLETED",
> -  "data": {} }
> +  "data": {"result": {"total": 1090650112, "status": "completed",
> +                      "completed": 1090650112} } }
>  
>  GUEST_PANICKED
>  --------------
> diff --git a/dump.c b/dump.c
> index ff543ae..c566a32 100644
> --- a/dump.c
> +++ b/dump.c
> @@ -1433,7 +1433,7 @@ static void dump_state_prepare(DumpState *s)
>  bool dump_in_progress(void)
>  {
>      DumpState *state = &dump_state_global;
> -    return (state->status == DUMP_STATUS_ACTIVE);
> +    return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
>  }
>  
>  /* calculate total size of memory to be dumped (taking filter into
> @@ -1634,6 +1634,7 @@ cleanup:
>  static void dump_process(DumpState *s, Error **errp)
>  {
>      Error *local_err = NULL;
> +    DumpQueryResult *result = NULL;
>  
>      if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
>          create_kdump_vmcore(s, &local_err);
> @@ -1641,12 +1642,19 @@ static void dump_process(DumpState *s, Error **errp)
>          create_vmcore(s, &local_err);
>      }
>  
> -    s->status = (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED);
> +    /* make sure status is written after written_size updates */
> +    smp_wmb();
> +    atomic_set(&s->status,
> +               (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
>  
>      /* send DUMP_COMPLETED message (unconditionally) */
> -    qapi_event_send_dump_completed(!!local_err, (local_err ? \
> +    result = qmp_query_dump(NULL);
> +    /* should never fails */

s/fails/fail/

> +    assert(result);
> +    qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
>                                     error_get_pretty(local_err) : NULL),
>                                     &error_abort);
> +    qapi_free_DumpQueryResult(result);
>  
>      error_propagate(errp, local_err);
>      dump_cleanup(s);
> @@ -1661,6 +1669,18 @@ static void *dump_thread(void *data)
>      return NULL;
>  }
>  
> +DumpQueryResult *qmp_query_dump(Error **errp)
> +{
> +    DumpQueryResult *result = g_new(DumpQueryResult, 1);
> +    DumpState *state = &dump_state_global;
> +    result->status = atomic_read(&state->status);
> +    /* make sure we are reading status and written_size in order */
> +    smp_rmb();
> +    result->completed = state->written_size;
> +    result->total = state->total_size;
> +    return result;
> +}
> +
>  void qmp_dump_guest_memory(bool paging, const char *file,
>                             bool has_detach, bool detach,
>                             bool has_begin, int64_t begin, bool has_length,
> @@ -1751,7 +1771,7 @@ void qmp_dump_guest_memory(bool paging, const char *file,
>                begin, length, &local_err);
>      if (local_err) {
>          error_propagate(errp, local_err);
> -        s->status = DUMP_STATUS_FAILED;
> +        atomic_set(&s->status, DUMP_STATUS_FAILED);
>          return;
>      }
>  
> diff --git a/qapi-schema.json b/qapi-schema.json
> index f0d3c4a..71f6523 100644
> --- a/qapi-schema.json
> +++ b/qapi-schema.json
> @@ -2116,8 +2116,9 @@
>  #               is the fd's name.
>  #
>  # @detach: #optional if true, QMP will return immediately rather than
> -#          waiting for the dump to finish. A DUMP_COMPLETED event will
> -#          occur at the end. (since 2.6).
> +#          waiting for the dump to finish. The user can track progress
> +#          using "query-dump". A DUMP_COMPLETED event will occur
> +#          at the end. (since 2.6).
>  #
>  # @begin: #optional if specified, the starting physical address.
>  #
> @@ -2158,6 +2159,35 @@
>    'data': [ 'none', 'active', 'completed', 'failed' ] }
>  
>  ##
> +# @DumpQueryResult
> +#
> +# The result format for 'query-dump'.
> +#
> +# @status: enum of @DumpStatus, which shows current dump status
> +#
> +# @completed: bytes written in latest dump (uncompressed)
> +#
> +# @total: total bytes to be written in latest dump (uncompressed)
> +#
> +# Since 2.6
> +##
> +{ 'struct': 'DumpQueryResult',
> +  'data': { 'status': 'DumpStatus',
> +            'completed': 'int',
> +            'total': 'int' } }
> +
> +##
> +# @query-dump
> +#
> +# Query latest dump status.
> +#
> +# Returns: A @DumpStatus object showing the dump status.
> +#
> +# Since: 2.6
> +##
> +{ 'command': 'query-dump', 'returns': 'DumpQueryResult' }
> +
> +##
>  # @DumpGuestMemoryCapability:
>  #
>  # A list of the available formats for dump-guest-memory
> diff --git a/qapi/event.json b/qapi/event.json
> index b18ed66..bf7dd61 100644
> --- a/qapi/event.json
> +++ b/qapi/event.json
> @@ -362,6 +362,8 @@
>  #
>  # Emitted when background dump has completed
>  #
> +# @result: DumpQueryResult type described in qapi-schema.json.
> +#
>  # @error: #optional human-readable error string that provides
>  #         hint on why dump failed. Only presents on failure. The
>  #         user should not try to interpret the error string.
> @@ -369,4 +371,4 @@
>  # Since: 2.6
>  ##
>  { 'event': 'DUMP_COMPLETED' ,
> -  'data': { '*error': 'str' } }
> +  'data': { 'result': 'DumpQueryResult', '*error': 'str' } }
> diff --git a/qmp-commands.hx b/qmp-commands.hx
> index 7b6f915..eb5bfe2 100644
> --- a/qmp-commands.hx
> +++ b/qmp-commands.hx
> @@ -858,8 +858,9 @@ Arguments:
>  - "protocol": destination file(started with "file:") or destination file
>                descriptor (started with "fd:") (json-string)
>  - "detach": if specified, command will return immediately rather than waiting
> -            for the dump completion. A DUMP_COMPLETED event will occur at
> -            the end. (json-bool)
> +            for the dump completion. The user can track progress using
> +            "query-dump" A DUMP_COMPLETED event will occur at the

Missing "." before "A DUMP_COMPLETED"?

> +            end. (json-bool)
Peter Xu Dec. 7, 2015, 7:03 a.m. UTC | #2
On Mon, Dec 07, 2015 at 02:21:44PM +0800, Fam Zheng wrote:
> On Mon, 12/07 13:56, Peter Xu wrote:
> >      /* send DUMP_COMPLETED message (unconditionally) */
> > -    qapi_event_send_dump_completed(!!local_err, (local_err ? \
> > +    result = qmp_query_dump(NULL);
> > +    /* should never fails */
> 
> s/fails/fail/

Got.

> 
> > diff --git a/qmp-commands.hx b/qmp-commands.hx
> > index 7b6f915..eb5bfe2 100644
> > --- a/qmp-commands.hx
> > +++ b/qmp-commands.hx
> > @@ -858,8 +858,9 @@ Arguments:
> >  - "protocol": destination file(started with "file:") or destination file
> >                descriptor (started with "fd:") (json-string)
> >  - "detach": if specified, command will return immediately rather than waiting
> > -            for the dump completion. A DUMP_COMPLETED event will occur at
> > -            the end. (json-bool)
> > +            for the dump completion. The user can track progress using
> > +            "query-dump" A DUMP_COMPLETED event will occur at the
> 
> Missing "." before "A DUMP_COMPLETED"?

Possibly... Changing both. Thanks!

Peter

> 
> > +            end. (json-bool)
diff mbox

Patch

diff --git a/docs/qmp-events.txt b/docs/qmp-events.txt
index 1f79588..60af2ab 100644
--- a/docs/qmp-events.txt
+++ b/docs/qmp-events.txt
@@ -227,6 +227,8 @@  Emitted when the guest has finished one memory dump.
 
 Data:
 
+- "result": DumpQueryResult type as described in qapi-schema.json
+
 - "error": Error message when dump failed. This is only a
   human-readable string provided when dump failed. It should not be
   parsed in any way (json-string, optional)
@@ -234,7 +236,8 @@  Data:
 Example:
 
 { "event": "DUMP_COMPLETED",
-  "data": {} }
+  "data": {"result": {"total": 1090650112, "status": "completed",
+                      "completed": 1090650112} } }
 
 GUEST_PANICKED
 --------------
diff --git a/dump.c b/dump.c
index ff543ae..c566a32 100644
--- a/dump.c
+++ b/dump.c
@@ -1433,7 +1433,7 @@  static void dump_state_prepare(DumpState *s)
 bool dump_in_progress(void)
 {
     DumpState *state = &dump_state_global;
-    return (state->status == DUMP_STATUS_ACTIVE);
+    return (atomic_read(&state->status) == DUMP_STATUS_ACTIVE);
 }
 
 /* calculate total size of memory to be dumped (taking filter into
@@ -1634,6 +1634,7 @@  cleanup:
 static void dump_process(DumpState *s, Error **errp)
 {
     Error *local_err = NULL;
+    DumpQueryResult *result = NULL;
 
     if (s->has_format && s->format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
         create_kdump_vmcore(s, &local_err);
@@ -1641,12 +1642,19 @@  static void dump_process(DumpState *s, Error **errp)
         create_vmcore(s, &local_err);
     }
 
-    s->status = (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED);
+    /* make sure status is written after written_size updates */
+    smp_wmb();
+    atomic_set(&s->status,
+               (local_err ? DUMP_STATUS_FAILED : DUMP_STATUS_COMPLETED));
 
     /* send DUMP_COMPLETED message (unconditionally) */
-    qapi_event_send_dump_completed(!!local_err, (local_err ? \
+    result = qmp_query_dump(NULL);
+    /* should never fails */
+    assert(result);
+    qapi_event_send_dump_completed(result, !!local_err, (local_err ? \
                                    error_get_pretty(local_err) : NULL),
                                    &error_abort);
+    qapi_free_DumpQueryResult(result);
 
     error_propagate(errp, local_err);
     dump_cleanup(s);
@@ -1661,6 +1669,18 @@  static void *dump_thread(void *data)
     return NULL;
 }
 
+DumpQueryResult *qmp_query_dump(Error **errp)
+{
+    DumpQueryResult *result = g_new(DumpQueryResult, 1);
+    DumpState *state = &dump_state_global;
+    result->status = atomic_read(&state->status);
+    /* make sure we are reading status and written_size in order */
+    smp_rmb();
+    result->completed = state->written_size;
+    result->total = state->total_size;
+    return result;
+}
+
 void qmp_dump_guest_memory(bool paging, const char *file,
                            bool has_detach, bool detach,
                            bool has_begin, int64_t begin, bool has_length,
@@ -1751,7 +1771,7 @@  void qmp_dump_guest_memory(bool paging, const char *file,
               begin, length, &local_err);
     if (local_err) {
         error_propagate(errp, local_err);
-        s->status = DUMP_STATUS_FAILED;
+        atomic_set(&s->status, DUMP_STATUS_FAILED);
         return;
     }
 
diff --git a/qapi-schema.json b/qapi-schema.json
index f0d3c4a..71f6523 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -2116,8 +2116,9 @@ 
 #               is the fd's name.
 #
 # @detach: #optional if true, QMP will return immediately rather than
-#          waiting for the dump to finish. A DUMP_COMPLETED event will
-#          occur at the end. (since 2.6).
+#          waiting for the dump to finish. The user can track progress
+#          using "query-dump". A DUMP_COMPLETED event will occur
+#          at the end. (since 2.6).
 #
 # @begin: #optional if specified, the starting physical address.
 #
@@ -2158,6 +2159,35 @@ 
   'data': [ 'none', 'active', 'completed', 'failed' ] }
 
 ##
+# @DumpQueryResult
+#
+# The result format for 'query-dump'.
+#
+# @status: enum of @DumpStatus, which shows current dump status
+#
+# @completed: bytes written in latest dump (uncompressed)
+#
+# @total: total bytes to be written in latest dump (uncompressed)
+#
+# Since 2.6
+##
+{ 'struct': 'DumpQueryResult',
+  'data': { 'status': 'DumpStatus',
+            'completed': 'int',
+            'total': 'int' } }
+
+##
+# @query-dump
+#
+# Query latest dump status.
+#
+# Returns: A @DumpStatus object showing the dump status.
+#
+# Since: 2.6
+##
+{ 'command': 'query-dump', 'returns': 'DumpQueryResult' }
+
+##
 # @DumpGuestMemoryCapability:
 #
 # A list of the available formats for dump-guest-memory
diff --git a/qapi/event.json b/qapi/event.json
index b18ed66..bf7dd61 100644
--- a/qapi/event.json
+++ b/qapi/event.json
@@ -362,6 +362,8 @@ 
 #
 # Emitted when background dump has completed
 #
+# @result: DumpQueryResult type described in qapi-schema.json.
+#
 # @error: #optional human-readable error string that provides
 #         hint on why dump failed. Only presents on failure. The
 #         user should not try to interpret the error string.
@@ -369,4 +371,4 @@ 
 # Since: 2.6
 ##
 { 'event': 'DUMP_COMPLETED' ,
-  'data': { '*error': 'str' } }
+  'data': { 'result': 'DumpQueryResult', '*error': 'str' } }
diff --git a/qmp-commands.hx b/qmp-commands.hx
index 7b6f915..eb5bfe2 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -858,8 +858,9 @@  Arguments:
 - "protocol": destination file(started with "file:") or destination file
               descriptor (started with "fd:") (json-string)
 - "detach": if specified, command will return immediately rather than waiting
-            for the dump completion. A DUMP_COMPLETED event will occur at
-            the end. (json-bool)
+            for the dump completion. The user can track progress using
+            "query-dump" A DUMP_COMPLETED event will occur at the
+            end. (json-bool)
 - "begin": the starting physical address. It's optional, and should be specified
            with length together (json-int)
 - "length": the memory size, in bytes. It's optional, and should be specified
@@ -899,6 +900,30 @@  Example:
 
 EQMP
 
+    {
+        .name       = "query-dump",
+        .args_type  = "",
+        .params     = "",
+        .help       = "query background dump status",
+        .mhandler.cmd_new = qmp_marshal_query_dump,
+    },
+
+SQMP
+query-dump
+----------
+
+Query background dump status.
+
+Arguments: None.
+
+Example:
+
+-> { "execute": "query-dump" }
+<- { "return": { "status": "active", "completed": 1024000,
+                 "total": 2048000 } }
+
+EQMP
+
 #if defined TARGET_S390X
     {
         .name       = "dump-skeys",