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
36
37class XrdSysError;
38class XrdOucTrace;
40
42{
43
44friend class XrdThrottleTimer;
45
46public:
47
48void Init();
49
50void Apply(int reqsize, int reqops, int uid);
51
52bool IsThrottling() {return (m_ops_per_second > 0) || (m_bytes_per_second > 0);}
53
54void 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
58void 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
63static
64int GetUid(const char *username);
65
67
68void PrepLoadShed(const char *opaque, std::string &lsOpaque);
69
70bool CheckLoadShed(const std::string &opaque);
71
72void PerformLoadShed(const std::string &opaque, std::string &host, unsigned &port);
73
75
76 ~XrdThrottleManager() {} // The buffmanager is never deleted
77
78protected:
79
80void StopIOTimer(struct timespec);
81
82private:
83
84void Recompute();
85
87
88static
89void * RecomputeBootstrap(void *pp);
90
92
93void GetShares(int &shares, int &request);
94
95void StealShares(int uid, int &reqsize, int &reqops);
96
99
101
102// Controls for the various rates.
107
108// Maintain the shares
109static const
111std::vector<int> m_primary_bytes_shares;
113std::vector<int> m_primary_ops_shares;
114std::vector<int> m_secondary_ops_shares;
116
117// Active IO counter
119struct timespec m_io_wait;
120// Stable IO counters - must hold m_compute_var lock when reading/writing;
122struct timespec m_stable_io_wait;
123
124// Load shed details
125std::string m_loadshed_host;
129
130static const char *TraceID;
131
132};
133
135{
136
138
139public:
140
142{
143 struct timespec end_timer = {0, 0};
144#if defined(__linux__)
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
175protected:
176
178 m_manager(manager)
179{
180#if defined(__linux__)
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
192private:
194struct timespec m_timer;
195
196static int clock_id;
197};
198
199#endif
#define likely(x)
Definition: XrdThrottleManager.hh:27
#define unlikely(x)
Definition: XrdThrottleManager.hh:28
Definition: XrdOucTrace.hh:36
Definition: XrdSysPthread.hh:79
Definition: XrdSysError.hh:90
Definition: XrdThrottleManager.hh:42
std::vector< int > m_secondary_ops_shares
Definition: XrdThrottleManager.hh:114
~XrdThrottleManager()
Definition: XrdThrottleManager.hh:76
unsigned m_loadshed_frequency
Definition: XrdThrottleManager.hh:127
void SetThrottles(float reqbyterate, float reqoprate, int concurrency, float interval_length)
Definition: XrdThrottleManager.hh:54
bool IsThrottling()
Definition: XrdThrottleManager.hh:52
XrdSysError * m_log
Definition: XrdThrottleManager.hh:98
int m_last_round_allocation
Definition: XrdThrottleManager.hh:115
void Apply(int reqsize, int reqops, int uid)
float m_ops_per_second
Definition: XrdThrottleManager.hh:105
void StopIOTimer(struct timespec)
void StealShares(int uid, int &reqsize, int &reqops)
std::vector< int > m_secondary_bytes_shares
Definition: XrdThrottleManager.hh:112
XrdOucTrace * m_trace
Definition: XrdThrottleManager.hh:97
std::vector< int > m_primary_ops_shares
Definition: XrdThrottleManager.hh:113
void SetLoadShed(std::string &hostname, unsigned port, unsigned frequency)
Definition: XrdThrottleManager.hh:58
XrdSysCondVar m_compute_var
Definition: XrdThrottleManager.hh:100
void PrepLoadShed(const char *opaque, std::string &lsOpaque)
bool CheckLoadShed(const std::string &opaque)
struct timespec m_io_wait
Definition: XrdThrottleManager.hh:119
int m_loadshed_limit_hit
Definition: XrdThrottleManager.hh:128
void GetShares(int &shares, int &request)
int m_io_counter
Definition: XrdThrottleManager.hh:118
XrdThrottleTimer StartIOTimer()
unsigned m_loadshed_port
Definition: XrdThrottleManager.hh:126
int m_concurrency_limit
Definition: XrdThrottleManager.hh:106
float m_interval_length_seconds
Definition: XrdThrottleManager.hh:103
XrdThrottleManager(XrdSysError *lP, XrdOucTrace *tP)
static const char * TraceID
Definition: XrdThrottleManager.hh:130
void PerformLoadShed(const std::string &opaque, std::string &host, unsigned &port)
float m_bytes_per_second
Definition: XrdThrottleManager.hh:104
struct timespec m_stable_io_wait
Definition: XrdThrottleManager.hh:122
std::vector< int > m_primary_bytes_shares
Definition: XrdThrottleManager.hh:111
int m_stable_io_counter
Definition: XrdThrottleManager.hh:121
static int GetUid(const char *username)
std::string m_loadshed_host
Definition: XrdThrottleManager.hh:125
static const int m_max_users
Definition: XrdThrottleManager.hh:110
static void * RecomputeBootstrap(void *pp)
Definition: XrdThrottleManager.hh:135
static int clock_id
Definition: XrdThrottleManager.hh:196
XrdThrottleTimer(XrdThrottleManager &manager)
Definition: XrdThrottleManager.hh:177
XrdThrottleManager & m_manager
Definition: XrdThrottleManager.hh:193
void StopTimer()
Definition: XrdThrottleManager.hh:141
~XrdThrottleTimer()
Definition: XrdThrottleManager.hh:167
struct timespec m_timer
Definition: XrdThrottleManager.hh:194