From patchwork Tue Nov 8 16:49:21 2022 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: 622648 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp3073801pvb; Tue, 8 Nov 2022 08:50:34 -0800 (PST) X-Google-Smtp-Source: AMsMyM7LetGiQyRLkWer6/JLzsENRQbTKVWqaSBiVbf4RPU5D/94pV9iG8n2zTeP0um8x0jmEmt6 X-Received: by 2002:a05:6214:ac7:b0:4bb:6236:e190 with SMTP id g7-20020a0562140ac700b004bb6236e190mr50245053qvi.87.1667926234184; Tue, 08 Nov 2022 08:50:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667926234; cv=none; d=google.com; s=arc-20160816; b=IXWsE6KP20JuWHjvLlJMEzqvwXSiucgaBl3tvmUGCHSUUeyoSSbGXZUIlxV/uohaIg 1CsXq9kWRHdaSNJ3iKrIP0yAEg16vBZ9NIVSwI8K7yYNcQkm7Q9kFbkUBGDUNchhLFEp 5lO4i0ZJXvIbbANxYDFGJDKBvLhO+Zp6ODuy+qpWiqLYaMmRpoJdo+US7H+YDanBHTzU g9k3R+XrRAmAK7DrYkZgag0tGfHFVkD9t4If0YY86Yf1Etq39YVTPGyim+yEHXaejt5I rbwNKMOrT/3B8FG1XcxfI4ua4zVVpBgxoZOshe7U3d3AHA/agfR6wfibkLzosTOXcrAO 2p2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=uU9x535hBA9H0/7B2F4VwSab2yApwGhUC89KkuwR1vM=; b=p8cZCEkd+t+QVw068fzIZssEiGhVyIYfQo6fcNj4vhsWkHmRZx2NM5X7Q1LvQRyszg IUkgvq9nAV0UZjhZuRDeZYaHzmqC8yMGMw9Q8md+nmh4LNKTBEk+SjG12IfO9zDJLoLt K9MCU2yLaA590u9U9vJLThKwZj2Y90Zi/PSFv36iQ2/Mr11qtCaqsKNrpcSUBmPhSjMy M2bTzf8E+pf8iiX1NqNbBISSUMbVgP+40ymuHvba1afZYqysJ5M8poAr+y9PF5jWPQ7z lJ1s/JEUqDSMAFq7tAPPceL+/YL5dtaYSIfjTAatcw5qfLEX2EQNr1OfeFYisJKMybiv 72hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KSKLZlsy; 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=pass (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 h7-20020a0562140da700b004b46bdb02a7si7028731qvh.400.2022.11.08.08.50.34 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 08 Nov 2022 08:50:34 -0800 (PST) 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=pass header.i=@linaro.org header.s=google header.b=KSKLZlsy; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1osRnD-0003qr-Ji; Tue, 08 Nov 2022 11:49:43 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1osRnA-0003pe-S1 for qemu-devel@nongnu.org; Tue, 08 Nov 2022 11:49:40 -0500 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1osRn5-0001qy-OM for qemu-devel@nongnu.org; Tue, 08 Nov 2022 11:49:40 -0500 Received: by mail-wr1-x430.google.com with SMTP id h9so21956111wrt.0 for ; Tue, 08 Nov 2022 08:49:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=uU9x535hBA9H0/7B2F4VwSab2yApwGhUC89KkuwR1vM=; b=KSKLZlsygk5Yl+PCADVr8HCnfmBqqR5e24ybs0ZjND0qHG+E9MSkNKbuy4eYMKhVka rMtBCSRR0Xhw+JLa2LGLM6rjni6d0vPg3YM7jWa+x/Lt+wSGzJYkXrPeAyhqGqvi8YRW Xt59mzgTF64clvvFbpJXCzBXfuAUbaDSxektUJQdjB2yVeKAM1C496URWMAgHX4W9jIk rA9fLCayPwWVcdPIJ50S6m+Jjlxk74pgB/CtjJB5VvtWcw8B6TgEFJuZIWXQh6jUfCFb P2Y+ZLXG8A4sOutAUD12cf8xkbBhRKDICX4V/QheudkSf7L4TpZIRIaCC7nYxxFlbwJD jjrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uU9x535hBA9H0/7B2F4VwSab2yApwGhUC89KkuwR1vM=; b=vKOOFM8rmwjQQC0ezeAplpYuRc2FBvlHxnfXTzeQbBcnpG4Iiu8uU1msDzZdtIq26J rc8rYX8cbsa8/HXm5SgIgMjVhSfcRiHw/v2zD3iyleta7uGw32z8ruuJi4AslpWW7d7Z tFOmKTCn5Sf3Nrnc2qiNdhTofyxkIBTv6qgKu73o+AphknIZxLdiDTcvgdAaFB1R9BBW fYxcs8dRgns41JqT4HTvs/8po2GHaaPXomKrIN9orOlO09gQklLwZGwhSqdEq/9ZzgVF 0+HHMtAOKG7K0f+Ozy1in4Vvj+fokOFL2l921WA5fLRxGlUpSTMBOozsOmwfhTM9/k5E POWg== X-Gm-Message-State: ACrzQf0QgXsrupeSZmWxD8M5HTtQzOqZQbHrs4K/YCqX52/2GpZhhYdT oglgmzKD3eWqY1oz75SuLPB5lw== X-Received: by 2002:a5d:60d1:0:b0:237:46c8:1f6d with SMTP id x17-20020a5d60d1000000b0023746c81f6dmr20942972wrt.232.1667926173251; Tue, 08 Nov 2022 08:49:33 -0800 (PST) Received: from zen.linaroharston ([185.81.254.11]) by smtp.gmail.com with ESMTPSA id e4-20020adff344000000b002364c77bc96sm11088095wrp.33.2022.11.08.08.49.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Nov 2022 08:49:32 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 000F91FFB7; Tue, 8 Nov 2022 16:49:31 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= , Idan Horowitz , Alexandre Iooss , Mahmoud Mandour Subject: [RFC PATCH] tests/plugins: add a new vcpu state tracking plugin Date: Tue, 8 Nov 2022 16:49:21 +0000 Message-Id: <20221108164921.1980508-1-alex.bennee@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::430; envelope-from=alex.bennee@linaro.org; helo=mail-wr1-x430.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Although we call qemu_plugin_register_vcpu_idle_cb() in the bb test we don't really exercise the rest of the state change callbacks. Add a new test that tests the whole API. [AJB: I wrote this in an attempt to flush out a reproducer for #1195 although so far now joy.] Signed-off-by: Alex Bennée Cc: Idan Horowitz --- tests/plugin/vcpu.c | 140 +++++++++++++++++++++++++++++++++++++++ tests/plugin/meson.build | 2 +- 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 tests/plugin/vcpu.c diff --git a/tests/plugin/vcpu.c b/tests/plugin/vcpu.c new file mode 100644 index 0000000000..8bf9acd1df --- /dev/null +++ b/tests/plugin/vcpu.c @@ -0,0 +1,140 @@ +/* + * Test plugin for exercising the vcpu event callbacks. These exist + * for when vcpus are created and destroyed (especially in linux-user + * where vcpu ~= thread) and when they pause and restart (generally + * for wfi and the like in system emulation). + * + * Copyright (c) 2022 Linaro Ltd + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION; + +typedef struct { + uint64_t start_time_ns; + uint64_t idle_count; + uint64_t last_idle_ts; + uint64_t total_idle_ns; + uint64_t exit_time_ns; +} VCPUData; + +static GMutex expand_counts_lock; +static GArray *counts; /* array of VCPUData */ +static bool sys_emu; + +/* + * Fetch VCPU data for a given index, allocate if required. + */ +static VCPUData * get_vcpu_data(int cpu_index) +{ + if (cpu_index >= counts->len) { + g_mutex_lock(&expand_counts_lock); + counts = g_array_set_size(counts, cpu_index + 1); + g_mutex_unlock(&expand_counts_lock); + } + /* race if set size re-allocs? */ + return &g_array_index(counts, VCPUData, cpu_index); +} + +static uint64_t get_timestamp(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ts.tv_sec * 1000000000LL + ts.tv_nsec; +} + +static void vcpu_init(qemu_plugin_id_t id, unsigned int cpu_index) +{ + VCPUData *d = get_vcpu_data(cpu_index); + d->start_time_ns = get_timestamp(); +} + +static void vcpu_idle(qemu_plugin_id_t id, unsigned int cpu_index) +{ + VCPUData *d = get_vcpu_data(cpu_index); + d->last_idle_ts = get_timestamp(); + d->idle_count++; +} + +static void vcpu_resume(qemu_plugin_id_t id, unsigned int cpu_index) +{ + VCPUData *d = get_vcpu_data(cpu_index); + uint64_t now = get_timestamp(); + d->total_idle_ns += now - d->last_idle_ts; +} + +static void vcpu_exit(qemu_plugin_id_t id, unsigned int cpu_index) +{ + VCPUData *d = get_vcpu_data(cpu_index); + d->exit_time_ns = get_timestamp(); +} + +/* + * Report our final stats + */ +static void plugin_exit(qemu_plugin_id_t id, void *p) +{ + g_autoptr(GString) report = g_string_new(""); + const char *vcpu_or_thread = sys_emu ? "vcpu" : "thread"; + int i; + + g_string_printf(report, "Exit: we had a total of %d %ss\n", + counts->len, vcpu_or_thread); + + for (i = 0; i < counts->len; i++) { + VCPUData *d = &g_array_index(counts, VCPUData, i); + + /* FIXME: we never see vcpu_exit for the main thread */ + if (!d->exit_time_ns) { + d->exit_time_ns = get_timestamp(); + } + + g_string_append_printf(report, "%s %d: %"PRId64" µs lifetime", + vcpu_or_thread, i, + (d->exit_time_ns - d->start_time_ns) / 1000); + if (d->idle_count) { + uint64_t idle_us = d->total_idle_ns / 1000; + uint64_t idle_avg = d->total_idle_ns / d->idle_count; + g_string_append_printf(report, ", %"PRId64" idles, %" + PRId64 " µs total idle time, %" + PRId64 " ns per idle", + d->idle_count, idle_us, idle_avg); + } + g_string_append_printf(report, "\n"); + } + qemu_plugin_outs(report->str); +} + + +QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, + const qemu_info_t *info, + int argc, char **argv) +{ + int entries = 2; + + if (info->system_emulation) { + entries = info->system.max_vcpus; + sys_emu = true; + } + + counts = g_array_sized_new(true, true, sizeof(VCPUData), entries); + g_mutex_init(&expand_counts_lock); + + qemu_plugin_register_vcpu_init_cb(id, vcpu_init); + qemu_plugin_register_vcpu_idle_cb(id, vcpu_idle); + qemu_plugin_register_vcpu_resume_cb(id, vcpu_resume); + qemu_plugin_register_vcpu_exit_cb(id, vcpu_exit); + + qemu_plugin_register_atexit_cb(id, plugin_exit, NULL); + return 0; +} diff --git a/tests/plugin/meson.build b/tests/plugin/meson.build index 2bbfc4b19e..8c6b232183 100644 --- a/tests/plugin/meson.build +++ b/tests/plugin/meson.build @@ -1,5 +1,5 @@ t = [] -foreach i : ['bb', 'empty', 'insn', 'mem', 'syscall'] +foreach i : ['bb', 'empty', 'insn', 'mem', 'syscall', 'vcpu'] t += shared_module(i, files(i + '.c'), include_directories: '../../include/qemu', dependencies: glib)