3
0
Просмотр исходного кода

Use string pointers instead of std::string (mem consumption for MAWI is now 12.6GiB)

Stefan Schmidt 6 лет назад
Родитель
Сommit
6ae7e099b4

+ 18 - 18
code_boost/src/cxx/pcap_processor.cpp

@@ -267,8 +267,8 @@ void pcap_processor::process_packets(const Packet &pkt) {
         stats.incrementToScount(ipAddressSender, ipLayer.tos());
 
         // Protocol distribution
-        stats.incrementProtocolCount(ipAddressSender, "IPv4");
-        stats.increaseProtocolByteCount(ipAddressSender, "IPv4", sizeCurrentPacket);
+        stats.incrementProtocolCount(ipAddressSender, stats.PROT_IPV4);
+        stats.increaseProtocolByteCount(ipAddressSender, stats.PROT_IPV4, sizeCurrentPacket);
 
         // Assign IP Address to MAC Address
         stats.assignMacAddress(ipAddressSender, macAddressSender);
@@ -289,8 +289,8 @@ void pcap_processor::process_packets(const Packet &pkt) {
         stats.incrementTTLcount(ipAddressSender, ipLayer.hop_limit());
 
         // Protocol distribution
-        stats.incrementProtocolCount(ipAddressSender, "IPv6");
-        stats.increaseProtocolByteCount(ipAddressSender, "IPv6", sizeCurrentPacket);
+        stats.incrementProtocolCount(ipAddressSender, stats.PROT_IPV6);
+        stats.increaseProtocolByteCount(ipAddressSender, stats.PROT_IPV6, sizeCurrentPacket);
 
         // Assign IP Address to MAC Address
         stats.assignMacAddress(ipAddressSender, macAddressSender);
@@ -325,12 +325,12 @@ void pcap_processor::process_packets(const Packet &pkt) {
                 stats.checkTCPChecksum(ipAddressSender, ipAddressReceiver, tcpPkt);
             }
 
-            stats.incrementProtocolCount(ipAddressSender, "TCP");
-            stats.increaseProtocolByteCount(ipAddressSender, "TCP", sizeCurrentPacket);
+            stats.incrementProtocolCount(ipAddressSender, stats.PROT_TCP);
+            stats.increaseProtocolByteCount(ipAddressSender, stats.PROT_TCP, sizeCurrentPacket);
 
             // Conversation statistics
             stats.addConvStat(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), pkt.timestamp());
-            stats.addConvStatExt(ipAddressSender,tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), "TCP", pkt.timestamp());
+            stats.addConvStatExt(ipAddressSender,tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), stats.PROT_TCP, pkt.timestamp());
 
             // Window Size distribution
             int win = tcpPkt.window();
@@ -343,23 +343,23 @@ void pcap_processor::process_packets(const Packet &pkt) {
                 stats.incrementMSScount(ipAddressSender, mss_value);
             }
 
-            stats.incrementPortCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), "TCP");
-            stats.increasePortByteCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), sizeCurrentPacket, "TCP");
+            stats.incrementPortCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), stats.PROT_TCP);
+            stats.increasePortByteCount(ipAddressSender, tcpPkt.sport(), ipAddressReceiver, tcpPkt.dport(), sizeCurrentPacket, stats.PROT_TCP);
 
           // UDP Packet
         } else if (p == PDU::PDUType::UDP) {
             const UDP &udpPkt = (const UDP &) *pdu_l4;
-            stats.incrementProtocolCount(ipAddressSender, "UDP");
-            stats.increaseProtocolByteCount(ipAddressSender, "UDP", sizeCurrentPacket);
-            stats.incrementPortCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), "UDP");
-            stats.increasePortByteCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), sizeCurrentPacket, "UDP");
-            stats.addConvStatExt(ipAddressSender,udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), "UDP", pkt.timestamp());
+            stats.incrementProtocolCount(ipAddressSender, stats.PROT_UDP);
+            stats.increaseProtocolByteCount(ipAddressSender, stats.PROT_UDP, sizeCurrentPacket);
+            stats.incrementPortCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), stats.PROT_UDP);
+            stats.increasePortByteCount(ipAddressSender, udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), sizeCurrentPacket, stats.PROT_UDP);
+            stats.addConvStatExt(ipAddressSender,udpPkt.sport(), ipAddressReceiver, udpPkt.dport(), stats.PROT_UDP, pkt.timestamp());
         } else if (p == PDU::PDUType::ICMP) {
-            stats.incrementProtocolCount(ipAddressSender, "ICMP");
-            stats.increaseProtocolByteCount(ipAddressSender, "ICMP", sizeCurrentPacket);
+            stats.incrementProtocolCount(ipAddressSender, stats.PROT_ICMP);
+            stats.increaseProtocolByteCount(ipAddressSender, stats.PROT_ICMP, sizeCurrentPacket);
         } else if (p == PDU::PDUType::ICMPv6) {
-            stats.incrementProtocolCount(ipAddressSender, "ICMPv6");
-            stats.increaseProtocolByteCount(ipAddressSender, "ICMPv6", sizeCurrentPacket);
+            stats.incrementProtocolCount(ipAddressSender, stats.PROT_ICMPV6);
+            stats.increaseProtocolByteCount(ipAddressSender, stats.PROT_ICMPV6, sizeCurrentPacket);
         }
     }
 }

