From nobody Wed Feb 11 10:11:58 2026 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.zoho.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 1490382342979810.1511602013077; Fri, 24 Mar 2017 12:05:42 -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 BE29E4E05D; Fri, 24 Mar 2017 19:05:42 +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 8EA107F6A7; Fri, 24 Mar 2017 19:05:42 +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 3F3FC18523D3; Fri, 24 Mar 2017 19:05:42 +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 v2OJ0jVV019551 for ; Fri, 24 Mar 2017 15:00:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id BB5567D569; Fri, 24 Mar 2017 19:00:45 +0000 (UTC) Received: from caroline.brq.redhat.com (dhcp129-198.brq.redhat.com [10.34.129.198]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4356419630 for ; Fri, 24 Mar 2017 19:00:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BE29E4E05D Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com BE29E4E05D From: Martin Kletzander To: libvir-list@redhat.com Date: Fri, 24 Mar 2017 20:00:10 +0100 Message-Id: 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 22/23] tests: Add virnumamock 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.38]); Fri, 24 Mar 2017 19:05:43 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" All mocked functions are related to numactl/virNuma and rely only on virsysfs, so the paths they touch can be nicely controlled. And because it is so nicely self-contained NUMA mock, it is named numamock (instead of naming it after the test that will use it first). Signed-off-by: Martin Kletzander --- tests/Makefile.am | 7 ++ tests/virnumamock.c | 191 ++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 198 insertions(+) create mode 100644 tests/virnumamock.c diff --git a/tests/Makefile.am b/tests/Makefile.am index af69a3a843a5..8b78607c81a7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -206,6 +206,7 @@ test_libraries =3D libshunload.la \ virnetdevmock.la \ virrandommock.la \ virhostcpumock.la \ + virnumamock.la \ domaincapsmock.la \ $(NULL) @@ -1120,6 +1121,12 @@ virhostcpumock_la_CFLAGS =3D $(AM_CFLAGS) virhostcpumock_la_LDFLAGS =3D $(MOCKLIBS_LDFLAGS) virhostcpumock_la_LIBADD =3D $(MOCKLIBS_LIBS) +virnumamock_la_SOURCES =3D \ + virnumamock.c +virnumamock_la_CFLAGS =3D $(AM_CFLAGS) +virnumamock_la_LDFLAGS =3D $(MOCKLIBS_LDFLAGS) +virnumamock_la_LIBADD =3D $(MOCKLIBS_LIBS) + if WITH_NSS nsstest_SOURCES =3D \ nsstest.c testutils.h testutils.c diff --git a/tests/virnumamock.c b/tests/virnumamock.c new file mode 100644 index 000000000000..89e420fa4173 --- /dev/null +++ b/tests/virnumamock.c @@ -0,0 +1,191 @@ +/* + * virnumamock.c: Mock some virNuma functions using virsysfs + * + * 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 "internal.h" +#include "virmock.h" +#include "virnuma.h" +#include "virfile.h" +#include "viralloc.h" +#include "virstring.h" +#include "virsysfspriv.h" + +#define VIR_FROM_THIS VIR_FROM_NONE + +static int numa_avail =3D -1; + + +/* + * Poor man's mocked NUMA guesser. We basically check if + * /sys/devices/system/node (where /sys/devices/system can already be mock= ed or + * changed in the tests) exists and cache the result. + */ +bool +virNumaIsAvailable(void) +{ + if (numa_avail < 0) { + char *sysfs_node_path =3D NULL; + + if (virAsprintfQuiet(&sysfs_node_path, "%s/node", virSysfsGetSyste= mPath()) < 0) + return false; + + numa_avail =3D virFileExists(sysfs_node_path); + + VIR_FREE(sysfs_node_path); + } + + /* + * Quite a few more things need to be mocked if NUMA is not available = and + * you are using this file. Do not remove the abort() call below unle= ss you + * make sure all under virCapabilitiesInitNUMAFake() is mocked (and wh= atever + * might have changed since this comment was added. You are welcome. + */ + if (!numa_avail) + abort(); + + return numa_avail; +} + +int +virNumaGetMaxNode(void) +{ + int ret =3D -1; + virBitmapPtr map =3D NULL; + + if (virSysfsGetValueBitmap("node/online", &map) < 0) + return -1; + + ret =3D virBitmapLastSetBit(map); + virBitmapFree(map); + return ret; +} + +bool +virNumaNodeIsAvailable(int node) +{ + bool ret =3D false; + virBitmapPtr map =3D NULL; + + if (virSysfsGetValueBitmap("node/online", &map) < 0) + return false; + + ret =3D virBitmapIsBitSet(map, node); + virBitmapFree(map); + return ret; +} + +int +virNumaGetNodeMemory(int node, + unsigned long long *memsize, + unsigned long long *memfree) +{ + const unsigned long long base =3D 1 << 30; + + if (memsize) + *memsize =3D base * (node + 1); + + if (memfree) + *memfree =3D base; + + return 0; +} + +int +virNumaGetDistances(int node ATTRIBUTE_UNUSED, + int **distances, + int *ndistances) +{ + *distances =3D NULL; + *ndistances =3D 0; + return 0; +} + +/* + * TODO: Adapt virNumaGetHugePageInfo{Path,Dir} to use virsysfs so that the + * paths can be modified and this function can be thrown away and instead = we'd + * have copied info from /sys (as we do with /sys/devices/system). + */ +int +virNumaGetPages(int node, + unsigned int **pages_size, + unsigned int **pages_avail, + unsigned int **pages_free, + size_t *npages) +{ + const int pages_def[] =3D { 4, 2 * 1024, 1 * 1024 * 1024}; + const int npages_def =3D ARRAY_CARDINALITY(pages_def); + size_t i =3D 0; + + if (pages_size) + *pages_size =3D NULL; + + if (pages_avail) + *pages_avail =3D NULL; + + if (pages_free) + *pages_free =3D NULL; + + *npages =3D 0; + + if ((pages_size && VIR_ALLOC_N(*pages_size, npages_def) < 0) || + (pages_avail && VIR_ALLOC_N(*pages_avail, npages_def) < 0) || + (pages_free && VIR_ALLOC_N(*pages_free, npages_def) < 0)) { + VIR_FREE(*pages_size); + VIR_FREE(*pages_avail); + return -1; + } + + *npages =3D npages_def; + if (pages_size) + memcpy(*pages_size, pages_def, sizeof(pages_def)); + + node++; + if (node <=3D 0) + node =3D 32; + + if (pages_avail || pages_free) { + for (i =3D 0; i < *npages; i++) { + if (pages_avail) + (*pages_avail)[i] =3D (node + i) * 2 << 10; + if (pages_free) + (*pages_free)[i] =3D (node + i) * 1 << 10; + } + } + + return 0; +} + +int +virNumaGetNodeCPUs(int node, virBitmapPtr *cpus) +{ + int ret =3D -1; + char *cpulist =3D NULL; + + if (virSysfsGetNodeValueString(node, "cpulist", &cpulist) < 0) + return -1; + + *cpus =3D virBitmapParseUnlimited(cpulist); + if (!cpus) + goto cleanup; + + ret =3D virBitmapCountBits(*cpus); + cleanup: + VIR_FREE(cpulist); + return ret; +} --=20 2.12.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list