i2pd/libi2pd/Profiling.cpp

224 lines
6.3 KiB
C++
Raw Normal View History

#include <memory>
2015-03-24 12:47:57 -04:00
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
2015-11-03 09:15:49 -05:00
#include "Base.h"
2016-02-11 00:00:00 +00:00
#include "FS.h"
2015-11-03 09:15:49 -05:00
#include "Log.h"
2015-03-24 12:47:57 -04:00
#include "Profiling.h"
namespace i2p
{
namespace data
{
std::unique_ptr<IdentStorage> m_ProfilesStorage(nullptr);
2016-12-30 20:09:41 -05:00
RouterProfile::RouterProfile ():
m_LastUpdateTime (boost::posix_time::second_clock::local_time()),
2015-06-05 15:55:21 -04:00
m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0), m_NumTunnelsNonReplied (0),
2018-01-06 11:48:51 +08:00
m_NumTimesTaken (0), m_NumTimesRejected (0)
2015-03-24 12:47:57 -04:00
{
}
2015-03-30 21:05:04 -04:00
boost::posix_time::ptime RouterProfile::GetTime () const
{
return boost::posix_time::second_clock::local_time();
2018-01-06 11:48:51 +08:00
}
2015-03-30 21:05:04 -04:00
void RouterProfile::UpdateTime ()
{
m_LastUpdateTime = GetTime ();
2018-01-06 11:48:51 +08:00
}
2016-12-30 20:09:41 -05: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 15:55:21 -04:00
boost::property_tree::ptree usage;
usage.put (PEER_PROFILE_USAGE_TAKEN, m_NumTimesTaken);
usage.put (PEER_PROFILE_USAGE_REJECTED, m_NumTimesRejected);
// fill property tree
boost::property_tree::ptree pt;
2015-03-30 21:05:04 -04:00
pt.put (PEER_PROFILE_LAST_UPDATE_TIME, boost::posix_time::to_simple_string (m_LastUpdateTime));
pt.put_child (PEER_PROFILE_SECTION_PARTICIPATION, participation);
2016-02-11 00:00:00 +00:00
pt.put_child (PEER_PROFILE_SECTION_USAGE, usage);
2015-06-05 15:55:21 -04:00
// save to string stream
2016-12-30 20:09:41 -05:00
std::string ident = identHash.ToBase64 ();
std::ostringstream ss;
2016-02-11 00:00:00 +00:00
try {
boost::property_tree::write_ini (ss, pt);
std::string str = ss.str();
StorageRecord record(str.c_str(), str.length());
m_ProfilesStorage->Store(identHash, record);
2016-02-11 00:00:00 +00:00
} catch (std::exception& ex) {
/* boost exception verbose enough */
LogPrint (eLogError, "Profiling: ", ex.what ());
}
2016-02-11 00:00:00 +00:00
}
2015-03-25 08:45:50 -04:00
2016-12-30 20:09:41 -05:00
void RouterProfile::Load (const IdentHash& identHash)
2015-03-25 08:45:50 -04:00
{
StorageRecord record = m_ProfilesStorage->Fetch(identHash);
2016-02-11 00:00:00 +00:00
if (!record.IsValid())
2016-12-30 20:09:41 -05:00
{
LogPrint(eLogWarning, "Profiling: no profile yet for ", identHash.ToBase64());
2016-02-11 00:00:00 +00:00
return;
}
std::istringstream ss(std::string(record.data.get(), record.data.get() + record.len));
boost::property_tree::ptree pt;
2018-01-06 11:48:51 +08:00
try
2016-12-30 20:09:41 -05:00
{
boost::property_tree::read_ini (ss, pt);
2018-01-06 11:48:51 +08:00
} catch (std::exception& ex)
2016-12-30 20:09:41 -05:00
{
2016-02-11 00:00:00 +00:00
/* boost exception verbose enough */
LogPrint (eLogError, "Profiling: ", ex.what ());
return;
}
2018-01-06 11:48:51 +08:00
try
2016-12-30 20:09:41 -05:00
{
2016-02-11 00:00:00 +00:00
auto t = pt.get (PEER_PROFILE_LAST_UPDATE_TIME, "");
if (t.length () > 0)
m_LastUpdateTime = boost::posix_time::time_from_string (t);
2018-01-06 11:48:51 +08:00
if ((GetTime () - m_LastUpdateTime).hours () < PEER_PROFILE_EXPIRATION_TIMEOUT)
2016-12-30 20:09:41 -05:00
{
2018-01-06 11:48:51 +08:00
try
{
2016-02-11 00:00:00 +00: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 11:48:51 +08:00
}
catch (boost::property_tree::ptree_bad_path& ex)
2016-12-30 20:09:41 -05:00
{
LogPrint (eLogWarning, "Profiling: Missing section ", PEER_PROFILE_SECTION_PARTICIPATION, " in profile for ", identHash.ToBase64());
2018-01-06 11:48:51 +08:00
}
try
2016-12-30 20:09:41 -05:00
{
2016-02-11 00:00:00 +00: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);
2018-01-06 11:48:51 +08:00
}
catch (boost::property_tree::ptree_bad_path& ex)
2016-12-30 20:09:41 -05:00
{
LogPrint (eLogWarning, "Missing section ", PEER_PROFILE_SECTION_USAGE, " in profile for ", identHash.ToBase64());
2016-02-11 00:00:00 +00:00
}
2018-01-06 11:48:51 +08:00
}
else
2016-12-30 20:09:41 -05:00
*this = RouterProfile ();
2018-01-06 11:48:51 +08:00
}
catch (std::exception& ex)
2016-12-30 20:09:41 -05:00
{
LogPrint (eLogError, "Profiling: Can't read profile ", identHash.ToBase64(), " :", ex.what ());
2016-02-11 00:00:00 +00:00
}
}
void RouterProfile::TunnelBuildResponse (uint8_t ret)
{
UpdateTime ();
if (ret > 0)
m_NumTunnelsDeclined++;
else
m_NumTunnelsAgreed++;
2018-01-06 11:48:51 +08:00
}
void RouterProfile::TunnelNonReplied ()
{
m_NumTunnelsNonReplied++;
2015-03-30 21:05:04 -04:00
UpdateTime ();
2018-01-06 11:48:51 +08:00
}
2015-06-05 15:55:21 -04:00
bool RouterProfile::IsLowPartcipationRate () const
{
2015-06-05 15:55:21 -04:00
return 4*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // < 20% rate
2018-01-06 11:48:51 +08:00
}
2015-06-05 15:55:21 -04:00
bool RouterProfile::IsLowReplyRate () const
{
auto total = m_NumTunnelsAgreed + m_NumTunnelsDeclined;
2015-06-05 15:55:21 -04:00
return m_NumTunnelsNonReplied > 10*(total + 1);
2018-01-06 11:48:51 +08:00
}
2015-06-05 15:55:21 -04:00
bool RouterProfile::IsBad ()
2018-01-06 11:48:51 +08:00
{
2015-06-09 14:04:25 -04:00
auto isBad = IsAlwaysDeclining () || IsLowPartcipationRate () /*|| IsLowReplyRate ()*/;
2018-01-06 11:48:51 +08:00
if (isBad && m_NumTimesRejected > 10*(m_NumTimesTaken + 1))
2015-06-05 15:55:21 -04:00
{
2015-06-09 14:04:25 -04:00
// reset profile
m_NumTunnelsAgreed = 0;
m_NumTunnelsDeclined = 0;
m_NumTunnelsNonReplied = 0;
isBad = false;
2018-01-06 11:48:51 +08:00
}
2015-06-05 15:55:21 -04:00
if (isBad) m_NumTimesRejected++; else m_NumTimesTaken++;
2018-01-06 11:48:51 +08:00
return isBad;
}
2018-01-06 11:48:51 +08:00
std::shared_ptr<RouterProfile> GetRouterProfile (const IdentHash& identHash)
2015-03-24 12:47:57 -04:00
{
2016-12-30 20:09:41 -05:00
auto profile = std::make_shared<RouterProfile> ();
profile->Load (identHash); // if possible
2015-03-25 08:45:50 -04:00
return profile;
2018-01-06 11:48:51 +08:00
}
2015-04-11 15:39:23 -04:00
void InitProfilesStorage ()
{
m_ProfilesStorage.reset(new FsIdentStorage("peerProfiles", "p", "profile-", "txt"));
m_ProfilesStorage->Init();
}
2015-04-11 15:39:23 -04:00
void DeleteObsoleteProfiles ()
{
boost::posix_time::ptime now = boost::posix_time::from_time_t(std::time(nullptr));
m_ProfilesStorage->Iterate([now](const IdentHash &ident, const StorageRecord &record) {
std::istringstream ss(std::string(record.data.get(), record.data.get() + record.len));
boost::property_tree::ptree pt;
try
{
boost::property_tree::read_ini (ss, pt);
} catch (std::exception& ex)
{
/* boost exception verbose enough */
LogPrint (eLogError, "Deleting obsolete profile: ", ex.what ());
return;
2016-02-11 00:00:00 +00:00
}
auto t = pt.get (PEER_PROFILE_LAST_UPDATE_TIME, "");
boost::posix_time::ptime lastUpdate = now;
if (t.length () > 0)
lastUpdate = boost::posix_time::time_from_string (t);
if((now - lastUpdate).total_seconds()/ 3600 >= PEER_PROFILE_EXPIRATION_TIMEOUT) {
LogPrint(eLogDebug, "Profiling: removing expired peer profile: ", ident.ToBase64());
m_ProfilesStorage->Remove(ident);
2016-02-11 00:00:00 +00:00
}
});
}
bool BeginProfilesStorageUpdate()
{
return m_ProfilesStorage->BeginUpdate();
}
bool EndProfilesStorageUpdate()
{
return m_ProfilesStorage->EndUpdate();
}
void DeInitProfilesStorage()
{
m_ProfilesStorage->DeInit();
2016-02-11 00:00:00 +00:00
}
2018-01-06 11:48:51 +08:00
}
}