gdunlap/sched-sim.hg

changeset 2:1d7310217c5a

Add command-line options
author George Dunlap <gdunlap@xensource.com>
date Wed Oct 14 16:12:26 2009 +0100 (2009-10-14)
parents ec2d50e41437
children d957708efa86
files Makefile options.c options.h sched_rr.c sim.h simulator.c workloads.c
line diff
     1.1 --- a/Makefile	Tue Oct 13 17:29:50 2009 +0100
     1.2 +++ b/Makefile	Wed Oct 14 16:12:26 2009 +0100
     1.3 @@ -10,7 +10,7 @@ CFLAGS  += -Werror
     1.4  
     1.5  BIN      = simulator
     1.6  
     1.7 -HDRS = list.h workload.h sim.h stats.h
     1.8 +HDRS = list.h workload.h sim.h stats.h options.h
     1.9  
    1.10  all: $(BIN)
    1.11  
    1.12 @@ -21,5 +21,5 @@ clean:
    1.13  %.o: %.c $(HDRS) Makefile
    1.14  	$(CC) $(CFLAGS) -c -o $@ $<
    1.15  
    1.16 -simulator: simulator.o workloads.o sched_rr.o stats.o
    1.17 +simulator: simulator.o workloads.o sched_rr.o stats.o options.o
    1.18  	$(CC) $(CFLAGS) -o $@ $^
    1.19 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/options.c	Wed Oct 14 16:12:26 2009 +0100
     2.3 @@ -0,0 +1,173 @@
     2.4 +#define _XOPEN_SOURCE 600
     2.5 +#include <stdio.h>
     2.6 +#include <stdlib.h>
     2.7 +#include <string.h>
     2.8 +#include <argp.h>
     2.9 +
    2.10 +#include "sim.h"
    2.11 +#include "workload.h"
    2.12 +#include "sched.h"
    2.13 +#include "options.h"
    2.14 +
    2.15 +extern int default_scheduler;
    2.16 +
    2.17 +struct options opt = {
    2.18 +    .time_limit = 100000,
    2.19 +    .pcpu_count = 2,
    2.20 +    .workload = NULL,
    2.21 +    .scheduler = NULL,
    2.22 +};
    2.23 +
    2.24 +enum {
    2.25 +    OPT_NULL = 0,
    2.26 +    OPT_WORKLOAD,
    2.27 +    OPT_SCHEDULER,
    2.28 +    OPT_PCPU_COUNT,
    2.29 +    OPT_TIME_LIMIT,
    2.30 +    OPT_LIST_WORKLOADS,
    2.31 +    OPT_LIST_SCHEDULERS
    2.32 +};
    2.33 +
    2.34 +const struct argp_option cmd_opts[] =  {
    2.35 +    { .name = "workload",
    2.36 +      .key = OPT_WORKLOAD,
    2.37 +      .arg="workload-name",
    2.38 +      .doc = "Synthetic workload to run.", },
    2.39 +    { .name = "scheduler",
    2.40 +      .arg="workload-name",
    2.41 +      .key = OPT_SCHEDULER,
    2.42 +      .doc = "Chose scheduler to run.", },
    2.43 +    { .name = "pcpus",
    2.44 +      .arg="n",
    2.45 +      .key = OPT_PCPU_COUNT,
    2.46 +      .doc = "Number of pcpus to simulate.  Defaults to 2.", },
    2.47 +    { .name = "time",
    2.48 +      .arg="n",
    2.49 +      .key = OPT_TIME_LIMIT,
    2.50 +      .doc = "How long to simulate.  Default 100000", },
    2.51 +    { .name = "list-schedulers",
    2.52 +      .key = OPT_LIST_SCHEDULERS,
    2.53 +      .doc = "List available schedulers.  Simulation won't run if this option is set.", },
    2.54 +    { .name = "list-workloads",
    2.55 +      .key = OPT_LIST_WORKLOADS,
    2.56 +      .doc = "List available workloads.  Simulation won't run if this option is set.", },
    2.57 +
    2.58 +    { 0 },
    2.59 +};
    2.60 +
    2.61 +error_t cmd_parser(int key, char *arg, struct argp_state *state)
    2.62 +{
    2.63 +    static int dont_run = 0;
    2.64 +
    2.65 +    switch (key)
    2.66 +    {
    2.67 +    case OPT_LIST_SCHEDULERS:
    2.68 +    {
    2.69 +        int i;
    2.70 +        printf("Schedulers:\n");
    2.71 +        for ( i=0; schedulers[i]; i++)
    2.72 +            printf(" %s\n", schedulers[i]->name);
    2.73 +        dont_run=1;
    2.74 +        break;
    2.75 +    }   
    2.76 +    case OPT_LIST_WORKLOADS:
    2.77 +    {
    2.78 +        int i;
    2.79 +        printf("Workloads:\n");
    2.80 +        for ( i=0; builtin_workloads[i].name; i++)
    2.81 +            printf(" %s\n", builtin_workloads[i].name);
    2.82 +        dont_run=1;
    2.83 +        break;
    2.84 +    }   
    2.85 +    case OPT_WORKLOAD:
    2.86 +    {
    2.87 +        int i;
    2.88 +
    2.89 +        for ( i=0; builtin_workloads[i].name; i++)
    2.90 +        {
    2.91 +            if ( !strcmp(builtin_workloads[i].name, arg) )
    2.92 +                break;
    2.93 +        }
    2.94 +        if ( !builtin_workloads[i].name )
    2.95 +        {
    2.96 +            fprintf(stderr, "Unknown workload: %s\n",
    2.97 +                    arg);
    2.98 +            exit(1);
    2.99 +        }
   2.100 +        opt.workload = builtin_workloads + i;
   2.101 +    }
   2.102 +        break;
   2.103 +    case OPT_SCHEDULER:
   2.104 +    {
   2.105 +        int i;
   2.106 +
   2.107 +        for ( i=0; schedulers[i]; i++)
   2.108 +        {
   2.109 +            if ( !strcmp(schedulers[i]->name, arg) )
   2.110 +                break;
   2.111 +        }
   2.112 +        if ( !schedulers[i] )
   2.113 +        {
   2.114 +            fprintf(stderr, "Unknown scheduler: %s\n",
   2.115 +                    arg);
   2.116 +            exit(1);
   2.117 +        }
   2.118 +        opt.scheduler = schedulers[i];
   2.119 +    }
   2.120 +        break;
   2.121 +    case OPT_PCPU_COUNT:
   2.122 +    {
   2.123 +        char *inval;
   2.124 +
   2.125 +        opt.pcpu_count=(int)strtol(arg, &inval, 0);
   2.126 +        if ( inval == arg )
   2.127 +            argp_usage(state);
   2.128 +    }
   2.129 +        break;
   2.130 +    case OPT_TIME_LIMIT:
   2.131 +    {
   2.132 +        char *inval;
   2.133 +
   2.134 +        opt.time_limit=(int)strtol(arg, &inval, 0);
   2.135 +        if ( inval == arg )
   2.136 +            argp_usage(state);
   2.137 +    }
   2.138 +        break;
   2.139 +    case ARGP_KEY_ARG:
   2.140 +    {
   2.141 +        argp_usage(state);
   2.142 +    }
   2.143 +    break;
   2.144 +    case ARGP_KEY_END:
   2.145 +    {
   2.146 +        if ( dont_run )
   2.147 +            exit(0);
   2.148 +
   2.149 +        if ( !opt.workload )
   2.150 +            opt.workload = builtin_workloads+default_workload;
   2.151 +
   2.152 +        if ( !opt.scheduler )
   2.153 +            opt.scheduler = schedulers[default_scheduler];
   2.154 +    }
   2.155 +    break;
   2.156 +
   2.157 +    default:
   2.158 +        return ARGP_ERR_UNKNOWN;
   2.159 +    }
   2.160 +
   2.161 +    return 0;
   2.162 +}
   2.163 +
   2.164 +const struct argp parser_def = {
   2.165 +    .options = cmd_opts,
   2.166 +    .parser = cmd_parser,
   2.167 +    .args_doc = "",
   2.168 +    .doc = "",
   2.169 +};
   2.170 +
   2.171 +const char *argp_program_version = "Scheduler simulator";
   2.172 +const char *argp_program_bug_address = "George Dunlap <george.dunlap@eu.citrix.com>";
   2.173 +
   2.174 +void parse_options(int argc, char * argv[]) {
   2.175 +    argp_parse(&parser_def, argc, argv, 0, NULL, NULL);
   2.176 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/options.h	Wed Oct 14 16:12:26 2009 +0100
     3.3 @@ -0,0 +1,13 @@
     3.4 +#ifndef _OPTIONS_H
     3.5 +#define _OPTIONS_H
     3.6 +struct options {
     3.7 +    int time_limit;
     3.8 +    int pcpu_count;
     3.9 +    const struct workload * workload;
    3.10 +    const struct scheduler * scheduler;
    3.11 +} opt;
    3.12 +
    3.13 +extern struct options opt;
    3.14 +void parse_options(int argc, char * argv[]);
    3.15 +
    3.16 +#endif
     4.1 --- a/sched_rr.c	Tue Oct 13 17:29:50 2009 +0100
     4.2 +++ b/sched_rr.c	Wed Oct 14 16:12:26 2009 +0100
     4.3 @@ -50,10 +50,13 @@ void sched_rr_vm_init(int vid)
     4.4      
     4.5  }
     4.6  
     4.7 -void sched_rr_wake(int time, struct vm * v)
     4.8 +void sched_rr_wake(int time, int vid)
     4.9  {
    4.10 +    struct vm *v;
    4.11      struct sched_vm *svm;
    4.12  
    4.13 +    v = vm_from_vid(vid);
    4.14 +
    4.15      printf("%s: time %d vid %d\n",
    4.16             __func__, time, v->vid);
    4.17  
     5.1 --- a/sim.h	Tue Oct 13 17:29:50 2009 +0100
     5.2 +++ b/sim.h	Wed Oct 14 16:12:26 2009 +0100
     5.3 @@ -3,6 +3,7 @@
     5.4  
     5.5  #include "stats.h"
     5.6  #include "workload.h"
     5.7 +#include "sched.h"
     5.8  
     5.9  enum runstate {
    5.10      RUNSTATE_RUNNING,
    5.11 @@ -44,18 +45,6 @@ struct vm
    5.12  
    5.13  };
    5.14  
    5.15 -struct sched_ops {
    5.16 -    void (*sched_init)(void);
    5.17 -    void (*vm_init)(int vid);
    5.18 -    void (*wake)(int time, struct vm* v);
    5.19 -    struct vm* (*schedule)(int time, int pid);
    5.20 -};
    5.21 -
    5.22 -struct scheduler {
    5.23 -    char *name;
    5.24 -    struct sched_ops ops;
    5.25 -};
    5.26 -
    5.27  #define MAX_PCPU 16
    5.28  struct global_pcpu_data {
    5.29      int count, idle;
     6.1 --- a/simulator.c	Tue Oct 13 17:29:50 2009 +0100
     6.2 +++ b/simulator.c	Wed Oct 14 16:12:26 2009 +0100
     6.3 @@ -8,6 +8,8 @@
     6.4  #include "list.h"
     6.5  #include "sim.h"
     6.6  #include "workload.h"
     6.7 +#include "sched.h"
     6.8 +#include "options.h"
     6.9  
    6.10  FILE *warn;
    6.11  
    6.12 @@ -59,19 +61,10 @@ int default_scheduler = 0;
    6.13  struct scheduler *schedulers[] =
    6.14  {
    6.15      &sched_rr,
    6.16 +    NULL
    6.17  };
    6.18  
    6.19  /* Options */
    6.20 -struct {
    6.21 -    int time_limit;
    6.22 -    int pcpu_count;
    6.23 -    const struct workload * workload;
    6.24 -    const struct scheduler * scheduler;
    6.25 -} opt = {
    6.26 -    .time_limit = 100000,
    6.27 -    .pcpu_count = 2,
    6.28 -    .workload = NULL,
    6.29 -};
    6.30  
    6.31  struct global_pcpu_data P;
    6.32  
    6.33 @@ -350,7 +343,7 @@ void simulate(void)
    6.34              struct vm *v = vm_from_vid(evt.param);
    6.35              ASSERT(v->processor == -1);
    6.36              sim_runstate_change(sim.now, v, RUNSTATE_RUNNABLE);
    6.37 -            sim.sched_ops->wake(sim.now, v);
    6.38 +            sim.sched_ops->wake(sim.now, v->vid);
    6.39          }
    6.40          break;
    6.41          case EVT_BLOCK:
    6.42 @@ -507,12 +500,8 @@ int main(int argc, char * argv[])
    6.43  {
    6.44      warn = stdout;
    6.45  
    6.46 -    /* Read opts, config file? */
    6.47 -    if ( !opt.workload )
    6.48 -        opt.workload = builtin_workloads+default_workload;
    6.49 +    parse_options(argc, argv);
    6.50  
    6.51 -    if ( !opt.scheduler )
    6.52 -        opt.scheduler = schedulers[default_scheduler];
    6.53      /* Setup simulation */
    6.54      init();
    6.55      
     7.1 --- a/workloads.c	Tue Oct 13 17:29:50 2009 +0100
     7.2 +++ b/workloads.c	Wed Oct 14 16:12:26 2009 +0100
     7.3 @@ -1,3 +1,4 @@
     7.4 +#include <stdlib.h>
     7.5  #include "workload.h"
     7.6  
     7.7  const int default_workload = 0;
     7.8 @@ -87,4 +88,6 @@ struct workload builtin_workloads[] =
     7.9              },
    7.10          }
    7.11      },
    7.12 +    { .name=NULL }
    7.13 +        
    7.14  };