Tutorial 1 Erindale: Thu@3pm 970116 ========== St George: Thu@6pm CSC 354, Spring 1997 Tutorial notes, T1 ========================================================================== Announce: - these tutorial notes are also available at... http://www.cs.toronto.edu/~andria/354s97/ Topics: - Getting started with CSIM - CSIM example ----------------------- (1) What is CSIM? CSIM is a process-oriented discrete-event simulation package for use with C programs. It is implemented in C as a library of routines which implement all of the necessary operations. The end result is a convenient tool which programmers can use to create simulation programs. A CSIM program models a system as a collection of CSIM processes that interact with each other by using the CSIM structures. The purpose of modelling a system is to produce estimates of time and performance. The model maintains simulated time, so that the model can yield insight into the dynamic behaviour of the modelled system. (2) Where are the CSIM References? For Version 16: On cdf, look at: /local/doc/csim/rev16-docs/csim.doc /local/doc/csim/rev16-docs/short.doc /local/doc/csim/rev16-docs/user.doc To convert these files to plain text: tbl csim.doc | psroff -ms (send to printer) tbl csim.doc | nroff -ms (view plain text) For Version 18: On www, look at: http://www.mesquite.com/ Cdf currently has version 16 of CSIM, but we will be upgrading to version 18 shortly. (3) Where are the CSIM examples? For Version 16: On cdf, look in this directory: /local/doc/csim/rev16-docs/examples/c/ (4) Running a CSIM example program % csim ex1.c (to compile) % a.out (regular run) % a.out -T (run with debug) Give the UNIX command "man csim" for more information on compiling and running CSIM. (5) CSIM Introduction CSIM is a process-oriented simulation language which is implemented on top of the C programming language. CSIM gives the simulation programmer facilities for defining processes, for initiating these processes and for synchronizing these processes. Programs written in CSIM are really C programs, with calls to procedures which form an extended run-time support system. Thus, users have all the power and convenience of the C programming language, with the additional feature of being able to create process- oriented simulation models. In the process-oriented simulation approach, the programmer defines the processes (entities, transactions, etc.) which "use" the resources of the system (facilities). Each process can be in one of 4 states: 1. active - currently being processed by the simulator 2. ready - not currently active, but ready to be active 3. holding - waiting for an interval of simulated time to pass 4. waiting - waiting (in a queue) for an event to occur Simulated time passes only when a process is in the "hold" state. (6) CSIM Processes In CSIM, processes represent the active entities in a CSIM model. A process is a C procedure which executes a "create" statement. There can be several simultaneously "active" instances of the same process. Each of these instances appears to be executing in parallel (in simulated time) even though they are in fact executing sequentially on a single processor. (7) CSIM Facilities Facilities are normally used to model a resource (something a process requests service from) in a simulated system. One process at a time can use a facility. Other processes must wait until the facility becomes available (the default scheduling discipline is first-come-first-served). A set of queueing and usage statistics is automatically maintained for each facility in a model. Usually the "report()" statement is executed near the end of the simulation to display these statistics. (8) CSIM Data Structures There are several different data structures available in CSIM. For each CSIM structure, the program must have a declaration (which is a pointer to an object). Before an object can be used, it must be initialized by the constructor function for that kind of object. The following CSIM data structures are provided: Processes - the active entities that request service at facilities, wait for events, etc (processes deal with all of the other structures) Facilities - queues and servers reserved or used by processes Storages - resources which can be partially allocated to processes Events - used to synchronize process activity Mailboxes - used for inter-process communications Data Collection Structures - used to collect data during the execution of a model Process Classes - used to segregate statistics for reporting purposes Streams - streams of random numbers (9) Examples of CSIM structures (a) Processes sim() { create("sim"); ... } (b) Facilities FACILITY f; /* declaration of facility */ f = facility ("Server"); /* initialization of facility */ reserve (f); /* reserve facility for exclusive use */ hold (3.2); /* hold faciliity for simulated time */ release (f); /* release facility */ (10) CSIM M/M/1 Program Example /* simulate an M/M/1 queue (an open queue with exponential service times and interarrival intervals) */ #include #define SVTM 1.0 /*mean of service time distribution */ #define IATM 2.0 /*mean of inter-arrival time distribution */ #define NARS 5000 /*number of arrivals to be simulated*/ FACILITY f; /*pointer for facility */ EVENT done; /*pointer for event */ TABLE tbl; /*pointer for table */ QHIST qtbl; /*pointer for qhistogram */ int cnt; /*number of active tasks*/ sim() /*1st process - named sim */ { int i; set_model_name("M/M/1 Queue"); create("sim"); /*required statement*/ f = facility("facility"); /*initialize facility*/ done = event("done"); /*initialize event*/ tbl = table("resp tms"); /*initialize table */ qtbl = qhistogram("num in sys", 10); /*initialize qhistogram*/ cnt = NARS; /*initialize cnt*/ for(i = 1; i <= NARS; i++) { hold(expntl(IATM)); /*hold interarrival*/ cust(); /*initiate process cust*/ } wait(done); /*wait until all done*/ report(); /*print report*/ theory(); /*print theoretical res*/ mdlstat(); /*print model stats*/ } cust() /*process customer*/ { float t1; create("cust"); /*required statement*/ t1 = clock; /*time of request */ note_entry(qtbl); /*note arrival */ reserve(f); /*reserve facility f*/ hold(expntl(SVTM)); /*hold service time*/ release(f); /*release facility f*/ record(clock-t1, tbl); /*record response time*/ note_exit(qtbl); /*note departure */ cnt--; /*decrement cnt*/ if(cnt == 0) set(done); /*if last arrival, signal*/ } theory() /*print theoretical results*/ { float rho, nbar, rtime, tput; printf("\n\n\n\t\t\tM/M/1 Theoretical Results\n"); tput = 1.0/IATM; rho = tput*SVTM; nbar = rho/(1.0 - rho); rtime = SVTM/(1.0 - rho); printf("\n\n"); printf("\t\tInter-arrival time = %10.3f\n",IATM); printf("\t\tService time = %10.3f\n",SVTM); printf("\t\tUtilization = %10.3f\n",rho); printf("\t\tThroughput rate = %10.3f\n",tput); printf("\t\tMn nbr at queue = %10.3f\n",nbar); printf("\t\tMn queue length = %10.3f\n",nbar-rho); printf("\t\tResponse time = %10.3f\n",rtime); printf("\t\tTime in queue = %10.3f\n",rtime - SVTM); } CSIM Simulation Report (Revision: 16, System: SUN3) Date and time: Thu Jul 9 11:45:33 1992 Model: M/M/1 Queue Time: 10041.661 Interval: 10041.661 CPU Time: 10.850 (seconds) Facility Usage Statistics +----------------------+---------------means----------------+---counts----+ facility srv disp serv_tm util tput qlen resp cmp pre facility 0.992 0.494 0.5 0.991 1.989 5000 0 Table 1 Table Name: resp tms mean 1.989 min 0.000 variance 3.813 max 14.273 Number of entries 5000 QTable 2 QTable Name: num in sys Mean queue length 0.991 Max queue length 13 Mean time in queue 1.989 Number of entries 5000 Queue Table Histogram Length % of Elapsed Time Cumulative Count Mean Time 0 0.506 0.506 2516 2.020 1 0.242 0.748 3694 0.657 2 0.123 0.871 1845 0.671 3 0.067 0.938 1014 0.659 4 0.035 0.972 510 0.686 5 0.015 0.988 234 0.652 6 0.007 0.995 99 0.700 7 0.002 0.997 40 0.627 8 0.001 0.998 21 0.469 9 0.001 0.999 13 0.823 10 0.000 0.999 6 0.519 over 0.001 1.000 8 0.807 M/M/1 Theoretical Results Inter-arrival time = 2.000 Service time = 1.000 Utilization = 0.500 Throughput rate = 0.500 Mn nbr at queue = 1.000 Mn queue length = 0.500 Response time = 2.000 Time in queue = 1.000 CSIM Model Statistics CPU Time: 10.917 (seconds) Events processed: 17485 Memory (malloc): 7116 (bytes) Number of malloc's: 41 Processes Started: 5001 Saved: 9105 Terminated: 5000 High water mark 14 Stacks Allocated: 5001 High water mark 673 (words) Average: 48 (words) Maximum 49 (words) Current 49 (words) (11) CSIM M/M/1 Program Explanation (a) sim() - process "sim" is created - for loop generates at appropriate intervals (exponentially distributed with mean IATM) arriving customers. - process is suspended until all customers finish (event done) - stats are displayed (b) cust() - process "cust" is created for each customer. - these customers contend for the facility on a first-come first-served basis. - as each customer gains exclusive use of the facility (reserve()), they delay for a service period (exponentially distributed with mean SVTM) and then depart. - individual response times are collected in a table. - qhistogram feature is used to collect the frequency distribution of the queue length. - after customer is finished, decrement count and signal if this is the last customer.