diff options
| author | John Ogness <john.ogness@linutronix.de> | 2017-12-19 10:59:40 +0100 |
|---|---|---|
| committer | John Ogness <john.ogness@linutronix.de> | 2017-12-19 10:59:40 +0100 |
| commit | 270520b4a2eac8725c8575c3180964289722e191 (patch) | |
| tree | d9512cd96d0c52e14293c3f8bc19fd168ee14025 /schulung_tools/rtex/func.c | |
| parent | 27209bb802048f4803d9cd9a5c2f99d613986446 (diff) | |
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 <john.ogness@linutronix.de>
Diffstat (limited to 'schulung_tools/rtex/func.c')
| -rw-r--r-- | schulung_tools/rtex/func.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/schulung_tools/rtex/func.c b/schulung_tools/rtex/func.c new file mode 100644 index 0000000..4b88166 --- /dev/null +++ b/schulung_tools/rtex/func.c @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <sys/time.h> +#include <sys/resource.h> + +void timestamp(const char *msg, int val) +{ + static struct timespec tsl; + static long minfltl; + + struct timespec ts; + unsigned long diff; + struct rusage r; + long rdiff; + + clock_gettime(CLOCK_MONOTONIC, &ts); + getrusage(RUSAGE_SELF, &r); + + if (tsl.tv_sec != 0) { + diff = (ts.tv_sec - tsl.tv_sec) * 1000000000; + diff += ts.tv_nsec - tsl.tv_nsec; + + rdiff = r.ru_minflt - minfltl; + + printf("% 10d ns % 5ld faults : %s", diff, rdiff, msg); + if (val >= 0) + printf(" (%d)", val); + printf("\n"); + } + + tsl = ts; + minfltl = r.ru_minflt; +} + +/* 10 MiB */ +#define SIZE (10 * 1024 * 1024) + +void testfunc_malloc(void) +{ + char *p; + + p = malloc(SIZE); + if (!p) { + fprintf(stderr, "MALLOC FAILED: %s:%d\n", __FILE__, __LINE__); + return; + } + + memset(p, 42, SIZE); + + if (p[SIZE - 1] != 42) + fprintf(stderr, "MEMSET FAILED: %s:%d\n", __FILE__, __LINE__); + + free(p); +} + +void recursive_stack(int i) +{ + char buf[1024]; + if (i > 0) + recursive_stack(i - 1); +} + +void testfunc_deepstack(void) +{ + char buf[1024]; + recursive_stack(7 * 1024); +} |
