From 853378b46df13e33c7d08109f219cb38f4532b71 Mon Sep 17 00:00:00 2001 From: nyyu Date: Tue, 30 Apr 2024 09:12:09 +0200 Subject: [PATCH] feat: init --- .woodpecker.yml | 15 +++++++++++++++ Dockerfile | 9 +++++++++ copy.sh | 3 +++ numcpu_override.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 .woodpecker.yml create mode 100644 Dockerfile create mode 100755 copy.sh create mode 100644 numcpu_override.c diff --git a/.woodpecker.yml b/.woodpecker.yml new file mode 100644 index 0000000..53fa11e --- /dev/null +++ b/.woodpecker.yml @@ -0,0 +1,15 @@ +steps: + docker: + image: docker-public.nyyu.dev/nyyu/plugin-kaniko + pull: true + settings: + username: + from_secret: registry_username + password: + from_secret: registry_password + repo: ${CI_REPO_OWNER}/numcpu_override + registry: ${DOCKER_REGISTRY} + tags: alpine + when: + branch: ${CI_REPO_DEFAULT_BRANCH} + event: [push, manual, tag] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fffa592 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine:3.18 +COPY . . +RUN apk add --no-cache gcc musl-dev && \ + gcc -shared -fPIC -Wall -Wextra -Werror numcpu_override.c -o numcpu_override.so -ldl + +FROM alpine:3.18 +COPY /copy.sh / +COPY --from=0 numcpu_override.so / +ENTRYPOINT ["/copy.sh"] diff --git a/copy.sh b/copy.sh new file mode 100755 index 0000000..9d93a58 --- /dev/null +++ b/copy.sh @@ -0,0 +1,3 @@ +#!/bin/sh +echo "copy '$(ls *.so)' to /_libs/" +cp -f *.so /_libs/ diff --git a/numcpu_override.c b/numcpu_override.c new file mode 100644 index 0000000..da23d20 --- /dev/null +++ b/numcpu_override.c @@ -0,0 +1,32 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +typedef long (*orig_sysconf_f_type)(int name); + +long sysconf(int name) { + orig_sysconf_f_type orig; + orig = (orig_sysconf_f_type)dlsym(RTLD_NEXT, "sysconf"); + + if (name != _SC_NPROCESSORS_CONF && name != _SC_NPROCESSORS_ONLN) { + return orig(name); + } + + printf("Overriding call sysconf %d\n", name); + + long numcpus = 0; + char *env = getenv("NUMCPUS"); + printf("Env NUMCPUS %s\n", env); + if (env != NULL && strlen(env) > 0) { + numcpus = strtol(env, NULL, 10); + } + if (numcpus == 0) { + numcpus = orig(name); + } + printf("Return %ld\n", numcpus); + fflush(stdout); + return numcpus; +}