From nobody Tue Feb 10 06:44:02 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1597400507; cv=none; d=zohomail.com; s=zohoarc; b=irq+TCeybPudd2tUAXGK6MMch0yQH1mYns+LQEq9jf22Nu9GL9HDECpdKokPBavJ+nWB/U8A3vY5JDpFrPImdN0qNj2QttQvcjC9pqWWkxNe3+PiiqUzyh+OMMlVZbrZVshZbaCXTubJz6uvp1LHVpvH+jUjBCCOgV8VMSu7Bkw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597400507; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=XLOwFckvvoG9sLmHn+TY/DG7ZqsqB+fXiSLQws8N+ts=; b=aTg7ZiSwJZtofrmpMsfTPvrfCZ1+vanhu63GZaDTLcBkgHPpXTtmq9Y8w1kKC+yWvi8mDIP2AWVNcC2fP2BblYmRuummPI3MlRXAEbUDxYh7Wz/OFYjR3RYPx4/UWyvrx3fd/Kh3KaTZ7T9z6RAKGEvcLJgQjMg0O7eJ/1XDkEs= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1597400507069566.5234360187189; Fri, 14 Aug 2020 03:21:47 -0700 (PDT) Received: from localhost ([::1]:35092 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k6WqH-0000Rv-Q6 for importer@patchew.org; Fri, 14 Aug 2020 06:21:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46166) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k6VuC-0002Ht-QP for qemu-devel@nongnu.org; Fri, 14 Aug 2020 05:21:44 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:50084 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k6VuA-00045v-2l for qemu-devel@nongnu.org; Fri, 14 Aug 2020 05:21:44 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 8658E4B0DE9F0F804950; Fri, 14 Aug 2020 17:21:33 +0800 (CST) Received: from huawei.com (10.175.101.6) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.487.0; Fri, 14 Aug 2020 17:21:27 +0800 From: Chuan Zheng To: , , Subject: [PATCH v2 05/10] migration/dirtyrate: Record hash results for each sampled page Date: Fri, 14 Aug 2020 17:32:08 +0800 Message-ID: <1597397532-68043-6-git-send-email-zhengchuan@huawei.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1597397532-68043-1-git-send-email-zhengchuan@huawei.com> References: <1597397532-68043-1-git-send-email-zhengchuan@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.101.6] X-CFilter-Loop: Reflected Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=45.249.212.35; envelope-from=zhengchuan@huawei.com; helo=huawei.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/14 05:21:34 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: zhang.zhanghailiang@huawei.com, linyilu@huawei.com, qemu-devel@nongnu.org, alex.chen@huawei.com, ann.zhuangyanying@huawei.com, fangying1@huawei.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Zheng Chuan Record hash results for each sampled page. Signed-off-by: Zheng Chuan Signed-off-by: YanYing Zhuang --- migration/dirtyrate.c | 135 ++++++++++++++++++++++++++++++++++++++++++++++= ++++ migration/dirtyrate.h | 7 +++ 2 files changed, 142 insertions(+) diff --git a/migration/dirtyrate.c b/migration/dirtyrate.c index c4304ef..11c0051 100644 --- a/migration/dirtyrate.c +++ b/migration/dirtyrate.c @@ -25,6 +25,7 @@ #include "dirtyrate.h" =20 CalculatingDirtyRateState CalculatingState =3D CAL_DIRTY_RATE_INIT; +static unsigned long int qcrypto_hash_len =3D QCRYPTO_HASH_LEN; static struct DirtyRateStat dirty_stat; =20 static int dirty_rate_set_state(int new_state) @@ -71,6 +72,140 @@ static void update_dirtyrate(uint64_t msec) dirty_stat.dirty_rate =3D dirty_rate; } =20 +/* + * get hash result for the sampled memory with length of 4K byte in ramblo= ck, + * which starts from ramblock base address. + */ +static int get_ramblock_vfn_hash(struct RamblockDirtyInfo *info, unsigned = long vfn, + uint8_t **md) +{ + struct iovec iov_array; + int ret =3D 0; + int nkey =3D 1; + + iov_array.iov_base =3D info->ramblock_addr + + vfn * DIRTYRATE_SAMPLE_PAGE_SIZE; + iov_array.iov_len =3D DIRTYRATE_SAMPLE_PAGE_SIZE; + + if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_MD5, + &iov_array, nkey, + md, &qcrypto_hash_len, NULL) < 0) { + ret =3D -1; + } + + return ret; +} + +static int save_ramblock_hash(struct RamblockDirtyInfo *info) +{ + unsigned int sample_pages_count; + uint8_t *md =3D NULL; + int i; + int ret =3D -1; + GRand* rand =3D g_rand_new(); + + sample_pages_count =3D info->sample_pages_count; + + /* ramblock size less than one page, return success to skip this rambl= ock */ + if (unlikely(info->ramblock_pages =3D=3D 0 || sample_pages_count =3D= =3D 0)) { + ret =3D 0; + goto out; + } + + info->hash_result =3D g_try_malloc0_n(sample_pages_count, sizeof(uint8= _t) * qcrypto_hash_len); + if (!info->hash_result) { + ret =3D -1; + goto out; + } + + info->sample_page_vfn =3D g_try_malloc0_n(sample_pages_count, sizeof(u= nsigned long)); + if (!info->sample_page_vfn) { + g_free(info->hash_result); + ret =3D -1; + goto out; + } + + for (i =3D 0; i < sample_pages_count; i++) { + md =3D info->hash_result + i * qcrypto_hash_len; + info->sample_page_vfn[i] =3D g_rand_int_range(rand, 0, info->rambl= ock_pages - 1); + ret =3D get_ramblock_vfn_hash(info, info->sample_page_vfn[i], &md); + if (ret < 0) { + goto out; + } + } + ret =3D 0; + +out: + g_rand_free(rand); + return ret; +} + +static void get_ramblock_dirty_info(RAMBlock *block, struct RamblockDirtyI= nfo *info, + struct DirtyRateConfig *config) +{ + uint64_t sample_pages_per_gigabytes =3D config->sample_pages_per_gigab= ytes; + + /* Right shift 30 bits to calc block size in GB */ + info->sample_pages_count =3D (qemu_ram_get_used_length(block) * sample= _pages_per_gigabytes) >> 30; + + /* Right shift 12 bits to calc page count in 4KB */ + info->ramblock_pages =3D qemu_ram_get_used_length(block) >> 12; + info->ramblock_addr =3D qemu_ram_get_host_addr(block); + strcpy(info->idstr, qemu_ram_get_idstr(block)); +} + +static struct RamblockDirtyInfo * +alloc_ramblock_dirty_info(int *block_index, + struct RamblockDirtyInfo *block_dinfo) +{ + struct RamblockDirtyInfo *info =3D NULL; + int index =3D *block_index; + + if (!block_dinfo) { + block_dinfo =3D g_try_new(struct RamblockDirtyInfo, 1); + index =3D 0; + } else { + index++; + block_dinfo =3D g_try_realloc(block_dinfo, (index + 1) * + sizeof(struct RamblockDirtyInfo)); + } + if (!block_dinfo) + return NULL; + + info =3D &block_dinfo[index]; + memset(info, 0, sizeof(struct RamblockDirtyInfo)); + + *block_index =3D index; + return block_dinfo; +} + +static int record_ramblock_hash_info(struct DirtyRateConfig config, + struct RamblockDirtyInfo **block_dinf= o, int *block_index) +{ + struct RamblockDirtyInfo *info =3D NULL; + struct RamblockDirtyInfo *dinfo =3D NULL; + RAMBlock *block =3D NULL; + int index =3D 0; + + RAMBLOCK_FOREACH_MIGRATABLE(block) { + dinfo =3D alloc_ramblock_dirty_info(&index, dinfo); + if (dinfo =3D=3D NULL) + return -1; + info =3D &dinfo[index]; + get_ramblock_dirty_info(block, info, &config); + if (save_ramblock_hash(info) < 0) { + *block_dinfo =3D dinfo; + *block_index =3D index; + return -1; + } + } + + *block_dinfo =3D dinfo; + *block_index =3D index; + + return 0; +} + static void calculate_dirtyrate(struct DirtyRateConfig config) { /* todo */ diff --git a/migration/dirtyrate.h b/migration/dirtyrate.h index af57c80..0812b16 100644 --- a/migration/dirtyrate.h +++ b/migration/dirtyrate.h @@ -20,10 +20,17 @@ #define DIRTYRATE_DEFAULT_SAMPLE_PAGES 256 =20 /* + * Sample page size 4K as default. + */ +#define DIRTYRATE_SAMPLE_PAGE_SIZE 4096 + +/* * Record ramblock idstr */ #define RAMBLOCK_INFO_MAX_LEN 256 =20 +#define QCRYPTO_HASH_LEN 16 + /* Take 1s as default for calculation duration */ #define DEFAULT_FETCH_DIRTYRATE_TIME_SEC 1 =20 --=20 1.8.3.1