lokinet/llarp/profiling.hpp

123 lines
2.8 KiB
C++
Raw Normal View History

#ifndef LLARP_PROFILING_HPP
#define LLARP_PROFILING_HPP
2019-01-11 01:19:36 +00:00
#include <path/path.hpp>
#include <router_id.hpp>
#include <util/bencode.hpp>
#include <util/threading.hpp>
#include <absl/base/thread_annotations.h>
#include <map>
namespace llarp
{
2019-05-24 02:01:36 +00:00
struct RouterProfile
{
static constexpr size_t MaxSize = 256;
uint64_t connectTimeoutCount = 0;
uint64_t connectGoodCount = 0;
uint64_t pathSuccessCount = 0;
uint64_t pathFailCount = 0;
2019-03-04 17:03:18 +00:00
llarp_time_t lastUpdated = 0;
2019-04-16 17:30:07 +00:00
llarp_time_t lastDecay = 0;
2019-05-24 02:01:36 +00:00
uint64_t version = LLARP_PROTO_VERSION;
bool
2019-05-24 02:01:36 +00:00
BEncode(llarp_buffer_t* buf) const;
bool
2019-05-24 02:01:36 +00:00
DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf);
bool
IsGood(uint64_t chances) const;
2019-03-04 17:03:18 +00:00
2019-04-16 11:44:55 +00:00
bool
IsGoodForConnect(uint64_t chances) const;
bool
IsGoodForPath(uint64_t chances) const;
/// decay stats
2019-03-04 17:03:18 +00:00
void
Decay();
2019-03-04 17:03:18 +00:00
// rotate stats if timeout reached
void
Tick();
};
2019-05-24 02:01:36 +00:00
struct Profiling
{
Profiling();
2019-04-16 11:44:55 +00:00
/// generic variant
bool
2019-03-22 14:48:53 +00:00
IsBad(const RouterID& r, uint64_t chances = 8)
LOCKS_EXCLUDED(m_ProfilesMutex);
2019-05-24 02:01:36 +00:00
/// check if this router should have paths built over it
bool
IsBadForPath(const RouterID& r, uint64_t chances = 8)
2019-04-16 11:44:55 +00:00
LOCK_RETURNED(m_ProfilesMutex);
/// check if this router should be connected directly to
bool
IsBadForConnect(const RouterID& r, uint64_t chances = 8)
2019-04-16 11:44:55 +00:00
LOCKS_EXCLUDED(m_ProfilesMutex);
void
MarkConnectTimeout(const RouterID& r) LOCKS_EXCLUDED(m_ProfilesMutex);
void
MarkConnectSuccess(const RouterID& r) LOCKS_EXCLUDED(m_ProfilesMutex);
void
MarkPathFail(path::Path* p) LOCKS_EXCLUDED(m_ProfilesMutex);
void
MarkPathSuccess(path::Path* p) LOCKS_EXCLUDED(m_ProfilesMutex);
2019-03-31 15:25:13 +00:00
void
ClearProfile(const RouterID& r) LOCKS_EXCLUDED(m_ProfilesMutex);
2019-03-04 17:03:18 +00:00
void
2019-03-14 00:20:37 +00:00
Tick() LOCKS_EXCLUDED(m_ProfilesMutex);
2019-03-04 17:03:18 +00:00
bool
2019-05-24 02:01:36 +00:00
BEncode(llarp_buffer_t* buf) const LOCKS_EXCLUDED(m_ProfilesMutex);
bool
2019-05-24 02:01:36 +00:00
DecodeKey(const llarp_buffer_t& k,
llarp_buffer_t* buf) NO_THREAD_SAFETY_ANALYSIS;
// disabled because we do load -> bencode::BDecodeReadFromFile -> DecodeKey
bool
Load(const char* fname) LOCKS_EXCLUDED(m_ProfilesMutex);
bool
Save(const char* fname) LOCKS_EXCLUDED(m_ProfilesMutex);
2019-03-25 15:41:37 +00:00
bool
ShouldSave(llarp_time_t now) const;
void
Disable();
void
Enable();
private:
bool
BEncodeNoLock(llarp_buffer_t* buf) const
SHARED_LOCKS_REQUIRED(m_ProfilesMutex);
using lock_t = util::Lock;
mutable util::Mutex m_ProfilesMutex; // protects m_Profiles
std::map< RouterID, RouterProfile > m_Profiles GUARDED_BY(m_ProfilesMutex);
2019-03-25 15:41:37 +00:00
llarp_time_t m_LastSave = 0;
std::atomic< bool > m_DisableProfiling;
};
} // namespace llarp
#endif