From nobody Fri May 16 01:08:48 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1502371938255677.8838432663754; Thu, 10 Aug 2017 06:32:18 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 51C72C04A5A3; Thu, 10 Aug 2017 13:32:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2AEE9B1ED1; Thu, 10 Aug 2017 13:32:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id CF6611805974; Thu, 10 Aug 2017 13:32:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v7ADVt8d022605 for ; Thu, 10 Aug 2017 09:31:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0D3F793432; Thu, 10 Aug 2017 13:31:55 +0000 (UTC) Received: from caroline.localdomain (unknown [10.43.2.67]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8543C9568B for ; Thu, 10 Aug 2017 13:31:51 +0000 (UTC) Received: from caroline.brq.redhat.com (caroline.brq.redhat.com [127.0.0.1]) by caroline.localdomain (Postfix) with ESMTP id 4595A120463 for ; Thu, 10 Aug 2017 15:31:50 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 51C72C04A5A3 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com From: Martin Kletzander To: libvir-list@redhat.com Date: Thu, 10 Aug 2017 15:31:47 +0200 Message-Id: <4f64b5cc2b5af3a96faf500f6c51d83b6d9da6e8.1502368247.git.mkletzan@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/5] Move resctrl-related code from conf/capabilities to util/virresctrl X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 10 Aug 2017 13:32:17 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" It doesn't access anything from conf/ and ti will be needed to use from other util/ places. This split makes the separation clearer. Signed-off-by: Martin Kletzander --- include/libvirt/virterror.h | 1 + src/Makefile.am | 1 + src/conf/capabilities.c | 138 +++++---------------------------------- src/conf/capabilities.h | 26 +------- src/libvirt_private.syms | 7 ++ src/util/virerror.c | 1 + src/util/virresctrl.c | 153 ++++++++++++++++++++++++++++++++++++++++= ++++ src/util/virresctrl.h | 63 ++++++++++++++++++ 8 files changed, 245 insertions(+), 145 deletions(-) create mode 100644 src/util/virresctrl.c create mode 100644 src/util/virresctrl.h diff --git a/include/libvirt/virterror.h b/include/libvirt/virterror.h index 2efee8f0c086..3dd2d0822d6d 100644 --- a/include/libvirt/virterror.h +++ b/include/libvirt/virterror.h @@ -132,6 +132,7 @@ typedef enum { =20 VIR_FROM_PERF =3D 65, /* Error from perf */ VIR_FROM_LIBSSH =3D 66, /* Error from libssh connection transpor= t */ + VIR_FROM_RESCTRL =3D 67, /* Error from resource control */ =20 # ifdef VIR_ENUM_SENTINELS VIR_ERR_DOMAIN_LAST diff --git a/src/Makefile.am b/src/Makefile.am index b8e875482969..c3c7a8f04d36 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -167,6 +167,7 @@ UTIL_SOURCES =3D \ util/virprocess.c util/virprocess.h \ util/virqemu.c util/virqemu.h \ util/virrandom.h util/virrandom.c \ + util/virresctrl.h util/virresctrl.c \ util/virrotatingfile.h util/virrotatingfile.c \ util/virscsi.c util/virscsi.h \ util/virscsihost.c util/virscsihost.h \ diff --git a/src/conf/capabilities.c b/src/conf/capabilities.c index e851539bd677..18849adfcaec 100644 --- a/src/conf/capabilities.c +++ b/src/conf/capabilities.c @@ -31,8 +31,6 @@ #include =20 #include "capabilities.h" -#include "c-ctype.h" -#include "count-one-bits.h" #include "cpu_conf.h" #include "domain_conf.h" #include "physmem.h" @@ -52,7 +50,6 @@ #define VIR_FROM_THIS VIR_FROM_CAPABILITIES =20 #define SYSFS_SYSTEM_PATH "/sys/devices/system" -#define SYSFS_RESCTRL_PATH "/sys/fs/resctrl" =20 VIR_LOG_INIT("conf.capabilities") =20 @@ -1539,12 +1536,6 @@ VIR_ENUM_IMPL(virCacheKernel, VIR_CACHE_TYPE_LAST, "Instruction", "Data") =20 -/* Our naming for cache types and scopes */ -VIR_ENUM_IMPL(virCache, VIR_CACHE_TYPE_LAST, - "both", - "code", - "data") - bool virCapsHostCacheBankEquals(virCapsHostCacheBankPtr a, virCapsHostCacheBankPtr b) @@ -1571,111 +1562,6 @@ virCapsHostCacheBankFree(virCapsHostCacheBankPtr pt= r) VIR_FREE(ptr); } =20 -/* - * This function tests which TYPE of cache control is supported - * Return values are: - * -1: not supported - * 0: CAT - * 1: CDP - */ -static int -virCapabilitiesGetCacheControlType(virCapsHostCacheBankPtr bank) -{ - int ret =3D -1; - char *path =3D NULL; - - if (virAsprintf(&path, - SYSFS_RESCTRL_PATH "/info/L%u", - bank->level) < 0) - return -1; - - if (virFileExists(path)) { - ret =3D 0; - } else { - VIR_FREE(path); - /* - * If CDP is enabled, there will be both CODE and DATA, but it's e= nough - * to check one of those only. - */ - if (virAsprintf(&path, - SYSFS_RESCTRL_PATH "/info/L%uCODE", - bank->level) < 0) - return -1; - if (virFileExists(path)) - ret =3D 1; - } - - VIR_FREE(path); - return ret; -} - -static int -virCapabilitiesGetCacheControl(virCapsHostCacheBankPtr bank, - virCacheType scope) -{ - int ret =3D -1; - char *tmp =3D NULL; - char *path =3D NULL; - char *cbm_mask =3D NULL; - char *type_upper =3D NULL; - unsigned int bits =3D 0; - unsigned int min_cbm_bits =3D 0; - virCapsHostCacheControlPtr control; - - if (VIR_ALLOC(control) < 0) - goto cleanup; - - if (scope !=3D VIR_CACHE_TYPE_BOTH && - virStringToUpper(&type_upper, virCacheTypeToString(scope)) < 0) - goto cleanup; - - if (virFileReadValueUint(&control->max_allocation, - SYSFS_RESCTRL_PATH "/info/L%u%s/num_closids", - bank->level, - type_upper ? type_upper : "") < 0) - goto cleanup; - - if (virFileReadValueString(&cbm_mask, - SYSFS_RESCTRL_PATH - "/info/L%u%s/cbm_mask", - bank->level, - type_upper ? type_upper: "") < 0) - goto cleanup; - - if (virFileReadValueUint(&min_cbm_bits, - SYSFS_RESCTRL_PATH "/info/L%u%s/min_cbm_bits", - bank->level, - type_upper ? type_upper : "") < 0) - goto cleanup; - - virStringTrimOptionalNewline(cbm_mask); - - for (tmp =3D cbm_mask; *tmp !=3D '\0'; tmp++) { - if (c_isxdigit(*tmp)) - bits +=3D count_one_bits(virHexToBin(*tmp)); - } - - control->granularity =3D bank->size / bits; - if (min_cbm_bits !=3D 1) - control->min =3D min_cbm_bits * control->granularity; - - control->scope =3D scope; - - if (VIR_APPEND_ELEMENT(bank->controls, - bank->ncontrols, - control) < 0) - goto cleanup; - - ret =3D 0; - - cleanup: - VIR_FREE(path); - VIR_FREE(cbm_mask); - VIR_FREE(type_upper); - VIR_FREE(control); - return ret; -} - int virCapabilitiesInitCaches(virCapsPtr caps) { @@ -1760,17 +1646,27 @@ virCapabilitiesInitCaches(virCapsPtr caps) SYSFS_SYSTEM_PATH, pos, ent->d_name= ) < 0) goto cleanup; =20 - typeret =3D virCapabilitiesGetCacheControlType(bank); + typeret =3D virResctrlGetCacheControlType(bank->level); =20 if (typeret =3D=3D 0) { - if (virCapabilitiesGetCacheControl(bank, - VIR_CACHE_TYPE_BOTH) < = 0) + if (virResctrlGetCacheInfo(bank->level, + bank->size, + VIR_CACHE_TYPE_BOTH, + &bank->controls, + &bank->ncontrols) < 0) goto cleanup; } else if (typeret =3D=3D 1) { - if (virCapabilitiesGetCacheControl(bank, - VIR_CACHE_TYPE_CODE) < = 0 || - virCapabilitiesGetCacheControl(bank, - VIR_CACHE_TYPE_DATA) < = 0) + if (virResctrlGetCacheInfo(bank->level, + bank->size, + VIR_CACHE_TYPE_CODE, + &bank->controls, + &bank->ncontrols) < 0) + goto cleanup; + if (virResctrlGetCacheInfo(bank->level, + bank->size, + VIR_CACHE_TYPE_DATA, + &bank->controls, + &bank->ncontrols) < 0) goto cleanup; } =20 diff --git a/src/conf/capabilities.h b/src/conf/capabilities.h index 39a9bc689279..bd64498d1c7c 100644 --- a/src/conf/capabilities.h +++ b/src/conf/capabilities.h @@ -30,6 +30,7 @@ # include "virarch.h" # include "virmacaddr.h" # include "virobject.h" +# include "virresctrl.h" =20 # include =20 @@ -138,29 +139,6 @@ struct _virCapsHostSecModel { virCapsHostSecModelLabelPtr labels; }; =20 -typedef enum { - VIR_CACHE_TYPE_BOTH, - VIR_CACHE_TYPE_CODE, - VIR_CACHE_TYPE_DATA, - - VIR_CACHE_TYPE_LAST -} virCacheType; - -VIR_ENUM_DECL(virCache); - -typedef struct _virCapsHostCacheControl virCapsHostCacheControl; -typedef virCapsHostCacheControl *virCapsHostCacheControlPtr; -struct _virCapsHostCacheControl { - /* Smallest possible increase of the allocation size in bytes */ - unsigned long long granularity; - /* Minimal allocatable size in bytes (if different from granularity) */ - unsigned long long min; - /* Type of the allocation */ - virCacheType scope; - /* Maximum number of simultaneous allocations */ - unsigned int max_allocation; -}; - typedef struct _virCapsHostCacheBank virCapsHostCacheBank; typedef virCapsHostCacheBank *virCapsHostCacheBankPtr; struct _virCapsHostCacheBank { @@ -170,7 +148,7 @@ struct _virCapsHostCacheBank { virCacheType type; /* Data, Instruction or Unified */ virBitmapPtr cpus; /* All CPUs that share this bank */ size_t ncontrols; - virCapsHostCacheControlPtr *controls; + virResctrlPtr *controls; }; =20 typedef struct _virCapsHost virCapsHost; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 183a9194d766..9de19628d480 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2464,6 +2464,13 @@ virRandomGenerateWWN; virRandomInt; =20 =20 +# util/virresctrl.h +virCacheTypeFromString; +virCacheTypeToString; +virResctrlGetCacheControlType; +virResctrlGetCacheInfo; + + # util/virrotatingfile.h virRotatingFileReaderConsume; virRotatingFileReaderFree; diff --git a/src/util/virerror.c b/src/util/virerror.c index ef17fb5e6e74..a5a2d6ed10f3 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -139,6 +139,7 @@ VIR_ENUM_IMPL(virErrorDomain, VIR_ERR_DOMAIN_LAST, =20 "Perf", /* 65 */ "Libssh transport layer", + "Resource control", ) =20 =20 diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c new file mode 100644 index 000000000000..dfc062c2a0f8 --- /dev/null +++ b/src/util/virresctrl.c @@ -0,0 +1,153 @@ +/* + * virresctrl.c: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "virresctrl.h" + +#include "c-ctype.h" +#include "count-one-bits.h" +#include "viralloc.h" +#include "virfile.h" +#include "virlog.h" +#include "virstring.h" + +#define VIR_FROM_THIS VIR_FROM_RESCTRL + +VIR_LOG_INIT("util.virresctrl") + +#define SYSFS_RESCTRL_PATH "/sys/fs/resctrl" + +/* Resctrl is short for Resource Control. It might be implemented for var= ious + * resources, but at the time of this writing this is only supported for c= ache + * allocation technology (aka CAT). Hence the reson for leaving 'Cache' o= ut of + * all the structure and function names for now (can be added later if nee= ded. + */ + +/* Our naming for cache types and scopes */ +VIR_ENUM_IMPL(virCache, VIR_CACHE_TYPE_LAST, + "both", + "code", + "data") + +int +virResctrlGetCacheInfo(unsigned int level, + unsigned long long size, + virCacheType scope, + virResctrlPtr **controls, + size_t *ncontrols) +{ + int ret =3D -1; + char *tmp =3D NULL; + char *path =3D NULL; + char *cbm_mask =3D NULL; + char *type_upper =3D NULL; + unsigned int bits =3D 0; + unsigned int min_cbm_bits =3D 0; + virResctrlPtr control; + + if (VIR_ALLOC(control) < 0) + goto cleanup; + + if (scope !=3D VIR_CACHE_TYPE_BOTH && + virStringToUpper(&type_upper, virCacheTypeToString(scope)) < 0) + goto cleanup; + + if (virFileReadValueUint(&control->max_allocation, + SYSFS_RESCTRL_PATH "/info/L%u%s/num_closids", + level, + type_upper ? type_upper : "") < 0) + goto cleanup; + + if (virFileReadValueString(&cbm_mask, + SYSFS_RESCTRL_PATH + "/info/L%u%s/cbm_mask", + level, + type_upper ? type_upper: "") < 0) + goto cleanup; + + if (virFileReadValueUint(&min_cbm_bits, + SYSFS_RESCTRL_PATH "/info/L%u%s/min_cbm_bits", + level, + type_upper ? type_upper : "") < 0) + goto cleanup; + + virStringTrimOptionalNewline(cbm_mask); + + for (tmp =3D cbm_mask; *tmp !=3D '\0'; tmp++) { + if (c_isxdigit(*tmp)) + bits +=3D count_one_bits(virHexToBin(*tmp)); + } + + control->granularity =3D size / bits; + if (min_cbm_bits !=3D 1) + control->min =3D min_cbm_bits * control->granularity; + + control->scope =3D scope; + + if (VIR_APPEND_ELEMENT(*controls, *ncontrols, control) < 0) + goto cleanup; + + ret =3D 0; + + cleanup: + VIR_FREE(path); + VIR_FREE(cbm_mask); + VIR_FREE(type_upper); + VIR_FREE(control); + return ret; +} + + +/* + * This function tests which TYPE of cache control is supported + * Return values are: + * -1: not supported + * 0: CAT + * 1: CDP + */ +int +virResctrlGetCacheControlType(unsigned int level) +{ + int ret =3D -1; + char *path =3D NULL; + + if (virAsprintf(&path, + SYSFS_RESCTRL_PATH "/info/L%u", + level) < 0) + return -1; + + if (virFileExists(path)) { + ret =3D 0; + } else { + VIR_FREE(path); + /* + * If CDP is enabled, there will be both CODE and DATA, but it's e= nough + * to check one of those only. + */ + if (virAsprintf(&path, + SYSFS_RESCTRL_PATH "/info/L%uCODE", + level) < 0) + return -1; + if (virFileExists(path)) + ret =3D 1; + } + + VIR_FREE(path); + return ret; +} diff --git a/src/util/virresctrl.h b/src/util/virresctrl.h new file mode 100644 index 000000000000..ee219cbb9261 --- /dev/null +++ b/src/util/virresctrl.h @@ -0,0 +1,63 @@ +/* + * virresctrl.h: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#ifndef __VIR_RESCTRL_H__ +# define __VIR_RESCTRL_H__ + +# include "internal.h" + +# include "virbitmap.h" +# include "virutil.h" + + +typedef enum { + VIR_CACHE_TYPE_BOTH, + VIR_CACHE_TYPE_CODE, + VIR_CACHE_TYPE_DATA, + + VIR_CACHE_TYPE_LAST +} virCacheType; + +VIR_ENUM_DECL(virCache); + + +typedef struct _virResctrl virResctrl; +typedef virResctrl *virResctrlPtr; +struct _virResctrl { + /* Smallest possible increase of the allocation size in bytes */ + unsigned long long granularity; + /* Minimal allocatable size in bytes (if different from granularity) */ + unsigned long long min; + /* Type of the allocation */ + virCacheType scope; + /* Maximum number of simultaneous allocations */ + unsigned int max_allocation; +}; + + +int +virResctrlGetCacheInfo(unsigned int level, + unsigned long long size, + virCacheType scope, + virResctrlPtr **controls, + size_t *ncontrols); + +int +virResctrlGetCacheControlType(unsigned int level); + +#endif /* __VIR_RESCTRL_H__ */ --=20 2.14.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list