summaryrefslogtreecommitdiff
path: root/schulung_tools/rtex/rt.c
diff options
context:
space:
mode:
authorJohn Ogness <john.ogness@linutronix.de>2017-12-19 10:59:40 +0100
committerJohn Ogness <john.ogness@linutronix.de>2017-12-19 10:59:40 +0100
commit270520b4a2eac8725c8575c3180964289722e191 (patch)
treed9512cd96d0c52e14293c3f8bc19fd168ee14025 /schulung_tools/rtex/rt.c
parent27209bb802048f4803d9cd9a5c2f99d613986446 (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/rt.c')
-rw-r--r--schulung_tools/rtex/rt.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/schulung_tools/rtex/rt.c b/schulung_tools/rtex/rt.c
new file mode 100644
index 0000000..86a6506
--- /dev/null
+++ b/schulung_tools/rtex/rt.c
@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <malloc.h>
+#include <sys/mman.h>
+
+extern void timestamp(const char *msg, int val);
+extern void testfunc_malloc(void);
+extern void testfunc_deepstack(void);
+
+void prefault_stack(void)
+{
+#define STACK_SIZE (7680 * 1024) /* 7.5 MiB */
+ char buf[STACK_SIZE];
+ long pagesize;
+ int i;
+
+ pagesize = sysconf(_SC_PAGESIZE);
+
+ for (i = 0; i < STACK_SIZE; i += pagesize)
+ buf[i] = 0;
+}
+
+void prefault_heap(void)
+{
+#define HEAP_SIZE (20 * 1024 * 1024) /* 20 MiB */
+ long pagesize;
+ char *buf;
+ int i;
+
+ pagesize = sysconf(_SC_PAGESIZE);
+
+ buf = malloc(HEAP_SIZE);
+ if (!buf) {
+ fprintf(stderr, "MALLOC FAILED: %s:%d\n", __FILE__, __LINE__);
+ return;
+ }
+
+ for (i = 0; i < HEAP_SIZE; i += pagesize)
+ buf[i] = 0;
+
+ free(buf);
+}
+
+void setup_rt(unsigned int opts)
+{
+ if (opts & 0x1) {
+ if (mallopt(M_TRIM_THRESHOLD, -1) == 0) {
+ fprintf(stderr, "MALLOPT FAILED: %s:%d\n",
+ __FILE__, __LINE__);
+ }
+
+ if (mallopt(M_MMAP_MAX, 0) == 0) {
+ fprintf(stderr, "MALLOPT FAILED: %s:%d\n",
+ __FILE__, __LINE__);
+ }
+ }
+
+ if (opts & 0x2) {
+ if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
+ fprintf(stderr, "MLOCKALL FAILED: %s:%d\n",
+ __FILE__, __LINE__);
+ }
+ }
+
+ if (opts & 0x4)
+ prefault_stack();
+
+ if (opts & 0x8)
+ prefault_heap();
+}
+