Instantly share code, notes, and snippets.

Embed
What would you like to do?
csim - EEE
// C++/CSIM Model of EEE - Energy Efficient Ethernet
//This program is a modification of M/M/1 queue example bundled with CSIM software
//Compiled and run using the default compiler available with MS visual studio 2010
//TRACE files used were those provided at the project-3 link in Dr. Ken Christensen's web page
// class definitions
#include "cpp.h"
#include <stdio.h>
#include <string.h>
/*Type of arrival, Use: NORMAL or BULK_ARRIVAL or TRACE or PROPOSED*/
#define NORMAL
/*define max link speed in this experiment, Use: _100Mbs or _1Gbs or _10Gbs */
#define _100Mbs
/* number of packet arrivals to be simulated*/
#define NARS 5000
/* load in percentage */
#define LOAD 10
/* Size of the packet in Bytes */
#define PACKET_SIZE 1250
#ifdef BULK_ARRIVAL
#define MEAN_BURST_LENGTH 2
#endif
#ifdef TRACE
#define TRACE_FILENAME "trace_outbound.txt"
//make sure that speed is set to _100Mbs
#define _100Mbs
#endif
#ifdef _100Mbs
/* mean of service time distribution */
#define TF 0.000120
/* mean of inter-arrival time distribution */
#define AR 100000000
/* time to wake-up */
#define TW 0.000030
/* time to sleep */
#define TS 0.000100
#endif
#ifdef _1Gbs
#define TF 0.000012
#define AR 1000000000
#define TW 0.000016
#define TS 0.000182
#endif
#ifdef _10Gbs
#define TF 0.0000012
#define AR 10000000000
#define TW 0.00000416
#define TS 0.00000288
#endif
// pointer to event done
event* done;
// pointer to facility f
facility* f;
// count of remaining packets
int cnt;
#ifdef TRACE
double mean_arrival;
double mean_packet_size;
double mean_service_time;
#endif
#ifdef PROPOSED
double last_arrival;
double last_packet_exit_time;
bool predicted_sleep;
int packet_count;
double arrival_mean;
#endif
void init();
void generatePackets(double load);
void packet(double org_time, double service_time);
extern "C" void sim(int argc, char* argv[])
{
//initialize variables
init();
//create the simulation process
create("sim");
generatePackets((double)LOAD / 100);
// wait for all packets to be passed through the link
done->wait();
//calculate utilization in percentage
double utilization = f->util() * 100.0;
//calculate power consumption in percentage
double power_consumption = utilization + ((100 - utilization) * 0.1);
//display results for trace simulation
#ifdef TRACE
printf("Speed = %.1f bits/seconds\n", 100000000.0);
printf("Mean arrival time = %f seconds\n", mean_arrival);
printf("Average packet Size = %.1f Bytes\n", mean_packet_size);
printf("Mean service rate = %f seconds\n", mean_service_time);
printf("Total Packets Sent = %d \n", NARS);
printf("Link Utilization = %6.3f %% \n", utilization);
printf("Power Consumption = %6.3f %% \n", power_consumption);
#else
//display results for other simulations
printf("Speed = %.1f % bits/seconds\n", (float)AR);
printf("Load = %6.3f %% \n", (float)LOAD);
#ifdef BULK_ARRIVAL
printf("Mean burst length = %d \n", MEAN_BURST_LENGTH);
#endif
printf("Total Packets Sent = %d \n", NARS);
printf("Link Utilization = %6.3f %% \n", utilization);
printf("Power Consumption = %6.3f %% \n", power_consumption);
#endif
fflush(stdout);
getchar();
}
/*generates the packets*/
void generatePackets(double load)
{
//if simulation is from a trace file
#ifdef TRACE
//open the trace file
FILE* fp = fopen(TRACE_FILENAME, "r");
char line[80];
double prev_time = 0.0;
double total_arrival = 0.0;
double total_service = 0.0;
double total_packetsize = 0.0;
int i = 0;
//read line by line
while (fgets(line, 80, fp) != NULL) {
i = i + 1;
char* temp[sizeof(strtok(line, " "))];
if (sizeof(temp) > 0) {
//read arrival time
temp[0] = strtok(line, " ");
//read packet size in Bytes
temp[1] = strtok(NULL, " ,.-");
//hold for the computed arrival interval
hold(atof(temp[0]) - prev_time);
//compute service time using the current packet size
double service_time = (atof(temp[1]) / (double)PACKET_SIZE) * (double)TF;
//genrate packets
packet(csim_clock, service_time);
/*get the totals for finding mean*/
total_arrival = total_arrival + atof(temp[0]) - prev_time;
total_service = total_service + service_time;
total_packetsize = total_packetsize + atof(temp[1]);
//record the arrival time of previous packet so that we could find the arrival interval for next packet
prev_time = atof(temp[0]);
}
//if number of arrivals equals the total number of packets for simulation the exit
if (i == NARS)
break;
}
//close the file
fclose(fp);
/*compute mean values by diving with the total number of packets*/
mean_arrival = total_arrival / (double)NARS;
mean_service_time = total_service / (double)NARS;
mean_packet_size = total_packetsize / (double)NARS;
#else
//if simulation is from poisson process/bulk arrivals
//calculate arrival rate
double rate = (AR / (PACKET_SIZE * 8)) * load;
create("gen");
int i = 0;
for (int i = 0; i < 1000; i++)
//do until no packets is left
while (i < NARS) {
#ifdef BULK_ARRIVAL
//case bulk arrivals at poisson rate
// compute batch size
long bulk_size = 1 + geometric(1.0 / (double)MEAN_BURST_LENGTH);
// interarrival interval for bulk arrivals
hold(exponential(1.0 / rate) * (bulk_size));
//generate bulk packets and send
for (int k = 0; k < bulk_size; k++) {
i = i + 1;
packet(csim_clock, (double)TF);
}
#else
//case poisson arrivals
// interarrival interval
hold(exponential(1.0 / rate));
i = i + 1;
// generate next packet
packet(csim_clock, (double)TF);
#endif
}
#endif
}
// arriving packet
void packet(double org_time, double service_time)
{
#ifdef PROPOSED
++packet_count;
double arrival_delay = clock - last_arrival;
arrival_mean = (arrival_mean * (packet_count - 1) + arrival_delay) / packet_count;
last_arrival = csim_clock;
#endif
create("packet");
// reserve facility
f->reserve();
//if the link was not busy
if (org_time == csim_clock)
#ifdef PROPOSED
//for proposed enhancement only
//if the link is active and waiting for a packet based on feedback prediction
if (predicted_sleep) {
double delay = clock - last_packet_exit_time;
// if delay is greater than the sleep time
if (delay > TS)
// hold for waittime, sleeptime and wakeup time (penalty for prediction failure)
hold((2 * TS) + TW);
else
//hold for the time between last packet exit time and the current time
hold(delay);
predicted_sleep = false;
}
else
//otherwise hold for the wake up time
hold(TW);
#else
//hold for the wake up time
hold(TW);
#endif
//frame size is fixed and therefore hold for the service time.
hold(service_time);
if (f->qlength() == 0)
//for proposed enhancement only
#ifdef PROPOSED
//if queue is empty and delay between last and present arrival is less than sleep time
if (TS > arrival_mean) {
//hold for the sleep time
hold(TS);
predicted_sleep = true;
//note the exit time of the packet
last_packet_exit_time = csim_clock;
}
#else
//sleep
hold(TS);
#endif
// release facility
f->release();
if (--cnt == 0)
// if last packet, set event done
done->set();
}
void init()
{
// instantiate event done
done = new event("done");
// instantiate facility f
f = new facility("facility");
// initialize counter
cnt = NARS;
//initialize mean variables to zero
#ifdef TRACE
mean_arrival = 0.0;
mean_packet_size = 0.0;
mean_service_time = 0.0;
#endif
//initialize arrival time of last packet as zero
#ifdef PROPOSED
last_arrival = 0.0;
last_packet_exit_time = 0.0;
predicted_sleep = false;
packet_count = 0;
arrival_mean = 0.0;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment