Skip to content

Commit

Permalink
Write tunnel failures to file
Browse files Browse the repository at this point in the history
  • Loading branch information
Vort committed Jun 27, 2023
1 parent 07c5291 commit 1981f1e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
63 changes: 62 additions & 1 deletion daemon/i2pd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,64 @@ int main( int argc, char* argv[] )

#ifdef _WIN32
#include <windows.h>
#include <fstream>
#include <thread>
#include <ctime>
#include <mutex>
#include <vector>
#include <iomanip>

std::ofstream failstatfs;
bool timeThreadExiting = false;
time_t lastRequestTime = 0;
const int dumpInterval = 30;
std::shared_ptr<std::thread> timeThread;

namespace i2p
{
extern std::mutex g_FailsMutex;
extern std::vector<std::string> g_Fails;
}

void DumpProfileData()
{
time_t now = std::time(nullptr);
auto pt = std::put_time(std::gmtime(&now), "%Y_%m_%d_%H_%M_%S");
std::unique_lock<std::mutex> l(i2p::g_FailsMutex);
for (int i = 0; i < i2p::g_Fails.size(); i++)
failstatfs << "[" << pt << "]: " << i2p::g_Fails[i] << std::endl;
i2p::g_Fails.clear();
}

void StartProfiling()
{
lastRequestTime = std::time(nullptr);
timeThread = std::make_shared<std::thread>([] {
for (;;)
{
time_t now = std::time(nullptr);
if (now / dumpInterval > lastRequestTime / dumpInterval)
{
lastRequestTime = now;
DumpProfileData();
}
Sleep(250);
if (timeThreadExiting)
return;
}
});

failstatfs.open("failstat.txt", std::ios::app);
}

void StopMemoryProfiling()
{
failstatfs.close();

timeThreadExiting = true;
timeThread->join();
timeThread = nullptr;
}

int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
Expand All @@ -47,6 +105,9 @@ int CALLBACK WinMain(
_In_ int nCmdShow
)
{
return main(__argc, __argv);
StartProfiling();
int r = main(__argc, __argv);
StopMemoryProfiling();
return r;
}
#endif
27 changes: 27 additions & 0 deletions libi2pd/TunnelPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
#include "TunnelPool.h"
#include "Destination.h"

#include <mutex>
#include <vector>

namespace i2p
{
std::mutex g_FailsMutex;
std::vector<std::string> g_Fails;

namespace tunnel
{
void Path::Add (std::shared_ptr<const i2p::data::RouterInfo> r)
Expand Down Expand Up @@ -317,6 +323,25 @@ namespace tunnel
m_LocalDestination->SetLeaseSetUpdated (); // update LeaseSet immediately
}

void LogFailedTunel(Tunnel& tunnel)
{
std::stringstream ss;
std::unique_lock<std::mutex> l(i2p::g_FailsMutex);

bool first = true;

tunnel.VisitTunnelHops(
[&](std::shared_ptr<const i2p::data::IdentityEx> hopIdent)
{
if (!first)
ss << "|";
ss << hopIdent->GetIdentHash().ToBase64();
first = false;
}
);
g_Fails.push_back(ss.str());
}

void TunnelPool::TestTunnels ()
{
decltype(m_Tests) tests;
Expand All @@ -334,6 +359,7 @@ namespace tunnel
if (it.second.first->GetState () == eTunnelStateTestFailed)
{
it.second.first->SetState (eTunnelStateFailed);
LogFailedTunel(*it.second.first);
std::unique_lock<std::mutex> l(m_OutboundTunnelsMutex);
m_OutboundTunnels.erase (it.second.first);
}
Expand All @@ -345,6 +371,7 @@ namespace tunnel
if (it.second.second->GetState () == eTunnelStateTestFailed)
{
it.second.second->SetState (eTunnelStateFailed);
LogFailedTunel(*it.second.second);
{
std::unique_lock<std::mutex> l(m_InboundTunnelsMutex);
m_InboundTunnels.erase (it.second.second);
Expand Down

0 comments on commit 1981f1e

Please sign in to comment.