"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/linux/loadmeter.cc" (11 Jul 2020, 4601 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 "loadmeter.cc" see the
Fossies "Dox" file reference documentation.
1 //
2 // Copyright (c) 1994, 1995, 2006, 2008 by Mike Romberg ( mike.romberg@noaa.gov )
3 //
4 // This file may be distributed under terms of the GPL
5 //
6 // Most of this code was written by Werner Fink <werner@suse.de>.
7 // Only small changes were made on my part (M.R.)
8 //
9
10 #include "loadmeter.h"
11 #include "cpumeter.h"
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <fstream>
16 #include <iostream>
17 #include <string>
18
19 static const char LOADFILENAME[] = "/proc/loadavg";
20 static const char SPEEDFILENAME[] = "/proc/cpuinfo";
21
22
23 LoadMeter::LoadMeter( XOSView *parent )
24 : FieldMeterGraph( parent, 2, "LOAD", "PROCS/MIN", 1, 1, 0 ){
25 lastalarmstate = -1;
26 total_ = 2.0;
27 old_cpu_speed_= cur_cpu_speed_=0;
28 do_cpu_speed = 0;
29
30 }
31
32 LoadMeter::~LoadMeter( void ){
33 }
34
35 void LoadMeter::checkResources( void ){
36 FieldMeterGraph::checkResources();
37
38 procloadcol_ = parent_->allocColor(parent_->getResource( "loadProcColor" ));
39 warnloadcol_ = parent_->allocColor(parent_->getResource( "loadWarnColor" ));
40 critloadcol_ = parent_->allocColor(parent_->getResource( "loadCritColor" ));
41
42 setfieldcolor( 0, procloadcol_ );
43 setfieldcolor( 1, parent_->getResource( "loadIdleColor" ) );
44 priority_ = atoi (parent_->getResource( "loadPriority" ) );
45 useGraph_ = parent_->isResourceTrue( "loadGraph" );
46 dodecay_ = parent_->isResourceTrue( "loadDecay" );
47 SetUsedFormat (parent_->getResource("loadUsedFormat"));
48
49 const char *warn = parent_->getResource("loadWarnThreshold");
50 if (strncmp(warn, "auto", 2) == 0) {
51 warnThreshold = CPUMeter::countCPUs();
52 } else {
53 warnThreshold = atoi(warn);
54 }
55
56 const char *crit = parent_->getResource("loadCritThreshold");
57 if (strncmp(crit, "auto", 2) == 0) {
58 critThreshold = warnThreshold * 4;
59 } else {
60 critThreshold = atoi(crit);
61 }
62
63 do_cpu_speed = parent_->isResourceTrue( "loadCpuSpeed" );
64
65 if (dodecay_){
66 // Warning: Since the loadmeter changes scale occasionally, old
67 // decay values need to be rescaled. However, if they are rescaled,
68 // they could go off the edge of the screen. Thus, for now, to
69 // prevent this whole problem, the load meter can not be a decay
70 // meter. The load is a decaying average kind of thing anyway,
71 // so having a decaying load average is redundant.
72 std::cerr << "Warning: The loadmeter can not be configured as a decay\n"
73 << " meter. See the source code (" << __FILE__ << ") for further\n"
74 << " details.\n";
75 dodecay_ = 0;
76 }
77 }
78
79 void LoadMeter::checkevent( void ){
80 getloadinfo();
81 if ( do_cpu_speed ) {
82 getspeedinfo();
83 if ( old_cpu_speed_ != cur_cpu_speed_ ) {
84 // update the legend:
85 char l[32];
86 snprintf(l, 32, "PROCS/MIN %d MHz", cur_cpu_speed_);
87 legend(l);
88 drawlegend();
89 }
90 }
91
92 drawfields();
93 }
94
95
96 void LoadMeter::getloadinfo( void ){
97 std::ifstream loadinfo( LOADFILENAME );
98
99 if ( !loadinfo ){
100 std::cerr <<"Can not open file : " <<LOADFILENAME << std::endl;
101 parent_->done(1);
102 return;
103 }
104
105 loadinfo >> fields_[0];
106
107 if ( fields_[0] < warnThreshold ) alarmstate = 0;
108 else
109 if ( fields_[0] >= critThreshold ) alarmstate = 2;
110 else
111 /* if fields_[0] >= warnThreshold */ alarmstate = 1;
112
113 if ( alarmstate != lastalarmstate ){
114 if ( alarmstate == 0 ) setfieldcolor( 0, procloadcol_ );
115 else
116 if ( alarmstate == 1 ) setfieldcolor( 0, warnloadcol_ );
117 else
118 /* if alarmstate == 2 */ setfieldcolor( 0, critloadcol_ );
119 drawlegend();
120 lastalarmstate = alarmstate;
121 }
122
123 // Adjust total to next power-of-two of the current load.
124 if ( (fields_[0]*5.0 < total_ && total_ > 1.0) || fields_[0] > total_ ) {
125 unsigned int i = fields_[0];
126 i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16; // i = 2^n - 1
127 total_ = i + 1;
128 }
129
130 fields_[1] = (float) (total_ - fields_[0]);
131
132 setUsed(fields_[0], (float) 1.0);
133 }
134
135 // just check /proc/cpuinfo for the speed of cpu
136 // (average multi-cpus on different speeds)
137 // (yes - i know about devices/system/cpu/cpu*/cpufreq )
138 void LoadMeter::getspeedinfo( void ){
139 std::ifstream speedinfo(SPEEDFILENAME);
140 std::string line, val;
141 unsigned int total_cpu = 0, ncpus = 0;
142
143 while ( speedinfo.good() ) {
144 std::getline(speedinfo, line);
145 if ( strncmp(line.c_str(), "cpu MHz", 7) == 0 ) {
146 val = line.substr(line.find_last_of(':') + 1);
147 XOSDEBUG("SPEED: %s\n", val.c_str());
148 total_cpu += atoi(val.c_str());
149 ncpus++;
150 }
151 }
152
153 old_cpu_speed_ = cur_cpu_speed_;
154 if (ncpus > 0)
155 cur_cpu_speed_ = total_cpu / ncpus;
156 else
157 cur_cpu_speed_ = 0;
158 }