]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lscpu: add setsize to lscpu context
authorKarel Zak <kzak@redhat.com>
Fri, 21 Aug 2020 10:21:43 +0000 (12:21 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 13 Nov 2020 08:19:02 +0000 (09:19 +0100)
Don't recalculate again and again setsize from maxcpus.

Signed-off-by: Karel Zak <kzak@redhat.com>
sys-utils/lscpu-cputype.c
sys-utils/lscpu-topology.c
sys-utils/lscpu.h

index e0f3636fdfb3b2ba5ba597c3d800072be3a01abd..5b5cfca05c74f532886a48d57c2ee6673b73ffad 100644 (file)
@@ -593,8 +593,6 @@ void lscpu_free_architecture(struct lscpu_arch *ar)
 
 int lscpu_read_cpulists(struct lscpu_cxt *cxt)
 {
-       size_t maxn;
-       size_t setsize;
        cpu_set_t *cpuset = NULL;
 
        assert(cxt);
@@ -613,12 +611,11 @@ int lscpu_read_cpulists(struct lscpu_cxt *cxt)
                 * real /sys, let's use any crazy number... */
                cxt->maxcpus = 2048;
 
-       maxn = cxt->maxcpus;
-       setsize = CPU_ALLOC_SIZE(maxn);
+       cxt->setsize = CPU_ALLOC_SIZE(cxt->maxcpus);
 
        /* create CPUs from possible mask */
-       if (ul_path_readf_cpulist(cxt->syscpu, &cpuset, maxn, "possible") == 0) {
-               lscpu_create_cpus(cxt, cpuset, setsize);
+       if (ul_path_readf_cpulist(cxt->syscpu, &cpuset, cxt->maxcpus, "possible") == 0) {
+               lscpu_create_cpus(cxt, cpuset, cxt->setsize);
                cpuset_free(cpuset);
                cpuset = NULL;
        } else
@@ -627,12 +624,12 @@ int lscpu_read_cpulists(struct lscpu_cxt *cxt)
 
 
        /* get mask for present CPUs */
-       if (ul_path_readf_cpulist(cxt->syscpu, &cxt->present, maxn, "present") == 0)
-               cxt->npresents = CPU_COUNT_S(setsize, cxt->present);
+       if (ul_path_readf_cpulist(cxt->syscpu, &cxt->present, cxt->maxcpus, "present") == 0)
+               cxt->npresents = CPU_COUNT_S(cxt->setsize, cxt->present);
 
        /* get mask for online CPUs */
-       if (ul_path_readf_cpulist(cxt->syscpu, &cxt->online, maxn, "online") == 0)
-               cxt->nonlines = CPU_COUNT_S(setsize, cxt->online);
+       if (ul_path_readf_cpulist(cxt->syscpu, &cxt->online, cxt->maxcpus, "online") == 0)
+               cxt->nonlines = CPU_COUNT_S(cxt->setsize, cxt->online);
 
        return 0;
 }
index e24900a4baeabf275706913ba8c818cb21a2b8fd..05d1ae1640abeeabfb10850e4a1d5c52d6f66759 100644 (file)
@@ -92,13 +92,12 @@ void lscpu_sort_caches(struct lscpu_cache *caches, size_t n)
 /* Read topology for specified type */
 static int cputype_read_topology(struct lscpu_cxt *cxt, struct lscpu_cputype *ct)
 {
-       size_t i, setsize, npos;
+       size_t i, npos;
        struct path_cxt *sys;
        int nthreads = 0, sw_topo = 0;
        FILE *fd;
 
        sys = cxt->syscpu;                              /* /sys/devices/system/cpu/ */
-       setsize = CPU_ALLOC_SIZE(cxt->maxcpus);         /* CPU set size */
        npos = cxt->npossibles;                         /* possible CPUs */
 
        DBG(TYPE, ul_debugobj(ct, "reading %s/%s/%s topology",
@@ -130,7 +129,7 @@ static int cputype_read_topology(struct lscpu_cxt *cxt, struct lscpu_cputype *ct
                ul_path_readf_cpuset(sys, &drawer_siblings, cxt->maxcpus,
                                        "cpu%d/topology/drawer_siblings", num);
 
-               n = CPU_COUNT_S(setsize, thread_siblings);
+               n = CPU_COUNT_S(cxt->setsize, thread_siblings);
                if (!n)
                        n = 1;
                if (n > nthreads)
@@ -154,13 +153,13 @@ static int cputype_read_topology(struct lscpu_cxt *cxt, struct lscpu_cputype *ct
                        ct->drawermaps = xcalloc(npos, sizeof(cpu_set_t *));
 
                /* add to topology maps */
-               add_cpuset_to_array(ct->coremaps, &ct->ncores, thread_siblings, setsize);
-               add_cpuset_to_array(ct->socketmaps, &ct->nsockets, core_siblings, setsize);
+               add_cpuset_to_array(ct->coremaps, &ct->ncores, thread_siblings, cxt->setsize);
+               add_cpuset_to_array(ct->socketmaps, &ct->nsockets, core_siblings, cxt->setsize);
 
                if (book_siblings)
-                       add_cpuset_to_array(ct->bookmaps, &ct->nbooks, book_siblings, setsize);
+                       add_cpuset_to_array(ct->bookmaps, &ct->nbooks, book_siblings, cxt->setsize);
                if (drawer_siblings)
-                       add_cpuset_to_array(ct->drawermaps, &ct->ndrawers, drawer_siblings, setsize);
+                       add_cpuset_to_array(ct->drawermaps, &ct->ndrawers, drawer_siblings, cxt->setsize);
 
        }
 
@@ -277,7 +276,7 @@ static int read_caches(struct lscpu_cxt *cxt, struct lscpu_cpu *cpu)
        char buf[256];
        struct path_cxt *sys = cxt->syscpu;
        int num = cpu->logical_id;
-       size_t i, ncaches, setsize;
+       size_t i, ncaches;
 
        ncaches = cxt->ncaches;
        while (ul_path_accessf(sys, F_OK,
@@ -285,8 +284,6 @@ static int read_caches(struct lscpu_cxt *cxt, struct lscpu_cpu *cpu)
                                num, ncaches) == 0)
                ncaches++;
 
-       setsize = CPU_ALLOC_SIZE(cxt->maxcpus);
-
        for (i = 0; i < ncaches; i++) {
                struct lscpu_cache *ca;
                cpu_set_t *map;
@@ -350,7 +347,7 @@ static int read_caches(struct lscpu_cxt *cxt, struct lscpu_cpu *cpu)
                if (!ca->sharedmaps)
                        ca->sharedmaps = xcalloc(cxt->npossibles, sizeof(cpu_set_t *));
 
-               add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map, setsize);
+               add_cpuset_to_array(ca->sharedmaps, &ca->nsharedmaps, map, cxt->setsize);
        }
 
        return 0;
index 45ac844417cc7d23bcb48a70af24a0a1edd68e62..868b0e20c1bafd5507beeafd6fe874b84f9fe880 100644 (file)
@@ -186,6 +186,7 @@ enum {
 
 struct lscpu_cxt {
        int maxcpus;            /* size in bits of kernel cpu mask */
+       size_t setsize;
        const char *prefix;     /* path to /sys and /proc snapshot or NULL */
 
        struct path_cxt *syscpu; /* _PATH_SYS_CPU path handler */
@@ -231,6 +232,14 @@ struct lscpu_cxt {
 
 };
 
+#define is_cpu_online(_cxt, _cpu) \
+               ((_cxt) && (_cpu) && (_cxt)->online && \
+                CPU_ISSET_S((_cpu)->logical_id, (_cxt)->setsize, (_cxt)->online))
+
+#define is_cpu_present(_cxt, _cpu) \
+               ((_cxt) && (_cpu) && (_cxt)->present && \
+                CPU_ISSET_S((_cpu)->logical_id, (_cxt)->setsize, (_cxt)->present))
+
 struct lscpu_cputype *lscpu_new_cputype(void);
 void lscpu_ref_cputype(struct lscpu_cputype *ct);
 void lscpu_unref_cputype(struct lscpu_cputype *ct);