pass in a hashed key to the threadlocal cache

500 mb threadlocal 2 t
This commit is contained in:
Kajari Ghosh 2018-04-05 08:11:05 -04:00
parent 90e0beaed6
commit 0fb706866c
3 changed files with 60 additions and 21 deletions

View File

@ -60,7 +60,8 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>( DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
std::make_shared<datafacade::SharedMemoryAllocator>( std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{ 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); watcher = std::thread(&DataWatchdogImpl::Run, this);
@ -115,7 +116,8 @@ class DataWatchdogImpl<AlgorithmT, datafacade::ContiguousInternalMemoryDataFacad
DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>( DataFacadeFactory<datafacade::ContiguousInternalMemoryDataFacade, AlgorithmT>(
std::make_shared<datafacade::SharedMemoryAllocator>( std::make_shared<datafacade::SharedMemoryAllocator>(
std::vector<storage::SharedRegionRegister::ShmKey>{ 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"; util::Log() << "DataWatchdog thread stopped";

View File

@ -71,7 +71,8 @@ template <template <typename A> class FacadeT, typename AlgorithmT> class DataFa
std::size_t index = std::size_t index =
std::stoi(exclude_prefix.substr(index_begin + 1, exclude_prefix.size())); std::stoi(exclude_prefix.substr(index_begin + 1, exclude_prefix.size()));
BOOST_ASSERT(index >= 0 && index < facades.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())) for (const auto index : util::irange<std::size_t>(0, properties->class_names.size()))

View File

@ -2,6 +2,7 @@
#define UNPACKING_CACHE_HPP #define UNPACKING_CACHE_HPP
#include <boost/optional/optional_io.hpp> #include <boost/optional/optional_io.hpp>
#include <boost/thread.hpp>
#include "../../third_party/compute_detail/lru_cache.hpp" #include "../../third_party/compute_detail/lru_cache.hpp"
#include "util/typedefs.hpp" #include "util/typedefs.hpp"
@ -11,28 +12,60 @@ namespace osrm
namespace engine namespace engine
{ {
typedef unsigned char ExcludeIndex; 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 class UnpackingCache
{ {
private: private:
boost::compute::detail::lru_cache<std::tuple<NodeID, NodeID, ExcludeIndex>, EdgeDuration> boost::compute::detail::lru_cache<HashedKey, EdgeDuration> m_cache;
m_cache;
unsigned m_current_data_timestamp = 0; unsigned m_current_data_timestamp = 0;
public: public:
// TO FIGURE OUT HOW MANY LINES TO INITIALIZE CACHE TO: // TO FIGURE OUT HOW MANY LINES TO INITIALIZE CACHE TO:
// Assume max cache size is 500mb (see bottom of OP here: // Assume max cache size is 500mb (see bottom of OP here:
// https://github.com/Project-OSRM/osrm-backend/issues/4798#issue-288608332) // https://github.com/Project-OSRM/osrm-backend/issues/4798#issue-288608332)
// 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){}; // 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
// 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) UnpackingCache(std::size_t cache_size, unsigned timestamp)
: m_cache(cache_size), m_current_data_timestamp(timestamp){}; : m_cache(cache_size), m_current_data_timestamp(timestamp){};
@ -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; return duration ? *duration : MAXIMAL_EDGE_DURATION;
} }
}; };