From patchwork Thu Oct 25 17:20:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 10656269 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 830D417DE for ; Thu, 25 Oct 2018 17:40:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 700472C29B for ; Thu, 25 Oct 2018 17:40:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 642802C294; Thu, 25 Oct 2018 17:40:18 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D1C8D2C29B for ; Thu, 25 Oct 2018 17:40:17 +0000 (UTC) Received: from localhost ([::1]:56111 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFjcH-00087j-5d for patchwork-qemu-devel@patchwork.kernel.org; Thu, 25 Oct 2018 13:40:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:40556) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gFjKm-0006mD-Q3 for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gFjKk-0000Sr-00 for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:12 -0400 Received: from out3-smtp.messagingengine.com ([66.111.4.27]:36425) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gFjKj-0000Fa-Oz for qemu-devel@nongnu.org; Thu, 25 Oct 2018 13:22:09 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 4187D2221C; Thu, 25 Oct 2018 13:21:18 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 25 Oct 2018 13:21:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=braap.org; h= from:to:cc:subject:date:message-id:in-reply-to:references; s= mesmtp; bh=0rGxpgjZDCD3/+qCLPmFZWluORjN0xaoJLWO5RaPgwA=; b=ukqt+ hu4NyWgTTwDhNIVglTkCy1GSbx+ut+As5Dvu5LFSKnIPuFhL4jd5M80biZrRVuqy EyM7gODIvBTiwD3VxO73eVVvEJHEqStf1Du85vAi3BPNTHsZKiz+VRAQAbUswpRZ ngvJFRWtvtftjRkzruOSr5M7tCHJoGmEjAkVKQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=0rGxpgjZDCD3/+qCLPmFZWluORjN0 xaoJLWO5RaPgwA=; b=nOEj3gz5EDBNBUtxQDLUB3CT3bfJHKBoPeBb/G97mG6QL 3oOayUI175Ol5d9epzUebNv3GLTuoOeCIO8rKM0kQSTntIjPGY5QvKUQ9OIICjBl RIMlYVSxO/zm//MzDxFxy6vYmrMK5gsOl1Cr5kearLnvT5mYmFZBJW+YKlrdGVks skQoWnaB7BnNviBS8aLxgofYDGqgohHJntgb9AHJz8UHx0o8Jy7QY7k4IAMJLuzr cPB/xAKp3j6p6JclkQOzpVxxiBuvJGRfEk8I88RjYyf44Rg+IY8+4wx4a+4yF0sr M/U5xxmkFfjO1QxsKUHyi/NMdv5pX5DunrvRgMvfQ== X-ME-Sender: X-ME-Proxy: Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id DC2B1E4074; Thu, 25 Oct 2018 13:21:17 -0400 (EDT) From: "Emilio G. Cota" To: qemu-devel@nongnu.org Date: Thu, 25 Oct 2018 13:20:57 -0400 Message-Id: <20181025172057.20414-49-cota@braap.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025172057.20414-1-cota@braap.org> References: <20181025172057.20414-1-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.27 Subject: [Qemu-devel] [RFC 48/48] plugin: add a couple of very simple examples 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: Peter Maydell , =?utf-8?q?Alex_Benn=C3=A9e?= , =?utf-8?q?Llu=C3=ADs?= =?utf-8?q?_Vilanova?= , Pavel Dovgalyuk , Stefan Hajnoczi Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Emilio G. Cota --- plugin-examples/bbcount_avgsize_racy.c | 50 ++++++++++++++++++++++ plugin-examples/mem_count_racy_both.c | 58 ++++++++++++++++++++++++++ plugin-examples/Makefile | 31 ++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 plugin-examples/bbcount_avgsize_racy.c create mode 100644 plugin-examples/mem_count_racy_both.c create mode 100644 plugin-examples/Makefile diff --git a/plugin-examples/bbcount_avgsize_racy.c b/plugin-examples/bbcount_avgsize_racy.c new file mode 100644 index 0000000000..ccdf96c1fa --- /dev/null +++ b/plugin-examples/bbcount_avgsize_racy.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include + +#include + +static uint64_t bb_count; +static uint64_t insn_count; +const char *filename; +static int stdout_fd; + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + dprintf(stdout_fd, "insns: %" PRIu64", bb: %" PRIu64 ", " + "avg insns/bb: %.2f\n", + insn_count, bb_count, (double)insn_count / bb_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, unsigned int cpu_index, + struct qemu_plugin_tb *tb) +{ + unsigned long n_insns = qemu_plugin_tb_n_insns(tb); + + 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) +{ + /* 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; +} diff --git a/plugin-examples/mem_count_racy_both.c b/plugin-examples/mem_count_racy_both.c new file mode 100644 index 0000000000..a47f2025bf --- /dev/null +++ b/plugin-examples/mem_count_racy_both.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include + +#include + +static uint64_t mem_count; +static int stdout_fd; +static bool do_inline; + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + dprintf(stdout_fd, "accesses: %" PRIu64 "\n", mem_count); +} + +static void vcpu_mem(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo, + uint64_t vaddr, void *udata) +{ + mem_count++; +} + +static void vcpu_tb_trans(qemu_plugin_id_t id, unsigned int cpu_index, + 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, + QEMU_PLUGIN_INLINE_ADD_U64, + &mem_count, 1); + } else { + qemu_plugin_register_vcpu_mem_cb(insn, vcpu_mem, + QEMU_PLUGIN_CB_NO_REGS, NULL); + } + } +} + +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; + } + /* 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; +} diff --git a/plugin-examples/Makefile b/plugin-examples/Makefile new file mode 100644 index 0000000000..71bbcda7a8 --- /dev/null +++ b/plugin-examples/Makefile @@ -0,0 +1,31 @@ +NAMES := +NAMES += bbcount_avgsize_racy +NAMES += mem_count_racy_both + +SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES))) + +# QEMU installed path, set by --prefix during configure +QEMU_PATH ?= /data/src/qemu-inst/plugin-test + +CC := gcc +CFLAGS := +CFLAGS += -O2 -Werror -Wall +CFLAGS += -Wundef -Wwrite-strings -Wmissing-prototypes +CFLAGS += -Wstrict-prototypes -Wredundant-decls +CFLAGS += -fno-strict-aliasing -fno-common -fwrapv +CFLAGS += -I$(QEMU_PATH)/include +LDLIBS := -lc + +all: $(SONAMES) + +%.o: %.c + $(CC) $(CFLAGS) -fPIC -c $< -o $@ + +lib%.so: %.o + $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDLIBS) + +clean: + $(RM) -f *.o *.so + $(RM) -Rf .libs + +.PHONY: all clean