]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bwlimit.h
Add temporary fix to avoid a deadlock after a reload command on an incorrect configur...
[bacula/bacula] / bacula / src / lib / bwlimit.h
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 #ifndef BWLIMIT_H
21 #define BWLIMIT_H
22
23 class bwlimit: public SMARTALLOC
24 {
25    static const int sample_capacity=10;
26
27 private:
28    int64_t m_bwlimit;           /* set to limit bandwidth */
29    int64_t m_nb_bytes;          /* bytes sent/recv since the last tick */
30    btime_t m_last_tick;         /* last tick used by bwlimit */
31    btime_t m_backlog_limit;     /* don't handle more backlog thna this us */
32    pthread_mutex_t m_bw_mutex;
33
34    int64_t samples_time[sample_capacity];
35    int64_t samples_byte[sample_capacity];
36    int64_t samples_sleep[sample_capacity];
37    int64_t total_time, total_byte, total_sleep;
38    int64_t current_time, current_byte, current_sleep;
39    int64_t current_sample;
40
41    void push_sample(int64_t t, int64_t bytes, int64_t sleep);
42
43 public:
44    bwlimit(int64_t speed=0): m_bwlimit(speed), m_nb_bytes(0), m_last_tick(0),
45          m_backlog_limit(10*1000*1000),
46          total_time(0), total_byte(0), total_sleep(0), current_sample(0)
47    {
48       pthread_mutex_init(&m_bw_mutex, NULL);
49       reset_sample();
50    };
51    ~bwlimit() {
52       pthread_mutex_destroy(&m_bw_mutex);
53    };
54
55    void control_bwlimit(int bytes);
56    void set_bwlimit(int64_t maxspeed) { m_bwlimit = maxspeed; };
57    int64_t get_bwlimit() { return m_bwlimit; };
58    bool use_bwlimit() { return m_bwlimit > 0;};
59    int64_t get_bw();
60    void get_total(int64_t *t, int64_t *bytes, int64_t *sleep);
61    void reset_sample();
62 };
63 #endif