www.pudn.com > ghs1.2.rar > smeas.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 1.0 released 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(s)
 * -----------------
 * Illinois Institute of Technology:
 * -Ming Wu
 * -Xian-He Sun
 * g++ -g -o smeas smeas.cpp statistics.cpp unixutilities.cpp file.cpp distributor.cpp warehouse.cpp
 */

//#include "top.h"
//#include "machine.h"
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "ghs.h"
#include "file.h"
#include "warehouse.h"
#include "statistics.h"
#include "unixutilities.h"

using namespace std;

class Monitor
{
  static const int MAX_DATA_SIZE=100; // max number of bytes we can get at once for one line
  static const int MAX_IT = 5;
  static const int MAX_SAMPLE_SIZE = 150; // maximum sample size in an hour
  static const int MIN_SAMPLE_SIZE = 10; // maximum sample size in an hour
 
  static const string data_log; // the log file storing the J_arrival and M_utilization original information

  double procMiniTime;
  long time1;

  int debugOn;

  vector lambdaMeanPredict; //J_Lambda expectation prediction array
  vector muMeanPredict; //M_Utilization expectation prediction array
  vector lambdaSTDPredict; //J_Lambda std prediction array
  vector muSTDPredict; //M_Utilization std prediction array

  vector< vector > lambdaValues; //J_Lambda sample values
  vector< vector > muValues; //J_serviceRate sample values
  vector< vector > thetaValues; //J_serviceSTD sample values
  vector< vector > rhoValues; //M_Utilization sample values
  //double rhoValues[MAX_HOUR][MAX_SAMPLE_SIZE]; //M_Utilization sample values

  string measureLog; // string used to store the log file name

  //static const int debugOn =0; // debug tag
  int logfileRenew; // logfile renew tag

  // initial unit_time for measuring, the maximum measuring time_unit
  int sampleTime;
  int loopNum;

  // numIter means the number of samples during one hour,
  // numIterNext means the number of samples during the next hour
  //

  int numIter, numIterNext;

  struct _JobStatistics sampleStatistics;

  double jobServTime[MAX_PROC_NUM];

  Statistics mySta;
  WareHouse *myWarehouse;
  UnixUtilities myUtils;

  int GetNextSampleSize(int hIndex);
  void ReadConfig();

  public:
    Monitor();
	int Run();
    
};

const string Monitor::data_log = "data.log";

Monitor::Monitor() {
  struct timeval tv;

  numIter = 0;
  numIterNext = 50;
  sampleTime = 3600;
  loopNum = 1000;
  procMiniTime = 1.0;
  debugOn = 0;
  
  ReadConfig();
  
  gettimeofday(&tv, NULL);
  time1 = tv.tv_sec;
  if (debugOn == 1) cout << "The time in seconds is " << time1 << endl;

  myWarehouse = new WareHouse(measureLog, time1, logfileRenew);

}


/* Read the parameters from the configuration file */

void 
Monitor::ReadConfig()
{
  fstream fdConfig;
  string ghsConfig;
  int i = 0;

  vector paraStrs; 
  vector paraValues;

  stringstream astrstream;
  astrstream << getenv("GHS_HOME");
  ghsConfig = astrstream.str() + "/ghs_config.dat";
  fdConfig.open(ghsConfig.c_str(), ios::in);
  string line;
  if ( fdConfig.is_open())
  {	
    while (getline(fdConfig, line))
    {   
      if (line[0] != '/' && line[1] != '/') {
        stringstream tmpstream;
        string tmppara, tmpvalue;
        tmpstream << line;
        tmpstream >> tmppara >> tmpvalue;
        paraStrs.push_back(tmppara);
        paraValues.push_back(tmpvalue);
        if (debugOn == 1) cout << tmppara << " " << tmpvalue << endl;
        i++; // number of configuration line
      }
    }
  }
  else {
    if (debugOn == 1) cout << "\n ghsHome is " << getenv("GHS_HOME") << endl;
    if (debugOn == 1) cout << "\n Cannot open the configuration file" << endl;
    exit(1);
  }

  if (debugOn == 1) cout << "\nReading configuration parameters... done" << endl;
  if (debugOn == 1) cout << "\nPrinting configuration parameters" << endl;

  string temp;
  for(int j = 0; j < i; j++)
  {	
    temp = paraStrs[j];

    if (temp == "UNIT_TIME") // unit_time, usually it is an hour
    {	
      sampleTime = atoi(paraValues[j].c_str());
      if (debugOn == 1) cout << "\n The time to take one set of samples is: " << sampleTime << endl;
    }
		
    if (temp ==  "INITIAL_SAMPLE_SIZE") // initial sample size
    {	
      numIterNext = atoi(paraValues[j].c_str());
      if (debugOn == 1) cout << "\n The initial sample size is: " << numIterNext << endl;
    }

    if (temp ==  "LOOP_NUMBER") // the maximum measuring number of unit_time
    {	
      loopNum = atoi(paraValues[j].c_str());
      if (debugOn == 1) cout << "\n The loop number  is: " << loopNum << endl;
    }

    if (temp ==  "MINIMUM_EFFECTIVE_LIFETIME") // unit_time, usually it is an hour
    {
      float pmTime;
      stringstream tmpstream;
      tmpstream << paraValues[j];
      tmpstream >> pmTime;
      procMiniTime = pmTime;
      if (debugOn == 1) cout << " the minimum effective process lifetime is : " << procMiniTime << endl;
    }

    if (temp ==  "LOGFILE_NAME") // unit_time, usually it is an hour
    {
      char hostName[80];
      string measureName;
      measureName = paraValues[j];
      if (gethostname(hostName, 80) != 0)
      {
        cout << "Can not get the host name to generate the measure log file " << endl;
        exit(1);
      }
      stringstream aStrstream;
      aStrstream << hostName;
      measureLog = aStrstream.str() + measureName; 
      if (debugOn == 1) cout << " the log file's name is: " << measureLog << endl;
    }

    if (temp ==  "LOGFILE_RENEW") // debug tag
    { 	
      logfileRenew = atoi(paraValues[j].c_str());;
      if (debugOn == 1) cout << " The logfile Renew is set as: " << logfileRenew << endl;
    }

    if (temp ==  "DEBUG") // debug tag
    { 	
      debugOn = atoi(paraValues[j].c_str());
      if (debugOn == 1) cout << "\n The debug set is: " << debugOn << endl;
    }

  }

  fdConfig.close();
}


/* 
 * start monitoring 
 */

int 
Monitor::Run()
{

  int currSampleNum = 1; 
        
  // These are the times in seconds(measured from the parsed output of the date command)
  // and converted into seconds) that are measured at the beginning and the ending of 
  // one hour. The difference gives the physical time(world time) elapsed to take one set of
  // readings

  long time2 = 0;

  // cumulative time, sleep period, measure period
  int cTime = 0, sTime = 10, mTime = 10;
  int p = 0;

  // initial unit_time for measuring, the maximum measuring time_unit
  int loopCtr = 0;
  int hourIndex = 0;	 

  time_t currTime = 0;
  int measureTime = 0;
                		
  time_t currentHour = 0;
  struct timeval tv;
  int measureInterval;

  //loop until the maximum measuring number is reached

  while (loopCtr < loopNum)
  {
    vector lvalues, mvalues, tvalues, rvalues;
    numIter = numIterNext; 
    gettimeofday(&tv, NULL);
    currentHour = tv.tv_sec / 3600 ;

    // measure system parameters in a time_unit

    int i = 1;
    currSampleNum = 1;

    measureInterval = sampleTime / numIter;

    //debugOn = 0;
    while (i < numIter+1)
    { 

    // measure the number of processes executed in the system, until this point from the out
    // put of the command "lastcomm | wc". Note this information is maitained in a file called 
    // /var/adm/pacct recent values overwrite the old values as the file becomes very large.

      mTime = (measureInterval/2 > 20)? 20:measureInterval/2; // measurement period is fixed as 20 second

      cTime += mTime;
				
      if (debugOn == 1) cout << "\n measure time is : " << mTime << endl;

      string firstline = myUtils.GetLastFinishedProc();
      if (debugOn == 1) cout << "first line is " << firstline;

      rvalues.push_back(myUtils.FindUtil(mTime) * 1.0/100.0);

      if (debugOn == 1) cout << "After " << mTime << " seconds " << endl;

      sampleStatistics = myUtils.GetFinishedProcNum(const_cast(firstline.c_str()), (float) mTime, (float) procMiniTime);

      double lambda = 0.0, miu = 0.0, therta = 0.0;

      lambda = sampleStatistics.arrivalRate;
      miu = sampleStatistics.serviceRate; // miu is actually the average job service time here
      //therta = sampleStatistics.serviceSTD;

      if (debugOn == 1) cout << "J_arrival : " << lambda << endl;
      if (debugOn == 1) cout << "J_serviceRate : " << miu << endl;
      if (debugOn == 1) cout << "J_serviceSTD : " << therta << endl;

      lvalues.push_back(lambda);
      mvalues.push_back(miu);
      for(int ii=0; iiRecordParameters((int) currentHour, mySta.CalculateMean(rhoValues[hourIndex]), mySta.CalculateMean(lambdaValues[hourIndex]), 
    mySta.CalculateNonZeroMean(muValues[hourIndex]), mySta.CalculateSTD(thetaValues[hourIndex]));
		
    // sd is calculated for all the samples obtained in the lasthour

    double lambdaAdaptSTD = mySta.CalculateAdaptiveSTD(lambdaValues, hourIndex, lambdaMeanPredict, lambdaSTDPredict);
    double rhoAdaptSTD = mySta.CalculateAdaptiveSTD(rhoValues, hourIndex, muMeanPredict, muSTDPredict);

    hourIndex++;

    numIterNext = GetNextSampleSize(hourIndex);

    if (debugOn == 1) cout << "\nThe number of iterations that should be in the next observation is " << numIterNext << endl;

    loopCtr ++;
                          
  } /*outside while loop */
  
  return 0;

}

int 
Monitor::GetNextSampleSize(int hIndex)
{
    int preNum;
    int iterNext;

    if (hIndex < 24) preNum = hIndex;
    else preNum = 24;

    if (muMeanPredict[preNum-1] == 0) 
    {
      if (debugOn == 1) cout << "the utilization prediction is 0 percent, so the number of iterations remains " << iterNext << endl;
    }
    else 
    {
      iterNext = (int) ceil( (1536.64 / 24) * 
		  pow((muSTDPredict[preNum-1] / muMeanPredict[preNum-1]), 2.0));
    }

    if (iterNext > MAX_SAMPLE_SIZE) 
    {
      iterNext = MAX_SAMPLE_SIZE;
    }
    else if (iterNext < MIN_SAMPLE_SIZE)
    {
      iterNext = MIN_SAMPLE_SIZE;
    }

	return iterNext;
}

int main(int argc, char** argv)
{
	Monitor myMonitor;

	myMonitor.Run();
}