Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shm to ffi tools #349

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions tests/e2e/tools/FFI/Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
CC = gcc
CFLAGS = -Wall -g
CFLAGS_SHM = -lrt -Wall -Werror

VPATH=disk/QM:memory
VPATH=disk/QM:memory:shared-memory
QM_BIN=./bin/QM
ASIL_BIN=./bin/ASIL

all: cratetestqm cratetestasil

cratetestqm: createqmbin file-allocate 90_percent_memory_eat
cratetestqm: createqmbin file-allocate 90_percent_memory_eat tst_shm

cratetestasil: createasilbin 20_percent_memory_eat
cratetestasil: createasilbin 20_percent_memory_eat tst_sys_shm

createqmbin:
@mkdir -p $(QM_BIN)
Expand All @@ -26,6 +27,12 @@ file-allocate: file-allocate.c
90_percent_memory_eat: memory_eat.c
$(CC) $(CFLAGS) -DMEM_PERCENT=0.9 -o $(QM_BIN)/$@ $<

tst_sys_shm: shm.c
$(CC) $(CFLAGS_SHM) -o $(ASIL_BIN)/$@ $<

tst_shm: shm.c
$(CC) $(CFLAGS_SHM) -o $(QM_BIN)/$@ $<

clean:
rm -rf $(QM_BIN) $(ASIL_QM)

Expand Down
98 changes: 98 additions & 0 deletions tests/e2e/tools/FFI/shared-memory/shm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */

#include <fcntl.h> /* For O_* constants */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>


/* simple SHM access test which can be used to test access e.g. through
* container boundaries */

int main(int argc, char* argv[]) {

/* use the hostname to pepper the seed because we start this several times
* quickly within separate containers or on the bare system, so time() alone
* is not good enough */
char hostname[HOST_NAME_MAX];
gethostname(hostname, HOST_NAME_MAX);
int sum = 0;
for (char *c = hostname; *c != '\0'; c++) {
sum += (int)*c;
}

srand(time(NULL) + sum);

int timeout = 2;
int value = rand();
char shm_name[NAME_MAX];
struct timespec ts_start, ts_elapsed;

if (argc > 1) {
strncpy(shm_name, argv[1], NAME_MAX-1);
shm_name[NAME_MAX] = '\0';
} else {
strcpy(shm_name, "/memtest");
}
if (argc > 2) {
value = atoi(argv[2]);
}
if (argc > 3) {
timeout = atoi(argv[3]);
}

printf("Test is starting!\n"
"Hostname: %s | IPC name: %s | Initial value: %d\n",
hostname, shm_name, value);

int fd = shm_open(shm_name, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (fd < 0) {
perror("shm_open");
return 1;
}

if (ftruncate(fd, sizeof(int)) < 0) {
perror("ftruncate");
return 1;
}

int *payload = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED,
fd, 0);

if (payload == MAP_FAILED) {
perror("mmap");
return 1;
}

*payload = value;

clock_gettime(CLOCK_MONOTONIC, &ts_start);
long elapsed = 0;

// barrier
while(*payload == value) {
clock_gettime(CLOCK_MONOTONIC, &ts_elapsed);
// keep it coarse, +-1sec is totally unimportant
elapsed = ts_elapsed.tv_sec - ts_start.tv_sec;
if (elapsed >= timeout) {
fprintf(stderr, "Test aborted, timeout!\n");
munmap(payload, sizeof(int));
shm_unlink(shm_name);
return 1;
}
}
printf("SHM access - value changed to: %d\n", *payload);

// signal the other process to also stop
*payload = value;

munmap(payload, sizeof(int));
shm_unlink(shm_name);

return 0;
}

Loading