Changeset: d74ab7ec5091 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/d74ab7ec5091
Modified Files:
        gdk/gdk_system.c
        gdk/gdk_system_private.h
        gdk/gdk_utils.c
        tools/mserver/monet_version.c.in
Branch: default
Log Message:

Merge with Dec2025 branch.


diffs (204 lines):

diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -1177,6 +1177,40 @@ MT_kill_threads(void)
 }
 
 int
+parse_cpuset(FILE *f)
+{
+       int ncpu = 0;
+       char buf[512];
+       char *p = fgets(buf, 512, f);
+       if (p != NULL) {
+               /* syntax is: ranges of CPU numbers separated by comma;
+                * a range is either a single CPU id, or two IDs
+                * separated by a minus; any deviation causes the file
+                * to be ignored */
+               for (;;) {
+                       char *q;
+                       unsigned fst = strtoul(p, &q, 10);
+                       if (q == p)
+                               return 0;
+                       ncpu++;
+                       if (*q == '-') {
+                               p = q + 1;
+                               unsigned lst = strtoul(p, &q, 10);
+                               if (q == p || lst <= fst)
+                                       return 0;
+                               ncpu += lst - fst;
+                       }
+                       if (*q == '\n')
+                               break;
+                       if (*q != ',')
+                               return 0;
+                       p = q + 1;
+               }
+       }
+       return ncpu;
+}
+
+int
 MT_check_nr_cores(void)
 {
        int ncpus = -1;
@@ -1224,36 +1258,24 @@ MT_check_nr_cores(void)
        if (f == NULL)
                f = fopen("/sys/fs/cgroup/cpuset.cpus.effective", "r"); /* v2 */
        if (f != NULL) {
-               char buf[512];
-               char *p = fgets(buf, 512, f);
+               int ncpu = parse_cpuset(f);
                fclose(f);
-               if (p != NULL) {
-                       /* syntax is: ranges of CPU numbers separated
-                        * by comma; a range is either a single CPU
-                        * id, or two IDs separated by a minus; any
-                        * deviation causes the file to be ignored */
-                       int ncpu = 0;
-                       for (;;) {
-                               char *q;
-                               unsigned fst = strtoul(p, &q, 10);
-                               if (q == p)
-                                       return ncpus;
-                               ncpu++;
-                               if (*q == '-') {
-                                       p = q + 1;
-                                       unsigned lst = strtoul(p, &q, 10);
-                                       if (q == p || lst <= fst)
-                                               return ncpus;
-                                       ncpu += lst - fst;
-                               }
-                               if (*q == '\n')
-                                       break;
-                               if (*q != ',')
-                                       return ncpus;
-                               p = q + 1;
+               if (ncpu > 0 && ncpu < ncpus)
+                       ncpus = ncpu;
+       } else {
+               f = fopen("/sys/fs/cgroup/cpu.max", "r");
+               if (f != NULL) {
+                       uint64_t quota, period;
+                       /* there should either be two numbers, or the
+                        * word "max" followed by a number; the latter
+                        * case is ignored by the fscanf not returning
+                        * 2 */
+                       if (fscanf(f, "%" SCNu64 " %" SCNu64, &quota, &period) 
== 2 && period > 0) {
+                               int ncpu = quota / period;
+                               if (ncpu < ncpus)
+                                       ncpus = ncpu;
                        }
-                       if (ncpu < ncpus)
-                               return ncpu;
+                       fclose(f);
                }
        }
 #endif
diff --git a/gdk/gdk_system_private.h b/gdk/gdk_system_private.h
--- a/gdk/gdk_system_private.h
+++ b/gdk/gdk_system_private.h
@@ -27,6 +27,9 @@ bool MT_kill_threads(void)
        __attribute__((__visibility__("hidden")));
 bool MT_thread_override_limits(void)
        __attribute__((__visibility__("hidden")));
+int parse_cpuset(FILE *f)
+       __attribute__((__visibility__("hidden")));
+
 #ifdef NATIVE_WIN32
 #define GDKwinerror(...)                                               \
        do {                                                            \
diff --git a/gdk/gdk_utils.c b/gdk/gdk_utils.c
--- a/gdk/gdk_utils.c
+++ b/gdk/gdk_utils.c
@@ -450,6 +450,8 @@ size_t _MT_npages = 0;              /* variable hold
 
 static lng programepoch;
 
+int GDKnr_threads = 0;
+
 void
 MT_init(void)
 {
@@ -697,6 +699,27 @@ MT_init(void)
                                        fclose(f);
                                }
 #endif
+                               strcpy(q, "cpu.max");
+                               f = fopen(pth, "r");
+                               if (f != NULL) {
+                                       uint64_t quota, period;
+                                       if (fscanf(f, "%" SCNu64 " %" SCNu64,
+                                                  &quota, &period) == 2) {
+                                               GDKnr_threads = quota / period;
+                                       }
+                                       fclose(f);
+                               }
+                               strcpy(q, "cpuset.cpus.effective");
+                               f = fopen(pth, "r");
+                               if (f != NULL) {
+                                       int ncpu = parse_cpuset(f);
+                                       fclose(f);
+                                       if (ncpu > 0 &&
+                                           (GDKnr_threads == 0 ||
+                                            ncpu < GDKnr_threads)) {
+                                               GDKnr_threads = ncpu;
+                                       }
+                               }
                        } else {
                                /* cgroup v1 entry */
                                p = strchr(buf, ':');
@@ -1152,9 +1175,11 @@ GDKinit(opt *set, int setlen, bool embed
                }
        free(n);
 
-       GDKnr_threads = GDKgetenv_int("gdk_nr_threads", 0);
-       if (GDKnr_threads == 0) {
-               GDKnr_threads = MT_check_nr_cores();
+       if (GDKgetenv_int("gdk_nr_threads", 0) != 0) {
+               GDKnr_threads = GDKgetenv_int("gdk_nr_threads", 0);
+       } else {
+               if (GDKnr_threads == 0)
+                       GDKnr_threads = MT_check_nr_cores();
                snprintf(buf, sizeof(buf), "%d", GDKnr_threads);
                if (GDKsetenv("gdk_nr_threads", buf) != GDK_SUCCEED) {
                        TRC_CRITICAL(GDK, "GDKsetenv gdk_nr_threads failed");
@@ -1263,9 +1288,7 @@ GDKinit(opt *set, int setlen, bool embed
        return GDK_SUCCEED;
 }
 
-int GDKnr_threads = 0;
 BUN GDKL3_size = 0;
-static ATOMIC_TYPE GDKnrofthreads = ATOMIC_VAR_INIT(0);
 
 bool
 GDKexiting(void)
@@ -1361,7 +1384,6 @@ GDKreset(int status)
 
                GDKnr_threads = 0;
                GDKL3_size = 0;
-               ATOMIC_SET(&GDKnrofthreads, 0);
                close_stream(GDKstdout);
                close_stream(GDKstdin);
                GDKstdout = NULL;
diff --git a/tools/mserver/monet_version.c.in b/tools/mserver/monet_version.c.in
--- a/tools/mserver/monet_version.c.in
+++ b/tools/mserver/monet_version.c.in
@@ -9,6 +9,7 @@
  */
 
 #include "monetdb_config.h"
+#include "gdk.h"
 #include "mal.h"
 #include "monet_version.h"
 #include "mutils.h"
@@ -42,7 +43,8 @@ monet_version(void)
 
        MT_init();  /* for MT_pagesize */
        sz_mem_gb = (dbl)(MT_npages() * MT_pagesize()) / (1024.0 * 1024.0 * 
1024.0);
-       cores = MT_check_nr_cores();
+       if ((cores = GDKnr_threads) == 0)
+               cores = MT_check_nr_cores();
 
        printf("MonetDB 5 server %s", GDKversion(false));
 #ifdef MONETDB_RELEASE
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to