From 4c91cf7a6e0803fe2ad83d534ff3bcb419410e8a Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Wed, 13 Aug 2025 12:26:10 +0200 Subject: [PATCH] proj: Add perf/ --- .gitignore | 1 + proj/intercept/src/intercept.c | 2 +- proj/perf/Makefile | 16 ++++++++++++++++ proj/perf/main.c | 33 +++++++++++++++++++++++++++++++++ proj/perf/test.sh | 22 ++++++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 proj/perf/Makefile create mode 100644 proj/perf/main.c create mode 100755 proj/perf/test.sh diff --git a/.gitignore b/.gitignore index 4f8bb60..e4db9f5 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ bin/ *.log *.pdf related-work/ +main diff --git a/proj/intercept/src/intercept.c b/proj/intercept/src/intercept.c index 7a95e40..6d70f82 100644 --- a/proj/intercept/src/intercept.c +++ b/proj/intercept/src/intercept.c @@ -1240,7 +1240,7 @@ void sym(exit)(int status) { char msg_buf[BUFFER_SIZE]; rcv(msg_buf, sizeof(msg_buf)); if_modify_int("exit", int, status) - else if_invalid(getopt) + else if_invalid(exit) } __real_exit(status); } diff --git a/proj/perf/Makefile b/proj/perf/Makefile new file mode 100644 index 0000000..ec3ec12 --- /dev/null +++ b/proj/perf/Makefile @@ -0,0 +1,16 @@ + +CC=gcc +CFLAGS=-std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L -g + +.PHONY: all clean +all: default +default: main + +main.o: main.c + $(CC) -c -o $@ $^ $(CFLAGS) + +main: main.o + $(CC) -o $@ $^ $(CFLAGS) -lc + +clean: + rm -rf main *.o diff --git a/proj/perf/main.c b/proj/perf/main.c new file mode 100644 index 0000000..fa84381 --- /dev/null +++ b/proj/perf/main.c @@ -0,0 +1,33 @@ + +#include +#include +#include +#include +#include +#include + +int main(const int argc, const char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: main \n"); + return 1; + } + + const long cycles = strtol(argv[1], NULL, 10); + + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + + int pipes[2]; + for (int i = 0; i < cycles; i++) { + if (pipe(pipes) != 0) { + fprintf(stderr, "unable to create pipes: %s\n", strerror(errno)); + exit(1); + } + close(pipes[0]); + close(pipes[1]); + } + + clock_gettime(CLOCK_MONOTONIC, &end); + const long duration = end.tv_sec * 1000000000 + end.tv_nsec - start.tv_sec * 1000000000 - start.tv_nsec; + printf("start: %li.%09li\nend: %li.%09li\nduration: %li,%03li,%03li ns\n", start.tv_sec, start.tv_nsec, end.tv_sec, end.tv_nsec, duration / 1000000, duration / 1000 % 1000, duration % 1000); +} diff --git a/proj/perf/test.sh b/proj/perf/test.sh new file mode 100755 index 0000000..e98cd13 --- /dev/null +++ b/proj/perf/test.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +REPEAT=30 +CYCLES=50 + +function test() { + echo $@ + for c in $(seq 1 $CYCLES); do + for i in $(seq 1 $REPEAT); do + echo "cycles: $((c * 100)) ($i/$REPEAT)" + $@ $((c * 100)) + sleep 1 + done + done +} + +test ./main +cd ../intercept +test ./intercept -o -i - -- ../perf/main +test ./intercept -o -i stderr -- ../perf/main +test ./intercept -o -i file:out.log -- ../perf/main +#test ./intercept -o -i unix:/ -- ../perf/main