#include #include #include #define MAX_SENDERS 64 #define MAXINT 2147483647.0 /* Buffer strategy: shared or partitioned */ /* #define SHARED 1 */ /* #define DEBUG 1 */ /* #define RAND_DEBUG 1 */ int main() { int n; int inpkt[MAX_SENDERS]; int outputq[MAX_SENDERS]; int pktssent[MAX_SENDERS]; int pktslost[MAX_SENDERS]; int totpktssent, totpktslost; int totalq; int maxqsize; int steps; int i,j; int pktsthistime; int randint; float lambda; float prob; int dest; printf("N? "); scanf("%d", &n); printf("Number of buffers for each output queue? "); scanf("%d", &maxqsize); #ifdef SHARED maxqsize *= n; #endif printf("Traffic load level? (lambda) "); scanf("%f", &lambda); printf("Simulation time length? "); scanf("%d", &steps); /* Initialization */ for( j = 0; j < n; j++ ) { pktssent[j] = 0; pktslost[j] = 0; inpkt[j] = 0; outputq[j] = 0; } totalq = 0; totpktssent = 0; totpktslost = 0; srandom(time(NULL)); /* Main loop: repeat once for each time step interval being simulated */ for( i = 0; i < steps; i++ ) { /* Transmit one packet on each output link (if non-empty queue there) */ for( j = 0; j < n; j++ ) { if( outputq[j] > 0 ) { outputq[j]--; totalq--; } } #ifdef DEBUG printf("Total output queue at time step %d: %d\n", i, totalq); printf("Output queue info after transmission, before new arrivals...\n"); for( j = 0; j < n; j++ ) { printf("%2d ", outputq[j]); } printf("\n"); #endif /* Generate the new packet arrivals in time step i */ pktsthistime = 0; for( j = 0; j < n; j++ ) { /* Generate a packet (at random) from each input link */ randint = random(); prob = randint / MAXINT; #ifdef RAND_DEBUG printf("randfloat for pkt generation is %f\n", prob); #endif if( prob <= lambda ) inpkt[j] = 1; else inpkt[j] = 0; if( inpkt[j] > 0 ) { pktsthistime++; pktssent[j]++; totpktssent++; /* Generate a destination for each packet, uniform distribution */ randint = random(); prob = randint / MAXINT; #ifdef RAND_DEBUG printf("randfloat for destination is %f\n", prob); #endif dest = prob * n + 1; inpkt[j] = dest; } } #ifdef DEBUG printf("There were %d new packets generated in round %d\n", pktsthistime, i); printf("Destination info for those packets: "); for( j = 0; j < n; j++ ) { if( inpkt[j] == 0 ) printf(" - "); else printf("%2d ", inpkt[j]); } printf("\n"); #endif /* Move each new packet onto its appropriate output queue */ for( j = 0; j < n; j++ ) { if( inpkt[j] == 0 ) continue; dest = inpkt[j] - 1; /* Check for packet loss */ #ifdef SHARED /* completely shared buffers */ if( totalq < maxqsize ) #else /* completely partitioned buffers */ if( outputq[dest] < maxqsize ) #endif { outputq[dest]++; totalq++; } else { pktslost[dest]++; totpktslost++; } } #ifdef DEBUG printf("Total output queue for step %d: %d\n", i, totalq); printf("Output queue info after arrivals and tossing...\n"); for( j = 0; j < n; j++ ) { printf("%2d ", outputq[j]); } printf("\n"); printf("Cumulative toss information to date, by output link...\n"); for( j = 0; j < n; j++ ) { printf("%2d ", pktslost[j]); } printf("\n"); #endif } /* Print simulation results */ printf("\n\nSimulation run length: %d time steps\n", steps); printf("NxN switch, with N = %d\n", n); printf("Total packets generated: %d\n", totpktssent); printf(" By input link: "); for( j = 0; j < n; j++ ) { printf("%2d ", pktssent[j]); } printf("\n"); printf("Total packets lost: %d (%10.8f)\n", totpktslost, (float) 1.0 * totpktslost/totpktssent); printf(" By output link: "); for( j = 0; j < n; j++ ) { printf("%2d ", pktslost[j]); } printf("\n"); }