From patchwork Mon Oct 14 10:49:38 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: 176179 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp4379063ill; Mon, 14 Oct 2019 04:26:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqyFldrp0hwU5J91Bul60xFkFdnvi0w/7UnUNq0WaHKMM+Bao9Avt7AIBzjrN6fLtm9+gavV X-Received: by 2002:a37:67d6:: with SMTP id b205mr29550366qkc.183.1571052407129; Mon, 14 Oct 2019 04:26:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571052407; cv=none; d=google.com; s=arc-20160816; b=EU+4Jej2AYjbARHRtNPZdW6znU95YPr5INS3AulfPR2fTe1EYn8T1didcJ9QjWE+zG kfMct5veLlcxC0Hn9ZWo3o3+e+f9KQGoeoVTgIu6gn8Q86oJwo10BWMvqEn8E0MDa5wU iQwGwP8yiH68bClwauHTM33lI4q8H4xJvQcy/YBNAOYpvwLNQxW6lBRzmahVSpRGz1xf UpqLcHozBK6UwzvGvXshRIlqkOH6gYkqHb1Yu8N4A4dr5gDaTTZbS+SfHGLW3hxduxYN XHgMjhWUWuxyNuIsCzmflgohwOiPGxqzI60RmYt4yIvkE1+//CASCxzTCmCl0zsrP1Cv 6IFQ== 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=ouNEnxImoHWm/wNyLGaSm67QiP36d/93Hs/vzEx9qe0=; b=Xnyh4DIud74NaU4Bd86HcMG922dqjuPIL+16m2AB3+5QQ3kkvGLAex+adUuWHJ0VhU fE7IRyioDTdi6hcxo94mM9zJ0YET/tos3zcTnAUPek84yBqtjagh/gohel1aItrudZRk u4/lWW8Z0q3K4xNJMtIR6k8gsGtinPqGoHem3c0yaoWho6H1+Kx2lfxJReQ3D4SEkE/Y sJemf/T31nCnMbnUJKH8XNJP2uRa8ToUTmgIjBL/fsaX6jzIqJAKuF7+cqwh0hqn8Bt7 v+pYg/wqgZG5Wr5nqoYw/VbYp3xzm4nvyexYxLfKvHwC9x9s9jiU5pc19RPKYjIcCM3v 0kBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=SKHSn+DL; 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 2si17557029qkj.366.2019.10.14.04.26.47 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 14 Oct 2019 04:26:47 -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=SKHSn+DL; 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]:47738 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJyUw-0005Zw-4i for patch@linaro.org; Mon, 14 Oct 2019 07:26:46 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45503) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iJy2B-0006fH-Ec for qemu-devel@nongnu.org; Mon, 14 Oct 2019 06:57:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iJy29-0005Aj-QG for qemu-devel@nongnu.org; Mon, 14 Oct 2019 06:57:03 -0400 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]:34513) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iJy29-00058x-Hr for qemu-devel@nongnu.org; Mon, 14 Oct 2019 06:57:01 -0400 Received: by mail-wm1-x341.google.com with SMTP id y135so14227108wmc.1 for ; Mon, 14 Oct 2019 03:57:01 -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=ouNEnxImoHWm/wNyLGaSm67QiP36d/93Hs/vzEx9qe0=; b=SKHSn+DL+ng4qlmoynBsiGbH9Azsi1Xdh+ct127aWpKhh+BQb3kZ1asd6U24F5Naxl NhItxw4YbYFwKJK3BYhtJz1k2cfMdyJHyif519PJaaf6/Gjuw9XTJNYi55PORgR7yqDD +TskcI3Mro0hkR8deZEPJx3i/AIQxSadcadb2QWaePDjNAQfmU/bUtbLW7cxsYmgPtbo ytg6a/CZtqjwEdUhnaWn+hcVIgICihpniZIZf2b/sdCZlO4lvhsdQMYUnlAs2ynuE3t6 Kfbs6by66Gb7L0ysf1WbAF3u+Wo3ijKMS0WEH7B3cwalhBM80tt4YnTvSoR8S9BWY7CT V1Sw== 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=ouNEnxImoHWm/wNyLGaSm67QiP36d/93Hs/vzEx9qe0=; b=BFMVqYROXTd0NNC4Iluor45Yn1v/eQn53jNS45lvkJU40N0Ka/h0ra9ss4X7iY/joZ bCv+d3SBWocxHOdrVlSc4iVT0gffpTzu5otAbM9wmm9ijr52EsCdJu+uKF1z3/9oKywD pIk1927Lmb5LX8W2wzDVQxP0rJYKuH8FBpaAqRtUqlzdqkC5IKJiSPfKnVdUVcZEnHLb n2m2MfZFDomtL3kdI9cDh/7VXQ+l2CoZzDTh7oiQ5IxpxIMF32cRY6yLVzP0BLoWKl4/ xTJL4AvhYeFqCPDTTgYrdKkdJjZxfaGMQlFm3SkEmsR7pI9ktYoymL+0mI7Okgr7/unx n8PA== X-Gm-Message-State: APjAAAV9ZiGusI3eBuEOBj2dUmQ1u7qq8+FwhkVyuigfUwo5SQtEsiqs WW6l8efxUAyz1B5NJq9jMz8IDA== X-Received: by 2002:a7b:c206:: with SMTP id x6mr7007965wmi.147.1571050620018; Mon, 14 Oct 2019 03:57:00 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id f9sm21400754wre.74.2019.10.14.03.56.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 14 Oct 2019 03:56:56 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 0D94C1FF99; Mon, 14 Oct 2019 11:49:59 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v5 45/55] tests/plugin: add a hotblocks plugin Date: Mon, 14 Oct 2019 11:49:38 +0100 Message-Id: <20191014104948.4291-46-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::341 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" 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 --- v5 - handle 0 length report --- tests/plugin/Makefile | 1 + tests/plugin/hotblocks.c | 148 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 tests/plugin/hotblocks.c -- 2.20.1 Reviewed-by: Richard Henderson 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..a150179a5c --- /dev/null +++ b/tests/plugin/hotblocks.c @@ -0,0 +1,148 @@ +/* + * 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; +static int stdout_fd; + +/* 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) +{ + 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); + } + + dprintf(stdout_fd, "%s", report->str); + g_string_free(report, true); +} + +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, 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); + + plugin_init(); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +}