Files
rd_sqngen/sqnanalyzer.c
T

148 lines
4.8 KiB
C

// to compile g++ -Wall sqnanalyzer.c -o sqnanalyzer
//
//syntax:
// sqngenanalyzer
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//s published by the Free Software Foundation; either version
//2 of the License, or (at your option) any later version.
//
//Author: Fabio Ludovici <fabio.ludovici at yahoo.it>
//Version 1.0 final
//July 2009
//
//This program was used to test the loss generation algorithm implemented in Netem2
//available at http://netgroup.uniroma2.it/twiki/bin/view.cgi/Main/NetEm2
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define UNO 4294967295
#include <iostream>
#include <fstream>
using namespace std;
void blind_external_check(int length, int gmin_max, int sequence[], int sample, float *ploss, float *mean_length_of_burst, float *density, float *pisolated_loss, float *mean_length_of_good_burst) {
bool this_is_burst;
int num_of_bursts;
int length_of_all_bursts;
int num_of_good_bursts;
int length_of_all_good_bursts;
int num_of_drops;
int num_of_burst_drops;
int num_of_isolated_drops;
for(int gmin=2;gmin<gmin_max*2;gmin=gmin*2) {
this_is_burst=false;
num_of_bursts=0;
num_of_good_bursts=0;
length_of_all_bursts=0;
length_of_all_good_bursts=0;
num_of_drops=0;
num_of_burst_drops=0;
num_of_isolated_drops=0;
for (int k=0;k<length;k++) {
if (this_is_burst==true) { //BURST ON this_is_burst=true
if ((sequence[k-1]==1) & (sequence[k]==0))
num_of_good_bursts++;
if (sequence[k]==0) {
length_of_all_bursts++;
length_of_all_good_bursts++;
} else if (sequence[k]==1) {
num_of_drops++;
num_of_burst_drops++;
length_of_all_bursts++;
}
if ((k<(length-gmin)) & (sequence[k]==1) & (sequence[k+1]==0)) {
int zeros=1;
for(int l=1;l<gmin;l++) {
if (sequence[k+1+l]==0) zeros++;
}
if (zeros==gmin)
this_is_burst=false;
}
} else { //BURST OFF this_is_burst=false
if (k==(length-1)) {
if(sequence[k]==1) {
num_of_drops++;
num_of_isolated_drops++;
}
} else if ((sequence[k]==1) & (sequence[k+1]==1)) {
this_is_burst=true;
num_of_drops++;
num_of_bursts++;
num_of_burst_drops++;
length_of_all_bursts++;
} else if ((sequence[k]==1) & (sequence[k+1]==0)) {
num_of_drops++;
num_of_isolated_drops++;
}
}
}
printf("\nFor gmin=%d the generated sequence has the following characteristics (from sequence analysis):\n",gmin);
int i=log2(gmin/2)+1;
int j=log2(gmin_max/2)+1;
ploss[sample*j+i-1]=100*((float)num_of_drops)/((float)length);
pisolated_loss[sample*j+i-1]=100*((float)num_of_isolated_drops)/((float)length);
if (num_of_bursts>0)
mean_length_of_burst[sample*j+i-1]=(float) length_of_all_bursts/num_of_bursts;
else
mean_length_of_burst[sample*j+i-1]=0;
if (num_of_good_bursts>0)
mean_length_of_good_burst[sample*j+i-1]=(float) length_of_all_good_bursts/num_of_good_bursts;
else
mean_length_of_good_burst[sample*j+i-1]=0;
density[sample*j+i-1]=100*(((float)num_of_burst_drops)/((float)length_of_all_bursts));
printf("P_loss %.3f%% Mean_length_of_burst %.3f Density %.3f%% P_isol %.3f%% Mean_length_of_good_burst %.3f\n", ploss[sample*j+i-1], mean_length_of_burst[sample*j+i-1], density[sample*j+i-1], pisolated_loss[sample*j+i-1], mean_length_of_good_burst[sample*j+i-1]);
fstream data;
data.open("data.dat", ios::out | ios::app);
data<<gmin<<";"<<fixed<<ploss[sample*j+i-1]<<";"<<mean_length_of_burst[sample*j+i-1]<<";"<<density[sample*j+i-1]<<";"<<pisolated_loss[sample*j+i-1]<<";"<<mean_length_of_good_burst[sample*j+i-1]<<";";
data.close();
}
}
int main (int argc, char *argv[]) {
if (argc<2) {
printf("Error: too few arguments\n");
printf("Syntax is: sqnanalyzer filename.dat\n");
return 0;
}
int num_of_samples=1;
int gmin_max=128;
char * buffer;
int length;
ifstream file (argv[1], ios::in|ios::binary|ios::ate);
length=file.tellg();
file.seekg (0, ios::beg);
buffer = new char [length];
file.read (buffer, length);
file.close();
int sequence[length];
printf("\nThe sequence is: ");
for(register int i=0; i<(length); i++) {
if(buffer[i]==';')
break;
else
sequence[i]=buffer[i];
if (sequence[i]==48)
sequence[i]=0;
else if (sequence[i]==49)
sequence[i]=1;
printf("%d", sequence[i]);
}
float ploss[num_of_samples*gmin_max];
float pisolated_loss[num_of_samples*gmin_max];
float mean_length_of_burst[num_of_samples*gmin_max];
float density[num_of_samples*gmin_max];
float mean_length_of_good_burst[num_of_samples*gmin_max];
int sample=1;
printf("\nLength is %d: ", length);
blind_external_check (length, gmin_max, sequence, sample, ploss, mean_length_of_burst, density, pisolated_loss, mean_length_of_good_burst);
}