aboutsummaryrefslogtreecommitdiffstats
path: root/prototypes/connections
diff options
context:
space:
mode:
authoreunhhu <[email protected]>2026-09-27 07:37:44 +0100
committereunhhu <[email protected]>2026-09-27 07:37:44 +0100
commit78e9ba927d0c6f15e0388c8a5e056d1e82e21e88 (patch)
treeea45a572c651def9927d4f1b4c48cd2e8a59b868 /prototypes/connections
parent9b5f7629fdf87bcbaade4b9baa349cddc631c63f (diff)
downloadpure-78e9ba927d0c6f15e0388c8a5e056d1e82e21e88.tar.gz
pure-78e9ba927d0c6f15e0388c8a5e056d1e82e21e88.zip
Choose the connection threads with --threads auto
One connection thread for each CPU the process may run on (its affinity, or the online CPUs), less the one the owner of an app with states takes: three CPUs give two connection threads and the owner. The default stays one thread.
Diffstat (limited to 'prototypes/connections')
-rw-r--r--prototypes/connections/serve.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/prototypes/connections/serve.c b/prototypes/connections/serve.c
index 24248b6..1eb5ca5 100644
--- a/prototypes/connections/serve.c
+++ b/prototypes/connections/serve.c
@@ -49,6 +49,7 @@
#include <string.h>
#include <strings.h>
#include <poll.h>
+#include <sched.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/socket.h>
@@ -1650,6 +1651,16 @@ static void stop_shard(Host *host) {
free(host->views);
}
+/* --threads auto: a connection thread for each CPU this process may run on,
+ * less the one the owner of an app with states takes. */
+static unsigned auto_threads(bool owner) {
+ cpu_set_t set;
+ long cpus = sched_getaffinity(0, sizeof(set), &set) ? sysconf(_SC_NPROCESSORS_ONLN) : CPU_COUNT(&set);
+ if (cpus < 1) cpus = 1;
+ if (owner && cpus > 1) --cpus;
+ return cpus > 256 ? 256 : (unsigned)cpus;
+}
+
int main(int argc, char **argv) {
static PureModule module;
static Owner owner;
@@ -1665,6 +1676,7 @@ int main(int argc, char **argv) {
else if (!strcmp(argv[i], "--port") && number(argv[i + 1], 65535, &value)) port = (unsigned)value;
else if (!strcmp(argv[i], "--budget") && number(argv[i + 1], SIZE_MAX, &value) && value) budget = (size_t)value;
else if (!strcmp(argv[i], "--connections") && number(argv[i + 1], 1u << 20, &value) && value) capacity = (size_t)value;
+ else if (!strcmp(argv[i], "--threads") && !strcmp(argv[i + 1], "auto")) threads = 0;
else if (!strcmp(argv[i], "--threads") && number(argv[i + 1], 256, &value) && value) threads = (unsigned)value;
else if (!strcmp(argv[i], "--timeout-ms") && number(argv[i + 1], 3600000, &value) && value) timeout_ms = value;
else goto usage;
@@ -1672,6 +1684,7 @@ int main(int argc, char **argv) {
uint32_t show, edit;
bool documents;
if (!module_path || !store || !load_module(&module, module_path, &documents, &show, &edit)) goto usage;
+ if (!threads) threads = auto_threads(module.states);
if (pure_store_init(store) != PURE_OK) { fputs("Cannot initialize the store directory\n", stderr); return 1; }
signal(SIGPIPE, SIG_IGN);
struct sigaction stop_action = {.sa_handler = stop};
@@ -1746,7 +1759,7 @@ int main(int argc, char **argv) {
free(thread);
return 0;
usage:
- fputs("pure-serve --module FILE.pmod --store DIRECTORY [--port 8767] [--threads 1] [--budget BYTES] "
+ fputs("pure-serve --module FILE.pmod --store DIRECTORY [--port 8767] [--threads 1|N|auto] [--budget BYTES] "
"[--connections 1024] [--timeout-ms 10000]\n", stderr);
return 2;
}