Merge remote-tracking branch 'origin/topic/timw/sessions-unordered-map'

* origin/topic/timw/sessions-unordered-map:
  Only sort sessions during Drain() if a random seed is set
  Use unordered_map to store sessions for performance reasons
This commit is contained in:
Johanna Amann 2021-08-04 09:57:32 +01:00
commit d24cecf268
6 changed files with 59 additions and 7 deletions

View file

@ -1,3 +1,8 @@
4.2.0-dev.68 | 2021-08-04 09:57:32 +0100
* Use unordered_map to store sessions for performance reasons. This might lead to an 8-9% speedup of Zeek.
See GH-1706 for details. (Tim Wojtulewicz, Corelight)
4.2.0-dev.64 | 2021-08-03 10:23:41 +0100
* Under certain circumstances, Zeek processes could get into an infinite looping state inside RotationTimer.

View file

@ -1 +1 @@
4.2.0-dev.64
4.2.0-dev.68

View file

@ -70,4 +70,14 @@ bool Key::operator<(const Key& rhs) const
return memcmp(data, rhs.data, size) < 0;
}
bool Key::operator==(const Key& rhs) const
{
if ( size != rhs.size )
return false;
else if ( type != rhs.type )
return false;
return memcmp(data, rhs.data, size) == 0;
}
} // namespace zeek::session::detail

View file

@ -4,9 +4,12 @@
#include <cstddef>
#include <cstdint>
#include "zeek/Hash.h"
namespace zeek::session::detail {
struct KeyHash;
/**
* This type is used as the key for the map in SessionManager. It represents a
* raw block of memory that points to a key of some type for a session, such as
@ -57,12 +60,23 @@ public:
void CopyData();
bool operator<(const Key& rhs) const;
bool operator==(const Key& rhs) const;
std::size_t Hash() const {
return zeek::detail::HashKey::HashBytes(data, size);
}
private:
friend struct KeyHash;
const uint8_t* data = nullptr;
size_t size = 0;
size_t type = CONNECTION_KEY_TYPE;
bool copied = false;
};
struct KeyHash {
std::size_t operator()(const Key& k) const { return k.Hash(); }
};
} // namespace zeek::session::detail

View file

@ -217,6 +217,28 @@ void Manager::Insert(Session* s, bool remove_existing)
}
void Manager::Drain()
{
// If a random seed was passed in, we're most likely in testing mode and need the
// order of the sessions to be consistent. Sort the keys to force that order
// every run.
if ( zeek::util::detail::have_random_seed() )
{
std::vector<const detail::Key*> keys;
keys.reserve(session_map.size());
for ( auto& entry : session_map )
keys.push_back(&(entry.first));
std::sort(keys.begin(), keys.end(), [](const detail::Key* a, const detail::Key* b) {
return *a < *b; });
for ( const auto* k : keys )
{
Session* tc = session_map.at(*k);
tc->Done();
tc->RemovalEvent();
}
}
else
{
for ( const auto& entry : session_map )
{
@ -225,6 +247,7 @@ void Manager::Drain()
tc->RemovalEvent();
}
}
}
void Manager::Clear()
{

View file

@ -3,7 +3,7 @@
#pragma once
#include <sys/types.h> // for u_char
#include <map>
#include <unordered_map>
#include <utility>
#include "zeek/Frag.h"
@ -119,7 +119,7 @@ public:
private:
using SessionMap = std::map<detail::Key, Session*>;
using SessionMap = std::unordered_map<detail::Key, Session*, detail::KeyHash>;
// Inserts a new connection into the sessions map. If a connection with
// the same key already exists in the map, it will be overwritten by