2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
23 #define ONE_SEC 1000000L /* number of microseconds in a second */
25 void bwlimit::reset_sample()
27 memset(samples_time, '\0', sizeof(samples_time));
28 memset(samples_byte, '\0', sizeof(samples_byte));
29 memset(samples_sleep, '\0', sizeof(samples_sleep));
30 total_time=total_byte=total_sleep=0;
35 void bwlimit::push_sample(int64_t t, int64_t bytes, int64_t sleep)
37 // accumulate data in current sample
41 if (current_time>ONE_SEC) {
42 // we have accumulated enough data for this sample go to the next one
43 // First "pop" the data from the total
44 total_time-=samples_time[current_sample];
45 total_byte-=samples_byte[current_sample];
46 total_sleep-=samples_sleep[current_sample];
48 total_time+=current_time;
49 total_byte+=current_byte;
50 total_sleep+=current_sleep;
51 // record the data in the table
52 samples_time[current_sample]=current_time;
53 samples_byte[current_sample]=current_byte;
54 samples_sleep[current_sample]=current_sleep;
55 // be ready for next sample
59 current_sample=(current_sample+1)%sample_capacity;
63 void bwlimit::get_total(int64_t *t, int64_t *bytes, int64_t *sleep)
65 pthread_mutex_lock(&m_bw_mutex);
66 *t=total_time+current_time;
67 *bytes=total_byte+current_byte;
68 *sleep=total_sleep+current_sleep;
69 pthread_mutex_unlock(&m_bw_mutex);
72 int64_t bwlimit::get_bw()
75 btime_t temp = get_current_btime() - m_last_tick;
79 pthread_mutex_lock(&m_bw_mutex);
80 if (total_time+current_time>0) {
81 bw=(total_byte+current_byte)*ONE_SEC/(total_time+current_time+temp);
83 pthread_mutex_unlock(&m_bw_mutex);
87 void bwlimit::control_bwlimit(int bytes)
90 if (bytes == 0 || m_bwlimit == 0) {
94 lock_guard lg(m_bw_mutex); /* Release the mutex automatically when we quit the function*/
95 now = get_current_btime(); /* microseconds */
96 temp = now - m_last_tick; /* microseconds */
98 if (temp < 0 || temp > m_backlog_limit) { /* Take care of clock problems (>10s) or back in time */
105 /* remove what as been consumed */
108 /* Less than 0.1ms since the last call, see the next time */
110 push_sample(temp, bytes, 0);
114 /* Add what is authorized to be written in temp us */
115 m_nb_bytes += (int64_t)(temp * ((double)m_bwlimit / ONE_SEC));
118 /* limit the backlog */
119 if (m_nb_bytes > m_backlog_limit*m_bwlimit) {
120 m_nb_bytes = m_backlog_limit*m_bwlimit;
121 push_sample(temp, bytes, 0);
122 } else if (m_nb_bytes < 0) {
123 /* What exceed should be converted in sleep time */
124 int64_t usec_sleep = (int64_t)(-m_nb_bytes /((double)m_bwlimit / ONE_SEC));
125 if (usec_sleep > 100) {
126 pthread_mutex_unlock(&m_bw_mutex);
127 bmicrosleep(usec_sleep / ONE_SEC, usec_sleep % ONE_SEC);
128 pthread_mutex_lock(&m_bw_mutex);
130 push_sample(temp, bytes, usec_sleep>100?usec_sleep:0);
131 /* m_nb_bytes & m_last_tick will be updated at next iteration */