From patchwork Fri Apr 29 04:23:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 8977091 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 0F136BF29F for ; Fri, 29 Apr 2016 04:32:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BD43F2021B for ; Fri, 29 Apr 2016 04:32:45 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6790720166 for ; Fri, 29 Apr 2016 04:32:44 +0000 (UTC) Received: from localhost ([::1]:52276 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aw06d-000194-O8 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Apr 2016 00:32:43 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37802) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy5-0007GG-Hc for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1avzy3-0000tV-T0 for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57906) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1avzy3-0000tJ-KO for qemu-devel@nongnu.org; Fri, 29 Apr 2016 00:23:51 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 53F53C0D7F1E for ; Fri, 29 Apr 2016 04:23:51 +0000 (UTC) Received: from red.redhat.com (ovpn-113-21.phx2.redhat.com [10.3.113.21]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u3T4Ngf7016324; Fri, 29 Apr 2016 00:23:50 -0400 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 28 Apr 2016 22:23:31 -0600 Message-Id: <1461903820-3092-11-git-send-email-eblake@redhat.com> In-Reply-To: <1461903820-3092-1-git-send-email-eblake@redhat.com> References: <1461903820-3092-1-git-send-email-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v3 10/18] vmstate: Use new JSON output visitor X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Amit Shah , famz@redhat.com, armbru@redhat.com, Juan Quintela Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Rather than using a QJSON object and converting the QString result to a char *, we can use the new JSON output visitor and get directly to a char *. The conversions are a bit tricky in place (in places, we have to copy an integer to an int64_t temporary to get the right pointer for visit_type_int(); and for several strings, we have to copy to a temporary variable so we can take an address (&char[] is not the same as &char*) and cast away const), but overall still fairly mechanical. Suggested-by: Paolo Bonzini Signed-off-by: Eric Blake --- v3: retitle, rebase to master v2: new patch --- include/migration/vmstate.h | 4 +-- migration/savevm.c | 68 ++++++++++++++++++++++++++++----------------- migration/vmstate.c | 64 ++++++++++++++++++++++++++---------------- tests/Makefile | 2 +- 4 files changed, 86 insertions(+), 52 deletions(-) diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h index 84ee355..2cdfce9 100644 --- a/include/migration/vmstate.h +++ b/include/migration/vmstate.h @@ -29,7 +29,7 @@ #ifndef CONFIG_USER_ONLY #include #endif -#include +#include "qapi/json-output-visitor.h" typedef void SaveStateHandler(QEMUFile *f, void *opaque); typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id); @@ -925,7 +925,7 @@ void loadvm_free_handlers(MigrationIncomingState *mis); int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd, void *opaque, int version_id); void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, - void *opaque, QJSON *vmdesc); + void *opaque, Visitor *vmdesc); bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque); diff --git a/migration/savevm.c b/migration/savevm.c index 16ba443..c858fe9 100644 --- a/migration/savevm.c +++ b/migration/savevm.c @@ -2,7 +2,7 @@ * QEMU System Emulator * * Copyright (c) 2003-2008 Fabrice Bellard - * Copyright (c) 2009-2015 Red Hat Inc + * Copyright (c) 2009-2016 Red Hat Inc * * Authors: * Juan Quintela @@ -645,27 +645,32 @@ static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) return vmstate_load_state(f, se->vmsd, se->opaque, version_id); } -static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) +static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, + Visitor *vmdesc) { int64_t old_offset, size; + const char *tmp; old_offset = qemu_ftell_fast(f); se->ops->save_state(f, se->opaque); size = qemu_ftell_fast(f) - old_offset; if (vmdesc) { - json_prop_int(vmdesc, "size", size); - json_start_array(vmdesc, "fields"); - json_start_object(vmdesc, NULL); - json_prop_str(vmdesc, "name", "data"); - json_prop_int(vmdesc, "size", size); - json_prop_str(vmdesc, "type", "buffer"); - json_end_object(vmdesc); - json_end_array(vmdesc); + visit_type_int(vmdesc, "size", &size, &error_abort); + visit_start_list(vmdesc, "fields", NULL, 0, &error_abort); + visit_start_struct(vmdesc, NULL, NULL, 0, &error_abort); + tmp = "data"; + visit_type_str(vmdesc, "name", (char **)&tmp, &error_abort); + visit_type_int(vmdesc, "size", &size, &error_abort); + tmp = "buffer"; + visit_type_str(vmdesc, "type", (char **)&tmp, &error_abort); + visit_check_struct(vmdesc, &error_abort); + visit_end_struct(vmdesc); + visit_end_list(vmdesc); } } -static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) +static void vmstate_save(QEMUFile *f, SaveStateEntry *se, Visitor *vmdesc) { trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); if (!se->vmsd) { @@ -891,7 +896,7 @@ void qemu_savevm_state_header(QEMUFile *f) if (!savevm_state.skip_configuration || enforce_config_section()) { qemu_put_byte(f, QEMU_VM_CONFIGURATION); - vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0); + vmstate_save_state(f, &vmstate_configuration, &savevm_state, NULL); } } @@ -1033,11 +1038,15 @@ void qemu_savevm_state_complete_postcopy(QEMUFile *f) void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only) { - QJSON *vmdesc; + JsonOutputVisitor *vmdesc_jov; + Visitor *vmdesc; + char *vmdesc_str; int vmdesc_len; SaveStateEntry *se; int ret; bool in_postcopy = migration_in_postcopy(migrate_get_current()); + int64_t tmp_i; + char *tmp_s; trace_savevm_state_complete_precopy(); @@ -1073,9 +1082,12 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only) return; } - vmdesc = qjson_new(); - json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE); - json_start_array(vmdesc, "devices"); + vmdesc_jov = json_output_visitor_new(); + vmdesc = json_output_get_visitor(vmdesc_jov); + visit_start_struct(vmdesc, NULL, NULL, 0, &error_abort); + tmp_i = TARGET_PAGE_SIZE; + visit_type_int(vmdesc, "page_size", &tmp_i, &error_abort); + visit_start_list(vmdesc, "devices", NULL, 0, &error_abort); QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { if ((!se->ops || !se->ops->save_state) && !se->vmsd) { @@ -1088,16 +1100,19 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only) trace_savevm_section_start(se->idstr, se->section_id); - json_start_object(vmdesc, NULL); - json_prop_str(vmdesc, "name", se->idstr); - json_prop_int(vmdesc, "instance_id", se->instance_id); + visit_start_struct(vmdesc, NULL, NULL, 0, &error_abort); + tmp_s = se->idstr; + visit_type_str(vmdesc, "name", &tmp_s, &error_abort); + tmp_i = se->instance_id; + visit_type_int(vmdesc, "instance_id", &tmp_i, &error_abort); save_section_header(f, se, QEMU_VM_SECTION_FULL); vmstate_save(f, se, vmdesc); trace_savevm_section_end(se->idstr, se->section_id, 0); save_section_footer(f, se); - json_end_object(vmdesc); + visit_check_struct(vmdesc, &error_abort); + visit_end_struct(vmdesc); } if (!in_postcopy) { @@ -1105,16 +1120,19 @@ void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only) qemu_put_byte(f, QEMU_VM_EOF); } - json_end_array(vmdesc); - qjson_finish(vmdesc); - vmdesc_len = strlen(qjson_get_str(vmdesc)); + visit_end_list(vmdesc); + visit_check_struct(vmdesc, &error_abort); + visit_end_struct(vmdesc); + vmdesc_str = json_output_get_string(vmdesc_jov); + vmdesc_len = strlen(vmdesc_str); if (should_send_vmdesc()) { qemu_put_byte(f, QEMU_VM_VMDESCRIPTION); qemu_put_be32(f, vmdesc_len); - qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len); + qemu_put_buffer(f, (uint8_t *)vmdesc_str, vmdesc_len); } - object_unref(OBJECT(vmdesc)); + g_free(vmdesc_str); + json_output_visitor_cleanup(vmdesc_jov); qemu_fflush(f); } diff --git a/migration/vmstate.c b/migration/vmstate.c index bf3d5db..e7f894c 100644 --- a/migration/vmstate.c +++ b/migration/vmstate.c @@ -1,15 +1,15 @@ #include "qemu/osdep.h" #include "qemu-common.h" +#include "qapi/error.h" #include "migration/migration.h" #include "migration/qemu-file.h" #include "migration/vmstate.h" #include "qemu/bitops.h" #include "qemu/error-report.h" #include "trace.h" -#include "qjson.h" static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, - void *opaque, QJSON *vmdesc); + void *opaque, Visitor *vmdesc); static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, void *opaque); @@ -226,12 +226,15 @@ static bool vmsd_can_compress(VMStateField *field) return true; } -static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc, +static void vmsd_desc_field_start(const VMStateDescription *vmsd, + Visitor *vmdesc, VMStateField *field, int i, int max) { char *name, *old_name; bool is_array = max > 1; bool can_compress = vmsd_can_compress(field); + int64_t tmp; + const char *type; if (!vmdesc) { return; @@ -247,38 +250,47 @@ static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc, g_free(old_name); } - json_start_object(vmdesc, NULL); - json_prop_str(vmdesc, "name", name); + visit_start_struct(vmdesc, NULL, NULL, 0, &error_abort); + visit_type_str(vmdesc, "name", &name, &error_abort); if (is_array) { if (can_compress) { - json_prop_int(vmdesc, "array_len", max); + tmp = max; + visit_type_int(vmdesc, "array_len", &tmp, &error_abort); } else { - json_prop_int(vmdesc, "index", i); + tmp = i; + visit_type_int(vmdesc, "index", &tmp, &error_abort); } } - json_prop_str(vmdesc, "type", vmfield_get_type_name(field)); + type = vmfield_get_type_name(field); + visit_type_str(vmdesc, "type", (char **)&type, &error_abort); if (field->flags & VMS_STRUCT) { - json_start_object(vmdesc, "struct"); + visit_start_struct(vmdesc, "struct", NULL, 0, &error_abort); } g_free(name); } -static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc, +static void vmsd_desc_field_end(const VMStateDescription *vmsd, + Visitor *vmdesc, VMStateField *field, size_t size, int i) { + int64_t tmp; + if (!vmdesc) { return; } if (field->flags & VMS_STRUCT) { /* We printed a struct in between, close its child object */ - json_end_object(vmdesc); + visit_check_struct(vmdesc, &error_abort); + visit_end_struct(vmdesc); } - json_prop_int(vmdesc, "size", size); - json_end_object(vmdesc); + tmp = size; + visit_type_int(vmdesc, "size", &tmp, &error_abort); + visit_check_struct(vmdesc, &error_abort); + visit_end_struct(vmdesc); } @@ -293,7 +305,7 @@ bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque) void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, - void *opaque, QJSON *vmdesc) + void *opaque, Visitor *vmdesc) { VMStateField *field = vmsd->fields; @@ -302,9 +314,11 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, } if (vmdesc) { - json_prop_str(vmdesc, "vmsd_name", vmsd->name); - json_prop_int(vmdesc, "version", vmsd->version_id); - json_start_array(vmdesc, "fields"); + const char *tmp_s = vmsd->name; + int64_t tmp_i = vmsd->version_id; + visit_type_str(vmdesc, "vmsd_name", (char **)&tmp_s, &error_abort); + visit_type_int(vmdesc, "version", &tmp_i, &error_abort); + visit_start_list(vmdesc, "fields", NULL, 0, &error_abort); } while (field->name) { @@ -314,7 +328,7 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, int i, n_elems = vmstate_n_elems(opaque, field); int size = vmstate_size(opaque, field); int64_t old_offset, written_bytes; - QJSON *vmdesc_loop = vmdesc; + Visitor *vmdesc_loop = vmdesc; for (i = 0; i < n_elems; i++) { void *addr = base_addr + size * i; @@ -350,7 +364,7 @@ void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd, } if (vmdesc) { - json_end_array(vmdesc); + visit_end_list(vmdesc); } vmstate_subsection_save(f, vmsd, opaque, vmdesc); @@ -420,7 +434,7 @@ static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd, } static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, - void *opaque, QJSON *vmdesc) + void *opaque, Visitor *vmdesc) { const VMStateDescription **sub = vmsd->subsections; bool subsection_found = false; @@ -433,11 +447,12 @@ static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, if (vmdesc) { /* Only create subsection array when we have any */ if (!subsection_found) { - json_start_array(vmdesc, "subsections"); + visit_start_list(vmdesc, "subsections", NULL, 0, + &error_abort); subsection_found = true; } - json_start_object(vmdesc, NULL); + visit_start_struct(vmdesc, NULL, NULL, 0, &error_abort); } qemu_put_byte(f, QEMU_VM_SUBSECTION); @@ -448,14 +463,15 @@ static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd, vmstate_save_state(f, vmsd, opaque, vmdesc); if (vmdesc) { - json_end_object(vmdesc); + visit_check_struct(vmdesc, &error_abort); + visit_end_struct(vmdesc); } } sub++; } if (vmdesc && subsection_found) { - json_end_array(vmdesc); + visit_end_list(vmdesc); } } diff --git a/tests/Makefile b/tests/Makefile index 0b5a7a8..1f8a39d 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -442,7 +442,7 @@ tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \ $(test-qapi-obj-y) tests/test-vmstate$(EXESUF): tests/test-vmstate.o \ migration/vmstate.o migration/qemu-file.o migration/qemu-file-buf.o \ - migration/qemu-file-unix.o qjson.o \ + migration/qemu-file-unix.o qapi/json-output-visitor.o \ $(test-qom-obj-y) tests/test-timed-average$(EXESUF): tests/test-timed-average.o qemu-timer.o \ $(test-util-obj-y)