From 7510c2a7b3481fc208c0347549d46d6480439eb0 Mon Sep 17 00:00:00 2001 From: dragonmuffin Date: Tue, 4 Mar 2025 22:03:00 +0500 Subject: [PATCH] add tests, initial protobuf and rabbitmq support, ... --- Makefile | 37 +++++++-- resource_handler/resource_handler.c | 43 +++++++++++ resource_handler/resource_handler.h | 10 +++ starter/starter.c | 36 +-------- tests/Makefile | 11 +++ tests/resource_handler_test | Bin 0 -> 29720 bytes tests/resource_handler_test.c | 38 ++++++++++ tests/transport_test | Bin 0 -> 92024 bytes tests/transport_test.c | 114 ++++++++++++++++++++++++++++ transport/transport | Bin 0 -> 105840 bytes transport/transport.c | 96 +++++++++++++++++++++++ transport/transport.h | 27 +++++++ transport/utils.c | 48 ++++++++++++ 13 files changed, 421 insertions(+), 39 deletions(-) create mode 100644 resource_handler/resource_handler.c create mode 100644 resource_handler/resource_handler.h create mode 100644 tests/Makefile create mode 100755 tests/resource_handler_test create mode 100644 tests/resource_handler_test.c create mode 100755 tests/transport_test create mode 100644 tests/transport_test.c create mode 100755 transport/transport create mode 100644 transport/transport.c create mode 100644 transport/transport.h create mode 100644 transport/utils.c diff --git a/Makefile b/Makefile index e67c5db..619d406 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,44 @@ -.PHONY: all clean install uninstall +.PHONY: all clean install uninstall test PROTOBUF=/usr/include/ STARTER=starter GEN=gen SRCS=$(STARTER)/cgroup_prepare.c $(STARTER)/ns_exec.c $(STARTER)/util.c $(STARTER)/starter.c +RUNS=runs +PROTO_OUT=$(GEN)/google/protobuf/empty.pb-c.c $(GEN)/google/protobuf/empty.pb-c.h $(GEN)/google/protobuf/timestamp.pb-c.c $(GEN)/google/protobuf/timestamp.pb-c.h $(GEN)/google/protobuf/duration.pb-c.c $(GEN)/google/protobuf/duration.pb-c.h gen/runner/v1/runner.pb-c.c gen/runner/v1/runner.pb-c.c -all: proto $(STARTER)/alpine-make-rootfs $(STARTER)/minrootfs $(STARTER)/starter -proto: /usr/include/google/protobuf/empty.proto proto/runner/v1/runner.proto +all: $(STARTER)/alpine-make-rootfs $(STARTER)/minrootfs $(STARTER)/starter transport/transport $(RUNS) + +$(GEN): mkdir -p $(GEN) - protoc --c_out=$(GEN) runner/v1/runner.proto -I proto + +$(GEN)/google/protobuf/empty.pb-c.c $(GEN)/google/protobuf/empty.pb-c.h: $(PROTOBUF)/google/protobuf/empty.proto $(GEN) protoc --c_out=$(GEN) google/protobuf/empty.proto -I $(PROTOBUF) + +$(GEN)/google/protobuf/timestamp.pb-c.c $(GEN)/google/protobuf/timestamp.pb-c.h: $(PROTOBUF)/google/protobuf/timestamp.proto $(GEN) + protoc --c_out=$(GEN) google/protobuf/timestamp.proto -I $(PROTOBUF) + +$(GEN)/google/protobuf/duration.pb-c.c $(GEN)/google/protobuf/duration.pb-c.h: $(PROTOBUF)/google/protobuf/duration.proto $(GEN) + protoc --c_out=$(GEN) google/protobuf/duration.proto -I $(PROTOBUF) + +gen/runner/v1/runner.pb-c.c gen/runner/v1/runner.pb-c.h: proto/runner/v1/runner.proto $(GEN) + protoc --c_out=$(GEN) runner/v1/runner.proto -I proto + + $(STARTER)/alpine-make-rootfs: git clone https://github.com/alpinelinux/alpine-make-rootfs starter/alpine-make-rootfs + $(STARTER)/minrootfs: $(STARTER)/create_rootfs.sh $(STARTER)/create_rootfs.sh -$(STARTER)/starter: proto $(STARTER)/starter.c $(STARTER)/cgroup_prepare.c $(STARTER)/ns_exec.c Makefile - $(CC) -o $(STARTER)/starter $(SRCS) -g -fsanitize=address -fsanitize=leak -fno-omit-frame-pointer -lrabbitmq -lprotobuf-c -I gen + +$(STARTER)/starter: $(PROTO) $(STARTER)/starter.c $(STARTER)/cgroup_prepare.c $(STARTER)/ns_exec.c Makefile + #$(CC) -shared -o $(STARTER)/starter $(SRCS) -g -fsanitize=address -fsanitize=leak -fno-omit-frame-pointer -lrabbitmq -lprotobuf-c -I gen + $(CC) -fPIC -shared -o $(STARTER)/starter $(SRCS) -lrabbitmq -lprotobuf-c -I gen #$(CC) $(STARTER)/starter.c -o $(STARTER)/starter + +transport/transport: $(PROTO) transport/utils.c transport/transport.c transport/transport.h + $(CC) -o $@ gen/runner/v1/runner.pb-c.c transport/utils.c transport/transport.c -I . -I gen -lrabbitmq -lprotobuf-c -g -fsanitize=address -fsanitize=leak -fno-omit-frame-pointer + +$(RUNS): + mkdir $(RUNS) +include tests/Makefile diff --git a/resource_handler/resource_handler.c b/resource_handler/resource_handler.c new file mode 100644 index 0000000..ab57761 --- /dev/null +++ b/resource_handler/resource_handler.c @@ -0,0 +1,43 @@ +#include "resource_handler/resource_handler.h" + +int extract_numbers(char* str, int* res) { + bool is_digit=false,last_digit=false; + int cur=0; + int begin=-1; + int* old_res=res; + bool go=true; + while(go) { + go=(*str)!=0; + is_digit = *str <= '9' && *str >= '0'; + if(is_digit) { + cur *= 10; + cur += *str - '0'; + } + else if(last_digit) { + if(*str == '-') { + begin = cur; + } else if(begin != -1) { + for(int core = begin; core <= cur; core++) { + *res = core; + res++; + } + begin = -1; + } else { + *res = cur; + res++; + begin = -1; + } + cur = 0; + } + str++; + last_digit = is_digit; + } + return res - old_res; +} + +int get_isolated_cores(int** res) { + char buf[MAX_OPTION]; + read(open("/sys/devices/system/cpu/isolated", O_RDONLY),buf,MAX_OPTION); + *res=malloc(sizeof(int)*MAX_CORES); + return extract_numbers(buf,*res); +} diff --git a/resource_handler/resource_handler.h b/resource_handler/resource_handler.h new file mode 100644 index 0000000..a35aad7 --- /dev/null +++ b/resource_handler/resource_handler.h @@ -0,0 +1,10 @@ +#include +#include +#include +#include +#include +#define MAX_CORES 128 +#define MAX_OPTION 4096 + +int extract_numbers(char* str, int* res); +int get_isolated_cores(int** res); diff --git a/starter/starter.c b/starter/starter.c index 7d70fb7..771fa14 100644 --- a/starter/starter.c +++ b/starter/starter.c @@ -25,26 +25,6 @@ static int killafter(void *arg) { static char nmstack[STACK_SIZE]; static char killstack[STACK_SIZE]; -static void parse_args(int argc, char **argv, struct params *params, struct limits *limits){ - if (argc < 7) { - puts("usage:\n starter