caches the time every 100 ms and use the cache where high resolution timer is not needed

This commit is contained in:
Arvid Norberg 2009-05-25 02:45:51 +00:00
parent 95c3367b5e
commit 30c704523c
5 changed files with 50 additions and 28 deletions

View File

@ -104,12 +104,7 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_USE_MLOCK 1
#define TORRENT_USE_READV 1
#define TORRENT_USE_WRITEV 1
#ifdef TORRENT_DEBUG
#define TORRENT_USE_IOSTREAM 1
#else
#define TORRENT_USE_IOSTREAM 0
#endif
// should wpath or path be used?
#if defined UNICODE && !defined BOOST_FILESYSTEM_NARROW_ONLY \
@ -146,5 +141,17 @@ POSSIBILITY OF SUCH DAMAGE.
# define TORRENT_WRITE_HANDLER_MAX_SIZE 256
#endif
// determine what timer implementation we can use
#if defined(__MACH__)
#define TORRENT_USE_ABSOLUTE_TIME 1
#elif defined(_WIN32)
#define TORRENT_USE_QUERY_PERFORMANCE_TIMER 1
#elif defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
#define TORRENT_USE_CLOCK_GETTIME 1
#else
#define TORRENT_USE_BOOST_DATE_TIME 1
#endif
#endif // TORRENT_CONFIG_HPP_INCLUDED

View File

@ -55,8 +55,7 @@ namespace libtorrent
std::string log_time();
}
#if (!defined (__MACH__) && !defined (_WIN32) && (!defined(_POSIX_MONOTONIC_CLOCK) \
|| _POSIX_MONOTONIC_CLOCK < 0)) || defined (TORRENT_USE_BOOST_DATE_TIME)
#if defined TORRENT_USE_BOOST_DATE_TIME
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "libtorrent/assert.hpp"
@ -65,7 +64,7 @@ namespace libtorrent
{
typedef boost::posix_time::ptime ptime;
typedef boost::posix_time::time_duration time_duration;
inline ptime time_now()
inline ptime time_now_hires()
{ return boost::posix_time::microsec_clock::universal_time(); }
inline ptime min_time()
{ return boost::posix_time::ptime(boost::posix_time::min_date_time); }
@ -86,7 +85,7 @@ namespace libtorrent
}
#else
#else // TORRENT_USE_BOOST_DATE_TIME
#if BOOST_VERSION < 103500
#include <asio/time_traits.hpp>
@ -158,7 +157,7 @@ namespace libtorrent
inline ptime operator-(ptime lhs, time_duration rhs)
{ return ptime(lhs.time - rhs.diff); }
ptime time_now();
ptime time_now_hires();
inline ptime min_time() { return ptime(0); }
inline ptime max_time() { return ptime((std::numeric_limits<boost::uint64_t>::max)()); }
int total_seconds(time_duration td);
@ -178,7 +177,7 @@ namespace asio
typedef libtorrent::ptime time_type;
typedef libtorrent::time_duration duration_type;
static time_type now()
{ return time_type(libtorrent::time_now()); }
{ return time_type(libtorrent::time_now_hires()); }
static time_type add(time_type t, duration_type d)
{ return time_type(t.time + d.diff);}
static duration_type subtract(time_type t1, time_type t2)
@ -194,7 +193,7 @@ namespace asio
}
#endif
#if defined(__MACH__)
#if defined TORRENT_USE_ABSOLUTE_TIME
#include <mach/mach_time.h>
#include <boost/cstdint.hpp>
@ -217,7 +216,7 @@ namespace libtorrent
return td.diff;
}
inline ptime time_now()
inline ptime time_now_hires()
{
static mach_timebase_info_data_t timebase_info = {0,0};
if (timebase_info.denom == 0)
@ -251,7 +250,7 @@ namespace libtorrent
}
}
#elif defined(_WIN32)
#elif defined TORRENT_USE_QUERY_PERFORMANCE_TIMER
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
@ -307,7 +306,7 @@ namespace libtorrent
return aux::performance_counter_to_microseconds(td.diff);
}
inline ptime time_now()
inline ptime time_now_hires()
{
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
@ -341,7 +340,7 @@ namespace libtorrent
}
#elif defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
#elif defined TORRENT_USE_CLOCK_GETTIME
#include <time.h>
#include "libtorrent/assert.hpp"
@ -361,7 +360,7 @@ namespace libtorrent
return td.diff;
}
inline ptime time_now()
inline ptime time_now_hires()
{
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
@ -391,9 +390,19 @@ namespace libtorrent
}
#endif
#endif // TORRENT_USE_CLOCK_GETTIME
#endif
#endif // TORRENT_USE_BOOST_DATE_TIME
#endif
namespace libtorrent
{
namespace aux
{
extern ptime g_current_time;
}
inline ptime const& time_now() { return aux::g_current_time; }
}
#endif // TORRENT_TIME_HPP_INCLUDED

View File

@ -3086,7 +3086,7 @@ namespace libtorrent
void peer_connection::second_tick(float tick_interval)
{
ptime now(time_now());
ptime now = time_now();
boost::intrusive_ptr<peer_connection> me(self());
// the invariant check must be run before me is destructed

View File

@ -141,16 +141,22 @@ namespace detail
if (user.empty()) return std::string();
return user + ":" + passwd;
}
}
namespace aux {
// used to cache the current time
// every 100 ms. This is cheaper
// than a system call and can be
// used where more accurate time
// is not necessary
ptime g_current_time = time_now_hires();
struct seed_random_generator
{
seed_random_generator()
{
std::srand(total_microseconds(time_now() - min_time()));
std::srand(total_microseconds(time_now_hires() - min_time()));
}
};
@ -199,7 +205,7 @@ namespace aux {
, m_disconnect_time_scaler(90)
, m_auto_scrape_time_scaler(180)
, m_incoming_connection(false)
, m_created(time_now())
, m_created(time_now_hires())
, m_last_tick(m_created)
, m_last_second_tick(m_created)
, m_last_choke(m_created)
@ -1146,6 +1152,8 @@ namespace aux {
{
session_impl::mutex_t::scoped_lock l(m_mutex);
ptime now = time_now_hires();
aux::g_current_time = now;
// too expensive
// INVARIANT_CHECK;
@ -1160,8 +1168,6 @@ namespace aux {
return;
}
ptime now = time_now();
error_code ec;
m_timer.expires_at(now + milliseconds(100), ec);
m_timer.async_wait(bind(&session_impl::on_tick, this, _1));

View File

@ -4575,7 +4575,7 @@ namespace libtorrent
int ret = 0;
ptime now(time_now());
ptime now = time_now();
int seed_time = total_seconds(m_seeding_time);
int download_time = total_seconds(m_active_time) - seed_time;