From patchwork Mon Oct 14 10:49:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 176198 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp4400167ill; Mon, 14 Oct 2019 04:47:02 -0700 (PDT) X-Google-Smtp-Source: APXvYqyfXxXVst6ad0xfXiSwmBUhNG980d0RTsOjavCXThi7idgtXbp7jdnH+dqqVYqzWR5BGVsm X-Received: by 2002:a0c:b057:: with SMTP id l23mr30634758qvc.216.1571053621996; Mon, 14 Oct 2019 04:47:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571053621; cv=none; d=google.com; s=arc-20160816; b=nv7G+pxdKaTJ2WtMMWWvPYXieugknmoO55NFQkUlY9bFNYxcXX+eLXnCl+hSmuI88e DQTHq8bZGP4Sop7cRgduD65cThpBi0LzGSK9lIm+DhKWrzf2mjbUSezzIRTL4oUYOxN8 +u/n4SwNKAMQmXGAxl/EOeciWlPYi4w7Zb/WqnuYhPfb866n4MKRvIP/2OIswRObsgmf JJzU32HePkk2IhvEhtAYJGmwS+KmQXJcJCjrBbMaKUyx73xTVJir3adh3q4LjsxOKt1P hnBneajQzOQ6CFfjut9mjz0iW8624m1VX7pXZ1Qu4NZfF7EdifJsMYZ2aGtsc5/gR6/g phPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=jb94Z+Gp/SEto0c7c9pjyGvoGowBStIfIYtnSg1TGYw=; b=PRR3Ig32/QHCounpwWbIB6x7HEG65zQDEAO6TrTN5fuI3UKcPTmFh/vIA6kZ0hbZ7X 5cjUBia3RTGe/qMh/AZMi9c3OBLmL0C60mg7wkCy1P+ZHsniXXCxfkMQcQY4vCgXa03m OmSsyQqqFTQJwJOyhjprYFmh/0nFzpDtArm5Bdps/iKypxJrFQ+IQpVBqhFiXpK5OV8+ HoSgUUA/HB0IM7dTvEwUokg25lQ3cbaKN2CT0lakqPD4HCv8IBsQQ68cYqbUsIiEc+6y fWPRWUFH69bJ3TMBG+CElIuIJhZG47knmbW2APDFxrJatO6/qDFrx94SRQfDM9wgVsDs UvwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=ZTxPVhnl; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id j62si16795931qtd.367.2019.10.14.04.47.01 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 14 Oct 2019 04:47:01 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=ZTxPVhnl; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:48090 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJyoX-0001pX-CK for patch@linaro.org; Mon, 14 Oct 2019 07:47:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46909) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJyCF-0008C8-7A for qemu-devel@nongnu.org; Mon, 14 Oct 2019 07:07:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iJyCD-0007mQ-H8 for qemu-devel@nongnu.org; Mon, 14 Oct 2019 07:07:27 -0400 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]:41285) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iJyCD-0007lU-AA for qemu-devel@nongnu.org; Mon, 14 Oct 2019 07:07:25 -0400 Received: by mail-wr1-x441.google.com with SMTP id p4so3326796wrm.8 for ; Mon, 14 Oct 2019 04:07:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jb94Z+Gp/SEto0c7c9pjyGvoGowBStIfIYtnSg1TGYw=; b=ZTxPVhnlh6IJ0i9/HS02KCIiEi452j/wOypJG/2pzNwQM0y55fz2G6P+8xE1OdzHRW 5NwTdR70fOms4try8kfS76K5rlAhyJX7qvxyK7vH9kuLEwTmGtLoyyvCeZ4pWpCBJid0 iNk8WARfSjA9N7g3EDTvdHZOKHBhicbmfrM4rvrx69f+KYaVH2YZvJFYyJirw+kbUqDN 17k33wRfEncHSllzRMpz5jfP3poJiqICCdf3nsXxc5kQE56tPfJf47uNld/wgp0zPdXd MzGh3tVLq5o8GTTxdkdxrImEwkSGrjJwH49Lw8opIOFJIqmy9e/mBpZv95Khl7yz+dLh WhfQ== 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:mime-version:content-transfer-encoding; bh=jb94Z+Gp/SEto0c7c9pjyGvoGowBStIfIYtnSg1TGYw=; b=rRoTFY7nyFF0+IUhRcrxcq8QI7MGBf5ed4o6rWKBWaF5lzik3CICpjkQeVijZ7V713 s7bFLPiP7iUSksIPLWXw/XtYEi/q5ETtGm1OTqITijRemK2LLzc4Zv2io87Vw0N0o9Vz JDGeMFNWZPFjuWAebvae9Z88BbgOKrT8BsYUGoSoVcXSj7/lk/nqZrJPhtRj+QcWOk+H 7FlPkl0hZVzOw6d9N6RlkpWGX0ce+z2sv+B3hJORSzpAV8LbhU0YOgRmDZmp26uEa+1p wdkZHsvfvGKHMYCW1ANNRt84Rv3kFCcrOSDBXbvZ0ZI/pRpUon0Wjs6393UMq1DKY219 pX0g== X-Gm-Message-State: APjAAAX3hI7HMiM6e3rPHuzh7b28kSmiRDnUFU7I0ajv1WU4RMb/atFl NC80UyOirlr45WRQ7M13iSAExg== X-Received: by 2002:adf:f188:: with SMTP id h8mr25028454wro.38.1571051244216; Mon, 14 Oct 2019 04:07:24 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id f8sm19894304wmb.37.2019.10.14.04.06.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2019 04:07:23 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id C01971FFC9; Mon, 14 Oct 2019 11:49:57 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v5 39/55] tests/plugin: add sample plugins Date: Mon, 14 Oct 2019 11:49:32 +0100 Message-Id: <20191014104948.4291-40-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191014104948.4291-1-alex.bennee@linaro.org> References: <20191014104948.4291-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::441 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: aaron@os.amperecomputing.com, cota@braap.org, =?utf-8?q?Alex_Benn?= =?utf-8?b?w6ll?= , robert.foley@futurewei.com, peter.puhov@futurewei.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: "Emilio G. Cota" Pass arguments with -plugin=libfoo.so,arg=bar,arg=baz Signed-off-by: Emilio G. Cota Signed-off-by: Alex Bennée --- v4 - tweaks for hwaddr API --- configure | 1 + tests/plugin/Makefile | 28 +++++++++++++ tests/plugin/bb.c | 65 +++++++++++++++++++++++++++++ tests/plugin/empty.c | 29 +++++++++++++ tests/plugin/insn.c | 62 ++++++++++++++++++++++++++++ tests/plugin/mem.c | 96 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 281 insertions(+) create mode 100644 tests/plugin/Makefile create mode 100644 tests/plugin/bb.c create mode 100644 tests/plugin/empty.c create mode 100644 tests/plugin/insn.c create mode 100644 tests/plugin/mem.c -- 2.20.1 Reviewed-by: Richard Henderson diff --git a/configure b/configure index 75632b1df1..d5527a9f15 100755 --- a/configure +++ b/configure @@ -7887,6 +7887,7 @@ DIRS="$DIRS roms/seabios roms/vgabios" LINKS="Makefile" LINKS="$LINKS tests/tcg/lm32/Makefile po/Makefile" LINKS="$LINKS tests/tcg/Makefile.target tests/fp/Makefile" +LINKS="$LINKS tests/plugin/Makefile" LINKS="$LINKS pc-bios/optionrom/Makefile pc-bios/keymaps" LINKS="$LINKS pc-bios/s390-ccw/Makefile" LINKS="$LINKS roms/seabios/Makefile roms/vgabios/Makefile" diff --git a/tests/plugin/Makefile b/tests/plugin/Makefile new file mode 100644 index 0000000000..f9a3546ea3 --- /dev/null +++ b/tests/plugin/Makefile @@ -0,0 +1,28 @@ +BUILD_DIR := $(CURDIR)/../.. + +include $(BUILD_DIR)/config-host.mak +include $(SRC_PATH)/rules.mak + +$(call set-vpath, $(SRC_PATH)/tests/plugin) + +NAMES := +NAMES += bb +NAMES += empty +NAMES += insn +NAMES += mem + +SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES))) + +QEMU_CFLAGS += -fPIC +QEMU_CFLAGS += -I$(SRC_PATH)/include/qemu + +all: $(SONAMES) + +lib%.so: %.o + $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDLIBS) + +clean: + rm -f *.o *.so *.d + rm -Rf .libs + +.PHONY: all clean diff --git a/tests/plugin/bb.c b/tests/plugin/bb.c new file mode 100644 index 0000000000..93d25de363 --- /dev/null +++ b/tests/plugin/bb.c @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2018, Emilio G. Cota + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#include +#include +#include +#include +#include +#include + +#include + +static uint64_t bb_count; +static uint64_t insn_count; +static int stdout_fd; +static bool do_inline; + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + dprintf(stdout_fd, "bb's: %" PRIu64", insns: %" PRIu64 "\n", + bb_count, insn_count); +} + +static void vcpu_tb_exec(unsigned int cpu_index, void *udata) +{ + unsigned long n_insns = (unsigned long)udata; + + insn_count += n_insns; + bb_count++; +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + unsigned long n_insns = qemu_plugin_tb_n_insns(tb); + + if (do_inline) { + qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64, + &bb_count, 1); + qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64, + &insn_count, n_insns); + } else { + qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec, + QEMU_PLUGIN_CB_NO_REGS, + (void *)n_insns); + } +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, + char **argv) +{ + if (argc && strcmp(argv[0], "inline") == 0) { + do_inline = true; + } + + /* to be used when in the exit hook */ + stdout_fd = dup(STDOUT_FILENO); + assert(stdout_fd); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +} diff --git a/tests/plugin/empty.c b/tests/plugin/empty.c new file mode 100644 index 0000000000..b141ddd0df --- /dev/null +++ b/tests/plugin/empty.c @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2018, Emilio G. Cota + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#include +#include +#include +#include +#include +#include + +#include + +/* + * Empty TB translation callback. + * This allows us to measure the overhead of injecting and then + * removing empty instrumentation. + */ +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ } + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, + char **argv) +{ + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + return 0; +} diff --git a/tests/plugin/insn.c b/tests/plugin/insn.c new file mode 100644 index 0000000000..3000ab4b73 --- /dev/null +++ b/tests/plugin/insn.c @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2018, Emilio G. Cota + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#include +#include +#include +#include +#include +#include + +#include + +static int stdout_fd; +static uint64_t insn_count; +static bool do_inline; + +static void vcpu_insn_exec_before(unsigned int cpu_index, void *udata) +{ + insn_count++; +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + size_t n = qemu_plugin_tb_n_insns(tb); + size_t i; + + for (i = 0; i < n; i++) { + struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i); + + if (do_inline) { + qemu_plugin_register_vcpu_insn_exec_inline( + insn, QEMU_PLUGIN_INLINE_ADD_U64, &insn_count, 1); + } else { + qemu_plugin_register_vcpu_insn_exec_cb( + insn, vcpu_insn_exec_before, QEMU_PLUGIN_CB_NO_REGS, NULL); + } + } +} + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + dprintf(stdout_fd, "insns: %" PRIu64 "\n", insn_count); +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, + char **argv) +{ + if (argc && !strcmp(argv[0], "inline")) { + do_inline = true; + } + + /* to be used when in the exit hook */ + stdout_fd = dup(STDOUT_FILENO); + assert(stdout_fd); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +} diff --git a/tests/plugin/mem.c b/tests/plugin/mem.c new file mode 100644 index 0000000000..e5490f4a99 --- /dev/null +++ b/tests/plugin/mem.c @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2018, Emilio G. Cota + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#include +#include +#include +#include +#include +#include + +#include + +static uint64_t mem_count; +static uint64_t io_count; +static int stdout_fd; +static bool do_inline; +static bool do_haddr; +static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW; + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + dprintf(stdout_fd, "mem accesses: %" PRIu64 "\n", mem_count); + if (do_haddr) { + dprintf(stdout_fd, "io accesses: %" PRIu64 "\n", mem_count); + } +} + +static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo, + uint64_t vaddr, void *udata) +{ + if (do_haddr) { + struct qemu_plugin_hwaddr *hwaddr; + hwaddr = qemu_plugin_get_hwaddr(meminfo, vaddr); + if (qemu_plugin_hwaddr_is_io(hwaddr)) { + io_count++; + } else { + mem_count++; + } + } else { + mem_count++; + } +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + size_t n = qemu_plugin_tb_n_insns(tb); + size_t i; + + for (i = 0; i < n; i++) { + struct qemu_plugin_insn *insn = qemu_plugin_tb_get_insn(tb, i); + + if (do_inline) { + qemu_plugin_register_vcpu_mem_inline(insn, rw, + QEMU_PLUGIN_INLINE_ADD_U64, + &mem_count, 1); + } else { + qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem, + QEMU_PLUGIN_CB_NO_REGS, + rw, NULL); + } + } +} + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, int argc, + char **argv) +{ + if (argc) { + if (argc >= 3) { + if (!strcmp(argv[2], "haddr")) { + do_haddr = true; + } + } + if (argc >= 2) { + const char *str = argv[1]; + + if (!strcmp(str, "r")) { + rw = QEMU_PLUGIN_MEM_R; + } else if (!strcmp(str, "w")) { + rw = QEMU_PLUGIN_MEM_W; + } + } + if (!strcmp(argv[0], "inline")) { + do_inline = true; + } + } + /* plugin_exit might write to stdout after stdout has been closed */ + stdout_fd = dup(STDOUT_FILENO); + assert(stdout_fd); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +}