123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- #include <types.h>
- #include <lib.h>
- #include <test.h>
- #include <clock.h>
- #include <thread.h>
- #include <synch.h>
- #include <synchprobs.h>
- #include <lamebus/ltimer.h>
- #include <kern/errno.h>
- #define MAX_THREADS 10
- static int NumIterations = 100;
- static int NumThreads = 10;
- static int InterArrivalTime = 1;
- static int ServiceTime = 1;
- static int DirectionBias = 0;
- static struct semaphore *SimulationWait;
- typedef struct Vehicles
- {
- Direction origin;
- Direction destination;
- } Vehicle;
- static Vehicle * volatile vehicles[MAX_THREADS];
- static struct semaphore *mutex;
- static volatile time_t total_wait_secs[4];
- static volatile uint32_t total_wait_nsecs[4];
- static volatile int wait_count[4];
- static volatile time_t max_wait_secs[4];
- static volatile uint32_t max_wait_nsecs[4];
- static struct semaphore *perf_mutex;
- #define TICK_MILLISEC (LT_GRANULARITY/1000)
- time_t start_sec, end_sec;
- uint32_t start_nsec, end_nsec;
- Direction heavy_direction;
- static void initialize_state(void);
- static void cleanup_state(void);
- static void print_direction(Direction d);
- static void print_perf_stats(void);
- static void vehicle_simulation(void *ptr, unsigned long thread_num);
- static bool right_turn(Vehicle *v);
- static void check_constraints(int thread_num);
- static void
- print_direction(Direction d) {
- switch (d)
- {
- case north:
- kprintf("N");
- break;
- case east:
- kprintf("E");
- break;
- case south:
- kprintf("S");
- break;
- case west:
- kprintf("W");
- break;
- }
- }
- static Direction
- choose_direction(void) {
- int x;
- if (DirectionBias) {
-
- x = random()%10;
- if (x >= 4) {
-
- return heavy_direction;
- }
- else {
-
- return x;
- }
- }
- else {
- return random()%4;
- }
- }
- static void
- print_perf_stats(void) {
- int i;
- int max_allowed_ms;
- int wait_msecs,mean_wait_msecs,max_wait_msecs;
- int total_wait_msecs = 0;
- int total_count = 0;
- int sim_msec;
- time_t run_sec;
- uint32_t run_nsec;
-
-
- max_allowed_ms = (NumThreads-1)*ServiceTime*TICK_MILLISEC;
- kprintf("max permitted wait:\t%d.%03d seconds\n",
- max_allowed_ms/1000,max_allowed_ms%1000);
-
- for(i=0;i<4;i++) {
- print_direction((Direction)i);
- kprintf(":\t");
- if (wait_count[i] > 0) {
- wait_msecs = (total_wait_secs[i]*1000+total_wait_nsecs[i]/1000000);
- total_wait_msecs += wait_msecs;
-
- mean_wait_msecs = wait_msecs/wait_count[i];
- total_count += wait_count[i];
- max_wait_msecs = max_wait_secs[i]*1000+max_wait_nsecs[i]/1000000;
- kprintf("%d vehicles, average wait %d.%03d seconds, max wait %d.%03d seconds\n",
- wait_count[i], mean_wait_msecs/1000,mean_wait_msecs%1000,
- max_wait_msecs/1000,max_wait_msecs%1000);
- } else {
- kprintf("0 vehicles, average wait 0.000 seconds, max wait 0.000 seconds\n");
- }
- }
-
- if (total_count > 0) {
- kprintf("all:\t%d vehicles, average %d.%03d seconds waiting\n",total_count,
- (total_wait_msecs/total_count)/1000,
- (total_wait_msecs/total_count)%1000);
- } else{
- kprintf("all:\t0 vehicles, average 0.000 seconds waiting\n");
- }
-
- getinterval(start_sec,start_nsec,end_sec,end_nsec,&run_sec,&run_nsec);
- sim_msec = run_sec*1000;
- sim_msec += run_nsec/1000000;
- kprintf("Simulation: %d.%03d seconds, %d vehicles\n",
- sim_msec/1000,
- sim_msec%1000,
- total_count);
- }
- bool
- right_turn(Vehicle *v) {
- KASSERT(v != NULL);
- if (((v->origin == west) && (v->destination == south)) ||
- ((v->origin == south) && (v->destination == east)) ||
- ((v->origin == east) && (v->destination == north)) ||
- ((v->origin == north) && (v->destination == west))) {
- return true;
- } else {
- return false;
- }
- }
- void
- check_constraints(int thread_num) {
- int i;
- KASSERT(thread_num < NumThreads);
-
- for(i=0;i<NumThreads;i++) {
- if ((i==thread_num) || (vehicles[i] == NULL)) continue;
-
- if (vehicles[i]->origin == vehicles[thread_num]->origin) continue;
-
- if ((vehicles[i]->origin == vehicles[thread_num]->destination) &&
- (vehicles[i]->destination == vehicles[thread_num]->origin)) continue;
-
- if ((right_turn(vehicles[i]) || right_turn(vehicles[thread_num])) &&
- (vehicles[thread_num]->destination != vehicles[i]->destination)) continue;
-
-
- kprintf("Vehicle A: ");
- print_direction(vehicles[i]->origin);
- kprintf("->");
- print_direction(vehicles[i]->destination);
- kprintf("\n");
- kprintf("Vehicle B: ");
- print_direction(vehicles[thread_num]->origin);
- kprintf("->");
- print_direction(vehicles[thread_num]->destination);
- kprintf("\n");
- panic("intersection synchronization constraint violation!\n");
- }
- }
- static void
- initialize_state(void)
- {
- int i;
- for(i=0;i<MAX_THREADS;i++) {
- vehicles[i] = (Vehicle * volatile)NULL;
- }
- for(i=0;i<4;i++) {
- total_wait_secs[i] = total_wait_nsecs[i] = wait_count[i] = 0;
- max_wait_secs[i] = max_wait_nsecs[i] = 0;
- }
- mutex = sem_create("Vehicle Mutex",1);
- if (mutex == NULL) {
- panic("could not create vehicle mutex semaphore\n");
- }
- perf_mutex = sem_create("PerfMutex",1);
- if (perf_mutex == NULL) {
- panic("could not create perf_mutex semaphore\n");
- }
- SimulationWait = sem_create("SimulationWait",0);
- if (SimulationWait == NULL) {
- panic("could not create SimulationWait semaphore\n");
- }
- heavy_direction = random()%4;
-
- intersection_sync_init();
- }
- static void
- cleanup_state(void)
- {
- sem_destroy(mutex);
- sem_destroy(perf_mutex);
- sem_destroy(SimulationWait);
- intersection_sync_cleanup();
- }
- static void
- in_intersection(void) {
- clocknap(ServiceTime);
- }
- static
- void
- vehicle_simulation(void * unusedpointer,
- unsigned long thread_num)
- {
- int i;
- Vehicle v;
- time_t before_sec, after_sec, wait_sec;
- uint32_t before_nsec, after_nsec, wait_nsec;
- int sleeptime;
-
- (void) unusedpointer;
- KASSERT((long)thread_num < NumThreads);
- for(i=0;i<NumIterations;i++) {
-
- sleeptime = InterArrivalTime + random()%3 - 1;
- KASSERT(sleeptime >= InterArrivalTime-1);
- KASSERT(sleeptime <= InterArrivalTime+1);
-
- clocknap(sleeptime);
-
- if (random()%NumThreads < thread_num) {
- thread_yield();
- }
-
-
- v.origin = choose_direction();
-
- v.destination = v.origin + (random()%3) + 1;
- if (v.destination >= 4) {
- v.destination = v.destination % 4;
- }
- KASSERT(4 > v.origin);
- KASSERT(4 > v.destination);
- KASSERT(v.origin != v.destination);
-
-
-
- gettime(&before_sec,&before_nsec);
- intersection_before_entry(v.origin, v.destination);
- gettime(&after_sec,&after_nsec);
-
-
- P(mutex);
- KASSERT(vehicles[thread_num] == NULL);
- vehicles[thread_num] = &v;
-
-
- check_constraints(thread_num);
- V(mutex);
-
- in_intersection();
-
-
- P(mutex);
- KASSERT(vehicles[thread_num] == &v);
- vehicles[thread_num] = NULL;
- V(mutex);
-
- intersection_after_exit(v.origin, v.destination);
- getinterval(before_sec,before_nsec,after_sec,after_nsec,&wait_sec,&wait_nsec);
- P(perf_mutex);
- total_wait_secs[v.origin] += wait_sec;
- total_wait_nsecs[v.origin] += wait_nsec;
- if (total_wait_nsecs[v.origin] > 1000000000) {
- total_wait_nsecs[v.origin] -= 1000000000;
- total_wait_secs[v.origin] ++;
- }
- wait_count[v.origin]++;
- if (wait_sec > max_wait_secs[v.origin]) {
- max_wait_secs[v.origin] = wait_sec;
- max_wait_nsecs[v.origin] = wait_nsec;
- }
- else if ((wait_sec == max_wait_secs[v.origin])&&
- (wait_nsec > max_wait_nsecs[v.origin])) {
- max_wait_nsecs[v.origin] = wait_nsec;
- }
- V(perf_mutex);
- }
-
- V(SimulationWait);
- }
- int
- traffic_simulation(int nargs,
- char ** args)
- {
- int i;
- int error;
- if ((nargs != 1) && (nargs != 6)) {
- kprintf("Usage: <command> [threads iterations interarrivaltime servicetime directionbias\n");
- return EINVAL;
- }
- if (nargs == 6) {
- NumThreads = atoi(args[1]);
- if (NumThreads <= 0 || NumThreads > MAX_THREADS) {
- kprintf("invalid number of threads: %d\n",NumThreads);
- return EINVAL;
- }
- NumIterations = atoi(args[2]);
- if (NumIterations < 0) {
- kprintf("invalid number of iterations per thread: %d\n",NumIterations);
- return EINVAL;
- }
- InterArrivalTime = atoi(args[3]);
- if (InterArrivalTime < 0) {
- kprintf("invalid interarrival time: %d\n",InterArrivalTime);
- return EINVAL;
- }
- ServiceTime = atoi(args[4]);
- if (ServiceTime < 0) {
- kprintf("invalid service time: %d\n",ServiceTime);
- return EINVAL;
- }
- DirectionBias = atoi(args[5]);
- if ((DirectionBias != 0) && (DirectionBias != 1)) {
- kprintf("Direction Bias (%d) must be 0 (unbiased) or 1 (biased)\n",DirectionBias);
- return EINVAL;
- }
- }
-
- kprintf("Threads: %d Iterations: %d Interarrival time: %d Service time: %d Bias?: %s\n",
- NumThreads,NumIterations,InterArrivalTime,ServiceTime,DirectionBias?"yes":"no");
-
- initialize_state();
-
- gettime(&start_sec,&start_nsec);
- for (i = 0; i < NumThreads; i++) {
- error = thread_fork("vehicle_simulation thread", NULL, vehicle_simulation, NULL, i);
- if (error) {
- panic("traffic_simulation: thread_fork failed: %s\n", strerror(error));
- }
- }
-
-
- for(i=0;i<NumThreads;i++) {
- P(SimulationWait);
- }
-
- gettime(&end_sec,&end_nsec);
-
- cleanup_state();
-
- print_perf_stats();
- return 0;
- }
|