From 31c2cd2dcb758974fea28caf01a4d9b49f929669 Mon Sep 17 00:00:00 2001 From: Srivats P Date: Wed, 19 Oct 2016 18:52:35 +0530 Subject: [PATCH] sign: refactored tx stats into a new PcapTxStats class --- server/drone.pro | 1 + server/pcaptransmitter.cpp | 12 +++++- server/pcaptransmitter.h | 5 +++ server/pcaptxstats.cpp | 85 ++++++++++++++++++++++++++++++++++++++ server/pcaptxstats.h | 53 ++++++++++++++++++++++++ server/pcaptxthread.cpp | 20 ++++----- server/pcaptxthread.h | 10 +++-- server/statstuple.h | 31 ++++++++++++++ 8 files changed, 201 insertions(+), 16 deletions(-) create mode 100644 server/pcaptxstats.cpp create mode 100644 server/pcaptxstats.h create mode 100644 server/statstuple.h diff --git a/server/drone.pro b/server/drone.pro index 143101a..42f45f2 100644 --- a/server/drone.pro +++ b/server/drone.pro @@ -41,6 +41,7 @@ SOURCES += \ abstractport.cpp \ pcapport.cpp \ pcaptransmitter.cpp \ + pcaptxstats.cpp \ pcaptxthread.cpp \ bsdport.cpp \ linuxport.cpp \ diff --git a/server/pcaptransmitter.cpp b/server/pcaptransmitter.cpp index 8c807f8..a81ede9 100644 --- a/server/pcaptransmitter.cpp +++ b/server/pcaptransmitter.cpp @@ -22,6 +22,16 @@ along with this program. If not, see PcapTransmitter::PcapTransmitter(const char *device) : txThread_(device) { + memset(&stats_, 0, sizeof(stats_)); + txStats_.setTxThreadStats(&stats_); + txStats_.start(); // TODO: alongwith user transmit start + + txThread_.setStats(&stats_); +} + +PcapTransmitter::~PcapTransmitter() +{ + txStats_.stop(); // TODO: alongwith user transmit stop } bool PcapTransmitter::setRateAccuracy( @@ -65,7 +75,7 @@ void PcapTransmitter::setPacketListLoopMode( void PcapTransmitter::useExternalStats(AbstractPort::PortStats *stats) { - txThread_.useExternalStats(stats); + txStats_.useExternalStats(stats); } void PcapTransmitter::start() diff --git a/server/pcaptransmitter.h b/server/pcaptransmitter.h index 3327b24..3dc8eb9 100644 --- a/server/pcaptransmitter.h +++ b/server/pcaptransmitter.h @@ -21,12 +21,15 @@ along with this program. If not, see #define _PCAP_TRANSMITTER_H #include "abstractport.h" +#include "pcaptxstats.h" #include "pcaptxthread.h" +#include "statstuple.h" class PcapTransmitter { public: PcapTransmitter(const char *device); + ~PcapTransmitter(); bool setRateAccuracy(AbstractPort::Accuracy accuracy); @@ -46,6 +49,8 @@ public: private: PcapTxThread txThread_; + PcapTxStats txStats_; + StatsTuple stats_; }; #endif diff --git a/server/pcaptxstats.cpp b/server/pcaptxstats.cpp new file mode 100644 index 0000000..353ea9d --- /dev/null +++ b/server/pcaptxstats.cpp @@ -0,0 +1,85 @@ +/* +Copyright (C) 2016 Srivats P. + +This file is part of "Ostinato" + +This is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +#include "pcaptxstats.h" + +#include "statstuple.h" + +PcapTxStats::PcapTxStats() +{ + txThreadStats_ = NULL; + + stats_ = new AbstractPort::PortStats; + usingInternalStats_ = true; + + stop_ = false; +} + +PcapTxStats::~PcapTxStats() +{ + if (usingInternalStats_) + delete stats_; +} + +void PcapTxStats::setTxThreadStats(StatsTuple *stats) +{ + txThreadStats_ = stats; +} + +void PcapTxStats::useExternalStats(AbstractPort::PortStats *stats) +{ + if (usingInternalStats_) + delete stats_; + stats_ = stats; + usingInternalStats_ = false; +} + +void PcapTxStats::start() +{ + QThread::start(); + + while (!isRunning()) + QThread::msleep(10); +} + +void PcapTxStats::stop() +{ + stop_ = true; + + while (isRunning()) + QThread::msleep(10); +} + +void PcapTxStats::run() +{ + Q_ASSERT(txThreadStats_); + + qDebug("txStats: collection start"); + + while (1) { + stats_->txPkts = txThreadStats_->pkts; + stats_->txBytes = txThreadStats_->bytes; + + if (stop_) + break; + QThread::msleep(1000); + } + stop_ = false; + qDebug("txStats: collection end"); +} diff --git a/server/pcaptxstats.h b/server/pcaptxstats.h new file mode 100644 index 0000000..ed134bb --- /dev/null +++ b/server/pcaptxstats.h @@ -0,0 +1,53 @@ +/* +Copyright (C) 2016 Srivats P. + +This file is part of "Ostinato" + +This is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +#ifndef _PCAP_TX_STATS_H +#define _PCAP_TX_STATS_H + +#include "abstractport.h" + +#include + +class StatsTuple; + +class PcapTxStats : public QThread +{ +public: + PcapTxStats(); + ~PcapTxStats(); + + void setTxThreadStats(StatsTuple *stats); + + void useExternalStats(AbstractPort::PortStats *stats); + + void start(); + void stop(); +private: + void run(); + + StatsTuple *txThreadStats_; + + bool usingInternalStats_; + AbstractPort::PortStats *stats_; + + volatile bool stop_; +}; + +#endif + diff --git a/server/pcaptxthread.cpp b/server/pcaptxthread.cpp index 86076c6..9b84f06 100644 --- a/server/pcaptxthread.cpp +++ b/server/pcaptxthread.cpp @@ -19,6 +19,7 @@ along with this program. If not, see #include "pcaptransmitter.h" +#include "statstuple.h" #include "timestamp.h" PcapTxThread::PcapTxThread(const char *device) @@ -37,8 +38,6 @@ PcapTxThread::PcapTxThread(const char *device) returnToQIdx_ = -1; loopDelay_ = 0; stop_ = false; - stats_ = new AbstractPort::PortStats; - usingInternalStats_ = true; handle_ = pcap_open_live(device, 64 /* FIXME */, 0, 1000 /* ms */, errbuf); if (handle_ == NULL) @@ -46,6 +45,8 @@ PcapTxThread::PcapTxThread(const char *device) usingInternalHandle_ = true; + stats_ = NULL; + return; _open_error: @@ -55,8 +56,6 @@ _open_error: PcapTxThread::~PcapTxThread() { - if (usingInternalStats_) - delete stats_; if (usingInternalHandle_) pcap_close(handle_); } @@ -199,12 +198,9 @@ void PcapTxThread::setHandle(pcap_t *handle) usingInternalHandle_ = false; } -void PcapTxThread::useExternalStats(AbstractPort::PortStats *stats) +void PcapTxThread::setStats(StatsTuple *stats) { - if (usingInternalStats_) - delete stats_; stats_ = stats; - usingInternalStats_ = false; } void PcapTxThread::run() @@ -266,8 +262,8 @@ _restart: seq->sendQueue_, kSyncTransmit); if (ret >= 0) { - stats_->txPkts += seq->packets_; - stats_->txBytes += seq->bytes_; + stats_->pkts += seq->packets_; + stats_->bytes += seq->bytes_; getTimeStamp(&ovrEnd); overHead += seq->usecDuration_ @@ -407,8 +403,8 @@ int PcapTxThread::sendQueueTransmit(pcap_t *p, Q_ASSERT(pktLen > 0); pcap_sendpacket(p, pkt, pktLen); - stats_->txPkts++; - stats_->txBytes += pktLen; + stats_->pkts++; + stats_->bytes += pktLen; // Step to the next packet in the buffer hdr = (struct pcap_pkthdr*) (pkt + pktLen); diff --git a/server/pcaptxthread.h b/server/pcaptxthread.h index b9064b1..ad8de2b 100644 --- a/server/pcaptxthread.h +++ b/server/pcaptxthread.h @@ -26,6 +26,8 @@ along with this program. If not, see #include #include +class StatsTuple; + class PcapTxThread: public QThread { public: @@ -42,13 +44,15 @@ public: void setPacketListLoopMode(bool loop, quint64 secDelay, quint64 nsecDelay); void setHandle(pcap_t *handle); - void useExternalStats(AbstractPort::PortStats *stats); + + void setStats(StatsTuple *stats); void run(); void start(); void stop(); bool isRunning(); + private: enum State { @@ -72,12 +76,12 @@ private: void (*udelayFn_)(unsigned long); - bool usingInternalStats_; - AbstractPort::PortStats *stats_; bool usingInternalHandle_; pcap_t *handle_; volatile bool stop_; volatile State state_; + + StatsTuple *stats_; }; #endif diff --git a/server/statstuple.h b/server/statstuple.h new file mode 100644 index 0000000..61781d7 --- /dev/null +++ b/server/statstuple.h @@ -0,0 +1,31 @@ +/* +Copyright (C) 2016 Srivats P. + +This file is part of "Ostinato" + +This is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +#ifndef _STATS_TUPLE_H +#define _STATS_TUPLE_H + +#include + +struct StatsTuple +{ + quint64 pkts; + quint64 bytes; +}; + +#endif