Changeset: f7479cec3e71 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/f7479cec3e71
Modified Files:
gdk/gdk_system.c
gdk/gdk_system_private.h
gdk/gdk_utils.c
tools/mserver/monet_version.c.in
Branch: Dec2025
Log Message:
Delve deeper into cgroup settings to find #CPUs.
Also fix a scanf pattern.
diffs (203 lines):
diff --git a/gdk/gdk_system.c b/gdk/gdk_system.c
--- a/gdk/gdk_system.c
+++ b/gdk/gdk_system.c
@@ -1171,6 +1171,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;
@@ -1218,46 +1252,19 @@ 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 < ncpus)
- return ncpu;
- }
+ if (ncpu > 0 && ncpu < ncpus)
+ ncpus = ncpu;
} else {
f = fopen("/sys/fs/cgroup/cpu.max", "r");
if (f != NULL) {
- int32_t quota, period;
+ 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, "%" SCNi32 " %" SCNi32, "a, &period)
== 2 && period > 0) {
+ if (fscanf(f, "%" SCNu64 " %" SCNu64, "a, &period)
== 2 && period > 0) {
int ncpu = quota / period;
if (ncpu < ncpus)
ncpus = ncpu;
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
@@ -22,6 +22,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
@@ -462,6 +462,8 @@ size_t _MT_npages = 0; /* variable hold
static lng programepoch;
+int GDKnr_threads = 0;
+
void
MT_init(void)
{
@@ -709,6 +711,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,
+ "a, &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, ':');
@@ -1164,9 +1187,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");
@@ -1268,9 +1293,6 @@ GDKinit(opt *set, int setlen, bool embed
return GDK_SUCCEED;
}
-int GDKnr_threads = 0;
-static ATOMIC_TYPE GDKnrofthreads = ATOMIC_VAR_INIT(0);
-
bool
GDKexiting(void)
{
@@ -1364,7 +1386,6 @@ GDKreset(int status)
}
GDKnr_threads = 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());
#ifdef MONETDB_RELEASE
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]