xrootd
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 <time.h>
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 
87 
88 static
89 void * RecomputeBootstrap(void *pp);
90 
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__)
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__)
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
XrdThrottleTimer::StopTimer
void StopTimer()
Definition: XrdThrottleManager.hh:141
XrdThrottleTimer::XrdThrottleTimer
XrdThrottleTimer(XrdThrottleManager &manager)
Definition: XrdThrottleManager.hh:177
XrdThrottleManager::m_secondary_ops_shares
std::vector< int > m_secondary_ops_shares
Definition: XrdThrottleManager.hh:114
XrdThrottleManager::m_interval_length_seconds
float m_interval_length_seconds
Definition: XrdThrottleManager.hh:103
XrdThrottleManager::Recompute
void Recompute()
XrdSysPthread.hh
XrdThrottleManager::m_log
XrdSysError * m_log
Definition: XrdThrottleManager.hh:98
XrdThrottleManager
Definition: XrdThrottleManager.hh:42
XrdThrottleManager::PrepLoadShed
void PrepLoadShed(const char *opaque, std::string &lsOpaque)
XrdThrottleManager::m_max_users
static const int m_max_users
Definition: XrdThrottleManager.hh:110
XrdThrottleTimer::clock_id
static int clock_id
Definition: XrdThrottleManager.hh:196
XrdThrottleManager::m_primary_bytes_shares
std::vector< int > m_primary_bytes_shares
Definition: XrdThrottleManager.hh:111
XrdThrottleTimer::~XrdThrottleTimer
~XrdThrottleTimer()
Definition: XrdThrottleManager.hh:167
XrdThrottleManager::m_compute_var
XrdSysCondVar m_compute_var
Definition: XrdThrottleManager.hh:100
XrdThrottleManager::RecomputeBootstrap
static void * RecomputeBootstrap(void *pp)
XrdThrottleManager::CheckLoadShed
bool CheckLoadShed(const std::string &opaque)
XrdThrottleManager::StealShares
void StealShares(int uid, int &reqsize, int &reqops)
XrdThrottleManager::IsThrottling
bool IsThrottling()
Definition: XrdThrottleManager.hh:52
XrdThrottleManager::XrdThrottleManager
XrdThrottleManager(XrdSysError *lP, XrdOucTrace *tP)
XrdThrottleManager::m_loadshed_port
unsigned m_loadshed_port
Definition: XrdThrottleManager.hh:126
XrdSysCondVar
Definition: XrdSysPthread.hh:79
XrdThrottleManager::Init
void Init()
XrdThrottleManager::m_concurrency_limit
int m_concurrency_limit
Definition: XrdThrottleManager.hh:106
XrdThrottleManager::WaitForShares
int WaitForShares()
XrdThrottleTimer
Definition: XrdThrottleManager.hh:135
XrdThrottleManager::m_loadshed_host
std::string m_loadshed_host
Definition: XrdThrottleManager.hh:125
XrdThrottleManager::GetUid
static int GetUid(const char *username)
XrdThrottleManager::GetShares
void GetShares(int &shares, int &request)
XrdThrottleManager::SetLoadShed
void SetLoadShed(std::string &hostname, unsigned port, unsigned frequency)
Definition: XrdThrottleManager.hh:58
XrdThrottleManager::m_loadshed_limit_hit
int m_loadshed_limit_hit
Definition: XrdThrottleManager.hh:128
XrdThrottleManager::m_secondary_bytes_shares
std::vector< int > m_secondary_bytes_shares
Definition: XrdThrottleManager.hh:112
XrdThrottleManager::TraceID
static const char * TraceID
Definition: XrdThrottleManager.hh:130
likely
#define likely(x)
Definition: XrdThrottleManager.hh:27
XrdThrottleManager::m_trace
XrdOucTrace * m_trace
Definition: XrdThrottleManager.hh:97
XrdThrottleManager::m_stable_io_counter
int m_stable_io_counter
Definition: XrdThrottleManager.hh:121
XrdThrottleManager::m_io_counter
int m_io_counter
Definition: XrdThrottleManager.hh:118
XrdThrottleTimer::m_manager
XrdThrottleManager & m_manager
Definition: XrdThrottleManager.hh:193
XrdThrottleManager::m_stable_io_wait
struct timespec m_stable_io_wait
Definition: XrdThrottleManager.hh:122
XrdThrottleManager::Apply
void Apply(int reqsize, int reqops, int uid)
XrdThrottleManager::SetThrottles
void SetThrottles(float reqbyterate, float reqoprate, int concurrency, float interval_length)
Definition: XrdThrottleManager.hh:54
unlikely
#define unlikely(x)
Definition: XrdThrottleManager.hh:28
XrdThrottleManager::StopIOTimer
void StopIOTimer(struct timespec)
XrdThrottleManager::~XrdThrottleManager
~XrdThrottleManager()
Definition: XrdThrottleManager.hh:76
XrdThrottleManager::RecomputeInternal
void RecomputeInternal()
XrdThrottleManager::m_io_wait
struct timespec m_io_wait
Definition: XrdThrottleManager.hh:119
XrdThrottleManager::PerformLoadShed
void PerformLoadShed(const std::string &opaque, std::string &host, unsigned &port)
XrdOucTrace
Definition: XrdOucTrace.hh:36
XrdThrottleManager::m_last_round_allocation
int m_last_round_allocation
Definition: XrdThrottleManager.hh:115
XrdSysError
Definition: XrdSysError.hh:90
XrdThrottleManager::StartIOTimer
XrdThrottleTimer StartIOTimer()
XrdThrottleManager::m_loadshed_frequency
unsigned m_loadshed_frequency
Definition: XrdThrottleManager.hh:127
XrdThrottleManager::m_primary_ops_shares
std::vector< int > m_primary_ops_shares
Definition: XrdThrottleManager.hh:113
XrdThrottleManager::m_bytes_per_second
float m_bytes_per_second
Definition: XrdThrottleManager.hh:104
XrdThrottleTimer::m_timer
struct timespec m_timer
Definition: XrdThrottleManager.hh:194
XrdThrottleManager::m_ops_per_second
float m_ops_per_second
Definition: XrdThrottleManager.hh:105