20 #ifndef __XrdThrottleManager_hh_
21 #define __XrdThrottleManager_hh_
24 #define likely(x) __builtin_expect(!!(x), 1)
25 #define unlikely(x) __builtin_expect(!!(x), 0)
35 #include <unordered_map>
54 bool OpenFile(
const std::string &entity, std::string &open_error_message);
55 bool CloseFile(
const std::string &entity);
57 void Apply(
int reqsize,
int reqops,
int uid);
59 bool IsThrottling() {
return (m_ops_per_second > 0) || (m_bytes_per_second > 0);}
61 void SetThrottles(
float reqbyterate,
float reqoprate,
int concurrency,
float interval_length)
62 {m_interval_length_seconds = interval_length; m_bytes_per_second = reqbyterate;
63 m_ops_per_second = reqoprate; m_concurrency_limit = concurrency;}
65 void SetLoadShed(std::string &hostname,
unsigned port,
unsigned frequency)
66 {m_loadshed_host = hostname; m_loadshed_port = port; m_loadshed_frequency = frequency;}
68 void SetMaxOpen(
unsigned long max_open) {m_max_open = max_open;}
70 void SetMaxConns(
unsigned long max_conns) {m_max_conns = max_conns;}
77 int GetUid(
const char *username);
81 void PrepLoadShed(
const char *opaque, std::string &lsOpaque);
85 void PerformLoadShed(
const std::string &opaque, std::string &host,
unsigned &port);
99 void RecomputeInternal();
102 void * RecomputeBootstrap(
void *pp);
106 void GetShares(
int &shares,
int &request);
108 void StealShares(
int uid,
int &reqsize,
int &reqops);
116 float m_interval_length_seconds;
117 float m_bytes_per_second;
118 float m_ops_per_second;
119 int m_concurrency_limit;
124 std::vector<int> m_primary_bytes_shares;
125 std::vector<int> m_secondary_bytes_shares;
126 std::vector<int> m_primary_ops_shares;
127 std::vector<int> m_secondary_ops_shares;
128 int m_last_round_allocation;
132 struct timespec m_io_wait;
133 unsigned m_io_total{0};
135 int m_stable_io_active;
136 int m_stable_io_total{0};
137 struct timespec m_stable_io_wait;
140 std::string m_loadshed_host;
141 unsigned m_loadshed_port;
142 unsigned m_loadshed_frequency;
143 int m_loadshed_limit_hit;
146 unsigned long m_max_open{0};
147 unsigned long m_max_conns{0};
148 std::unordered_map<std::string, unsigned long> m_file_counters;
149 std::unordered_map<std::string, unsigned long> m_conn_counters;
150 std::unordered_map<std::string, std::unique_ptr<std::unordered_map<pid_t, unsigned long>>> m_active_conns;
151 std::mutex m_file_mutex;
156 static const char *TraceID;
169 struct timespec end_timer = {0, 0};
170 #if defined(__linux__) || defined(__APPLE__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
171 int retval = clock_gettime(clock_id, &end_timer);
177 end_timer.tv_sec -= m_timer.tv_sec;
178 end_timer.tv_nsec -= m_timer.tv_nsec;
179 if (end_timer.tv_nsec < 0)
182 end_timer.tv_nsec += 1000000000;
185 if (m_timer.tv_nsec != -1)
190 m_timer.tv_nsec = -1;
195 if (!((m_timer.tv_sec == 0) && (m_timer.tv_nsec == -1)))
206 #if defined(__linux__) || defined(__APPLE__) || defined(__GNU__) || (defined(__FreeBSD_kernel__) && defined(__GLIBC__))
207 int retval = clock_gettime(clock_id, &m_timer);
220 struct timespec m_timer;
222 static clockid_t clock_id;
void SetThrottles(float reqbyterate, float reqoprate, int concurrency, float interval_length)
void SetMaxOpen(unsigned long max_open)
void Apply(int reqsize, int reqops, int uid)
void StopIOTimer(struct timespec)
void SetLoadShed(std::string &hostname, unsigned port, unsigned frequency)
void SetMonitor(XrdXrootdGStream *gstream)
void PrepLoadShed(const char *opaque, std::string &lsOpaque)
bool CheckLoadShed(const std::string &opaque)
XrdThrottleTimer StartIOTimer()
void SetMaxConns(unsigned long max_conns)
XrdThrottleManager(XrdSysError *lP, XrdOucTrace *tP)
void PerformLoadShed(const std::string &opaque, std::string &host, unsigned &port)
bool CloseFile(const std::string &entity)
bool OpenFile(const std::string &entity, std::string &open_error_message)
static int GetUid(const char *username)
XrdThrottleTimer(XrdThrottleManager &manager)