"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/sunos5/loadmeter.cc" (11 Jul 2020, 4891 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 // Initial port performed by Greg Onufer (exodus@cheers.bungi.com)
3 //
4
5 #include "loadmeter.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <iostream>
11 #ifdef NO_GETLOADAVG
12 #ifndef FSCALE
13 #define FSCALE (1<<8)
14 #endif
15 #else
16 #include <sys/loadavg.h>
17 #endif
18
19
20 LoadMeter::LoadMeter(XOSView *parent, kstat_ctl_t *_kc)
21 : FieldMeterGraph(parent, 2, "LOAD", "PROCS/MIN", 1, 1, 0)
22 {
23 kc = _kc;
24 cpulist = KStatList::getList(kc, KStatList::CPU_INFO);
25 #ifdef NO_GETLOADAVG
26 ksp = kstat_lookup(kc, "unix", 0, "system_misc");
27 if (ksp == NULL) {
28 parent_->done(1);
29 return;
30 }
31 #endif
32 total_ = -1;
33 lastalarmstate = -1;
34 old_cpu_speed = cur_cpu_speed = 0;
35 }
36
37 LoadMeter::~LoadMeter(void)
38 {
39 }
40
41 void LoadMeter::checkResources(void)
42 {
43 FieldMeterGraph::checkResources();
44
45 warnloadcol = parent_->allocColor(parent_->getResource("loadWarnColor"));
46 procloadcol = parent_->allocColor(parent_->getResource("loadProcColor"));
47 critloadcol = parent_->allocColor(parent_->getResource("loadCritColor"));
48
49 setfieldcolor(0, procloadcol);
50 setfieldcolor(1, parent_->getResource("loadIdleColor"));
51 priority_ = atoi (parent_->getResource("loadPriority"));
52 dodecay_ = parent_->isResourceTrue("loadDecay");
53 useGraph_ = parent_->isResourceTrue("loadGraph");
54 SetUsedFormat(parent_->getResource("loadUsedFormat"));
55 do_cpu_speed = parent_->isResourceTrue("loadCpuSpeed");
56
57 const char *warn = parent_->getResource("loadWarnThreshold");
58 if (strncmp(warn, "auto", 2) == 0)
59 warnThreshold = sysconf(_SC_NPROCESSORS_ONLN);
60 else
61 warnThreshold = atoi(warn);
62
63 const char *crit = parent_->getResource("loadCritThreshold");
64 if (strncmp(crit, "auto", 2) == 0)
65 critThreshold = warnThreshold * 4;
66 else
67 critThreshold = atoi(crit);
68
69 if (dodecay_){
70 /*
71 * Warning: Since the loadmeter changes scale
72 * occasionally, old decay values need to be rescaled.
73 * However, if they are rescaled, they could go off the
74 * edge of the screen. Thus, for now, to prevent this
75 * whole problem, the load meter can not be a decay
76 * meter. The load is a decaying average kind of thing
77 * anyway, so having a decaying load average is
78 * redundant.
79 */
80 std::cerr << "Warning: The loadmeter can not be configured as a decay\n"
81 << " meter. See the source code (" << __FILE__ << ") for further\n"
82 << " details.\n";
83 dodecay_ = 0;
84 }
85 }
86
87 void LoadMeter::checkevent(void)
88 {
89 getloadinfo();
90 if (do_cpu_speed) {
91 getspeedinfo();
92 if (old_cpu_speed != cur_cpu_speed) {
93 // update the legend:
94 char l[32];
95 snprintf(l, 32, "PROCS/MIN %d MHz", cur_cpu_speed);
96 legend(l);
97 drawlegend();
98 }
99 }
100 drawfields();
101 }
102
103 void LoadMeter::getloadinfo(void)
104 {
105 int alarmstate;
106 #ifdef NO_GETLOADAVG
107 // This code is mainly for Solaris 6 and earlier, but should work on
108 // any version.
109 kstat_named_t *k;
110
111 if (kstat_read(kc, ksp, NULL) == -1) {
112 parent_->done(1);
113 return;
114 }
115 k = (kstat_named_t *)kstat_data_lookup(ksp, "avenrun_1min");
116 if (k == NULL) {
117 parent_->done(1);
118 return;
119 }
120 fields_[0] = kstat_to_double(k) / FSCALE;
121 #else
122 // getloadavg() if found on Solaris 7 and newer.
123 getloadavg(&fields_[0], 1);
124 #endif
125
126 if (fields_[0] < warnThreshold)
127 alarmstate = 0;
128 else if (fields_[0] >= critThreshold)
129 alarmstate = 2;
130 else /* if fields_[0] >= warnThreshold */
131 alarmstate = 1;
132
133 if (alarmstate != lastalarmstate) {
134 if (alarmstate == 0)
135 setfieldcolor(0, procloadcol);
136 else if (alarmstate == 1)
137 setfieldcolor(0, warnloadcol);
138 else /* if alarmstate == 2 */
139 setfieldcolor(0, critloadcol);
140 drawlegend();
141 lastalarmstate = alarmstate;
142 }
143
144 // Adjust total to next power-of-two of the current load.
145 if ( (fields_[0]*5.0 < total_ && total_ > 1.0) || fields_[0] > total_ ) {
146 unsigned int i = fields_[0];
147 i |= i >> 1; i |= i >> 2; i |= i >> 4; i |= i >> 8; i |= i >> 16; // i = 2^n - 1
148 total_ = i + 1;
149 }
150
151 fields_[1] = total_ - fields_[0];
152 setUsed(fields_[0], total_);
153 }
154
155 void LoadMeter::getspeedinfo(void)
156 {
157 unsigned int total_mhz = 0, i = 0;
158 kstat_named_t *k;
159 kstat_t *cpu;
160 cpulist->update(kc);
161
162 for (i = 0; i < cpulist->count(); i++) {
163 cpu = (*cpulist)[i];
164 if (kstat_read(kc, cpu, NULL) == -1) {
165 parent_->done(1);
166 return;
167 }
168 // Try current_clock_Hz first (needs frequency scaling support),
169 // then clock_MHz.
170 k = (kstat_named_t *)kstat_data_lookup(cpu, "current_clock_Hz");
171 if (k == NULL) {
172 k = (kstat_named_t *)kstat_data_lookup(cpu, "clock_MHz");
173 if (k == NULL) {
174 std::cerr << "CPU speed is not available." << std::endl;
175 parent_->done(1);
176 return;
177 }
178 XOSDEBUG("Speed of cpu %d is %lld MHz\n", i, kstat_to_ui64(k));
179 total_mhz += kstat_to_ui64(k);
180 }
181 else {
182 XOSDEBUG("Speed of cpu %d is %lld Hz\n", i, kstat_to_ui64(k));
183 total_mhz += ( kstat_to_ui64(k) / 1000000 );
184 }
185 }
186 old_cpu_speed = cur_cpu_speed;
187 cur_cpu_speed = ( i > 0 ? total_mhz / i : 0 );
188 }
189