Skip to content

Commit

Permalink
pass in a hashed key to the threadlocal cache
Browse files Browse the repository at this point in the history
500 mb threadlocal 2 t
  • Loading branch information
ghoshkaj committed Apr 9, 2018
1 parent 87d1cb8 commit 0470a4e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 20 deletions.
6 changes: 4 additions & 2 deletions include/engine/data_watchdog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{
static_region.shm_key, updatable_region.shm_key}), static_region.timestamp);
static_region.shm_key, updatable_region.shm_key}),
static_region.timestamp);
}

watcher = std::thread(&DataWatchdogImpl::Run, this);
Expand Down Expand Up @@ -115,7 +116,8 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{
static_region.shm_key, updatable_region.shm_key}), static_region.timestamp);
static_region.shm_key, updatable_region.shm_key}),
static_region.timestamp);
}

util::Log() << "DataWatchdog thread stopped";
Expand Down
3 changes: 2 additions & 1 deletion include/engine/datafacade_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ template <template <typename A> class FacadeT, typename AlgorithmT> class DataFa
std::size_t index =
std::stoi(exclude_prefix.substr(index_begin + 1, exclude_prefix.size()));
BOOST_ASSERT(index >= 0 && index < facades.size());
facades[index] = std::make_shared<const Facade>(allocator, metric_name, index, timestamp);
facades[index] =
std::make_shared<const Facade>(allocator, metric_name, index, timestamp);
}

for (const auto index : util::irange<std::size_t>(0, properties->class_names.size()))
Expand Down
70 changes: 53 additions & 17 deletions include/engine/unpacking_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define UNPACKING_CACHE_HPP

#include <boost/optional/optional_io.hpp>
#include <boost/thread.hpp>

#include "../../third_party/compute_detail/lru_cache.hpp"
#include "util/typedefs.hpp"
Expand All @@ -11,28 +12,60 @@ namespace osrm
namespace engine
{
typedef unsigned char ExcludeIndex;
typedef unsigned Timestamp;
typedef std::tuple<NodeID, NodeID, ExcludeIndex> Key;
typedef std::size_t HashedKey;

struct HashKey
{
std::size_t operator()(Key const &key) const noexcept
{
std::size_t h1 = std::hash<NodeID>{}(std::get<0>(key));
std::size_t h2 = std::hash<NodeID>{}(std::get<1>(key));
std::size_t h3 = std::hash<ExcludeIndex>{}(std::get<2>(key));

std::size_t seed = 0;
boost::hash_combine(seed, h1);
boost::hash_combine(seed, h2);
boost::hash_combine(seed, h3);

return seed;
}
};

class UnpackingCache
{
private:
boost::compute::detail::lru_cache<std::tuple<NodeID, NodeID, ExcludeIndex>, EdgeDuration>
m_cache;
boost::compute::detail::lru_cache<HashedKey, EdgeDuration> m_cache;
unsigned m_current_data_timestamp = 0;

public:
// TO FIGURE OUT HOW MANY LINES TO INITIALIZE CACHE TO:
// Assume max cache size is 500mb (see bottom of OP here:
// https://github.com/Project-OSRM/osrm-backend/issues/4798#issue-288608332)

// LRU CACHE IMPLEMENTATION HAS THESE TWO STORAGE CONTAINERS
// map: n * tuple_hash + n * EdgeDuration
// = n * std::size_t + n * std::int32_t
// = n * 8 bytes + n * 4 bytes
// = n * 12 bytes
// list: n * HashedKey
// = n * std::size_t
// = n * 8 bytes
// Total = n * 20 bytes
// Total cache size: 500 mb = 500 * 1024 *1024 bytes = 524288000 bytes
// Assume unsigned char is 1 byte (my local machine this is the case):
// Current cache line = NodeID * 2 + unsigned char * 1 + EdgeDuration * 1
// = std::uint32_t * 2 + unsigned char * 1 + std::int32_t * 1
// = 4 bytes * 3 + 1 byte = 13 bytes
// Number of cache lines is 500 mb = 500 * 1024 *1024 bytes = 524288000 bytes / 13 = 40329846
// For threadlocal cache, Number of cache lines = max cache size / number of threads
// (Assume that the number of threads is 16)
// = 40329846 / 16 = 2520615

UnpackingCache(unsigned timestamp) : m_cache(2520615), m_current_data_timestamp(timestamp){};
// THREAD LOCAL STORAGE
// Number of lines we need = 524288000 / 20 / number of threads = 26214400 / number of threads
// 16 threads: 26214400 / 16 = 1638400
// 8 threads: 26214400 / 8 = 3276800
// 4 threads: 26214400 / 4 = 6553600
// 2 threads: 26214400 / 2 = 13107200

// SHARED STORAGE CACHE
// Number of lines we need for shared storage cache = 524288000 / 20 = 26214400

UnpackingCache(unsigned timestamp) : m_cache(13107200), m_current_data_timestamp(timestamp){};

UnpackingCache(std::size_t cache_size, unsigned timestamp)
: m_cache(cache_size), m_current_data_timestamp(timestamp){};
Expand All @@ -46,19 +79,22 @@ class UnpackingCache
}
}

bool IsEdgeInCache(std::tuple<NodeID, NodeID, ExcludeIndex> edge)
bool IsEdgeInCache(Key edge)
{
return m_cache.contains(edge);
HashedKey hashed_edge = HashKey{}(edge);
return m_cache.contains(hashed_edge);
}

void AddEdge(std::tuple<NodeID, NodeID, ExcludeIndex> edge, EdgeDuration duration)
void AddEdge(Key edge, EdgeDuration duration)
{
m_cache.insert(edge, duration);
HashedKey hashed_edge = HashKey{}(edge);
m_cache.insert(hashed_edge, duration);
}

EdgeDuration GetDuration(std::tuple<NodeID, NodeID, ExcludeIndex> edge)
EdgeDuration GetDuration(Key edge)
{
boost::optional<EdgeDuration> duration = m_cache.get(edge);
HashedKey hashed_edge = HashKey{}(edge);
boost::optional<EdgeDuration> duration = m_cache.get(hashed_edge);
return duration ? *duration : MAXIMAL_EDGE_DURATION;
}
};
Expand Down

0 comments on commit 0470a4e

Please sign in to comment.