XRootD
Loading...
Searching...
No Matches
XrdPfc::ResourceMonitor Class Reference

#include <XrdPfcResourceMonitor.hh>

Collaboration diagram for XrdPfc::ResourceMonitor:

Public Member Functions

 ResourceMonitor (XrdOss &oss)
 ~ResourceMonitor ()
void CrossCheckIfScanIsInProgress (const std::string &lfn, XrdSysCondVar &cond)
void fill_pshot_vec_children (const DirState &parent_ds, int parent_idx, std::vector< DirPurgeElement > &vec, int max_depth)
void fill_sshot_vec_children (const DirState &parent_ds, int parent_idx, std::vector< DirStateElement > &vec, int max_depth)
void heart_beat ()
void init_before_main ()
void main_thread_function ()
bool perform_initial_scan ()
void perform_purge_check (bool purge_cold_files, int tl)
void perform_purge_task (DataFsPurgeshot &ps)
void perform_purge_task_cleanup ()
int process_queues ()
void register_file_close (int token_id, time_t close_timestamp, const Stats &full_stats)
int register_file_open (const std::string &filename, time_t open_timestamp, bool existing_file)
void register_file_purge (const std::string &filename, long long size_in_st_blocks)
void register_file_purge (DirState *target, long long size_in_st_blocks)
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_multi_file_purge (DirState *target, long long size_in_st_blocks, int n_files)
void scan_dir_and_recurse (FsTraversal &fst)
AccessToken & token (int i)
void update_vs_and_file_usage_info ()

Public Attributes

bool m_purge_task_active {false}
bool m_purge_task_complete {false}
XrdSysCondVar m_purge_task_cond {0}
time_t m_purge_task_end {0}
time_t m_purge_task_start {0}

Detailed Description

Definition at line 42 of file XrdPfcResourceMonitor.hh.

Constructor & Destructor Documentation

◆ ResourceMonitor()

ResourceMonitor::ResourceMonitor ( XrdOss & oss)

Definition at line 32 of file XrdPfcResourceMonitor.cc.

32 :
33 m_fs_state(* new DataFsState),
34 m_oss(oss)
35{
36 // XXXX This should come from config. Also: N2N?
37 m_protected_top_dirs.insert("pfc-stats");
38}

◆ ~ResourceMonitor()

ResourceMonitor::~ResourceMonitor ( )

Definition at line 40 of file XrdPfcResourceMonitor.cc.

41{
42 delete &m_fs_state;
43}

Member Function Documentation

◆ CrossCheckIfScanIsInProgress()

void ResourceMonitor::CrossCheckIfScanIsInProgress ( const std::string & lfn,
XrdSysCondVar & cond )

Definition at line 49 of file XrdPfcResourceMonitor.cc.

50{
51 m_dir_scan_mutex.Lock();
52 if (m_dir_scan_in_progress) {
53 bool dir_checked = false;
54 m_dir_scan_open_requests.push_back({lfn, cond, dir_checked});
55 cond.Lock();
56 m_dir_scan_mutex.UnLock();
57 while ( ! dir_checked)
58 cond.Wait();
59 cond.UnLock();
60 } else {
61 m_dir_scan_mutex.UnLock();
62 }
63}

References XrdSysCondVar::Lock(), XrdSysCondVar::UnLock(), and XrdSysCondVar::Wait().

Here is the call graph for this function:

◆ fill_pshot_vec_children()

void ResourceMonitor::fill_pshot_vec_children ( const DirState & parent_ds,
int parent_idx,
std::vector< DirPurgeElement > & vec,
int max_depth )

Definition at line 555 of file XrdPfcResourceMonitor.cc.

559{
560 const int pos0 = (int) vec.size();
561 const int n_children = (int) parent_ds.m_subdirs.size();
562
563 // maxdepth 0 is the root alone: a node at max_depth is emitted, its children are not.
564 if (parent_ds.m_depth >= max_depth)
565 {
566 // Truncated here: keep the daughter range at (-1, -1). No children: empty range.
567 if (n_children == 0) {
568 DirPurgeElement &dse = vec[parent_idx];
569 dse.m_daughters_begin = dse.m_daughters_end = pos0;
570 }
571 return;
572 }
573
574 { // scoped: the emplace_back loop below can reallocate vec
575 DirPurgeElement &parent_dse = vec[parent_idx];
576 parent_dse.m_daughters_begin = pos0;
577 parent_dse.m_daughters_end = pos0 + n_children;
578 }
579
580 if (n_children == 0) return;
581
582 for (auto const & [name, child] : parent_ds.m_subdirs)
583 {
584 vec.emplace_back( DirPurgeElement(child, child.m_here_usage, child.m_recursive_subdir_usage, parent_idx) );
585 }
586
587 int pos = pos0;
588 for (auto const & [name, child] : parent_ds.m_subdirs)
589 {
590 fill_pshot_vec_children(child, pos, vec, max_depth);
591 ++pos;
592 }
593}
static void child()
void fill_pshot_vec_children(const DirState &parent_ds, int parent_idx, std::vector< DirPurgeElement > &vec, int max_depth)

References child(), fill_pshot_vec_children(), XrdPfc::DirPurgeElement::m_daughters_begin, XrdPfc::DirPurgeElement::m_daughters_end, XrdPfc::DirState::m_depth, and XrdPfc::DirState::m_subdirs.

Referenced by fill_pshot_vec_children(), and perform_purge_check().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fill_sshot_vec_children()

void ResourceMonitor::fill_sshot_vec_children ( const DirState & parent_ds,
int parent_idx,
std::vector< DirStateElement > & vec,
int max_depth )

Definition at line 515 of file XrdPfcResourceMonitor.cc.

