Skip to content

Instantly share code, notes, and snippets.

Last active Jan 26, 2017
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 TRACE
#define TRACE_FILENAME "trace_outbound.txt"
//make sure that speed is set to _100Mbs
#define _100Mbs
#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
#ifdef _1Gbs
#define TF 0.000012
#define AR 1000000000
#define TW 0.000016
#define TS 0.000182
#ifdef _10Gbs
#define TF 0.0000012
#define AR 10000000000
#define TW 0.00000416
#define TS 0.00000288
// 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;
double last_arrival;
double last_packet_exit_time;
bool predicted_sleep;
int packet_count;
double arrival_mean;
void init();
void generatePackets(double load);
void packet(double org_time, double service_time);
extern "C" void sim(int argc, char* argv[])
//initialize variables
//create the simulation process
generatePackets((double)LOAD / 100);
// wait for all packets to be passed through the link
//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);
//display results for other simulations
printf("Speed = %.1f % bits/seconds\n", (float)AR);
printf("Load = %6.3f %% \n", (float)LOAD);
printf("Mean burst length = %d \n", MEAN_BURST_LENGTH);
printf("Total Packets Sent = %d \n", NARS);
printf("Link Utilization = %6.3f %% \n", utilization);
printf("Power Consumption = %6.3f %% \n", power_consumption);
/*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)
//close the file
/*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;
//if simulation is from poisson process/bulk arrivals
//calculate arrival rate
double rate = (AR / (PACKET_SIZE * 8)) * load;
int i = 0;
for (int i = 0; i < 1000; i++)
//do until no packets is left
while (i < NARS) {
//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);
//case poisson arrivals
// interarrival interval
hold(exponential(1.0 / rate));
i = i + 1;
// generate next packet
packet(csim_clock, (double)TF);
// arriving packet
void packet(double org_time, double service_time)
double arrival_delay = clock - last_arrival;
arrival_mean = (arrival_mean * (packet_count - 1) + arrival_delay) / packet_count;
last_arrival = csim_clock;
// reserve facility
//if the link was not busy
if (org_time == csim_clock)
//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);
//hold for the time between last packet exit time and the current time
predicted_sleep = false;
//otherwise hold for the wake up time
//hold for the wake up time
//frame size is fixed and therefore hold for the service time.
if (f->qlength() == 0)
//for proposed enhancement only
//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
predicted_sleep = true;
//note the exit time of the packet
last_packet_exit_time = csim_clock;
// release facility
if (--cnt == 0)
// if last packet, set event done
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;
//initialize arrival time of last packet as zero
last_arrival = 0.0;
last_packet_exit_time = 0.0;
predicted_sleep = false;
packet_count = 0;
arrival_mean = 0.0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment