//===================================================== file = tr_csim.c ===== //= A CSIM18 simulation of a trace/trace/1/K queue = //============================================================================ //= Notes: 1) Assumes an input file with pairs with = //= delta_time in real seconds and pkt_len in integer bytes = //= 2) Assumes MEDIA_RATE is in bits per second and CAPACITY is = //= in bytes = //= 3) Note that the trace file is hardwired as TRACE_FILE and that = //= no error checking is performed on the opened file = //= 4) Outputs overflow packets to OVER_FILE = //=--------------------------------------------------------------------------= //= History: KJC (02/28/00) - Genesis (from mm1_csim.c) = //= KJC (05/23/00) - Modified to include an overflow file = //============================================================================ //----- Includes ------------------------------------------------------------- #include // Needed for printf(), feof(), and fscanf() #include // Needed for exit() and ato*() #include "csim.h" // Needed for CSIM18 stuff //----- Defines --------------------------------------------------------------- #define TRACE_FILE "trace.dat" // Name of trace file #define OVER_FILE "over.dat" // Name of overflow file #define MEDIA_RATE 166.5442e6 // Output Media rate in bits per second #define CAPACITY 1048576 // Queue capacity in bytes //----- Globals --------------------------------------------------------------- FILE *Tf; // Trace file pointer FILE *Of; // Overflow file pointer FACILITY Server; // Server facility EVENT Done; // Done event int Bytes_buffered; // Bytes buffered in queue int Total_count; // Counter for packets arriving to queue int Accept_count; // Counter for packets acceptted by queue int Discard_count; // Counter for packets discarded by queue double Last_over_time; // Last overflow time //----- Prototypes ------------------------------------------------------------ void generate(void); // Generate customers from trace file void queue1(int pkt_len); // Single server queue with finite capacity //============================================================================= //== Main program == //============================================================================= void sim(void) { // Create the simulation create("sim"); // Increase max_processes max_processes(1000000L); // Open the trace and overflow file (clobber existing overflow file) Tf = fopen(TRACE_FILE, "r"); if (Tf == NULL) { printf(" >>> ERROR in opening trace file '%s' \n", TRACE_FILE); exit(1); } Of = fopen(OVER_FILE, "w"); if (Of == NULL) { printf(" >>> ERROR in opening overflow file '%s' \n", OVER_FILE); exit(1); } // Initializations Server = facility("Server"); Done = event("Done event"); Bytes_buffered = Total_count = Accept_count = Discard_count = 0; Last_over_time = 0.0; // Output begin-of-simulation banner printf("*** BEGIN SIMULATION *** \n"); // Initiate generate function and wait for Done event to be set generate(); wait(Done); // Output results printf("============================================================= \n"); printf("== *** CSIM18 trace/trace/1/K queue simulation *** == \n"); printf("============================================================= \n"); printf("= Media rate = %9.0f bits/sec \n", MEDIA_RATE); printf("= Queue capacity (K) = %9d bytes \n", CAPACITY); printf("============================================================= \n"); printf("= Total CPU time = %9.3f sec \n", cputime()); printf("= Total sim time = %9.3f sec \n", clock); printf("= Total arrived = %9d pkts \n", Total_count); printf("= Total acceptted = %9d pkts \n", Accept_count); printf("= Total discarded = %9d pkts \n", Discard_count); printf("============================================================= \n"); printf("= Utilization = %9.6f %% \n", 100.0 * util(Server)); printf("= Mean queue len = %9.6f pkts \n", qlen(Server)); printf("= Mean response time = %9.6f sec \n", resp(Server)); printf("= Mean service time = %9.6f sec \n", serv(Server)); printf("= Mean throughput = %9.6f pkts/sec \n", tput(Server)); printf("= Loss percentage = %9.6f %% pkts \n", 100.0 * Discard_count / Total_count); printf("============================================================= \n"); // Output end-of-simulation banner printf("*** END SIMULATION *** \n"); } //============================================================================= //== Function to open a trace file and generate packets == //============================================================================= void generate(void) { char in_string1[80]; // Input string #1 char in_string2[80]; // Input string #2 double time_stamp; // Delta time in seconds from trace file int pkt_len; // Packet length in bytes from trace file create("generate"); while(1) { fscanf(Tf, "%s %s \n", in_string1, in_string2); if (feof(Tf)) break; time_stamp = atof(in_string1); pkt_len = atoi(in_string2); hold(time_stamp); queue1(pkt_len); } // Set the Done event set(Done); return; } //============================================================================= //== Function for single server queue with finite capacity in bytes == //============================================================================= void queue1(int pkt_len) { create("queue1"); // Increment total arriving counter Total_count++; // If no space for packet, discard it (logging to overflow file) if (pkt_len > (CAPACITY - Bytes_buffered)) { Discard_count++; fprintf(Of, "%f -- %9d -- %5d \n", (clock - Last_over_time), Total_count, pkt_len); Last_over_time = clock; return; } // The packet got buffered, so update Bytes_buffered and increment Accept Bytes_buffered = Bytes_buffered + pkt_len; Accept_count++; // Reserve, hold, and release server reserve(Server); hold((8.0 * pkt_len) / MEDIA_RATE); release(Server); // Update Bytes_buffered Bytes_buffered = Bytes_buffered - pkt_len; return; }