Merge pull request #1877 from majestrate/path-cache-cpu-reduction-2022-03-29

Cache best paths determined by GetPathByRouter to reduce cpu usage
This commit is contained in:
majestrate 2022-03-30 20:12:58 -04:00 committed by GitHub
commit 619d8d3776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 11 deletions

View File

@ -367,7 +367,7 @@ local docs_pipeline(name, image, extra_cmds=[], allow_fail=false) = {
'-DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8 ' + '-DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8 ' +
'-DCMAKE_CXX_FLAGS="-march=x86-64 -mtune=haswell" ' + '-DCMAKE_CXX_FLAGS="-march=x86-64 -mtune=haswell" ' +
'-DCMAKE_C_FLAGS="-march=x86-64 -mtune=haswell" ' + '-DCMAKE_C_FLAGS="-march=x86-64 -mtune=haswell" ' +
'-DNATIVE_BUILD=OFF -DWITH_SYSTEMD=OFF -DWITH_BOOTSTRAP=OFF', '-DNATIVE_BUILD=OFF -DWITH_SYSTEMD=OFF -DWITH_BOOTSTRAP=OFF -DBUILD_LIBLOKINET=OFF',
extra_cmds=[ extra_cmds=[
'../contrib/ci/drone-check-static-libs.sh', '../contrib/ci/drone-check-static-libs.sh',
'../contrib/ci/drone-static-upload.sh', '../contrib/ci/drone-static-upload.sh',
@ -393,11 +393,11 @@ local docs_pipeline(name, image, extra_cmds=[], allow_fail=false) = {
cmake_extra='-DWITH_HIVE=ON'), cmake_extra='-DWITH_HIVE=ON'),
// Deb builds: // Deb builds:
deb_builder(docker_base + 'debian-sid-debhelper', 'sid', 'debian/sid'), deb_builder(docker_base + 'debian-sid-builder', 'sid', 'debian/sid'),
deb_builder(docker_base + 'debian-bullseye-debhelper', 'bullseye', 'debian/bullseye'), deb_builder(docker_base + 'debian-bullseye-builder', 'bullseye', 'debian/bullseye'),
deb_builder(docker_base + 'ubuntu-impish-debhelper', 'impish', 'ubuntu/impish'), deb_builder(docker_base + 'ubuntu-impish-builder', 'impish', 'ubuntu/impish'),
deb_builder(docker_base + 'ubuntu-focal-debhelper', 'focal', 'ubuntu/focal'), deb_builder(docker_base + 'ubuntu-focal-builder', 'focal', 'ubuntu/focal'),
deb_builder(docker_base + 'debian-sid-debhelper', 'sid', 'debian/sid', arch='arm64'), deb_builder(docker_base + 'debian-sid-builder', 'sid', 'debian/sid', arch='arm64'),
// Macos builds: // Macos builds:
mac_builder('macOS (Release)'), mac_builder('macOS (Release)'),

View File

@ -191,10 +191,11 @@ namespace llarp
lastBuild = 0s; lastBuild = 0s;
} }
void Builder::Tick(llarp_time_t) void
Builder::Tick(llarp_time_t now)
{ {
const auto now = llarp::time_now_ms(); PathSet::Tick(now);
now = llarp::time_now_ms();
m_router->pathBuildLimiter().Decay(now); m_router->pathBuildLimiter().Decay(now);
ExpirePaths(now, m_router); ExpirePaths(now, m_router);

View File

@ -66,13 +66,31 @@ namespace llarp
PathSet::TickPaths(AbstractRouter* r) PathSet::TickPaths(AbstractRouter* r)
{ {
const auto now = llarp::time_now_ms(); const auto now = llarp::time_now_ms();
Lock_t l(m_PathsMutex); Lock_t l{m_PathsMutex};
for (auto& item : m_Paths) for (auto& item : m_Paths)
{ {
item.second->Tick(now, r); item.second->Tick(now, r);
} }
} }
void PathSet::Tick(llarp_time_t)
{
std::unordered_set<RouterID> endpoints;
for (auto& item : m_Paths)
{
endpoints.emplace(item.second->Endpoint());
}
m_PathCache.clear();
for (const auto& ep : endpoints)
{
if (auto path = GetPathByRouter(ep))
{
m_PathCache[ep] = path->weak_from_this();
}
}
}
void void
PathSet::ExpirePaths(llarp_time_t now, AbstractRouter* router) PathSet::ExpirePaths(llarp_time_t now, AbstractRouter* router)
{ {
@ -150,6 +168,13 @@ namespace llarp
{ {
Lock_t l(m_PathsMutex); Lock_t l(m_PathsMutex);
Path_ptr chosen = nullptr; Path_ptr chosen = nullptr;
if (roles == ePathRoleAny)
{
if (auto itr = m_PathCache.find(id); itr != m_PathCache.end())
{
return itr->second.lock();
}
}
auto itr = m_Paths.begin(); auto itr = m_Paths.begin();
while (itr != m_Paths.end()) while (itr != m_Paths.end())
{ {

View File

@ -130,7 +130,7 @@ namespace llarp
/// tick owned paths /// tick owned paths
virtual void virtual void
Tick(llarp_time_t now) = 0; Tick(llarp_time_t now);
/// count the number of paths that will exist at this timestamp in future /// count the number of paths that will exist at this timestamp in future
size_t size_t
@ -320,6 +320,9 @@ namespace llarp
using PathMap_t = std::unordered_map<std::pair<RouterID, PathID_t>, Path_ptr>; using PathMap_t = std::unordered_map<std::pair<RouterID, PathID_t>, Path_ptr>;
mutable Mtx_t m_PathsMutex; mutable Mtx_t m_PathsMutex;
PathMap_t m_Paths; PathMap_t m_Paths;
private:
std::unordered_map<RouterID, std::weak_ptr<path::Path>> m_PathCache;
}; };
} // namespace path } // namespace path