www.pudn.com > ghs1.2.rar > syspred.cpp


/*
 * Copyright (c) 2000-2003 Illinois Institute of Technology.
 *                         All rights reserved.
 *
 * This file is part of the GHS software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * GHS source distribution.
 *
 * Released Date: August 18 2005
 * This is the GHS I release by the
 *   SCS Group,
 *   http://www.cs.iit.edu/~scs
 *   Department of Computer Science,
 *   Illinois Institute of Technology.
 *
 * This release has been tested under SUN OS 5.9.
 *
 * Supervisor
 * ----------
 * Illiniois Institute of Technology:
 * -Dr. Xian-He Sun
 *
 * Author
 * -----------------
 * Illinois Institute of Technology:
 * -Ming Wu
 * -Xian-He Sun
 *
 */

#include
#include 
#include
#include
#include
#include
#include "ghs.h"

using namespace std;

char measureLog[120]; 

vector paraDay;
vector paraHour;
vector paraAll;

double alpha = 0.5, beta = 0.5;
int hourSample, daySample;
int hourNum, dayNum;

/* This array is used to store the configuration parameters */
vector configArray;

double 
GetRandomAver(double *values, int maxNum, int numElements)
{
  int elementIndex;
  double mean = 0.0;

  //for(i=maxNum-1;i>=maxNum-numElements;i--)
  // mean += values[i];

  for (int i = 0; i < numElements; i++)
  {
    elementIndex = (int) (1.0 * rand() * (maxNum-1) / (RAND_MAX+1.0));
    mean += values[elementIndex];
  }
  
  mean /= numElements;

  return mean;
}

/* generate two arrays for store related measurement in history 
 * according to the users input "interval" which gives the time period 
 * over which the system parameter will be predicted
 */ 
void
GetParameters(char *paraName, int interval, int *maxDayNum, int *maxHourNum)
{  
  time_t currHour=0, currHourTmp=0;
  struct timeval tv;
  int initHour, paraPosi=0, lastHour;
  double paraValue;

  fstream measFile;
  measFile.open(measureLog, fstream::in);
  if (!measFile.is_open())
  {
    cout << "\ncannot open log file to read" << endl;
    exit(1);
  }

  gettimeofday(&tv, NULL);
  currHour = tv.tv_sec / 3600;

  // check which column data should be read 
  if (!strcmp(paraName, "utilization")) paraPosi = 1;
  else if (!strcmp(paraName, "arrival")) paraPosi = 2;
  else if (!strcmp(paraName, "service")) paraPosi = 3;
  else if (!strcmp(paraName, "std")) paraPosi = 4;

  string bufConfig;
  if (!getline(measFile, bufConfig))
  {
    cout << "No measurement in the file, check the filename input" << endl;
    exit(1);
  }

  int i = 0, j;
  char *str;
  while ( getline(measFile, bufConfig) && i < MAX_HOUR )
  {
    str = strtok(const_cast(bufConfig.c_str()), " ");
    if (i == 0) initHour = atoi(str);
    // ******************* the set of currHour ********************
    else currHourTmp = atoi(str);
    
    j = 0;
    while (j < paraPosi)
    { 
      str = strtok(NULL, " "); 
      j++;
    }
    paraValue = atof(str);
    cout << "the paravalue is " << paraValue < (currHourTmp + 1)) currHour = currHourTmp;

  // check whether there is any measurement data available
  if (i == 0) 
  {
    cout << "No measurment available by far" << endl;
    exit(0);
  }

  int dayNum, interNum;
  int currParaIndex, lastParaIndex;
  dayNum = (currHour - initHour) / 24 + 1;
  //cout << "currHour is %d and initHour is %d \n", currHour, initHour);
  if( ((dayNum-1)*24) == (currHour-initHour) ) dayNum--;

  // nonsense for the following two statements
  interNum = (currHour - initHour) / interval + 1;
  if( ((interNum-1)*interval) == (currHour-initHour) ) interNum--;

  // may have some problem if two measurements in the same hour
  lastHour = initHour + i -1; //the time when last measurement is collected

  lastParaIndex = i-1; //sample size
  if (DEBUG_ON == 1) cout << "the lastParaIndex is " << lastParaIndex <= 0) 
  {
    if(!invalidTag) paraHour.push_back(0);
    j = 0;
    invalidTag = 0;

    while ((currParaIndex >= 0) && (j < interval))
    {
      paraHour[k1] += paraAll[currParaIndex];
      if (paraAll[currParaIndex] == INVALID_PARA_VALUE) {
        invalidTag = 1;
      }
      currParaIndex--;
      j++;
    }

    if(invalidTag == 1) paraHour[k1] = 0;
    paraHour[k1] = paraHour[k1] / j;
    if (DEBUG_ON == 1) cout << "the paraHour[" << k1 << "] is " << paraHour[k1] << endl;
    if (invalidTag == 0) k1++;
    if (k1 == MAX_HOUR_SIZE-1) break;
  }

  currParaIndex = lastParaIndex; 
  k2 = 0;
  k3 = 0;

  invalidTag = 0;
  // calculate the day samples
  while ( (k3 < dayNum) && (currParaIndex >= 0) )
  {
    currParaIndex = lastParaIndex - 24*k3;

    if(!invalidTag) paraDay.push_back(0);
    j = 0;
    invalidTag = 0;
    
    while ((currParaIndex >= 0) && (j < interval))
    {
      paraDay[k2] += paraAll[currParaIndex];
      if (paraAll[currParaIndex] == INVALID_PARA_VALUE) {
        invalidTag = 1;
      }
      currParaIndex--;
      j++;
    }

    if(invalidTag == 1) paraDay[k2] = 0; 
    paraDay[k2] = paraDay[k2] / j;
    if (DEBUG_ON == 1) cout << "the paraDay[" << k2 <<"] is " << paraDay[k2] << endl;
    if (invalidTag == 0) k2++;
    if (k2 == MAX_DAY_SIZE-1) break;
    k3++;
  }	
	
  *maxDayNum = k2; //return current number of day samples
  *maxHourNum = k1; //return current number of hour samples

  measFile.close();

}

double
GetPrediction(int dayIndex, int hourIndex)
{

  double predValue = 0, valueDay = 0, valueHour = 0;

  if ( (dayIndex <=0) || (hourIndex <= 0) ) 
  {
    cout << "Data is missing for prediction " <(configArray[j].c_str())," ");
    temp1 = strtok(NULL, " ");
    if (strcmp(temp,"LOGFILE_NAME") == 0) // unit_time, usually it is an hour
    {  
      sscanf(temp1, "%s ", &fileName); //Linux option
      if (gethostname(hostName, 80) != 0)
      {
        cout << "Can not get the host name to generate the measure log file " << endl;
        exit(0);
      }
      sprintf(logFileName, "%s/%s%s", getenv("GHS_HOME"), hostName, fileName);
      //sprintf( logFileName, "%s/%s", getenv("GHS_HOME"), fileName); 
      //cout << "\n the log file's name is: %s \n", logFileName);
    }

  }

  fdConfig.close();
}

/*
 * main program
 */

int 
main(int argc, char **argv)
{

  int periodTmp;
  char paraTmp[10];
  int dayIndex, hourIndex;
  double predResult;

  // read input parameters

  if ( argc < 3 )
  {
    cout <<"syspred utilization|arrival|service|std period [filename]"<