|
|
9ae3a8 |
From 55c524d38cafb5856220c76b9573efa6e22dd1f1 Mon Sep 17 00:00:00 2001
|
|
|
9ae3a8 |
From: Laszlo Ersek <lersek@redhat.com>
|
|
|
9ae3a8 |
Date: Fri, 7 Nov 2014 17:18:01 +0100
|
|
|
9ae3a8 |
Subject: [PATCH 14/41] dump: add API to write dump pages
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Message-id: <1415380693-16593-15-git-send-email-lersek@redhat.com>
|
|
|
9ae3a8 |
Patchwork-id: 62200
|
|
|
9ae3a8 |
O-Subject: [RHEL-7.1 qemu-kvm PATCH 14/26] dump: add API to write dump pages
|
|
|
9ae3a8 |
Bugzilla: 1157798
|
|
|
9ae3a8 |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
|
|
|
9ae3a8 |
RH-Acked-by: dgibson <dgibson@redhat.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
From: qiaonuohan <qiaonuohan@cn.fujitsu.com>
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
functions are used to write page to vmcore. vmcore is written page by page.
|
|
|
9ae3a8 |
page desc is used to store the information of a page, including a page's size,
|
|
|
9ae3a8 |
offset, compression format, etc.
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
Signed-off-by: Qiao Nuohan <qiaonuohan@cn.fujitsu.com>
|
|
|
9ae3a8 |
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
|
|
|
9ae3a8 |
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
|
|
|
9ae3a8 |
(cherry picked from commit d12f57ec6640d36e380367a0ab6ab9f3f29b6d51)
|
|
|
9ae3a8 |
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
|
|
|
9ae3a8 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
9ae3a8 |
---
|
|
|
9ae3a8 |
dump.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
9ae3a8 |
include/sysemu/dump.h | 7 ++
|
|
|
9ae3a8 |
2 files changed, 238 insertions(+)
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
diff --git a/dump.c b/dump.c
|
|
|
9ae3a8 |
index 926ab84..fc5530f 100644
|
|
|
9ae3a8 |
--- a/dump.c
|
|
|
9ae3a8 |
+++ b/dump.c
|
|
|
9ae3a8 |
@@ -25,6 +25,14 @@
|
|
|
9ae3a8 |
#include "qapi/error.h"
|
|
|
9ae3a8 |
#include "qmp-commands.h"
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+#include <zlib.h>
|
|
|
9ae3a8 |
+#ifdef CONFIG_LZO
|
|
|
9ae3a8 |
+#include <lzo/lzo1x.h>
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+#ifdef CONFIG_SNAPPY
|
|
|
9ae3a8 |
+#include <snappy-c.h>
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
static uint16_t cpu_convert_to_target16(uint16_t val, int endian)
|
|
|
9ae3a8 |
{
|
|
|
9ae3a8 |
if (endian == ELFDATA2LSB) {
|
|
|
9ae3a8 |
@@ -1218,6 +1226,229 @@ static void free_data_cache(DataCache *data_cache)
|
|
|
9ae3a8 |
g_free(data_cache->buf);
|
|
|
9ae3a8 |
}
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+static size_t get_len_buf_out(size_t page_size, uint32_t flag_compress)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ size_t len_buf_out_zlib, len_buf_out_lzo, len_buf_out_snappy;
|
|
|
9ae3a8 |
+ size_t len_buf_out;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* init buf_out */
|
|
|
9ae3a8 |
+ len_buf_out_zlib = len_buf_out_lzo = len_buf_out_snappy = 0;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* buf size for zlib */
|
|
|
9ae3a8 |
+ len_buf_out_zlib = compressBound(page_size);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* buf size for lzo */
|
|
|
9ae3a8 |
+#ifdef CONFIG_LZO
|
|
|
9ae3a8 |
+ if (flag_compress & DUMP_DH_COMPRESSED_LZO) {
|
|
|
9ae3a8 |
+ if (lzo_init() != LZO_E_OK) {
|
|
|
9ae3a8 |
+ /* return 0 to indicate lzo is unavailable */
|
|
|
9ae3a8 |
+ return 0;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /*
|
|
|
9ae3a8 |
+ * LZO will expand incompressible data by a little amount. please check the
|
|
|
9ae3a8 |
+ * following URL to see the expansion calculation:
|
|
|
9ae3a8 |
+ * http://www.oberhumer.com/opensource/lzo/lzofaq.php
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+ len_buf_out_lzo = page_size + page_size / 16 + 64 + 3;
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+#ifdef CONFIG_SNAPPY
|
|
|
9ae3a8 |
+ /* buf size for snappy */
|
|
|
9ae3a8 |
+ len_buf_out_snappy = snappy_max_compressed_length(page_size);
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* get the biggest that can store all kinds of compressed page */
|
|
|
9ae3a8 |
+ len_buf_out = MAX(len_buf_out_zlib,
|
|
|
9ae3a8 |
+ MAX(len_buf_out_lzo, len_buf_out_snappy));
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ return len_buf_out;
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+/*
|
|
|
9ae3a8 |
+ * check if the page is all 0
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+static inline bool is_zero_page(const uint8_t *buf, size_t page_size)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ return buffer_is_zero(buf, page_size);
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+static int write_dump_pages(DumpState *s)
|
|
|
9ae3a8 |
+{
|
|
|
9ae3a8 |
+ int ret = 0;
|
|
|
9ae3a8 |
+ DataCache page_desc, page_data;
|
|
|
9ae3a8 |
+ size_t len_buf_out, size_out;
|
|
|
9ae3a8 |
+#ifdef CONFIG_LZO
|
|
|
9ae3a8 |
+ lzo_bytep wrkmem = NULL;
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+ uint8_t *buf_out = NULL;
|
|
|
9ae3a8 |
+ off_t offset_desc, offset_data;
|
|
|
9ae3a8 |
+ PageDescriptor pd, pd_zero;
|
|
|
9ae3a8 |
+ uint8_t *buf;
|
|
|
9ae3a8 |
+ int endian = s->dump_info.d_endian;
|
|
|
9ae3a8 |
+ GuestPhysBlock *block_iter = NULL;
|
|
|
9ae3a8 |
+ uint64_t pfn_iter;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* get offset of page_desc and page_data in dump file */
|
|
|
9ae3a8 |
+ offset_desc = s->offset_page;
|
|
|
9ae3a8 |
+ offset_data = offset_desc + sizeof(PageDescriptor) * s->num_dumpable;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ prepare_data_cache(&page_desc, s, offset_desc);
|
|
|
9ae3a8 |
+ prepare_data_cache(&page_data, s, offset_data);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* prepare buffer to store compressed data */
|
|
|
9ae3a8 |
+ len_buf_out = get_len_buf_out(s->page_size, s->flag_compress);
|
|
|
9ae3a8 |
+ if (len_buf_out == 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to get length of output buffer.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+#ifdef CONFIG_LZO
|
|
|
9ae3a8 |
+ wrkmem = g_malloc(LZO1X_1_MEM_COMPRESS);
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ buf_out = g_malloc(len_buf_out);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /*
|
|
|
9ae3a8 |
+ * init zero page's page_desc and page_data, because every zero page
|
|
|
9ae3a8 |
+ * uses the same page_data
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+ pd_zero.size = cpu_convert_to_target32(s->page_size, endian);
|
|
|
9ae3a8 |
+ pd_zero.flags = cpu_convert_to_target32(0, endian);
|
|
|
9ae3a8 |
+ pd_zero.offset = cpu_convert_to_target64(offset_data, endian);
|
|
|
9ae3a8 |
+ pd_zero.page_flags = cpu_convert_to_target64(0, endian);
|
|
|
9ae3a8 |
+ buf = g_malloc0(s->page_size);
|
|
|
9ae3a8 |
+ ret = write_cache(&page_data, buf, s->page_size, false);
|
|
|
9ae3a8 |
+ g_free(buf);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to write page data(zero page).\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ offset_data += s->page_size;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /*
|
|
|
9ae3a8 |
+ * dump memory to vmcore page by page. zero page will all be resided in the
|
|
|
9ae3a8 |
+ * first page of page section
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+ while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
|
|
|
9ae3a8 |
+ /* check zero page */
|
|
|
9ae3a8 |
+ if (is_zero_page(buf, s->page_size)) {
|
|
|
9ae3a8 |
+ ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
|
|
|
9ae3a8 |
+ false);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to write page desc.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ } else {
|
|
|
9ae3a8 |
+ /*
|
|
|
9ae3a8 |
+ * not zero page, then:
|
|
|
9ae3a8 |
+ * 1. compress the page
|
|
|
9ae3a8 |
+ * 2. write the compressed page into the cache of page_data
|
|
|
9ae3a8 |
+ * 3. get page desc of the compressed page and write it into the
|
|
|
9ae3a8 |
+ * cache of page_desc
|
|
|
9ae3a8 |
+ *
|
|
|
9ae3a8 |
+ * only one compression format will be used here, for
|
|
|
9ae3a8 |
+ * s->flag_compress is set. But when compression fails to work,
|
|
|
9ae3a8 |
+ * we fall back to save in plaintext.
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+ size_out = len_buf_out;
|
|
|
9ae3a8 |
+ if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
|
|
|
9ae3a8 |
+ (compress2(buf_out, (uLongf *)&size_out, buf, s->page_size,
|
|
|
9ae3a8 |
+ Z_BEST_SPEED) == Z_OK) && (size_out < s->page_size)) {
|
|
|
9ae3a8 |
+ pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_ZLIB,
|
|
|
9ae3a8 |
+ endian);
|
|
|
9ae3a8 |
+ pd.size = cpu_convert_to_target32(size_out, endian);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = write_cache(&page_data, buf_out, size_out, false);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to write page data.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+#ifdef CONFIG_LZO
|
|
|
9ae3a8 |
+ } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
|
|
|
9ae3a8 |
+ (lzo1x_1_compress(buf, s->page_size, buf_out,
|
|
|
9ae3a8 |
+ (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
|
|
|
9ae3a8 |
+ (size_out < s->page_size)) {
|
|
|
9ae3a8 |
+ pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_LZO,
|
|
|
9ae3a8 |
+ endian);
|
|
|
9ae3a8 |
+ pd.size = cpu_convert_to_target32(size_out, endian);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = write_cache(&page_data, buf_out, size_out, false);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to write page data.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+#ifdef CONFIG_SNAPPY
|
|
|
9ae3a8 |
+ } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
|
|
|
9ae3a8 |
+ (snappy_compress((char *)buf, s->page_size,
|
|
|
9ae3a8 |
+ (char *)buf_out, &size_out) == SNAPPY_OK) &&
|
|
|
9ae3a8 |
+ (size_out < s->page_size)) {
|
|
|
9ae3a8 |
+ pd.flags = cpu_convert_to_target32(
|
|
|
9ae3a8 |
+ DUMP_DH_COMPRESSED_SNAPPY, endian);
|
|
|
9ae3a8 |
+ pd.size = cpu_convert_to_target32(size_out, endian);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = write_cache(&page_data, buf_out, size_out, false);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to write page data.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+ } else {
|
|
|
9ae3a8 |
+ /*
|
|
|
9ae3a8 |
+ * fall back to save in plaintext, size_out should be
|
|
|
9ae3a8 |
+ * assigned to s->page_size
|
|
|
9ae3a8 |
+ */
|
|
|
9ae3a8 |
+ pd.flags = cpu_convert_to_target32(0, endian);
|
|
|
9ae3a8 |
+ size_out = s->page_size;
|
|
|
9ae3a8 |
+ pd.size = cpu_convert_to_target32(size_out, endian);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = write_cache(&page_data, buf, s->page_size, false);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to write page data.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ /* get and write page desc here */
|
|
|
9ae3a8 |
+ pd.page_flags = cpu_convert_to_target64(0, endian);
|
|
|
9ae3a8 |
+ pd.offset = cpu_convert_to_target64(offset_data, endian);
|
|
|
9ae3a8 |
+ offset_data += size_out;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = write_cache(&page_desc, &pd, sizeof(PageDescriptor), false);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to write page desc.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ ret = write_cache(&page_desc, NULL, 0, true);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to sync cache for page_desc.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+ ret = write_cache(&page_data, NULL, 0, true);
|
|
|
9ae3a8 |
+ if (ret < 0) {
|
|
|
9ae3a8 |
+ dump_error(s, "dump: failed to sync cache for page_data.\n");
|
|
|
9ae3a8 |
+ goto out;
|
|
|
9ae3a8 |
+ }
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+out:
|
|
|
9ae3a8 |
+ free_data_cache(&page_desc);
|
|
|
9ae3a8 |
+ free_data_cache(&page_data);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+#ifdef CONFIG_LZO
|
|
|
9ae3a8 |
+ g_free(wrkmem);
|
|
|
9ae3a8 |
+#endif
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ g_free(buf_out);
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
+ return ret;
|
|
|
9ae3a8 |
+}
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
static ram_addr_t get_start_block(DumpState *s)
|
|
|
9ae3a8 |
{
|
|
|
9ae3a8 |
GuestPhysBlock *block;
|
|
|
9ae3a8 |
diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h
|
|
|
9ae3a8 |
index 92a95e4..efab7a3 100644
|
|
|
9ae3a8 |
--- a/include/sysemu/dump.h
|
|
|
9ae3a8 |
+++ b/include/sysemu/dump.h
|
|
|
9ae3a8 |
@@ -151,6 +151,13 @@ typedef struct DataCache {
|
|
|
9ae3a8 |
off_t offset; /* offset of the file */
|
|
|
9ae3a8 |
} DataCache;
|
|
|
9ae3a8 |
|
|
|
9ae3a8 |
+typedef struct QEMU_PACKED PageDescriptor {
|
|
|
9ae3a8 |
+ uint64_t offset; /* the offset of the page data*/
|
|
|
9ae3a8 |
+ uint32_t size; /* the size of this dump page */
|
|
|
9ae3a8 |
+ uint32_t flags; /* flags */
|
|
|
9ae3a8 |
+ uint64_t page_flags; /* page flags */
|
|
|
9ae3a8 |
+} PageDescriptor;
|
|
|
9ae3a8 |
+
|
|
|
9ae3a8 |
struct GuestPhysBlockList; /* memory_mapping.h */
|
|
|
9ae3a8 |
int cpu_get_dump_info(ArchDumpInfo *info,
|
|
|
9ae3a8 |
const struct GuestPhysBlockList *guest_phys_blocks);
|
|
|
9ae3a8 |
--
|
|
|
9ae3a8 |
1.8.3.1
|
|
|
9ae3a8 |
|