Merge pull request #4949

5464725a protocol: change standby mode to not wait sleeping (moneromooo-monero)
85807dfb add a once_a_time_milliseconds class (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2019-01-06 20:37:51 +02:00
commit 13b006137c
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
3 changed files with 54 additions and 21 deletions

View File

@ -230,35 +230,56 @@ namespace math_helper
}
}
template<int default_interval, bool start_immediate = true>
class once_a_time_seconds
template<uint64_t scale, int default_interval, bool start_immediate = true>
class once_a_time
{
uint64_t get_time() const
{
#ifdef _WIN32
FILETIME fileTime;
GetSystemTimeAsFileTime(&fileTime);
unsigned __int64 present = 0;
present |= fileTime.dwHighDateTime;
present = present << 32;
present |= fileTime.dwLowDateTime;
present /= 10; // mic-sec
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
#endif
}
public:
once_a_time_seconds():m_interval(default_interval)
once_a_time():m_interval(default_interval * scale)
{
m_last_worked_time = 0;
if(!start_immediate)
time(&m_last_worked_time);
m_last_worked_time = get_time();
}
template<class functor_t>
bool do_call(functor_t functr)
{
time_t current_time = 0;
time(&current_time);
uint64_t current_time = get_time();
if(current_time - m_last_worked_time > m_interval)
{
bool res = functr();
time(&m_last_worked_time);
m_last_worked_time = get_time();
return res;
}
return true;
}
private:
time_t m_last_worked_time;
time_t m_interval;
uint64_t m_last_worked_time;
uint64_t m_interval;
};
template<int default_interval, bool start_immediate = true>
class once_a_time_seconds: public once_a_time<1000000, default_interval, start_immediate> {};
template<int default_interval, bool start_immediate = true>
class once_a_time_milliseconds: public once_a_time<1000, default_interval, start_immediate> {};
}
}

View File

@ -131,6 +131,7 @@ namespace cryptonote
bool should_download_next_span(cryptonote_connection_context& context) const;
void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
bool kick_idle_peers();
bool check_standby_peers();
int try_add_next_blocks(cryptonote_connection_context &context);
t_core& m_core;
@ -143,6 +144,7 @@ namespace cryptonote
boost::mutex m_sync_lock;
block_queue m_block_queue;
epee::math_helper::once_a_time_seconds<30> m_idle_peer_kicker;
epee::math_helper::once_a_time_milliseconds<100> m_standby_checker;
boost::mutex m_buffer_mutex;
double get_avg_block_size();

View File

@ -1210,6 +1210,7 @@ skip:
bool t_cryptonote_protocol_handler<t_core>::on_idle()
{
m_idle_peer_kicker.do_call(boost::bind(&t_cryptonote_protocol_handler<t_core>::kick_idle_peers, this));
m_standby_checker.do_call(boost::bind(&t_cryptonote_protocol_handler<t_core>::check_standby_peers, this));
return m_core.on_idle();
}
//------------------------------------------------------------------------------------------------------------------------
@ -1245,6 +1246,22 @@ skip:
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
bool t_cryptonote_protocol_handler<t_core>::check_standby_peers()
{
m_p2p->for_each_connection([&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags)->bool
{
if (context.m_state == cryptonote_connection_context::state_standby)
{
LOG_PRINT_CCONTEXT_L2("requesting callback");
++context.m_callback_request_count;
m_p2p->request_callback(context);
}
return true;
});
return true;
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
int t_cryptonote_protocol_handler<t_core>::handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context)
{
MLOG_P2P_MESSAGE("Received NOTIFY_REQUEST_CHAIN (" << arg.block_ids.size() << " blocks");
@ -1338,14 +1355,13 @@ skip:
bool start_from_current_chain = false;
if (!force_next_span)
{
bool first = true;
while (1)
do
{
size_t nblocks = m_block_queue.get_num_filled_spans();
size_t size = m_block_queue.get_data_size();
if (nblocks < BLOCK_QUEUE_NBLOCKS_THRESHOLD || size < BLOCK_QUEUE_SIZE_THRESHOLD)
{
if (!first)
if (context.m_state != cryptonote_connection_context::state_standby)
{
LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", resuming");
}
@ -1368,10 +1384,9 @@ skip:
break;
}
if (first)
if (context.m_state != cryptonote_connection_context::state_standby)
{
LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", pausing");
first = false;
context.m_state = cryptonote_connection_context::state_standby;
}
@ -1385,13 +1400,8 @@ skip:
return true;
}
for (size_t n = 0; n < 50; ++n)
{
if (m_stopping)
return true;
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
}
return true;
} while(0);
context.m_state = cryptonote_connection_context::state_synchronizing;
}