XRootD
XrdPfcResourceMonitor.cc
Go to the documentation of this file.
2 #include "XrdPfc.hh"
4 #include "XrdPfcFsTraversal.hh"
5 #include "XrdPfcDirState.hh"
8 #include "XrdPfcTrace.hh"
9 #include "XrdPfcPurgePin.hh"
10 
11 #include "XrdOss/XrdOss.hh"
12 
13 #include <algorithm>
14 
15 // #define RM_DEBUG
16 #ifdef RM_DEBUG
17 #define dprintf(...) printf(__VA_ARGS__)
18 #else
19 #define dprintf(...) (void(0))
20 #endif
21 
22 using namespace XrdPfc;
23 
24 namespace
25 {
26  XrdSysTrace* GetTrace() { return Cache::GetInstance().GetTrace(); }
27  const char *m_traceID = "ResourceMonitor";
28 }
29 
30 //------------------------------------------------------------------------------
31 
33  m_fs_state(* new DataFsState),
34  m_oss(oss)
35 {}
36 
38 {
39  delete &m_fs_state;
40 }
41 
42 //------------------------------------------------------------------------------
43 // Initial scan
44 //------------------------------------------------------------------------------
45 
47 {
48  m_dir_scan_mutex.Lock();
49  if (m_dir_scan_in_progress) {
50  bool dir_checked = false;
51  m_dir_scan_open_requests.push_back({lfn, cond, dir_checked});
52  cond.Lock();
53  m_dir_scan_mutex.UnLock();
54  while ( ! dir_checked)
55  cond.Wait();
56  cond.UnLock();
57  } else {
58  m_dir_scan_mutex.UnLock();
59  }
60 }
61 
62 void ResourceMonitor::process_inter_dir_scan_open_requests(FsTraversal &fst)
63 {
64  m_dir_scan_mutex.Lock();
65  while ( ! m_dir_scan_open_requests.empty())
66  {
67  LfnCondRecord &lcr = m_dir_scan_open_requests.front();
68  m_dir_scan_mutex.UnLock();
69 
70  cross_check_or_process_oob_lfn(lcr.f_lfn, fst);
71  lcr.f_cond.Lock();
72  lcr.f_checked = true;
73  lcr.f_cond.Signal();
74  lcr.f_cond.UnLock();
75 
76  m_dir_scan_mutex.Lock();
77  m_dir_scan_open_requests.pop_front();
78  }
79  m_dir_scan_mutex.UnLock();
80 }
81 
82 void ResourceMonitor::cross_check_or_process_oob_lfn(const std::string &lfn, FsTraversal &fst)
83 {
84  // Check if lfn has already been processed ... or process it now and mark
85  // the DirState accordingly (partially processed oob).
86  static const char *trc_pfx = "cross_check_or_process_oob_lfn() ";
87 
88  // last-existing-dir-state is currently not used. It was expected some work would need
89  // to be done along the path ... but we know they are all empty as they were just
90  // created. We could tag them all as already scanned.
91  DirState *last_existing_ds = nullptr;
92  DirState *ds = m_fs_state.find_dirstate_for_lfn(lfn, &last_existing_ds);
93  if (ds->m_scanned)
94  return;
95 
96  size_t pos = lfn.find_last_of("/");
97  std::string dir = (pos == std::string::npos) ? "" : lfn.substr(0, pos);
98 
99  XrdOssDF *dhp = m_oss.newDir(trc_pfx);
100  bool opened = (dhp->Opendir(dir.c_str(), fst.default_env()) == XrdOssOK);
101  if (opened)
102  {
103  fst.slurp_dir_ll(*dhp, ds->m_depth, dir.c_str(), trc_pfx);
104 
105  // XXXX clone of function below .... move somewhere? Esp. removal of non-paired files?
106  DirUsage &here = ds->m_here_usage;
107  for (auto it = fst.m_current_files.begin(); it != fst.m_current_files.end(); ++it)
108  {
109  if (it->second.has_data && it->second.has_cinfo) {
110  here.m_StBlocks += it->second.stat_data.st_blocks;
111  here.m_NFiles += 1;
112  }
113  }
114  }
115  delete dhp;
116  // Only a directory that was read counts as scanned, else its files are skipped.
117  if (opened)
118  ds->m_scanned = true;
119 }
120 
122 {
123  dprintf("In scan_dir_and_recurse for '%s', size of dir_vec = %d, file_stat_map = %d\n",
124  fst.m_current_path.c_str(),
125  (int)fst.m_current_dirs.size(), (int)fst.m_current_files.size());
126 
127  // Breadth first, accumulate into "here", unless it was already scanned via an
128  // OOB open file request.
129  if ( ! fst.m_dir_state->m_scanned)
130  {
131  DirUsage &here = fst.m_dir_state->m_here_usage;
132  for (auto it = fst.m_current_files.begin(); it != fst.m_current_files.end(); ++it)
133  {
134  dprintf("would be doing something with %s ... has_data=%d, has_cinfo=%d\n",
135  it->first.c_str(), it->second.has_data, it->second.has_cinfo);
136 
137  // XXX Make some of these optional?
138  // Remove files that do not have both cinfo and data?
139  // Remove empty directories before even descending?
140  // Leave this for some consistency pass?
141  // Note that FsTraversal supports ignored paths ... some details (config, N2N) to be clarified.
142 
143  if (it->second.has_data && it->second.has_cinfo) {
144  here.m_StBlocks += it->second.stat_data.st_blocks;
145  here.m_NFiles += 1;
146  }
147  }
148  fst.m_dir_state->m_scanned = true;
149  }
150 
151  // Swap-out directories as inter_dir_scan can use the FsTraversal.
152  std::vector<std::string> dirs;
153  dirs.swap(fst.m_current_dirs);
154 
155  if (++m_dir_scan_check_counter >= 100)
156  {
157  process_inter_dir_scan_open_requests(fst);
158  m_dir_scan_check_counter = 0;
159  }
160 
161  // Descend into sub-dirs, do not accumulate into recursive_subdir_usage yet. This is done
162  // in a separate pass to allow for proper accounting of files being opened during the initial scan.
163  for (auto &dname : dirs)
164  {
165  if (fst.cd_down(dname))
166  {
168  fst.cd_up();
169  }
170  // XXX else try to remove it?
171  }
172 }
173 
175 {
176  // Called after PFC configuration is complete, but before full startup of the daemon.
177  // Base line usages are accumulated as part of the file-system, traversal.
178 
180 
181  DirState *root_ds = m_fs_state.get_root();
182  FsTraversal fst(m_oss);
183  fst.m_protected_top_dirs.insert("pfc-stats"); // XXXX This should come from config. Also: N2N?
184 
185  if ( ! fst.begin_traversal(root_ds, "/"))
186  return false;
187 
188  // The following are initialized in ResourceMonitor.hh to avoid a race at startup:
189  // m_dir_scan_in_progress = true;
190  // m_dir_scan_check_counter = 0;
191 
193 
194  fst.end_traversal();
195 
196  // We have all directories scanned, available in DirState tree, let all remaining files go
197  // and then we shall do the upward propagation of usages.
198  {
199  XrdSysMutexHelper _lock(m_dir_scan_mutex);
200  m_dir_scan_in_progress = false;
201  m_dir_scan_check_counter = 0;
202  }
203  // m_dir_scan_open_requests should now be final, ie, no new entries will be added.
204  while ( ! m_dir_scan_open_requests.empty())
205  {
206  LfnCondRecord &lcr = m_dir_scan_open_requests.front();
207  lcr.f_cond.Lock();
208  lcr.f_checked = true;
209  lcr.f_cond.Signal();
210  lcr.f_cond.UnLock();
211 
212  m_dir_scan_open_requests.pop_front();
213  }
214 
215  // Do upward propagation of usages.
217  m_current_usage_in_st_blocks = root_ds->m_here_usage.m_StBlocks +
220 
221  return true;
222 }
223 
224 //------------------------------------------------------------------------------
225 // Processing of queues
226 //------------------------------------------------------------------------------
227 
229 {
230  static const char *trc_pfx = "process_queues() ";
231 
232  // Assure that we pick up only entries that are present now.
233  // We really want all open records to be processed before file-stats updates
234  // and all those before the close records.
235  // Purges are sort of tangential as they really just modify bytes / number
236  // of files in a directory and do not deal with any persistent file id tokens.
237 
238  int n_records = 0;
239  {
240  XrdSysMutexHelper _lock(&m_queue_mutex);
241  n_records += m_file_open_q.swap_queues();
242  n_records += m_file_update_stats_q.swap_queues();
243  n_records += m_file_close_q.swap_queues();
244  n_records += m_file_purge_q1.swap_queues();
245  n_records += m_file_purge_q2.swap_queues();
246  n_records += m_file_purge_q3.swap_queues();
247  ++m_queue_swap_u1;
248  }
249 
250  for (auto &i : m_file_open_q.read_queue())
251  {
252  // i.id: LFN, i.record: OpenRecord
253  AccessToken &at = token(i.id);
254  dprintf("process file open for token %d, time %ld -- %s\n",
255  i.id, i.record.m_open_time, at.m_filename.c_str());
256 
257  // Resolve fname into DirState.
258  // We could clear the filename after this ... or keep it, should we need it later on.
259  // For now it is just used for debug printouts.
260  DirState *last_existing_ds = nullptr;
261  DirState *ds = m_fs_state.find_dirstate_for_lfn(at.m_filename, &last_existing_ds);
262  at.m_dir_state = ds;
263  note_file_open(ds, last_existing_ds, i.record.m_existing_file);
264 
265  ds->m_here_usage.m_LastOpenTime = i.record.m_open_time;
266  }
267 
268  for (auto &i : m_file_update_stats_q.read_queue())
269  {
270  // i.id: token, i.record: Stats
271  AccessToken &at = token(i.id);
272  // Stats
273  DirState *ds = at.m_dir_state;
274  dprintf("process file update for token %d, %p -- %s\n",
275  i.id, ds, at.m_filename.c_str());
276 
277  ds->m_here_stats.AddUp(i.record);
278  m_current_usage_in_st_blocks += i.record.m_StBlocksAdded;
279  }
280 
281  for (auto &i : m_file_close_q.read_queue())
282  {
283  // i.id: token, i.record: CloseRecord
284  AccessToken &at = token(i.id);
285  dprintf("process file close for token %d, time %ld -- %s\n",
286  i.id, i.record.m_close_time, at.m_filename.c_str());
287 
288  DirState *ds = at.m_dir_state;
289  ds->m_here_stats.m_NFilesClosed += 1;
290  ds->m_here_usage.m_LastCloseTime = i.record.m_close_time;
291 
292  }
293  { // Under the lock: clear() writes fields register_file_update_stats() uses.
294  XrdSysMutexHelper _lock(&m_queue_mutex);
295  for (auto &i : m_file_close_q.read_queue())
296  m_access_tokens.release(i.id);
297  }
298 
299  for (auto &i : m_file_purge_q1.read_queue())
300  {
301  // i.id: DirState*, i.record: PurgeRecord
302  DirState *ds = i.id;
303  ds->m_here_stats.m_StBlocksRemoved += i.record.m_size_in_st_blocks;
304  ds->m_here_stats.m_NFilesRemoved += i.record.m_n_files;
305  m_current_usage_in_st_blocks -= i.record.m_size_in_st_blocks;
306  }
307  for (auto &i : m_file_purge_q2.read_queue())
308  {
309  // i.id: directory-path, i.record: PurgeRecord
310  DirState *ds = m_fs_state.get_root()->find_path(i.id, -1, false, false);
311  if ( ! ds) {
312  TRACE(Error, trc_pfx << "DirState not found for directory path '" << i.id << "'.");
313  // find_path can return the last dir found ... but this clearly isn't a valid purge record.
314  continue;
315  }
316  ds->m_here_stats.m_StBlocksRemoved += i.record.m_size_in_st_blocks;
317  ds->m_here_stats.m_NFilesRemoved += i.record.m_n_files;
318  m_current_usage_in_st_blocks -= i.record.m_size_in_st_blocks;
319  }
320  for (auto &i : m_file_purge_q3.read_queue())
321  {
322  // i.id: LFN, i.record: size of file in st_blocks
323  DirState *ds = m_fs_state.get_root()->find_path(i.id, -1, true, false);
324  if ( ! ds) {
325  TRACE(Error, trc_pfx << "DirState not found for LFN path '" << i.id << "'.");
326  continue;
327  }
328  ds->m_here_stats.m_StBlocksRemoved += i.record;
329  ds->m_here_stats.m_NFilesRemoved += 1;
330  m_current_usage_in_st_blocks -= i.record;
331  }
332 
333  // Read queues / vectors are cleared at swap time.
334  // We might consider reducing their capacity by half if, say, their usage is below 25%.
335 
336  return n_records;
337 }
338 
339 //------------------------------------------------------------------------------
340 // Heart beat
341 //------------------------------------------------------------------------------
342 
344 {
345  static const char *tpfx = "heart_beat() ";
346 
347  const Configuration &conf = Cache::Conf();
348 
349  const int s_queue_proc_interval = 10;
350  const int s_sshot_report_interval = conf.m_dirStatsInterval; // one of 60, 120, 300,
351  // 600, 900, 1200, 1800, 3600 s
352  const int s_purge_check_interval = 60;
353  const int s_purge_report_interval = conf.m_purgeInterval;
354  const int s_purge_cold_files_interval = conf.m_purgeInterval * conf.m_purgeAgeBasedPeriod;
355 
356  // initial scan performed as part of config
357 
358  time_t now = time(0);
359  time_t next_queue_proc_time = now + s_queue_proc_interval;
360  time_t next_sshot_report_time = (now / 60) * 60 + 60; // at next full minute
361  time_t next_purge_check_time = now + s_purge_check_interval;
362  time_t next_purge_report_time = now + s_purge_report_interval;
363  time_t next_purge_cold_files_time = now + s_purge_cold_files_interval;
364 
365  long long n_reported_token_failures = 0;
366 
367  while (true)
368  {
369  time_t start = time(0);
370  time_t next_event = std::min({ next_queue_proc_time, next_sshot_report_time,
371  next_purge_check_time, next_purge_report_time, next_purge_cold_files_time });
372 
373  if (next_event > start)
374  {
375  unsigned int t_sleep = next_event - start;
376  TRACE(Dump, tpfx << "sleeping for " << t_sleep << " seconds until the next beat.");
377  sleep(t_sleep);
378  }
379 
380  // Check if purge has been running and has completed yet.
381  // For now this is only used to prevent removal of empty leaf directories
382  // during stat propagation so we do not need to wait for the condition in
383  // the above sleep.
384  if (m_purge_task_active) {
386  if (m_purge_task_complete) {
388  }
389  }
390 
391  time_t queue_swap_time = time(0);
392 
393  // Always process the queues.
394  int n_processed = process_queues();
395  next_queue_proc_time = queue_swap_time + s_queue_proc_interval;
396  TRACE(Dump, tpfx << "process_queues -- n_records=" << n_processed);
397 
398  { // Access-token space exhausted -- should be unreachable, so say so loudly.
399  long long taf;
400  { XrdSysMutexHelper _lock(&m_queue_mutex); taf = m_token_alloc_failures; }
401  if (taf > n_reported_token_failures) {
402  TRACE(Error, tpfx << "access-token space exhausted "
403  << taf - n_reported_token_failures << " time(s) (total " << taf
404  << "); those files are open but not accounted for. Raise MaxChunks "
405  "on ResourceMonitor::m_access_tokens.");
406  n_reported_token_failures = taf;
407  }
408  }
409 
410  // Always update basic info on m_fs_state (space, usage, file_usage).
412 
413  now = time(0);
414 
415  // Make planning for fs_state_update, sshot dump and purge task.
416  // Second two require the first, so figure out what is going to happen.
417  bool do_sshot_report = next_sshot_report_time <= now;
418  bool do_purge_check = next_purge_check_time <= now;
419  bool do_purge_report = next_purge_report_time <= now;
420  bool do_purge_cold_files = next_purge_cold_files_time <= now;
421 
422  // Update stats in usages if any secondary activity will happen.
423  if (do_sshot_report || do_purge_check || do_purge_report || do_purge_cold_files)
424  {
425  unlink_func unlink_foo = [&](const std::string &dp)->int {
426  int ret = m_oss.Unlink(dp.c_str());
427  if (ret != 0) {
428  TRACE(Info, tpfx << "Empty dir unlink error: " << ret << " at " << dp);
429  } else {
430  TRACE(Debug, tpfx << "Empty dir unlink success: " << dp);
431  }
432  return ret;
433  };
434 
435  // Potentially prune the empty leaf dirs even less frequently, once per hour, maybe?
436  bool purge_leaf_dirs = do_sshot_report && ! m_purge_task_active;
437  m_fs_state.update_stats_and_usages(queue_swap_time, purge_leaf_dirs, unlink_foo);
438 
439  // This reporting into log/stdout is to be removed.
440  // Meaning of conf.is_dir_stat_reporting_on() etc is to be clarified / improved.
441  if (do_sshot_report && conf.is_dir_stat_reporting_on())
442  {
443  const int store_depth = conf.m_dirStatsStoreDepth;
444  #ifdef RM_DEBUG
445  const DirState &root_ds = *m_fs_state.get_root();
446  dprintf("Snapshot n_dirs=%d, total n_dirs=%d\n", root_ds.count_dirs_to_level(store_depth),
448  #endif
449  m_fs_state.dump_recursively(store_depth);
450  }
451 
452  m_fs_state.reset_stats(queue_swap_time);
453  }
454 
455  if (do_sshot_report)
456  {
457  // Sshot reports are equidistant, at "full" reporting interval.
458  next_sshot_report_time = ((now + 1) / s_sshot_report_interval) * s_sshot_report_interval + s_sshot_report_interval;
459 
460  // This should dump out binary snapshot into /pfc-stats/, if so configured.
461 
462  // json dump to std::out for debug purpose
463  DataFsSnapshot ss(m_fs_state, m_fs_state.m_sshot_stats_reset_time);
464  const DirState &root_ds = *m_fs_state.get_root();
465  const int store_depth = conf.m_dirStatsStoreDepth;
466  const int n_sshot_dirs = root_ds.count_dirs_to_level(store_depth);
467  ss.m_dir_states.reserve(n_sshot_dirs);
468  ss.m_dir_states.emplace_back( DirStateElement(root_ds, -1) );
469  fill_sshot_vec_children(root_ds, 0, ss.m_dir_states, store_depth);
470 
471  // This should really be export to a file (preferably binary, but then bin->json command is needed, too).
472  // ss.dump();
473 
474  const char* dumpfile = "/pfc-stats/DirStat.json";
475  ss.write_json_file(dumpfile, m_oss, false);
476  m_fs_state.reset_sshot_stats(queue_swap_time);
477  }
478 
479  if (do_purge_check || do_purge_report || do_purge_cold_files)
480  {
481  perform_purge_check(do_purge_cold_files, do_purge_report ? TRACE_Info : TRACE_Debug);
482 
483  next_purge_check_time = now + s_purge_check_interval;
484  if (do_purge_report) next_purge_report_time = now + s_purge_report_interval;
485  if (do_purge_cold_files) next_purge_cold_files_time = now + s_purge_cold_files_interval;
486  }
487 
488  } // end while forever
489 }
490 
491 //------------------------------------------------------------------------------
492 // DirState export helpers
493 //------------------------------------------------------------------------------
494 
496  int parent_idx,
497  std::vector<DirStateElement> &vec,
498  int max_depth)
499 {
500  const int pos0 = (int) vec.size();
501  const int n_children = (int) parent_ds.m_subdirs.size();
502 
503  // maxdepth 0 is the root alone: a node at max_depth is emitted, its children are not.
504  if (parent_ds.m_depth >= max_depth)
505  {
506  // Truncated here: keep the daughter range at (-1, -1). No children: empty range.
507  if (n_children == 0) {
508  DirStateElement &dse = vec[parent_idx];
509  dse.m_daughters_begin = dse.m_daughters_end = pos0;
510  }
511  return;
512  }
513 
514  { // scoped: the emplace_back loop below can reallocate vec
515  DirStateElement &parent_dse = vec[parent_idx];
516  parent_dse.m_daughters_begin = pos0;
517  parent_dse.m_daughters_end = pos0 + n_children;
518  }
519 
520  if (n_children == 0) return;
521 
522  for (auto const & [name, child] : parent_ds.m_subdirs)
523  {
524  vec.emplace_back( DirStateElement(child, parent_idx) );
525  }
526 
527  int pos = pos0;
528  for (auto const & [name, child] : parent_ds.m_subdirs)
529  {
530  fill_sshot_vec_children(child, pos, vec, max_depth);
531  ++pos;
532  }
533 }
534 
536  int parent_idx,
537  std::vector<DirPurgeElement> &vec,
538  int max_depth)
539 {
540  const int pos0 = (int) vec.size();
541  const int n_children = (int) parent_ds.m_subdirs.size();
542 
543  // maxdepth 0 is the root alone: a node at max_depth is emitted, its children are not.
544  if (parent_ds.m_depth >= max_depth)
545  {
546  // Truncated here: keep the daughter range at (-1, -1). No children: empty range.
547  if (n_children == 0) {
548  DirPurgeElement &dse = vec[parent_idx];
549  dse.m_daughters_begin = dse.m_daughters_end = pos0;
550  }
551  return;
552  }
553 
554  { // scoped: the emplace_back loop below can reallocate vec
555  DirPurgeElement &parent_dse = vec[parent_idx];
556  parent_dse.m_daughters_begin = pos0;
557  parent_dse.m_daughters_end = pos0 + n_children;
558  }
559 
560  if (n_children == 0) return;
561 
562  for (auto const & [name, child] : parent_ds.m_subdirs)
563  {
564  vec.emplace_back( DirPurgeElement(child, child.m_here_usage, child.m_recursive_subdir_usage, parent_idx) );
565  }
566 
567  int pos = pos0;
568  for (auto const & [name, child] : parent_ds.m_subdirs)
569  {
570  fill_pshot_vec_children(child, pos, vec, max_depth);
571  ++pos;
572  }
573 }
574 
575 //------------------------------------------------------------------------------
576 // Purge helpers, drivers, etc.
577 //------------------------------------------------------------------------------
578 
580 {
581  static const char *trc_pfx = "update_vs_and_file_usage_info() ";
582 
583  const auto &conf = Cache::Conf();
584  XrdOssVSInfo vsi;
585 
586  // StatVS error (after it succeeded in config) implies a memory corruption (according to Mr. H).
587  if (m_oss.StatVS(&vsi, conf.m_data_space.c_str(), 1) < 0) {
588  TRACE(Error, trc_pfx << "can't get StatVS for oss space '" << conf.m_data_space << "'. This is a fatal error.");
589  _exit(1);
590  }
591  m_fs_state.m_disk_total = vsi.Total;
592  m_fs_state.m_disk_used = vsi.Total - vsi.Free;
593  m_fs_state.m_file_usage = 512ll * m_current_usage_in_st_blocks;
594  if (m_oss.StatVS(&vsi, conf.m_meta_space.c_str(), 1) < 0) {
595  TRACE(Error, trc_pfx << "can't get StatVS for oss space '" << conf.m_meta_space << "'. This is a fatal error.");
596  _exit(1);
597  }
598  m_fs_state.m_meta_total = vsi.Total;
599  m_fs_state.m_meta_used = vsi.Total - vsi.Free;
600 }
601 
602 long long ResourceMonitor::get_file_usage_bytes_to_remove(const DataFsPurgeshot &ps, long long write_estimate, int tl)
603 {
604  // short names from config values
605  const Configuration &conf = Cache::Conf();
606  long long f0 = conf.m_fileUsageBaseline;
607  long long f1 = conf.m_fileUsageNominal;
608  long long f2 = conf.m_fileUsageMax;
609  long long w1 = conf.m_diskUsageLWM;
610  long long w2 = conf.m_diskUsageHWM;
611 
612  // get usage from purge snapshot
613  long long T = ps.m_disk_total;
614  long long x = ps.m_file_usage;
615  long long u = ps.m_disk_used;
616 
617  // get file usage increase from the previous time interval check
618  long long delta = write_estimate;
619  TRACE_INT(tl, "file usage increased since the previous purge interval in bytes: " << delta );
620 
621  long long bytes_to_remove = 0;
622 
623  // helper lambda function
624  auto clamp = [&x, &bytes_to_remove](long long lowval, long long highval)
625  {
626  long long val = x;
627  long long newval = val - bytes_to_remove;
628 
629  // removed too much
630  if (newval < lowval)
631  {
632  return lowval - val;
633  }
634 
635  // removed too little
636  if (newval > highval)
637  {
638  return val - highval;
639  }
640  // keep the original value
641  return bytes_to_remove;
642  };
643 
644  // under file quota, nothing to do
645  if (x < f0)
646  return 0;
647 
648  // total disk usage exceeds highWatermark
649  if (u >= w2)
650  {
651  TRACE_INT(tl, "Disk usage: " << ps.m_disk_used << " exceed highWatermark " << conf.m_diskUsageHWM);
652  float frac_u = static_cast<float>(u - w2) / (T - w2);
653  float frac_x = static_cast<float>(x - f0) / (f1 - f0);
654 
655  if (w2 == T)
656  {
657  bytes_to_remove = u -w1;
658  }
659  else
660  {
661  if (frac_x > frac_u)
662  {
663  // the cache is the reason for going out of w2 range
664  bytes_to_remove = (frac_x - frac_u) * (f1 - f0);
665  bytes_to_remove += delta;
666  bytes_to_remove = clamp(f0, f1);
667  }
668  else
669  {
670  // someone else is filling disk space, go to f1
671  bytes_to_remove = clamp(f0, f2);
672  }
673  return bytes_to_remove;
674  }
675  }
676 
677  // file quota and total disk usage is within normal range, check if this space usage is
678  // proportinal to disk usage and correct it
679  if (u > w1 && x > f1)
680  {
681  float frac_u = static_cast<float>(u - w1) / (w2 - w1);
682  float frac_x = static_cast<float>(x - f1) / (f2 - f1);
683  if (frac_x > frac_u)
684  {
685  TRACE_INT(tl, "Disproportional file quota usage comapared to disc usage (frac_x/frac_u) = " << frac_x << "/"<< frac_u);
686  bytes_to_remove = (frac_x - frac_u) * (f2 - f1);
687  bytes_to_remove += delta;
688  }
689 
690  // check the new x val will not be below f0
691  bytes_to_remove = clamp(f0, f2);
692  return bytes_to_remove;
693  }
694 
695  // final check: disk useage is lower that w1, check if exceed the max file usage f2
696  if (x > f2)
697  {
698  // drop usage to f2
699  // compare with global disk usage in the previous purge cycle (default 300s)
700  // check delta is not overflowing f2, else set numver of bytes to remove according remove to f0
701 
702  TRACE_INT(tl, "File usage exceeds maxim file usage. Total disk usage is under lowWatermark. Clearing to low file usage.");
703  long long f2delta = std::max(f2 - delta, f0);
704  bytes_to_remove = clamp(f0, f2delta);
705  return bytes_to_remove;
706  }
707 
708  return bytes_to_remove;
709 }
710 
711 void ResourceMonitor::perform_purge_check(bool purge_cold_files, int tl)
712 {
713  static const char *trc_pfx = "perform_purge_check() ";
714  const Configuration &conf = Cache::Conf();
715 
716  std::unique_ptr<DataFsPurgeshot> psp( new DataFsPurgeshot(m_fs_state) );
717  DataFsPurgeshot &ps = *psp;
718 
719  ps.m_file_usage = 512ll * m_current_usage_in_st_blocks;
720  // These are potentially wrong as cache might be writing over preallocated byte ranges.
722  // Can have another estimate based on eiter writes or st-blocks from purge-stats, once we have them.
723 
724  TRACE_INT(tl, trc_pfx << "Purge check:");
725 
726  ps.m_bytes_to_remove = 0;
727  if (conf.are_file_usage_limits_set())
728  {
729  ps.m_bytes_to_remove = get_file_usage_bytes_to_remove(ps, ps.m_estimated_writes_from_writeq, tl);
730  }
731  else
732  {
733  if (ps.m_disk_used > conf.m_diskUsageHWM)
734  {
735  TRACE_INT(tl, "Disk usage: " << ps.m_disk_used << " exceed highWatermark.");
737  }
738  }
739 
740  ps.m_space_based_purge = ps.m_bytes_to_remove ? 1 : 0;
741 
742  // Purge precheck -- check if age-based purge is required
743  // We ignore uvkeep time, it requires reading of cinfo files and it is enforced in File::Open() anyway.
744 
745  if (purge_cold_files && conf.is_age_based_purge_in_effect()) // || conf.is_uvkeep_purge_in_effect())
746  {
747  ps.m_age_based_purge = true;
748  }
749 
750  TRACE_INT(tl, "\tbytes_to_remove = " << ps.m_bytes_to_remove << " B");
751  TRACE_INT(tl, "\tspace_based_purge = " << ps.m_space_based_purge);
752  TRACE_INT(tl, "\tage_based_purge = " << ps.m_age_based_purge);
753 
754  bool periodic = Cache::GetInstance().GetPurgePin() ?
756 
757  if ( ! ps.m_space_based_purge && ! ps.m_age_based_purge && !periodic ) {
758  TRACE(Info, trc_pfx << "purge not required.");
760  return;
761  }
762  if (m_purge_task_active) {
763  TRACE(Warning, trc_pfx << "purge required but previous purge task is still active!");
764  return;
765  }
766 
767  TRACE(Info, trc_pfx << "scheduling purge task.");
768 
769  // At this point we have all the information: report, decide on action.
770  // There is still some missing infrastructure, especially as regards to purge-plugin:
771  // - at what point do we start bugging the pu-pin to start coughing up purge lists?
772  // - have a new parameter or just do it "one cycle before full"?
773  // - what if it doesn't -- when do we do the old-stlye scan & purge?
774  // - how do we do age-based purge and uvkeep purge?
775  // - they are really quite different -- and could run separately, registering
776  // files into a purge-candidate list. This has to be rechecked before the actual
777  // deletion -- eg, by comparing stat time of cinfo + doing the is-active / is-purge-protected.
778 
779  const DirState &root_ds = *m_fs_state.get_root();
780 
781  // Size from the tree itself; the usage counters can be off (#2808).
782  const int n_pshot_dirs = root_ds.count_dirs_to_level(9999);
783 
784  // Report a counters-vs-tree mismatch when it appears or changes.
785  const int n_calc_dirs = 1 + root_ds.m_here_usage.m_NDirectories
787  dprintf("purge dir count recursive=%d vs from_usage=%d\n", n_pshot_dirs, n_calc_dirs);
788  const int discrepancy = n_calc_dirs - n_pshot_dirs;
789  if (discrepancy != m_dir_count_discrepancy)
790  {
791  if (discrepancy != 0) {
792  TRACE(Error, trc_pfx << "directory count mismatch: usage counters " << n_calc_dirs
793  << ", tree " << n_pshot_dirs << ", previous difference " << m_dir_count_discrepancy);
794  } else {
795  TRACE(Info, trc_pfx << "directory count consistent again: " << n_pshot_dirs);
796  }
797  m_dir_count_discrepancy = discrepancy;
798  }
799 
800  ps.m_dir_vec.reserve(n_pshot_dirs);
801  ps.m_dir_vec.emplace_back( DirPurgeElement(root_ds, root_ds.m_here_usage, root_ds.m_recursive_subdir_usage, -1) );
802  fill_pshot_vec_children(root_ds, 0, ps.m_dir_vec, 9999);
803 
804  m_purge_task_active = true;
805 
806  struct PurgeDriverJob : public XrdJob
807  {
808  DataFsPurgeshot *m_purge_shot_ptr;
809 
810  PurgeDriverJob(DataFsPurgeshot *psp) :
811  XrdJob("XrdPfc::ResourceMonitor::PurgeDriver"),
812  m_purge_shot_ptr(psp)
813  {}
814 
815  void DoIt() override
816  {
817  Cache::ResMon().perform_purge_task(*m_purge_shot_ptr);
819 
820  delete m_purge_shot_ptr;
821  delete this;
822  }
823  };
824 
825  Cache::schedP->Schedule( new PurgeDriverJob(psp.release()) );
826 }
827 
828 namespace XrdPfc
829 {
831 }
832 
834 {
835  // BEWARE: Runs in a dedicated thread - is only to communicate back to the
836  // hear_beat() / data structs via the purge queues and condition variable.
837 
838  // const char *tpfx = "perform_purge_task ";
839 
840  {
842  m_purge_task_start = time(0);
843  }
844 
845  // For now, fall back to the old purge ... to be improved with:
846  // - new scan, following the DataFsPurgeshot;
847  // - usage of cinfo stat mtime for time of last access (touch already done at output);
848  // - use DirState* to report back purged files.
849  // Already changed to report back purged files --- but using the string / path variant.
850  OldStylePurgeDriver(ps); // In XrdPfcPurge.cc
851 }
852 
854 {
855  // Separated out so the purge_task can exit without post-checks.
856 
857  {
859  m_purge_task_end = time(0);
860  m_purge_task_complete = true;
862  }
864 }
865 
866 //==============================================================================
867 // Main thread function, do initial test, then enter heart_beat().
868 //==============================================================================
869 
871 {
872  // setup for in-scan -- this is called from initial setup.
873  MutexHolder _lck(m_dir_scan_mutex);
874  m_dir_scan_in_progress = true;
875 }
876 
878 {
879  const char *tpfx = "main_thread_function ";
880  {
881  time_t is_start = time(0);
882  m_fs_state.init_stat_reset_times(is_start);
883  TRACE(Info, tpfx << "Stating initial directory scan.");
884 
885  if ( ! perform_initial_scan()) {
886  TRACE(Error, tpfx << "Initial directory scan has failed. This is a terminal error, aborting.")
887  _exit(1);
888  }
889  // Reset of m_dir_scan_in_progress is done in perform_initial_scan()
890 
891  time_t is_duration = time(0) - is_start;
892  TRACE(Info, tpfx << "Initial directory scan complete, duration=" << is_duration <<"s");
893 
894  // run first process queues
895  int n_proc_is = process_queues();
896  TRACE(Info, tpfx << "First process_queues finished, n_records=" << n_proc_is);
897 
898  // shrink queues if scan time was longer than 30s.
899  if (is_duration > 30 || n_proc_is > 3000)
900  {
901  m_file_open_q.shrink_read_queue();
902  m_file_update_stats_q.shrink_read_queue();
903  m_file_close_q.shrink_read_queue();
904  m_file_purge_q1.shrink_read_queue();
905  m_file_purge_q2.shrink_read_queue();
906  m_file_purge_q3.shrink_read_queue();
907  }
908  }
909  heart_beat();
910 }
911 
912 //==============================================================================
913 // Old prototype from Cache / Purge, now to go into heart_beat() here, above.
914 //==============================================================================
915 
917 {
918  // static const char *trc_pfx = "ResourceMonitorHeartBeat() ";
919 
920  // Pause before initial run
921  sleep(1);
922 
923  // XXXX Setup initial / constant stats (total RAM, total disk, ???)
924 
927 
928  S.Lock();
929 
931 
933 
934  S.UnLock();
935 
936  // XXXX Schedule initial disk scan, time it!
937  //
938  // TRACE(Info, trc_pfx << "scheduling intial disk scan.");
939  // schedP->Schedule( new ScanAndPurgeJob("XrdPfc::ScanAndPurge") );
940  //
941  // bool scan_and_purge_running = true;
942 
943  // XXXX Could we really hold last-usage for all files in memory?
944 
945  // XXXX Think how to handle disk-full, scan/purge not finishing:
946  // - start dropping things out of write queue, but only when RAM gets near full;
947  // - monitoring this then becomes a high-priority job, inner loop with sleep of,
948  // say, 5 or 10 seconds.
949 
950  while (true)
951  {
952  time_t heartbeat_start = time(0);
953 
954  // TRACE(Info, trc_pfx << "HeartBeat starting ...");
955 
956  // if sumary monitoring configured, pupulate OucCacheStats:
957  S.Lock();
958 
959  // - available / used disk space (files usage calculated elsewhere (maybe))
960 
961  // - RAM usage
962  /* XXXX From Cache
963  { XrdSysMutexHelper lck(&m_RAM_mutex);
964  X.MemUsed = m_RAM_used;
965  X.MemWriteQ = m_RAM_write_queue;
966  }
967  */
968 
969  // - files opened / closed etc
970 
971  // do estimate of available space
972  S.UnLock();
973 
974  // if needed, schedule purge in a different thread.
975  // purge is:
976  // - deep scan + gather FSPurgeState
977  // - actual purge
978  //
979  // this thread can continue running and, if needed, stop writing to disk
980  // if purge is taking too long.
981 
982  // think how data is passed / synchronized between this and purge thread
983 
984  // !!!! think how stat collection is done and propgated upwards;
985  // until now it was done once per purge-interval.
986  // now stats will be added up more often, but purge will be done
987  // only occasionally.
988  // also, do we report cumulative values or deltas? cumulative should
989  // be easier and consistent with summary data.
990  // still, some are state - like disk usage, num of files.
991 
992  // Do we take care of directories that need to be newly added into DirState hierarchy?
993  // I.e., when user creates new directories and these are covered by either full
994  // spec or by root + depth declaration.
995 
996  int heartbeat_duration = time(0) - heartbeat_start;
997 
998  // TRACE(Info, trc_pfx << "HeartBeat finished, heartbeat_duration " << heartbeat_duration);
999 
1000  // int sleep_time = m_fs_state..m_purgeInterval - heartbeat_duration;
1001  int sleep_time = 60 - heartbeat_duration;
1002  if (sleep_time > 0)
1003  {
1004  sleep(sleep_time);
1005  }
1006  }
1007 }
int DoIt(int argpnt, int argc, char **argv, bool singleshot)
Definition: XrdAccTest.cc:262
static void child()
#define TRACE_Debug
Definition: XrdCmsTrace.hh:37
#define TRACE_Info
#define XrdOssOK
Definition: XrdOss.hh:50
#define dprintf(...)
void Proto_ResourceMonitorHeartBeat()
#define TRACE_INT(act, x)
Definition: XrdPfcTrace.hh:52
bool Debug
@ Error
#define TRACE(act, x)
Definition: XrdTrace.hh:63
Definition: XrdJob.hh:43
virtual int Opendir(const char *path, XrdOucEnv &env)
Definition: XrdOss.hh:79
long long Total
Definition: XrdOssVS.hh:90
long long Free
Definition: XrdOssVS.hh:91
virtual XrdOssDF * newDir(const char *tident)=0
virtual int StatVS(XrdOssVSInfo *vsP, const char *sname=0, int updt=0)
Definition: XrdOss.cc:117
virtual int Unlink(const char *path, int Opts=0, XrdOucEnv *envP=0)=0
struct XrdOucCacheStats::CacheStats X
XrdOucCacheStats Statistics
Definition: XrdOucCache.hh:686
PurgePin * GetPurgePin() const
Definition: XrdPfc.hh:284
static const Configuration & Conf()
Definition: XrdPfc.cc:134
static ResourceMonitor & ResMon()
Definition: XrdPfc.cc:135
void ClearPurgeProtectedSet()
Definition: XrdPfc.cc:687
static Cache & GetInstance()
Singleton access.
Definition: XrdPfc.cc:132
XrdSysTrace * GetTrace() const
Definition: XrdPfc.hh:295
static XrdScheduler * schedP
Definition: XrdPfc.hh:302
long long WritesSinceLastCall()
Definition: XrdPfc.cc:320
void AddUp(const DirStats &s)
Definition: XrdPfcStats.hh:192
long long m_StBlocksRemoved
Definition: XrdPfcStats.hh:149
std::vector< std::string > m_current_dirs
XrdOucEnv & default_env()
void slurp_dir_ll(XrdOssDF &dh, int dir_level, const char *path, const char *trc_pfx)
bool begin_traversal(DirState *root, const char *root_path)
std::set< std::string > m_protected_top_dirs
bool cd_down(const std::string &dir_name)
std::map< std::string, FilePairStat > m_current_files
Status of cached file. Can be read from and written into a binary file.
Definition: XrdPfcInfo.hh:41
virtual bool CallPeriodically()
void CrossCheckIfScanIsInProgress(const std::string &lfn, XrdSysCondVar &cond)
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 scan_dir_and_recurse(FsTraversal &fst)
void fill_pshot_vec_children(const DirState &parent_ds, int parent_idx, std::vector< DirPurgeElement > &vec, int max_depth)
void release(int i)
Clear the slot and return it for reuse. Caller must hold its mutex.
void Schedule(XrdJob *jp)
@ Warning
Definition: XrdPfc.hh:41
void OldStylePurgeDriver(DataFsPurgeshot &ps)
Definition: XrdPfcPurge.cc:99
void note_file_open(DirState *node, DirState *last_existing, bool existing_file)
std::function< int(const std::string &)> unlink_func
Contains parameters configurable from the xrootd config file.
Definition: XrdPfc.hh:64
long long m_RamAbsAvailable
available from configuration
Definition: XrdPfc.hh:108
long long m_diskTotalSpace
total disk space on configured partition or oss space
Definition: XrdPfc.hh:91
long long m_fileUsageMax
cache purge - files usage maximum
Definition: XrdPfc.hh:96
long long m_fileUsageBaseline
cache purge - files usage baseline
Definition: XrdPfc.hh:94
int m_dirStatsStoreDepth
maximum depth for statistics write out
Definition: XrdPfc.hh:105
long long m_diskUsageHWM
cache purge - disk usage high water mark
Definition: XrdPfc.hh:93
bool are_file_usage_limits_set() const
Definition: XrdPfc.hh:67
long long m_fileUsageNominal
cache purge - files usage nominal
Definition: XrdPfc.hh:95
int m_purgeAgeBasedPeriod
peform cold file / uvkeep purge every this many purge cycles
Definition: XrdPfc.hh:99
long long m_diskUsageLWM
cache purge - disk usage low water mark
Definition: XrdPfc.hh:92
int m_dirStatsInterval
time between resource monitor statistics dump in seconds
Definition: XrdPfc.hh:104
bool is_age_based_purge_in_effect() const
Definition: XrdPfc.hh:68
int m_purgeInterval
sleep interval between cache purges
Definition: XrdPfc.hh:97
bool is_dir_stat_reporting_on() const
Definition: XrdPfc.hh:70
std::vector< DirPurgeElement > m_dir_vec
void write_json_file(const std::string &fname, XrdOss &oss, bool include_preamble)
std::vector< DirStateElement > m_dir_states
void reset_stats(time_t last_update)
void dump_recursively(int max_depth) const
void init_stat_reset_times(time_t t)
void update_stats_and_usages(time_t last_update, bool purge_empty_dirs, unlink_func unlink_foo)
DirState * find_dirstate_for_lfn(const std::string &lfn, DirState **last_existing_dir=nullptr)
void reset_sshot_stats(time_t last_update)
DirUsage m_recursive_subdir_usage
int count_dirs_to_level(int max_depth) const
DirState * find_path(const std::string &path, int max_depth, bool parse_as_lfn, bool create_subdirs, DirState **last_existing_dir=nullptr)
void upward_propagate_initial_scan_usages()