1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
//
// Copyright (c) 1999 by Mike Romberg (romberg@fsl.noaa.gov)
//
// This file may be distributed under terms of the GPL
//
// $Id: diskmeter.cc,v 1.13 1999/11/07 20:30:58 romberg Exp $
//
#include "diskmeter.h"
#include "xosview.h"
#include <fstream.h>
#include <stdlib.h>
static const char STATFILENAME[] = "/proc/stat";
DiskMeter::DiskMeter( XOSView *parent, float max ) : FieldMeterGraph(
parent, 3, "DISK", "READ/WRITE/IDLE")
{
read_prev_ = 0;
write_prev_ = 0;
maxspeed_ = max;
getdiskinfo();
}
DiskMeter::~DiskMeter( void )
{
}
void DiskMeter::checkResources( void )
{
FieldMeterGraph::checkResources();
setfieldcolor( 0, parent_->getResource("diskReadColor") );
setfieldcolor( 1, parent_->getResource("diskWriteColor") );
setfieldcolor( 2, parent_->getResource("diskIdleColor") );
priority_ = atoi (parent_->getResource( "diskPriority" ) );
dodecay_ = parent_->isResourceTrue("diskDecay" );
useGraph_ = parent_->isResourceTrue( "diskGraph" );
SetUsedFormat(parent_->getResource("diskUsedFormat"));
}
void DiskMeter::checkevent( void )
{
getdiskinfo();
drawfields();
}
void DiskMeter::getdiskinfo( void )
{
IntervalTimerStop();
total_ = maxspeed_;
char buf[1024];
ifstream stats( STATFILENAME );
if ( !stats )
{
cerr <<"Can not open file : " <<STATFILENAME <<endl;
exit( 1 );
}
// Find the line with 'page'
stats >> buf;
while (strncmp(buf, "page", 9))
{
stats.ignore(1024, '\n');
stats >> buf;
}
// read values
unsigned long one, two;
stats >> one >> two;
// assume each "unit" is 1k.
// This is true for ext2, but seems to be 512 bytes
// for vfat and 2k for cdroms
// work in 512-byte blocks
// tw: strange, on my system, a ext2fs (read and write)
// unit seems to be 2048. kernel 2.2.12 and the file system
// is on a SW-RAID5 device (/dev/md0).
// So this is a FIXME - but how ???
float itim = IntervalTimeInMicrosecs();
unsigned long read_curr = one * 2; // FIXME!
unsigned long write_curr = two * 2; // FIXME!
// avoid strange values at first call
if(read_prev_ == 0) read_prev_ = read_curr;
if(write_prev_ == 0) write_prev_ = write_curr;
// calculate rate in bytes per second
fields_[0] = ((read_curr - read_prev_) * 1e6 * 512) / itim;
fields_[1] = ((write_curr - write_prev_) * 1e6 * 512) / itim;
// fix overflow (conversion bug?)
if (fields_[0] < 0.0)
fields_[0] = 0.0;
if (fields_[1] < 0.0)
fields_[1] = 0.0;
if (fields_[0] + fields_[1] > total_)
total_ = fields_[0] + fields_[1];
fields_[2] = total_ - (fields_[0] + fields_[1]);
read_prev_ = read_curr;
write_prev_ = write_curr;
setUsed((fields_[0]+fields_[1]), total_);
IntervalTimerStart();
}
|