lokinet/llarp/profiling.hpp

133 lines
2.5 KiB
C++
Raw Normal View History

#pragma once
#include "path/path.hpp"
#include "router_id.hpp"
#include "util/bencode.hpp"
#include "util/thread/threading.hpp"
#include <map>
namespace oxenc
{
class bt_dict_consumer;
class bt_dict_producer;
} // namespace oxenc
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;
uint64_t pathTimeoutCount = 0;
llarp_time_t lastUpdated = 0s;
llarp_time_t lastDecay = 0s;
2022-05-26 15:59:44 +00:00
uint64_t version = llarp::constants::proto_version;
RouterProfile() = default;
RouterProfile(oxenc::bt_dict_consumer dict);
void
BEncode(oxenc::bt_dict_producer& dict) const;
void
BEncode(oxenc::bt_dict_producer&& dict) const
{
BEncode(dict);
}
void
BDecode(oxenc::bt_dict_consumer dict);
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();
inline static const int profiling_chances = 4;
2019-04-16 11:44:55 +00:00
/// generic variant
bool
2023-10-25 19:43:32 +00:00
IsBad(const RouterID& r, uint64_t chances = profiling_chances);
2019-05-24 02:01:36 +00:00
/// check if this router should have paths built over it
bool
2023-10-25 19:43:32 +00:00
IsBadForPath(const RouterID& r, uint64_t chances = profiling_chances);
2019-04-16 11:44:55 +00:00
/// check if this router should be connected directly to
bool
2023-10-25 19:43:32 +00:00
IsBadForConnect(const RouterID& r, uint64_t chances = profiling_chances);
2019-04-16 11:44:55 +00:00
void
2023-10-25 19:43:32 +00:00
MarkConnectTimeout(const RouterID& r);
void
2023-10-25 19:43:32 +00:00
MarkConnectSuccess(const RouterID& r);
void
2023-10-25 19:43:32 +00:00
MarkPathTimeout(path::Path* p);
void
2023-10-25 19:43:32 +00:00
MarkPathFail(path::Path* p);
void
2023-10-25 19:43:32 +00:00
MarkPathSuccess(path::Path* p);
void
2023-10-25 19:43:32 +00:00
MarkHopFail(const RouterID& r);
2019-03-31 15:25:13 +00:00
void
2023-10-25 19:43:32 +00:00
ClearProfile(const RouterID& r);
2019-03-31 15:25:13 +00:00
2019-03-04 17:03:18 +00:00
void
2023-10-25 19:43:32 +00:00
Tick();
2019-03-04 17:03:18 +00:00
bool
2023-10-25 19:43:32 +00:00
Load(const fs::path fname);
bool
2023-10-25 19:43:32 +00:00
Save(const fs::path fname);
2019-03-25 15:41:37 +00:00
bool
ShouldSave(llarp_time_t now) const;
void
Disable();
void
Enable();
private:
void
BEncode(oxenc::bt_dict_producer& dict) const;
void
BDecode(oxenc::bt_dict_consumer dict);
mutable util::Mutex m_ProfilesMutex; // protects m_Profiles
2023-10-25 19:43:32 +00:00
std::map<RouterID, RouterProfile> m_Profiles;
2020-02-24 19:40:45 +00:00
llarp_time_t m_LastSave = 0s;
std::atomic<bool> m_DisableProfiling;
};
} // namespace llarp