mirror of https://github.com/oxen-io/lokinet
basic router profiling and fix ip rewrite
parent
76e87aa608
commit
6986f04418
@ -0,0 +1,67 @@
|
||||
#ifndef LLARP_PROFILING_HPP
|
||||
#define LLARP_PROFILING_HPP
|
||||
#include <llarp/bencode.hpp>
|
||||
#include <llarp/threading.hpp>
|
||||
#include <llarp/router_id.hpp>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
struct RouterProfile : public IBEncodeMessage
|
||||
{
|
||||
static constexpr size_t MaxSize = 128;
|
||||
uint64_t connectTimeoutCount = 0;
|
||||
uint64_t connectGoodCount = 0;
|
||||
|
||||
RouterProfile() : IBEncodeMessage(){};
|
||||
~RouterProfile(){};
|
||||
|
||||
bool
|
||||
BEncode(llarp_buffer_t* buf) const;
|
||||
|
||||
bool
|
||||
DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf);
|
||||
|
||||
bool
|
||||
IsGood() const;
|
||||
};
|
||||
|
||||
struct Profiling : public IBEncodeMessage
|
||||
{
|
||||
Profiling() : IBEncodeMessage(){};
|
||||
~Profiling()
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
IsBad(const RouterID& r);
|
||||
|
||||
void
|
||||
MarkSuccess(const RouterID& r);
|
||||
|
||||
void
|
||||
MarkTimeout(const RouterID& r);
|
||||
|
||||
bool
|
||||
BEncode(llarp_buffer_t* buf) const;
|
||||
|
||||
bool
|
||||
DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf);
|
||||
|
||||
bool
|
||||
Load(const char* fname);
|
||||
|
||||
bool
|
||||
Save(const char* fname);
|
||||
|
||||
private:
|
||||
typedef llarp::util::Lock lock_t;
|
||||
typedef llarp::util::Mutex mtx_t;
|
||||
mtx_t m_ProfilesMutex;
|
||||
std::map< RouterID, RouterProfile > m_Profiles;
|
||||
};
|
||||
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -0,0 +1,125 @@
|
||||
#include <llarp/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("t", connectTimeoutCount, buf))
|
||||
return false;
|
||||
if(!BEncodeWriteDictInt("v", version, buf))
|
||||
return false;
|
||||
|
||||
return bencode_end(buf);
|
||||
}
|
||||
|
||||
bool
|
||||
RouterProfile::DecodeKey(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;
|
||||
if(!BEncodeMaybeReadDictInt("v", version, read, k, buf))
|
||||
return false;
|
||||
return read;
|
||||
}
|
||||
|
||||
bool
|
||||
RouterProfile::IsGood() const
|
||||
{
|
||||
return connectTimeoutCount <= connectGoodCount;
|
||||
}
|
||||
|
||||
bool
|
||||
Profiling::IsBad(const RouterID& r)
|
||||
{
|
||||
lock_t lock(m_ProfilesMutex);
|
||||
auto itr = m_Profiles.find(r);
|
||||
if(itr == m_Profiles.end())
|
||||
return false;
|
||||
return !itr->second.IsGood();
|
||||
}
|
||||
|
||||
void
|
||||
Profiling::MarkTimeout(const RouterID& r)
|
||||
{
|
||||
lock_t lock(m_ProfilesMutex);
|
||||
m_Profiles[r].connectTimeoutCount += 1;
|
||||
}
|
||||
|
||||
void
|
||||
Profiling::MarkSuccess(const RouterID& r)
|
||||
{
|
||||
lock_t lock(m_ProfilesMutex);
|
||||
m_Profiles[r].connectGoodCount += 1;
|
||||
}
|
||||
|
||||
bool
|
||||
Profiling::Save(const char* fname)
|
||||
{
|
||||
lock_t lock(m_ProfilesMutex);
|
||||
size_t sz = (m_Profiles.size() * (RouterProfile::MaxSize + 32 + 8)) + 8;
|
||||
|
||||
byte_t* tmp = new byte_t[sz];
|
||||
auto buf = llarp::InitBuffer(tmp, sz);
|
||||
auto res = BEncode(&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);
|
||||
}
|
||||
}
|
||||
delete[] tmp;
|
||||
return res;
|
||||
}
|
||||
|
||||
bool
|
||||
Profiling::BEncode(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
|
||||
Profiling::DecodeKey(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.insert(std::make_pair(pk, profile)).second;
|
||||
}
|
||||
|
||||
bool
|
||||
Profiling::Load(const char* fname)
|
||||
{
|
||||
lock_t lock(m_ProfilesMutex);
|
||||
m_Profiles.clear();
|
||||
return BDecodeReadFile(fname, *this);
|
||||
}
|
||||
|
||||
} // namespace llarp
|
Loading…
Reference in New Issue