]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bwlimit.c
Make out of freespace non-fatal for removable devices -- i.e. behaves like tape
[bacula/bacula] / bacula / src / lib / bwlimit.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
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.
8
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.
13
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19
20 #include "bacula.h"
21 #include "bwlimit.h"
22
23 #define ONE_SEC 1000000L /* number of microseconds in a second */
24
25 void bwlimit::reset_sample()
26 {
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;
31    current_sample=0;
32    current_byte=0;
33 }
34
35 void bwlimit::push_sample(int64_t t, int64_t bytes, int64_t sleep)
36 {
37    // accumulate data in current sample
38    current_time+=t;
39    current_byte+=bytes;
40    current_sleep+=sleep;
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];
47       // Push the new one
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
56       current_time=0;
57       current_byte=0;
58       current_sleep=0;
59       current_sample=(current_sample+1)%sample_capacity;
60    }
61 }
62
63 void bwlimit::get_total(int64_t *t, int64_t *bytes, int64_t *sleep)
64 {
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);
70 }
71
72 int64_t bwlimit::get_bw()
73 {
74    int64_t bw = 0;
75    btime_t temp = get_current_btime() - m_last_tick;
76    if (temp < 0) {
77       temp = 0;
78    }
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);
82    }
83    pthread_mutex_unlock(&m_bw_mutex);
84    return bw;
85 }
86
87 void bwlimit::control_bwlimit(int bytes)
88 {
89    btime_t now, temp;
90    if (bytes == 0 || m_bwlimit == 0) {
91       return;
92    }
93
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 */
97
98    if (temp < 0 || temp > m_backlog_limit) { /* Take care of clock problems (>10s) or back in time */
99       m_nb_bytes = bytes;
100       m_last_tick = now;
101       reset_sample();
102       return;
103    }
104
105    /* remove what as been consumed */
106    m_nb_bytes -= bytes;
107
108    /* Less than 0.1ms since the last call, see the next time */
109    if (temp < 100) {
110       push_sample(temp, bytes, 0);
111       return;
112    }
113
114    /* Add what is authorized to be written in temp us */
115    m_nb_bytes += (int64_t)(temp * ((double)m_bwlimit / ONE_SEC));
116    m_last_tick = now;
117
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);
129       }
130       push_sample(temp, bytes, usec_sleep>100?usec_sleep:0);
131       /* m_nb_bytes & m_last_tick will be updated at next iteration */
132    }
133 }