//================================================= file = loadBalance1.c ===== //= A CSIM simulation of a stateless load balancer for two queues = //= - Uses two seperate facilities = //============================================================================= //= 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 *** == = //= ============================================================= = //= = >>> Round Robin load balancing = //= = Lambda = 1.600000 cust/sec = //= = Mu = 1.000000 cust/sec = //= ============================================================= = //= = Total CPU time = 2.153000 sec = //= = Total sim time = 1000000.000000 sec = //= = Total completions = 1600563 cust = //= =------------------------------------------------------------ = //= = >>> Simulation results - = //= =------------------------------------------------------------ = //= = Utilization = 80.030375 % = //= = Mean num in system = 3.058015 cust = //= = Mean response time = 3.821177 sec = //= = Mean service time = 1.000028 sec = //= = Mean throughput = 0.800281 cust/sec = //= =------------------------------------------------------------ = //= = Utilization = 80.140519 % = //= = Mean num in system = 3.098335 cust = //= = Mean response time = 3.871554 sec = //= = Mean service time = 1.001403 sec = //= = Mean throughput = 0.800282 cust/sec = //= ============================================================= = //= *** END SIMULATION *** = //=---------------------------------------------------------------------------= //= Build: standard CSIM build = //=---------------------------------------------------------------------------= //= Execute: loadBalance1 = //=---------------------------------------------------------------------------= //= 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 Server1; // Declaration of CSIM Server facility #1 FACILITY Server2; // Declaration of CSIM Server facility #2 //----- Prototypes ------------------------------------------------------------ void generate(double lambda, double mu); // Customer generator void balancer(double mu); // Load balancer void queue1(double service_time); // Single server queue #1 void queue2(double service_time); // Single server queue #2 //============================================================================= //== 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 Server1 = facility("Server #1"); Server2 = facility("Server #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(Server1) + completions(Server2)); printf("=------------------------------------------------------------ \n"); printf("= >>> Simulation results - \n"); printf("=------------------------------------------------------------ \n"); printf("= Utilization = %f %% \n", 100.0 * util(Server1)); printf("= Mean num in system = %f cust \n", qlen(Server1)); printf("= Mean response time = %f sec \n", resp(Server1)); printf("= Mean service time = %f sec \n", serv(Server1)); printf("= Mean throughput = %f cust/sec \n", tput(Server1)); printf("=------------------------------------------------------------ \n"); printf("= Utilization = %f %% \n", 100.0 * util(Server2)); printf("= Mean num in system = %f cust \n", qlen(Server2)); printf("= Mean response time = %f sec \n", resp(Server2)); printf("= Mean service time = %f sec \n", serv(Server2)); printf("= Mean throughput = %f cust/sec \n", tput(Server2)); 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) queue1(mu); else if (index == 1) queue2(mu); } //============================================================================= //== Process for single server queue #1 == //============================================================================= void queue1(double service_time) { create("queue1"); // Reserve, hold, and release server #1 reserve(Server1); hold(service_time); release(Server1); } //============================================================================= //== Process for single server queue #2 == //============================================================================= void queue2(double service_time) { create("queue2"); // Reserve, hold, and release server #2 reserve(Server2); hold(service_time); release(Server2); }