519{
520 const int pos0 = (int) vec.size();
521 const int n_children = (int) parent_ds.m_subdirs.size();
522
523 // maxdepth 0 is the root alone: a node at max_depth is emitted, its children are not.
524 if (parent_ds.m_depth >= max_depth)
525 {
526 // Truncated here: keep the daughter range at (-1, -1). No children: empty range.
527 if (n_children == 0) {
528 DirStateElement &dse = vec[parent_idx];
529 dse.m_daughters_begin = dse.m_daughters_end = pos0;
530 }
531 return;
532 }
533
534 { // scoped: the emplace_back loop below can reallocate vec
535 DirStateElement &parent_dse = vec[parent_idx];
536 parent_dse.m_daughters_begin = pos0;
537 parent_dse.m_daughters_end = pos0 + n_children;
538 }
539
540 if (n_children == 0) return;
541
542 for (auto const & [name, child] : parent_ds.m_subdirs)
543 {
544 vec.emplace_back( DirStateElement(child, parent_idx) );
545 }
546
547 int pos = pos0;
548 for (auto const & [name, child] : parent_ds.m_subdirs)
549 {
550 fill_sshot_vec_children(child, pos, vec, max_depth);
551 ++pos;
552 }
553}
void fill_sshot_vec_children(const DirState &parent_ds, int parent_idx, std::vector< DirStateElement > &vec, int max_depth)

References child(), fill_sshot_vec_children(), XrdPfc::DirStateElement::m_daughters_begin, XrdPfc::DirStateElement::m_daughters_end, XrdPfc::DirState::m_depth, and XrdPfc::DirState::m_subdirs.

Referenced by fill_sshot_vec_children(), and heart_beat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ heart_beat()

void ResourceMonitor::heart_beat ( )

Definition at line 358 of file XrdPfcResourceMonitor.cc.

359{
360 static const char *tpfx = "heart_beat() ";
361
362 const Configuration &conf = Cache::Conf();
363
364 const int s_queue_proc_interval = 10;
365 const int s_sshot_report_interval = conf.m_dirStatsInterval; // one of 60, 120, 300,
366 // 600, 900, 1200, 1800, 3600 s
367 const int s_purge_check_interval = 60;
368 const int s_purge_report_interval = conf.m_purgeInterval;
369 const int s_purge_cold_files_interval = conf.m_purgeInterval * conf.m_purgeAgeBasedPeriod;
370
371 // initial scan performed as part of config
372
373 time_t now = time(0);
374 time_t next_queue_proc_time = now + s_queue_proc_interval;
375 time_t next_sshot_report_time = (now / 60) * 60 + 60; // at next full minute
376 time_t next_purge_check_time = now + s_purge_check_interval;
377 time_t next_purge_report_time = now + s_purge_report_interval;
378 time_t next_purge_cold_files_time = now + s_purge_cold_files_interval;
379
380 long long n_reported_token_failures = 0;
381
382 while (true)
383 {
384 time_t start = time(0);
385 time_t next_event = std::min({ next_queue_proc_time, next_sshot_report_time,
386 next_purge_check_time, next_purge_report_time, next_purge_cold_files_time });
387
388 if (next_event > start)
389 {
390 unsigned int t_sleep = next_event - start;
391 TRACE(Dump, tpfx << "sleeping for " << t_sleep << " seconds until the next beat.");
392 sleep(t_sleep);
393 }
394
395 // Check if purge has been running and has completed yet.
396 // For now this is only used to prevent removal of empty leaf directories
397 // while it runs, so we do not need to wait for the condition in the above sleep.
399 MutexHolder _lck(m_purge_task_cond);
402 }
403 }
404
405 time_t queue_swap_time = time(0);
406
407 // Always process the queues.
408 int n_processed = process_queues();
409 next_queue_proc_time = queue_swap_time + s_queue_proc_interval;
410 TRACE(Dump, tpfx << "process_queues -- n_records=" << n_processed);
411
412 { // Access-token space exhausted -- should be unreachable, so say so loudly.
413 long long taf;
414 { XrdSysMutexHelper _lock(&m_queue_mutex); taf = m_token_alloc_failures; }
415 if (taf > n_reported_token_failures) {
416 TRACE(Error, tpfx << "access-token space exhausted "
417 << taf - n_reported_token_failures << " time(s) (total " << taf
418 << "); those files are open but not accounted for. Raise MaxChunks "
419 "on ResourceMonitor::m_access_tokens.");
420 n_reported_token_failures = taf;
421 }
422 }
423
424 // Always update basic info on m_fs_state (space, usage, file_usage).
426
427 now = time(0);
428
429 // Make planning for fs_state_update, sshot dump and purge task.
430 // Second two require the first, so figure out what is going to happen.
431 bool do_sshot_report = next_sshot_report_time <= now;
432 bool do_purge_check = next_purge_check_time <= now;
433 bool do_purge_report = next_purge_report_time <= now;
434 bool do_purge_cold_files = next_purge_cold_files_time <= now;
435
436 // Update stats in usages if any secondary activity will happen.
437 if (do_sshot_report || do_purge_check || do_purge_report || do_purge_cold_files)
438 {
439 m_fs_state.update_stats_and_usages(queue_swap_time, do_sshot_report);
440
441 // This reporting into log/stdout is to be removed.
442 // Meaning of conf.is_dir_stat_reporting_on() etc is to be clarified / improved.
443 if (do_sshot_report && conf.is_dir_stat_reporting_on())
444 {
445 const int store_depth = conf.m_dirStatsStoreDepth;
446 #ifdef RM_DEBUG
447 const DirState &root_ds = *m_fs_state.get_root();
448 dprintf("Snapshot n_dirs=%d, total n_dirs=%d\n", root_ds.count_dirs_to_level(store_depth),
450 #endif
451 m_fs_state.dump_recursively(store_depth);
452 }
453
454 }
455
456 if (do_sshot_report)
457 {
458 // Sshot reports are equidistant, at "full" reporting interval.
459 next_sshot_report_time = ((now + 1) / s_sshot_report_interval) * s_sshot_report_interval + s_sshot_report_interval;
460
461 // This should dump out binary snapshot into /pfc-stats/, if so configured.
462
463 // json dump to std::out for debug purpose
464 DataFsSnapshot ss(m_fs_state, m_fs_state.m_sshot_stats_reset_time);
465 ss.m_st_blocks_fuzz = m_fs_state.m_st_blocks_fuzz;
466 const DirState &root_ds = *m_fs_state.get_root();
467 const int store_depth = conf.m_dirStatsStoreDepth;
468 const int n_sshot_dirs = root_ds.count_dirs_to_level(store_depth);
469 ss.m_dir_states.reserve(n_sshot_dirs);
470 ss.m_dir_states.emplace_back( DirStateElement(root_ds, -1) );
471 fill_sshot_vec_children(root_ds, 0, ss.m_dir_states, store_depth);
472
473 // This should really be export to a file (preferably binary, but then bin->json command is needed, too).
474 // ss.dump();
475
476 const char* dumpfile = "/pfc-stats/DirStat.json";
477 ss.write_json_file(dumpfile, m_oss, false);
478 m_fs_state.reset_sshot_stats(queue_swap_time);
479 }
480
481 if (do_purge_check || do_purge_report || do_purge_cold_files)
482 {
483 perform_purge_check(do_purge_cold_files, do_purge_report ? TRACE_Info : TRACE_Debug);
484
485 next_purge_check_time = now + s_purge_check_interval;
486 if (do_purge_report) next_purge_report_time = now + s_purge_report_interval;
487 if (do_purge_cold_files) next_purge_cold_files_time = now + s_purge_cold_files_interval;
488 }
489
490 // Prune the empty leaves the last snapshot reported, after the purge check, which
491 // needs the tree as propagated, and never while a purge task traverses the
492 // directories: one started above defers this to a later beat.
493 if ( ! m_fs_state.m_reap_candidates.empty() && ! m_purge_task_active)
494 {
495 unlink_func unlink_foo = [&](const std::string &dp)->int {
496 int ret = m_oss.Unlink(dp.c_str());
497 if (ret != 0) {
498 TRACE(Info, tpfx << "Empty dir unlink error: " << ret << " at " << dp);
499 } else {
500 TRACE(Debug, tpfx << "Empty dir unlink success: " << dp);
501 }
502 return ret;
503 };
504 // The residue leaves the tree at the next propagation; keep the total in step.
505 m_current_usage_in_st_blocks -= m_fs_state.reap_empty_leaf_dirs(unlink_foo);
506 }
507
508 } // end while forever
509}
#define TRACE_Debug
#define TRACE_Info
#define dprintf(...)
bool Debug
#define TRACE(act, x)
Definition XrdTrace.hh:63
static const Configuration & Conf()
Definition XrdPfc.cc:138
void perform_purge_check(bool purge_cold_files, int tl)
std::function< int(const std::string &)> unlink_func
int m_dirStatsStoreDepth
maximum depth for statistics write out
Definition XrdPfc.hh:109
int m_purgeAgeBasedPeriod
peform cold file / uvkeep purge every this many purge cycles
Definition XrdPfc.hh:103
int m_dirStatsInterval
time between resource monitor statistics dump in seconds
Definition XrdPfc.hh:108
int m_purgeInterval
sleep interval between cache purges
Definition XrdPfc.hh:101
bool is_dir_stat_reporting_on() const
Definition XrdPfc.hh:73
DirUsage m_recursive_subdir_usage
int count_dirs_to_level(int max_depth) const

References XrdPfc::Cache::Conf(), XrdPfc::DirState::count_dirs_to_level(), Debug, dprintf, Error, fill_sshot_vec_children(), XrdPfc::Configuration::is_dir_stat_reporting_on(), XrdPfc::DataFsSnapshot::m_dir_states, XrdPfc::Configuration::m_dirStatsInterval, XrdPfc::Configuration::m_dirStatsStoreDepth, XrdPfc::DirState::m_here_usage, XrdPfc::DirUsage::m_NDirectories, m_purge_task_active, m_purge_task_complete, m_purge_task_cond, XrdPfc::Configuration::m_purgeAgeBasedPeriod, XrdPfc::Configuration::m_purgeInterval, XrdPfc::DirState::m_recursive_subdir_usage, XrdPfc::DataFsSnapshot::m_st_blocks_fuzz, perform_purge_check(), process_queues(), TRACE, TRACE_Debug, TRACE_Info, update_vs_and_file_usage_info(), and XrdPfc::DataFsSnapshot::write_json_file().

Referenced by main_thread_function().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_before_main()

void ResourceMonitor::init_before_main ( )

Definition at line 890 of file XrdPfcResourceMonitor.cc.

891{
892 // setup for in-scan -- this is called from initial setup.
893 MutexHolder _lck(m_dir_scan_mutex);
894 m_dir_scan_in_progress = true;
895}

◆ main_thread_function()

void ResourceMonitor::main_thread_function ( )

Definition at line 897 of file XrdPfcResourceMonitor.cc.

898{
899 const char *tpfx = "main_thread_function ";
900 {
901 time_t is_start = time(0);
902 m_fs_state.init_stat_reset_times(is_start);
903 TRACE(Info, tpfx << "Stating initial directory scan.");
904
905 if ( ! perform_initial_scan()) {
906 TRACE(Error, tpfx << "Initial directory scan has failed. This is a terminal error, aborting.")
907 _exit(1);
908 }
909 // Reset of m_dir_scan_in_progress is done in perform_initial_scan()
910
911 time_t is_duration = time(0) - is_start;
912 TRACE(Info, tpfx << "Initial directory scan complete, duration=" << is_duration <<"s");
913
914 // run first process queues
915 int n_proc_is = process_queues();
916 TRACE(Info, tpfx << "First process_queues finished, n_records=" << n_proc_is);
917
918 // shrink queues if scan time was longer than 30s.
919 if (is_duration > 30 || n_proc_is > 3000)
920 {
921 m_file_open_q.shrink_read_queue();
922 m_file_update_stats_q.shrink_read_queue();
923 m_file_close_q.shrink_read_queue();
924 m_file_purge_q1.shrink_read_queue();
925 m_file_purge_q2.shrink_read_queue();
926 m_file_purge_q3.shrink_read_queue();
927 }
928 }
929 heart_beat();
930}
if(ec< 0) ec

