From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47064) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YplF3-00083k-UH for qemu-devel@nongnu.org; Tue, 05 May 2015 18:23:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YplF1-0000QY-38 for qemu-devel@nongnu.org; Tue, 05 May 2015 18:23:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33703) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YplF0-0000QT-Ro for qemu-devel@nongnu.org; Tue, 05 May 2015 18:23:03 -0400 From: John Snow Date: Tue, 5 May 2015 18:22:57 -0400 Message-Id: <1430864578-22072-5-git-send-email-jsnow@redhat.com> In-Reply-To: <1430864578-22072-1-git-send-email-jsnow@redhat.com> References: <1430864578-22072-1-git-send-email-jsnow@redhat.com> Subject: [Qemu-devel] [PATCH v3 4/5] qtest: precompute hex nibs List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, John Snow , afaerber@suse.de Instead of letting printf and friends do this for us one byte at a time, fill a buffer ourselves and then send the entire buffer in one go. This gives a moderate speed improvement over the old method. Signed-off-by: John Snow --- qtest.c | 20 ++++++++++++++++---- tests/libqtest.c | 17 ++++++++++++++--- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/qtest.c b/qtest.c index c1a0493..383f9ed 100644 --- a/qtest.c +++ b/qtest.c @@ -157,6 +157,8 @@ static bool qtest_opened; * NUM=0 even though it is remapped to GSI 2). */ +static const char *hex = "0123456789abcdef"; + static int hex2nib(char ch) { if (ch >= '0' && ch <= '9') { @@ -170,6 +172,12 @@ static int hex2nib(char ch) } } +static inline void byte2hex(uint8_t byte, uint8_t *out) +{ + *out++ = hex[byte >> 4]; + *out = hex[byte & 0x0F]; +} + static void qtest_get_time(qemu_timeval *tv) { qemu_gettimeofday(tv); @@ -414,6 +422,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words) } else if (strcmp(words[0], "read") == 0) { uint64_t addr, len, i; uint8_t *data; + uint8_t *enc; g_assert(words[1] && words[2]); addr = strtoull(words[1], NULL, 0); @@ -422,14 +431,17 @@ static void qtest_process_command(CharDriverState *chr, gchar **words) data = g_malloc(len); cpu_physical_memory_read(addr, data, len); - qtest_send_prefix(chr); - qtest_send(chr, "OK 0x"); + enc = g_malloc(2 * len + 1); for (i = 0; i < len; i++) { - qtest_sendf(chr, "%02x", data[i]); + byte2hex(data[i], &enc[i * 2]); } - qtest_send(chr, "\n"); + enc[2 * len] = '\0'; + + qtest_send_prefix(chr); + qtest_sendf(chr, "OK 0x%s\n", enc); g_free(data); + g_free(enc); } else if (strcmp(words[0], "b64read") == 0) { uint64_t addr, len; uint8_t *data; diff --git a/tests/libqtest.c b/tests/libqtest.c index 055aad6..1b246c9 100644 --- a/tests/libqtest.c +++ b/tests/libqtest.c @@ -651,6 +651,8 @@ uint64_t qtest_readq(QTestState *s, uint64_t addr) return qtest_read(s, "readq", addr); } +static const char *hex = "0123456789abcdef"; + static int hex2nib(char ch) { if (ch >= '0' && ch <= '9') { @@ -664,6 +666,12 @@ static int hex2nib(char ch) } } +static inline void byte2hex(uint8_t byte, uint8_t *out) +{ + *out++ = hex[byte >> 4]; + *out = hex[byte & 0x0f]; +} + void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size) { uint8_t *ptr = data; @@ -730,13 +738,16 @@ void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size) { const uint8_t *ptr = data; size_t i; + uint8_t *enc = g_malloc(2 * size + 1); - qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size); for (i = 0; i < size; i++) { - qtest_sendf(s, "%02x", ptr[i]); + byte2hex(ptr[i], &enc[i * 2]); } - qtest_sendf(s, "\n"); + enc[2 * size] = '\0'; + + qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc); qtest_rsp(s, 0); + g_free(enc); } void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size) -- 2.1.0