lokinet/llarp/profiling.cpp

210 lines
4.9 KiB
C++
Raw Normal View History

#include <profiling.hpp>
#include <fstream>
namespace llarp
{
bool
RouterProfile::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictInt("g", connectGoodCount, buf))
return false;
if(!BEncodeWriteDictInt("p", pathSuccessCount, buf))
return false;
if(!BEncodeWriteDictInt("s", pathFailCount, buf))
return false;
if(!BEncodeWriteDictInt("t", connectTimeoutCount, buf))
return false;
2019-03-04 17:03:18 +00:00
if(!BEncodeWriteDictInt("u", lastUpdated, buf))
return false;
if(!BEncodeWriteDictInt("v", version, buf))
return false;
return bencode_end(buf);
}
bool
2019-02-02 23:12:42 +00:00
RouterProfile::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictInt("g", connectGoodCount, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("t", connectTimeoutCount, read, k, buf))
return false;
2019-03-04 17:03:18 +00:00
if(!BEncodeMaybeReadDictInt("u", lastUpdated, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("v", version, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("s", pathFailCount, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("p", pathSuccessCount, read, k, buf))
return false;
return read;
}
2019-03-04 17:03:18 +00:00
void
RouterProfile::Decay()
2019-03-04 17:03:18 +00:00
{
connectGoodCount /= 2;
connectTimeoutCount /= 2;
pathSuccessCount /= 2;
pathFailCount /= 2;
lastUpdated = llarp::time_now_ms();
2019-03-04 17:03:18 +00:00
}
void
RouterProfile::Tick()
{
2019-03-22 16:45:04 +00:00
// 5 minutes
static constexpr llarp_time_t updateInterval = DEFAULT_PATH_LIFETIME / 2;
2019-03-04 17:03:18 +00:00
auto now = llarp::time_now_ms();
if(lastUpdated < now && now - lastUpdated > updateInterval)
{
Decay();
2019-03-04 17:03:18 +00:00
}
}
bool
RouterProfile::IsGood(uint64_t chances) const
{
if(connectTimeoutCount > chances)
2019-03-07 15:17:29 +00:00
return connectTimeoutCount <= connectGoodCount
&& (pathSuccessCount * chances) >= pathFailCount;
else
return (pathSuccessCount * chances) >= pathFailCount;
}
bool
Profiling::IsBad(const RouterID& r, uint64_t chances)
{
lock_t lock(&m_ProfilesMutex);
auto itr = m_Profiles.find(r);
if(itr == m_Profiles.end())
return false;
return !itr->second.IsGood(chances);
}
2019-03-04 17:03:18 +00:00
void
Profiling::Tick()
{
lock_t lock(&m_ProfilesMutex);
2019-03-04 17:03:18 +00:00
std::for_each(m_Profiles.begin(), m_Profiles.end(),
[](auto& item) { item.second.Tick(); });
}
void
Profiling::MarkTimeout(const RouterID& r)
{
lock_t lock(&m_ProfilesMutex);
m_Profiles[r].connectTimeoutCount += 1;
2019-03-04 17:03:18 +00:00
m_Profiles[r].lastUpdated = llarp::time_now_ms();
}
void
Profiling::MarkSuccess(const RouterID& r)
{
lock_t lock(&m_ProfilesMutex);
m_Profiles[r].connectGoodCount += 1;
2019-03-04 17:03:18 +00:00
m_Profiles[r].lastUpdated = llarp::time_now_ms();
}
void
Profiling::MarkPathFail(path::Path* p)
{
lock_t lock(&m_ProfilesMutex);
for(const auto& hop : p->hops)
{
// TODO: also mark bad?
m_Profiles[hop.rc.pubkey].pathFailCount += 1;
2019-03-04 17:03:18 +00:00
m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms();
}
}
void
Profiling::MarkPathSuccess(path::Path* p)
{
lock_t lock(&m_ProfilesMutex);
for(const auto& hop : p->hops)
{
m_Profiles[hop.rc.pubkey].pathSuccessCount += 1;
2019-03-04 17:03:18 +00:00
m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms();
}
}
bool
Profiling::Save(const char* fname)
{
absl::ReaderMutexLock lock(&m_ProfilesMutex);
size_t sz = (m_Profiles.size() * (RouterProfile::MaxSize + 32 + 8)) + 8;
2019-01-17 14:02:50 +00:00
std::vector< byte_t > tmp(sz, 0);
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(tmp);
auto res = BEncodeNoLock(&buf);
if(res)
{
buf.sz = buf.cur - buf.base;
std::ofstream f;
f.open(fname);
if(f.is_open())
{
f.write((char*)buf.base, buf.sz);
}
}
return res;
}
bool
Profiling::BEncode(llarp_buffer_t* buf) const
{
absl::ReaderMutexLock lock(&m_ProfilesMutex);
return BEncodeNoLock(buf);
}
bool
Profiling::BEncodeNoLock(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
auto itr = m_Profiles.begin();
while(itr != m_Profiles.end())
{
if(!itr->first.BEncode(buf))
return false;
if(!itr->second.BEncode(buf))
return false;
++itr;
}
return bencode_end(buf);
}
bool
2019-02-02 23:12:42 +00:00
Profiling::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
{
if(k.sz != 32)
return false;
RouterProfile profile;
if(!profile.BDecode(buf))
return false;
RouterID pk = k.base;
return m_Profiles.emplace(pk, profile).second;
}
bool
Profiling::Load(const char* fname)
{
lock_t lock(&m_ProfilesMutex);
m_Profiles.clear();
if(!BDecodeReadFile(fname, *this))
{
llarp::LogWarn("failed to load router profiles from ", fname);
return false;
}
return true;
}
} // namespace llarp