References Error, heart_beat(), perform_initial_scan(), process_queues(), and TRACE.

Referenced by ResourceMonitorThread().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ perform_initial_scan()

bool ResourceMonitor::perform_initial_scan ( )

Definition at line 179 of file XrdPfcResourceMonitor.cc.

180{
181 // Called after PFC configuration is complete, but before full startup of the daemon.
182 // Base line usages are accumulated as part of the file-system, traversal.
183
185
186 DirState *root_ds = m_fs_state.get_root();
187 FsTraversal fst(m_oss);
188 fst.m_protected_top_dirs = m_protected_top_dirs;
189
190 if ( ! fst.begin_traversal(root_ds, "/"))
191 return false;
192
193 // The following are initialized in ResourceMonitor.hh to avoid a race at startup:
194 // m_dir_scan_in_progress = true;
195 // m_dir_scan_check_counter = 0;
196
198
199 fst.end_traversal();
200
201 // We have all directories scanned, available in DirState tree, let all remaining files go
202 // and then we shall do the upward propagation of usages.
203 {
204 XrdSysMutexHelper _lock(m_dir_scan_mutex);
205 m_dir_scan_in_progress = false;
206 m_dir_scan_check_counter = 0;
207 }
208 // m_dir_scan_open_requests should now be final, ie, no new entries will be added.
209 while ( ! m_dir_scan_open_requests.empty())
210 {
211 LfnCondRecord &lcr = m_dir_scan_open_requests.front();
212 lcr.f_cond.Lock();
213 lcr.f_checked = true;
214 lcr.f_cond.Signal();
215 lcr.f_cond.UnLock();
216
217 m_dir_scan_open_requests.pop_front();
218 }
219
220 // Do upward propagation of usages.
222 m_current_usage_in_st_blocks = root_ds->m_here_usage.m_StBlocks +
225
226 return true;
227}
void scan_dir_and_recurse(FsTraversal &fst)
void upward_propagate_initial_scan_usages()

References XrdPfc::FsTraversal::begin_traversal(), XrdPfc::FsTraversal::end_traversal(), XrdSysCondVar::Lock(), XrdPfc::DirState::m_here_usage, XrdPfc::FsTraversal::m_protected_top_dirs, XrdPfc::DirState::m_recursive_subdir_usage, XrdPfc::DirUsage::m_StBlocks, scan_dir_and_recurse(), XrdSysCondVar::Signal(), XrdSysCondVar::UnLock(), update_vs_and_file_usage_info(), and XrdPfc::DirState::upward_propagate_initial_scan_usages().

Referenced by main_thread_function().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ perform_purge_check()

void ResourceMonitor::perform_purge_check ( bool purge_cold_files,
int tl )

Definition at line 731 of file XrdPfcResourceMonitor.cc.

