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