+ 11 - 11
code_boost/src/cxx/statistics.cpp

@@ -259,7 +259,7 @@ void statistics::addConvStat(const IPv4Address ipAddressSender, std::uint16_t sp
  * @param protocol The used protocol.
  * @param timestamp The timestamp of the packet.
  */
-void statistics::addConvStatExt(const IPv4Address ipAddressSender, std::uint16_t sport, const IPv4Address ipAddressReceiver, std::uint16_t dport, const std::string &protocol, std::chrono::microseconds timestamp){
+void statistics::addConvStatExt(const IPv4Address ipAddressSender, std::uint16_t sport, const IPv4Address ipAddressReceiver, std::uint16_t dport, const char * const protocol, std::chrono::microseconds timestamp){
     if(this->getDoExtraTests()) {
         convWithProt f1 = {ipAddressReceiver, ipAddressSender, dport, sport, protocol};
         convWithProt f2 = {ipAddressSender, ipAddressReceiver, sport, dport, protocol};
@@ -389,7 +389,7 @@ void statistics::incrementToScount(const IPv4Address ipAddress, int tosValue) {
  * @param ipAddress The IP address whose protocol packet counter should be incremented.
  * @param protocol The protocol of the packet.
  */
-void statistics::incrementProtocolCount(const IPv4Address ipAddress, const std::string &protocol) {
+void statistics::incrementProtocolCount(const IPv4Address ipAddress, const char * const protocol) {
     protocol_distribution[{ipAddress, protocol}].count++;
 }
 
@@ -398,7 +398,7 @@ void statistics::incrementProtocolCount(const IPv4Address ipAddress, const std::
  * @param ipAddress The IP address whose packet count is wanted.
  * @param protocol The protocol whose packet count is wanted.
  */
-int statistics::getProtocolCount(const IPv4Address ipAddress, const std::string &protocol) {
+int statistics::getProtocolCount(const IPv4Address ipAddress, const char * const protocol) {
     return protocol_distribution[{ipAddress, protocol}].count;
 }
 
@@ -408,7 +408,7 @@ int statistics::getProtocolCount(const IPv4Address ipAddress, const std::string
  * @param protocol The protocol of the packet.
  * @param byteSent The packet's size.
  */
-void statistics::increaseProtocolByteCount(const IPv4Address ipAddress, const std::string &protocol, long bytesSent) {
+void statistics::increaseProtocolByteCount(const IPv4Address ipAddress, const char * const protocol, long bytesSent) {
     protocol_distribution[{ipAddress, protocol}].byteCount += bytesSent;
 }
 
@@ -418,7 +418,7 @@ void statistics::increaseProtocolByteCount(const IPv4Address ipAddress, const st
  * @param protocol The protocol whose byte count is wanted.
  * @return a float: The number of bytes
  */
-float statistics::getProtocolByteCount(const IPv4Address ipAddress, const std::string &protocol) {
+float statistics::getProtocolByteCount(const IPv4Address ipAddress, const char * const protocol) {
     return protocol_distribution[{ipAddress, protocol}].byteCount;
 }
 
@@ -432,11 +432,11 @@ float statistics::getProtocolByteCount(const IPv4Address ipAddress, const std::s
  * @param incomingPort The port used by the receiver.
  */
 void statistics::incrementPortCount(const IPv4Address ipAddressSender, std::uint16_t outgoingPort, const IPv4Address ipAddressReceiver,
-                                    std::uint16_t incomingPort, const std::string &protocol) {
+                                    std::uint16_t incomingPort, const char * const protocol) {
     port_values[outgoingPort]++;
     port_values[incomingPort]++;
-    ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].count++;
-    ip_ports[{ipAddressReceiver, "in", incomingPort, protocol}].count++;
+    ip_ports[{ipAddressSender, outgoingPort, DIR_OUT, protocol}].count++;
+    ip_ports[{ipAddressReceiver, incomingPort, DIR_IN, protocol}].count++;
 }
 
 /**
@@ -450,9 +450,9 @@ void statistics::incrementPortCount(const IPv4Address ipAddressSender, std::uint
  * @param byteSent The packet's size.
  */
 void statistics::increasePortByteCount(const IPv4Address ipAddressSender, std::uint16_t outgoingPort, const IPv4Address ipAddressReceiver,
-                                       std::uint16_t incomingPort, long bytesSent, const std::string &protocol) {
-    ip_ports[{ipAddressSender, "out", outgoingPort, protocol}].byteCount += bytesSent;
-    ip_ports[{ipAddressReceiver, "in", incomingPort, protocol}].byteCount += bytesSent;
+                                       std::uint16_t incomingPort, long bytesSent, const char * const protocol) {
+    ip_ports[{ipAddressSender, outgoingPort, DIR_OUT, protocol}].byteCount += bytesSent;
+    ip_ports[{ipAddressReceiver, incomingPort, DIR_IN, protocol}].byteCount += bytesSent;
 }
 
 /**

+ 24 - 14
code_boost/src/cxx/statistics.h

@@ -76,7 +76,7 @@ struct convWithProt{
     IPv4Address ipAddressB;
     std::uint16_t portA;
     std::uint16_t portB;
-    std::string protocol;
+    const char *protocol;
 
     bool operator==(const convWithProt &other) const {
         return ipAddressA == other.ipAddressA
@@ -154,7 +154,7 @@ struct ipAddress_ttl {
  */
 struct ipAddress_protocol {
     IPv4Address ipAddress;
-    std::string protocol;
+    const char *protocol;
 
     bool operator==(const ipAddress_protocol &other) const {
         return ipAddress == other.ipAddress
@@ -302,9 +302,9 @@ struct entry_convStat {
  */
 struct ipAddress_inOut_port {
     IPv4Address ipAddress;
-    std::string trafficDirection;
     std::uint16_t portNumber;
-    std::string protocol;
+    const char *trafficDirection;
+    const char *protocol;
 
     bool operator==(const ipAddress_inOut_port &other) const {
         return ipAddress == other.ipAddress
@@ -470,7 +470,7 @@ namespace std {
                      ^ (hash<std::uint16_t>()(c.portA) << 1)) >> 1)
                      ^ ((hash<uint32_t>()(c.ipAddressB)
                      ^ (hash<std::uint16_t>()(c.portB) << 1)) >> 1)
-                     ^ (hash<string>()(c.protocol));
+                     ^ (hash<size_t>()((size_t)c.protocol));
         }
     };
     
@@ -481,7 +481,7 @@ namespace std {
             using std::hash;
             using std::string;
             return ((hash<uint32_t>()(k.ipAddress)
-                     ^ (hash<string>()(k.protocol) << 1)) >> 1);
+                     ^ (hash<size_t>()((size_t)k.protocol) << 1)) >> 1);
         }
     };
 
@@ -492,7 +492,7 @@ namespace std {
             using std::hash;
             using std::string;
             return ((hash<uint32_t>()(k.ipAddress)
-                     ^ (hash<string>()(k.trafficDirection) << 1)) >> 1)
+                     ^ (hash<size_t>()((size_t)k.trafficDirection) << 1)) >> 1)
                    ^ (hash<std::uint16_t>()(k.portNumber) << 1);
         }
     };
@@ -535,7 +535,7 @@ public:
 
     void addConvStat(const IPv4Address ipAddressSender, std::uint16_t sport, const IPv4Address ipAddressReceiver, std::uint16_t dport, std::chrono::microseconds timestamp);
 
-    void addConvStatExt(const IPv4Address ipAddressSender, std::uint16_t sport, const IPv4Address ipAddressReceiver, std::uint16_t dport, const std::string &protocol, std::chrono::microseconds timestamp);
+    void addConvStatExt(const IPv4Address ipAddressSender, std::uint16_t sport, const IPv4Address ipAddressReceiver, std::uint16_t dport, const char * const protocol, std::chrono::microseconds timestamp);
 
     void createCommIntervalStats();
 
@@ -555,22 +555,22 @@ public:
 
     void incrementTTLcount(const IPv4Address ipAddress, int ttlValue);
 
-    void incrementProtocolCount(const IPv4Address ipAddress, const std::string &protocol);
+    void incrementProtocolCount(const IPv4Address ipAddress, const char * const protocol);
 
-    void increaseProtocolByteCount(const IPv4Address ipAddress, const std::string &protocol, long bytesSent);
+    void increaseProtocolByteCount(const IPv4Address ipAddress, const char * const protocol, long bytesSent);
 
     void incrementUnrecognizedPDUCount(const std::string &srcMac, const std::string &dstMac, uint32_t typeNumber,
                                        const std::string &timestamp);
 
     void incrementPortCount(const IPv4Address ipAddressSender, std::uint16_t outgoingPort, const IPv4Address ipAddressReceiver,
-                            std::uint16_t incomingPort, const std::string &protocol);
+                            std::uint16_t incomingPort, const char * const protocol);
 
     void increasePortByteCount(const IPv4Address ipAddressSender, std::uint16_t outgoingPort, const IPv4Address ipAddressReceiver,
-                               std::uint16_t incomingPort, long bytesSent, const std::string &protocol);
+                               std::uint16_t incomingPort, long bytesSent, const char * const protocol);
 
-    int getProtocolCount(const IPv4Address ipAddress, const std::string &protocol);
+    int getProtocolCount(const IPv4Address ipAddress, const char * const protocol);
 
-    float getProtocolByteCount(const IPv4Address ipAddress, const std::string &protocol);
+    float getProtocolByteCount(const IPv4Address ipAddress, const char * const protocol);
 
     void setTimestampFirstPacket(Tins::Timestamp ts);
 
@@ -609,6 +609,16 @@ public:
      * IP Address-specific statistics
      */
     ip_stats getStatsForIP(const IPv4Address ipAddress);
+    
+    static constexpr const char * const PROT_TCP = "TCP";
+    static constexpr const char * const PROT_UDP = "UDP";
+    static constexpr const char * const PROT_IPV4 = "IPv4";
+    static constexpr const char * const PROT_IPV6 = "IPv6";
+    static constexpr const char * const PROT_ICMP = "ICMP";
+    static constexpr const char * const PROT_ICMPV6 = "ICMPv6";
+    
+    static constexpr const char * const DIR_OUT = "out";
+    static constexpr const char * const DIR_IN = "in";
 
 private:
     /*

+ 2 - 2
code_boost/src/cxx/statistics_db.cpp

@@ -475,7 +475,7 @@ void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entr
             int minDelay = -1;
             int maxDelay = -1;
 
-            if (e.pkts_count > 1 && f.protocol == "TCP"){
+            if (e.pkts_count > 1 && f.protocol == statistics::PROT_TCP){
                 for (int i = 0; (unsigned) i < e.interarrival_time.size(); i++) {
                     sumDelay += e.interarrival_time[i].count();
                     if (maxDelay < e.interarrival_time[i].count())
@@ -503,7 +503,7 @@ void statistics_db::writeStatisticsConvExt(std::unordered_map<convWithProt, entr
                 query.bind(6, (int) e.pkts_count);
                 query.bind(7, (float) e.avg_pkt_rate);
 
-                if (f.protocol == "UDP" || (f.protocol == "TCP" && e.pkts_count < 2))
+                if (f.protocol == statistics::PROT_UDP || (f.protocol == statistics::PROT_TCP && e.pkts_count < 2))
                     query.bind(8);
                 else
                     query.bind(8, (int) e.avg_interarrival_time.count());