732{
733 static const char *trc_pfx = "perform_purge_check() ";
734 const Configuration &conf = Cache::Conf();
735
736 std::unique_ptr<DataFsPurgeshot> psp( new DataFsPurgeshot(m_fs_state) );
737 DataFsPurgeshot &ps = *psp;
738
739 ps.m_file_usage = 512ll * m_current_usage_in_st_blocks;
740 // These are potentially wrong as cache might be writing over preallocated byte ranges.
742 // Can have another estimate based on eiter writes or st-blocks from purge-stats, once we have them.
743
744 TRACE_INT(tl, trc_pfx << "Purge check:");
745
746 ps.m_bytes_to_remove = 0;
747 if (conf.are_file_usage_limits_set())
748 {
749 ps.m_bytes_to_remove = get_file_usage_bytes_to_remove(ps, ps.m_estimated_writes_from_writeq, tl);
750 }
751 else
752 {
753 if (ps.m_disk_used > conf.m_diskUsageHWM)
754 {
755 TRACE_INT(tl, "Disk usage: " << ps.m_disk_used << " exceed highWatermark.");
757 }
758 }
759
761
762 // Purge precheck -- check if age-based purge is required
763 // We ignore uvkeep time, it requires reading of cinfo files and it is enforced in File::Open() anyway.
764
765 if (purge_cold_files && conf.is_age_based_purge_in_effect()) // || conf.is_uvkeep_purge_in_effect())
766 {
767 ps.m_age_based_purge = true;
768 }
769
770 TRACE_INT(tl, "\tbytes_to_remove = " << ps.m_bytes_to_remove << " B");
771 TRACE_INT(tl, "\tspace_based_purge = " << ps.m_space_based_purge);
772 TRACE_INT(tl, "\tage_based_purge = " << ps.m_age_based_purge);
773
774 bool periodic = Cache::GetInstance().GetPurgePin() ?
776
777 if ( ! ps.m_space_based_purge && ! ps.m_age_based_purge && !periodic ) {
778 TRACE(Info, trc_pfx << "purge not required.");
780 return;
781 }
783 TRACE(Warning, trc_pfx << "purge required but previous purge task is still active!");
784 return;
785 }
786
787 TRACE(Info, trc_pfx << "scheduling purge task.");
788
789 // At this point we have all the information: report, decide on action.
790 // There is still some missing infrastructure, especially as regards to purge-plugin:
791 // - at what point do we start bugging the pu-pin to start coughing up purge lists?
792 // - have a new parameter or just do it "one cycle before full"?
793 // - what if it doesn't -- when do we do the old-stlye scan & purge?
794 // - how do we do age-based purge and uvkeep purge?
795 // - they are really quite different -- and could run separately, registering
796 // files into a purge-candidate list. This has to be rechecked before the actual
797 // deletion -- eg, by comparing stat time of cinfo + doing the is-active / is-purge-protected.
798
799 const DirState &root_ds = *m_fs_state.get_root();
800
801 // Size from the tree itself; the usage counters can be off (#2808).
802 const int n_pshot_dirs = root_ds.count_dirs_to_level(9999);
803
804 // Report a counters-vs-tree mismatch when it appears or changes.
805 const int n_calc_dirs = 1 + root_ds.m_here_usage.m_NDirectories
807 dprintf("purge dir count recursive=%d vs from_usage=%d\n", n_pshot_dirs, n_calc_dirs);
808 const int discrepancy = n_calc_dirs - n_pshot_dirs;
809 if (discrepancy != m_dir_count_discrepancy)
810 {
811 if (discrepancy != 0) {
812 TRACE(Error, trc_pfx << "directory count mismatch: usage counters " << n_calc_dirs
813 << ", tree " << n_pshot_dirs << ", previous difference " << m_dir_count_discrepancy);
814 } else {
815 TRACE(Info, trc_pfx << "directory count consistent again: " << n_pshot_dirs);
816 }
817 m_dir_count_discrepancy = discrepancy;
818 }
819
820 ps.m_dir_vec.reserve(n_pshot_dirs);
821 ps.m_dir_vec.emplace_back( DirPurgeElement(root_ds, root_ds.m_here_usage, root_ds.m_recursive_subdir_usage, -1) );
822 fill_pshot_vec_children(root_ds, 0, ps.m_dir_vec, 9999);
823
824 m_purge_task_active = true;
825
826 struct PurgeDriverJob : public XrdJob
827 {
828 DataFsPurgeshot *m_purge_shot_ptr;
829
830 PurgeDriverJob(DataFsPurgeshot *psp) :
831 XrdJob("XrdPfc::ResourceMonitor::PurgeDriver"),
832 m_purge_shot_ptr(psp)
833 {}
834
835 void DoIt() override
836 {
837 Cache::ResMon().perform_purge_task(*m_purge_shot_ptr);
839
840 delete m_purge_shot_ptr;
841 delete this;
842 }
843 };
844
845 Cache::schedP->Schedule( new PurgeDriverJob(psp.release()) );
846}
int DoIt(int argpnt, int argc, char **argv, bool singleshot)
#define TRACE_INT(act, x)
XrdJob(const char *desc="")
Definition XrdJob.hh:51
static ResourceMonitor & ResMon()
Definition XrdPfc.cc:139
void ClearPurgeProtectedSet()
Definition XrdPfc.cc:696
static Cache & GetInstance()
Singleton access.
Definition XrdPfc.cc:136
static XrdScheduler * schedP
Definition XrdPfc.hh:328
long long WritesSinceLastCall()
Definition XrdPfc.cc:328
PurgePin * GetPurgePin() const
Definition XrdPfc.hh:310
virtual bool CallPeriodically()
void perform_purge_task(DataFsPurgeshot &ps)
void Schedule(XrdJob *jp)
long long m_diskUsageHWM
cache purge - disk usage high water mark
Definition XrdPfc.hh:97
bool are_file_usage_limits_set() const
Definition XrdPfc.hh:70
long long m_diskUsageLWM
cache purge - disk usage low water mark
Definition XrdPfc.hh:96
bool is_age_based_purge_in_effect() const
Definition XrdPfc.hh:71
std::vector< DirPurgeElement > m_dir_vec

References XrdPfc::Configuration::are_file_usage_limits_set(), XrdPfc::PurgePin::CallPeriodically(), XrdPfc::Cache::ClearPurgeProtectedSet(), XrdPfc::Cache::Conf(), XrdPfc::DirState::count_dirs_to_level(), DoIt(), dprintf, Error, fill_pshot_vec_children(), XrdPfc::Cache::GetInstance(), XrdPfc::Cache::GetPurgePin(), XrdPfc::Configuration::is_age_based_purge_in_effect(), XrdPfc::DataFsPurgeshot::m_age_based_purge, XrdPfc::DataFsPurgeshot::m_bytes_to_remove, XrdPfc::DataFsPurgeshot::m_dir_vec, XrdPfc::DataFsStateBase::m_disk_used, XrdPfc::Configuration::m_diskUsageHWM, XrdPfc::Configuration::m_diskUsageLWM, XrdPfc::DataFsPurgeshot::m_estimated_writes_from_writeq, XrdPfc::DataFsStateBase::m_file_usage, XrdPfc::DirState::m_here_usage, XrdPfc::DirUsage::m_NDirectories, m_purge_task_active, XrdPfc::DirState::m_recursive_subdir_usage, XrdPfc::DataFsPurgeshot::m_space_based_purge, perform_purge_task(), perform_purge_task_cleanup(), XrdPfc::Cache::ResMon(), XrdPfc::Cache::schedP, TRACE, TRACE_INT, and XrdPfc::Cache::WritesSinceLastCall().

Referenced by heart_beat().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ perform_purge_task()

void ResourceMonitor::perform_purge_task ( DataFsPurgeshot & ps)

Definition at line 853 of file XrdPfcResourceMonitor.cc.

854{
855 // BEWARE: Runs in a dedicated thread - is only to communicate back to the
856 // hear_beat() / data structs via the purge queues and condition variable.
857
858 // const char *tpfx = "perform_purge_task ";
859
860 {
861 MutexHolder _lck(m_purge_task_cond);
862 m_purge_task_start = time(0);
863 }
864
865 // For now, fall back to the old purge ... to be improved with:
866 // - new scan, following the DataFsPurgeshot;
867 // - usage of cinfo stat mtime for time of last access (touch already done at output);
868 // - use DirState* to report back purged files.
869 // Already changed to report back purged files --- but using the string / path variant.
870 OldStylePurgeDriver(ps); // In XrdPfcPurge.cc
871}
void OldStylePurgeDriver(DataFsPurgeshot &ps)

References m_purge_task_cond, m_purge_task_start, and XrdPfc::OldStylePurgeDriver().

Referenced by perform_purge_check().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ perform_purge_task_cleanup()

void ResourceMonitor::perform_purge_task_cleanup ( )

Definition at line 873 of file XrdPfcResourceMonitor.cc.

874{
875 // Separated out so the purge_task can exit without post-checks.
876
877 {
878 MutexHolder _lck(m_purge_task_cond);
879 m_purge_task_end = time(0);
881 m_purge_task_cond.Signal();
882 }
884}

References XrdPfc::Cache::ClearPurgeProtectedSet(), XrdPfc::Cache::GetInstance(), m_purge_task_complete, m_purge_task_cond, and m_purge_task_end.

Referenced by perform_purge_check().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_queues()

int ResourceMonitor::process_queues ( )

Definition at line 233 of file XrdPfcResourceMonitor.cc.

234{
235 static const char *trc_pfx = "process_queues() ";
236
237 // Assure that we pick up only entries that are present now.
238 // We really want all open records to be processed before file-stats updates
239 // and all those before the close records.
240 // Purges are sort of tangential as they really just modify bytes / number
241 // of files in a directory and do not deal with any persistent file id tokens.
242
243 int n_records = 0;
244 {
245 XrdSysMutexHelper _lock(&m_queue_mutex);
246 n_records += m_file_open_q.swap_queues();
247 n_records += m_file_update_stats_q.swap_queues();
248 n_records += m_file_close_q.swap_queues();
249 n_records += m_file_purge_q1.swap_queues();
250 n_records += m_file_purge_q2.swap_queues();
251 n_records += m_file_purge_q3.swap_queues();
252 ++m_queue_swap_u1;
253 }
254
255 for (auto &i : m_file_open_q.read_queue())
256 {
257 // i.id: LFN, i.record: OpenRecord
258 AccessToken &at = token(i.id);
259 dprintf("process file open for token %d, time %ld -- %s\n",
260 i.id, i.record.m_open_time, at.m_filename.c_str());
261
262 // Resolve fname into DirState.
263 // We could clear the filename after this ... or keep it, should we need it later on.
264 // For now it is just used for debug printouts.
265 DirState *last_existing_ds = nullptr;
266 DirState *ds = m_fs_state.find_dirstate_for_lfn(at.m_filename, &last_existing_ds);
267 at.m_dir_state = ds;
268 note_file_open(ds, last_existing_ds, i.record.m_existing_file);
269
270 ds->m_here_usage.m_LastOpenTime = i.record.m_open_time;
271 }
272
273 for (auto &i : m_file_update_stats_q.read_queue())
274 {
275 // i.id: token, i.record: Stats
276 AccessToken &at = token(i.id);
277 // Stats
278 DirState *ds = at.m_dir_state;
279 dprintf("process file update for token %d, %p -- %s\n",
280 i.id, ds, at.m_filename.c_str());
281
282 ds->m_here_stats.AddUp(i.record);
283 m_current_usage_in_st_blocks += i.record.m_StBlocksAdded;
284 }
285
286 for (auto &i : m_file_close_q.read_queue())
287 {
288 // i.id: token, i.record: CloseRecord
289 AccessToken &at = token(i.id);
290 dprintf("process file close for token %d, time %ld -- %s\n",
291 i.id, i.record.m_close_time, at.m_filename.c_str());
292
293 DirState *ds = at.m_dir_state;
295 ds->m_here_usage.m_LastCloseTime = i.record.m_close_time;
296
297 }
298 { // Under the lock: clear() writes fields register_file_update_stats() uses.
299 XrdSysMutexHelper _lock(&m_queue_mutex);
300 for (auto &i : m_file_close_q.read_queue())
301 m_access_tokens.release(i.id);
302 }
303
304 for (auto &i : m_file_purge_q1.read_queue())
305 {
306 // i.id: DirState*, i.record: PurgeRecord
307 DirState *ds = i.id;
308 ds->m_here_stats.m_StBlocksRemoved += i.record.m_size_in_st_blocks;
309 ds->m_here_stats.m_NFilesRemoved += i.record.m_n_files;
310 m_current_usage_in_st_blocks -= i.record.m_size_in_st_blocks;
311 }
312 for (auto &i : m_file_purge_q2.read_queue())
313 {
314 // i.id: directory-path, i.record: PurgeRecord
315 DirState *ds = m_fs_state.get_root()->find_path(i.id, -1, false, false);
316 if ( ! ds) {
317 if (is_in_protected_top_dir(i.id)) {
318 TRACE(Debug, trc_pfx << "no DirState for '" << i.id
319 << "', as expected under a protected top directory.");
320 } else {
321 TRACE(Error, trc_pfx << "DirState not found for directory path '" << i.id << "'.");
322 }
323 // find_path can return the last dir found ... but this clearly isn't a valid purge record.
324 continue;
325 }
326 ds->m_here_stats.m_StBlocksRemoved += i.record.m_size_in_st_blocks;
327 ds->m_here_stats.m_NFilesRemoved += i.record.m_n_files;
328 m_current_usage_in_st_blocks -= i.record.m_size_in_st_blocks;
329 }
330 for (auto &i : m_file_purge_q3.read_queue())
331 {
332 // i.id: LFN, i.record: size of file in st_blocks
333 DirState *ds = m_fs_state.get_root()->find_path(i.id, -1, true, false);
334 if ( ! ds) {
335 if (is_in_protected_top_dir(i.id)) {
336 TRACE(Debug, trc_pfx << "no DirState for '" << i.id
337 << "', as expected under a protected top directory.");
338 } else {
339 TRACE(Error, trc_pfx << "DirState not found for LFN path '" << i.id << "'.");
340 }
341 continue;
342 }
343 ds->m_here_stats.m_StBlocksRemoved += i.record;
345 m_current_usage_in_st_blocks -= i.record;
346 }
347
348 // Read queues / vectors are cleared at swap time.
349 // We might consider reducing their capacity by half if, say, their usage is below 25%.
350
351 return n_records;
352}
void AddUp(const DirStats &s)
long long m_StBlocksRemoved
void note_file_open(DirState *node, DirState *last_existing, bool existing_file)
DirState * find_path(const std::string &path, int max_depth, bool parse_as_lfn, bool create_subdirs, DirState **last_existing_dir=nullptr)

