aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-16 12:20:00 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-16 12:20:00 +0200
commit7752cf79b288f9812360f55155a7c9c16c6a0209 (patch)
tree9aab54192f18350405198b73e606cdf9251d9878
parent7087c2928f5b61a73c6b1a15a6c1f32108e3c4b3 (diff)
downloadffwd-7752cf79b288f9812360f55155a7c9c16c6a0209.tar.gz
ffwd-7752cf79b288f9812360f55155a7c9c16c6a0209.zip
cpu: size default threads to cgroup cpu quota, not host core count
-rw-r--r--embed_cli.c2
-rw-r--r--embed_server.c2
-rw-r--r--qwen_kernels.c57
3 files changed, 57 insertions, 4 deletions
diff --git a/embed_cli.c b/embed_cli.c
index 7b549e5..df70d7e 100644
--- a/embed_cli.c
+++ b/embed_cli.c
@@ -56,7 +56,7 @@ static void print_usage(const char *prog) {
" -b, --batch-size N\n"
" Max texts per engine batch (default: all; --stream: 1)\n"
" -t, --threads N\n"
- " CPU threads (default: all cores)\n"
+ " CPU threads (default: available cores, cgroup-aware)\n"
" -e, --embeddings\n"
" Print raw embeddings (with multiple texts)\n"
" -v, --verbose\n"
diff --git a/embed_server.c b/embed_server.c
index a5a8841..e1f2858 100644
--- a/embed_server.c
+++ b/embed_server.c
@@ -3446,7 +3446,7 @@ static void print_usage(const char *prog) {
" --cuda-weight-dtype DTYPE CUDA weight storage: f32 or bf16 (default:\n"
" f32); bf16 halves weight memory\n"
#endif
- " -t, --threads N CPU threads (default: all cores)\n"
+ " -t, --threads N CPU threads (default: available cores, cgroup-aware)\n"
" -v, --verbose Verbose (-vv for debug)\n"
" -V, --version Print version and exit\n"
" --build-info Print build details and exit\n"
diff --git a/qwen_kernels.c b/qwen_kernels.c
index 9368529..a5d9778 100644
--- a/qwen_kernels.c
+++ b/qwen_kernels.c
@@ -144,6 +144,51 @@ void qwen_set_threads(int n) {
}
}
+#ifndef __APPLE__
+/* CPU bandwidth this process may actually use, in whole cores, or 0 if there
+ * is no cgroup limit. Containers (Docker --cpus, Kubernetes, RunPod) expose
+ * every host CPU through sysconf()/cpuset, but the CFS quota caps the CPU time
+ * the process really gets. Sizing the BLAS/thread pool to the host count then
+ * oversubscribes that quota: the threads spend the period's budget early and
+ * the scheduler throttles them for the rest of each period, which adds large,
+ * bursty stalls (e.g. a 16-thread default against a 5.1-core quota ran the
+ * 0.6B model ~2x slower than a 5-thread pool). Floor, not ceil, so a fully
+ * busy pool stays under the quota and never trips throttling. */
+static int cgroup_cpu_quota(void) {
+ /* cgroup v2: "/sys/fs/cgroup/cpu.max" is "<quota> <period>", or
+ * "max <period>" when unlimited. */
+ FILE *f = fopen("/sys/fs/cgroup/cpu.max", "r");
+ if (f) {
+ char quota[32];
+ long period = 0;
+ int got = fscanf(f, "%31s %ld", quota, &period);
+ fclose(f);
+ if (got == 2 && period > 0 && strcmp(quota, "max") != 0) {
+ long q = atol(quota);
+ if (q > 0)
+ return q / period < 1 ? 1 : (int)(q / period);
+ }
+ }
+ /* cgroup v1: separate files; quota -1 means unlimited. */
+ long q = -1, period = 0;
+ f = fopen("/sys/fs/cgroup/cpu/cpu.cfs_quota_us", "r");
+ if (f) {
+ if (fscanf(f, "%ld", &q) != 1)
+ q = -1;
+ fclose(f);
+ }
+ f = fopen("/sys/fs/cgroup/cpu/cpu.cfs_period_us", "r");
+ if (f) {
+ if (fscanf(f, "%ld", &period) != 1)
+ period = 0;
+ fclose(f);
+ }
+ if (q > 0 && period > 0)
+ return q / period < 1 ? 1 : (int)(q / period);
+ return 0;
+}
+#endif
+
int qwen_get_num_cpus(void) {
#ifdef __APPLE__
int n = 0;
@@ -151,8 +196,16 @@ int qwen_get_num_cpus(void) {
sysctlbyname("hw.ncpu", &n, &len, NULL, 0);
return n > 0 ? n : 1;
#else
- int n = (int)sysconf(_SC_NPROCESSORS_ONLN);
- return n > 0 ? n : 1;
+ int online = (int)sysconf(_SC_NPROCESSORS_ONLN);
+ if (online < 1)
+ online = 1;
+ /* Honor a cgroup CPU quota when it is tighter than the visible core count,
+ * so the default thread pool does not oversubscribe a CPU-limited
+ * container and get throttled. */
+ int quota = cgroup_cpu_quota();
+ if (quota >= 1 && quota < online)
+ return quota;
+ return online;
#endif
}