Skip to content

Commit eadfbba

Browse files
Implement peak bandwidth statistics
Closes #4
1 parent 2c173cb commit eadfbba

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/cbm.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ int main(int argc, char **argv) {
180180

181181
// Create the interface table
182182
VerticalTable interfaceTable(screen);
183-
interfaceTable.setColumns(4);
183+
interfaceTable.setColumns(6);
184184
interfaceTable.setActiveStyle(A_BOLD);
185185
interfaceTable.setActiveRow(1);
186186
// Position the interface table
@@ -272,6 +272,14 @@ int main(int argc, char **argv) {
272272
interfaceTable.setStyle(3, 0,
273273
COLOR_PAIR(COLOR_HEADING) | A_BOLD);
274274

275+
interfaceTable.setText (4, 0, "Receive Max");
276+
interfaceTable.setStyle(4, 0,
277+
COLOR_PAIR(COLOR_HEADING) | A_BOLD);
278+
279+
interfaceTable.setText (5, 0, "Transmit Max");
280+
interfaceTable.setStyle(5, 0,
281+
COLOR_PAIR(COLOR_HEADING) | A_BOLD);
282+
275283
unsigned index = 1;
276284
for (statistics::Reader::Interfaces::const_iterator
277285
interface = statisticsReader.getInterfaces().begin();
@@ -296,6 +304,14 @@ int main(int argc, char **argv) {
296304
+ interface->getTransmitSpeed(), bit_mode));
297305
interfaceTable.setStyle(3, index, A_NORMAL);
298306

307+
interfaceTable.setText(4, index,
308+
formatBandwidth(interface->getReceiveMax(), bit_mode));
309+
interfaceTable.setStyle(4, index, A_NORMAL);
310+
311+
interfaceTable.setText(5, index,
312+
formatBandwidth(interface->getTransmitMax(), bit_mode));
313+
interfaceTable.setStyle(5, index, A_NORMAL);
314+
299315
if (index == interfaceTable.getActiveRow()) {
300316
// Get the details for the active interface
301317
struct ifreq req;

src/statistics.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
namespace statistics {
3636

37-
Interface::Interface(const std::string& name) : name_(name), updated_(false) {}
37+
Interface::Interface(const std::string& name) : name_(name), updated_(false), receiveMax_(0.0), transmitMax_(0.0) {}
3838

3939
class InterfaceNameMatchesPredicate {
4040
public:
@@ -55,6 +55,8 @@ class InterfaceNotUpdatedPredicate {
5555
};
5656

5757
void Interface::update(const Statistics& statistics) {
58+
static unsigned int count = 0;
59+
5860
memcpy(statistics_ + 1, statistics_ + 0, sizeof(Statistics));
5961
memcpy(statistics_, &statistics, sizeof(Statistics));
6062

@@ -68,6 +70,18 @@ void Interface::update(const Statistics& statistics) {
6870
receiveSpeed_ = (x1.rx_bytes - x0.rx_bytes) / timeDelta;
6971
transmitSpeed_ = (x1.tx_bytes - x0.tx_bytes) / timeDelta;
7072

73+
count++;
74+
75+
// Waits some iterations before calculating max speeds.
76+
// This avoids presenting wrong initial peaks.
77+
if (count < 8)
78+
return;
79+
80+
if (receiveSpeed_ > receiveMax_)
81+
receiveMax_ = receiveSpeed_;
82+
if (transmitSpeed_ > transmitMax_)
83+
transmitMax_ = transmitSpeed_;
84+
7185
updated_ = true;
7286
}
7387

@@ -168,6 +182,14 @@ double Interface::getTransmitSpeed() const {
168182
return transmitSpeed_;
169183
}
170184

185+
double Interface::getReceiveMax() const {
186+
return receiveMax_;
187+
}
188+
189+
double Interface::getTransmitMax() const {
190+
return transmitMax_;
191+
}
192+
171193
const Reader::Interfaces& Reader::getInterfaces() const {
172194
return interfaces_;
173195
}

src/statistics.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ class Interface {
4949

5050
double getReceiveSpeed() const;
5151
double getTransmitSpeed() const;
52+
double getReceiveMax() const;
53+
double getTransmitMax() const;
5254

5355
private:
5456
std::string name_;
5557
bool updated_;
5658
Statistics statistics_[2];
5759
double receiveSpeed_, transmitSpeed_;
60+
double receiveMax_, transmitMax_;
5861
};
5962

6063
class Reader {

0 commit comments

Comments
 (0)