XRootD
Loading...
Searching...
No Matches
XrdPfcResourceMonitor.hh
Go to the documentation of this file.
1#ifndef __XRDPFC_RESOURCEMONITOR_HH__
2#define __XRDPFC_RESOURCEMONITOR_HH__
3
4#include "XrdPfcStats.hh"
6
8
9#include <chrono>
10#include <string>
11#include <vector>
12#include <list>
13
14class XrdOss;
15
16namespace XrdPfc {
17
18struct DataFsState;
19struct DirState;
20struct DirStateElement;
21struct DataFsSnapshot;
22struct DirPurgeElement;
23struct DataFsPurgeshot;
24class FsTraversal;
25
26//==============================================================================
27// ResourceMonitor
28//==============================================================================
29
30// Encapsulates local variables used withing the previous mega-function Purge().
31//
32// This will be used within the continuously/periodically ran heart-beat / breath
33// function ... and then parts of it will be passed to invoked FS scan and purge
34// jobs (which will be controlled throught this as well).
35
36// Andy: XRDADMINPATH Is the directory for administrative files (i.e. all.adminpath)
37// Also: XrdOucEnv::Export("XRDLOGDIR", logParms.logfn); (in XrdOucLogging::configLog)
38
40{
41 template<typename ID, typename RECORD>
42 class Queue {
43 public:
44 struct Entry {
46 RECORD record;
47 };
48 using queue_type = std::vector<Entry>;
49 using iterator = typename queue_type::iterator;
50
51 Queue() = default;
52
53 int write_queue_size() const { return m_write_queue.size(); }
54 bool read_queue_empty() const { return m_read_queue.empty(); }
55 int read_queue_size() const { return m_read_queue.size(); }
56
57 // Writer / producer access
58 void push(ID id, RECORD stat) { m_write_queue.push_back({ id, stat }); }
59 // Existing entry access for updating Stats
60 RECORD& write_record(int pos) { return m_write_queue[pos].record; }
61
62 // Reader / consumer access
63 int swap_queues() { m_read_queue.clear(); m_write_queue.swap(m_read_queue); return read_queue_size(); }
64 const queue_type& read_queue() const { return m_read_queue; }
65 iterator begin() const { return m_read_queue.begin(); }
66 iterator end() const { return m_read_queue.end(); }
67
68 // Shrinkage of overgrown queues
69 void shrink_read_queue() { m_read_queue.clear(); m_read_queue.shrink_to_fit(); }
70
71 private:
72 queue_type m_write_queue, m_read_queue;
73 };
74
75 struct AccessToken {
76 std::string m_filename;
77 unsigned int m_last_queue_swap_u1 = 0xffffffff;
78 int m_last_write_queue_pos = -1;
79 DirState *m_dir_state = nullptr;
80
81 void clear() {
82 m_filename.clear();
83 m_last_queue_swap_u1 = 0xffffffff;
84 m_last_write_queue_pos = -1;
85 m_dir_state = nullptr;
86 }
87 };
88 // Stable addresses: process_queues() uses AccessToken& without m_queue_mutex.
89 // 4096 * 1024 tokens, one per open file. acquire() and release() run under
90 // m_queue_mutex, in the same critical section as the queue push.
91 StableChunkPool<AccessToken, 12, 1024> m_access_tokens;
92
93 struct OpenRecord {
94 time_t m_open_time;
95 bool m_existing_file;
96 };
97
98 struct CloseRecord {
99 time_t m_close_time;
100 Stats m_full_stats;
101 };
102
103 struct PurgeRecord {
104 long long m_size_in_st_blocks;
105 int m_n_files;
106 };
107
108 Queue<int, OpenRecord> m_file_open_q;
109 Queue<int, Stats> m_file_update_stats_q;
110 Queue<int, CloseRecord> m_file_close_q;
111 Queue<DirState*, PurgeRecord> m_file_purge_q1;
112 Queue<std::string, PurgeRecord> m_file_purge_q2;
113 Queue<std::string, long long> m_file_purge_q3;
114 // DirPurge queue -- not needed? But we do need last-change timestamp in DirState.
115
116 long long m_current_usage_in_st_blocks = 0; // aggregate disk usage by files
117 int m_dir_count_discrepancy = 0; // last reported usage-vs-tree mismatch
118 long long m_token_alloc_failures = 0; // token space exhausted, under m_queue_mutex
119
120 XrdSysMutex m_queue_mutex; // mutex shared between queues
121 unsigned int m_queue_swap_u1 = 0u; // identifier of current swap cycle
122
123 DataFsState &m_fs_state;
124 XrdOss &m_oss;
125
126 // Requests for File opens during name-space scans. Such LFNs are processed
127 // with some priority
128 struct LfnCondRecord
129 {
130 const std::string &f_lfn;
131 XrdSysCondVar &f_cond;
132 bool &f_checked;
133 };
134
135 XrdSysMutex m_dir_scan_mutex;
136 std::list<LfnCondRecord> m_dir_scan_open_requests;
137 bool m_dir_scan_in_progress = true;
138
139 // Open requests are serviced from the scan thread at most this often; an open
140 // waits for roughly this long (plus the time to scan its own directory).
141 static constexpr std::chrono::milliseconds s_dir_scan_check_interval{100};
142 std::chrono::steady_clock::time_point m_dir_scan_last_check;
143
144 void check_inter_dir_scan_open_requests();
145 void process_inter_dir_scan_open_requests();
146 void cross_check_or_process_oob_lfn(const std::string &lfn, FsTraversal &fst);
147 long long get_file_usage_bytes_to_remove(const DataFsPurgeshot &ps, long long previous_file_usage, int logLeve);
148
149public:
150 ResourceMonitor(XrdOss& oss);
152
153 // --- Initial scan, building of DirState tree
154
155 void scan_dir_and_recurse(FsTraversal &fst);
157
158 // --- Event registration
159
160 int register_file_open(const std::string& filename, time_t open_timestamp, bool existing_file) {
161 // Simply return a token, we will resolve it in the actual processing of the queue.
162 XrdSysMutexHelper _lock(&m_queue_mutex);
163 int token_id = m_access_tokens.acquire();
164 if (token_id < 0) {
165 // Unreachable in practice; heart_beat() reports the count.
166 ++m_token_alloc_failures;
167 return -1;
168 }
169 AccessToken &at = m_access_tokens[token_id];
170 at.m_filename = filename;
171 at.m_last_queue_swap_u1 = m_queue_swap_u1 - 1;
172
173 m_file_open_q.push(token_id, {open_timestamp, existing_file});
174 return token_id;
175 }
176
177 void register_file_update_stats(int token_id, const Stats& stats) {
178 if (token_id < 0) return; // register_file_open() could not get a token
179 XrdSysMutexHelper _lock(&m_queue_mutex);
180 AccessToken &at = token(token_id);
181 // Check if this is the first update within this queue swap cycle.
182 if (at.m_last_queue_swap_u1 != m_queue_swap_u1) {
183 m_file_update_stats_q.push(token_id, stats);
184 at.m_last_queue_swap_u1 = m_queue_swap_u1;
185 at.m_last_write_queue_pos = m_file_update_stats_q.write_queue_size() - 1;
186 } else {
187 Stats &existing_stats = m_file_update_stats_q.write_record(at.m_last_write_queue_pos);
188 existing_stats.AddUp(stats);
189 }
190 // Optionally, one could return "scaler" to moodify stat-reporting
191 // frequency in the file ... if it comes too often or too rarely.
192 // See also the logic for determining reporting interval (in N_bytes_read)
193 // in File::Open().
194 }
195
196 void register_file_close(int token_id, time_t close_timestamp, const Stats& full_stats) {
197 if (token_id < 0) return; // register_file_open() could not get a token
198 XrdSysMutexHelper _lock(&m_queue_mutex);
199 m_file_close_q.push(token_id, {close_timestamp, full_stats});
200 }
201
202 // deletions can come from purge and from direct requests (Cache::UnlinkFile), the latter
203 // also covering the emergency shutdown of a file.
204 void register_file_purge(DirState* target, long long size_in_st_blocks) {
205 XrdSysMutexHelper _lock(&m_queue_mutex);
206 m_file_purge_q1.push(target, {size_in_st_blocks, 1});
207 }
208 void register_multi_file_purge(DirState* target, long long size_in_st_blocks, int n_files) {
209 XrdSysMutexHelper _lock(&m_queue_mutex);
210 m_file_purge_q1.push(target, {size_in_st_blocks, n_files});
211 }
212 void register_multi_file_purge(const std::string& target, long long size_in_st_blocks, int n_files) {
213 XrdSysMutexHelper _lock(&m_queue_mutex);
214 m_file_purge_q2.push(target, {size_in_st_blocks, n_files});
215 }
216 void register_file_purge(const std::string& filename, long long size_in_st_blocks) {
217 XrdSysMutexHelper _lock(&m_queue_mutex);
218 m_file_purge_q3.push(filename, size_in_st_blocks);
219 }
220
221 // void register_dir_purge(DirState* target);
222 // target assumed to be empty at this point, triggered by a file_purge removing the last file in it.
223 // hmmh, this is actually tricky ... who will purge the dirs? we should now at export-to-vector time
224 // and can prune leaf directories. This might fail if a file has been created in there in the meantime, which is ok.
225 // However, is there a race condition between rmdir and creation of a new file in that dir? Ask Andy.
226
227 // --- Helpers for event processing and actions
228
229 AccessToken& token(int i) { return m_access_tokens[i]; }
230
231 // --- Actions
232
233 int process_queues();
234
235 void heart_beat();
236
237 // --- Helpers for export of DirState vector snapshot.
238
239 void fill_sshot_vec_children(const DirState &parent_ds,
240 int parent_idx,
241 std::vector<DirStateElement> &vec,
242 int max_depth);
243
244 void fill_pshot_vec_children(const DirState &parent_ds,
245 int parent_idx,
246 std::vector<DirPurgeElement> &vec,
247 int max_depth);
248
249 // Interface to other part of XCache -- note the CamelCase() notation.
250 void CrossCheckIfScanIsInProgress(const std::string &lfn, XrdSysCondVar &cond);
251
252 // main function, steers startup then enters heart_beat. does not die.
253 void init_before_main(); // called from startup thread / configuration processing
254 void main_thread_function(); // run in dedicated thread
255
257 // The following variables are set under the above lock, purge task signals to heart_beat.
260 bool m_purge_task_active {false}; // from the perspective of heart-beat, set only in heartbeat
261 bool m_purge_task_complete {false}; // from the perspective of the task, reset in heartbeat, set in task
262 // When m_purge_task_active == true, DirState entries are not removed from the tree to
263 // allow purge thread to report cleared files directly via DirState ptr.
264 // Note, DirState removal happens during stat propagation traversal.
265
266 // Purge helpers etc.
268 void perform_purge_check(bool purge_cold_files, int tl);
269
272};
273
274}
275
276#endif
#define stat(a, b)
Definition XrdPosix.hh:101
#define ID
void CrossCheckIfScanIsInProgress(const std::string &lfn, XrdSysCondVar &cond)
int register_file_open(const std::string &filename, time_t open_timestamp, bool existing_file)
void perform_purge_check(bool purge_cold_files, int tl)
void fill_sshot_vec_children(const DirState &parent_ds, int parent_idx, std::vector< DirStateElement > &vec, int max_depth)
void perform_purge_task(DataFsPurgeshot &ps)
void register_file_purge(const std::string &filename, long long size_in_st_blocks)
void scan_dir_and_recurse(FsTraversal &fst)
void register_file_purge(DirState *target, long long size_in_st_blocks)
void fill_pshot_vec_children(const DirState &parent_ds, int parent_idx, std::vector< DirPurgeElement > &vec, int max_depth)
void register_multi_file_purge(DirState *target, long long size_in_st_blocks, int n_files)
void register_file_update_stats(int token_id, const Stats &stats)
void register_multi_file_purge(const std::string &target, long long size_in_st_blocks, int n_files)
void register_file_close(int token_id, time_t close_timestamp, const Stats &full_stats)
Statistics of cache utilisation by a File object.
void AddUp(const Stats &s)
XrdPosixStats Stats