2018-12-12 03:52:51 +01:00
|
|
|
#include <profiling.hpp>
|
|
|
|
|
2018-09-13 18:41:53 +02:00
|
|
|
#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;
|
2018-09-14 16:50:37 +02:00
|
|
|
if(!BEncodeWriteDictInt("p", pathSuccessCount, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("s", pathFailCount, buf))
|
|
|
|
return false;
|
2018-09-13 18:41:53 +02:00
|
|
|
if(!BEncodeWriteDictInt("t", connectTimeoutCount, buf))
|
|
|
|
return false;
|
2019-03-04 18:03:18 +01:00
|
|
|
if(!BEncodeWriteDictInt("u", lastUpdated, buf))
|
|
|
|
return false;
|
2018-09-13 18:41:53 +02:00
|
|
|
if(!BEncodeWriteDictInt("v", version, buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-03 00:12:42 +01:00
|
|
|
RouterProfile::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
|
2018-09-13 18:41:53 +02:00
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("g", connectGoodCount, read, k, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("t", connectTimeoutCount, read, k, buf))
|
|
|
|
return false;
|
2019-03-04 18:03:18 +01:00
|
|
|
if(!BEncodeMaybeReadDictInt("u", lastUpdated, read, k, buf))
|
|
|
|
return false;
|
2018-09-13 18:41:53 +02:00
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, k, buf))
|
|
|
|
return false;
|
2018-09-14 16:50:37 +02:00
|
|
|
if(!BEncodeMaybeReadDictInt("s", pathFailCount, read, k, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("p", pathSuccessCount, read, k, buf))
|
|
|
|
return false;
|
2018-09-13 18:41:53 +02:00
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
2019-03-04 18:03:18 +01:00
|
|
|
void
|
2019-03-05 14:38:50 +01:00
|
|
|
RouterProfile::Decay()
|
2019-03-04 18:03:18 +01:00
|
|
|
{
|
2019-03-05 14:38:50 +01:00
|
|
|
connectGoodCount /= 2;
|
|
|
|
connectTimeoutCount /= 2;
|
|
|
|
pathSuccessCount /= 2;
|
|
|
|
pathFailCount /= 2;
|
2019-04-16 19:30:07 +02:00
|
|
|
lastDecay = llarp::time_now_ms();
|
2019-03-04 18:03:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterProfile::Tick()
|
|
|
|
{
|
2019-04-16 19:27:35 +02:00
|
|
|
// 15 seconds
|
|
|
|
static constexpr llarp_time_t updateInterval = 15 * 1000;
|
2019-04-16 19:30:07 +02:00
|
|
|
const auto now = llarp::time_now_ms();
|
|
|
|
if(lastDecay < now && now - lastDecay > updateInterval)
|
2019-03-05 14:38:50 +01:00
|
|
|
Decay();
|
2019-03-04 18:03:18 +01:00
|
|
|
}
|
|
|
|
|
2018-09-13 18:41:53 +02:00
|
|
|
bool
|
2018-09-14 16:50:37 +02:00
|
|
|
RouterProfile::IsGood(uint64_t chances) const
|
2018-09-13 18:41:53 +02:00
|
|
|
{
|
2019-03-11 14:58:31 +01:00
|
|
|
if(connectTimeoutCount > chances)
|
2019-04-05 16:58:22 +02:00
|
|
|
return connectTimeoutCount < connectGoodCount
|
|
|
|
&& (pathSuccessCount * chances) > pathFailCount;
|
|
|
|
return (pathSuccessCount * chances) > pathFailCount;
|
2018-09-13 18:41:53 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 15:20:48 +02:00
|
|
|
static bool constexpr checkIsGood(uint64_t fails, uint64_t success,
|
|
|
|
uint64_t chances)
|
2019-04-16 13:44:55 +02:00
|
|
|
{
|
2019-04-16 19:27:35 +02:00
|
|
|
if(fails > 0 && (fails + success) >= chances)
|
2019-04-17 16:46:00 +02:00
|
|
|
return (success / fails) > 1;
|
2019-04-16 13:44:55 +02:00
|
|
|
if(success == 0)
|
|
|
|
return fails < chances;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterProfile::IsGoodForConnect(uint64_t chances) const
|
|
|
|
{
|
|
|
|
return checkIsGood(connectTimeoutCount, connectGoodCount, chances);
|
|
|
|
}
|
2019-04-16 15:20:48 +02:00
|
|
|
|
2019-04-16 13:44:55 +02:00
|
|
|
bool
|
|
|
|
RouterProfile::IsGoodForPath(uint64_t chances) const
|
|
|
|
{
|
|
|
|
return checkIsGood(pathFailCount, pathSuccessCount, chances);
|
|
|
|
}
|
|
|
|
|
2019-04-25 13:00:18 +02:00
|
|
|
Profiling::Profiling() : IBEncodeMessage()
|
|
|
|
{
|
|
|
|
m_DisableProfiling.store(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Profiling::~Profiling()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Profiling::Disable()
|
|
|
|
{
|
|
|
|
m_DisableProfiling.store(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Profiling::Enable()
|
|
|
|
{
|
|
|
|
m_DisableProfiling.store(false);
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:44:55 +02:00
|
|
|
bool
|
2019-04-16 15:20:48 +02:00
|
|
|
Profiling::IsBadForConnect(const RouterID& r, uint64_t chances)
|
2019-04-16 13:44:55 +02:00
|
|
|
{
|
2019-04-25 13:00:18 +02:00
|
|
|
if(m_DisableProfiling.load())
|
|
|
|
return false;
|
2019-04-16 13:44:55 +02:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
|
|
|
auto itr = m_Profiles.find(r);
|
|
|
|
if(itr == m_Profiles.end())
|
|
|
|
return false;
|
|
|
|
return !itr->second.IsGoodForConnect(chances);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-04-16 15:20:48 +02:00
|
|
|
Profiling::IsBadForPath(const RouterID& r, uint64_t chances)
|
2019-04-16 13:44:55 +02:00
|
|
|
{
|
2019-04-25 13:00:18 +02:00
|
|
|
if(m_DisableProfiling.load())
|
|
|
|
return false;
|
2019-04-16 13:44:55 +02:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
|
|
|
auto itr = m_Profiles.find(r);
|
|
|
|
if(itr == m_Profiles.end())
|
|
|
|
return false;
|
|
|
|
return !itr->second.IsGoodForPath(chances);
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:41:53 +02:00
|
|
|
bool
|
2018-09-14 16:50:37 +02:00
|
|
|
Profiling::IsBad(const RouterID& r, uint64_t chances)
|
2018-09-13 18:41:53 +02:00
|
|
|
{
|
2019-04-25 13:00:18 +02:00
|
|
|
if(m_DisableProfiling.load())
|
|
|
|
return false;
|
2019-03-03 16:01:05 +01:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
2018-09-13 18:41:53 +02:00
|
|
|
auto itr = m_Profiles.find(r);
|
|
|
|
if(itr == m_Profiles.end())
|
|
|
|
return false;
|
2018-09-14 16:50:37 +02:00
|
|
|
return !itr->second.IsGood(chances);
|
2018-09-13 18:41:53 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 18:03:18 +01:00
|
|
|
void
|
|
|
|
Profiling::Tick()
|
|
|
|
{
|
2019-03-06 01:54:35 +01:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
2019-03-04 18:03:18 +01:00
|
|
|
std::for_each(m_Profiles.begin(), m_Profiles.end(),
|
|
|
|
[](auto& item) { item.second.Tick(); });
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:41:53 +02:00
|
|
|
void
|
2019-04-17 16:46:00 +02:00
|
|
|
Profiling::MarkConnectTimeout(const RouterID& r)
|
2018-09-13 18:41:53 +02:00
|
|
|
{
|
2019-03-03 16:01:05 +01:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
2018-09-13 18:41:53 +02:00
|
|
|
m_Profiles[r].connectTimeoutCount += 1;
|
2019-03-04 18:03:18 +01:00
|
|
|
m_Profiles[r].lastUpdated = llarp::time_now_ms();
|
2018-09-13 18:41:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-04-17 16:46:00 +02:00
|
|
|
Profiling::MarkConnectSuccess(const RouterID& r)
|
2018-09-13 18:41:53 +02:00
|
|
|
{
|
2019-03-03 16:01:05 +01:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
2018-09-13 18:41:53 +02:00
|
|
|
m_Profiles[r].connectGoodCount += 1;
|
2019-03-04 18:03:18 +01:00
|
|
|
m_Profiles[r].lastUpdated = llarp::time_now_ms();
|
2018-09-13 18:41:53 +02:00
|
|
|
}
|
|
|
|
|
2019-03-31 17:25:13 +02:00
|
|
|
void
|
|
|
|
Profiling::ClearProfile(const RouterID& r)
|
|
|
|
{
|
|
|
|
lock_t lock(&m_ProfilesMutex);
|
|
|
|
m_Profiles.erase(r);
|
|
|
|
}
|
|
|
|
|
2018-09-14 16:50:37 +02:00
|
|
|
void
|
|
|
|
Profiling::MarkPathFail(path::Path* p)
|
|
|
|
{
|
2019-03-03 16:01:05 +01:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
2019-05-07 14:31:34 +02:00
|
|
|
size_t idx = 0;
|
2018-09-14 16:50:37 +02:00
|
|
|
for(const auto& hop : p->hops)
|
|
|
|
{
|
2019-05-07 14:31:34 +02:00
|
|
|
// don't mark first hop as failure because we are connected to it directly
|
|
|
|
if(idx)
|
|
|
|
{
|
|
|
|
m_Profiles[hop.rc.pubkey].pathFailCount += 1;
|
|
|
|
m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms();
|
|
|
|
}
|
|
|
|
++idx;
|
2018-09-14 16:50:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Profiling::MarkPathSuccess(path::Path* p)
|
|
|
|
{
|
2019-03-03 16:01:05 +01:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
2019-03-25 16:41:37 +01:00
|
|
|
const auto sz = p->hops.size();
|
2018-09-14 16:50:37 +02:00
|
|
|
for(const auto& hop : p->hops)
|
|
|
|
{
|
2019-03-25 16:41:37 +01:00
|
|
|
m_Profiles[hop.rc.pubkey].pathSuccessCount += sz;
|
2019-03-04 18:03:18 +01:00
|
|
|
m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms();
|
2018-09-14 16:50:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 18:41:53 +02:00
|
|
|
bool
|
|
|
|
Profiling::Save(const char* fname)
|
|
|
|
{
|
2019-03-03 21:51:47 +01:00
|
|
|
absl::ReaderMutexLock lock(&m_ProfilesMutex);
|
2018-09-13 18:41:53 +02:00
|
|
|
size_t sz = (m_Profiles.size() * (RouterProfile::MaxSize + 32 + 8)) + 8;
|
|
|
|
|
2019-01-17 15:02:50 +01:00
|
|
|
std::vector< byte_t > tmp(sz, 0);
|
2019-02-03 00:12:42 +01:00
|
|
|
llarp_buffer_t buf(tmp);
|
2019-03-03 21:51:47 +01:00
|
|
|
auto res = BEncodeNoLock(&buf);
|
2018-09-13 18:41:53 +02:00
|
|
|
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);
|
2019-03-25 16:41:37 +01:00
|
|
|
m_LastSave = llarp::time_now_ms();
|
2018-09-13 18:41:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Profiling::BEncode(llarp_buffer_t* buf) const
|
2019-03-03 21:51:47 +01:00
|
|
|
{
|
|
|
|
absl::ReaderMutexLock lock(&m_ProfilesMutex);
|
|
|
|
return BEncodeNoLock(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Profiling::BEncodeNoLock(llarp_buffer_t* buf) const
|
2018-09-13 18:41:53 +02:00
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
2019-03-03 21:51:47 +01:00
|
|
|
|
2018-09-13 18:41:53 +02:00
|
|
|
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-03 00:12:42 +01:00
|
|
|
Profiling::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
|
2018-09-13 18:41:53 +02:00
|
|
|
{
|
|
|
|
if(k.sz != 32)
|
|
|
|
return false;
|
|
|
|
RouterProfile profile;
|
|
|
|
if(!profile.BDecode(buf))
|
|
|
|
return false;
|
|
|
|
RouterID pk = k.base;
|
2019-02-18 11:35:16 +01:00
|
|
|
return m_Profiles.emplace(pk, profile).second;
|
2018-09-13 18:41:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Profiling::Load(const char* fname)
|
|
|
|
{
|
2019-03-03 16:01:05 +01:00
|
|
|
lock_t lock(&m_ProfilesMutex);
|
2018-09-13 18:41:53 +02:00
|
|
|
m_Profiles.clear();
|
2018-09-15 13:37:46 +02:00
|
|
|
if(!BDecodeReadFile(fname, *this))
|
|
|
|
{
|
|
|
|
llarp::LogWarn("failed to load router profiles from ", fname);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-12 14:05:43 +02:00
|
|
|
m_LastSave = llarp::time_now_ms();
|
2018-09-15 13:37:46 +02:00
|
|
|
return true;
|
2018-09-13 18:41:53 +02:00
|
|
|
}
|
|
|
|
|
2019-03-25 16:41:37 +01:00
|
|
|
bool
|
|
|
|
Profiling::ShouldSave(llarp_time_t now) const
|
|
|
|
{
|
|
|
|
auto dlt = now - m_LastSave;
|
|
|
|
return dlt > 60000;
|
|
|
|
}
|
2018-09-13 18:41:53 +02:00
|
|
|
} // namespace llarp
|