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/matrix/main.c | 91 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 schulung_tools/matrix/main.c (limited to 'schulung_tools/matrix/main.c') diff --git a/schulung_tools/matrix/main.c b/schulung_tools/matrix/main.c new file mode 100644 index 0000000..9a6f2fa --- /dev/null +++ b/schulung_tools/matrix/main.c @@ -0,0 +1,91 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +/* matrix dimension */ +static size_t dim; + +/* options */ +static struct option long_opts[] = { + { "dim", required_argument, NULL, 'd' }, + { "1", no_argument, NULL, '1' }, + { "2", no_argument, NULL, '2' }, + { NULL, 0, NULL, 0 }, +}; + +static int visit_every_element_1(const int *matrix, size_t dim) +{ + int sum = 0; + + /* row by row */ + for (int i = 0; i < dim; ++i) { + for (int j = 0; j < dim; ++j) { + sum += matrix[i * dim + j]; + } + } + + return sum; +} + +static int visit_every_element_2(const int *matrix, size_t dim) +{ + int sum = 0; + + /* column by column */ + for (int j = 0; j < dim; ++j) { + for (int i = 0; i < dim; ++i) { + sum += matrix[i * dim + j]; + } + } + + return sum; +} + +static inline void print_usage_and_die(void) +{ + fprintf(stderr, "usage: matrix [-1|-2] [-d ]\n"); + exit(EXIT_FAILURE); +} + +int main(int argc, char **argv) +{ + int *matrix, sum; + char *end; + int c, choice = 1; + + dim = 10000; + while ((c = getopt_long(argc, argv, "d:12", long_opts, NULL)) != -1) { + switch (c) { + case 'd': + dim = strtoull(optarg, &end, 10); + if (end == optarg || *end != '\0' || errno == ERANGE) { + fprintf(stderr, "given dim is not valid\n"); + return EXIT_FAILURE; + } + break; + case '1': + choice = 1; + break; + case '2': + choice = 2; + break; + default: + print_usage_and_die(); + } + } + + matrix = (int *)calloc(dim * dim, sizeof(int)); + if (!matrix) { + perror("malloc() failed"); + return EXIT_FAILURE; + } + + sum = choice == 1 ? visit_every_element_1(matrix, dim) : + visit_every_element_2(matrix, dim); + printf("Bogus result: %d\n", sum); + + return EXIT_SUCCESS; +} -- cgit v1.2.3