]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lscpu: improve topology calculation
authorKarel Zak <kzak@redhat.com>
Tue, 3 Nov 2020 08:46:45 +0000 (09:46 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 13 Nov 2020 08:19:02 +0000 (09:19 +0100)
Let's make it more robust and readable. The sysinfo file on s390 may
contain zeros, so we need to check the values and fallback to data
from shared maps if necessary.

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

index 5d85aa5bde53781db4769646f231e8434e3fb44e..2e1a3b9870124ebda47ccdc3ff0a869d09d5233b 100644 (file)
@@ -102,7 +102,7 @@ static int cputype_read_topology(struct lscpu_cxt *cxt, struct lscpu_cputype *ct
        DBG(TYPE, ul_debugobj(ct, "reading %s/%s/%s topology",
                                ct->vendor ?: "", ct->model ?: "", ct->modelname ?:""));
 
-       for (i = 0; i < npos; i++) {
+       for (i = 0; i < cxt->npossibles; i++) {
                struct lscpu_cpu *cpu = cxt->cpus[i];
                cpu_set_t *thread_siblings = NULL, *core_siblings = NULL;
                cpu_set_t *book_siblings = NULL, *drawer_siblings = NULL;
@@ -116,8 +116,6 @@ static int cputype_read_topology(struct lscpu_cxt *cxt, struct lscpu_cputype *ct
                                        "cpu%d/topology/thread_siblings", num) != 0)
                        continue;
 
-               /*DBG(TYPE, ul_debugobj(ct, " #%d", num));*/
-
                /* read topology maps */
                ul_path_readf_cpuset(sys, &thread_siblings, cxt->maxcpus,
                                        "cpu%d/topology/thread_siblings", num);
@@ -192,22 +190,13 @@ static int cputype_read_topology(struct lscpu_cxt *cxt, struct lscpu_cputype *ct
                        fclose(fd);
        }
 
-       if (ct->mtid)
-               ct->nthreads_per_core = atoi(ct->mtid) + 1;
-       else
-               ct->nthreads_per_core = nthreads;
+       ct->nthreads_per_core = ct->mtid ? atoi(ct->mtid) + 1 : nthreads;
 
        if (!sw_topo) {
-               ct->ndrawers_per_system = ct->nbooks_per_drawer =
-                       ct->nsockets_per_book = ct->ncores_per_socket = 0;
-               if (!ct->ncores_per_socket && ct->nsockets)
-                       ct->ncores_per_socket = ct->ncores / ct->nsockets;
-               if (!ct->nsockets_per_book && ct->nbooks)
-                       ct->nsockets_per_book = ct->nsockets / ct->nbooks;
-               if (!ct->nbooks_per_drawer && ct->ndrawers)
-                       ct->nbooks_per_drawer = ct->nbooks / ct->ndrawers;
-               if (ct->ndrawers_per_system)
-                       ct->ndrawers_per_system = ct->ndrawers;
+               ct->ncores_per_socket = ct->nsockets ? ct->ncores / ct->nsockets : 0;
+               ct->nsockets_per_book = ct->nbooks   ? ct->nsockets / ct->nbooks : 0;
+               ct->nbooks_per_drawer = ct->ndrawers ? ct->nbooks / ct->ndrawers : 0;
+               ct->ndrawers_per_system = ct->ndrawers;
        }
 
        DBG(TYPE, ul_debugobj(ct, " nthreads: %zu (per core)", ct->nthreads_per_core));
index 50412b859dbdcfc3bfbf9993867d3a7608706dd4..dca441df0838f87a4292c05a88d800f7c5fd6a1e 100644 (file)
@@ -832,15 +832,16 @@ print_summary_cputype(struct lscpu_cxt *cxt,
 
        add_summary_n(tb, sec, _("Thread(s) per core:"), ct->nthreads_per_core);
        add_summary_n(tb, sec, _("Core(s) per socket:"), ct->ncores_per_socket);
+
        if (ct->nbooks) {
                add_summary_n(tb, sec, _("Socket(s) per book:"), ct->nsockets_per_book);
-               if (ct->ndrawers_per_system) {
+               if (ct->ndrawers_per_system || ct->ndrawers) {
                        add_summary_n(tb, sec, _("Book(s) per drawer:"), ct->nbooks_per_drawer);
-                       add_summary_n(tb, sec, _("Drawer(s):"), ct->ndrawers_per_system);
+                       add_summary_n(tb, sec, _("Drawer(s):"), ct->ndrawers_per_system ?: ct->ndrawers);
                } else
-                       add_summary_n(tb, sec, _("Book(s):"), ct->nbooks);
+                       add_summary_n(tb, sec, _("Book(s):"), ct->nbooks_per_drawer ?: ct->nbooks);
        } else
-               add_summary_n(tb, sec, _("Socket(s):"), ct->nsockets);
+               add_summary_n(tb, sec, _("Socket(s):"), ct->nsockets_per_book ?: ct->nsockets);
 
        if (ct->stepping)
                add_summary_s(tb, sec, _("Stepping:"), ct->stepping);