From nobody Sun Nov 24 04:51:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 172647745156618.298144884412864; Mon, 16 Sep 2024 02:04:11 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 81D6A1B7B; Mon, 16 Sep 2024 05:04:10 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 8144F1B41; Mon, 16 Sep 2024 05:03:18 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 206A61123; Mon, 16 Sep 2024 05:03:14 -0400 (EDT) Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 00166A18 for ; Mon, 16 Sep 2024 05:03:12 -0400 (EDT) Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-42cb57f8b41so39987885e9.0 for ; Mon, 16 Sep 2024 02:03:12 -0700 (PDT) Received: from draig.lan ([85.9.250.243]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378e78054absm6651151f8f.106.2024.09.16.02.03.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Sep 2024 02:03:11 -0700 (PDT) Received: from draig.lan (localhost [IPv6:::1]) by draig.lan (Postfix) with ESMTP id BC3035F87D; Mon, 16 Sep 2024 09:54:01 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-1.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1726477392; x=1727082192; darn=lists.libvirt.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aeKWF4400mkgzW6RNuwJ6RAXELhPq+3Dr2Xaak76GOQ=; b=qvMhr9601ESCozyYw3+U3CNwPAZXkgc2D3WwJ47QQzXLFABWGvMPZ2Tau+QcmLtzOI A9Tv14eoOGGot9hRoeLl7YZaaTaKlEYS4TWB8KXVyPGy/mKaWpuDLMOLjx8QtEcjtqhx 3fLqeN7fkrmz8BWCXMYM5rZgGBmMzvqJZl6tdvDWsZpb7nuqFdrVtgJKcOCVowBgxKMx flQiwe6q2UkYpOjTd0KqG3CnGN60E+4slR/oslB8QAVQQhf93FN5QneaQ1wxTklu+Jpc GpcZsHz0aFutidQEVeQDMqiK1iH75QtRO4eTiH2ilAew4eXkSUZE3FjhZMkBp0FyArKa A4jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726477392; x=1727082192; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aeKWF4400mkgzW6RNuwJ6RAXELhPq+3Dr2Xaak76GOQ=; b=SHEVzQN2ffYIj0X1BfUtIgtu+V5O49bf/jKIMvK93fNZ2Z9Xl7lqwi7lxWFyvQLvZC mk3Zz00YSlFS6RrIh2XCLhL+TLuBNQDCL2u3Y9pw+25b096BpxGJ9Uc/3O7tALQSYLc0 f+nsmsYtfwkuOEuej7HwF2qMEKWN/jsYVCfv8iDQVgxemj6MVJBl15UMPkxX7Qo9W3bC isMqADnhlk998mgSTibYOxlCEYenTBj3tS9Sy3629wtNrzCSL2Qd1FR1Zp/a5Cq6gb10 458i9HmK3E7PC42+tFGYZwtWK5UCzk0k8VqL9sf+DclvzRzzOi+T+pupJPAdXiS6Xk1Y VCEg== X-Forwarded-Encrypted: i=1; AJvYcCV7r/YUnbmaPM0a0QukxSdGc6QacVcKePZXBFr0px5I4aiW9l4IlHYeISTqE8pIfFQWe5v6cA==@lists.libvirt.org X-Gm-Message-State: AOJu0YwpRuR41pRJEtooQfHijFJ7w/LGrt4WeXddCFcO4sQy3BL/nHLP yICUxuH5ZSjG0mfctVGxztmdKcJoRZADl2Lm3yL46Yf0Jh4qU6pfJiBmQPFABhs= X-Google-Smtp-Source: AGHT+IHVbZz/NiHyXOurXrr4FB8CxiO3Q9AKbXPL4IkGv2v38BzNNCYqkVN9nbSyTMDeNOJpYev++A== X-Received: by 2002:a05:6000:bd2:b0:374:c512:87ce with SMTP id ffacd0b85a97d-378d61f0beemr9107108f8f.30.1726477391525; Mon, 16 Sep 2024 02:03:11 -0700 (PDT) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v2 15/18] contrib/plugins: Add a plugin to generate basic block vectors Date: Mon, 16 Sep 2024 09:53:57 +0100 Message-Id: <20240916085400.1046925-16-alex.bennee@linaro.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20240916085400.1046925-1-alex.bennee@linaro.org> References: <20240916085400.1046925-1-alex.bennee@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: SIIL622OMPNXART36J7EMHJG7JE66ZAP X-Message-ID-Hash: SIIL622OMPNXART36J7EMHJG7JE66ZAP X-MailFrom: alex.bennee@linaro.org X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0 CC: qemu-s390x@nongnu.org, Thomas Huth , Richard Henderson , qemu-ppc@nongnu.org, Ilya Leoshkevich , Paolo Bonzini , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Marcel Apfelbaum , Alexandre Iooss , Eduardo Habkost , Daniel Henrique Barboza , Yanan Wang , Nicholas Piggin , David Hildenbrand , devel@lists.libvirt.org, Mahmoud Mandour , Zhao Liu , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Pierrick Bouvier , Akihiko Odaki , Yotaro Nada X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1726477452428116600 Content-Type: text/plain; charset="utf-8" From: Akihiko Odaki SimPoint is a widely used tool to find the ideal microarchitecture simulation points so Valgrind[2] and Pin[3] support generating basic block vectors for use with them. Let's add a corresponding plugin to QEMU too. Note that this plugin has a different goal with tests/plugin/bb.c. This plugin creates a vector for each constant interval instead of counting the execution of basic blocks for the entire run and able to describe the change of execution behavior. Its output is also syntactically simple and better suited for parsing, while the output of tests/plugin/bb.c is more human-readable. [1] https://cseweb.ucsd.edu/~calder/simpoint/ [2] https://valgrind.org/docs/manual/bbv-manual.html [3] https://www.intel.com/content/www/us/en/developer/articles/tool/pin-a-d= ynamic-binary-instrumentation-tool.html Signed-off-by: Yotaro Nada Signed-off-by: Akihiko Odaki Reviewed-by: Pierrick Bouvier Message-Id: <20240816-bb-v3-1-b9aa4a5c75c5@daynix.com> Signed-off-by: Alex Benn=C3=A9e --- docs/about/emulation.rst | 30 ++++++++ contrib/plugins/bbv.c | 158 +++++++++++++++++++++++++++++++++++++++ contrib/plugins/Makefile | 1 + 3 files changed, 189 insertions(+) create mode 100644 contrib/plugins/bbv.c diff --git a/docs/about/emulation.rst b/docs/about/emulation.rst index eea1261baa..a4470127c9 100644 --- a/docs/about/emulation.rst +++ b/docs/about/emulation.rst @@ -272,6 +272,36 @@ Behaviour can be tweaked with the following arguments: * - idle=3Dtrue|false - Dump the current execution stats whenever the guest vCPU idles =20 +Basic Block Vectors +................... + +``contrib/plugins/bbv.c`` + +The bbv plugin allows you to generate basic block vectors for use with the +`SimPoint `__ analysis tool. + +.. list-table:: Basic block vectors arguments + :widths: 20 80 + :header-rows: 1 + + * - Option + - Description + * - interval=3DN + - The interval to generate a basic block vector specified by the numbe= r of + instructions (Default: N =3D 100000000) + * - outfile=3DPATH + - The path to output files. + It will be suffixed with ``.N.bb`` where ``N`` is a vCPU index. + +Example:: + + $ qemu-aarch64 \ + -plugin contrib/plugins/libbbv.so,interval=3D100,outfile=3Dsha1 \ + tests/tcg/aarch64-linux-user/sha1 + SHA1=3D15dd99a1991e0b3826fede3deffc1feba42278e6 + $ du sha1.0.bb + 23128 sha1.0.bb + Instruction ........... =20 diff --git a/contrib/plugins/bbv.c b/contrib/plugins/bbv.c new file mode 100644 index 0000000000..a5256517dd --- /dev/null +++ b/contrib/plugins/bbv.c @@ -0,0 +1,158 @@ +/* + * Generate basic block vectors for use with the SimPoint analysis tool. + * SimPoint: https://cseweb.ucsd.edu/~calder/simpoint/ + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include +#include + +#include + +typedef struct Bb { + uint64_t vaddr; + struct qemu_plugin_scoreboard *count; + unsigned int index; +} Bb; + +typedef struct Vcpu { + uint64_t count; + FILE *file; +} Vcpu; + +QEMU_PLUGIN_EXPORT int qemu_plugin_version =3D QEMU_PLUGIN_VERSION; +static GHashTable *bbs; +static GRWLock bbs_lock; +static char *filename; +static struct qemu_plugin_scoreboard *vcpus; +static uint64_t interval =3D 100000000; + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + for (int i =3D 0; i < qemu_plugin_num_vcpus(); i++) { + fclose(((Vcpu *)qemu_plugin_scoreboard_find(vcpus, i))->file); + } + + g_hash_table_unref(bbs); + g_free(filename); + qemu_plugin_scoreboard_free(vcpus); +} + +static void free_bb(void *data) +{ + qemu_plugin_scoreboard_free(((Bb *)data)->count); + g_free(data); +} + +static qemu_plugin_u64 count_u64(void) +{ + return qemu_plugin_scoreboard_u64_in_struct(vcpus, Vcpu, count); +} + +static qemu_plugin_u64 bb_count_u64(Bb *bb) +{ + return qemu_plugin_scoreboard_u64(bb->count); +} + +static void vcpu_init(qemu_plugin_id_t id, unsigned int vcpu_index) +{ + g_autofree gchar *vcpu_filename =3D NULL; + Vcpu *vcpu =3D qemu_plugin_scoreboard_find(vcpus, vcpu_index); + + vcpu_filename =3D g_strdup_printf("%s.%u.bb", filename, vcpu_index); + vcpu->file =3D fopen(vcpu_filename, "w"); +} + +static void vcpu_interval_exec(unsigned int vcpu_index, void *udata) +{ + Vcpu *vcpu =3D qemu_plugin_scoreboard_find(vcpus, vcpu_index); + GHashTableIter iter; + void *value; + + if (!vcpu->file) { + return; + } + + vcpu->count -=3D interval; + + fputc('T', vcpu->file); + + g_rw_lock_reader_lock(&bbs_lock); + g_hash_table_iter_init(&iter, bbs); + + while (g_hash_table_iter_next(&iter, NULL, &value)) { + Bb *bb =3D value; + uint64_t bb_count =3D qemu_plugin_u64_get(bb_count_u64(bb), vcpu_i= ndex); + + if (!bb_count) { + continue; + } + + fprintf(vcpu->file, ":%u:%" PRIu64 " ", bb->index, bb_count); + qemu_plugin_u64_set(bb_count_u64(bb), vcpu_index, 0); + } + + g_rw_lock_reader_unlock(&bbs_lock); + fputc('\n', vcpu->file); +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + uint64_t n_insns =3D qemu_plugin_tb_n_insns(tb); + uint64_t vaddr =3D qemu_plugin_tb_vaddr(tb); + Bb *bb; + + g_rw_lock_writer_lock(&bbs_lock); + bb =3D g_hash_table_lookup(bbs, &vaddr); + if (!bb) { + bb =3D g_new(Bb, 1); + bb->vaddr =3D vaddr; + bb->count =3D qemu_plugin_scoreboard_new(sizeof(uint64_t)); + bb->index =3D g_hash_table_size(bbs); + g_hash_table_replace(bbs, &bb->vaddr, bb); + } + g_rw_lock_writer_unlock(&bbs_lock); + + qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( + tb, QEMU_PLUGIN_INLINE_ADD_U64, count_u64(), n_insns); + + qemu_plugin_register_vcpu_tb_exec_inline_per_vcpu( + tb, QEMU_PLUGIN_INLINE_ADD_U64, bb_count_u64(bb), n_insns); + + qemu_plugin_register_vcpu_tb_exec_cond_cb( + tb, vcpu_interval_exec, QEMU_PLUGIN_CB_NO_REGS, + QEMU_PLUGIN_COND_GE, count_u64(), interval, NULL); +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, + const qemu_info_t *info, + int argc, char **argv) +{ + for (int i =3D 0; i < argc; i++) { + char *opt =3D argv[i]; + g_auto(GStrv) tokens =3D g_strsplit(opt, "=3D", 2); + if (g_strcmp0(tokens[0], "interval") =3D=3D 0) { + interval =3D g_ascii_strtoull(tokens[1], NULL, 10); + } else if (g_strcmp0(tokens[0], "outfile") =3D=3D 0) { + filename =3D tokens[1]; + tokens[1] =3D NULL; + } else { + fprintf(stderr, "option parsing failed: %s\n", opt); + return -1; + } + } + + if (!filename) { + fputs("outfile unspecified\n", stderr); + return -1; + } + + bbs =3D g_hash_table_new_full(g_int64_hash, g_int64_equal, NULL, free_= bb); + vcpus =3D qemu_plugin_scoreboard_new(sizeof(Vcpu)); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + qemu_plugin_register_vcpu_init_cb(id, vcpu_init); + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + + return 0; +} diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile index d4ac599f93..bbddd4800f 100644 --- a/contrib/plugins/Makefile +++ b/contrib/plugins/Makefile @@ -13,6 +13,7 @@ TOP_SRC_PATH =3D $(SRC_PATH)/../.. VPATH +=3D $(SRC_PATH) =20 NAMES :=3D +NAMES +=3D bbv NAMES +=3D execlog NAMES +=3D hotblocks NAMES +=3D hotpages --=20 2.39.5