Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp800441pxp; Fri, 11 Mar 2022 15:29:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJyZVJqUq80EqxQNlBl57Cfot7dpPFzQF8hgQGBkY2cw65yW3zYjxps8hv/fL0qhn4Jk86Na X-Received: by 2002:a17:90a:5148:b0:1bf:cf3a:6751 with SMTP id k8-20020a17090a514800b001bfcf3a6751mr11919361pjm.82.1647041392675; Fri, 11 Mar 2022 15:29:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647041392; cv=none; d=google.com; s=arc-20160816; b=LNczJ2L+TtTzpsj+T2r0mL3UgpRF2G/gysy2HYgzXdJWGLE7i+6b3jhFw5ChpbZv8j AlZ8irsi8J7NiDGlZNZaPibJLtpnS79buXGRRMghxPtyrBHY7wnFWKhkeBa1o1ydIWaw d6WYTMSrCiLnFvB0UFEVBC6540e0DB53eunmsa9EP8HHWkQCr8dsv6GmZBbjx3HHOdDC 4BAJrOki510wYeIPvZ48i7OE5MPCsMUbdGCw2BJHxtaNK7+l9Hhx13WlLPdwEploKQTK mknDhINT+p5iegdza7ESAmnw3yRkTFAUdS9x0nMm/Nacocp0k7uFpWvUeA8u9FDQLtEo c6mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=3kP5r5OVryNKXjUYWSAGxldjSwy2u45gORSbNRbqjK4=; b=SEKu8OqSfWt2G603J/AMVLhrTcA0QijyBBUubyI4BY0penSwhYU0UhAIXBhPUyri2+ 5Jfczf/y28bclr32+G0wU1Ycu42ttQ/MxL6VDpRtzSLrAtajbADfOhFScKseN/r73b/d JyLkajFM1gezBmZNSodkBBzbWCnlJUp0h97KS8YyxZyDGybh8oYNir/TZUxKJT6D7+qs nkMgU+whXuaBPGHqdVpR687vpnxIbfqLQayOVw1dRUg34lg6L10MNgcnS3hRhj9Nr0b2 ozL9ZQcZFMMsml/nzFc49MzeHrPQaUVAM/GM2zWBptVQjjoiaGuSkAtxNzfKCP9jZWLv Zizw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=N7mCXve5; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id q7-20020a056a00088700b004e17cfb5f6dsi9411089pfj.262.2022.03.11.15.29.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 15:29:52 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=N7mCXve5; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 43EC823067A; Fri, 11 Mar 2022 14:16:17 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243062AbiCJQqz (ORCPT + 99 others); Thu, 10 Mar 2022 11:46:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243057AbiCJQqu (ORCPT ); Thu, 10 Mar 2022 11:46:50 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7157815F0A3 for ; Thu, 10 Mar 2022 08:45:48 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id a3-20020a5b0ac3000000b006288f395b25so4875522ybr.18 for ; Thu, 10 Mar 2022 08:45:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3kP5r5OVryNKXjUYWSAGxldjSwy2u45gORSbNRbqjK4=; b=N7mCXve5ChV6kK9nsxwOshVGth/7qmbMf51kU6YkpgF8R6ESL8tJepyaXcmo6v9Kuk xAO0vPhVtigqvjdK4U6NnOi0n/wTieAJu1R5iAjI/zpHkK9IM52wZ6PniIepdhd0y4uK SXELfzw82lHJe9m9lgBdEL3wrl8pl5nw59x1Pp+loeSS4xtuBCj+BHZoy7BNEYCPdmq3 Oz36xccn/KhThAZBe3+sPV435UdfCcRlXl5NckwOdDv2Oex/19bV2jcFe8UDWnwhVKLJ 625IN7/2hdifRgVYO3JYn9tfHaG7Z9gn/S+TvgcAr4Hys/mb7oOZ8109dSk4J/qH8ZNN pXUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3kP5r5OVryNKXjUYWSAGxldjSwy2u45gORSbNRbqjK4=; b=LSOUavdUlM4TGWP7m3nVsswbRbBbYuDZK4bFUhGD+VWY6RX1XeYSdL+2WN/6ervhLI 41E4dm4jbDPL4lnoY16aRfDtNtHwLqdNfxDITsLfghQ4V6AcyKZtcIi3Q2vXjM9vnDy9 b12cNzySIwOE7KG8XVvWzcQeZNxEqQUnOrHOFldAx/KV0BFQKGU1sThTQAoGyUJTBRAb cUbDRdxEaziIie9rhXQDBKtRhIBlTI77e7uIxvFv9Tb1hGZ6xJXdeQWl/INbjGsOQbVk 0uWUwLZVloFuqz3jpC8rukuu92sFbKWAQf8MdcVVtNEDgmUxRy4oUqiag9t1TS3N0l0X IHtw== X-Gm-Message-State: AOAM531rmR3+Qg3m8WsAXTlgtvdaBH44IPqa/m4dWcoAof+q07y5uV6S irY1ToxJQGmbi4U7jg3rnnXXqrhGfDi2XnhSt+cfOSDUO+Hkc/2FrR4eO3GgQjmaUVB9rkrKf9a 7fgFHoXdjDGyOr6iibHC/pDMVw4WctJMM5zLZCiuzxqLNfccvGTaQSg7O9GAOrnlLpMCHcs57 X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:2d58:733f:1853:8e86]) (user=bgardon job=sendgmr) by 2002:a05:6902:533:b0:629:52d7:e4ae with SMTP id y19-20020a056902053300b0062952d7e4aemr4515236ybs.601.1646930747605; Thu, 10 Mar 2022 08:45:47 -0800 (PST) Date: Thu, 10 Mar 2022 08:45:20 -0800 In-Reply-To: <20220310164532.1821490-1-bgardon@google.com> Message-Id: <20220310164532.1821490-2-bgardon@google.com> Mime-Version: 1.0 References: <20220310164532.1821490-1-bgardon@google.com> X-Mailer: git-send-email 2.35.1.616.g0bdcbb4464-goog Subject: [PATCH 01/13] selftests: KVM: Dump VM stats in binary stats test From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , David Matlack , Jim Mattson , David Dunn , Jing Zhang , Junaid Shahid , Ben Gardon Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add kvm_util library functions to read KVM stats through the binary stats interface and then dump them to stdout when running the binary stats test. Subsequent commits will extend the kvm_util code and use it to make assertions in a test for NX hugepages. CC: Jing Zhang Signed-off-by: Ben Gardon --- .../selftests/kvm/include/kvm_util_base.h | 1 + .../selftests/kvm/kvm_binary_stats_test.c | 3 + tools/testing/selftests/kvm/lib/kvm_util.c | 143 ++++++++++++++++++ 3 files changed, 147 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 92cef0ffb19e..c5f4a67772cb 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -400,6 +400,7 @@ void assert_on_unhandled_exception(struct kvm_vm *vm, uint32_t vcpuid); int vm_get_stats_fd(struct kvm_vm *vm); int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid); +void dump_vm_stats(struct kvm_vm *vm); uint32_t guest_get_vcpuid(void); diff --git a/tools/testing/selftests/kvm/kvm_binary_stats_test.c b/tools/testing/selftests/kvm/kvm_binary_stats_test.c index 17f65d514915..afc4701ce8dd 100644 --- a/tools/testing/selftests/kvm/kvm_binary_stats_test.c +++ b/tools/testing/selftests/kvm/kvm_binary_stats_test.c @@ -174,6 +174,9 @@ static void vm_stats_test(struct kvm_vm *vm) stats_test(stats_fd); close(stats_fd); TEST_ASSERT(fcntl(stats_fd, F_GETFD) == -1, "Stats fd not freed"); + + /* Dump VM stats */ + dump_vm_stats(vm); } static void vcpu_stats_test(struct kvm_vm *vm, int vcpu_id) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 1665a220abcb..4d21c3b46780 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -2556,3 +2556,146 @@ int vcpu_get_stats_fd(struct kvm_vm *vm, uint32_t vcpuid) return ioctl(vcpu->fd, KVM_GET_STATS_FD, NULL); } + +/* Caller is responsible for freeing the returned kvm_stats_header. */ +static struct kvm_stats_header *read_vm_stats_header(int stats_fd) +{ + struct kvm_stats_header *header; + ssize_t ret; + + /* Read kvm stats header */ + header = malloc(sizeof(*header)); + TEST_ASSERT(header, "Allocate memory for stats header"); + + ret = read(stats_fd, header, sizeof(*header)); + TEST_ASSERT(ret == sizeof(*header), "Read stats header"); + + return header; +} + +static void dump_header(int stats_fd, struct kvm_stats_header *header) +{ + ssize_t ret; + char *id; + + printf("flags: %u\n", header->flags); + printf("name size: %u\n", header->name_size); + printf("num_desc: %u\n", header->num_desc); + printf("id_offset: %u\n", header->id_offset); + printf("desc_offset: %u\n", header->desc_offset); + printf("data_offset: %u\n", header->data_offset); + + /* Read kvm stats id string */ + id = malloc(header->name_size); + TEST_ASSERT(id, "Allocate memory for id string"); + ret = pread(stats_fd, id, header->name_size, header->id_offset); + TEST_ASSERT(ret == header->name_size, "Read id string"); + + printf("id: %s\n", id); + + free(id); +} + +static ssize_t stats_desc_size(struct kvm_stats_header *header) +{ + return sizeof(struct kvm_stats_desc) + header->name_size; +} + +/* Caller is responsible for freeing the returned kvm_stats_desc. */ +static struct kvm_stats_desc *read_vm_stats_desc(int stats_fd, + struct kvm_stats_header *header) +{ + struct kvm_stats_desc *stats_desc; + size_t size_desc; + ssize_t ret; + + size_desc = header->num_desc * stats_desc_size(header); + + /* Allocate memory for stats descriptors */ + stats_desc = malloc(size_desc); + TEST_ASSERT(stats_desc, "Allocate memory for stats descriptors"); + + /* Read kvm stats descriptors */ + ret = pread(stats_fd, stats_desc, size_desc, header->desc_offset); + TEST_ASSERT(ret == size_desc, "Read KVM stats descriptors"); + + return stats_desc; +} + +/* Caller is responsible for freeing the memory *data. */ +static int read_stat_data(int stats_fd, struct kvm_stats_header *header, + struct kvm_stats_desc *desc, uint64_t **data) +{ + u64 *stats_data; + ssize_t ret; + + stats_data = malloc(desc->size * sizeof(*stats_data)); + + ret = pread(stats_fd, stats_data, desc->size * sizeof(*stats_data), + header->data_offset + desc->offset); + + /* ret is in bytes. */ + ret = ret / sizeof(*stats_data); + + TEST_ASSERT(ret == desc->size, + "Read data of KVM stats: %s", desc->name); + + *data = stats_data; + + return ret; +} + +static void dump_stat(int stats_fd, struct kvm_stats_header *header, + struct kvm_stats_desc *desc) +{ + u64 *stats_data; + ssize_t ret; + int i; + + printf("\tflags: %u\n", desc->flags); + printf("\texponent: %u\n", desc->exponent); + printf("\tsize: %u\n", desc->size); + printf("\toffset: %u\n", desc->offset); + printf("\tbucket_size: %u\n", desc->bucket_size); + printf("\tname: %s\n", (char *)&desc->name); + + ret = read_stat_data(stats_fd, header, desc, &stats_data); + + printf("\tdata: %lu", *stats_data); + for (i = 1; i < ret; i++) + printf(", %lu", *(stats_data + i)); + printf("\n\n"); + + free(stats_data); +} + +void dump_vm_stats(struct kvm_vm *vm) +{ + struct kvm_stats_desc *stats_desc; + struct kvm_stats_header *header; + struct kvm_stats_desc *desc; + size_t size_desc; + int stats_fd; + int i; + + stats_fd = vm_get_stats_fd(vm); + + header = read_vm_stats_header(stats_fd); + dump_header(stats_fd, header); + + stats_desc = read_vm_stats_desc(stats_fd, header); + + size_desc = stats_desc_size(header); + + /* Read kvm stats data one by one */ + for (i = 0; i < header->num_desc; ++i) { + desc = (void *)stats_desc + (i * size_desc); + dump_stat(stats_fd, header, desc); + } + + free(stats_desc); + free(header); + + close(stats_fd); +} + -- 2.35.1.616.g0bdcbb4464-goog