From 0ecb919e738cff0b060f393495e53c18e931671f Mon Sep 17 00:00:00 2001 From: LI Qing Date: Fri, 19 Apr 2024 16:32:19 +0800 Subject: [PATCH] Add getpid to measure the performance of system calls --- regression/apps/Makefile | 15 ++++++++++++- regression/apps/getpid/Makefile | 5 +++++ regression/apps/getpid/getpid.c | 36 ++++++++++++++++++++++++++++++ regression/apps/scripts/process.sh | 2 +- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 regression/apps/getpid/Makefile create mode 100644 regression/apps/getpid/getpid.c diff --git a/regression/apps/Makefile b/regression/apps/Makefile index 8a50147e2..64b2ca471 100644 --- a/regression/apps/Makefile +++ b/regression/apps/Makefile @@ -8,7 +8,20 @@ CUR_DIR := $(patsubst %/,%,$(dir $(MKFILE_PATH))) INITRAMFS ?= $(CUR_DIR)/../build/initramfs REGRESSION_BUILD_DIR ?= $(INITRAMFS)/regression -TEST_APPS := signal_c pthread network hello_world hello_pie hello_c fork_c fork execve pty mongoose +# These test apps are sorted by name +TEST_APPS := \ + execve \ + fork \ + fork_c \ + getpid \ + hello_c \ + hello_pie \ + hello_world \ + mongoose \ + network \ + pthread \ + pty \ + signal_c \ C_SOURCES := $(shell find . -type f \( -name "*.c" -or -name "*.h" \) ) diff --git a/regression/apps/getpid/Makefile b/regression/apps/getpid/Makefile new file mode 100644 index 000000000..c603a781a --- /dev/null +++ b/regression/apps/getpid/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: MPL-2.0 + +include ../test_common.mk + +EXTRA_C_FLAGS := diff --git a/regression/apps/getpid/getpid.c b/regression/apps/getpid/getpid.c new file mode 100644 index 000000000..af2191a62 --- /dev/null +++ b/regression/apps/getpid/getpid.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: MPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include + +#define NUM_OF_CALLS 1000000 + +int main() +{ + struct timespec start, end; + long seconds, nanoseconds, total_nanoseconds, avg_latency; + pid_t pid; + + clock_gettime(CLOCK_MONOTONIC, &start); + + for (int i = 0; i < NUM_OF_CALLS; i++) { + pid = syscall(SYS_getpid); + } + + clock_gettime(CLOCK_MONOTONIC, &end); + + seconds = end.tv_sec - start.tv_sec; + nanoseconds = end.tv_nsec - start.tv_nsec; + + total_nanoseconds = seconds * 1e9 + nanoseconds; + avg_latency = total_nanoseconds / NUM_OF_CALLS; + + printf("Process %d executed the getpid() syscall %d times.\n", pid, + NUM_OF_CALLS); + printf("Syscall average latency: %ld nanoseconds.\n", avg_latency); + + return 0; +} diff --git a/regression/apps/scripts/process.sh b/regression/apps/scripts/process.sh index 3b00757e2..84087fa6c 100755 --- a/regression/apps/scripts/process.sh +++ b/regression/apps/scripts/process.sh @@ -8,7 +8,7 @@ SCRIPT_DIR=/regression cd ${SCRIPT_DIR}/.. echo "Start process test......" -tests="hello_world/hello_world fork/fork execve/execve fork_c/fork signal_c/signal_test pthread/pthread_test hello_pie/hello pty/open_pty" +tests="hello_world/hello_world fork/fork execve/execve fork_c/fork signal_c/signal_test pthread/pthread_test hello_pie/hello pty/open_pty getpid/getpid" for testcase in ${tests} do echo "Running test ${testcase}......"