aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Kenna <cjk@cs.unc.edu>2012-04-08 18:25:23 -0400
committerChristopher Kenna <cjk@cs.unc.edu>2012-04-08 18:25:23 -0400
commitb52581ce89bc80938f497bfb61969466e0f2610e (patch)
treef08005897441c18834fdf68af1cb1b3efaaad2df
parent8000ef44f4a82d845d5139cebfcac047ee291433 (diff)
test color program
-rw-r--r--.gitignore1
-rw-r--r--Makefile7
-rw-r--r--bin/color.c50
-rw-r--r--bin/colortest.c94
-rw-r--r--include/color.h7
5 files changed, 157 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 7f419d0..255ac82 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,6 +22,7 @@ showst
22rtspin 22rtspin
23cycles 23cycles
24measure_syscall 24measure_syscall
25colortest
25 26
26# build system files 27# build system files
27.config 28.config
diff --git a/Makefile b/Makefile
index 2109a45..582e9a7 100644
--- a/Makefile
+++ b/Makefile
@@ -71,7 +71,7 @@ AR := ${CROSS_COMPILE}${AR}
71 71
72all = lib ${rt-apps} 72all = lib ${rt-apps}
73rt-apps = cycles base_task rt_launch rtspin release_ts measure_syscall \ 73rt-apps = cycles base_task rt_launch rtspin release_ts measure_syscall \
74 base_mt_task runtests 74 base_mt_task runtests colortest
75 75
76.PHONY: all lib clean dump-config TAGS tags cscope help 76.PHONY: all lib clean dump-config TAGS tags cscope help
77 77
@@ -216,12 +216,15 @@ obj-release_ts = release_ts.o
216obj-measure_syscall = null_call.o 216obj-measure_syscall = null_call.o
217lib-measure_syscall = -lm 217lib-measure_syscall = -lm
218 218
219obj-colortest = colortest.o color.o
220lib-colortest = -static
221
219# ############################################################################## 222# ##############################################################################
220# Build everything that depends on liblitmus. 223# Build everything that depends on liblitmus.
221 224
222.SECONDEXPANSION: 225.SECONDEXPANSION:
223${rt-apps}: $${obj-$$@} liblitmus.a 226${rt-apps}: $${obj-$$@} liblitmus.a
224 $(CC) -o $@ $(LDFLAGS) ${ldf-$@} $(filter-out liblitmus.a,$+) $(LOADLIBS) $(LDLIBS) ${lib-$@} ${liblitmus-flags} 227 $(CC) -o $@ $(LDFLAGS) ${ldf-$@} $(filter-out liblitmus.a,$+) $(LOADLIBS) $(LDLIBS) ${liblitmus-flags} ${lib-$@}
225 228
226# ############################################################################## 229# ##############################################################################
227# Dependency resolution. 230# 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 @@
1#include <stdlib.h>
2#include <stdint.h>
3#include <sys/mman.h>
4#include <sys/fcntl.h>
5#include <unistd.h>
6
7#include <litmus/rt_param.h>
8
9#include "color.h"
10
11#define LITMUS_COLOR_ALLOC "/dev/litmus/color_alloc"
12#define LITMUS_COLOR_CTRL "/dev/litmus/color_ctrl"
13
14static int map_file(const char* filename, void **addr, size_t size)
15{
16 int error = 0;
17 int fd;
18
19 if (size > 0) {
20 fd = open(filename, O_RDWR);
21 if (fd >= 0) {
22 *addr = mmap(NULL, size,
23 PROT_READ | PROT_WRITE,
24 MAP_PRIVATE,
25 fd, 0);
26 if (*addr == MAP_FAILED)
27 error = -1;
28 close(fd);
29 } else
30 error = fd;
31 } else
32 *addr = NULL;
33 return error;
34}
35
36int map_color_ctrl(void **addr)
37{
38 return map_file(LITMUS_COLOR_CTRL, addr, PAGE_SIZE);
39}
40
41void* color_malloc(size_t size)
42{
43 int err;
44 void *mem;
45
46 err = map_file(LITMUS_COLOR_ALLOC, &mem, size);
47 if (err)
48 mem = NULL;
49 return mem;
50}
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 @@
1#include <stdlib.h>
2#include <stdio.h>
3#include <stdint.h>
4
5#include <litmus/rt_param.h>
6
7#include "color.h"
8
9#define PAGES_TO_ALLOC 5
10
11static void* color_malloc_or_exit(size_t size)
12{
13 void *mem;
14 mem = color_malloc(size);
15 if (!mem) {
16 fprintf(stderr, "could not allocate memory.\n");
17 exit(EXIT_FAILURE);
18 }
19 return mem;
20}
21
22static void check_memory(const char *start, const size_t len, const char expect)
23{
24 const char *cur = start;
25 int i;
26
27 for (i = 0; i < len; i++, cur++) {
28 if (expect != *cur) {
29 fprintf(stderr, "inconsistent memory: "
30 "start:0x%p i:%d cur:0x%p "
31 "expect:0x%x have:0x%x\n",
32 start, i, cur,
33 expect, *cur);
34 exit(EXIT_FAILURE);
35 }
36 }
37}
38
39void setup_pages(struct color_ctrl_page *ctrl, uint32_t start,
40 uint32_t nr_pages, uint32_t stride)
41{
42 int i;
43 for (i = 0; i < nr_pages; i++, start += stride)
44 ctrl->colors[i] = start;
45}
46
47int main(int argc, char **argv)
48{
49 const char VAL1 = 0x0e; /* 0b00001110 */
50 const char VAL2 = 0x3c; /* 0b00111100 */
51 struct color_ctrl_page *color_ctrl;
52 char *mem1, *mem2;
53 int err, i;
54
55 err = map_color_ctrl((void**)&color_ctrl);
56 if (err) {
57 fprintf(stderr, "Could not map color control interface.\n");
58 exit(EXIT_FAILURE);
59 }
60
61 setup_pages(color_ctrl, 0, PAGES_TO_ALLOC, 1);
62 mem1 = color_malloc_or_exit(PAGE_SIZE * PAGES_TO_ALLOC);
63 setup_pages(color_ctrl, PAGES_TO_ALLOC, PAGES_TO_ALLOC, 2);
64 mem2 = color_malloc_or_exit(PAGE_SIZE * PAGES_TO_ALLOC);
65
66 /* zero it */
67 for (i = 0; i < PAGE_SIZE * PAGES_TO_ALLOC; i++) {
68 mem1[i] = mem2[i] = 0;
69 }
70
71 printf("checking both arenas are zero\n");
72 check_memory(mem1, PAGE_SIZE * PAGES_TO_ALLOC, 0);
73 check_memory(mem2, PAGE_SIZE * PAGES_TO_ALLOC, 0);
74
75 printf("writing to mem1\n");
76 for (i = 0; i < PAGE_SIZE * PAGES_TO_ALLOC; i++) {
77 mem1[i] = VAL1;
78 }
79
80 printf("checking mem1 for value and mem2 for zero\n");
81 check_memory(mem1, PAGE_SIZE * PAGES_TO_ALLOC, VAL1);
82 check_memory(mem2, PAGE_SIZE * PAGES_TO_ALLOC, 0);
83
84 printf("writing to mem2\n");
85 for (i = 0; i < PAGE_SIZE * PAGES_TO_ALLOC; i++) {
86 mem2[i] = VAL2;
87 }
88
89 printf("checking mem1 and mem2 for their values\n");
90 check_memory(mem1, PAGE_SIZE * PAGES_TO_ALLOC, VAL1);
91 check_memory(mem2, PAGE_SIZE * PAGES_TO_ALLOC, VAL2);
92
93 exit(EXIT_SUCCESS);
94}
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 @@
1#ifndef COLOR_H
2#define COLOR_H
3
4int map_color_ctrl(void **);
5void* color_malloc(size_t);
6
7#endif