From patchwork Thu Oct 17 13:16:09 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: 176634 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp957970ill; Thu, 17 Oct 2019 06:51:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqwZL/qgKKveVOzARbP2CGm/Baw7WqHBN5RLyLzWlzssgBCwPXp2bJqVyEpbVik8WYwcEA6Z X-Received: by 2002:a50:e445:: with SMTP id e5mr3789727edm.257.1571320311608; Thu, 17 Oct 2019 06:51:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571320311; cv=none; d=google.com; s=arc-20160816; b=OKNCINGkVLfHGPLSlBBALN3U2B3jIX2Oli1/2tIo5AajXVmXD/p/LAJPA9DU0gNkno Dmu0y9MyzRXVOQcNfUWflow53oEIotGUHuBeROKwkaycWpI8s9e9baHKfWeRcAwbr6nV wMBPYyrCAHw9PPRAuDEAQ/4O02eFhFZH7lirai3ITjG13tuQkkChKx44vfmjSm2bfnrM pkInoHK5qjd5IE6p87fEDt6wzKU93F2BptXd21F04AEb6EyoL5ivTaj1IQ2j7GYfRtAp hFE3CDSRDWtwolAzF3zsC317FDmkaYICFG9Cht78bEUK/jB7y7iDwXuMboY4I6snQ5+L SB7w== 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=2I9Xg4qOydxt2Irqp2d5IKEYfjCajVr5MTKGy7FknVU=; b=JjjCngC5pQzaeliwTyTrVUj1EK+grlusM0Fo7lr1IMWLfqLfOuuM2g19WtIZwWcc6o 29v5mjlWlNO1vzFxsXdyujoUqxh6zekvuHW+nEvfRVv8mDKV732Mq5WS6iWuZ+pTk9t2 ferCjtR6Krb0Csplokp2gKHhnNnp+0itO2Bz3us4emYBtFI7hdlL5GEgWobjjcKDmKp5 KzLyq18O4PyC8PPnQEB/P1Q8uwwzr6qlWa74qY7G7o0V2PDGANsB9w83s46UrAGZQgSx hJjlrm7SMnK8KfCxhHcfo3brpyZfjZb2pyfpNqIDmU+U5ijsUZVVt5CaYtzm4peT6KM4 4kYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b="SC8qN0Q/"; 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 b42si1540757eda.367.2019.10.17.06.51.51 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 17 Oct 2019 06:51:51 -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="SC8qN0Q/"; 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]:48416 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iL6By-0007YN-1r for patch@linaro.org; Thu, 17 Oct 2019 09:51:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35570) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iL5mV-0002Hp-FW for qemu-devel@nongnu.org; Thu, 17 Oct 2019 09:25:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iL5mU-0001Hr-2d for qemu-devel@nongnu.org; Thu, 17 Oct 2019 09:25:31 -0400 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]:53409) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iL5mT-0001HP-S1 for qemu-devel@nongnu.org; Thu, 17 Oct 2019 09:25:30 -0400 Received: by mail-wm1-x32a.google.com with SMTP id i16so2564614wmd.3 for ; Thu, 17 Oct 2019 06:25:29 -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=2I9Xg4qOydxt2Irqp2d5IKEYfjCajVr5MTKGy7FknVU=; b=SC8qN0Q/Mg/qkl7yS7oEgA6MzUQqgH8zj4IUo8bCf4WjlZhT5PrZYFs6bqJTg9iB3H xp5UZWt61Qy28ReYoZ29Ui/WZKudfG6oFNLp/95mXnD3vou3E0ZmhaVFfNClUXOB7pna jthDjALf5lV8q10E9S/d0+vCzrV3eSA06QRV1xk19ewvkwCETBh+WSXGTWGqxD+vEyGB ixgmoxYj5wSJcIVhEG45OVSO4GpMbx94XZ7AxcUtUPPi9B7oXjPLOrQbTtbwbnk5KXGN /WKqHAU1YpViFd96DVojtcncp46asazssDwk4BwNLAHqvM31gap+lXlqBmkxvQyZX9i6 c8+A== 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=2I9Xg4qOydxt2Irqp2d5IKEYfjCajVr5MTKGy7FknVU=; b=saloc1i9nJoJEo+cawvx1+qkTAtiwNtF2JYUhYc5vlcgRjdava3ytRAFcjJDR8S18t rQrua0LSMUr9gelg/AzuShn/U6EayfokunPG4Uq44LzNpA+5I51CilPVIL2K10O8tG7O dA+EOXccFFzfoKXe3qojX/8MORRYCXo9dbHhfAxu5Jjro/JSVHs99Gd47roNPvWNTq6U WVj4ws1uyOMbTIYNLckxIEcfmX9OjNR4sKXzLXlWhPM955GYx2mCiGIih9DKM2MOOCeC KiG+EPbvzJotqIjGarQQ36hRTqi4CcF9O+EUcfEq3vYW5x5N31MfQfYAR+fjvSC2WU0w W68g== X-Gm-Message-State: APjAAAUGYDWVrqvdoxPOIg1D1NB3zQ59rhgU9XixNPUsbJOcbHvmbQcM JnvXjhdv9N3K0RZzOgpXsZlAxw== X-Received: by 2002:a7b:c938:: with SMTP id h24mr2912915wml.117.1571318728580; Thu, 17 Oct 2019 06:25:28 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id y186sm2689296wmb.41.2019.10.17.06.25.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 06:25:25 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 6C3471FFC6; Thu, 17 Oct 2019 14:16:20 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v6 48/54] tests/plugin: add a hotblocks plugin Date: Thu, 17 Oct 2019 14:16:09 +0100 Message-Id: <20191017131615.19660-49-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191017131615.19660-1-alex.bennee@linaro.org> References: <20191017131615.19660-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::32a 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: robert.foley@futurewei.com, Richard Henderson , peter.puhov@futurewei.com, aaron@os.amperecomputing.com, cota@braap.org, =?utf-8?q?Alex_Benn?= =?utf-8?b?w6ll?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This is a simple plugin to track which translation blocks are call most often. As we don't have a view of the internals of TCG we can only work by the address of the start of the block so we also need to tracks how often the address is translated. As there will be multiple blocks starting at the same address. We can try and work around this by futzing the value to feed to the hash with the insn count. Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson --- v5 - handle 0 length report v6 - init api update - use plugin_outs squash! tests/plugin: add a hotblocks plugin --- tests/plugin/Makefile | 1 + tests/plugin/hotblocks.c | 143 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 tests/plugin/hotblocks.c -- 2.20.1 diff --git a/tests/plugin/Makefile b/tests/plugin/Makefile index f9a3546ea3..e74940eaac 100644 --- a/tests/plugin/Makefile +++ b/tests/plugin/Makefile @@ -10,6 +10,7 @@ NAMES += bb NAMES += empty NAMES += insn NAMES += mem +NAMES += hotblocks SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES))) diff --git a/tests/plugin/hotblocks.c b/tests/plugin/hotblocks.c new file mode 100644 index 0000000000..1bd183849a --- /dev/null +++ b/tests/plugin/hotblocks.c @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2019, Alex Bennée + * + * License: GNU GPL, version 2 or later. + * See the COPYING file in the top-level directory. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static bool do_inline; + +/* Plugins need to take care of their own locking */ +static GMutex lock; +static GHashTable *hotblocks; +static guint64 limit = 20; + +/* + * Counting Structure + * + * The internals of the TCG are not exposed to plugins so we can only + * get the starting PC for each block. We cheat this slightly by + * xor'ing the number of instructions to the hash to help + * differentiate. + */ +typedef struct { + uint64_t start_addr; + uint64_t exec_count; + int trans_count; + unsigned long insns; +} ExecCount; + +static gint cmp_exec_count(gconstpointer a, gconstpointer b) +{ + ExecCount *ea = (ExecCount *) a; + ExecCount *eb = (ExecCount *) b; + return ea->exec_count > eb->exec_count ? -1 : 1; +} + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + g_autoptr(GString) report = g_string_new("collected "); + GList *counts, *it; + int i; + + g_mutex_lock(&lock); + g_string_append_printf(report, "%d entries in the hash table\n", + g_hash_table_size(hotblocks)); + counts = g_hash_table_get_values(hotblocks); + it = g_list_sort(counts, cmp_exec_count); + + if (it) { + g_string_append_printf(report, "pc, tcount, icount, ecount\n"); + + for (i = 0; i < limit && it->next; i++, it = it->next) { + ExecCount *rec = (ExecCount *) it->data; + g_string_append_printf(report, "%#016"PRIx64", %d, %ld, %"PRId64"\n", + rec->start_addr, rec->trans_count, + rec->insns, rec->exec_count); + } + + g_list_free(it); + g_mutex_unlock(&lock); + } + + qemu_plugin_outs(report->str); +} + +static void plugin_init(void) +{ + hotblocks = g_hash_table_new(NULL, g_direct_equal); +} + +static void vcpu_tb_exec(unsigned int cpu_index, void *udata) +{ + ExecCount *cnt; + uint64_t hash = (uint64_t) udata; + + g_mutex_lock(&lock); + cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash); + /* should always succeed */ + g_assert(cnt); + cnt->exec_count++; + g_mutex_unlock(&lock); +} + +/* + * When do_inline we ask the plugin to increment the counter for us. + * Otherwise a helper is inserted which calls the vcpu_tb_exec + * callback. + */ +static void vcpu_tb_trans(qemu_plugin_id_t id, struct qemu_plugin_tb *tb) +{ + ExecCount *cnt; + uint64_t pc = qemu_plugin_tb_vaddr(tb); + unsigned long insns = qemu_plugin_tb_n_insns(tb); + uint64_t hash = pc ^ insns; + + g_mutex_lock(&lock); + cnt = (ExecCount *) g_hash_table_lookup(hotblocks, (gconstpointer) hash); + if (cnt) { + cnt->trans_count++; + } else { + cnt = g_new0(ExecCount, 1); + cnt->start_addr = pc; + cnt->trans_count = 1; + cnt->insns = insns; + g_hash_table_insert(hotblocks, (gpointer) hash, (gpointer) cnt); + } + + g_mutex_unlock(&lock); + + if (do_inline) { + qemu_plugin_register_vcpu_tb_exec_inline(tb, QEMU_PLUGIN_INLINE_ADD_U64, + &cnt->exec_count, 1); + } else { + qemu_plugin_register_vcpu_tb_exec_cb(tb, vcpu_tb_exec, + QEMU_PLUGIN_CB_NO_REGS, + (void *)hash); + } +} + +QEMU_PLUGIN_EXPORT +int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, + int argc, char **argv) +{ + if (argc && strcmp(argv[0], "inline") == 0) { + do_inline = true; + } + + plugin_init(); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +}