Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2337078pxp; Mon, 21 Mar 2022 17:20:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZ0UQFY9wveJ1U+pjehR9Da359uIY39gxeqGog9jMPv3jDcSHbaBnnfkRpu4E5IYWotL3R X-Received: by 2002:a05:6a00:13a9:b0:4fa:ae49:7c06 with SMTP id t41-20020a056a0013a900b004faae497c06mr4031990pfg.38.1647908408218; Mon, 21 Mar 2022 17:20:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647908408; cv=none; d=google.com; s=arc-20160816; b=ZIAHG83H6Yr5cyKhyaD5ZUi90DPNEptrQJl3ees+Si0lotrZyNOjZYBfw78c7eHZtz /yoS5u8qLe956Hayc+5PSIUUnDhtcBoUGEs5dcCQdha2Ug3fbeHZh7YvmmaEpMgsAbga 72MMS1O9ngpcX3uIK55t/FdZxI/z5FwlKZpByvil87Frc0eSBIxoE4cwWHhdjo8DOMUr G18UdcqoxknVEh1MNRbKbj5WOE/NGi1A+Ku+WkxEQ5XXnQGUkYBwl4BAMlQ+XSaxLXvF odkyD7BHLSizVe37N1BPpMzSsPvAhACeL0j9BCiqRlM87SHNlML0ExZ3WD/ExIvUpuQn 501g== 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=D0DwgEYL9FDtap/ma2duhrafHKDI2k2H7Kdd1DgpGcA=; b=yT7NkdecLZYNn4uDWkcUhNbr0F+k6ht8qpiW88EAqkqv/FUe7l9wMntUKlT9A7j91y 7y4ckwBsspNqZ9EObfaxYFdJBAWETt59qeIYrwGH1MPrgzxR9RMdVA4Hj4qnjsBEhjG7 pWtSze5jujibPVWdhcuXUZ5S6vc9ickiFYLpByeYp1aG7y+TRsqbsD//ZDEk2bTHMGJ0 VJM3qzbh9CF2D6ISoiBZBd25WPglOcq0WmOT5A4NYTTwLdx1veLowroiiHccC4sn3Frr 27FB32agpjzrVL5H27N0gOoV4oAWxnYUjlbzR+GcdLUHjzLfqIpgtVKLUo3xMzAg6BoW 7t3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lj0TKTl0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id d20-20020aa78e54000000b004faba391fb6si454448pfr.50.2022.03.21.17.20.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 17:20:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=lj0TKTl0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 1E7C16E562; Mon, 21 Mar 2022 16:50:06 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233586AbiCUXu5 (ORCPT + 99 others); Mon, 21 Mar 2022 19:50:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233551AbiCUXuq (ORCPT ); Mon, 21 Mar 2022 19:50:46 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D56611D97DE for ; Mon, 21 Mar 2022 16:49:01 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id j10-20020a63b60a000000b003814a68f3dcso7898769pgf.16 for ; Mon, 21 Mar 2022 16:49:01 -0700 (PDT) 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=D0DwgEYL9FDtap/ma2duhrafHKDI2k2H7Kdd1DgpGcA=; b=lj0TKTl03MICUUzJ93f3Ojew9olRP8p1BnY2sOeXVZvCD3HdKCY2N2c/GtmUUS4x6Y tKRfNV/MyL9hhjjAX36tJK/CBufA5OeqOWcw2LL3BaO1XCj7ugzVz1rgpNOxZmBKowQT OYeDbEcb2Soy4afqkwYFKJRAlATr3us1x7c+FO7L1JAZRsB/KfFdD2syHI8ks9u16wW7 0noRD+Rezs5xWBJkbs+rDmmP3104i3w+/cnf9n9CREvHlelrYrx9ufjyTyjbFf7o8gSB b4gVNme3LazD8qNoLaR714YqALarCheIKm/7DilxLtR+wHUq8hXhjLCaq2JC0nYBLxvx qWwA== 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=D0DwgEYL9FDtap/ma2duhrafHKDI2k2H7Kdd1DgpGcA=; b=0EbSMObTa1yniYs++Dm/MGAiVkC/SEAe9nlTxbkrO71MWanEOlNIdJB8VR3XocpaTN 8uhDzd4TuzkWQG9Ij2YyGNMJs50TFtNzy0V7bXuVjbzpzoUHZ9NDaWzANKSv+xsAOByz OMW0pLw2vMnGLkWZIEZPHXYZrtL66w7R8TW6XNhrocH5XR2/UOHg6k9UhF63mTaA+Lov E6NgwYT1lX0PqW5FOB1BpQLttm+jmHlvYd9mXhOe3cKUtZwpa1yqzvm8geGHlFZeMM8B DVw7+4W9uWdOKor/IZVfiSagWsGTeryFpZrKSs86/cZgncEnkJYae5N05eNpuk7ALesK T8WQ== X-Gm-Message-State: AOAM531uQoi3cOu4uED8vAUQTcmwgz2JeqWVoehDCpXSmz0si0Wpkeph 79JbDVVB2vSxledWPoG1pmucUwaN8OYUblvmGvyCZrpKv842upxOhkjFeHNlmc7s924TaHAmoJT a/oZUA722wV++iL80AnzrSgxnh61IWi6boJlKMVsxWeI4F0igsalHbgPkQGCaSq5gXf0JCB8m X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:b76a:f152:cb5e:5cd2]) (user=bgardon job=sendgmr) by 2002:a62:1ad3:0:b0:4fa:686f:9938 with SMTP id a202-20020a621ad3000000b004fa686f9938mr21701293pfa.6.1647906536223; Mon, 21 Mar 2022 16:48:56 -0700 (PDT) Date: Mon, 21 Mar 2022 16:48:35 -0700 In-Reply-To: <20220321234844.1543161-1-bgardon@google.com> Message-Id: <20220321234844.1543161-3-bgardon@google.com> Mime-Version: 1.0 References: <20220321234844.1543161-1-bgardon@google.com> X-Mailer: git-send-email 2.35.1.894.gb6a874cedc-goog Subject: [PATCH v2 02/11] KVM: selftests: 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 976aaaba8769..4783fd1cd4cf 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -401,6 +401,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 11a692cf4570..f87df68b150d 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -2562,3 +2562,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.894.gb6a874cedc-goog