"Fossies" - the Fresh Open Source Software Archive 
Member "xosview-1.23/sunos5/netmeter.cc" (11 Jul 2020, 4965 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 "netmeter.cc" see the
Fossies "Dox" file reference documentation.
1 //
2 // Rewritten for Solaris by Arno Augustin 1999
3 // augustin@informatik.uni-erlangen.de
4 //
5
6 #include "netmeter.h"
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stropts.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/sockio.h>
13 #include <string.h>
14 #include <iostream>
15 #ifdef DEBUG
16 #include <sstream>
17 #endif
18
19
20 NetMeter::NetMeter( XOSView *parent, kstat_ctl_t *kc, float max )
21 : FieldMeterGraph( parent, 3, "NET", "IN/OUT/IDLE" ){
22 _kc = kc;
23 _ignored = false;
24 _maxpackets = max;
25 _lastBytesIn = _lastBytesOut = 0;
26 _nets = KStatList::getList(_kc, KStatList::NETS);
27 if ( (_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
28 std::cerr << "Opening socket failed." << std::endl;
29 parent_->done(1);
30 return;
31 }
32 }
33
34 NetMeter::~NetMeter( void ){
35 close(_socket);
36 }
37
38 void NetMeter::checkResources( void ){
39 FieldMeterGraph::checkResources();
40
41 setfieldcolor( 0, parent_->getResource("netInColor") );
42 setfieldcolor( 1, parent_->getResource("netOutColor") );
43 setfieldcolor( 2, parent_->getResource("netBackground") );
44 priority_ = atoi( parent_->getResource("netPriority") );
45 dodecay_ = parent_->isResourceTrue("netDecay");
46 useGraph_ = parent_->isResourceTrue("netGraph");
47 SetUsedFormat( parent_->getResource("netUsedFormat") );
48 _netIface = parent_->getResource("netIface");
49 if (_netIface[0] == '-') {
50 _ignored = true;
51 _netIface.erase(0, _netIface.find_first_not_of("- "));
52 }
53 }
54
55 void NetMeter::checkevent( void ){
56 getnetstats();
57 drawfields();
58 }
59
60 void NetMeter::getnetstats( void ){
61 uint64_t nowBytesIn = 0, nowBytesOut = 0;
62 kstat_named_t *k;
63 kstat_t *ksp;
64 total_ = _maxpackets;
65 _nets->update(_kc);
66
67 IntervalTimerStop();
68 for (unsigned int i = 0; i < _nets->count(); i++) {
69 #ifdef DEBUG
70 std::stringstream msg;
71 #endif
72 ksp = (*_nets)[i];
73 if ( _netIface != "False" &&
74 ( (!_ignored && ksp->ks_name != _netIface) ||
75 ( _ignored && ksp->ks_name == _netIface) ) )
76 continue;
77 if ( kstat_read(_kc, ksp, NULL) == -1 )
78 continue;
79
80 #ifdef DEBUG
81 msg << ksp->ks_name << ": ";
82 #endif
83 // try 64-bit byte counter first, then 32-bit one, then packet counter
84 if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "rbytes64")) == NULL ) {
85 if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "rbytes")) == NULL ) {
86 if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "ipackets")) == NULL )
87 continue;
88 // for packet counter, mtu is needed
89 strncpy(_lfr.lifr_name, ksp->ks_name, sizeof(_lfr.lifr_name));
90 if ( ioctl(_socket, SIOCGLIFMTU, (caddr_t)&_lfr) < 0 )
91 continue;
92 nowBytesIn += kstat_to_ui64(k) * _lfr.lifr_mtu; // not exactly, but must do
93 #ifdef DEBUG
94 msg << kstat_to_ui64(k) << " packets received ";
95 #endif
96 }
97 else {
98 nowBytesIn += kstat_to_ui64(k);
99 #ifdef DEBUG
100 msg << kstat_to_ui64(k) << " bytes received ";
101 #endif
102 }
103 }
104 else {
105 nowBytesIn += kstat_to_ui64(k);
106 #ifdef DEBUG
107 msg << kstat_to_ui64(k) << " bytes received ";
108 #endif
109 }
110
111 if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "obytes64")) == NULL ) {
112 if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "obytes")) == NULL ) {
113 if ( (k = (kstat_named_t *)kstat_data_lookup(ksp, "opackets")) == NULL )
114 continue;
115 strncpy(_lfr.lifr_name, ksp->ks_name, sizeof(_lfr.lifr_name));
116 if ( ioctl(_socket, SIOCGLIFMTU, (caddr_t)&_lfr) < 0 )
117 continue;
118 nowBytesOut += kstat_to_ui64(k) * _lfr.lifr_mtu;
119 #ifdef DEBUG
120 msg << kstat_to_ui64(k) << " packets sent ";
121 #endif
122 }
123 else {
124 nowBytesOut += kstat_to_ui64(k);
125 #ifdef DEBUG
126 msg << kstat_to_ui64(k) << " bytes sent ";
127 #endif
128 }
129 }
130 else {
131 nowBytesOut += kstat_to_ui64(k);
132 #ifdef DEBUG
133 msg << kstat_to_ui64(k) << " bytes sent ";
134 #endif
135 }
136 #ifdef DEBUG
137 msg << std::ends;
138 XOSDEBUG("%s\n", msg.str().c_str())
139 #endif
140 }
141
142 uint64_t correction = 0x10000000;
143 correction *= 0x10;
144 /* Deal with 32-bit wrap by making last value 2^32 less. Yes,
145 * this is a better idea than adding to nowBytesIn -- the
146 * latter would only work for the first wrap (1+2^32 vs. 1)
147 * but not for the second (1+2*2^32 vs. 1) -- 1+2^32 -
148 * (1+2^32) is still too big. */
149 if (nowBytesIn < _lastBytesIn)
150 _lastBytesIn -= correction;
151 if (nowBytesOut < _lastBytesOut)
152 _lastBytesOut -= correction;
153 if(_lastBytesIn == 0)
154 _lastBytesIn = nowBytesIn;
155 if(_lastBytesOut == 0)
156 _lastBytesOut = nowBytesOut;
157
158 double t = IntervalTimeInSecs();
159 fields_[0] = (double)(nowBytesIn - _lastBytesIn) / t;
160 fields_[1] = (double)(nowBytesOut - _lastBytesOut) / t;
161
162 IntervalTimerStart();
163 _lastBytesIn = nowBytesIn;
164 _lastBytesOut = nowBytesOut;
165
166 if (total_ < fields_[0] + fields_[1])
167 total_ = fields_[0] + fields_[1];
168 fields_[2] = total_ - fields_[0] - fields_[1];
169 setUsed(fields_[0] + fields_[1], total_);
170 }