References XrdPfc::DirStats::AddUp(), Debug, dprintf, Error, XrdPfc::DirState::find_path(), XrdPfc::DirState::m_here_stats, XrdPfc::DirState::m_here_usage, XrdPfc::DirUsage::m_LastCloseTime, XrdPfc::DirUsage::m_LastOpenTime, XrdPfc::DirStats::m_NFilesClosed, XrdPfc::DirStats::m_NFilesRemoved, XrdPfc::DirStats::m_StBlocksRemoved, XrdPfc::note_file_open(), token(), and TRACE.

Referenced by heart_beat(), and main_thread_function().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ register_file_close()

void XrdPfc::ResourceMonitor::register_file_close ( int token_id,
time_t close_timestamp,
const Stats & full_stats )
inline

Definition at line 212 of file XrdPfcResourceMonitor.hh.

212 {
213 if (token_id < 0) return; // register_file_open() could not get a token
214 XrdSysMutexHelper _lock(&m_queue_mutex);
215 m_file_close_q.push(token_id, {close_timestamp, full_stats});
216 }

◆ register_file_open()

int XrdPfc::ResourceMonitor::register_file_open ( const std::string & filename,
time_t open_timestamp,
bool existing_file )
inline

Definition at line 176 of file XrdPfcResourceMonitor.hh.

176 {
177 // Simply return a token, we will resolve it in the actual processing of the queue.
178 XrdSysMutexHelper _lock(&m_queue_mutex);
179 int token_id = m_access_tokens.acquire();
180 if (token_id < 0) {
181 // Unreachable in practice; heart_beat() reports the count.
182 ++m_token_alloc_failures;
183 return -1;
184 }
185 AccessToken &at = m_access_tokens[token_id];
186 at.m_filename = filename;
187 at.m_last_queue_swap_u1 = m_queue_swap_u1 - 1;
188
189 m_file_open_q.push(token_id, {open_timestamp, existing_file});
190 return token_id;
191 }

◆ register_file_purge() [1/2]

void XrdPfc::ResourceMonitor::register_file_purge ( const std::string & filename,
long long size_in_st_blocks )
inline

Definition at line 232 of file XrdPfcResourceMonitor.hh.

232 {
233 XrdSysMutexHelper _lock(&m_queue_mutex);
234 m_file_purge_q3.push(filename, size_in_st_blocks);
235 }

◆ register_file_purge() [2/2]

void XrdPfc::ResourceMonitor::register_file_purge ( DirState * target,
long long size_in_st_blocks )
inline

Definition at line 220 of file XrdPfcResourceMonitor.hh.

220 {
221 XrdSysMutexHelper _lock(&m_queue_mutex);
222 m_file_purge_q1.push(target, {size_in_st_blocks, 1});
223 }

◆ register_file_update_stats()

void XrdPfc::ResourceMonitor::register_file_update_stats ( int token_id,
const Stats & stats )
inline

Definition at line 193 of file XrdPfcResourceMonitor.hh.

193 {
194 if (token_id < 0) return; // register_file_open() could not get a token
195 XrdSysMutexHelper _lock(&m_queue_mutex);
196 AccessToken &at = token(token_id);
197 // Check if this is the first update within this queue swap cycle.
198 if (at.m_last_queue_swap_u1 != m_queue_swap_u1) {
199 m_file_update_stats_q.push(token_id, stats);
200 at.m_last_queue_swap_u1 = m_queue_swap_u1;
201 at.m_last_write_queue_pos = m_file_update_stats_q.write_queue_size() - 1;
202 } else {
203 Stats &existing_stats = m_file_update_stats_q.write_record(at.m_last_write_queue_pos);
204 existing_stats.AddUp(stats);
205 }
206 // Optionally, one could return "scaler" to moodify stat-reporting
207 // frequency in the file ... if it comes too often or too rarely.
208 // See also the logic for determining reporting interval (in N_bytes_read)
209 // in File::Open().
210 }
XrdPosixStats Stats

References XrdPfc::Stats::AddUp(), and token().

Here is the call graph for this function:

◆ register_multi_file_purge() [1/2]

void XrdPfc::ResourceMonitor::register_multi_file_purge ( const std::string & target,
long long size_in_st_blocks,
int n_files )
inline

Definition at line 228 of file XrdPfcResourceMonitor.hh.

228 {
229 XrdSysMutexHelper _lock(&m_queue_mutex);
230 m_file_purge_q2.push(target, {size_in_st_blocks, n_files});
231 }

◆ register_multi_file_purge() [2/2]

void XrdPfc::ResourceMonitor::register_multi_file_purge ( DirState * target,
long long size_in_st_blocks,
int n_files )
inline

Definition at line 224 of file XrdPfcResourceMonitor.hh.

224 {
225 XrdSysMutexHelper _lock(&m_queue_mutex);
226 m_file_purge_q1.push(target, {size_in_st_blocks, n_files});
227 }

◆ scan_dir_and_recurse()

void ResourceMonitor::scan_dir_and_recurse ( FsTraversal & fst)

Definition at line 125 of file XrdPfcResourceMonitor.cc.

