From 270520b4a2eac8725c8575c3180964289722e191 Mon Sep 17 00:00:00 2001 From: John Ogness Date: Tue, 19 Dec 2017 10:59:40 +0100 Subject: schulung_tools: add various demos and tools Different tools have been used by various trainers as demos. Put all these into master so they are available to all trainers. ipc_pipe: ipc demo using pipes ipc_shm: ipc demo using shared memory libduma: source and instructions for compiling libduma matrix: demo of good and bad cache access mtrace: patch and infos for using mtrace with ASLR rtex: demo of handling page faults Signed-off-by: John Ogness --- schulung_tools/ipc_pipe/Makefile | 12 ++++ schulung_tools/ipc_pipe/README | 10 ++++ schulung_tools/ipc_pipe/main.c | 115 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 schulung_tools/ipc_pipe/Makefile create mode 100644 schulung_tools/ipc_pipe/README create mode 100644 schulung_tools/ipc_pipe/main.c (limited to 'schulung_tools/ipc_pipe') diff --git a/schulung_tools/ipc_pipe/Makefile b/schulung_tools/ipc_pipe/Makefile new file mode 100644 index 0000000..a5c8404 --- /dev/null +++ b/schulung_tools/ipc_pipe/Makefile @@ -0,0 +1,12 @@ +CFLAGS=-O0 -g -Wall -pedantic -lpthread -std=gnu99 +LDFLAGS=-lpthread + +all: main + +main.o: main.c +main: main.o + +clean: + rm -rf main *.o + +.PHONY: clean diff --git a/schulung_tools/ipc_pipe/README b/schulung_tools/ipc_pipe/README new file mode 100644 index 0000000..c0fccac --- /dev/null +++ b/schulung_tools/ipc_pipe/README @@ -0,0 +1,10 @@ +#!/bin/sh + +# run test program +sudo trace-cmd record \ + -e sched:sched_switch \ + -e sched:sched_wakeup \ + taskset 1 chrt -f 80 ./main -s 1000000 + +# view results +kernelshark diff --git a/schulung_tools/ipc_pipe/main.c b/schulung_tools/ipc_pipe/main.c new file mode 100644 index 0000000..bda2da7 --- /dev/null +++ b/schulung_tools/ipc_pipe/main.c @@ -0,0 +1,115 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +/* pipe file descriptors */ +static int fd[2]; + +/* send/write size */ +static size_t size; + +/* options */ +static struct option long_opts[] = { + { "size", required_argument, NULL, 's' }, + { NULL, 0, NULL, 0 }, +}; + +static void *reader(void *arg) +{ + unsigned char buf[size]; + ssize_t read_ = 0; + + while (read_ < size) { + ssize_t ret; + + ret = read(fd[0], buf + read_, size - read_); + if (ret < 0) { + perror("read() failed"); + exit(EXIT_FAILURE); + } + read_ += ret; + } + + printf("Reader: got data\n"); + return NULL; +} + +static void *writer(void *arg) +{ + unsigned char buf[size]; + ssize_t written = 0; + + sleep(1); + + while (written < size) { + ssize_t ret; + + ret = write(fd[1], buf + written, size - written); + if (ret < 0) { + perror("write() failed"); + exit(EXIT_FAILURE); + } + + written += ret; + } + + printf("Writer: sent data\n"); + + sleep(1); + + return NULL; +} + +static inline void print_usage_and_die(void) +{ + fprintf(stderr, "usage: pipedemo [-s ]\n"); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) +{ + pthread_t tid1, tid2; + char *end; + int c; + + size = 4 << 10; + while ((c = getopt_long(argc, argv, "s:", long_opts, NULL)) != -1) { + switch (c) { + case 's': + size = strtoull(optarg, &end, 10); + if (end == optarg || *end != '\0' || errno == ERANGE) { + fprintf(stderr, "given size is not valid\n"); + return EXIT_FAILURE; + } + break; + default: + print_usage_and_die(); + } + } + + if (pipe(fd) < 0) { + perror("pipe() failed"); + return EXIT_FAILURE; + } + + if (pthread_create(&tid1, NULL, reader, NULL) || + pthread_create(&tid2, NULL, writer, NULL)) { + fprintf(stderr, "pthread_create() failed\n"); + return EXIT_FAILURE; + } + + if (pthread_setname_np(tid1, "pipedemo-reader") || + pthread_setname_np(tid2, "pipedemo-writer")) { + fprintf(stderr, "pthread_create() failed\n"); + return EXIT_FAILURE; + } + + pthread_join(tid1, NULL); + pthread_join(tid2, NULL); + + return EXIT_SUCCESS; +} -- cgit v1.2.3