]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cpu-topo: implement a sorting mechanism for CPU index
authorWilly Tarreau <w@1wt.eu>
Fri, 21 Jul 2023 15:03:20 +0000 (17:03 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 14 Mar 2025 17:30:31 +0000 (18:30 +0100)
CPU selection will be performed by sorting CPUs according to
various criteria. For dumps however, that's really not convenient
and we'll need to reorder the CPUs according to their index only.
This is what the new function cpu_reorder_by_index() does. It's
called  in thread_detect_count() before dumping the CPU topology.

include/haproxy/cpu_topo.h
src/cpu_topo.c
src/thread.c

index 72c611380d50d509fafdb278c5670cb13653f662..d802440791f730facfd7f3766e04aec5ebcaf5e3 100644 (file)
@@ -43,4 +43,14 @@ int cpu_map_configured(void);
  */
 void cpu_dump_topology(const struct ha_cpu_topo *topo);
 
+/* re-order a CPU topology array by CPU index only, to undo the function above,
+ * in case other calls need to be made on top of this.
+ */
+void cpu_reorder_by_index(struct ha_cpu_topo *topo, int entries);
+
+/* Functions used by qsort to compare hardware CPUs (not meant to be used from
+ * outside cpu_topo).
+ */
+int _cmp_cpu_index(const void *a, const void *b);
+
 #endif /* _HAPROXY_CPU_TOPO_H */
index d1431a50345cac025ae8ed4f4470fde4d74fed77..363ab8707eaa8013e2db08cf2682e6b7be8eb87a 100644 (file)
@@ -221,6 +221,32 @@ void cpu_dump_topology(const struct ha_cpu_topo *topo)
        }
 }
 
+/* function used by qsort to re-arrange CPUs by index only, to restore original
+ * ordering.
+ */
+int _cmp_cpu_index(const void *a, const void *b)
+{
+       const struct ha_cpu_topo *l = (const struct ha_cpu_topo *)a;
+       const struct ha_cpu_topo *r = (const struct ha_cpu_topo *)b;
+
+       /* next, IDX, so that SMT ordering is preserved */
+       if (l->idx >= 0 && l->idx < r->idx)
+               return -1;
+       if (l->idx > r->idx && r->idx >= 0)
+               return  1;
+
+       /* exactly the same (e.g. absent, should not happend) */
+       return 0;
+}
+
+/* re-order a CPU topology array by CPU index only. This is mostly used before
+ * listing CPUs regardless of their characteristics.
+ */
+void cpu_reorder_by_index(struct ha_cpu_topo *topo, int entries)
+{
+       qsort(topo, entries, sizeof(*topo), _cmp_cpu_index);
+}
+
 /* returns an optimal maxcpus for the current system. It will take into
  * account what is reported by the OS, if any, otherwise will fall back
  * to the cpuset size, which serves as an upper limit in any case.
index 0227d4a16f921a5af1897a663c825a288211733c..7a720d827ed0a7191a95258710576abed666d1bc 100644 (file)
@@ -1695,8 +1695,10 @@ void thread_detect_count(void)
        }
 
 #if defined(USE_THREAD) && defined(USE_CPU_AFFINITY)
-       if (global.tune.debug & GDBG_CPU_AFFINITY)
+       if (global.tune.debug & GDBG_CPU_AFFINITY) {
+               cpu_reorder_by_index(ha_cpu_topo, cpu_topo_maxcpus);
                cpu_dump_topology(ha_cpu_topo);
+       }
 #endif
        return;
 }