Skip to content

Commit 043ba65

Browse files
authored
Merge pull request #9 from davidpolverari/issue-4
Implement peak bandwidth statistics
2 parents ff306e4 + eadfbba commit 043ba65

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
@@ -185,7 +185,7 @@ int main(int argc, char **argv) {
185185

186186
// Create the interface table
187187
VerticalTable interfaceTable(screen);
188-
interfaceTable.setColumns(4);
188+
interfaceTable.setColumns(6);
189189
interfaceTable.setActiveStyle(A_BOLD);
190190
interfaceTable.setActiveRow(1);
191191
// Position the interface table
@@ -277,6 +277,14 @@ int main(int argc, char **argv) {
277277
interfaceTable.setStyle(3, 0,
278278
COLOR_PAIR(COLOR_HEADING) | A_BOLD);
279279

280+
interfaceTable.setText (4, 0, "Receive Max");
281+
interfaceTable.setStyle(4, 0,
282+
COLOR_PAIR(COLOR_HEADING) | A_BOLD);
283+
284+
interfaceTable.setText (5, 0, "Transmit Max");
285+
interfaceTable.setStyle(5, 0,
286+
COLOR_PAIR(COLOR_HEADING) | A_BOLD);
287+
280288
unsigned index = 1;
281289
for (statistics::Reader::Interfaces::const_iterator
282290
interface = statisticsReader.getInterfaces().begin();
@@ -301,6 +309,14 @@ int main(int argc, char **argv) {
301309
+ interface->getTransmitSpeed(), bit_mode));
302310
interfaceTable.setStyle(3, index, A_NORMAL);
303311

312+
interfaceTable.setText(4, index,
313+
formatBandwidth(interface->getReceiveMax(), bit_mode));
314+
interfaceTable.setStyle(4, index, A_NORMAL);
315+
316+
interfaceTable.setText(5, index,
317+
formatBandwidth(interface->getTransmitMax(), bit_mode));
318+
interfaceTable.setStyle(5, index, A_NORMAL);
319+
304320
if (index == interfaceTable.getActiveRow()) {
305321
// Get the details for the active interface
306322
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)