"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/gnu/loadmeter.cc" (11 Jul 2020, 3255 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 by Mike Romberg ( romberg@fsl.noaa.gov )
3 // 2007 by Samuel Thibault ( samuel.thibault@ens-lyon.org )
4 //
5 // This file may be distributed under terms of the GPL
6 //
7 // Most of this code was written by Werner Fink <werner@suse.de>.
8 // Only small changes were made on my part (M.R.)
9 //
10
11 #include "loadmeter.h"
12 #include "xosview.h"
13 #include <fstream>
14 #include <stdlib.h>
15 #include <error.h>
16
17 extern "C" {
18 #include <mach/mach_traps.h>
19 #include <mach/mach_host.h>
20 }
21
22 LoadMeter::LoadMeter( XOSView *parent )
23 : FieldMeterGraph( parent, 2, "LOAD", "PROCS/MIN", 1, 1, 0 ){
24 lastalarmstate = -1;
25 total_ = 2.0;
26 }
27
28 LoadMeter::~LoadMeter( void ){
29 }
30
31 void LoadMeter::checkResources( void ){
32 FieldMeterGraph::checkResources();
33
34 procloadcol_ = parent_->allocColor(parent_->getResource( "loadProcColor" ));
35 warnloadcol_ = parent_->allocColor(parent_->getResource( "loadWarnColor" ));
36 critloadcol_ = parent_->allocColor(parent_->getResource( "loadCritColor" ));
37
38 setfieldcolor( 0, procloadcol_ );
39 setfieldcolor( 1, parent_->getResource( "loadIdleColor" ) );
40 priority_ = atoi (parent_->getResource( "loadPriority" ) );
41 useGraph_ = parent_->isResourceTrue( "loadGraph" );
42 dodecay_ = parent_->isResourceTrue( "loadDecay" );
43 SetUsedFormat (parent_->getResource("loadUsedFormat"));
44
45 warnThreshold = atoi (parent_->getResource("loadWarnThreshold"));
46 critThreshold = atoi (parent_->getResource("loadCritThreshold"));
47
48
49 if (dodecay_){
50 // Warning: Since the loadmeter changes scale occasionally, old
51 // decay values need to be rescaled. However, if they are rescaled,
52 // they could go off the edge of the screen. Thus, for now, to
53 // prevent this whole problem, the load meter can not be a decay
54 // meter. The load is a decaying average kind of thing anyway,
55 // so having a decaying load average is redundant.
56 std::cerr << "Warning: The loadmeter can not be configured as a decay\n"
57 << " meter. See the source code (" << __FILE__ << ") for further\n"
58 << " details.\n";
59 dodecay_ = 0;
60 }
61 }
62
63 void LoadMeter::checkevent( void ){
64 getloadinfo();
65 drawfields();
66 }
67
68
69 void LoadMeter::getloadinfo( void ){
70 host_load_info_data_t info;
71 mach_msg_type_number_t count = HOST_LOAD_INFO_COUNT;
72 kern_return_t err;
73
74 err = host_info(mach_host_self(), HOST_LOAD_INFO, (host_info_t) &info, &count);
75 if (err) {
76 std::cerr << "Can not get host info";
77 parent_->done(1);
78 return;
79 }
80 fields_[0] = (float) info.avenrun[0] / LOAD_SCALE;
81
82 if ( fields_[0] < warnThreshold ) alarmstate = 0;
83 else
84 if ( fields_[0] >= critThreshold ) alarmstate = 2;
85 else
86 /* if fields_[0] >= warnThreshold */ alarmstate = 1;
87
88 if ( alarmstate != lastalarmstate ){
89 if ( alarmstate == 0 ) setfieldcolor( 0, procloadcol_ );
90 else
91 if ( alarmstate == 1 ) setfieldcolor( 0, warnloadcol_ );
92 else
93 /* if alarmstate == 2 */ setfieldcolor( 0, critloadcol_ );
94 drawlegend();
95 lastalarmstate = alarmstate;
96 }
97
98 if ( fields_[0]*5.0<total_ )
99 total_ = fields_[0];
100 else
101 if ( fields_[0]>total_ )
102 total_ = fields_[0]*5.0;
103
104 if ( total_ < 1.0)
105 total_ = 1.0;
106
107 fields_[1] = (float) (total_ - fields_[0]);
108
109 setUsed(fields_[0], (float) 1.0);
110 }