From nobody Thu Nov 6 06:15:29 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542871544602126.9672078516844; Wed, 21 Nov 2018 23:25:44 -0800 (PST) Received: from localhost ([::1]:44402 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPjMe-0006BI-9L for importer@patchew.org; Thu, 22 Nov 2018 02:25:28 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51439) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPjIK-0002re-Vw for qemu-devel@nongnu.org; Thu, 22 Nov 2018 02:21:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gPjIJ-0002LE-IH for qemu-devel@nongnu.org; Thu, 22 Nov 2018 02:21:00 -0500 Received: from mail-pl1-x641.google.com ([2607:f8b0:4864:20::641]:37534) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gPjIJ-0002Jh-9M for qemu-devel@nongnu.org; Thu, 22 Nov 2018 02:20:59 -0500 Received: by mail-pl1-x641.google.com with SMTP id b5so8846076plr.4 for ; Wed, 21 Nov 2018 23:20:59 -0800 (PST) Received: from localhost.localdomain ([203.205.141.36]) by smtp.gmail.com with ESMTPSA id 19sm52731312pfs.108.2018.11.21.23.20.54 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 21 Nov 2018 23:20:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=wbPjx05ddO3emTOc85i0PBVC/qKf1knWe1gds+ynZDw=; b=VUqc7Zpn1rqjbGpPkPJnnm7vURgq2VoT1R1LLWyWBW+U8qn5/MRB8iRGSPjsYVWkht rw9Xxm8hgRDU40IlA/PAshVmenkXpx699Ogj4GI3u1iP8+np56fDtfCC4sRLUBNJRtXv Qhk70DKxrs5MLPT4GyL6NRwxkrRddoAR4WZ0J0kB7I+XoDXikydOA0X5cIRGsP2n7X4W Gy2cJH37L2G5reMWIzT5JdHwroylQHMx/nJfWHuuw20MKm3wzv+PGjmhG0CXKNDbFv3w f8XCU2q0PVTIDprKrxe5FHd+nnPGH2uU5C6TC0XCvzH8qhdAXVbqniwQ2Taj8HgZ0Ktf s0aw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=wbPjx05ddO3emTOc85i0PBVC/qKf1knWe1gds+ynZDw=; b=rk3ONkMpQiLyQNer0sseyayHigGwY3EHat730ZUTSvDgBimEi0Ro3BoOWIDSGd+DTp 082FtcNwzNDXecQSrJZSmOmnpDjkDzMzTpXiuTqiJkrc3oWSZDK1ozqIxiB4JPaP/ymG idY9BP1leLVcP6kZ7cIdNXfUGbAUgjiFrUsoEUHrJYX17l1B32ZyghP0LKjY/QEWVl2W Je8Fa2EJkcaJejaUuu9i1Nwz/SS80aBzPsigfGyQ4vnWKT6qAEIiK70nJhINxluJQhrj zCUJB3APJyaTZ9qO91NnvcXqL/5LqTqVcX4mCGQcLdcj52J+gRNp50V9vbUMsRNqEpGA 3miw== X-Gm-Message-State: AA+aEWZ9k/dgNtM6Dybi06KJ+4ySU5sF8Gxx7NP/OFl0rBjyPLAuwfFp QBXGfyLtEY6VdNg48bwDA1g= X-Google-Smtp-Source: AFSGD/XeJwyf+9wSMKTgdgd4HE2YPTpMeWIQZQ7zLysRNQhIJ283dKDS6Z6Xt+lv2BJ6KupbF9V7Tg== X-Received: by 2002:a17:902:8214:: with SMTP id x20-v6mr9719409pln.224.1542871258257; Wed, 21 Nov 2018 23:20:58 -0800 (PST) From: guangrong.xiao@gmail.com X-Google-Original-From: xiaoguangrong@tencent.com To: pbonzini@redhat.com, mst@redhat.com, mtosatti@redhat.com Date: Thu, 22 Nov 2018 15:20:28 +0800 Message-Id: <20181122072028.22819-6-xiaoguangrong@tencent.com> X-Mailer: git-send-email 2.14.5 In-Reply-To: <20181122072028.22819-1-xiaoguangrong@tencent.com> References: <20181122072028.22819-1-xiaoguangrong@tencent.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::641 Subject: [Qemu-devel] [PATCH v3 5/5] tests: add threaded-workqueue-bench X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kvm@vger.kernel.org, quintela@redhat.com, Xiao Guangrong , qemu-devel@nongnu.org, peterx@redhat.com, dgilbert@redhat.com, wei.w.wang@intel.com, cota@braap.org, jiang.biao2@zte.com.cn Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Xiao Guangrong It's the benhcmark of threaded-workqueue, also it's a good example to show how threaded-workqueue is used Signed-off-by: Xiao Guangrong --- tests/Makefile.include | 5 +- tests/threaded-workqueue-bench.c | 255 +++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 tests/threaded-workqueue-bench.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 613242bc6e..05ad27e75d 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -500,7 +500,8 @@ test-obj-y =3D tests/check-qnum.o tests/check-qstring.o= tests/check-qdict.o \ tests/test-rcu-tailq.o \ tests/test-qdist.o tests/test-shift128.o \ tests/test-qht.o tests/qht-bench.o tests/test-qht-par.o \ - tests/atomic_add-bench.o tests/atomic64-bench.o + tests/atomic_add-bench.o tests/atomic64-bench.o \ + tests/threaded-workqueue-bench.o =20 $(test-obj-y): QEMU_INCLUDES +=3D -Itests QEMU_CFLAGS +=3D -I$(SRC_PATH)/tests @@ -557,6 +558,8 @@ tests/qht-bench$(EXESUF): tests/qht-bench.o $(test-util= -obj-y) tests/test-bufferiszero$(EXESUF): tests/test-bufferiszero.o $(test-util-ob= j-y) tests/atomic_add-bench$(EXESUF): tests/atomic_add-bench.o $(test-util-obj-= y) tests/atomic64-bench$(EXESUF): tests/atomic64-bench.o $(test-util-obj-y) +tests/threaded-workqueue-bench$(EXESUF): tests/threaded-workqueue-bench.o = migration/qemu-file.o \ + $(test-util-obj-y) =20 tests/fp/%: $(MAKE) -C $(dir $@) $(notdir $@) diff --git a/tests/threaded-workqueue-bench.c b/tests/threaded-workqueue-be= nch.c new file mode 100644 index 0000000000..0d04948ed3 --- /dev/null +++ b/tests/threaded-workqueue-bench.c @@ -0,0 +1,255 @@ +/* + * Threaded Workqueue Benchmark + * + * Author: + * Xiao Guangrong + * + * Copyright(C) 2018 Tencent Corporation. + * + * This work is licensed under the terms of the GNU LGPL, version 2.1 or l= ater. + * See the COPYING.LIB file in the top-level directory. + */ +#include + +#include "qemu/osdep.h" +#include "exec/cpu-common.h" +#include "qemu/error-report.h" +#include "migration/qemu-file.h" +#include "qemu/threaded-workqueue.h" + +#define PAGE_SHIFT 12 +#define PAGE_SIZE (1 << PAGE_SHIFT) +#define DEFAULT_THREAD_NR 2 +#define DEFAULT_MEM_SIZE 1 +#define DEFAULT_REPEATED_COUNT 3 + +static ssize_t test_writev_buffer(void *opaque, struct iovec *iov, int iov= cnt, + int64_t pos) +{ + int i, size =3D 0; + + for (i =3D 0; i < iovcnt; i++) { + size +=3D iov[i].iov_len; + } + return size; +} + +static int test_fclose(void *opaque) +{ + return 0; +} + +static const QEMUFileOps test_write_ops =3D { + .writev_buffer =3D test_writev_buffer, + .close =3D test_fclose +}; + +static QEMUFile *dest_file; + +static const QEMUFileOps empty_ops =3D { }; + +struct CompressData { + uint8_t *ram_addr; + QEMUFile *file; + z_stream stream; +}; +typedef struct CompressData CompressData; + +static int compress_request_init(void *request) +{ + CompressData *cd =3D request; + + if (deflateInit(&cd->stream, 1) !=3D Z_OK) { + return -1; + } + cd->file =3D qemu_fopen_ops(NULL, &empty_ops); + return 0; +} + +static void compress_request_uninit(void *request) +{ + CompressData *cd =3D request; + + qemu_fclose(cd->file); + deflateEnd(&cd->stream); +} + +static void compress_thread_data_handler(void *request) +{ + CompressData *cd =3D request; + int blen; + + blen =3D qemu_put_compression_data(cd->file, &cd->stream, cd->ram_addr, + PAGE_SIZE); + if (blen < 0) { + error_report("compressed data failed!"); + qemu_file_set_error(dest_file, blen); + } +} + +struct CompressStats { + unsigned long pages; + unsigned long compressed_size; +}; +typedef struct CompressStats CompressStats; + +static CompressStats comp_stats; + +static void compress_thread_data_done(void *request) +{ + CompressData *cd =3D request; + int bytes_xmit; + + bytes_xmit =3D qemu_put_qemu_file(dest_file, cd->file); + + comp_stats.pages++; + comp_stats.compressed_size +=3D bytes_xmit; +} + +static const ThreadedWorkqueueOps ops =3D { + .thread_request_init =3D compress_request_init, + .thread_request_uninit =3D compress_request_uninit, + .thread_request_handler =3D compress_thread_data_handler, + .thread_request_done =3D compress_thread_data_done, + .request_size =3D sizeof(CompressData), +}; + +static void compress_threads_save_cleanup(Threads *threads) +{ + threaded_workqueue_destroy(threads); + qemu_fclose(dest_file); +} + +static Threads *compress_threads_save_setup(int threads_nr, int requests_n= r) +{ + Threads *compress_threads; + + dest_file =3D qemu_fopen_ops(NULL, &test_write_ops); + compress_threads =3D threaded_workqueue_create("compress", threads_nr, + requests_nr, &ops); + assert(compress_threads); + return compress_threads; +} + +static void compress_page_with_multi_thread(Threads *threads, uint8_t *add= r) +{ + CompressData *cd; + +retry: + cd =3D threaded_workqueue_get_request(threads); + if (!cd) { + goto retry; + } + + cd->ram_addr =3D addr; + threaded_workqueue_submit_request(threads, cd); +} + +static void run(Threads *threads, uint8_t *mem, unsigned long mem_size, + int repeated_count) +{ + uint8_t *ptr =3D mem, *end =3D mem + mem_size; + uint64_t start_ts, spend, total_ts =3D 0, pages =3D mem_size >> PAGE_S= HIFT; + double rate; + int i; + + for (i =3D 0; i < repeated_count; i++) { + ptr =3D mem; + memset(&comp_stats, 0, sizeof(comp_stats)); + + start_ts =3D g_get_monotonic_time(); + for (ptr =3D mem; ptr < end; ptr +=3D PAGE_SIZE) { + *ptr =3D 0x10; + compress_page_with_multi_thread(threads, ptr); + } + threaded_workqueue_wait_for_requests(threads); + spend =3D g_get_monotonic_time() - start_ts; + total_ts +=3D spend; + + if (comp_stats.pages !=3D pages) { + printf("ERROR: pages are compressed %ld, expect %ld.\n", + comp_stats.pages, pages); + exit(-1); + } + + rate =3D (double)(comp_stats.pages * PAGE_SIZE) / + comp_stats.compressed_size; + printf("RUN %d: Request # %ld Cost %ld, Compression Rate %f.\n", i, + comp_stats.pages, spend, rate); + } + + printf("AVG: Time Cost %ld\n", total_ts / repeated_count); + printf("AVG Throughput: %f GB/s\n", + (double)(mem_size >> 30) * repeated_count * 1e6 / total_ts); +} + +static void usage(const char *arg0) +{ + printf("\nThreaded Workqueue Benchmark.\n"); + printf("Usage:\n"); + printf(" %s [OPTIONS]\n", arg0); + printf("Options:\n"); + printf(" -t the number of threads (default %d).\n", + DEFAULT_THREAD_NR); + printf(" -r: the number of requests handled by each thread (de= fault %d).\n", + DEFAULT_THREAD_REQUEST_NR); + printf(" -m: the size of the memory (G) used to test (default = %dG).\n", + DEFAULT_MEM_SIZE); + printf(" -c: the repeated count (default %d).\n", + DEFAULT_REPEATED_COUNT); + printf(" -h show this help info.\n"); +} + +int main(int argc, char *argv[]) +{ + int c, threads_nr, requests_nr, repeated_count; + unsigned long mem_size; + uint8_t *mem; + Threads *threads; + + threads_nr =3D DEFAULT_THREAD_NR; + requests_nr =3D DEFAULT_THREAD_REQUEST_NR; + mem_size =3D DEFAULT_MEM_SIZE; + repeated_count =3D DEFAULT_REPEATED_COUNT; + + for (;;) { + c =3D getopt(argc, argv, "t:r:m:c:h"); + if (c < 0) { + break; + } + + switch (c) { + case 't': + threads_nr =3D atoi(optarg); + break; + case 'r': + requests_nr =3D atoi(optarg); + break; + case 'm': + mem_size =3D atol(optarg); + break; + case 'c': + repeated_count =3D atoi(optarg); + break; + default: + printf("Unkown option: %c.\n", c); + case 'h': + usage(argv[0]); + return -1; + } + } + + printf("Run the benchmark: threads %d requests-per-thread: %d memory %= ldG repeat %d.\n", + threads_nr, requests_nr, mem_size, repeated_count); + + mem_size =3D mem_size << 30; + mem =3D qemu_memalign(PAGE_SIZE, mem_size); + memset(mem, 0, mem_size); + + threads =3D compress_threads_save_setup(threads_nr, requests_nr); + run(threads, mem, mem_size, repeated_count); + compress_threads_save_cleanup(threads); + + qemu_vfree(mem); + return 0; +} --=20 2.14.5