]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
changes after review
authorMichal Nowikowski <godfryd@isc.org>
Fri, 7 Dec 2018 14:33:42 +0000 (15:33 +0100)
committerMichal Nowikowski <godfryd@isc.org>
Fri, 18 Jan 2019 09:05:12 +0000 (10:05 +0100)
- added usleep to main loop in run function when nothing is to be done to not overload CPU
- changed includes to use <...>
- changed socket desctructor to be virtual
- updated docs
- changed rate type to unsigned
- other minor fixes

21 files changed:
src/bin/perfdhcp/better_socket.cc
src/bin/perfdhcp/better_socket.h
src/bin/perfdhcp/command_options.cc
src/bin/perfdhcp/command_options.h
src/bin/perfdhcp/main.cc
src/bin/perfdhcp/perf_pkt4.cc
src/bin/perfdhcp/perf_pkt4.h
src/bin/perfdhcp/perf_pkt6.cc
src/bin/perfdhcp/perf_pkt6.h
src/bin/perfdhcp/perfdhcp.xml
src/bin/perfdhcp/pkt_transform.cc
src/bin/perfdhcp/pkt_transform.h
src/bin/perfdhcp/rate_control.cc
src/bin/perfdhcp/rate_control.h
src/bin/perfdhcp/receiver.cc
src/bin/perfdhcp/receiver.h
src/bin/perfdhcp/stats_mgr.h
src/bin/perfdhcp/test_control.cc
src/bin/perfdhcp/test_control.h
src/bin/perfdhcp/tests/Makefile.am
src/bin/perfdhcp/tests/rate_control_unittest.cc

index 3005340217ff1bce09716a0d0acf60f587b34e0c..ace47235f9eb6ea6206ecee869480e85e779c098 100644 (file)
@@ -5,7 +5,7 @@
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 
-#include "better_socket.h"
+#include <perfdhcp/better_socket.h>
 
 #include <dhcp/iface_mgr.h>
 
@@ -45,8 +45,7 @@ BetterSocket::initSocketData() {
             }
         }
     }
-    isc_throw(BadValue, "interface for for specified socket "
-              "descriptor not found");
+    isc_throw(BadValue, "interface for specified socket descriptor not found");
 }
 
 }
