xosview  1.23
About: xosview is an X Windows based system monitor (cpu, memory, swap and network usage; interrupt and serial activities; load average).
  Fossies Dox: xosview-1.23.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pagemeter.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 1996, 2004 by Massimiliano Ghilardi ( ghilardi@cibs.sns.it )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6 
7 #include "pagemeter.h"
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <string.h>
12 #include <fstream>
13 #include <iostream>
14 
15 #define MAX_PROCSTAT_LENGTH 4096
16 
17 
18 PageMeter::PageMeter( XOSView *parent, float max )
19  : FieldMeterGraph( parent, 3, "PAGE", "IN/OUT/IDLE" ),
20  _vmstat(false), _statFileName("/proc/stat"){
21  for ( int i = 0 ; i < 2 ; i++ )
22  for ( int j = 0 ; j < 2 ; j++ )
23  pageinfo_[j][i] = 0;
24 
25  maxspeed_ = max;
26  pageindex_ = 0;
27 
28  struct stat buf;
29  if (stat("/proc/vmstat", &buf) == 0
30  && buf.st_mode & S_IFREG)
31  {
32  _vmstat = true;
33  _statFileName = "/proc/vmstat";
34  }
35 }
36 
37 PageMeter::~PageMeter( void ){
38 }
39 
40 void PageMeter::checkResources( void ){
42 
43  setfieldcolor( 0, parent_->getResource( "pageInColor" ) );
44  setfieldcolor( 1, parent_->getResource( "pageOutColor" ) );
45  setfieldcolor( 2, parent_->getResource( "pageIdleColor" ) );
46  priority_ = atoi (parent_->getResource( "pagePriority" ) );
47  maxspeed_ *= priority_ / 10.0;
48  dodecay_ = parent_->isResourceTrue( "pageDecay" );
49  useGraph_ = parent_->isResourceTrue( "pageGraph" );
50  SetUsedFormat (parent_->getResource("pageUsedFormat"));
51 }
52 
53 void PageMeter::checkevent( void ){
54  if (_vmstat)
55  getvmpageinfo();
56  else
57  getpageinfo();
58  drawfields();
59 }
60 
61 void PageMeter::updateinfo(void)
62  {
63  int oldindex = (pageindex_+1)%2;
64  for ( int i = 0; i < 2; i++ )
65  {
66  if ( pageinfo_[oldindex][i] == 0 )
67  pageinfo_[oldindex][i] = pageinfo_[pageindex_][i];
68 
69  fields_[i] = pageinfo_[pageindex_][i] - pageinfo_[oldindex][i];
70  total_ += fields_[i];
71  }
72 
73  if ( total_ > maxspeed_ )
74  fields_[2] = 0.0;
75  else
76  {
77  fields_[2] = maxspeed_ - total_;
78  total_ = maxspeed_;
79  }
80 
82  pageindex_ = (pageindex_ + 1) % 2;
83  }
84 
86  {
87  total_ = 0;
88  char buf[MAX_PROCSTAT_LENGTH];
89  bool found_in = false, found_out = false;
90  std::ifstream stats(_statFileName);
91  if (!stats)
92  {
93  std::cerr <<"Cannot open file : " << _statFileName << std::endl;
94  exit(1);
95  }
96  while (!stats.eof() && !(found_in && found_out))
97  {
98  stats.getline(buf, MAX_PROCSTAT_LENGTH);
99  if (!strncmp(buf, "pswpin", 6))
100  {
101  pageinfo_[pageindex_][0] = strtoul(buf+7, NULL, 10);
102  found_in = true;
103  }
104  if (!strncmp(buf, "pswpout", 7))
105  {
106  pageinfo_[pageindex_][1] = strtoul(buf+8, NULL, 10);
107  found_out = true;
108  }
109  }
110  updateinfo();
111  }
112 
113 void PageMeter::getpageinfo( void ){
114  total_ = 0;
115  char buf[MAX_PROCSTAT_LENGTH];
116  std::ifstream stats(_statFileName);
117 
118  if ( !stats ){
119  std::cerr <<"Cannot open file : " << _statFileName << std::endl;
120  exit( 1 );
121  }
122 
123  do {
124  stats >>buf;
125  } while (!stats.eof() && strncasecmp(buf, "swap", 5));
126 
127  stats >>pageinfo_[pageindex_][0] >>pageinfo_[pageindex_][1];
128 
129  updateinfo();
130 }
virtual void drawfields(int mandatory=0)
virtual void checkResources(void)
double total_
Definition: fieldmeter.h:41
void setfieldcolor(int field, const char *color)
Definition: fieldmeter.cc:108
void setUsed(double val, double total)
Definition: fieldmeter.cc:76
double * fields_
Definition: fieldmeter.h:40
void SetUsedFormat(const char *const str)
Definition: fieldmeter.cc:60
int priority_
Definition: meter.h:54
XOSView * parent_
Definition: meter.h:52
~PageMeter(void)
Definition: pagemeter.cc:29
float pageinfo_[2][2]
Definition: pagemeter.h:23
void getvmpageinfo(void)
Definition: pagemeter.cc:85
const char * _statFileName
Definition: pagemeter.h:28
void updateinfo(void)
Definition: pagemeter.cc:53
int pageindex_
Definition: pagemeter.h:24
void getpageinfo(void)
Definition: pagemeter.cc:49
bool _vmstat
Definition: pagemeter.h:27
float maxspeed_
Definition: pagemeter.h:25
PageMeter(XOSView *parent, double total)
Definition: pagemeter.cc:22
void checkevent(void)
Definition: pagemeter.cc:44
void checkResources(void)
Definition: pagemeter.cc:32
int isResourceTrue(const char *name)
Definition: xwin.h:89
const char * getResource(const char *name)
Definition: xwin.cc:362
static const int max
Definition: intmeter.cc:17
#define MAX_PROCSTAT_LENGTH
Definition: pagemeter.cc:15