"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/linux/pagemeter.cc" (11 Jul 2020, 3220 Bytes) of package /linux/misc/xosview-1.23.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "pagemeter.cc" see the
Fossies "Dox" file reference documentation.
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 ){
41 FieldMeterGraph::checkResources();
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
81 setUsed (total_ - fields_[2], maxspeed_);
82 pageindex_ = (pageindex_ + 1) % 2;
83 }
84
85 void PageMeter::getvmpageinfo(void)
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 }