[CRIU] [PATCH] [RFC] zdtm: make datagen/datachk faster for big chunks

Andrey Vagin avagin at openvz.org
Tue Mar 5 15:21:07 EST 2013


Generate random data only for buffers with sizes less than FAST_SIZE
If a size of buffer is more that FAST_SIZE, the first FAST_SIZE bytes
are filled by random generator and then this chunk is used as pattern
for all other chunks.

With out this patch:
$ time bash -x test/zdtm.sh static/maps04
real	0m16.777s
user	0m0.054s
sys	0m0.724s

With this patch:
$ time bash -x test/zdtm.sh static/maps04
real	0m1.865s
user	0m0.128s
sys	0m0.745s

Signed-off-by: Andrey Vagin <avagin at openvz.org>
---
 test/zdtm/lib/datagen.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/test/zdtm/lib/datagen.c b/test/zdtm/lib/datagen.c
index fc4be3d..550339c 100644
--- a/test/zdtm/lib/datagen.c
+++ b/test/zdtm/lib/datagen.c
@@ -1,7 +1,60 @@
 #include <stdlib.h>
+#include <string.h>
 
 #include "zdtmtst.h"
 
+/*
+ * Generate random data only for buffers with sizes less than FAST_SIZE
+ * If a size of buffer is more than FAST_SIZE, the first FAST_SIZE bytes
+ * are filled by random generator and then this chunk is used as pattern
+ * for all other chunks.
+ */
+
+#define FAST_SIZE 99971 /* Prime number */
+
+static void datagen_fast(uint8_t *buffer, unsigned length, uint32_t *crc)
+{
+	uint8_t *p;
+
+	datagen(buffer, FAST_SIZE, crc);
+	p = buffer + FAST_SIZE;
+
+	while (p < buffer + length) {
+		unsigned long size = FAST_SIZE;
+
+		if (p + FAST_SIZE > buffer + length)
+			size = buffer + length - p;
+		memcpy(p, buffer, size);
+
+		p += FAST_SIZE;
+	}
+}
+
+static int datachk_fast(const uint8_t *buffer, unsigned length, uint32_t *crc)
+{
+	const uint8_t *p;
+
+	if (datachk(buffer, FAST_SIZE, crc))
+		return 1;
+
+	p = buffer + FAST_SIZE;
+
+	while (p < buffer + length) {
+		unsigned long size = FAST_SIZE;
+
+		if (p + FAST_SIZE > buffer + length)
+			size = buffer + length - p;
+
+		if (memcmp(p, buffer, size)) {
+			test_msg("Memory corruption [%p, %p]\n", p, p + size);
+			return 1;
+		}
+		p += FAST_SIZE;
+	}
+
+	return 0;
+}
+
 /* update CRC-32 */
 #define CRCPOLY 0xedb88320
 static inline uint32_t crc32_le8(uint32_t crc, uint8_t datum)
@@ -18,6 +71,9 @@ void datagen(uint8_t *buffer, unsigned length, uint32_t *crc)
 	uint32_t rnd = 0;
 	unsigned shift;
 
+	if (length > FAST_SIZE)
+		return datagen_fast(buffer, length, crc);
+
 	for (shift = 0; length-- > 4; buffer++, shift--, rnd >>= 8) {
 		if (!shift) {
 			shift = 4;
@@ -58,6 +114,9 @@ int datachk(const uint8_t *buffer, unsigned length, uint32_t *crc)
 {
 	uint32_t read_crc;
 
+	if (length > FAST_SIZE)
+		return datachk_fast(buffer, length, crc);
+
 	for (; length-- > 4; buffer++)
 		*crc = crc32_le8(*crc, *buffer);
 
-- 
1.7.11.7



More information about the CRIU mailing list