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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
//
// Copyright (c) 1996, 2004 by Massimiliano Ghilardi ( ghilardi@cibs.sns.it )
//
// This file may be distributed under terms of the GPL
//
#include "pagemeter.h"
#include "xosview.h"
#include <fstream>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_PROCSTAT_LENGTH 4096
PageMeter::PageMeter( XOSView *parent, float max )
: FieldMeterGraph( parent, 3, "PAGE", "IN/OUT/IDLE" ),
_vmstat(false), _statFileName("/proc/stat"){
for ( int i = 0 ; i < 2 ; i++ )
for ( int j = 0 ; j < 2 ; j++ )
pageinfo_[j][i] = 0;
maxspeed_ = max;
pageindex_ = 0;
struct stat buf;
if (stat("/proc/vmstat", &buf) == 0
&& buf.st_mode & S_IFREG)
{
_vmstat = true;
_statFileName = "/proc/vmstat";
}
}
PageMeter::~PageMeter( void ){
}
void PageMeter::checkResources( void ){
FieldMeterGraph::checkResources();
setfieldcolor( 0, parent_->getResource( "pageInColor" ) );
setfieldcolor( 1, parent_->getResource( "pageOutColor" ) );
setfieldcolor( 2, parent_->getResource( "pageIdleColor" ) );
priority_ = atoi (parent_->getResource( "pagePriority" ) );
maxspeed_ *= priority_ / 10.0;
dodecay_ = parent_->isResourceTrue( "pageDecay" );
useGraph_ = parent_->isResourceTrue( "pageGraph" );
SetUsedFormat (parent_->getResource("pageUsedFormat"));
}
void PageMeter::checkevent( void ){
if (_vmstat)
getvmpageinfo();
else
getpageinfo();
drawfields();
}
void PageMeter::updateinfo(void)
{
int oldindex = (pageindex_+1)%2;
for ( int i = 0; i < 2; i++ )
{
if ( pageinfo_[oldindex][i] == 0 )
pageinfo_[oldindex][i] = pageinfo_[pageindex_][i];
fields_[i] = pageinfo_[pageindex_][i] - pageinfo_[oldindex][i];
total_ += fields_[i];
}
if ( total_ > maxspeed_ )
fields_[2] = 0.0;
else
{
fields_[2] = maxspeed_ - total_;
total_ = maxspeed_;
}
setUsed (total_ - fields_[2], maxspeed_);
pageindex_ = (pageindex_ + 1) % 2;
}
void PageMeter::getvmpageinfo(void)
{
total_ = 0;
char buf[MAX_PROCSTAT_LENGTH];
bool found_in = false, found_out = false;
std::ifstream stats(_statFileName);
if (!stats)
{
std::cerr <<"Cannot open file : " << _statFileName << std::endl;
exit(1);
}
while (!stats.eof() && !(found_in && found_out))
{
stats.getline(buf, MAX_PROCSTAT_LENGTH);
if (!strncmp(buf, "pswpin", 6))
{
pageinfo_[pageindex_][0] = strtoul(buf+7, NULL, 10);
found_in = true;
}
if (!strncmp(buf, "pswpout", 7))
{
pageinfo_[pageindex_][1] = strtoul(buf+8, NULL, 10);
found_out = true;
}
}
updateinfo();
}
void PageMeter::getpageinfo( void ){
total_ = 0;
char buf[MAX_PROCSTAT_LENGTH];
std::ifstream stats(_statFileName);
if ( !stats ){
std::cerr <<"Cannot open file : " << _statFileName << std::endl;
exit( 1 );
}
do {
stats >>buf;
} while (!stats.eof() && strncasecmp(buf, "swap", 5));
stats >>pageinfo_[pageindex_][0] >>pageinfo_[pageindex_][1];
updateinfo();
}
|