# HG changeset patch # User George Dunlap # Date 1255533146 -3600 # Node ID 1d7310217c5aa9e1a64fc72a6a19ae98ddc2643d # Parent ec2d50e4143772443354f63b50d3980304de4901 Add command-line options diff -r ec2d50e41437 -r 1d7310217c5a Makefile --- a/Makefile Tue Oct 13 17:29:50 2009 +0100 +++ b/Makefile Wed Oct 14 16:12:26 2009 +0100 @@ -10,7 +10,7 @@ CFLAGS += -Werror BIN = simulator -HDRS = list.h workload.h sim.h stats.h +HDRS = list.h workload.h sim.h stats.h options.h all: $(BIN) @@ -21,5 +21,5 @@ clean: %.o: %.c $(HDRS) Makefile $(CC) $(CFLAGS) -c -o $@ $< -simulator: simulator.o workloads.o sched_rr.o stats.o +simulator: simulator.o workloads.o sched_rr.o stats.o options.o $(CC) $(CFLAGS) -o $@ $^ \ No newline at end of file diff -r ec2d50e41437 -r 1d7310217c5a options.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/options.c Wed Oct 14 16:12:26 2009 +0100 @@ -0,0 +1,173 @@ +#define _XOPEN_SOURCE 600 +#include +#include +#include +#include + +#include "sim.h" +#include "workload.h" +#include "sched.h" +#include "options.h" + +extern int default_scheduler; + +struct options opt = { + .time_limit = 100000, + .pcpu_count = 2, + .workload = NULL, + .scheduler = NULL, +}; + +enum { + OPT_NULL = 0, + OPT_WORKLOAD, + OPT_SCHEDULER, + OPT_PCPU_COUNT, + OPT_TIME_LIMIT, + OPT_LIST_WORKLOADS, + OPT_LIST_SCHEDULERS +}; + +const struct argp_option cmd_opts[] = { + { .name = "workload", + .key = OPT_WORKLOAD, + .arg="workload-name", + .doc = "Synthetic workload to run.", }, + { .name = "scheduler", + .arg="workload-name", + .key = OPT_SCHEDULER, + .doc = "Chose scheduler to run.", }, + { .name = "pcpus", + .arg="n", + .key = OPT_PCPU_COUNT, + .doc = "Number of pcpus to simulate. Defaults to 2.", }, + { .name = "time", + .arg="n", + .key = OPT_TIME_LIMIT, + .doc = "How long to simulate. Default 100000", }, + { .name = "list-schedulers", + .key = OPT_LIST_SCHEDULERS, + .doc = "List available schedulers. Simulation won't run if this option is set.", }, + { .name = "list-workloads", + .key = OPT_LIST_WORKLOADS, + .doc = "List available workloads. Simulation won't run if this option is set.", }, + + { 0 }, +}; + +error_t cmd_parser(int key, char *arg, struct argp_state *state) +{ + static int dont_run = 0; + + switch (key) + { + case OPT_LIST_SCHEDULERS: + { + int i; + printf("Schedulers:\n"); + for ( i=0; schedulers[i]; i++) + printf(" %s\n", schedulers[i]->name); + dont_run=1; + break; + } + case OPT_LIST_WORKLOADS: + { + int i; + printf("Workloads:\n"); + for ( i=0; builtin_workloads[i].name; i++) + printf(" %s\n", builtin_workloads[i].name); + dont_run=1; + break; + } + case OPT_WORKLOAD: + { + int i; + + for ( i=0; builtin_workloads[i].name; i++) + { + if ( !strcmp(builtin_workloads[i].name, arg) ) + break; + } + if ( !builtin_workloads[i].name ) + { + fprintf(stderr, "Unknown workload: %s\n", + arg); + exit(1); + } + opt.workload = builtin_workloads + i; + } + break; + case OPT_SCHEDULER: + { + int i; + + for ( i=0; schedulers[i]; i++) + { + if ( !strcmp(schedulers[i]->name, arg) ) + break; + } + if ( !schedulers[i] ) + { + fprintf(stderr, "Unknown scheduler: %s\n", + arg); + exit(1); + } + opt.scheduler = schedulers[i]; + } + break; + case OPT_PCPU_COUNT: + { + char *inval; + + opt.pcpu_count=(int)strtol(arg, &inval, 0); + if ( inval == arg ) + argp_usage(state); + } + break; + case OPT_TIME_LIMIT: + { + char *inval; + + opt.time_limit=(int)strtol(arg, &inval, 0); + if ( inval == arg ) + argp_usage(state); + } + break; + case ARGP_KEY_ARG: + { + argp_usage(state); + } + break; + case ARGP_KEY_END: + { + if ( dont_run ) + exit(0); + + if ( !opt.workload ) + opt.workload = builtin_workloads+default_workload; + + if ( !opt.scheduler ) + opt.scheduler = schedulers[default_scheduler]; + } + break; + + default: + return ARGP_ERR_UNKNOWN; + } + + return 0; +} + +const struct argp parser_def = { + .options = cmd_opts, + .parser = cmd_parser, + .args_doc = "", + .doc = "", +}; + +const char *argp_program_version = "Scheduler simulator"; +const char *argp_program_bug_address = "George Dunlap "; + +void parse_options(int argc, char * argv[]) { + argp_parse(&parser_def, argc, argv, 0, NULL, NULL); +} diff -r ec2d50e41437 -r 1d7310217c5a options.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/options.h Wed Oct 14 16:12:26 2009 +0100 @@ -0,0 +1,13 @@ +#ifndef _OPTIONS_H +#define _OPTIONS_H +struct options { + int time_limit; + int pcpu_count; + const struct workload * workload; + const struct scheduler * scheduler; +} opt; + +extern struct options opt; +void parse_options(int argc, char * argv[]); + +#endif diff -r ec2d50e41437 -r 1d7310217c5a sched_rr.c --- a/sched_rr.c Tue Oct 13 17:29:50 2009 +0100 +++ b/sched_rr.c Wed Oct 14 16:12:26 2009 +0100 @@ -50,10 +50,13 @@ void sched_rr_vm_init(int vid) } -void sched_rr_wake(int time, struct vm * v) +void sched_rr_wake(int time, int vid) { + struct vm *v; struct sched_vm *svm; + v = vm_from_vid(vid); + printf("%s: time %d vid %d\n", __func__, time, v->vid); diff -r ec2d50e41437 -r 1d7310217c5a sim.h --- a/sim.h Tue Oct 13 17:29:50 2009 +0100 +++ b/sim.h Wed Oct 14 16:12:26 2009 +0100 @@ -3,6 +3,7 @@ #include "stats.h" #include "workload.h" +#include "sched.h" enum runstate { RUNSTATE_RUNNING, @@ -44,18 +45,6 @@ struct vm }; -struct sched_ops { - void (*sched_init)(void); - void (*vm_init)(int vid); - void (*wake)(int time, struct vm* v); - struct vm* (*schedule)(int time, int pid); -}; - -struct scheduler { - char *name; - struct sched_ops ops; -}; - #define MAX_PCPU 16 struct global_pcpu_data { int count, idle; diff -r ec2d50e41437 -r 1d7310217c5a simulator.c --- a/simulator.c Tue Oct 13 17:29:50 2009 +0100 +++ b/simulator.c Wed Oct 14 16:12:26 2009 +0100 @@ -8,6 +8,8 @@ #include "list.h" #include "sim.h" #include "workload.h" +#include "sched.h" +#include "options.h" FILE *warn; @@ -59,19 +61,10 @@ int default_scheduler = 0; struct scheduler *schedulers[] = { &sched_rr, + NULL }; /* Options */ -struct { - int time_limit; - int pcpu_count; - const struct workload * workload; - const struct scheduler * scheduler; -} opt = { - .time_limit = 100000, - .pcpu_count = 2, - .workload = NULL, -}; struct global_pcpu_data P; @@ -350,7 +343,7 @@ void simulate(void) struct vm *v = vm_from_vid(evt.param); ASSERT(v->processor == -1); sim_runstate_change(sim.now, v, RUNSTATE_RUNNABLE); - sim.sched_ops->wake(sim.now, v); + sim.sched_ops->wake(sim.now, v->vid); } break; case EVT_BLOCK: @@ -507,12 +500,8 @@ int main(int argc, char * argv[]) { warn = stdout; - /* Read opts, config file? */ - if ( !opt.workload ) - opt.workload = builtin_workloads+default_workload; + parse_options(argc, argv); - if ( !opt.scheduler ) - opt.scheduler = schedulers[default_scheduler]; /* Setup simulation */ init(); diff -r ec2d50e41437 -r 1d7310217c5a workloads.c --- a/workloads.c Tue Oct 13 17:29:50 2009 +0100 +++ b/workloads.c Wed Oct 14 16:12:26 2009 +0100 @@ -1,3 +1,4 @@ +#include #include "workload.h" const int default_workload = 0; @@ -87,4 +88,6 @@ struct workload builtin_workloads[] = }, } }, + { .name=NULL } + };