126{
127 dprintf("In scan_dir_and_recurse for '%s', size of dir_vec = %d, file_stat_map = %d\n",
128 fst.m_current_path.c_str(),
129 (int)fst.m_current_dirs.size(), (int)fst.m_current_files.size());
130
131 // Breadth first, accumulate into "here", unless it was already scanned via an
132 // OOB open file request.
133 if ( ! fst.m_dir_state->m_scanned)
134 {
135 DirUsage &here = fst.m_dir_state->m_here_usage;
136 for (auto it = fst.m_current_files.begin(); it != fst.m_current_files.end(); ++it)
137 {
138 dprintf("would be doing something with %s ... has_data=%d, has_cinfo=%d\n",
139 it->first.c_str(), it->second.has_data, it->second.has_cinfo);
140
141 // XXX Make some of these optional?
142 // Remove files that do not have both cinfo and data?
143 // Remove empty directories before even descending?
144 // Leave this for some consistency pass?
145 // Note that FsTraversal supports ignored paths ... some details (config, N2N) to be clarified.
146
147 if (it->second.has_data && it->second.has_cinfo) {
148 here.m_StBlocks += it->second.stat_data.st_blocks;
149 here.m_NFiles += 1;
150 seed_last_times_from_cinfo(here, it->second.stat_cinfo);
151 }
152 }
153 fst.m_dir_state->m_scanned = true;
154 }
155
156 // Swap-out directories as inter_dir_scan can use the FsTraversal.
157 std::vector<std::string> dirs;
158 dirs.swap(fst.m_current_dirs);
159
160 if (++m_dir_scan_check_counter >= 100)
161 {
162 process_inter_dir_scan_open_requests(fst);
163 m_dir_scan_check_counter = 0;
164 }
165
166 // Descend into sub-dirs, do not accumulate into recursive_subdir_usage yet. This is done
167 // in a separate pass to allow for proper accounting of files being opened during the initial scan.
168 for (auto &dname : dirs)
169 {
170 if (fst.cd_down(dname))
171 {
173 fst.cd_up();
174 }
175 // XXX else try to remove it?
176 }
177}
std::vector< std::string > m_current_dirs
bool cd_down(const std::string &dir_name)
std::map< std::string, FilePairStat > m_current_files

References XrdPfc::FsTraversal::cd_down(), XrdPfc::FsTraversal::cd_up(), dprintf, XrdPfc::FsTraversal::m_current_dirs, XrdPfc::FsTraversal::m_current_files, XrdPfc::FsTraversal::m_current_path, XrdPfc::FsTraversal::m_dir_state, XrdPfc::DirState::m_here_usage, XrdPfc::DirUsage::m_NFiles, XrdPfc::DirState::m_scanned, XrdPfc::DirUsage::m_StBlocks, and scan_dir_and_recurse().

Referenced by perform_initial_scan(), and scan_dir_and_recurse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ token()

AccessToken & XrdPfc::ResourceMonitor::token ( int i)
inline

Definition at line 245 of file XrdPfcResourceMonitor.hh.

245{ return m_access_tokens[i]; }

Referenced by process_queues(), and register_file_update_stats().

Here is the caller graph for this function:

◆ update_vs_and_file_usage_info()

void ResourceMonitor::update_vs_and_file_usage_info ( )

Definition at line 599 of file XrdPfcResourceMonitor.cc.

600{
601 static const char *trc_pfx = "update_vs_and_file_usage_info() ";
602
603 const auto &conf = Cache::Conf();
604 XrdOssVSInfo vsi;
605
606 // StatVS error (after it succeeded in config) implies a memory corruption (according to Mr. H).
607 if (m_oss.StatVS(&vsi, conf.m_data_space.c_str(), 1) < 0) {
608 TRACE(Error, trc_pfx << "can't get StatVS for oss space '" << conf.m_data_space << "'. This is a fatal error.");
609 _exit(1);
610 }
611 m_fs_state.m_disk_total = vsi.Total;
612 m_fs_state.m_disk_used = vsi.Total - vsi.Free;
613 m_fs_state.m_file_usage = 512ll * m_current_usage_in_st_blocks;
614 if (m_oss.StatVS(&vsi, conf.m_meta_space.c_str(), 1) < 0) {
615 TRACE(Error, trc_pfx << "can't get StatVS for oss space '" << conf.m_meta_space << "'. This is a fatal error.");
616 _exit(1);
617 }
618 m_fs_state.m_meta_total = vsi.Total;
619 m_fs_state.m_meta_used = vsi.Total - vsi.Free;
620}
long long Total
Definition XrdOssVS.hh:90
long long Free
Definition XrdOssVS.hh:91

References XrdPfc::Cache::Conf(), Error, XrdOssVSInfo::Free, XrdOssVSInfo::Total, and TRACE.

Referenced by heart_beat(), and perform_initial_scan().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_purge_task_active

bool XrdPfc::ResourceMonitor::m_purge_task_active {false}

Definition at line 276 of file XrdPfcResourceMonitor.hh.

276{false}; // from the perspective of heart-beat, set only in heartbeat

Referenced by heart_beat(), and perform_purge_check().

◆ m_purge_task_complete

bool XrdPfc::ResourceMonitor::m_purge_task_complete {false}

Definition at line 277 of file XrdPfcResourceMonitor.hh.

277{false}; // from the perspective of the task, reset in heartbeat, set in task

Referenced by heart_beat(), and perform_purge_task_cleanup().

◆ m_purge_task_cond

XrdSysCondVar XrdPfc::ResourceMonitor::m_purge_task_cond {0}

Definition at line 272 of file XrdPfcResourceMonitor.hh.

272{0};

Referenced by heart_beat(), perform_purge_task(), and perform_purge_task_cleanup().

◆ m_purge_task_end

time_t XrdPfc::ResourceMonitor::m_purge_task_end {0}

Definition at line 275 of file XrdPfcResourceMonitor.hh.

275{0};

Referenced by perform_purge_task_cleanup().

◆ m_purge_task_start

time_t XrdPfc::ResourceMonitor::m_purge_task_start {0}

Definition at line 274 of file XrdPfcResourceMonitor.hh.

274{0};

Referenced by perform_purge_task().


The documentation for this class was generated from the following files: