From 3e288e2d25e7c62f69af1ae1ee315e28c9a5136f Mon Sep 17 00:00:00 2001 From: John Jenkins Date: Thu, 9 Jan 2014 14:07:10 -0600 Subject: [PATCH] Added LP definition template to reduce boilerplate for writing LPs --- src/Makefile.subdir | 13 ++ src/util/templates/README.txt | 7 + src/util/templates/lp_template.c | 190 ++++++++++++++++++++ src/util/templates/lp_template.h | 26 +++ src/util/templates/lp_template_dummy_main.c | 21 +++ 5 files changed, 257 insertions(+) create mode 100644 src/util/templates/README.txt create mode 100644 src/util/templates/lp_template.c create mode 100644 src/util/templates/lp_template.h create mode 100644 src/util/templates/lp_template_dummy_main.c diff --git a/src/Makefile.subdir b/src/Makefile.subdir index 98f80bc..e933399 100644 --- a/src/Makefile.subdir +++ b/src/Makefile.subdir @@ -96,3 +96,16 @@ src_libcodes_base_a_SOURCES = \ src/workload/codes-workload-method.h \ src/workload/codes-bgp-io-wrkld.c \ src/workload/test-workload-method.c + +# stealth testing of the template code (actual test is not run, just compiled as +# a program - Make error signifies test failure) +check_PROGRAMS += src/util/templates/lp_template_dummy_main +testlib_template = src/libcodes-base.a +src_util_templates_lp_template_dummy_main_CFLAGS = -Wno-unused-function +src_util_templates_lp_template_dummy_main_LDADD = $(testlib_template) ${ROSS_LIBS} +src_util_templates_lp_template_dummy_main_LDFLAGS = ${ROSS_LDFLAGS} +src_util_templates_lp_template_dummy_main_SOURCES = \ + src/util/templates/lp_template_dummy_main.c \ + src/util/templates/lp_template.c \ + src/util/templates/lp_template.h +# get rid of annoying unused function in template diff --git a/src/util/templates/README.txt b/src/util/templates/README.txt new file mode 100644 index 0000000..7e8c587 --- /dev/null +++ b/src/util/templates/README.txt @@ -0,0 +1,7 @@ +These contain LP definition templates, with the hope that you can copy/paste +and substitute template with your LP's name. At the moment, they are integrated +into the build system to keep them from being out of date, but aren't actually +linked into any libraries + +'make tests' will compile the dummy main program along with the templates +themselves. diff --git a/src/util/templates/lp_template.c b/src/util/templates/lp_template.c new file mode 100644 index 0000000..010909b --- /dev/null +++ b/src/util/templates/lp_template.c @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2014 University of Chicago. + * See COPYRIGHT notice in top-level directory. + * + */ + +/* This is meant to be a template file to use when developing new LPs. + * Roughly follows the format of the existing LPs in the CODES repos */ + +#include "lp_template.h" +#include "codes/codes_mapping.h" +#include "codes/lp-type-lookup.h" +#include "codes/jenkins-hash.h" +#include "codes/codes.h" + +/**** BEGIN SIMULATION DATA STRUCTURES ****/ + +static int template_magic; /* use this as sanity check on events */ + +typedef struct template_state template_state; +typedef struct template_msg template_msg; + +/* event types */ +enum template_event +{ + TEMPLATE_A, + TEMPLATE_B, +}; + +struct template_state { +}; + +struct template_msg { + enum template_event event_type; + int magic; +}; + +/**** END SIMULATION DATA STRUCTURES ****/ + +/**** BEGIN LP, EVENT PROCESSING FUNCTION DECLS ****/ + +/* ROSS LP processing functions */ +static void template_lp_init( + template_state * ns, + tw_lp * lp); +static void template_event_handler( + template_state * ns, + tw_bf * b, + template_msg * m, + tw_lp * lp); +static void template_rev_handler( + template_state * ns, + tw_bf * b, + template_msg * m, + tw_lp * lp); +static void template_finalize( + template_state * ns, + tw_lp * lp); + +/* event type handlers */ +static void handle_template_a( + template_state * ns, + template_msg * m, + tw_lp * lp); +static void handle_template_b( + template_state * ns, + template_msg * m, + tw_lp * lp); +static void handle_template_a_rev( + template_state * ns, + template_msg * m, + tw_lp * lp); +static void handle_template_b_rev( + template_state * ns, + template_msg * m, + tw_lp * lp); + +/* ROSS function pointer table for this LP */ +tw_lptype template_lp = { + (init_f) template_init, + (event_f) template_event_handler, + (revent_f) template_rev_handler, + (final_f) template_finalize, + (map_f) codes_mapping, + sizeof(template_state), +}; + +/**** END LP, EVENT PROCESSING FUNCTION DECLS ****/ + +/**** BEGIN IMPLEMENTATIONS ****/ + +void template_init(){ + uint32_t h1=0, h2=0; + + bj_hashlittle2("template", strlen("template"), &h1, &h2); + template_magic = h1+h2; + + lp_type_register("template", &template_lp); +} + +void template_lp_init( + template_state * ns, + tw_lp * lp){ + /* Fill me in... */ +} + +void template_event_handler( + template_state * ns, + tw_bf * b, + template_msg * m, + tw_lp * lp){ + assert(m->magic == template_magic); + + switch (m->event_type){ + case TEMPLATE_A: + handle_template_a(ns, m, lp); + break; + case TEMPLATE_B: + handle_template_b(ns, m, lp); + break; + /* ... */ + default: + assert(!"template event type not known"); + break; + } +} + +void template_rev_handler( + template_state * ns, + tw_bf * b, + template_msg * m, + tw_lp * lp){ + assert(m->magic == template_magic); + + switch (m->event_type){ + case TEMPLATE_A: + handle_template_a_rev(ns, m, lp); + break; + case TEMPLATE_B: + handle_template_b_rev(ns, m, lp); + break; + /* ... */ + default: + assert(!"template event type not known"); + break; + } +} + +void template_finalize( + template_state * ns, + tw_lp * lp){ + /* Fill me in... */ +} + +/* event type handlers */ +void handle_template_a( + template_state * ns, + template_msg * m, + tw_lp * lp){ + +} +void handle_template_b( + template_state * ns, + template_msg * m, + tw_lp * lp){ + +} +void handle_template_a_rev( + template_state * ns, + template_msg * m, + tw_lp * lp){ + +} +void handle_template_b_rev( + template_state * ns, + template_msg * m, + tw_lp * lp){ + +} + +/**** END IMPLEMENTATIONS ****/ + +/* + * Local variables: + * c-indent-level: 4 + * c-basic-offset: 4 + * End: + * + * vim: ft=c ts=8 sts=4 sw=4 expandtab + */ diff --git a/src/util/templates/lp_template.h b/src/util/templates/lp_template.h new file mode 100644 index 0000000..46d1728 --- /dev/null +++ b/src/util/templates/lp_template.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2014 University of Chicago. + * See COPYRIGHT notice in top-level directory. + * + */ + +#ifndef LP_TEMPLATE_H + +#define LP_TEMPLATE_H + +#include + +void template_init(); + +#endif /* end of include guard: LP_TEMPLATE_H */ + + +/* + * Local variables: + * c-indent-level: 4 + * c-basic-offset: 4 + * End: + * + * vim: ft=c ts=8 sts=4 sw=4 expandtab + */ + diff --git a/src/util/templates/lp_template_dummy_main.c b/src/util/templates/lp_template_dummy_main.c new file mode 100644 index 0000000..6986ee3 --- /dev/null +++ b/src/util/templates/lp_template_dummy_main.c @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2014 University of Chicago. + * See COPYRIGHT notice in top-level directory. + * + */ + +#include "lp_template.h" +int main(int argc, char *argv[]) +{ + /* won't actually be called - just here to make linker happy */ + template_init(); + return 0; +} +/* + * Local variables: + * c-indent-level: 4 + * c-basic-offset: 4 + * End: + * + * vim: ft=c ts=8 sts=4 sw=4 expandtab + */ -- 2.26.2