xrootd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
XrdThrottleManager.hh
Go to the documentation of this file.
1 
2 /*
3  * XrdThrottleManager
4  *
5  * This class provides an implementation of a throttle manager.
6  * The throttled manager purposely pause if the bandwidth, IOPS
7  * rate, or number of outstanding IO requests is sustained above
8  * a certain level.
9  *
10  * The XrdThrottleManager is user-aware and provides fairshare.
11  *
12  * This works by having a separate thread periodically refilling
13  * each user's shares.
14  *
15  * Note that we do not actually keep close track of users, but rather
16  * put them into a hash. This way, we can pretend there's a constant
17  * number of users and use a lock-free algorithm.
18  */
19 
20 #ifndef __XrdThrottleManager_hh_
21 #define __XrdThrottleManager_hh_
22 
23 #ifdef __GNUC__
24 #define likely(x) __builtin_expect(!!(x), 1)
25 #define unlikely(x) __builtin_expect(!!(x), 0)
26 #else
27 #define likely(x) x
28 #define unlikely(x) x
29 #endif
30 
31 #include <string>
32 #include <vector>
33 #include <ctime>
34 
35 #include "XrdSys/XrdSysPthread.hh"
36 
37 class XrdSysError;
38 class XrdOucTrace;
39 class XrdThrottleTimer;
40 
42 {
43 
44 friend class XrdThrottleTimer;
45 
46 public:
47 
48 void Init();
49 
50 void Apply(int reqsize, int reqops, int uid);
51 
52 bool IsThrottling() {return (m_ops_per_second > 0) || (m_bytes_per_second > 0);}
53 
54 void SetThrottles(float reqbyterate, float reqoprate, int concurrency, float interval_length)
55  {m_interval_length_seconds = interval_length; m_bytes_per_second = reqbyterate;
56  m_ops_per_second = reqoprate; m_concurrency_limit = concurrency;}
57 
58 void SetLoadShed(std::string &hostname, unsigned port, unsigned frequency)
59  {m_loadshed_host = hostname; m_loadshed_port = port; m_loadshed_frequency = frequency;}
60 
61 //int Stats(char *buff, int blen, int do_sync=0) {return m_pool.Stats(buff, blen, do_sync);}
62 
63 static
64 int GetUid(const char *username);
65 
67 
68 void PrepLoadShed(const char *opaque, std::string &lsOpaque);
69 
70 bool CheckLoadShed(const std::string &opaque);
71 
72 void PerformLoadShed(const std::string &opaque, std::string &host, unsigned &port);
73 
75 
76  ~XrdThrottleManager() {} // The buffmanager is never deleted
77 
78 protected:
79 
80 void StopIOTimer(struct timespec);
81 
82 private:
83 
84 void Recompute();
85 
86 void RecomputeInternal();
87 
88 static
89 void * RecomputeBootstrap(void *pp);
90 
91 int WaitForShares();
92 
93 void GetShares(int &shares, int &request);
94 
95 void StealShares(int uid, int &reqsize, int &reqops);
96 
99 
101 
102 // Controls for the various rates.
107 
108 // Maintain the shares
109 static const
111 std::vector<int> m_primary_bytes_shares;
112 std::vector<int> m_secondary_bytes_shares;
113 std::vector<int> m_primary_ops_shares;
114 std::vector<int> m_secondary_ops_shares;
116 
117 // Active IO counter
119 struct timespec m_io_wait;
120 // Stable IO counters - must hold m_compute_var lock when reading/writing;
122 struct timespec m_stable_io_wait;
123 
124 // Load shed details
125 std::string m_loadshed_host;
129 
130 static const char *TraceID;
131 
132 };
133 
135 {
136 
137 friend class XrdThrottleManager;
138 
139 public:
140 
141 void StopTimer()
142 {
143  struct timespec end_timer = {0, 0};
144 #if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
145  int retval = clock_gettime(clock_id, &end_timer);
146 #else
147  int retval = -1;
148 #endif
149  if (likely(retval == 0))
150  {
151  end_timer.tv_sec -= m_timer.tv_sec;
152  end_timer.tv_nsec -= m_timer.tv_nsec;
153  if (end_timer.tv_nsec < 0)
154  {
155  end_timer.tv_sec--;
156  end_timer.tv_nsec += 1000000000;
157  }
158  }
159  if (m_timer.tv_nsec != -1)
160  {
161  m_manager.StopIOTimer(end_timer);
162  }
163  m_timer.tv_sec = 0;
164  m_timer.tv_nsec = -1;
165 }
166 
168 {
169  if (!((m_timer.tv_sec == 0) && (m_timer.tv_nsec == -1)))
170  {
171  StopTimer();
172  }
173 }
174 
175 protected:
176 
178  m_manager(manager)
179 {
180 #if defined(__linux__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
181  int retval = clock_gettime(clock_id, &m_timer);
182 #else
183  int retval = -1;
184 #endif
185  if (unlikely(retval == -1))
186  {
187  m_timer.tv_sec = 0;
188  m_timer.tv_nsec = 0;
189  }
190 }
191 
192 private:
194 struct timespec m_timer;
195 
196 static int clock_id;
197 };
198 
199 #endif
~XrdThrottleManager()
Definition: XrdThrottleManager.hh:76
static int clock_id
Definition: XrdThrottleManager.hh:196
void PerformLoadShed(const std::string &opaque, std::string &host, unsigned &port)
Definition: XrdThrottleManager.hh:41
static const int m_max_users
Definition: XrdThrottleManager.hh:110
void PrepLoadShed(const char *opaque, std::string &lsOpaque)
bool CheckLoadShed(const std::string &opaque)
static void * RecomputeBootstrap(void *pp)
float m_ops_per_second
Definition: XrdThrottleManager.hh:105
XrdThrottleManager(XrdSysError *lP, XrdOucTrace *tP)
float m_interval_length_seconds
Definition: XrdThrottleManager.hh:103
void StopTimer()
Definition: XrdThrottleManager.hh:141
std::vector< int > m_secondary_bytes_shares
Definition: XrdThrottleManager.hh:112
Definition: XrdOucTrace.hh:35
Definition: XrdSysError.hh:89
void SetLoadShed(std::string &hostname, unsigned port, unsigned frequency)
Definition: XrdThrottleManager.hh:58
XrdThrottleTimer(XrdThrottleManager &manager)
Definition: XrdThrottleManager.hh:177
Definition: XrdThrottleManager.hh:134
static int GetUid(const char *username)
void GetShares(int &shares, int &request)
XrdThrottleManager & m_manager
Definition: XrdThrottleManager.hh:193
XrdOucTrace * m_trace
Definition: XrdThrottleManager.hh:97
struct timespec m_stable_io_wait
Definition: XrdThrottleManager.hh:122
XrdSysCondVar m_compute_var
Definition: XrdThrottleManager.hh:100
Definition: XrdSysPthread.hh:78
std::vector< int > m_primary_bytes_shares
Definition: XrdThrottleManager.hh:111
bool IsThrottling()
Definition: XrdThrottleManager.hh:52
struct timespec m_timer
Definition: XrdThrottleManager.hh:194
std::string m_loadshed_host
Definition: XrdThrottleManager.hh:125
struct timespec m_io_wait
Definition: XrdThrottleManager.hh:119
unsigned m_loadshed_port
Definition: XrdThrottleManager.hh:126
void SetThrottles(float reqbyterate, float reqoprate, int concurrency, float interval_length)
Definition: XrdThrottleManager.hh:54
std::vector< int > m_primary_ops_shares
Definition: XrdThrottleManager.hh:113
void StealShares(int uid, int &reqsize, int &reqops)
~XrdThrottleTimer()
Definition: XrdThrottleManager.hh:167
int m_last_round_allocation
Definition: XrdThrottleManager.hh:115
int m_concurrency_limit
Definition: XrdThrottleManager.hh:106
unsigned m_loadshed_frequency
Definition: XrdThrottleManager.hh:127
int m_loadshed_limit_hit
Definition: XrdThrottleManager.hh:128
float m_bytes_per_second
Definition: XrdThrottleManager.hh:104
int m_io_counter
Definition: XrdThrottleManager.hh:118
#define likely(x)
Definition: XrdThrottleManager.hh:27
#define unlikely(x)
Definition: XrdThrottleManager.hh:28
int m_stable_io_counter
Definition: XrdThrottleManager.hh:121
std::vector< int > m_secondary_ops_shares
Definition: XrdThrottleManager.hh:114
static const char * TraceID
Definition: XrdThrottleManager.hh:130
void Apply(int reqsize, int reqops, int uid)
XrdSysError * m_log
Definition: XrdThrottleManager.hh:98
void StopIOTimer(struct timespec)
XrdThrottleTimer StartIOTimer()