From 8435bfc4febe19bc5a79f0fbd7b30eede73d17ee Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Thu, 7 Jun 2007 12:25:40 +0200 Subject: [PATCH] add back optimization option --- src/common/packet-history.cc | 101 +++++++++++++++++++++++++++++++++++ src/common/packet-history.h | 9 ++-- utils/bench-packets.cc | 2 +- 3 files changed, 107 insertions(+), 5 deletions(-) diff --git a/src/common/packet-history.cc b/src/common/packet-history.cc index 36dd29a6a..f845fd678 100644 --- a/src/common/packet-history.cc +++ b/src/common/packet-history.cc @@ -35,6 +35,7 @@ bool PacketHistory::m_enable = false; uint32_t PacketHistory::m_maxSize = 0; uint16_t PacketHistory::m_chunkUid = 0; PacketHistory::DataFreeList PacketHistory::m_freeList; +bool g_optOne = false; void PacketHistory::Enable (void) @@ -42,6 +43,12 @@ PacketHistory::Enable (void) m_enable = true; } +void +PacketHistory::SetOptOne (bool optOne) +{ + g_optOne = optOne; +} + void PacketHistory::ReserveCopy (uint32_t size) { @@ -278,6 +285,71 @@ PacketHistory::TryToAppend (uint32_t value, uint8_t **pBuffer, uint8_t *end) return false; } +void +PacketHistory::AppendValueExtra (uint32_t value, uint8_t *buffer) +{ + if (value < 0x200000) + { + uint8_t byte = value & (~0x80); + buffer[0] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + buffer[1] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + buffer[2] = value; + return; + } + if (value < 0x10000000) + { + uint8_t byte = value & (~0x80); + buffer[0] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + buffer[1] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + buffer[2] = 0x80 | byte; + value >>= 7; + buffer[3] = value; + return; + } + { + uint8_t byte = value & (~0x80); + buffer[0] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + buffer[1] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + buffer[2] = 0x80 | byte; + value >>= 7; + byte = value & (~0x80); + buffer[3] = 0x80 | byte; + value >>= 7; + buffer[4] = value; + } +} + +void +PacketHistory::AppendValue (uint32_t value, uint8_t *buffer) +{ + if (value < 0x80) + { + buffer[0] = value; + return; + } + if (value < 0x4000) + { + uint8_t byte = value & (~0x80); + buffer[0] = 0x80 | byte; + value >>= 7; + buffer[1] = value; + return; + } + AppendValueExtra (value, buffer); +} + void PacketHistory::UpdateTail (uint16_t written) { @@ -331,6 +403,35 @@ uint16_t PacketHistory::AddSmall (const struct PacketHistory::SmallItem *item) { NS_ASSERT (m_data != 0); + if (g_optOne) + { + uint32_t typeUidSize = GetUleb128Size (item->typeUid); + uint32_t sizeSize = GetUleb128Size (item->size); + uint32_t n = typeUidSize + sizeSize + 2 + 2 + 2; + restart: + if (m_used + n <= m_data->m_size && + (m_head == 0xffff || + m_data->m_count == 1 || + m_used == m_data->m_dirtyEnd)) + { + uint8_t *buffer = &m_data->m_data[m_used]; + Append16 (item->next, buffer); + buffer += 2; + Append16 (item->prev, buffer); + buffer += 2; + AppendValue (item->typeUid, buffer); + buffer += typeUidSize; + AppendValue (item->size, buffer); + buffer += sizeSize; + Append16 (item->chunkUid, buffer); + } + else + { + ReserveCopy (n); + goto restart; + } + return n; + } append: uint8_t *start = &m_data->m_data[m_used]; uint8_t *end = &m_data->m_data[m_data->m_size]; diff --git a/src/common/packet-history.h b/src/common/packet-history.h index 084893bfb..a0424c8f6 100644 --- a/src/common/packet-history.h +++ b/src/common/packet-history.h @@ -27,10 +27,6 @@ #include "ns3/assert.h" #include "packet-printer.h" -namespace { -class ItemList; -} - namespace ns3 { class Chunk; @@ -39,6 +35,7 @@ class Buffer; class PacketHistory { public: static void Enable (void); + static void SetOptOne (bool optOne); inline PacketHistory (uint32_t uid, uint32_t size); inline PacketHistory (PacketHistory const &o); @@ -64,6 +61,8 @@ public: void PrintDefault (std::ostream &os, Buffer buffer) const; void Print (std::ostream &os, Buffer buffer, PacketPrinter const &printer) const; + static void PrintStats (void); + private: /** head -(next)-> tail @@ -113,6 +112,8 @@ private: inline bool TryToAppendFast (uint32_t value, uint8_t **pBuffer, uint8_t *end); inline bool TryToAppend32 (uint32_t value, uint8_t **pBuffer, uint8_t *end); inline bool TryToAppend16 (uint16_t value, uint8_t **pBuffer, uint8_t *end); + void AppendValue (uint32_t value, uint8_t *buffer); + void AppendValueExtra (uint32_t value, uint8_t *buffer); inline void Reserve (uint32_t n); void ReserveCopy (uint32_t n); uint32_t DoPrint (struct PacketHistory::SmallItem *item, uint32_t current, diff --git a/utils/bench-packets.cc b/utils/bench-packets.cc index e3c66011c..c18444536 100644 --- a/utils/bench-packets.cc +++ b/utils/bench-packets.cc @@ -179,7 +179,7 @@ int main (int argc, char *argv[]) char const *nAscii = argv[0] + strlen ("--n="); n = atoi (nAscii); } - if (strncmp ("--enable-history", argv[0], strlen ("--enable-metadata")) == 0) + if (strncmp ("--enable-metadata", argv[0], strlen ("--enable-metadata")) == 0) { Packet::EnableMetadata (); }