From b52581ce89bc80938f497bfb61969466e0f2610e Mon Sep 17 00:00:00 2001 From: Christopher Kenna Date: Sun, 8 Apr 2012 18:25:23 -0400 Subject: test color program --- .gitignore | 1 + Makefile | 7 +++-- bin/color.c | 50 ++++++++++++++++++++++++++++++ bin/colortest.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/color.h | 7 +++++ 5 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 bin/color.c create mode 100644 bin/colortest.c create mode 100644 include/color.h diff --git a/.gitignore b/.gitignore index 7f419d0..255ac82 100644 --- a/.gitignore +++ b/.gitignore @@ -22,6 +22,7 @@ showst rtspin cycles measure_syscall +colortest # build system files .config diff --git a/Makefile b/Makefile index 2109a45..582e9a7 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ AR := ${CROSS_COMPILE}${AR} all = lib ${rt-apps} rt-apps = cycles base_task rt_launch rtspin release_ts measure_syscall \ - base_mt_task runtests + base_mt_task runtests colortest .PHONY: all lib clean dump-config TAGS tags cscope help @@ -216,12 +216,15 @@ obj-release_ts = release_ts.o obj-measure_syscall = null_call.o lib-measure_syscall = -lm +obj-colortest = colortest.o color.o +lib-colortest = -static + # ############################################################################## # Build everything that depends on liblitmus. .SECONDEXPANSION: ${rt-apps}: $${obj-$$@} liblitmus.a - $(CC) -o $@ $(LDFLAGS) ${ldf-$@} $(filter-out liblitmus.a,$+) $(LOADLIBS) $(LDLIBS) ${lib-$@} ${liblitmus-flags} + $(CC) -o $@ $(LDFLAGS) ${ldf-$@} $(filter-out liblitmus.a,$+) $(LOADLIBS) $(LDLIBS) ${liblitmus-flags} ${lib-$@} # ############################################################################## # Dependency resolution. diff --git a/bin/color.c b/bin/color.c new file mode 100644 index 0000000..2ec97a4 --- /dev/null +++ b/bin/color.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +#include + +#include "color.h" + +#define LITMUS_COLOR_ALLOC "/dev/litmus/color_alloc" +#define LITMUS_COLOR_CTRL "/dev/litmus/color_ctrl" + +static int map_file(const char* filename, void **addr, size_t size) +{ + int error = 0; + int fd; + + if (size > 0) { + fd = open(filename, O_RDWR); + if (fd >= 0) { + *addr = mmap(NULL, size, + PROT_READ | PROT_WRITE, + MAP_PRIVATE, + fd, 0); + if (*addr == MAP_FAILED) + error = -1; + close(fd); + } else + error = fd; + } else + *addr = NULL; + return error; +} + +int map_color_ctrl(void **addr) +{ + return map_file(LITMUS_COLOR_CTRL, addr, PAGE_SIZE); +} + +void* color_malloc(size_t size) +{ + int err; + void *mem; + + err = map_file(LITMUS_COLOR_ALLOC, &mem, size); + if (err) + mem = NULL; + return mem; +} diff --git a/bin/colortest.c b/bin/colortest.c new file mode 100644 index 0000000..db552be --- /dev/null +++ b/bin/colortest.c @@ -0,0 +1,94 @@ +#include +#include +#include + +#include + +#include "color.h" + +#define PAGES_TO_ALLOC 5 + +static void* color_malloc_or_exit(size_t size) +{ + void *mem; + mem = color_malloc(size); + if (!mem) { + fprintf(stderr, "could not allocate memory.\n"); + exit(EXIT_FAILURE); + } + return mem; +} + +static void check_memory(const char *start, const size_t len, const char expect) +{ + const char *cur = start; + int i; + + for (i = 0; i < len; i++, cur++) { + if (expect != *cur) { + fprintf(stderr, "inconsistent memory: " + "start:0x%p i:%d cur:0x%p " + "expect:0x%x have:0x%x\n", + start, i, cur, + expect, *cur); + exit(EXIT_FAILURE); + } + } +} + +void setup_pages(struct color_ctrl_page *ctrl, uint32_t start, + uint32_t nr_pages, uint32_t stride) +{ + int i; + for (i = 0; i < nr_pages; i++, start += stride) + ctrl->colors[i] = start; +} + +int main(int argc, char **argv) +{ + const char VAL1 = 0x0e; /* 0b00001110 */ + const char VAL2 = 0x3c; /* 0b00111100 */ + struct color_ctrl_page *color_ctrl; + char *mem1, *mem2; + int err, i; + + err = map_color_ctrl((void**)&color_ctrl); + if (err) { + fprintf(stderr, "Could not map color control interface.\n"); + exit(EXIT_FAILURE); + } + + setup_pages(color_ctrl, 0, PAGES_TO_ALLOC, 1); + mem1 = color_malloc_or_exit(PAGE_SIZE * PAGES_TO_ALLOC); + setup_pages(color_ctrl, PAGES_TO_ALLOC, PAGES_TO_ALLOC, 2); + mem2 = color_malloc_or_exit(PAGE_SIZE * PAGES_TO_ALLOC); + + /* zero it */ + for (i = 0; i < PAGE_SIZE * PAGES_TO_ALLOC; i++) { + mem1[i] = mem2[i] = 0; + } + + printf("checking both arenas are zero\n"); + check_memory(mem1, PAGE_SIZE * PAGES_TO_ALLOC, 0); + check_memory(mem2, PAGE_SIZE * PAGES_TO_ALLOC, 0); + + printf("writing to mem1\n"); + for (i = 0; i < PAGE_SIZE * PAGES_TO_ALLOC; i++) { + mem1[i] = VAL1; + } + + printf("checking mem1 for value and mem2 for zero\n"); + check_memory(mem1, PAGE_SIZE * PAGES_TO_ALLOC, VAL1); + check_memory(mem2, PAGE_SIZE * PAGES_TO_ALLOC, 0); + + printf("writing to mem2\n"); + for (i = 0; i < PAGE_SIZE * PAGES_TO_ALLOC; i++) { + mem2[i] = VAL2; + } + + printf("checking mem1 and mem2 for their values\n"); + check_memory(mem1, PAGE_SIZE * PAGES_TO_ALLOC, VAL1); + check_memory(mem2, PAGE_SIZE * PAGES_TO_ALLOC, VAL2); + + exit(EXIT_SUCCESS); +} diff --git a/include/color.h b/include/color.h new file mode 100644 index 0000000..50707fd --- /dev/null +++ b/include/color.h @@ -0,0 +1,7 @@ +#ifndef COLOR_H +#define COLOR_H + +int map_color_ctrl(void **); +void* color_malloc(size_t); + +#endif -- cgit v1.2.2