//  
//  Copyright (c) 1994 by Mike Romberg (  romberg@md.fsl.noaa.gov )
//  Debugged and Featurized by Michael Hemy
//
//  This file may be distributed under terms of the GPL
//

#include "cpumeter.h"
#include <stdio.h>

CPUMeter::CPUMeter( XOSView *parent, int x, int y, int width, int height )
: FieldMeter( parent, x, y, width, height, 4, "CPU", "USR NICE SYS FREE" ){
  for ( int i = 0 ; i < 2 ; i++ )
    for ( int j = 0 ; j < 4 ; j++ )
      cputime_[i][j] = 0;
  cpuindex_ = 0;

  stats_ = new ifstream( STATFILENAME );
}

CPUMeter::~CPUMeter( void ){
  delete stats_;
}

void CPUMeter::checkevent( void ){
  getcputime();
  drawfields();
}

void CPUMeter::getcputime( void ){
  total_ = 0;
  stats_->seekg( 0 );
  char tmp[10];

  *stats_ >>tmp >>cputime_[cpuindex_][0]  
	        >>cputime_[cpuindex_][1]  
	        >>cputime_[cpuindex_][2]  
	        >>cputime_[cpuindex_][3];

  int oldindex = (cpuindex_+1)%2;
  for ( int i = 0 ; i < 4 ; i++ ){
    fields_[i] = cputime_[cpuindex_][i] - cputime_[oldindex][i];
    total_ += fields_[i];
  }
  extra_val_ = 100*((total_ - fields_[3])/total_);  
  sprintf(extra_str_,"%3d%%", int(extra_val_));
  cpuindex_ = (cpuindex_ + 1) % 2;
}
