lokinet/llarp/profiling.cpp

327 lines
7.8 KiB
C++
Raw Normal View History

#include "profiling.hpp"
#include <fstream>
#include "util/fs.hpp"
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("q", pathTimeoutCount, buf))
return false;
if (!BEncodeWriteDictInt("s", pathFailCount, buf))
return false;
if (!BEncodeWriteDictInt("t", connectTimeoutCount, buf))
return false;
if (!BEncodeWriteDictInt("u", lastUpdated.count(), buf))
2019-03-04 17:03:18 +00:00
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;
if (!BEncodeMaybeReadDictInt("u", lastUpdated, read, k, buf))
2019-03-04 17:03:18 +00:00
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;
if (!BEncodeMaybeReadDictInt("q", pathTimeoutCount, 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;
pathTimeoutCount /= 2;
2019-04-16 17:30:07 +00:00
lastDecay = llarp::time_now_ms();
2019-03-04 17:03:18 +00:00
}
void
RouterProfile::Tick()
{
static constexpr auto updateInterval = 30min;
const auto now = llarp::time_now_ms();
if (lastDecay < now && now - lastDecay > updateInterval)
Decay();
2019-03-04 17:03:18 +00:00
}
bool
RouterProfile::IsGood(uint64_t chances) const
{
if (connectTimeoutCount > chances)
return connectTimeoutCount < connectGoodCount && (pathSuccessCount * chances) > pathFailCount;
2019-04-05 14:58:22 +00:00
return (pathSuccessCount * chances) > pathFailCount;
}
static bool constexpr checkIsGood(uint64_t fails, uint64_t success, uint64_t chances)
2019-04-16 11:44:55 +00:00
{
if (fails > 0 && (fails + success) >= chances)
return (success / fails) > 1;
if (success == 0)
2019-04-16 11:44:55 +00:00
return fails < chances;
return true;
}
bool
RouterProfile::IsGoodForConnect(uint64_t chances) const
{
return checkIsGood(connectTimeoutCount, connectGoodCount, chances);
}
2019-04-16 11:44:55 +00:00
bool
RouterProfile::IsGoodForPath(uint64_t chances) const
{
return checkIsGood(pathFailCount, pathSuccessCount, chances)
and checkIsGood(pathTimeoutCount, pathSuccessCount, chances);
2019-04-16 11:44:55 +00:00
}
2019-05-24 02:01:36 +00:00
Profiling::Profiling() : m_DisableProfiling(false)
Config file improvements (#1397) * Config file API/comment improvements API improvements: ================= Make the config API use position-independent tag parameters (Required, Default{123}, MultiValue) rather than a sequence of bools with overloads. For example, instead of: conf.defineOption<int>("a", "b", false, true, 123, [] { ... }); you now write: conf.defineOption<int>("a", "b", MultiValue, Default{123}, [] { ... }); The tags are: - Required - MultiValue - Default{value} plus new abilities (see below): - Hidden - RelayOnly - ClientOnly - Comment{"line1", "line2", "line3"} Made option definition more powerful: ===================================== - `Hidden` allows you to define an option that won't show up in the generated config file if it isn't set. - `RelayOnly`/`ClientOnly` sets up an option that is only accepted and only shows up for relay or client configs. (If neither is specified the option shows up in both modes). - `Comment{...}` lets the option comments be specified as part of the defineOption. Comment improvements ==================== - Rewrote comments for various options to expand on details. - Inlined all the comments with the option definitions. - Several options that were missing comments got comments added. - Made various options for deprecated and or internal options hidden by default so that they don't show up in a default config file. - show the section comment (but not option comments) *after* the [section] tag instead of before it as it makes more sense that way (particularly for the [bind] section which has a new long comment to describe how it works). Disable profiling by default ============================ We had this weird state where we use and store profiling by default but never *load* it when starting up. This commit makes us just not use profiling at all unless explicitly enabled. Other misc changes: =================== - change default worker threads to 0 (= num cpus) instead of 1, and fix it to allow 0. - Actually apply worker-threads option - fixed default data-dir value erroneously having quotes around it - reordered ifname/ifaddr/mapaddr (was previously mapaddr/ifaddr/ifname) as mapaddr is a sort of specialization of ifaddr and so makes more sense to come after it (particularly because it now references ifaddr in its help message). - removed peer-stats option (since we always require it for relays and never use it for clients) - removed router profiles filename option (this doesn't need to be configurable) - removed defunct `service-node-seed` option - Change default logging output file to "" (which means stdout), and also made "-" work for stdout. * Router hive compilation fixes * Comments for SNApp SRV settings in ini file * Add extra blank line after section comments * Better deprecated option handling Allow {client,relay}-only options in {relay,client} configs to be specified as implicitly deprecated options: they warn, and don't set anything. Add an explicit `Deprecated` tag and move deprecated option handling into definition.cpp. * Move backwards compat options into section definitions Keep the "addBackwardsCompatibleConfigOptions" only for options in sections that no longer exist. * Fix INI parsing issues & C++17-ify - don't allow inline comments because it seems they aren't allowed in ini formats in general, and is going to cause problems if there is a comment character in a value (e.g. an exit auth string). Additionally it was breaking on a line such as: # some comment; see? because it was treating only `; see?` as the comment and then producing an error message about the rest of the line being invalid. - make section parsing stricter: the `[` and `]` have to be at the beginning at end of the line now (after stripping whitespace). - Move whitespace stripping to the top since everything in here does it. - chop off string_view suffix/prefix rather than maintaining position values - fix potential infinite loop/segfault when given a line such as `]foo[` * Make config parsing failure fatal Load() LogError's and returns false on failure, so we weren't aborting on config file errors. * Formatting: allow `{}` for empty functions/structs Instead of using two lines when empty: { } * Make default dns bind 127.0.0.1 on non-Linux * Don't show empty section; fix tests We can conceivably have sections that only make sense for clients or relays, and so want to completely omit that section if we have no options for the type of config being generated. Also fixes missing empty lines between tests. Co-authored-by: Thomas Winget <tewinget@gmail.com>
2020-10-07 22:22:58 +00:00
{}
void
Profiling::Disable()
{
m_DisableProfiling.store(true);
}
void
Profiling::Enable()
{
m_DisableProfiling.store(false);
}
2019-04-16 11:44:55 +00:00
bool
Profiling::IsBadForConnect(const RouterID& r, uint64_t chances)
2019-04-16 11:44:55 +00:00
{
if (m_DisableProfiling.load())
return false;
util::Lock lock{m_ProfilesMutex};
2019-04-16 11:44:55 +00:00
auto itr = m_Profiles.find(r);
if (itr == m_Profiles.end())
2019-04-16 11:44:55 +00:00
return false;
return not itr->second.IsGoodForConnect(chances);
2019-04-16 11:44:55 +00:00
}
bool
Profiling::IsBadForPath(const RouterID& r, uint64_t chances)
2019-04-16 11:44:55 +00:00
{
if (m_DisableProfiling.load())
return false;
util::Lock lock{m_ProfilesMutex};
2019-04-16 11:44:55 +00:00
auto itr = m_Profiles.find(r);
if (itr == m_Profiles.end())
2019-04-16 11:44:55 +00:00
return false;
return not itr->second.IsGoodForPath(chances);
2019-04-16 11:44:55 +00:00
}
bool
Profiling::IsBad(const RouterID& r, uint64_t chances)
{
if (m_DisableProfiling.load())
return false;
util::Lock lock{m_ProfilesMutex};
auto itr = m_Profiles.find(r);
if (itr == m_Profiles.end())
return false;
return not itr->second.IsGood(chances);
}
2019-03-04 17:03:18 +00:00
void
Profiling::Tick()
{
De-abseil, part 2: mutex, locks, (most) time - util::Mutex is now a std::shared_timed_mutex, which is capable of exclusive and shared locks. - util::Lock is still present as a std::lock_guard<util::Mutex>. - the locking annotations are preserved, but updated to the latest supported by clang rather than using abseil's older/deprecated ones. - ACQUIRE_LOCK macro is gone since we don't pass mutexes by pointer into locks anymore (WTF abseil). - ReleasableLock is gone. Instead there are now some llarp::util helper methods to obtain unique and/or shared locks: - `auto lock = util::unique_lock(mutex);` gets an RAII-but-also unlockable object (std::unique_lock<T>, with T inferred from `mutex`). - `auto lock = util::shared_lock(mutex);` gets an RAII shared (i.e. "reader") lock of the mutex. - `auto lock = util::unique_locks(mutex1, mutex2, mutex3);` can be used to atomically lock multiple mutexes at once (returning a tuple of the locks). This are templated on the mutex which makes them a bit more flexible than using a concrete type: they can be used for any type of lockable mutex, not only util::Mutex. (Some of the code here uses them for getting locks around a std::mutex). Until C++17, using the RAII types is painfully verbose: ```C++ // pre-C++17 - needing to figure out the mutex type here is annoying: std::unique_lock<util::Mutex> lock(mutex); // pre-C++17 and even more verbose (but at least the type isn't needed): std::unique_lock<decltype(mutex)> lock(mutex); // our compromise: auto lock = util::unique_lock(mutex); // C++17: std::unique_lock lock(mutex); ``` All of these functions will also warn (under gcc or clang) if you discard the return value. You can also do fancy things like `auto l = util::unique_lock(mutex, std::adopt_lock)` (which lets a lock take over an already-locked mutex). - metrics code is gone, which also removes a big pile of code that was only used by metrics: - llarp::util::Scheduler - llarp::thread::TimerQueue - llarp::util::Stopwatch
2020-02-21 17:21:11 +00:00
util::Lock lock(m_ProfilesMutex);
std::for_each(m_Profiles.begin(), m_Profiles.end(), [](auto& item) { item.second.Tick(); });
2019-03-04 17:03:18 +00:00
}
void
Profiling::MarkConnectTimeout(const RouterID& r)
{
util::Lock 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::MarkConnectSuccess(const RouterID& r)
{
util::Lock lock{m_ProfilesMutex};
m_Profiles[r].connectGoodCount += 1;
2019-03-04 17:03:18 +00:00
m_Profiles[r].lastUpdated = llarp::time_now_ms();
}
2019-03-31 15:25:13 +00:00
void
Profiling::ClearProfile(const RouterID& r)
{
util::Lock lock{m_ProfilesMutex};
2019-03-31 15:25:13 +00:00
m_Profiles.erase(r);
}
void
Profiling::MarkHopFail(const RouterID& r)
{
util::Lock lock{m_ProfilesMutex};
m_Profiles[r].pathFailCount += 1;
m_Profiles[r].lastUpdated = llarp::time_now_ms();
}
void
Profiling::MarkPathFail(path::Path* p)
{
util::Lock lock{m_ProfilesMutex};
size_t idx = 0;
for (const auto& hop : p->hops)
{
// 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;
}
}
void
Profiling::MarkPathTimeout(path::Path* p)
{
util::Lock lock{m_ProfilesMutex};
size_t idx = 0;
for (const auto& hop : p->hops)
{
if (idx)
{
m_Profiles[hop.rc.pubkey].pathTimeoutCount += 1;
m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms();
}
++idx;
}
}
void
Profiling::MarkPathSuccess(path::Path* p)
{
util::Lock lock{m_ProfilesMutex};
2019-03-25 15:41:37 +00:00
const auto sz = p->hops.size();
for (const auto& hop : p->hops)
{
// redeem previous fails by halfing the fail count and setting timeout to zero
m_Profiles[hop.rc.pubkey].pathFailCount /= 2;
m_Profiles[hop.rc.pubkey].pathTimeoutCount = 0;
// mark success at hop
2019-03-25 15:41:37 +00:00
m_Profiles[hop.rc.pubkey].pathSuccessCount += sz;
2019-03-04 17:03:18 +00:00
m_Profiles[hop.rc.pubkey].lastUpdated = llarp::time_now_ms();
}
}
bool
Profiling::Save(const fs::path fpath)
{
const size_t sz = (m_Profiles.size() * (RouterProfile::MaxSize + 32 + 8)) + 8;
std::vector<byte_t> tmp(sz, 0);
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(tmp);
{
util::Lock lock{m_ProfilesMutex};
if (not BEncode(&buf))
2019-06-24 16:39:03 +00:00
return false;
}
buf.sz = buf.cur - buf.base;
auto optional_f = util::OpenFileStream<std::ofstream>(fpath, std::ios::binary);
if (!optional_f)
return false;
auto& f = *optional_f;
if (not f.is_open())
return false;
f.write(reinterpret_cast<const char*>(buf.base), buf.sz);
if (not f.good())
return false;
m_LastSave = llarp::time_now_ms();
return true;
}
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
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 (!bencode_decode_dict(profile, buf))
return false;
RouterID pk = k.base;
return m_Profiles.emplace(pk, profile).second;
}
bool
Profiling::BDecode(llarp_buffer_t* buf)
{
return bencode_decode_dict(*this, buf);
}
bool
Profiling::Load(const fs::path fname)
{
util::Lock lock{m_ProfilesMutex};
m_Profiles.clear();
if (!BDecodeReadFile(fname, *this))
{
llarp::LogWarn("failed to load router profiles from ", fname);
return false;
}
m_LastSave = llarp::time_now_ms();
return true;
}
2019-03-25 15:41:37 +00:00
bool
Profiling::ShouldSave(llarp_time_t now) const
{
auto dlt = now - m_LastSave;
2020-02-24 19:40:45 +00:00
return dlt > 1min;
2019-03-25 15:41:37 +00:00
}
} // namespace llarp