1.83k likes | 2.02k Views
Mesquite Corporation. INTRODUCTION TO CSIM 19 FOR C++ PROGRAMMERS. http://www.mesquite.com/documentation/index.htm#startcpp. What is CSIM?. Simulation language developed by Mesquite Software in Austin, TX C/C++ based Has now a Java-based version
E N D
Mesquite Corporation INTRODUCTION TO CSIM 19 FOR C++ PROGRAMMERS http://www.mesquite.com/documentation/index.htm#startcpp
What is CSIM? Simulation language developed byMesquite Software in Austin, TX C/C++ based Has now a Java-based version Allows user to create process-oriented, discrete-event simulation models Implemented as a library No need to learn a new language
A very simple example (I) M/M/1 server with a FCFS queue Input parameters are Time intervals between arrivals Service times Server
A very simple example (II) We want to know Average customer response time:time of arrival to time of departure Customer throughput rate:customers served per unit time Server utilization:Fraction of elapsed time the server is busy Average queue length:number of customers at the facility
The sim process #include <cpp.h> // CSIM C++ header file facility *f; // the service center extern "C" void sim() { // sim process create("sim"); // make this a processf = new facility("f"); // create service center fwhile (simtime() < 5000.0) { hold(exponential(1.0)); // delay customer(); // generate new customer } // while loopreport(); // output results }// sim
The customer process void customer() {create("customer"); // make this a process f->use(exponential(0.5)); // get service } // customer Advantages of the approach • We can describe system through actions taken by each customer • Facility f manages its own queue
Output CSIM Simulation Report (Version 19 for MSVC++) Mon May 13 13:42:39 1996 Ending simulation time:10001.909 Elapsed simulation time:10001.909 CPU time used (seconds):0.490 FACILITY SUMMARY facility service service queue response compl name disc time util through-put length time count f fcfs 1.00954 0.512 0.506801. 019832.01229 5069 This is the default output. We can request much more specific data
Program analysis: processes Two processes sim: the main process customer: describes customer behavior A CSIM process is a C++ function which executes the "create" statement Establishes the procedure as an independent, ready-to-run process, Returns control to the calling process
Program analysis: facilities One facility f: the service center A facility is Declared with a “facility" declaration Initialized by the "facility()" function To request service from a facility f, process can call “f->use(Delta_t)”where “Delta_t” is the request duration
Program analysis: simtime() A global “clock” variable keeps track of the simulated time Double-precision floating point variable Value can be accessed through “simtime()” function
Program analysis: hold() “hold()” function causes time to pass for the process that calls it Deals with simulated time Do not use “sleep()”
Program analysis: exponential() “exponential(Delta_t)” function returns a value for a exponential random variable with mean “Delta_t” Many other functions of this type
Processes Model the active elements of a system Created through a call to “create()” Can have multiple instances of the same process: while (simtime() < 5000.0) { hold(exponential(1.0)); // delay customer(); // generate new customer } // while
Process attributes Each instance of a CSIM process has: Its own internal state A unique process id A process priority One of the following external states: Executing Waiting-to-execute Holding (for some time interval) Waiting (for some event to occur)
Warning CSIM processes look very much like conventional OS processes They are different: One OS process per CSIM simulation CSIM processes run within that process CSIM processes use simulated time Not physical time
CSIM process creation When a function calls create() CSIM creates a process control block (pcb) for the new process and puts it on the “next event list” Control is returned immediately to the process which invoked the function
CSIM process scheduling Non-preemptive A new process will execute only after the currently running process leaves the running state by Executing a hold() Going through a wait Terminating
Facilities We can define Single server facilities:Can service one process at a time Multi-server facilities:Can service several processes at a time and have a single queue Arrays of single server facilities:each with its own queue
Facility scheduling By default, a facility services processes in priority order Where several processes have the same priority, they will be served on FCFS basis Other service disciplines can be specified
Example:single-server facility (I) facility *ss; // declare facility ... ss = new facility("sngle srvr");// initialize ... ss->use(Delta_t); // use ss for duration Delta_t
Example:single-server facility (II) s->reserve(); // reserve facility ... hold(Delta_t); ... s->release(); // release facility ... Using a reserve/release pair lets us keep separate counts of time spent by customers (a) in queue and (b) being serviced
Example:facility with three servers All three servers share the same queue const long NSERV = 3; // 3 servers ... facility_ms *ms; // declare facility ms = new facility_ms("multi", NSERV); ... ms->use(service_time); ....
The post office problem revisited (I) #include <cpp.h> // CSIM C++ header file facility_ms *po; // the service center extern "C" void sim() { // sim process create("sim"); // make it a processpo = new facility_ms(“po“, 2); // create powhile (simtime() < 5000.0) { hold(exponential(1.0)); // delay customer(); // generate new customer } // while loopreport(); // output results }// sim
The post office problem revisited (II) void customer() {create("customer"); // make it a process po->use(exponential(0.5)); // get service } // customer
Example:array of single-server facilities Each facility now has its own queue const long NFACS = 10; facility_set *fa; // declare array fa = new facility_set("facs", NFACS); ... i = random(0, NFACS - 1); // pick one (*fa)[i].use(service_time); //use facility[i] Note the (*fa)[i]. without “->”
Example:setting a maximum wait time const double MAXWAIT = 5.0; ... st = ss->timed_reserve(MAXWAIT); if (st < MAXWAIT) { // success hold(service_time); ss->release(); // done } else { //request timed out ... }
Example:allowing preemption FACILITY cpu; // declare facility cpu cpu = new facility("cpu"); // initialize facility ... cpu->set_servicefunc(pre_res) // set service protocol to preempt-resume ... priority = 100; // raise process priority cpu->use(service_time);
Storages Like facilities but designed to be shared Each process gets some units of storage Used to simulate main memory Must have a name Only used to label report entries
Example: single storage const long SIZE = 100; storage *mem; // declare storage mem = new storage("mem", SIZE); // initialize mem with 100 units ... amt = random(1, SIZE); mem->allocate(amt); // get storage ... mem->deallocate(amt); // release it ...
Example: storage array const long NSTORES = 5; const long SIZE = 100; storage _set *mems; // declare array ... mems = new storage_set("mem", SIZE, NSTORES); //initialize ... (*mems)[3].allocate(amt); ... (*mems)[3].deallocate(amt);
Example:setting a maximum wait time const double MAXWAIT = 1.0; st = mem->timed_allocate(amt, MAXWAIT); // maximum wait is one time unit if (st < MAXWAIT) { // success ... mem->deallocate(amt); // release } else { // failure ... }
More options Can also specify faculties and storages that can only be allocated at regular points in time Clock ticks Must invoke “synchronous()” method before invoking “allocate()” method “synchronous()” method specifies phase and period of allocation policy
Buffers A buffer consist of a counter indicating the amount of available capacity A queue for processes waiting to get some buffer space A typical producer behavior A queue for processes waiting to free some buffer space A typical consumer behavior
Example:creating and accessing a buffer const long SIZE = 100; buffer *buf; buff = new buffer("buff", SIZE); ... amt = random(1, SIZE); buff->get(amt); ... buff->put(amt);
Example:setting a maximum wait time st = buff->timed_get(amt, 1.0); if (st < TIMED_OUT) { // success ... buff->put(amt); } else { // failure ... }
Events Used to synchronize and control interactions between different processes Have two states Occurred (OCC) Not occurred (NOT_OCC).
Processes and events A process can Set an event:mark it as occurred Wait for an event:when event occurs all waiting processes can continue occurred state Queue on an event:when event occurs one waiting processes can continue
More details (I) When a process waits for an event: If the event is in the not-occurred state, the process is suspended and placed in a queue of processes waiting for the event to occur. When the event occur All waiting processes are allowed to proceed Event is changed tonot occurred If the event is in the occurred state, The process continues to execute Event state is changed to not occurred
More details (II) When a process queues on an event: If the event is in the not-occurred state, the process is suspended and placed in a queueof processes queued on the event. When the event occur Only the first queued processes is allowed to proceed Event is changed to not occurred If the event is in the occurred state, The process continues to execute Event state is changed to not occurred
CSIM events and semaphores Like binary semaphores, CSIM events can have two values:occurred/not-occurred Occurred correspond to value of 1 Not-occurred corresponds to value of 0 Set() method corresponds to V() Queue() method corresponds to P() Wait() method does not correspond to any semaphore operation
Example:declaring and using an event event *ev; //declare event ev ev = new event("ev"); //initialize it ... ev->wait(); // wait for event ... ev->queue(); // queue on event ... ev->set(); // set event
Example:arrays of events const long NEV = 25; event_set *eva; // declare array eva = new event_set("ev array", NEV); // initialize it events*/ ... (*eva)[5].wait(); ... (*eva)[5].set();
Example: waiting/queuing foranyevent in an array i = eva->wait_any(); // i is index of event that occurred or i = eva->queue_any(); // i is index of event that occurred
Example: setting a maximum wait time ... t = ev->timed_wait(MAXWAIT); //wait for up to MAXWAIT time units if (st ! = MAXWAIT) { // success ... }