]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bwlimit.c
edabfd6cd19965ac4f615da12d3e8a862f214a2b
[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::control_bwlimit(int bytes)
26 {
27    btime_t now, temp;
28    if (bytes == 0 || m_bwlimit == 0) {
29       return;
30    }
31
32    lock_guard lg(m_bw_mutex);     /* Release the mutex automatically when we quit the function*/
33    now = get_current_btime();          /* microseconds */
34    temp = now - m_last_tick;           /* microseconds */
35
36    if (temp < 0 || temp > m_backlog_limit) { /* Take care of clock problems (>10s) or back in time */
37       m_nb_bytes = bytes;
38       m_last_tick = now;
39       return;
40    }
41
42    /* remove what as been consumed */
43    m_nb_bytes -= bytes;;
44
45    /* Less than 0.1ms since the last call, see the next time */
46    if (temp < 100) {
47       return;
48    }
49
50    /* Add what is authorized to be written in temp us */
51    m_nb_bytes += (int64_t)(temp * ((double)m_bwlimit / ONE_SEC));
52    m_last_tick = now;
53
54    /* limit the backlog */
55    if (m_nb_bytes > m_backlog_limit) {
56       m_nb_bytes = m_backlog_limit;
57    } else if (m_nb_bytes < 0) {
58       /* What exceed should be converted in sleep time */
59       int64_t usec_sleep = (int64_t)(-m_nb_bytes /((double)m_bwlimit / ONE_SEC));
60       if (usec_sleep > 100) {
61          bmicrosleep(usec_sleep / ONE_SEC, usec_sleep % ONE_SEC);
62       }
63       /* m_nb_bytes & m_last_tick will be updated at next iteration */
64    }
65 }