From e2fe69945301b02dbac60fb67332a8336f058f13 Mon Sep 17 00:00:00 2001 From: Alain Spineux Date: Fri, 2 Jun 2017 16:26:18 +0200 Subject: [PATCH] core: bwlimit handle backlog and allow burst --- bacula/src/lib/bwlimit.c | 33 +++++++++++++++++---------------- bacula/src/lib/bwlimit.h | 5 ++++- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/bacula/src/lib/bwlimit.c b/bacula/src/lib/bwlimit.c index 167992685c..edabfd6cd1 100644 --- a/bacula/src/lib/bwlimit.c +++ b/bacula/src/lib/bwlimit.c @@ -33,32 +33,33 @@ void bwlimit::control_bwlimit(int bytes) now = get_current_btime(); /* microseconds */ temp = now - m_last_tick; /* microseconds */ - m_nb_bytes += bytes; - if (temp < 0 || temp > (ONE_SEC*10)) { /* Take care of clock problems (>10s) or back in time */ + if (temp < 0 || temp > m_backlog_limit) { /* Take care of clock problems (>10s) or back in time */ m_nb_bytes = bytes; m_last_tick = now; return; } + /* remove what as been consumed */ + m_nb_bytes -= bytes;; + /* Less than 0.1ms since the last call, see the next time */ if (temp < 100) { return; } - /* Remove what was authorised to be written in temp us */ - m_nb_bytes -= (int64_t)(temp * ((double)m_bwlimit / ONE_SEC)); - - if (m_nb_bytes < 0) { - m_nb_bytes = 0; - } + /* Add what is authorized to be written in temp us */ + m_nb_bytes += (int64_t)(temp * ((double)m_bwlimit / ONE_SEC)); + m_last_tick = now; - /* What exceed should be converted in sleep time */ - int64_t usec_sleep = (int64_t)(m_nb_bytes /((double)m_bwlimit / ONE_SEC)); - if (usec_sleep > 100) { - bmicrosleep(usec_sleep / ONE_SEC, usec_sleep % ONE_SEC); - m_last_tick = get_current_btime(); - m_nb_bytes = 0; - } else { - m_last_tick = now; + /* limit the backlog */ + if (m_nb_bytes > m_backlog_limit) { + m_nb_bytes = m_backlog_limit; + } else if (m_nb_bytes < 0) { + /* What exceed should be converted in sleep time */ + int64_t usec_sleep = (int64_t)(-m_nb_bytes /((double)m_bwlimit / ONE_SEC)); + if (usec_sleep > 100) { + bmicrosleep(usec_sleep / ONE_SEC, usec_sleep % ONE_SEC); + } + /* m_nb_bytes & m_last_tick will be updated at next iteration */ } } diff --git a/bacula/src/lib/bwlimit.h b/bacula/src/lib/bwlimit.h index ec492edcaf..a26bbe41d0 100644 --- a/bacula/src/lib/bwlimit.h +++ b/bacula/src/lib/bwlimit.h @@ -26,10 +26,13 @@ private: int64_t m_bwlimit; /* set to limit bandwidth */ int64_t m_nb_bytes; /* bytes sent/recv since the last tick */ btime_t m_last_tick; /* last tick used by bwlimit */ + btime_t m_backlog_limit; /* don't handle more backlog thna this us */ + int64_t m_provision; pthread_mutex_t m_bw_mutex; public: - bwlimit(int64_t speed=0): m_bwlimit(speed), m_nb_bytes(0), m_last_tick(0) { + bwlimit(int64_t speed=0): m_bwlimit(speed), m_nb_bytes(0), m_last_tick(0), + m_backlog_limit(10*1000*1000) { pthread_mutex_init(&m_bw_mutex, NULL); }; ~bwlimit() { -- 2.39.5