index 976cd83d79fd0703572c2ade034bc889668a5c23..e0b9f0e7182d2e972bc5a6f82b72a437c28eed09 100644 (file)
@@ -41,7 +41,7 @@ struct BetterSocket : public dhcp::SocketInfo {
     /// \brief Destructor of the socket wrapper class.
     ///
     /// Destructor closes wrapped socket.
-    ~BetterSocket();
+    virtual ~BetterSocket();
 
 private:
     /// \brief Initialize socket data.
index 319152f3c05a895cbe8496530fc0db3edc7a2ba8..7c5237195bd2dd948fa077013faac826f0fbd8cf 100644 (file)
@@ -6,7 +6,8 @@
 
 #include <config.h>
 
-#include "command_options.h"
+#include <perfdhcp/command_options.h>
+
 #include <exceptions/exceptions.h>
 #include <dhcp/iface_mgr.h>
 #include <dhcp/duid.h>
@@ -16,7 +17,6 @@
 
 #include <boost/lexical_cast.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
-
 #include <sstream>
 #include <stdio.h>
 #include <stdlib.h>
@@ -1105,8 +1105,8 @@ CommandOptions::usage() const {
         "    to or less than the exchange rate.\n"
         "-g  Select thread mode: 'single' or 'multi'. In multi-thread mode packets\n"
         "    are received in separate thread. This allows better utilisation of CPUs."
-        "    If more than 1 CPU is present then multi-thread mode is default otherwise"
-        "    single-thread is default."
+        "    If more than 1 CPU is present then multi-thread mode is the default,"
+        "    otherwise single-thread is the default."
         "-h: Print this help.\n"
         "-i: Do only the initial part of an exchange: DO or SA, depending on\n"
         "    whether -6 is given.\n"
index 167d28ee7d1f61665bbe4c42989e72769f54884e..f843d2484dcad3284abb5b0784cdbd8827702bac 100644 (file)
@@ -7,9 +7,9 @@
 #ifndef COMMAND_OPTIONS_H
 #define COMMAND_OPTIONS_H
 
-#include <boost/noncopyable.hpp>
-
 #include <dhcp/option.h>
+
+#include <boost/noncopyable.hpp>
 #include <stdint.h>
 #include <string>
 #include <vector>
@@ -507,13 +507,13 @@ private:
     LeaseType lease_type_;
 
     /// Rate in exchange per second
-    int rate_;
+    unsigned int rate_;
 
     /// A rate at which DHCPv6 Renew messages are sent.
-    int renew_rate_;
+    unsigned int renew_rate_;
 
     /// A rate at which DHCPv6 Release messages are sent.
-    int release_rate_;
+    unsigned int release_rate_;
 
     /// Delay between generation of two consecutive performance reports
     int report_delay_;
index bb5409a548ea20bee10eb5a1b7a05f6d8a1590ad..7548ee76cd5d5b440ac1ade0e6b1c506f39f5254 100644 (file)
@@ -4,14 +4,15 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
-#include <iostream>
-#include <stdint.h>
-
 #include <config.h>
+
+#include <perfdhcp/test_control.h>
+#include <perfdhcp/command_options.h>
+
 #include <exceptions/exceptions.h>
 
-#include "test_control.h"
-#include "command_options.h"
+#include <iostream>
+#include <stdint.h>
 
 using namespace isc::perfdhcp;
 
index d8aca9549834f44e1d3f566e52dea485374be153..784f96daccf8f1c47633877781786d793bf61c1a 100644 (file)
@@ -6,11 +6,11 @@
 
 #include <config.h>
 
+#include <perfdhcp/perf_pkt4.h>
+
 #include <dhcp/libdhcp++.h>
 #include <dhcp/dhcp4.h>
 
-#include "perf_pkt4.h"
-
 using namespace std;
 using namespace isc;
 using namespace dhcp;
index 6cc8b49374c544115ab16912399476ba69aa64df..a74ed431439e28d49abd5434251cbf63274eab90 100644 (file)
@@ -7,12 +7,14 @@
 #ifndef PERF_PKT4_H
 #define PERF_PKT4_H
 
-#include <time.h>
-#include <boost/shared_ptr.hpp>
+#include <perfdhcp/localized_option.h>
+#include <perfdhcp/pkt_transform.h>
+
 #include <dhcp/pkt4.h>
 
-#include "localized_option.h"
-#include "pkt_transform.h"
+#include <boost/shared_ptr.hpp>
+#include <time.h>
+
 
 namespace isc {
 namespace perfdhcp {
index 4d802471dfd30aeb91cdf1d1c6e24a1fc12a3a7e..72724148ad704843d321f3f4a036bbff218e0ed9 100644 (file)
@@ -6,13 +6,14 @@
 
 #include <config.h>
 
-#include <iostream>
+#include <perfdhcp/perf_pkt6.h>
+#include <perfdhcp/pkt_transform.h>
+
 #include <exceptions/exceptions.h>
 #include <dhcp/libdhcp++.h>
 #include <dhcp/dhcp6.h>
 
-#include "perf_pkt6.h"
-#include "pkt_transform.h"
+#include <iostream>
 
 using namespace std;
 using namespace isc;
index 988dd73144a6820ab182e96e5ab06bea552ac748..8465c396ebf7235b9f9160cfac73247636320d90 100644 (file)
@@ -7,12 +7,13 @@
 #ifndef PERF_PKT6_H
 #define PERF_PKT6_H
 
-#include <time.h>
-#include <boost/shared_ptr.hpp>
+#include <perfdhcp/localized_option.h>
+#include <perfdhcp/pkt_transform.h>
+
 #include <dhcp/pkt6.h>
 
-#include "localized_option.h"
-#include "pkt_transform.h"
+#include <time.h>
+#include <boost/shared_ptr.hpp>
 
 namespace isc {
 namespace perfdhcp {
index cd8eaea691c2be05bbd68168979a61af924f1111..3f85823f80e4d600fe5c70d311818703a1034d3e 100644 (file)
               <term><option>-g <replaceable class="parameter">thread-mode</replaceable></option></term>
               <listitem>
                 <para>
-                    Thread operation mode can be either 'single' or 'multi'. In multi-thread mode packets
-                    are received in separate thread. This allows better utilisation of CPUs.
-                    In single CPU system it is better to run in 1 thread to avoid blocking of threads each other.
-                    If more than 1 CPU is present in the system then multi-thread mode is default otherwise
-                    single-thread is default.
+                  <replaceable class="parameter">thread-mode</replaceable> can be either 'single' or 'multi'.
+                  In multi-thread mode packets are received in separate thread. This allows better utilisation of CPUs.
+                  In single CPU system it is better to run in 1 thread to avoid blocking of threads each other.
+                  If more than 1 CPU is present in the system then multi-thread mode is default otherwise
+                  single-thread is default.
                 </para>
               </listitem>
             </varlistentry>
index d9588b1a65eab647daf9dd46929f468f39c6ab3a..fad8d1cbd12c7710a25298f0a4b21d1e93c9af59 100644 (file)
@@ -6,15 +6,16 @@
 
 #include <config.h>
 
-#include <iostream>
+#include <perfdhcp/pkt_transform.h>
+#include <perfdhcp/localized_option.h>
 
 #include <exceptions/exceptions.h>
 #include <dhcp/option.h>
 #include <dhcp/libdhcp++.h>
 #include <dhcp/dhcp6.h>
 
-#include "pkt_transform.h"
-#include "localized_option.h"
+#include <iostream>
+
 
 using namespace std;
 using namespace isc;
index 3239b0b081340d91a54def47db0055e68d1ad81f..d1272f1777d1242b046b97190af188c1680f09eb 100644 (file)
@@ -7,9 +7,9 @@
 #ifndef PKT_TRANSFORM_H
 #define PKT_TRANSFORM_H
 
-#include <dhcp/option.h>
+#include <perfdhcp/localized_option.h>
 
-#include "localized_option.h"
+#include <dhcp/option.h>
 
 namespace isc {
 namespace perfdhcp {
index 9b7c759a882b99acd2893acfe32ff7a1023deea7..8d5ac2453a6ce7363472fc049d05b13f4ab4b0b6 100644 (file)
@@ -6,8 +6,10 @@
 
 #include <config.h>
 
+#include <perfdhcp/rate_control.h>
+
 #include <exceptions/exceptions.h>
-#include "rate_control.h"
+
 
 namespace isc {
 namespace perfdhcp {
@@ -18,12 +20,8 @@ RateControl::RateControl()
     : rate_(0), total_pkts_sent_count_(0) {
 }
 
-RateControl::RateControl(const int rate)
+RateControl::RateControl(const unsigned int rate)
     : rate_(rate), total_pkts_sent_count_(0) {
-    if (rate_ < 0) {
-        isc_throw(isc::BadValue, "invalid value of rate " << rate
-                  << ", expected non-negative value");
-    }
 }
 
 uint64_t
index 347e67359de23d0e122414239bcce31e705b4b40..bbd9f9ea4a3f5b2c9c6ccba6865d710716f62c4c 100644 (file)
@@ -44,7 +44,7 @@ public:
     /// \brief Constructor which sets desired rate.
     ///
     /// \param rate A desired rate.
-    RateControl(const int rate);
+    RateControl(const unsigned int rate);
 
     /// \brief Returns number of messages to be sent "now".
     ///
@@ -72,7 +72,7 @@ public:
     uint64_t getOutboundMessageCount();
 
     /// \brief Returns the rate.
-    int getRate() const {
+    unsigned int getRate() const {
         return (rate_);
     }
 
@@ -90,7 +90,7 @@ protected:
     boost::posix_time::ptime currentTime();
 
     /// \brief Holds a desired rate value.
-    int rate_;
+    unsigned int rate_;
 
     /// \brief Holds number of packets send from the beginning.
 
index dce28f1dd435bb02e45b3d3452a30728368869c1..0473e58c88a89eaef2034ea6d76f364d862c92be 100644 (file)
@@ -4,12 +4,13 @@
 // License, v. 2.0. If a copy of the MPL was not distributed with this
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
-#include "receiver.h"
-#include "command_options.h"
+#include <perfdhcp/receiver.h>
+#include <perfdhcp/command_options.h>
 
 #include <dhcp/iface_mgr.h>
 
 using namespace std;
+using namespace isc::dhcp;
 
 namespace isc {
 namespace perfdhcp {
@@ -50,16 +51,15 @@ PktPtr
 Receiver::getPkt() {
     if (single_threaded_) {
         // In single thread mode read packet directly from the socket and return it.
-        auto pkt = readPktFromSocket();
-        return pkt;
+        return readPktFromSocket();
     } else {
         // In multi thread mode read packet from the queue which is feed by Receiver thread.
         unique_lock<mutex> lock(pkt_queue_mutex_);
         if (pkt_queue_.empty()) {
             if (CommandOptions::instance().getIpVersion() == 4) {
-                return Pkt4Ptr{nullptr};
+                return Pkt4Ptr();
             } else {
-                return Pkt6Ptr{nullptr};
+                return Pkt6Ptr();
             }
         }
         auto pkt = pkt_queue_.front();
@@ -76,8 +76,12 @@ Receiver::run() {
         while (run_flag_.test_and_set()) {
             receivePackets();
         }
+    } catch (const exception& e) {
+        cerr << "Something went wrong: " << e.what() << endl;
+        usleep(1000);
     } catch (...) {
-        cout << "SOMETHING WENT WRONG" << endl;
+        cerr << "Something went wrong" << endl;
+        usleep(1000);
     }
 }
 
@@ -101,7 +105,7 @@ Receiver::readPktFromSocket() {
             pkt = IfaceMgr::instance().receive6(0, timeout);
         }
     } catch (const Exception& e) {
-        cerr << "Failed to receive DHCP packet: " << e.what() <<  endl;
+        cerr << "Failed to receive DHCP packet: " << e.what() << endl;
     }
     if (!pkt) {
         return nullptr;
@@ -121,10 +125,10 @@ Receiver::receivePackets() {
             break;
         }
 
-        // Drop packet if not supported.
+        // Drop the packet if not supported. Do not bother main thread about it.
         if (pkt->getType() == DHCPOFFER || pkt->getType() == DHCPACK ||
             pkt->getType() == DHCPV6_ADVERTISE || pkt->getType() == DHCPV6_REPLY) {
-            // Otherwise push to another thread.
+            // Otherwise push the packet to the queue, to main thread.
             unique_lock<mutex> lock(pkt_queue_mutex_);
             pkt_queue_.push(pkt);
         }
index ce7fc4b1144b689afec104965eaf77b710fd744f..bda6a9dced51065204307c45e28e2b16d2de7f59 100644 (file)
@@ -6,8 +6,8 @@
 
 #pragma once
 
-#include "better_socket.h"
-#include "command_options.h"
+#include <perfdhcp/better_socket.h>
+#include <perfdhcp/command_options.h>
 
 #include <dhcp/pkt4.h>
 #include <dhcp/pkt6.h>
 #include <mutex>
 #include <boost/atomic.hpp>
 
-using namespace std;
-using namespace isc::dhcp;
-
 namespace isc {
 namespace perfdhcp {
 
-typedef boost::shared_ptr<Pkt> PktPtr;
+typedef boost::shared_ptr<isc::dhcp::Pkt> PktPtr;
 
 /// \brief A receviving DHCP packets class.
 ///
@@ -46,13 +43,13 @@ private:
     boost::atomic_flag run_flag_;
 
     /// \brief Thread for receiving packets.
-    unique_ptr<thread> recv_thread_;
+    std::unique_ptr<std::thread> recv_thread_;
 
     /// \brief Queue for passing packets from receiver thread to main thread.
-    queue<PktPtr> pkt_queue_;
+    std::queue<PktPtr> pkt_queue_;
 
     /// \brief Mutex for controlling access to the queue.
-    mutex pkt_queue_mutex_;
+    std::mutex pkt_queue_mutex_;
 
     /// \brief Single- or thread-mode indicator.
     bool single_threaded_;
index cf69c760dc48e6a3739c4e273dafccfe0db66c37..00a58904f9d1128002ae60b6c27bbd9b4bbdcc85 100644 (file)
@@ -684,7 +684,7 @@ public:
             using namespace std;
             auto sent = getSentPacketsNum();
             auto drops = getDroppedPacketsNum();
-            double drops_ratio = 100 * static_cast<double>(drops) / static_cast<double>(sent);
+            double drops_ratio = 100.0 * static_cast<double>(drops) / static_cast<double>(sent);
 
             cout << "sent packets: " << sent << endl
                  << "received packets: " << getRcvdPacketsNum() << endl
index d890edae5bf62ed24ccc023a747df31cf77b84fd..c1a9b665bce1b8b6e70a69a75de028c003b61867 100644 (file)
@@ -6,6 +6,12 @@
 
 #include <config.h>
 
+#include <perfdhcp/test_control.h>
+#include <perfdhcp/receiver.h>
+#include <perfdhcp/command_options.h>
+#include <perfdhcp/perf_pkt4.h>
+#include <perfdhcp/perf_pkt6.h>
+
 #include <exceptions/exceptions.h>
 #include <asiolink/io_address.h>
 #include <dhcp/libdhcp++.h>
 #include <dhcp/dhcp4.h>
 #include <dhcp/option6_ia.h>
 #include <util/unittests/check_valgrind.h>
-#include "test_control.h"
-#include "receiver.h"
-#include "command_options.h"
-#include "perf_pkt4.h"
-#include "perf_pkt6.h"
 
 #include <boost/date_time/posix_time/posix_time.hpp>
-
 #include <algorithm>
 #include <fstream>
 #include <stdio.h>
@@ -1235,10 +1235,12 @@ TestControl::processReceivedPacket6(const BetterSocket& socket,
     }
 }
 
-void
+unsigned int
 TestControl::consumeReceivedPackets(Receiver& receiver, const BetterSocket& socket) {
+    unsigned int pkt_count = 0;
     PktPtr pkt;
     while ((pkt = receiver.getPkt())) {
+        pkt_count += 1;
         if (CommandOptions::instance().getIpVersion() == 4) {
             Pkt4Ptr pkt4 = boost::dynamic_pointer_cast<Pkt4>(pkt);
             processReceivedPacket4(socket, pkt4);
@@ -1247,6 +1249,7 @@ TestControl::consumeReceivedPackets(Receiver& receiver, const BetterSocket& sock
             processReceivedPacket6(socket, pkt6);
         }
     }
+    return pkt_count;
 }
 void
 TestControl::registerOptionFactories4() const {
@@ -1421,7 +1424,14 @@ TestControl::run() {
 
         // Pull some packets from receiver thread, process them, update some stats
         // and respond to the server if needed.
-        consumeReceivedPackets(receiver, socket);
+        auto pkt_count = consumeReceivedPackets(receiver, socket);
+
+        // If there is nothing to do in this loop iteration then do some sleep to make
+        // CPU idle for a moment, to not consume 100% CPU all the time
+        // but only if it is not that high request rate expected.
+        if (options.getRate() < 10000 && packets_due == 0 && pkt_count == 0) {
+            usleep(1);
+        }
 
         // If test period finished, maximum number of packet drops
         // has been reached or test has been interrupted we have to
index cbbe403fdfcc017102916940c8bb02a15ff11423..2c40b36a624e584abb6803195d5bb1e8ea013697 100644 (file)
@@ -7,10 +7,10 @@
 #ifndef TEST_CONTROL_H
 #define TEST_CONTROL_H
 
-#include "packet_storage.h"
-#include "rate_control.h"
-#include "stats_mgr.h"
-#include "receiver.h"
+#include <perfdhcp/packet_storage.h>
+#include <perfdhcp/rate_control.h>
+#include <perfdhcp/stats_mgr.h>
+#include <perfdhcp/receiver.h>
 
 #include <dhcp/iface_mgr.h>
 #include <dhcp/dhcp6.h>
@@ -523,7 +523,7 @@ protected:
     /// \brief Pull packets from receiver and process them.
 
     /// It runs in a loop until there are no packets in receiver.
-    void consumeReceivedPackets(Receiver& receiver, const BetterSocket& socket);
+    unsigned int consumeReceivedPackets(Receiver& receiver, const BetterSocket& socket);
 
     /// \brief Process received DHCPv4 packet.
     ///
index ca65a9d2a16af5be2ccb1866bd5c9dbff3609381..73c78ee8d097edd005929b692dab2107dc10ea4f 100644 (file)
@@ -1,6 +1,6 @@
 SUBDIRS = . testdata
 
-AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib
+AM_CPPFLAGS = -I$(top_builddir)/src/lib -I$(top_srcdir)/src/lib -I$(top_builddir)/src/bin  -I$(top_srcdir)/src/bin
 AM_CPPFLAGS += -I$(srcdir)/.. -I$(builddir)/..
 AM_CPPFLAGS += -DTEST_DATA_DIR=\"$(abs_srcdir)/testdata\"
 AM_CPPFLAGS += $(BOOST_INCLUDES)
index 43b695a38a93975f7135ca7994d352ef163e63cf..69e6db50995e4a54ccad8b8ccfd04122f5905997 100644 (file)
@@ -53,9 +53,6 @@ TEST(RateControl, constructor) {
     // will be set correctly.
     NakedRateControl rc2(5);
     EXPECT_EQ(5, rc2.getRate());
-
-    // The negative value of rate is not acceptable.
-    EXPECT_THROW(RateControl(-1), isc::BadValue);
 }
 
 // Check the rate accessor.