/*
 * Copyright (C) 2008 The Trustees of Columbia University in the City of New York. 
 * All rights reserved.
 *
 * See the file LICENSE included in this distribution for details.
 */

#ifndef __CPUUTIL_H_
#define __CPUUTIL_H_

#include "gettimeofday.h"

#ifdef WIN32

#else

#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

#endif


class CpuUtil;

/*singleton class*/
class CpuUtil {

private:
  struct timeval m_stRT;  //recompute timer
  unsigned int m_uiRTexp; //recompute after this many seconds
  int32 m_isFlag; 

 public:
  ~CpuUtil() {
    if(m_pObj)
      delete m_pObj;
  }
  static CpuUtil* GetInstance();
  void Recompute();
  long double GetCpuUsage();
  long double GetTCpuUsage();

 private:
  //data
  static CpuUtil *m_pObj;

#ifndef WIN32
  struct timeval m_sStartTime, m_sCurrTime;
  struct rusage m_sPrevUsage, m_sUsage;

  unsigned long long uptime;  //for calculating cpu utilization
  unsigned long long idle;    //for calculating cpu utilization
#endif

  long double m_fCpuUsage;  //program cpu usage
  long double m_fTCpuUsage; //total cpu usage
  
  //functions
  CpuUtil();
  CpuUtil(const CpuUtil& obj) {}
  CpuUtil operator=(const CpuUtil& obj) {return *this;}

  void setRTTimer();
  int isRTexpired();

#ifndef WIN32
  int read_proc_stats(unsigned long long *idle, unsigned long long *uptime);
  double get_cpu_stats ();
#endif

  
};

#endif
