//================================================= file = loadBalance2.c ===== //= A CSIM simulation of a stateless load balancer for two queues = //= - Uses facility sets = //============================================================================= //= Notes: 1) Values for lambda and mu are set in the main program = //= 2) Type of load balancing is set in #define = //= 3) Ignore warning on build = //=---------------------------------------------------------------------------= //= Example execution: = //= = //= *** BEGIN SIMULATION *** = //= ============================================================= = //= == *** CSIM Stateless Load Balancing simulation *** == = //= ============================================================= = //= = >>> Random load balancing = //= = Lambda = 1.600000 cust/sec = //= = Mu = 1.000000 cust/sec = //= ============================================================= = //= = Total CPU time = 2.387000 sec = //= = Total sim time = 1000000.000000 sec = //= = Total completions = 1598977 cust = //= =------------------------------------------------------------ = //= = >>> Simulation results - = //= =------------------------------------------------------------ = //= = Utilization = 80.030815 % = //= = Mean num in system = 4.044433 cust = //= = Mean response time = 5.062039 sec = //= = Mean service time = 1.001671 sec = //= = Mean throughput = 0.798973 cust/sec = //= =------------------------------------------------------------ = //= = Utilization = 80.173774 % = //= = Mean num in system = 4.105401 cust = //= = Mean response time = 5.131725 sec = //= = Mean service time = 1.002167 sec = //= = Mean throughput = 0.800004 cust/sec = //= ============================================================= = //= *** END SIMULATION *** = //=---------------------------------------------------------------------------= //= Build: standard CSIM build = //=---------------------------------------------------------------------------= //= Execute: loadBalance2 = //=---------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=---------------------------------------------------------------------------= //= History: KJC (06/25/13) - Genesis (from mm1_csim.c) = //============================================================================= //----- Includes -------------------------------------------------------------- #include // Needed for printf() #include "csim.h" // Needed for CSIM stuff //----- Defines --------------------------------------------------------------- #define SIM_TIME 1.0e6 // Total simulation time in seconds #define TYPE 1 // Balance type (1 = random, 2 = round robin) //----- Globals --------------------------------------------------------------- FACILITY Server[2]; // Declaration of CSIM Server facility set //----- Prototypes ------------------------------------------------------------ void generate(double lambda, double mu); // Customer generator void balancer(double mu); // Load balancer void queuex(int index, double service_time); // Single server queue //============================================================================= //== Main program == //============================================================================= void sim(void) { double lambda; // Mean arrival rate (cust/sec) double mu; // Mean service rate (cust/sec) // Create the simulation create("sim"); // CSIM initializations facility_set(Server, "Server #1 and #2", 2); // Parameter initializations lambda = 1.6; mu = 1.0; // Output begin-of-simulation banner printf("*** BEGIN SIMULATION *** \n"); // Initiate generate function and hold for SIM_TIME generate(lambda, mu); hold(SIM_TIME); // Output results printf("============================================================= \n"); printf("== *** CSIM Stateless Load Balancing simulation *** == \n"); printf("============================================================= \n"); if (TYPE == 1) printf("= >>> Random load balancing \n"); else if (TYPE == 2) printf("= >>> Round Robin load balancing \n"); printf("= Lambda = %f cust/sec \n", lambda); printf("= Mu = %f cust/sec \n", mu); printf("============================================================= \n"); printf("= Total CPU time = %f sec \n", cputime()); printf("= Total sim time = %f sec \n", clock); printf("= Total completions = %d cust \n", completions(Server[0]) + completions(Server[1])); printf("=------------------------------------------------------------ \n"); printf("= >>> Simulation results - \n"); printf("=------------------------------------------------------------ \n"); printf("= Utilization = %f %% \n", 100.0 * util(Server[0])); printf("= Mean num in system = %f cust \n", qlen(Server[0])); printf("= Mean response time = %f sec \n", resp(Server[0])); printf("= Mean service time = %f sec \n", serv(Server[0])); printf("= Mean throughput = %f cust/sec \n", tput(Server[0])); printf("=------------------------------------------------------------ \n"); printf("= Utilization = %f %% \n", 100.0 * util(Server[1])); printf("= Mean num in system = %f cust \n", qlen(Server[1])); printf("= Mean response time = %f sec \n", resp(Server[1])); printf("= Mean service time = %f sec \n", serv(Server[1])); printf("= Mean throughput = %f cust/sec \n", tput(Server[1])); printf("============================================================= \n"); // Output end-of-simulation banner printf("*** END SIMULATION *** \n"); } //============================================================================= //== Process to generate Poisson customers == //============================================================================= void generate(double lambda, double mu) { double interarrival_time; // Interarrival time to next send double service_time; // Service time for this customer create("generate"); // Loop forever to create customers while(1) { // Pull an interarrival time and hold for it interarrival_time = exponential(1.0 / lambda); hold(interarrival_time); // Pull a service time and then send the customer to the queue service_time = exponential(1.0 / mu); balancer(service_time); } } //============================================================================= //== Process to load balance == //============================================================================= void balancer(double mu) { static int index = 0; // Queue index create("balancer"); // Random (Type 1) or Round Robin (Type 2) if (TYPE == 1) index = random(0, 1); else if (TYPE == 2) index = (index + 1) % 2; // Send to appropriate queue if (index == 0) queuex(index, mu); else if (index == 1) queuex(index, mu); } //============================================================================= //== Process for single server queue == //============================================================================= void queuex(int index, double service_time) { create("queuex"); // Reserve, hold, and release server reserve(Server[index]); hold(service_time); release(Server[index]); }