From nobody Sat May 11 10:13:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1665146459; cv=none; d=zohomail.com; s=zohoarc; b=KdpddLRqzJFajlzeijZSwdYJKdo1qCA9rXAPjbir04CMBhyRk9uoIf/T9WHiWQMCVL+wjaUTszTkAmo9Ew5+SPz2fmxU244rCFz1Nf1MTElLAn/BpMqDA0rPi/R3TdyNwziT3209JbuXLPZqlSQZjtdI5QaOQpdUnSNYiqfoAME= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1665146459; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=l7DXPQ0Ol36lIEkiwQv5EhGYaewpw0fwd3bSvTaydbA=; b=c81XZIJuKa1hvKQdMi7bt2rqVm7yob0s//HGpU5MDnWEPtkSgW/s7Pc9NXk6rm65FTGKk0T6eTSNlzJVIDrActh9UV6D5jcU7JVSd8JhglbX9tTkXtsYm+J/PZOLyGIPW8GZz+G4okAWWUMw6TWjwH/4pR+74dSDX27jqNU+n1o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1665146458929687.0423906480786; Fri, 7 Oct 2022 05:40:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.417960.662691 (Exim 4.92) (envelope-from ) id 1ogmeR-0001c9-Iz; Fri, 07 Oct 2022 12:40:27 +0000 Received: by outflank-mailman (output) from mailman id 417960.662691; Fri, 07 Oct 2022 12:40:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ogmeR-0001c2-Fx; Fri, 07 Oct 2022 12:40:27 +0000 Received: by outflank-mailman (input) for mailman id 417960; Fri, 07 Oct 2022 12:40:26 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ogmeQ-0001bd-0U for xen-devel@lists.xenproject.org; Fri, 07 Oct 2022 12:40:26 +0000 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [2a00:1450:4864:20::433]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 3690c7cf-463d-11ed-964a-05401a9f4f97; Fri, 07 Oct 2022 14:40:24 +0200 (CEST) Received: by mail-wr1-x433.google.com with SMTP id bu30so7104905wrb.8 for ; Fri, 07 Oct 2022 05:40:24 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:19f:35f0:dde5:d55a:20f5:7ef5]) by smtp.gmail.com with ESMTPSA id o19-20020a05600c511300b003b95ed78275sm2237208wms.20.2022.10.07.05.40.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Oct 2022 05:40:23 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 3690c7cf-463d-11ed-964a-05401a9f4f97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=l7DXPQ0Ol36lIEkiwQv5EhGYaewpw0fwd3bSvTaydbA=; b=fV/GOpuAgJXrH4iQ3IWDCQF8tX2Ucrof///Y8UO3GkK93NPM5Kiy9AJm7jw1ZAFYIT b2lDR0xdwJRenO/2tWSXQI3b/vyoNEXxF3aSZBW6VJghAGWLJOXUY35VVM5W03mYhtAc LIA1le30btJG9jklZGvwVELdQlErgX7UXDnncYpHVQnOeVFTCDC7dxe4+Se1/SEcYeqo +w2F0nIgWjhFRJtFXbvFkH4lz0wM4GMSNx7kxqgQoGo4JwMZU1COmN0qAFEV7dcMqfKL aFs/TTJjopK1jWQy9BdoCaDLD/X0jq1xRuGKXMgMbVkKcWBBUGvQMtazXQCK2rrZnwON LqoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=l7DXPQ0Ol36lIEkiwQv5EhGYaewpw0fwd3bSvTaydbA=; b=rNPLJXGHkWItBmemLSlcYKBFFIzy/FXQOVgPNQshkNXdqbWQynSQgQycIqBxBJ+IqJ wDIphhJkvo1doJraav23PppuI2ghKG3z2M+Y5HM+6UKggl6leG+F/uMqOAy8Ro3yEz6c s7jGKj8T+66+DWrHCRjqwwkawR2dWG6QPFwU+q4l4iV/urOkW9ePEG3vdKGrvkkfQEEA 6efGymF0nbJU9lF0IdPlWRJzNf9pcdVgvFdK78oHlrhQYFw6XHlaXmk+AXaY+I5e8pKz lmYShgEVNeu6yVCAExsvE/IBnEBB+TzC9XBQXUcdP+Fdg1hVKjmlnxNFXM7/BoyrhOD5 xxlw== X-Gm-Message-State: ACrzQf3KzuQhLxBYxvAvE5slob2r69UEwRp2lH6zVx7gQPACOHNQ880r OXkx8d9Ej3UtamTaAKrKP4+goU8/7p2ZDQ== X-Google-Smtp-Source: AMsMyM6/tKYPAIgdd8Ezu6OCrYePPn73oQ9O/uBjnx8rjsgZ23ETzMfJagM+j/3+1lXwi1GO5FlGMA== X-Received: by 2002:a5d:5a9d:0:b0:225:2783:d6f1 with SMTP id bp29-20020a5d5a9d000000b002252783d6f1mr3167811wrb.385.1665146423511; Fri, 07 Oct 2022 05:40:23 -0700 (PDT) From: Matias Ezequiel Vara Larsen X-Google-Original-From: Matias Ezequiel Vara Larsen To: xen-devel@lists.xenproject.org Cc: Matias Ezequiel Vara Larsen , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Wei Liu , George Dunlap , Julien Grall , Stefano Stabellini , Dario Faggioli Subject: [RFC PATCH v2 1/2] xen/memory : Add a stats_table resource type Date: Fri, 7 Oct 2022 14:39:53 +0200 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1665146460058100001 Content-Type: text/plain; charset="utf-8" This commit proposes a new mechanism to query the RUNSTATE_running counter = for a given vcpu from a dom0 userspace application. This commit proposes to exp= ose that counter by using the acquire_resource interface. The current mechanism relies on the XEN_DOMCTL_getvcpuinfo and holds a single global domctl_lock = for the entire hypercall; and iterate over every vcpu in the system for every update thus impacting operations that share that lock. This commit proposes to expose vcpu RUNSTATE_running via the xenforeignmemory interface thus preventing to issue the hypercall and holdi= ng the lock. For that purpose, a new resource type named stats_table is added.= The first frame of this resource stores per-vcpu counters. The frame has one en= try of type struct vcpu_stats per vcpu. The allocation of this frame only happe= ns if the resource is requested. The frame is released after the domain is destroyed. Note that the updating of this counter is in a hot path, thus, in this comm= it, copying only happens if it is specifically required. Note that the exposed structure is extensible in two ways. First, the struc= ture vcpu_stats can be extended with new per-vcpu counters while it fits in a fr= ame. Second, new frames can be added in case new counters are required. Signed-off-by: Matias Ezequiel Vara Larsen --- Changes in v2: - rework to ensure that guest reads a coherent value by using a version number in the vcpu_stats structure - add version to the vcpu_stats structure Changes in v1: - rework the allocation and releasing of the frames - use the zero frame for per-vcpu counters that are listed as an array - allocate vcpu stats frames only when the resource is requested - rewrite commit message - add the vcpu_stats structure to keep per-vcpu counters - add the shared_vcpustatspage to keep an array of per-vcpu counters for a given domain - declare the structures in a public header=20 - define the vcpustats_page in the domain structure --- xen/arch/x86/hvm/hvm.c | 2 + xen/common/memory.c | 94 +++++++++++++++++++++++++++++++++++++ xen/common/sched/core.c | 16 +++++++ xen/include/public/memory.h | 3 ++ xen/include/public/vcpu.h | 16 +++++++ xen/include/xen/mm.h | 2 + xen/include/xen/sched.h | 5 ++ 7 files changed, 138 insertions(+) diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index ddd001a6ad..1ef6cb5ff0 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -741,6 +741,8 @@ void hvm_domain_relinquish_resources(struct domain *d) =20 ioreq_server_destroy_all(d); =20 + stats_free_vcpu_mfn(d); + msixtbl_pt_cleanup(d); =20 /* Stop all asynchronous timer actions. */ diff --git a/xen/common/memory.c b/xen/common/memory.c index 297b98a562..749486d5d4 100644 --- a/xen/common/memory.c +++ b/xen/common/memory.c @@ -1078,6 +1078,12 @@ unsigned int ioreq_server_max_frames(const struct do= main *d) return nr; } =20 +unsigned int stats_table_max_frames(const struct domain *d) +{ + /* One frame per 512 vcpus. */ + return 1; +} + /* * Return 0 on any kind of error. Caller converts to -EINVAL. * @@ -1099,6 +1105,9 @@ static unsigned int resource_max_frames(const struct = domain *d, case XENMEM_resource_vmtrace_buf: return d->vmtrace_size >> PAGE_SHIFT; =20 + case XENMEM_resource_stats_table: + return stats_table_max_frames(d); + default: return -EOPNOTSUPP; } @@ -1162,6 +1171,88 @@ static int acquire_vmtrace_buf( return nr_frames; } =20 +void stats_free_vcpu_mfn(struct domain * d) +{ + struct page_info *pg =3D d->vcpustats_page.pg; + + if ( !pg ) + return; + + d->vcpustats_page.pg =3D NULL; + + if ( d->vcpustats_page.va ) + unmap_domain_page_global(d->vcpustats_page.va); + + d->vcpustats_page.va =3D NULL; + + put_page_alloc_ref(pg); + put_page_and_type(pg); +} + +static int stats_vcpu_alloc_mfn(struct domain *d) +{ + struct page_info *pg; + + pg =3D alloc_domheap_page(d, MEMF_no_refcount); + + if ( !pg ) + return -ENOMEM; + + if ( !get_page_and_type(pg, d, PGT_writable_page) ) { + put_page_alloc_ref(pg); + return -ENODATA; + } + + d->vcpustats_page.va =3D __map_domain_page_global(pg); + if ( !d->vcpustats_page.va ) + goto fail; + + d->vcpustats_page.pg =3D pg; + clear_page(d->vcpustats_page.va); + return 1; + +fail: + put_page_alloc_ref(pg); + put_page_and_type(pg); + + return -ENOMEM; +} + +static int acquire_stats_table(struct domain *d, + unsigned int id, + unsigned int frame, + unsigned int nr_frames, + xen_pfn_t mfn_list[]) +{ + mfn_t mfn; + int rc; + unsigned int i; + + if ( !d ) + return -ENOENT; + + for ( i =3D 0; i < nr_frames; i++ ) + { + switch ( i ) + { + case XENMEM_resource_stats_frame_vcpustats: + if ( !d->vcpustats_page.pg ) { + rc =3D stats_vcpu_alloc_mfn(d); + if ( rc < 1 ) + return rc; + } + mfn =3D page_to_mfn(d->vcpustats_page.pg); + mfn_list[i] =3D mfn_x(mfn); + break; + + default: + return -EINVAL; + } + } + + return nr_frames; +} + /* * Returns -errno on error, or positive in the range [1, nr_frames] on * success. Returning less than nr_frames contitutes a request for a @@ -1182,6 +1273,9 @@ static int _acquire_resource( case XENMEM_resource_vmtrace_buf: return acquire_vmtrace_buf(d, id, frame, nr_frames, mfn_list); =20 + case XENMEM_resource_stats_table: + return acquire_stats_table(d, id, frame, nr_frames, mfn_list); + default: return -EOPNOTSUPP; } diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c index 8f4b1ca10d..3543a531a1 100644 --- a/xen/common/sched/core.c +++ b/xen/common/sched/core.c @@ -264,6 +264,8 @@ static inline void vcpu_runstate_change( { s_time_t delta; struct sched_unit *unit =3D v->sched_unit; + shared_vcpustatspage_t * vcpustats_va; + struct domain *d =3D v->domain; =20 ASSERT(spin_is_locked(get_sched_res(v->processor)->schedule_lock)); if ( v->runstate.state =3D=3D new_state ) @@ -287,6 +289,20 @@ static inline void vcpu_runstate_change( } =20 v->runstate.state =3D new_state; + + vcpustats_va =3D (shared_vcpustatspage_t*)d->vcpustats_page.va; + if ( vcpustats_va ) + { + vcpustats_va->vcpu_info[v->vcpu_id].version =3D + version_update_begin(vcpustats_va->vcpu_info[v->vcpu_id].version); + smp_wmb(); + memcpy(&vcpustats_va->vcpu_info[v->vcpu_id].runstate_running_time, + &v->runstate.time[RUNSTATE_running], + sizeof(v->runstate.time[RUNSTATE_running])); + smp_wmb(); + vcpustats_va->vcpu_info[v->vcpu_id].version =3D + version_update_end(vcpustats_va->vcpu_info[v->vcpu_id].version= ); + } } =20 void sched_guest_idle(void (*idle) (void), unsigned int cpu) diff --git a/xen/include/public/memory.h b/xen/include/public/memory.h index 50e73eef98..e1a10b8b97 100644 --- a/xen/include/public/memory.h +++ b/xen/include/public/memory.h @@ -626,6 +626,7 @@ struct xen_mem_acquire_resource { #define XENMEM_resource_ioreq_server 0 #define XENMEM_resource_grant_table 1 #define XENMEM_resource_vmtrace_buf 2 +#define XENMEM_resource_stats_table 3 =20 /* * IN - a type-specific resource identifier, which must be zero @@ -683,6 +684,8 @@ struct xen_mem_acquire_resource { typedef struct xen_mem_acquire_resource xen_mem_acquire_resource_t; DEFINE_XEN_GUEST_HANDLE(xen_mem_acquire_resource_t); =20 +#define XENMEM_resource_stats_frame_vcpustats 0 + /* * XENMEM_get_vnumainfo used by guest to get * vNUMA topology from hypervisor. diff --git a/xen/include/public/vcpu.h b/xen/include/public/vcpu.h index 3623af932f..5c1812dfd2 100644 --- a/xen/include/public/vcpu.h +++ b/xen/include/public/vcpu.h @@ -235,6 +235,22 @@ struct vcpu_register_time_memory_area { typedef struct vcpu_register_time_memory_area vcpu_register_time_memory_ar= ea_t; DEFINE_XEN_GUEST_HANDLE(vcpu_register_time_memory_area_t); =20 +struct vcpu_stats{ + /* If the least-significant bit of the version number is set then an u= pdate + * is in progress and the guest must wait to read a consistent set of = values + * This mechanism is similar to Linux's seqlock. + */ + uint32_t version; + uint32_t pad0; + uint64_t runstate_running_time; +}; + +struct shared_vcpustatspage { + struct vcpu_stats vcpu_info[1]; +}; + +typedef struct shared_vcpustatspage shared_vcpustatspage_t; + #endif /* __XEN_PUBLIC_VCPU_H__ */ =20 /* diff --git a/xen/include/xen/mm.h b/xen/include/xen/mm.h index 667f9dac83..d1ca8b9aa8 100644 --- a/xen/include/xen/mm.h +++ b/xen/include/xen/mm.h @@ -134,6 +134,8 @@ int assign_pages( /* Dump info to serial console */ void arch_dump_shared_mem_info(void); =20 +void stats_free_vcpu_mfn(struct domain * d); + /* * Extra fault info types which are used to further describe * the source of an access violation. diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 5485d08afb..d9551ce35f 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -577,6 +577,11 @@ struct domain struct ioreq_server *server[MAX_NR_IOREQ_SERVERS]; } ioreq_server; #endif + /* Page that hosts vcpu stats */ + struct { + struct page_info *pg; + void *va; + } vcpustats_page; }; =20 static inline struct page_list_head *page_to_list( --=20 2.25.1 From nobody Sat May 11 10:13:45 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1665146450; cv=none; d=zohomail.com; s=zohoarc; b=Cxh7l8vxAvEiyDZ7HZDwcoGjwCK56+qOjRy1mni2MEZxXCgl0fV25JLBctNiDosTbbK9jFyXJzyKi1d3zZ0kyjzFi2DBOX39UUQg9iybla8yaZwBXWgahjRhlHSy1DvCopDq0tM07MgNLsUdaOJbWnRFPs0Xl3Pz7ag9pJ/Xi0A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1665146450; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kdQ1ywQBPJum0BhihWVir9mOqof1BeIfrkRzabeE3Hc=; b=C9iD7Hl7YMXiNrqXyVVLMt7CekxvlHRw9hSB8W7lKp6jswUarfgJ56DNZOKjgtLN9lWGxQHUYzbi8kPtPd5kVP30Xeanwiti7xkNXkaVkIE00cNEOYTDGMsesNLw/E+C3VxXxfnbVy6FrDQWz5OvZVeKCoUgp31/bx3o7OhbfGI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1665146450883272.8933604012399; Fri, 7 Oct 2022 05:40:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.417961.662702 (Exim 4.92) (envelope-from ) id 1ogmeS-0001sH-RV; Fri, 07 Oct 2022 12:40:28 +0000 Received: by outflank-mailman (output) from mailman id 417961.662702; Fri, 07 Oct 2022 12:40:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ogmeS-0001s2-OM; Fri, 07 Oct 2022 12:40:28 +0000 Received: by outflank-mailman (input) for mailman id 417961; Fri, 07 Oct 2022 12:40:27 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ogmeR-0001bd-6n for xen-devel@lists.xenproject.org; Fri, 07 Oct 2022 12:40:27 +0000 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [2a00:1450:4864:20::42b]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 37b9ad0a-463d-11ed-964a-05401a9f4f97; Fri, 07 Oct 2022 14:40:26 +0200 (CEST) Received: by mail-wr1-x42b.google.com with SMTP id bk15so7078385wrb.13 for ; Fri, 07 Oct 2022 05:40:26 -0700 (PDT) Received: from localhost.localdomain ([2a01:e0a:19f:35f0:dde5:d55a:20f5:7ef5]) by smtp.gmail.com with ESMTPSA id o19-20020a05600c511300b003b95ed78275sm2237208wms.20.2022.10.07.05.40.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Oct 2022 05:40:25 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 37b9ad0a-463d-11ed-964a-05401a9f4f97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kdQ1ywQBPJum0BhihWVir9mOqof1BeIfrkRzabeE3Hc=; b=E0ADwO8//FyPWDkI1qNXQzo+uMNfFFvYXI9dyWDDOHF7UAhnVcYd4b7bcSQv+JzMfW 2LGrMjIdEJEWedb3EaxCpq98+jzle/oiUHg2nw4vbyrLGWiSuXcgUKVDVcNh/jBZnlno 9TzUhcX+zZtA3YeRCfVY70mIWtTUjzvRSgMmzbxvZmr8RvDDyTRAG+zMEVPrDxflH4Py uNc93Mt+qho0vWhGM5gOu1exoWkjFFAwAVAk2v7fqoRHUXxtd+K/saxwkXIiVM7WB9xR 9RQSivs6MxVqouYl74zmHRSn6JUpHsv/wMusIqp+NhKZb9Cs/+Nsw/19WouDQFTgF7Zd xylA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kdQ1ywQBPJum0BhihWVir9mOqof1BeIfrkRzabeE3Hc=; b=TVvfmt3wLmPxHTSnRJgDZ18sthZSrfy1LmA7jjcJyX3TWV+vm092sdmXoKdJrDS3vy PYY3ptVwIbFzsRxnrYb2P366R3c42pilhjdb1YOfPxOMAUAPpSAwF4UX+bc52SX+azq3 /nTBs24s3Exw1u+sMqg1+o/bpeVzYFp1PCrpmACXDv07gEP6cSmjwR30EGF9Gzg9Px37 NJi2IoOi+fDVjklVhui1We3GGx2G7BiVJRg4C2+ojvPo+BC3K9/jQpfxSSaVsk3WaVSj kkjuINtbenj19YWm+21kQIb3hnvHaI8Hv+rsF2/Y/hq8IeLCe+YOLN0J/tyj8FDm6CoR qfyg== X-Gm-Message-State: ACrzQf3pH2hTIeqptWPHV/NMSh3DLrMrU3VVFwdhLBXAQbS9E5P91CTp eQduOJgKWwui+xQocm6b+PTQlxHcl7+gSQ== X-Google-Smtp-Source: AMsMyM6OCUBQH3Pu7RIknEpoknCMQbVu/GUlRpRvhhk9hW2I+5N2SYNC5lpnXJTbba7O5yVG2vWJ5A== X-Received: by 2002:a5d:674f:0:b0:22e:3d59:cf0f with SMTP id l15-20020a5d674f000000b0022e3d59cf0fmr3172589wrw.304.1665146425560; Fri, 07 Oct 2022 05:40:25 -0700 (PDT) From: Matias Ezequiel Vara Larsen X-Google-Original-From: Matias Ezequiel Vara Larsen To: xen-devel@lists.xenproject.org Cc: Matias Ezequiel Vara Larsen , Wei Liu , Anthony PERARD Subject: [RFC PATCH v2 2/2] tools/misc: Add xen-vcpus-stats tool Date: Fri, 7 Oct 2022 14:39:54 +0200 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1665146452229100001 Content-Type: text/plain; charset="utf-8" Add a demonstration tool that uses the stats_table resource to query vcpus' RUNSTATE_running counter for a DomU. Signed-off-by: Matias Ezequiel Vara Larsen --- Changes in v2: - use period instead of frec - rely on version to ensure reading is coherent=20 Changes in v1: - change the name of the tool to xen-vcpus-stats - set command line parameters in the same order that are passed - remove header libs.h - build by default - remove errno, strerrno, "\n", and identation - use errx when errno is not needed - address better the number of pages requested and error msgs - use the shared_vcpustatspage_t structure - use the correct frame id when requesting the resource --- tools/misc/Makefile | 6 +++ tools/misc/xen-vcpus-stats.c | 87 ++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tools/misc/xen-vcpus-stats.c diff --git a/tools/misc/Makefile b/tools/misc/Makefile index 2b683819d4..837e4b50da 100644 --- a/tools/misc/Makefile +++ b/tools/misc/Makefile @@ -49,6 +49,7 @@ TARGETS_COPY +=3D xenpvnetboot =20 # Everything which needs to be built TARGETS_BUILD :=3D $(filter-out $(TARGETS_COPY),$(TARGETS_ALL)) +TARGETS_BUILD +=3D xen-vcpus-stats =20 # ... including build-only targets TARGETS_BUILD-$(CONFIG_X86) +=3D xen-vmtrace @@ -135,4 +136,9 @@ xencov: xencov.o xen-ucode: xen-ucode.o $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS) =20 +xen-vcpus-stats.o: CFLAGS +=3D $(CFLAGS_libxenforeginmemory) + +xen-vcpus-stats: xen-vcpus-stats.o + $(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenforeignmemo= ry) $(APPEND_LDFLAGS) + -include $(DEPS_INCLUDE) diff --git a/tools/misc/xen-vcpus-stats.c b/tools/misc/xen-vcpus-stats.c new file mode 100644 index 0000000000..29d0efb124 --- /dev/null +++ b/tools/misc/xen-vcpus-stats.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define rmb() asm volatile("lfence":::"memory") + +static sig_atomic_t interrupted; +static void close_handler(int signum) +{ + interrupted =3D 1; +} + +int main(int argc, char **argv) +{ + xenforeignmemory_handle *fh; + xenforeignmemory_resource_handle *res; + size_t size; + int rc, domid, period, vcpu; + shared_vcpustatspage_t * info; + struct sigaction act; + uint32_t version; + uint64_t value; + + if (argc !=3D 4 ) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + domid =3D atoi(argv[1]); + vcpu =3D atoi(argv[2]); + period =3D atoi(argv[3]); + + act.sa_handler =3D close_handler; + act.sa_flags =3D 0; + sigemptyset(&act.sa_mask); + sigaction(SIGHUP, &act, NULL); + sigaction(SIGTERM, &act, NULL); + sigaction(SIGINT, &act, NULL); + sigaction(SIGALRM, &act, NULL); + + fh =3D xenforeignmemory_open(NULL, 0); + + if ( !fh ) + err(1, "xenforeignmemory_open"); + + rc =3D xenforeignmemory_resource_size( + fh, domid, XENMEM_resource_stats_table, + 0, &size); + + if ( rc ) + err(1, "Fail: Get size"); + + res =3D xenforeignmemory_map_resource( + fh, domid, XENMEM_resource_stats_table, + 0, XENMEM_resource_stats_frame_vcpustats, size >> XC_PAGE_SHIFT, + (void **)&info, PROT_READ, 0); + + if ( !res ) + err(1, "Fail: Map"); + + while ( !interrupted ) { + sleep(period); + do { + version =3D info->vcpu_info[vcpu].version; + rmb(); + value =3D info->vcpu_info[vcpu].runstate_running_time; + rmb(); + } while ((info->vcpu_info[vcpu].version & 1) || + (version !=3D info->vcpu_info[vcpu].version)); + printf("running_vcpu_time[%d]: %ld\n", vcpu, value); + } + + rc =3D xenforeignmemory_unmap_resource(fh, res); + if ( rc ) + err(1, "Fail: Unmap"); + + return 0; +} --=20 2.25.1