2013-01-31 08:12:16 +01:00
|
|
|
/*
|
|
|
|
* Xor Based Zero Run Length Encoding unit tests.
|
|
|
|
*
|
|
|
|
* Copyright 2013 Red Hat, Inc. and/or its affiliates
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Orit Wasserman <owasserm@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2016-03-20 18:16:19 +01:00
|
|
|
#include "qemu/cutils.h"
|
2017-04-05 21:47:50 +02:00
|
|
|
#include "../migration/xbzrle.h"
|
2013-01-31 08:12:16 +01:00
|
|
|
|
2021-01-18 07:38:05 +01:00
|
|
|
#define XBZRLE_PAGE_SIZE 4096
|
2013-01-31 08:12:16 +01:00
|
|
|
|
2022-11-16 16:29:23 +01:00
|
|
|
int (*xbzrle_encode_buffer_func)(uint8_t *, uint8_t *, int,
|
|
|
|
uint8_t *, int) = xbzrle_encode_buffer;
|
|
|
|
#if defined(CONFIG_AVX512BW_OPT)
|
|
|
|
#include "qemu/cpuid.h"
|
|
|
|
static void __attribute__((constructor)) init_cpu_flag(void)
|
|
|
|
{
|
|
|
|
unsigned max = __get_cpuid_max(0, NULL);
|
|
|
|
int a, b, c, d;
|
|
|
|
if (max >= 1) {
|
|
|
|
__cpuid(1, a, b, c, d);
|
|
|
|
/* We must check that AVX is not just available, but usable. */
|
|
|
|
if ((c & bit_OSXSAVE) && (c & bit_AVX) && max >= 7) {
|
|
|
|
int bv;
|
|
|
|
__asm("xgetbv" : "=a"(bv), "=d"(d) : "c"(0));
|
|
|
|
__cpuid_count(7, 0, a, b, c, d);
|
|
|
|
/* 0xe6:
|
|
|
|
* XCR0[7:5] = 111b (OPMASK state, upper 256-bit of ZMM0-ZMM15
|
|
|
|
* and ZMM16-ZMM31 state are enabled by OS)
|
|
|
|
* XCR0[2:1] = 11b (XMM state and YMM state are enabled by OS)
|
|
|
|
*/
|
|
|
|
if ((bv & 0xe6) == 0xe6 && (b & bit_AVX512BW)) {
|
|
|
|
xbzrle_encode_buffer_func = xbzrle_encode_buffer_avx512;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-31 08:12:16 +01:00
|
|
|
static void test_uleb(void)
|
|
|
|
{
|
|
|
|
uint32_t i, val;
|
|
|
|
uint8_t buf[2];
|
|
|
|
int encode_ret, decode_ret;
|
|
|
|
|
|
|
|
for (i = 0; i <= 0x3fff; i++) {
|
|
|
|
encode_ret = uleb128_encode_small(&buf[0], i);
|
|
|
|
decode_ret = uleb128_decode_small(&buf[0], &val);
|
|
|
|
g_assert(encode_ret == decode_ret);
|
|
|
|
g_assert(i == val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* decode invalid value */
|
|
|
|
buf[0] = 0x80;
|
|
|
|
buf[1] = 0x80;
|
|
|
|
|
|
|
|
decode_ret = uleb128_decode_small(&buf[0], &val);
|
|
|
|
g_assert(decode_ret == -1);
|
|
|
|
g_assert(val == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_zero(void)
|
|
|
|
{
|
2021-01-18 07:38:05 +01:00
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *compressed = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
int i = 0;
|
|
|
|
int dlen = 0;
|
2021-01-18 07:38:05 +01:00
|
|
|
int diff_len = g_test_rand_int_range(0, XBZRLE_PAGE_SIZE - 1006);
|
2013-01-31 08:12:16 +01:00
|
|
|
|
|
|
|
for (i = diff_len; i > 0; i--) {
|
|
|
|
buffer[1000 + i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[1000 + diff_len + 3] = 103;
|
|
|
|
buffer[1000 + diff_len + 5] = 105;
|
|
|
|
|
|
|
|
/* encode zero page */
|
2022-11-16 16:29:23 +01:00
|
|
|
dlen = xbzrle_encode_buffer_func(buffer, buffer, XBZRLE_PAGE_SIZE, compressed,
|
2021-01-18 07:38:05 +01:00
|
|
|
XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
g_assert(dlen == 0);
|
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_unchanged(void)
|
|
|
|
{
|
2021-01-18 07:38:05 +01:00
|
|
|
uint8_t *compressed = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
int i = 0;
|
|
|
|
int dlen = 0;
|
2021-01-18 07:38:05 +01:00
|
|
|
int diff_len = g_test_rand_int_range(0, XBZRLE_PAGE_SIZE - 1006);
|
2013-01-31 08:12:16 +01:00
|
|
|
|
|
|
|
for (i = diff_len; i > 0; i--) {
|
|
|
|
test[1000 + i] = i + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
test[1000 + diff_len + 3] = 107;
|
|
|
|
test[1000 + diff_len + 5] = 109;
|
|
|
|
|
|
|
|
/* test unchanged buffer */
|
2022-11-16 16:29:23 +01:00
|
|
|
dlen = xbzrle_encode_buffer_func(test, test, XBZRLE_PAGE_SIZE, compressed,
|
2021-01-18 07:38:05 +01:00
|
|
|
XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
g_assert(dlen == 0);
|
|
|
|
|
|
|
|
g_free(test);
|
|
|
|
g_free(compressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_1_byte(void)
|
|
|
|
{
|
2021-01-18 07:38:05 +01:00
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *compressed = g_malloc(XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
int dlen = 0, rc = 0;
|
|
|
|
uint8_t buf[2];
|
|
|
|
|
2021-01-18 07:38:05 +01:00
|
|
|
test[XBZRLE_PAGE_SIZE - 1] = 1;
|
2013-01-31 08:12:16 +01:00
|
|
|
|
2022-11-16 16:29:23 +01:00
|
|
|
dlen = xbzrle_encode_buffer_func(buffer, test, XBZRLE_PAGE_SIZE, compressed,
|
2021-01-18 07:38:05 +01:00
|
|
|
XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
g_assert(dlen == (uleb128_encode_small(&buf[0], 4095) + 2));
|
|
|
|
|
2021-01-18 07:38:05 +01:00
|
|
|
rc = xbzrle_decode_buffer(compressed, dlen, buffer, XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(rc == XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(memcmp(test, buffer, XBZRLE_PAGE_SIZE) == 0);
|
2013-01-31 08:12:16 +01:00
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
g_free(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode_overflow(void)
|
|
|
|
{
|
2021-01-18 07:38:05 +01:00
|
|
|
uint8_t *compressed = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
int i = 0, rc = 0;
|
|
|
|
|
2021-01-18 07:38:05 +01:00
|
|
|
for (i = 0; i < XBZRLE_PAGE_SIZE / 2 - 1; i++) {
|
2013-01-31 08:12:16 +01:00
|
|
|
test[i * 2] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* encode overflow */
|
2022-11-16 16:29:23 +01:00
|
|
|
rc = xbzrle_encode_buffer_func(buffer, test, XBZRLE_PAGE_SIZE, compressed,
|
2021-01-18 07:38:05 +01:00
|
|
|
XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
g_assert(rc == -1);
|
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
g_free(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void encode_decode_range(void)
|
|
|
|
{
|
2021-01-18 07:38:05 +01:00
|
|
|
uint8_t *buffer = g_malloc0(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *compressed = g_malloc(XBZRLE_PAGE_SIZE);
|
|
|
|
uint8_t *test = g_malloc0(XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
int i = 0, rc = 0;
|
|
|
|
int dlen = 0;
|
|
|
|
|
2021-01-18 07:38:05 +01:00
|
|
|
int diff_len = g_test_rand_int_range(0, XBZRLE_PAGE_SIZE - 1006);
|
2013-01-31 08:12:16 +01:00
|
|
|
|
|
|
|
for (i = diff_len; i > 0; i--) {
|
|
|
|
buffer[1000 + i] = i;
|
|
|
|
test[1000 + i] = i + 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[1000 + diff_len + 3] = 103;
|
|
|
|
test[1000 + diff_len + 3] = 107;
|
|
|
|
|
|
|
|
buffer[1000 + diff_len + 5] = 105;
|
|
|
|
test[1000 + diff_len + 5] = 109;
|
|
|
|
|
|
|
|
/* test encode/decode */
|
2022-11-16 16:29:23 +01:00
|
|
|
dlen = xbzrle_encode_buffer_func(test, buffer, XBZRLE_PAGE_SIZE, compressed,
|
2021-01-18 07:38:05 +01:00
|
|
|
XBZRLE_PAGE_SIZE);
|
2013-01-31 08:12:16 +01:00
|
|
|
|
2021-01-18 07:38:05 +01:00
|
|
|
rc = xbzrle_decode_buffer(compressed, dlen, test, XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(rc < XBZRLE_PAGE_SIZE);
|
|
|
|
g_assert(memcmp(test, buffer, XBZRLE_PAGE_SIZE) == 0);
|
2013-01-31 08:12:16 +01:00
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
g_free(compressed);
|
|
|
|
g_free(test);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_encode_decode(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 10000; i++) {
|
|
|
|
encode_decode_range();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
g_test_init(&argc, &argv, NULL);
|
|
|
|
g_test_rand_int();
|
|
|
|
g_test_add_func("/xbzrle/uleb", test_uleb);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_zero", test_encode_decode_zero);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_unchanged",
|
|
|
|
test_encode_decode_unchanged);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_1_byte", test_encode_decode_1_byte);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode_overflow",
|
|
|
|
test_encode_decode_overflow);
|
|
|
|
g_test_add_func("/xbzrle/encode_decode", test_encode_decode);
|
|
|
|
|
|
|
|
return g_test_run();
|
|
|
|
}
|