//===================================================== file = balance1.c ===== //= Simulation model of load balancing = //= - Random queue selection is modeled = //============================================================================= //= Notes: This program is the solution for an in-class example for = //= Simulation Spring 2005. = //=---------------------------------------------------------------------------= //= Build: Standard SMPL build = //=---------------------------------------------------------------------------= //= History: KJC (02/22/05) - Genesis = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() #include "smpl.h" // Needed for SMPL //----- Constant -------------------------------------------------------------- #define LOADSPLIT 0 // Define LoadSplit to be 0 #define SHORTQ 1 // Define ShortQ to be 1 #define LB_TYPE LOADSPLIT // Set load balancing type //===== Main program ========================================================== void main(void) { real Ta = 80; // Mean interarrival time (seconds) real Ts_1 = 100; // Mean service time for server #1 real Ts_2 = 100; // Mean service time for server #2 real te = 1.0e9; // Total simulation time int customer = 1; // Customer id (always '1' for this simulation) int event; // Event int server1; // Handle for server #1 facility int server2; // Handle for server #2 facility // Initialize SMPL subsystem smpl(0, "Load balancing model"); // Initialize server facility (single server) server1 = facility("server #1", 1); server2 = facility("server #2", 1); // Schedule arrival event at time 0 to kick-off simulation schedule(1, 0.0, customer); // Loop while simulation time is less than te while (time() < te) { // "Cause" the next event on the event list cause(&event,&customer); // Process the event switch(event) { case 1: // *** Arrival schedule(1, expntl(Ta), customer); if (LB_TYPE == LOADSPLIT) { if (ranf() < 0.50) schedule(2, 0.0, customer); else schedule(4, 0.0, customer); } if (LB_TYPE == SHORTQ) { if (inq(server1) < inq(server2)) schedule(2, 0.0, customer); else if (inq(server1) > inq(server2)) schedule(4, 0.0, customer); else if (ranf() < 0.50) schedule(2, 0.0, customer); else schedule(4, 0.0, customer); } break; case 2: // *** Request server #1 if (request(server1, customer, 0) == 0) schedule(3, Ts_1, customer); break; case 3: // *** Release server #1 release(server1, customer); break; case 4: // *** Request server #2 if (request(server2, customer, 0) == 0) schedule(5, Ts_2, customer); break; case 5: // *** Release server #2 release(server2, customer); break; default: // *** This is the error case break; } } // Output standard SMPL report printf("------------------------------------------------- \n"); if (LB_TYPE == LOADSPLIT) printf("- Using LoadSplit load balancing... \n"); else if (LB_TYPE == SHORTQ) printf("- Using ShortQ load balancing... \n"); printf("- Mean delay server #1 = %f millisec \n", Ta * (Lq(server1) + U(server1))); printf("- Mean delay server #2 = %f millisec \n", Ta * (Lq(server2) + U(server2))); printf("------------------------------------------------- \n"); }