i2pd/libi2pd/Profiling.cpp

362 lines
10 KiB
C++
Raw Normal View History

/*
2024-03-03 15:42:39 +03:00
* Copyright (c) 2013-2024, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2016-02-11 03:00:00 +03:00
#include <sys/stat.h>
2023-02-12 00:22:02 +03:00
#include <unordered_map>
2023-02-14 17:50:32 +03:00
#include <list>
2023-02-14 17:33:10 +03:00
#include <thread>
#include <iomanip>
2015-03-24 19:47:57 +03:00
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
2015-11-03 17:15:49 +03:00
#include "Base.h"
2016-02-11 03:00:00 +03:00
#include "FS.h"
2015-11-03 17:15:49 +03:00
#include "Log.h"
#include "Timestamp.h"
2023-08-26 17:57:05 +03:00
#include "NetDb.hpp"
2015-03-24 19:47:57 +03:00
#include "Profiling.h"
namespace i2p
{
namespace data
{
2023-02-12 00:22:02 +03:00
static i2p::fs::HashedStorage g_ProfilesStorage("peerProfiles", "p", "profile-", "txt");
static std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > g_Profiles;
2023-02-14 17:33:10 +03:00
static std::mutex g_ProfilesMutex;
2016-12-31 04:09:41 +03:00
RouterProfile::RouterProfile ():
m_IsUpdated (false), m_LastDeclineTime (0), m_LastUnreachableTime (0),
m_LastUpdateTime (i2p::util::GetSecondsSinceEpoch ()),
2015-06-05 22:55:21 +03:00
m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0), m_NumTunnelsNonReplied (0),
m_NumTimesTaken (0), m_NumTimesRejected (0), m_HasConnected (false),
m_IsDuplicated (false)
2015-03-24 19:47:57 +03:00
{
}
2015-03-31 04:05:04 +03:00
void RouterProfile::UpdateTime ()
{
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
2023-02-12 00:22:02 +03:00
m_IsUpdated = true;
2018-01-06 06:48:51 +03:00
}
2016-12-31 04:09:41 +03:00
void RouterProfile::Save (const IdentHash& identHash)
{
// fill sections
boost::property_tree::ptree participation;
participation.put (PEER_PROFILE_PARTICIPATION_AGREED, m_NumTunnelsAgreed);
participation.put (PEER_PROFILE_PARTICIPATION_DECLINED, m_NumTunnelsDeclined);
participation.put (PEER_PROFILE_PARTICIPATION_NON_REPLIED, m_NumTunnelsNonReplied);
2015-06-05 22:55:21 +03:00
boost::property_tree::ptree usage;
usage.put (PEER_PROFILE_USAGE_TAKEN, m_NumTimesTaken);
usage.put (PEER_PROFILE_USAGE_REJECTED, m_NumTimesRejected);
2023-05-06 02:58:58 +03:00
usage.put (PEER_PROFILE_USAGE_CONNECTED, m_HasConnected);
if (m_IsDuplicated)
usage.put (PEER_PROFILE_USAGE_DUPLICATED, true);
// fill property tree
boost::property_tree::ptree pt;
pt.put (PEER_PROFILE_LAST_UPDATE_TIMESTAMP, m_LastUpdateTime);
2023-02-06 21:19:41 +03:00
if (m_LastUnreachableTime)
pt.put (PEER_PROFILE_LAST_UNREACHABLE_TIME, m_LastUnreachableTime);
pt.put_child (PEER_PROFILE_SECTION_PARTICIPATION, participation);
2016-02-11 03:00:00 +03:00
pt.put_child (PEER_PROFILE_SECTION_USAGE, usage);
2015-06-05 22:55:21 +03:00
// save to file
2016-12-31 04:09:41 +03:00
std::string ident = identHash.ToBase64 ();
2023-02-12 00:22:02 +03:00
std::string path = g_ProfilesStorage.Path(ident);
2016-02-11 03:00:00 +03:00
try {
boost::property_tree::write_ini (path, pt);
} catch (std::exception& ex) {
/* boost exception verbose enough */
LogPrint (eLogError, "Profiling: ", ex.what ());
}
2016-02-11 03:00:00 +03:00
}
2015-03-25 15:45:50 +03:00
2016-12-31 04:09:41 +03:00
void RouterProfile::Load (const IdentHash& identHash)
2015-03-25 15:45:50 +03:00
{
2016-12-31 04:09:41 +03:00
std::string ident = identHash.ToBase64 ();
2023-02-12 00:22:02 +03:00
std::string path = g_ProfilesStorage.Path(ident);
2016-02-11 03:00:00 +03:00
boost::property_tree::ptree pt;
2018-01-06 06:48:51 +03:00
if (!i2p::fs::Exists(path))
2016-12-31 04:09:41 +03:00
{
LogPrint(eLogWarning, "Profiling: No profile yet for ", ident);
2016-02-11 03:00:00 +03:00
return;
}
2018-01-06 06:48:51 +03:00
try
2016-12-31 04:09:41 +03:00
{
2016-02-11 03:00:00 +03:00
boost::property_tree::read_ini (path, pt);
2018-01-06 06:48:51 +03:00
} catch (std::exception& ex)
2016-12-31 04:09:41 +03:00
{
2016-02-11 03:00:00 +03:00
/* boost exception verbose enough */
LogPrint (eLogError, "Profiling: ", ex.what ());
return;
}
2018-01-06 06:48:51 +03:00
try
2016-12-31 04:09:41 +03:00
{
auto ts = pt.get (PEER_PROFILE_LAST_UPDATE_TIMESTAMP, 0);
if (ts)
m_LastUpdateTime = ts;
else
{
// try old lastupdatetime
auto ut = pt.get (PEER_PROFILE_LAST_UPDATE_TIME, "");
if (ut.length () > 0)
{
std::istringstream ss (ut); std::tm t;
ss >> std::get_time(&t, "%Y-%b-%d %H:%M:%S");
if (!ss.fail())
m_LastUpdateTime = mktime (&t); // t is local time
}
}
if (i2p::util::GetSecondsSinceEpoch () - m_LastUpdateTime < PEER_PROFILE_EXPIRATION_TIMEOUT)
2016-12-31 04:09:41 +03:00
{
2023-02-06 21:19:41 +03:00
m_LastUnreachableTime = pt.get (PEER_PROFILE_LAST_UNREACHABLE_TIME, 0);
2018-01-06 06:48:51 +03:00
try
{
2016-02-11 03:00:00 +03:00
// read participations
auto participations = pt.get_child (PEER_PROFILE_SECTION_PARTICIPATION);
m_NumTunnelsAgreed = participations.get (PEER_PROFILE_PARTICIPATION_AGREED, 0);
m_NumTunnelsDeclined = participations.get (PEER_PROFILE_PARTICIPATION_DECLINED, 0);
m_NumTunnelsNonReplied = participations.get (PEER_PROFILE_PARTICIPATION_NON_REPLIED, 0);
2018-01-06 06:48:51 +03:00
}
catch (boost::property_tree::ptree_bad_path& ex)
2016-12-31 04:09:41 +03:00
{
2016-02-11 03:00:00 +03:00
LogPrint (eLogWarning, "Profiling: Missing section ", PEER_PROFILE_SECTION_PARTICIPATION, " in profile for ", ident);
2018-01-06 06:48:51 +03:00
}
try
2016-12-31 04:09:41 +03:00
{
2016-02-11 03:00:00 +03:00
// read usage
auto usage = pt.get_child (PEER_PROFILE_SECTION_USAGE);
m_NumTimesTaken = usage.get (PEER_PROFILE_USAGE_TAKEN, 0);
m_NumTimesRejected = usage.get (PEER_PROFILE_USAGE_REJECTED, 0);
2023-05-06 02:58:58 +03:00
m_HasConnected = usage.get (PEER_PROFILE_USAGE_CONNECTED, false);
m_IsDuplicated = usage.get (PEER_PROFILE_USAGE_DUPLICATED, false);
2018-01-06 06:48:51 +03:00
}
catch (boost::property_tree::ptree_bad_path& ex)
2016-12-31 04:09:41 +03:00
{
LogPrint (eLogWarning, "Profiling: Missing section ", PEER_PROFILE_SECTION_USAGE, " in profile for ", ident);
2016-02-11 03:00:00 +03:00
}
2018-01-06 06:48:51 +03:00
}
else
2016-12-31 04:09:41 +03:00
*this = RouterProfile ();
2018-01-06 06:48:51 +03:00
}
catch (std::exception& ex)
2016-12-31 04:09:41 +03:00
{
2016-02-11 03:00:00 +03:00
LogPrint (eLogError, "Profiling: Can't read profile ", ident, " :", ex.what ());
}
}
void RouterProfile::TunnelBuildResponse (uint8_t ret)
{
UpdateTime ();
if (ret > 0)
{
m_NumTunnelsDeclined++;
m_LastDeclineTime = i2p::util::GetSecondsSinceEpoch ();
}
else
{
2023-05-06 10:59:40 +03:00
m_NumTunnelsAgreed++;
m_LastDeclineTime = 0;
}
2018-01-06 06:48:51 +03:00
}
void RouterProfile::TunnelNonReplied ()
{
2023-05-06 10:59:40 +03:00
m_NumTunnelsNonReplied++;
2015-03-31 04:05:04 +03:00
UpdateTime ();
if (m_NumTunnelsNonReplied > 2*m_NumTunnelsAgreed && m_NumTunnelsNonReplied > 3)
2023-05-06 00:30:44 +03:00
{
m_LastDeclineTime = i2p::util::GetSecondsSinceEpoch ();
2023-05-06 00:30:44 +03:00
}
2018-01-06 06:48:51 +03:00
}
void RouterProfile::Unreachable (bool unreachable)
2023-02-06 21:19:41 +03:00
{
m_LastUnreachableTime = unreachable ? i2p::util::GetSecondsSinceEpoch () : 0;
2023-02-12 00:22:02 +03:00
UpdateTime ();
}
void RouterProfile::Connected ()
{
m_HasConnected = true;
UpdateTime ();
}
void RouterProfile::Duplicated ()
{
m_IsDuplicated = true;
}
2015-06-05 22:55:21 +03:00
bool RouterProfile::IsLowPartcipationRate () const
{
2015-06-05 22:55:21 +03:00
return 4*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // < 20% rate
2018-01-06 06:48:51 +03:00
}
2015-06-05 22:55:21 +03:00
bool RouterProfile::IsLowReplyRate () const
{
auto total = m_NumTunnelsAgreed + m_NumTunnelsDeclined;
2015-06-05 22:55:21 +03:00
return m_NumTunnelsNonReplied > 10*(total + 1);
2018-01-06 06:48:51 +03:00
}
bool RouterProfile::IsDeclinedRecently ()
{
if (!m_LastDeclineTime) return false;
auto ts = i2p::util::GetSecondsSinceEpoch ();
if (ts > m_LastDeclineTime + PEER_PROFILE_DECLINED_RECENTLY_INTERVAL ||
ts + PEER_PROFILE_DECLINED_RECENTLY_INTERVAL < m_LastDeclineTime)
m_LastDeclineTime = 0;
2023-02-06 21:19:41 +03:00
return (bool)m_LastDeclineTime;
}
2015-06-05 22:55:21 +03:00
bool RouterProfile::IsBad ()
2018-01-06 06:48:51 +03:00
{
if (IsDeclinedRecently () || IsUnreachable () || m_IsDuplicated) return true;
2015-06-09 21:04:25 +03:00
auto isBad = IsAlwaysDeclining () || IsLowPartcipationRate () /*|| IsLowReplyRate ()*/;
2018-01-06 06:48:51 +03:00
if (isBad && m_NumTimesRejected > 10*(m_NumTimesTaken + 1))
2015-06-05 22:55:21 +03:00
{
2015-06-09 21:04:25 +03:00
// reset profile
m_NumTunnelsAgreed = 0;
m_NumTunnelsDeclined = 0;
m_NumTunnelsNonReplied = 0;
isBad = false;
2018-01-06 06:48:51 +03:00
}
2015-06-05 22:55:21 +03:00
if (isBad) m_NumTimesRejected++; else m_NumTimesTaken++;
2018-01-06 06:48:51 +03:00
return isBad;
}
2018-01-06 06:48:51 +03:00
2023-02-06 21:19:41 +03:00
bool RouterProfile::IsUnreachable ()
{
if (!m_LastUnreachableTime) return false;
auto ts = i2p::util::GetSecondsSinceEpoch ();
if (ts > m_LastUnreachableTime + PEER_PROFILE_UNREACHABLE_INTERVAL ||
ts + PEER_PROFILE_UNREACHABLE_INTERVAL < m_LastUnreachableTime)
2023-02-06 21:19:41 +03:00
m_LastUnreachableTime = 0;
return (bool)m_LastUnreachableTime;
}
bool RouterProfile::IsUseful() const
{
return IsReal () || m_NumTunnelsNonReplied >= PEER_PROFILE_USEFUL_THRESHOLD;
2023-05-06 10:59:40 +03:00
}
std::shared_ptr<RouterProfile> GetRouterProfile (const IdentHash& identHash)
2015-03-24 19:47:57 +03:00
{
2023-02-14 17:33:10 +03:00
{
std::unique_lock<std::mutex> l(g_ProfilesMutex);
auto it = g_Profiles.find (identHash);
if (it != g_Profiles.end ())
return it->second;
}
2023-08-26 17:57:05 +03:00
auto profile = netdb.NewRouterProfile ();
2016-12-31 04:09:41 +03:00
profile->Load (identHash); // if possible
2023-02-14 17:33:10 +03:00
std::unique_lock<std::mutex> l(g_ProfilesMutex);
2023-02-12 00:22:02 +03:00
g_Profiles.emplace (identHash, profile);
2015-03-25 15:45:50 +03:00
return profile;
2018-01-06 06:48:51 +03:00
}
2015-04-11 22:39:23 +03:00
2024-03-03 15:42:39 +03:00
bool IsRouterBanned (const IdentHash& identHash)
{
std::unique_lock<std::mutex> l(g_ProfilesMutex);
auto it = g_Profiles.find (identHash);
if (it != g_Profiles.end ())
return it->second->IsUnreachable ();
return false;
}
void InitProfilesStorage ()
{
2023-02-12 00:22:02 +03:00
g_ProfilesStorage.SetPlace(i2p::fs::GetDataDir());
g_ProfilesStorage.Init(i2p::data::GetBase64SubstitutionTable(), 64);
}
static void SaveProfilesToDisk (std::list<std::pair<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > >&& profiles)
{
for (auto& it: profiles)
if (it.second) it.second->Save (it.first);
}
std::future<void> PersistProfiles ()
2023-02-12 00:22:02 +03:00
{
auto ts = i2p::util::GetSecondsSinceEpoch ();
2023-02-14 17:50:32 +03:00
std::list<std::pair<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > > tmp;
{
std::unique_lock<std::mutex> l(g_ProfilesMutex);
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
{
if (ts - it->second->GetLastUpdateTime () > PEER_PROFILE_PERSIST_INTERVAL)
2023-02-14 17:50:32 +03:00
{
if (it->second->IsUpdated ())
tmp.push_back (std::make_pair (it->first, it->second));
it = g_Profiles.erase (it);
}
else
it++;
}
2023-02-14 17:50:32 +03:00
}
if (!tmp.empty ())
return std::async (std::launch::async, SaveProfilesToDisk, std::move (tmp));
return std::future<void>();
}
2023-02-12 00:22:02 +03:00
void SaveProfiles ()
{
2023-02-14 17:50:32 +03:00
std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > tmp;
{
std::unique_lock<std::mutex> l(g_ProfilesMutex);
std::swap (tmp, g_Profiles);
2023-02-14 17:50:32 +03:00
}
auto ts = i2p::util::GetSecondsSinceEpoch ();
2023-02-14 17:50:32 +03:00
for (auto& it: tmp)
if (it.second->IsUseful() && (it.second->IsUpdated () || ts - it.second->GetLastUpdateTime () < PEER_PROFILE_EXPIRATION_TIMEOUT))
2023-02-12 00:22:02 +03:00
it.second->Save (it.first);
}
static void DeleteFilesFromDisk ()
{
std::vector<std::string> files;
g_ProfilesStorage.Traverse(files);
struct stat st;
std::time_t now = std::time(nullptr);
for (const auto& path: files)
{
if (stat(path.c_str(), &st) != 0)
{
LogPrint(eLogWarning, "Profiling: Can't stat(): ", path);
continue;
}
if (now - st.st_mtime >= PEER_PROFILE_EXPIRATION_TIMEOUT)
{
LogPrint(eLogDebug, "Profiling: Removing expired peer profile: ", path);
i2p::fs::Remove(path);
}
}
}
std::future<void> DeleteObsoleteProfiles ()
2015-04-11 22:39:23 +03:00
{
2023-02-14 17:33:10 +03:00
{
auto ts = i2p::util::GetSecondsSinceEpoch ();
2023-02-14 17:33:10 +03:00
std::unique_lock<std::mutex> l(g_ProfilesMutex);
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();)
{
if (ts - it->second->GetLastUpdateTime () >= PEER_PROFILE_EXPIRATION_TIMEOUT)
2023-02-14 17:33:10 +03:00
it = g_Profiles.erase (it);
else
it++;
}
}
return std::async (std::launch::async, DeleteFilesFromDisk);
2016-02-11 03:00:00 +03:00
}
2018-01-06 06:48:51 +03:00
}
}