From patchwork Fri Oct 25 06:37:07 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: 177681 Delivered-To: patch@linaro.org Received: by 2002:a92:409a:0:0:0:0:0 with SMTP id d26csp3289038ill; Fri, 25 Oct 2019 00:37:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqwSdMOVmKrfn2q8pypFdwJUDJuw9envGH6d5X/q340PBIDdeS+gYTV4c3TNgqxbZPuASrXo X-Received: by 2002:ac8:7546:: with SMTP id b6mr1723593qtr.104.1571989021200; Fri, 25 Oct 2019 00:37:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571989021; cv=none; d=google.com; s=arc-20160816; b=OkZeTSJvDUzYFOaNN4pekx2iXRV3a45TaHhigy2OC5oyfUKU+U0XKXrG3nTLvfjrCR GQvYAFbbIJd61UuqAMxRqOKhAY9DR2d4P0eR76YL5M3cTRLy60gRvScP8J4rbNVzGou4 glN+7D9bwrsQTz5hZuQ4yslYM2Kp3hoHRyE+DoclLka4QhxgaJcJwCvUOowMy1sO/cu3 NcS5OeS50Q2GI2TNXV/24zEo0LwOy1jteR5GHIWAp7fKu9afTD+GYxjNgh6NMM9vWKKO ue3Og6J+EhHwx5ouShy65uT58R104FGgkGK7z6x74IdLJF/byKnGyJqNGI7JmMauIn03 05xA== 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=pfQAx5RZvYEJHOBZFdvbNXzxHOiZtn/smcq9fr6c6xw=; b=oOM2i4TNskmpzHuYZ9t/lI0IY53hASUgyxAX2GlpnP6j31fUpmyp1PwA4/8W4uMePs lo5hkTy5HN44451RIoZoAVqVWZTT2puSoM3WDLNYeO4dDC8JCCPRt5kvtY9+/fm3Hlfh AL2QDvDGSP5TOXmW6GbGbysVBLock3zsI44tqb975iZ4V4cKrvdq+c7ZlBKN38SEIES9 WoTlGb8Udemk5V1jkPIorpBeOcqBci6FQ8Yf7bItJmjRFudq8lvHsfikgvFCC/JjvexV oMqFiKnsqLuf/z2qISLKFFyxUaTaXrs0gXqrevbwiIbg8UZWRK4a0zS57JzmJvbXHTrE nRpA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=rQdLN9LL; 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 w63si731524qte.157.2019.10.25.00.37.01 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 25 Oct 2019 00:37: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=rQdLN9LL; 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]:56930 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNu9b-0007tP-UG for patch@linaro.org; Fri, 25 Oct 2019 03:36:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38186) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNtWZ-0003ML-8C for qemu-devel@nongnu.org; Fri, 25 Oct 2019 02:56:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNtWX-0007eG-HA for qemu-devel@nongnu.org; Fri, 25 Oct 2019 02:56:39 -0400 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]:45945) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iNtWX-0007dj-AZ for qemu-devel@nongnu.org; Fri, 25 Oct 2019 02:56:37 -0400 Received: by mail-wr1-x436.google.com with SMTP id q13so937125wrs.12 for ; Thu, 24 Oct 2019 23:56:37 -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=pfQAx5RZvYEJHOBZFdvbNXzxHOiZtn/smcq9fr6c6xw=; b=rQdLN9LL7OSVSBNwRt42fhy6eOOZvLF+fTCCcGBq2hqN3SH8cHRn+AyGf8sQ7tmner femniAfqG6UYFI5hOwU8B/1octAeyrCa11tqMsNB0YOmWxgJnTrnU3DF0whJQxEZsTmd 8DufYLTM/q6cPrLDrGkkVrikdMx1anB5BNdRx31/r0wxro9qaWxvL1sh0LO8AtFj+gct +8SgV5biM7hZRDch7AZljkeM168LLob4LUFMXdTcy0QBzkcLcMxJfxQzAGrUnlR1HeuU LUJdU3NM74JKlPoXnFd8NKX999vlUH+K7MFQmsY+1QGZR/zMPrR59qQ62Puv2h2/kPKG yPKQ== 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=pfQAx5RZvYEJHOBZFdvbNXzxHOiZtn/smcq9fr6c6xw=; b=SuOZlfFQ15ks0ujf07U9S5Qx9iUZlIRukbeon1pjN0Av4Q8ICLbXZPzF+0H6HIcDYk ELP6wHwFo0s5sDssX/XTMKVM9ACm1HibBjd3LiRKiftvzop54QgnWBhZhOsoFkUXX94s RHk4uquzYLe2weTWMQgNEO3BcpZ0oI/ERySzgEi8dIGnFFmkaqds9SERp/HbXfX66oie FyMJkVKj9FnXmKjs8VLkKTYPJAib/gWC98m5dzbAwv16vhhoHzznOVbacmOkY6mDkVvv s0EuBPARKHVFeGjLmjG5bX737ZhcPXc3ImKfV5jNHxxfJGqq0yoeDeh9Y4WP06fzyJbg U6EQ== X-Gm-Message-State: APjAAAXP7Ot2okIh9+3R4zD4ZkNyYE+sCxJEmSn//CQBEoOUAkxT5hvf VptmIBP/sdgTgBg89vN3ZCDJ4w== X-Received: by 2002:adf:ea01:: with SMTP id q1mr1300847wrm.240.1571986596106; Thu, 24 Oct 2019 23:56:36 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id g10sm1244118wrr.28.2019.10.24.23.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2019 23:56:33 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 0E7841FFD9; Fri, 25 Oct 2019 07:37:20 +0100 (BST) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: peter.maydell@linaro.org Subject: [PULL v2 67/73] tests/plugin: add hotpages to analyse memory access patterns Date: Fri, 25 Oct 2019 07:37:07 +0100 Message-Id: <20191025063713.23374-68-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20191025063713.23374-1-alex.bennee@linaro.org> References: <20191025063713.23374-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::436 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: Richard Henderson , =?utf-8?q?Alex_Benn?= =?utf-8?b?w6ll?= , qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This plugin gives a summary of access patterns grouped by "pages" and showing read/write patterns by vCPUS. Signed-off-by: Alex Bennée Reviewed-by: Richard Henderson -- 2.20.1 diff --git a/tests/plugin/Makefile b/tests/plugin/Makefile index 3656429d46b..75467b6db85 100644 --- a/tests/plugin/Makefile +++ b/tests/plugin/Makefile @@ -12,6 +12,7 @@ NAMES += insn NAMES += mem NAMES += hotblocks NAMES += howvec +NAMES += hotpages SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES))) diff --git a/tests/plugin/hotpages.c b/tests/plugin/hotpages.c new file mode 100644 index 00000000000..77df07a3ccf --- /dev/null +++ b/tests/plugin/hotpages.c @@ -0,0 +1,191 @@ +/* + * Copyright (C) 2019, Alex Bennée + * + * Hot Pages - show which pages saw the most memory accesses. + * + * 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 + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +static uint64_t page_size = 4096; +static uint64_t page_mask; +static int limit = 50; +static enum qemu_plugin_mem_rw rw = QEMU_PLUGIN_MEM_RW; +static bool track_io; + +enum sort_type { + SORT_RW = 0, + SORT_R, + SORT_W, + SORT_A +}; + +static int sort_by = SORT_RW; + +typedef struct { + uint64_t page_address; + int cpu_read; + int cpu_write; + uint64_t reads; + uint64_t writes; +} PageCounters; + +static GMutex lock; +static GHashTable *pages; + +static gint cmp_access_count(gconstpointer a, gconstpointer b) +{ + PageCounters *ea = (PageCounters *) a; + PageCounters *eb = (PageCounters *) b; + int r; + switch (sort_by) { + case SORT_RW: + r = (ea->reads + ea->writes) > (eb->reads + eb->writes) ? -1 : 1; + break; + case SORT_R: + r = ea->reads > eb->reads ? -1 : 1; + break; + case SORT_W: + r = ea->writes > eb->writes ? -1 : 1; + break; + case SORT_A: + r = ea->page_address > eb->page_address ? -1 : 1; + break; + default: + g_assert_not_reached(); + } + return r; +} + + +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + g_autoptr(GString) report = g_string_new("Addr, RCPUs, Reads, WCPUs, Writes\n"); + int i; + GList *counts; + + counts = g_hash_table_get_values(pages); + if (counts && g_list_next(counts)) { + GList *it; + + it = g_list_sort(counts, cmp_access_count); + + for (i = 0; i < limit && it->next; i++, it = it->next) { + PageCounters *rec = (PageCounters *) it->data; + g_string_append_printf(report, + "%#016"PRIx64", 0x%04x, %"PRId64 + ", 0x%04x, %"PRId64"\n", + rec->page_address, + rec->cpu_read, rec->reads, + rec->cpu_write, rec->writes); + } + g_list_free(it); + } + + qemu_plugin_outs(report->str); +} + +static void plugin_init(void) +{ + page_mask = (page_size - 1); + pages = g_hash_table_new(NULL, g_direct_equal); +} + +static void vcpu_haddr(unsigned int cpu_index, qemu_plugin_meminfo_t meminfo, + uint64_t vaddr, void *udata) +{ + struct qemu_plugin_hwaddr *hwaddr = qemu_plugin_get_hwaddr(meminfo, vaddr); + uint64_t page; + PageCounters *count; + + /* We only get a hwaddr for system emulation */ + if (track_io) { + if (hwaddr && qemu_plugin_hwaddr_is_io(hwaddr)) { + page = vaddr; + } else { + return; + } + } else { + if (hwaddr && !qemu_plugin_hwaddr_is_io(hwaddr)) { + page = (uint64_t) qemu_plugin_hwaddr_device_offset(hwaddr); + } else { + page = vaddr; + } + } + page &= ~page_mask; + + g_mutex_lock(&lock); + count = (PageCounters *) g_hash_table_lookup(pages, GUINT_TO_POINTER(page)); + + if (!count) { + count = g_new0(PageCounters, 1); + count->page_address = page; + g_hash_table_insert(pages, GUINT_TO_POINTER(page), (gpointer) count); + } + if (qemu_plugin_mem_is_store(meminfo)) { + count->writes++; + count->cpu_write |= (1 << cpu_index); + } else { + count->reads++; + count->cpu_read |= (1 << cpu_index); + } + + g_mutex_unlock(&lock); +} + +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); + qemu_plugin_register_vcpu_mem_cb(insn, vcpu_haddr, + QEMU_PLUGIN_CB_NO_REGS, + rw, NULL); + } +} + +QEMU_PLUGIN_EXPORT +int qemu_plugin_install(qemu_plugin_id_t id, const qemu_info_t *info, + int argc, char **argv) +{ + int i; + + for (i = 0; i < argc; i++) { + char *opt = argv[i]; + if (g_strcmp0(opt, "reads") == 0) { + sort_by = SORT_R; + } else if (g_strcmp0(opt, "writes") == 0) { + sort_by = SORT_W; + } else if (g_strcmp0(opt, "address") == 0) { + sort_by = SORT_A; + } else if (g_strcmp0(opt, "io") == 0) { + track_io = true; + } else if (g_str_has_prefix(opt, "pagesize=")) { + page_size = g_ascii_strtoull(opt + 9, NULL, 10); + } else { + fprintf(stderr, "option parsing failed: %s\n", opt); + return -1; + } + } + + plugin_init(); + + qemu_plugin_register_vcpu_tb_trans_cb(id, vcpu_tb_trans); + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +}