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


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

using namespace std;

/* This function converts string to integer
 * same as stoi 
 */

int 
UnixUtilities::stoi(char *str)
{
  int len, i = 0, num = 0;
  int mul = 1;
        
  len = (int) strlen(str);

  for(i = len-1; i >= 0; i--)
  {
    num += (str[i] - 48) * mul;
    mul *= 10;
  }
  
  return num;
}       

/* This function reads the time from the date command in 
 *	HH:MM:SS format and converts into total number of seconds   by multiplying 
 *	HH * 3600 + MM * 60 + ss 
 */
long 
UnixUtilities::FindDate(char *buf)
{
  char *temp, *temp1;
  long time1;
  char startDate[40];

  strcpy(startDate, "");	
                        
  temp = strtok(buf," ");
  temp = strtok(NULL," ");
  strcat(startDate, temp); 
  temp = strtok(NULL," ");
  strcat(startDate, temp); 
  temp = strtok(NULL," ");
  if (DEBUG_ON == 1) cout << startDate << " " << temp << endl;

  temp1 = strtok(temp,":");
  strcat(startDate, temp1); 
  time1 = atol(temp1) * 3600;

  temp1 = strtok(NULL,":");
  strcat(startDate, temp1); 
  time1 += atol(temp1) * 60;

  temp1 = strtok(NULL,":");
  time1 += atol(temp1);
                        
  return time1;

}

/* This function returns the number of processes in system by calling psCmd
 *	char *psCmd = "ps -ef | wc"; 
 */

int 
UnixUtilities::FindProcNum()
{
  int procNum = 0;
  char *psCmd = "ps -ef | wc";
  char buf[BUF_SIZE];
  char *str;
  FILE *ptr;
             
  if ((ptr = popen(psCmd, "r")) != NULL)
  {
    while (fgets(buf, BUF_SIZE, ptr) != NULL)
    {   
      if (DEBUG_ON == 1) cout << buf << endl; 
      str = strtok(buf," ");
      procNum = atoi(str);
    }

    pclose(ptr);
  }

  return(procNum);

}

/* This function returns the M_utilization by calling vsCmd + time interval
 *	char *vmstatCmd = "vmstat ";
 *  Modified to return available system capacity, that 100 - utilization 
 */

int 
UnixUtilities::FindUtil(int timeInterval)
{
  FILE *ptr;
  string vmstatCmd("vmstat ");
  string tmpCmd;
  char *str;
  char buf[BUF_SIZE];
  int uUser, uSys, numIter;
  int jumpNum;

  #ifdef SunOS 
    jumpNum = 19;
  #else
    jumpNum = 13;
  #endif

  stringstream aStrstream;
  aStrstream << timeInterval;
  tmpCmd = vmstatCmd + aStrstream.str() + " 2"; //?
  if (DEBUG_ON == 1) cout << "vmstat cmd : " << tmpCmd << endl;
  if ((ptr = popen(tmpCmd.c_str(), "r")) != NULL)
  {
    while (fgets(buf, BUF_SIZE, ptr) != NULL)
    {
      str = fgets(buf, BUF_SIZE, ptr);
      str = fgets(buf, BUF_SIZE, ptr);
      str = fgets(buf, BUF_SIZE, ptr);
      /*str = fgets(buf, BUF_SIZE, ptr);
      str = fgets(buf, BUF_SIZE, ptr); */

      if (DEBUG_ON == 1) cout << "New Cycle: " << buf << endl;

      str = strtok(buf,"  ");
      for(numIter = 0; numIter < jumpNum; numIter++) {
        str = strtok(NULL,"  ");
        if(!str)  break;
      } 
      if(str) {
        uUser = atoi(str);
        str = strtok(NULL,"  ");
        uSys = (str)?atoi(str):0;
      } else {
        uUser = 0;
        uSys = 0;
      }

      if (DEBUG_ON == 1) cout << "The Utilization percentage is " << uSys + uUser << endl;

    }
    pclose(ptr);
	
  }
  
  return(uSys + uUser);

}

/* get the number of finished processes after the specific previous process whose 
 * information is given by the firstline of the output of lastcomm utility on last call
 */

struct _JobStatistics 
UnixUtilities::GetFinishedProcNum(char *firstLine, float timePeriod, float procMinTime)
{
  int iLc = 0, jLc = 0; // finished process number
  int flLength = 0, tempLc = 0;
  char *str, timeStr[20];
  char bufNext[100], bufTmp[100];
  char tokensFirst[20][20];
  FILE *ptr;
  // add later 
  double averServ, averSTD;
  struct _JobStatistics tmpJobstat;
  int matchedNum; //matched number of index in a line
  float procTime, arrivalRate;
  int i;
  vector jobServTime;

  // Tokenize the first string

  str = strtok(firstLine," ");
  strcpy(tokensFirst[0],str);
  if (DEBUG_ON == 1) cout << "the first string" << endl;
  if (DEBUG_ON == 1) cout << "\n" << str << endl;

  iLc=1;
  str=strtok(NULL," "); strcpy(tokensFirst[iLc++] , str);
  if (DEBUG_ON == 1) cout << str << endl;
  str=strtok(NULL," "); iLc++;
  str=strtok(NULL," "); strcpy(tokensFirst[iLc++] , str);
  if (DEBUG_ON == 1) cout << str << endl;
  str=strtok(NULL," "); strcpy(tokensFirst[iLc++] , str);
  if (DEBUG_ON == 1) cout << str << endl;
  str=strtok(NULL," "); strcpy(tokensFirst[iLc++] , str);
  if (DEBUG_ON == 1) cout << str << endl;
  str=strtok(NULL," "); strcpy(tokensFirst[iLc++] , str);
  if (DEBUG_ON == 1) cout << str << endl;
  str=strtok(NULL," "); strcpy(tokensFirst[iLc++] , str);
  if (DEBUG_ON == 1) cout << str << endl;
  str=strtok(NULL," "); strcpy(tokensFirst[iLc++] , str);
  if (DEBUG_ON == 1) cout << str << endl;

  flLength = iLc;
  if (DEBUG_ON == 1) cout << "The length of the first line is " << flLength << endl;
  iLc= 0;

  /* find current lastcommand output and compare the first line of it
   * with tokensFirst which store the first line of the last lastcommand
   * output
  */

  int jobNum = 0; //the number of jobs which service time is satisfied with the constraint

  if ((ptr = popen("lastcomm", "r")) != NULL)
  {
    while ((fgets(bufNext, BUF_SIZE, ptr) != NULL) && (iLc < MAX_PROC_NUM) )
    {   
      if (DEBUG_ON == 1) cout << "bufNext=  " << bufNext << endl;
      strcpy(bufTmp, bufNext);
      str = strtok(bufNext," ");
      strcpy(timeStr, str);

      jLc = 0;
      matchedNum = 0;

      if (strncmp(str,tokensFirst[jLc],strlen(str)) == 0) matchedNum++;   
      if (DEBUG_ON == 1) cout << "You are in the loop" << endl;
      jLc = 1;
      tempLc = 0;
      
      while( jLc < flLength )
      {
        if (DEBUG_ON == 1) cout << "You are in the inner loop" << endl;
        if (jLc == 2) 
        {
          str = strtok(NULL," ");
          jLc++;
          matchedNum++;
        }
								
        str = strtok(NULL," ");
        if (DEBUG_ON == 1) cout << "jLc= " << jLc << " " << str << " " << tokensFirst[jLc] << endl;

        if (strncmp(str, "secs", strlen(str)) == 0)
        {
          sscanf(timeStr,"%f",&procTime);
          if ( procTime >= procMinTime)
          {
            jobServTime.push_back(procTime);
            if (DEBUG_ON == 1) cout << "the job " << jobNum << " service time is " << jobServTime[jobNum] << endl;
            jobNum++;
          }
        }
						
        if (strncmp(str,tokensFirst[jLc], strlen(str)) == 0) matchedNum++;
        jLc++;
        strcpy(timeStr, str);
      };

      if ( matchedNum == flLength)
      {
        if (DEBUG_ON == 1) cout << "the line is mapped " << endl;
        // the processes should not include the first line
        if (procTime >= procMinTime) jobNum--;
        break;
      }
      else iLc++;
    }//End of while

    if (DEBUG_ON == 1) cout << "The number of commands executed in this interval are: " << jobNum << endl;
  }

  pclose(ptr);

  // *************************** error on job arrival calculation *******************
  if (jobNum > 0 )
  {
    arrivalRate = jobNum / timePeriod;
    averServ = mySta.CalculateMean(jobServTime);
    //cout << "jobs in this measurement : " << endl;
    //for(int dd = 0; dd < jobServTime.size(); dd++) cout << jobServTime[dd] << " ";
    //cout << "the end" << endl;
    //averSTD = mySta.CalculateSTD(jobServTime);
  } 
  else {
    arrivalRate = 0;
    averServ = 0;
    //averSTD = 0;
  }
	
  tmpJobstat.arrivalRate = arrivalRate;
  tmpJobstat.serviceRate = averServ;
  for (int i=0; i< jobServTime.size(); i++)  tmpJobstat.lifetimes.push_back(jobServTime[i]);

  //if ( (matchedNum != flLength) || (jobNum== 0) ) tmpJobstat.arrivalRate = -1;

  return(tmpJobstat);
}

string 
UnixUtilities::GetLastFinishedProc()
{
  char bufLastCMD[BUF_SIZE], firstline[BUF_SIZE];
  int lastcommOn = 0;
  FILE *ptr;
  
  while ( lastcommOn == 0) 
  {
    if ((ptr = popen("lastcomm", "r")) != NULL)
    {
      if (fgets(bufLastCMD, BUF_SIZE, ptr) != NULL)
      {   
        strcpy(firstline, bufLastCMD);
        if (DEBUG_ON == 1) cout << "\nThe first line is: " << firstline << endl;
        lastcommOn = 1;
      }
      else 
      {
        if (DEBUG_ON == 1) cout << "the lastcomm can't be executed " << endl;
        sleep(1);
      }
       pclose(ptr);
    }
  }

  stringstream aStrstream;
  aStrstream << firstline;
  return aStrstream.str();
}