XRootD
XrdPfcDirState.hh
Go to the documentation of this file.
1 #ifndef __XRDPFC_DIRSTATE_HH__
2 #define __XRDPFC_DIRSTATE_HH__
3 
4 #include "XrdPfcStats.hh"
5 #include "XrdPfcDirStateBase.hh"
6 
7 #include <ctime>
8 #include <functional>
9 #include <map>
10 #include <string>
11 #include <vector>
12 
13 
14 //==============================================================================
15 // Manifest:
16 //------------------------------------------------------------------------------
17 // - Data-holding struct DirUsage -- complementary to Stats.
18 // - Base classes for DirState and DataFsState, shared between in-memory
19 // tree form and snap-shot vector form.
20 // - Forward declatation of structs for DirState export in vector form:
21 // - struct DirStateElement \_ for stats and usages snapshot
22 // - struct DataFsSnapshot /
23 // - struct DirPurgeElement \_ for purge snapshot
24 // - struct DataFsPurgeshot /
25 // Those are in another file so the object file can be included in the
26 // dedicated binary for processing of the binary dumps.
27 // - class DirState -- state of a directory, including current delta-stats.
28 // - class DataFSState -- manager of the DirState tree, starting from root (as in "/").
29 //
30 // Structs for DirState export in vector form (DirStateElement and DataFsSnapshot)
31 // are declared in XrdPfcDirStateSnapshot.hh.
32 
33 //==============================================================================
34 
35 namespace XrdPfc
36 {
37 struct PathTokenizer;
38 
39 using unlink_func = std::function<int(const std::string&)>;
40 
41 //==============================================================================
42 // Structs for DirState export in vector form
43 //==============================================================================
44 
45 struct DirStateElement;
46 struct DataFsSnapshot;
47 
48 struct DirPurgeElement;
49 struct DataFsPurgeshot;
50 
51 
52 //==============================================================================
53 // DirState
54 //==============================================================================
55 
56 struct DirState : public DirStateBase
57 {
58  typedef std::map<std::string, DirState> DsMap_t;
59  typedef DsMap_t::iterator DsMap_i;
60 
63 
66 
67  // This should be optional, only if needed and only up to some max level.
68  // Preferably stored in some extrnal vector (as AccessTokens are) and indexed from here.
69  DirStats m_sshot_stats; // here + subdir, reset after sshot dump
70  // DirStats m_purge_stats; // here + subdir, running avg., as per purge params
71 
74  long long m_StBlocksFuzz = 0;
75  long long m_RecursiveStBlocksFuzz = 0;
76 
77  DirState *m_parent = nullptr;
79  int m_depth;
80  bool m_scanned = false; // set to true after files in this directory are scanned.
81 
82  void init();
83 
84  DirState* create_child(const std::string &dir);
85 
86  DirState* find_path_tok(PathTokenizer &pt, int pos, bool create_subdirs,
87  DirState **last_existing_dir = nullptr);
88 
89  // --- public part ---
90 
91  DirState();
92 
94 
95  DirState(DirState *parent, const std::string& dname);
96 
97  DirState* get_parent() { return m_parent; }
98 
99  DirState* find_path(const std::string &path, int max_depth, bool parse_as_lfn, bool create_subdirs,
100  DirState **last_existing_dir = nullptr);
101 
102  DirState* find_dir(const std::string &dir, bool create_subdirs);
103 
104  int generate_dir_path(std::string &result);
105 
106  // initial scan support
108 
109  // stat & usages updates / management
110  void update_stats_and_usages(std::vector<DirState*> *reap_candidates = nullptr);
111  bool is_empty_leaf() const;
112  void reset_sshot_stats();
113 
114  int count_dirs_to_level(int max_depth) const;
115 
116  void dump_recursively(const char *name, int max_depth) const;
117 };
118 
119 
121 inline void note_created_dirs(DirState *node, DirState *last_existing)
122 {
123  for (DirState *p = node; p != last_existing; )
124  {
125  p = p->get_parent();
126  p->m_here_stats.m_NDirectoriesCreated += 1;
127  }
128 }
129 
132 inline void note_file_open(DirState *node, DirState *last_existing, bool existing_file)
133 {
134  node->m_here_stats.m_NFilesOpened += 1;
135  note_created_dirs(node, last_existing);
136  if ( ! existing_file)
137  node->m_here_stats.m_NFilesCreated += 1;
138 }
139 
140 
141 //==============================================================================
142 // DataFsState
143 //==============================================================================
144 
146 {
148  time_t m_stats_reset_time = 0;
150  // time_t m_purge_stats_reset_time = 0;
151  long long m_st_blocks_fuzz = 0; // all corrections; equals the root's subtree fuzz
152  std::vector<DirState*> m_reap_candidates; // empty leaves at the last snapshot's propagation;
153  // only the reap erases DirStates
154 
156 
158 
159  DirState* get_root() { return & m_root; }
160 
161  DirState* find_dirstate_for_lfn(const std::string& lfn, DirState **last_existing_dir = nullptr)
162  {
163  return m_root.find_path(lfn, -1, true, true, last_existing_dir);
164  }
165 
166  void update_stats_and_usages(time_t last_update, bool collect_reap_candidates);
167  long long reap_empty_leaf_dirs(unlink_func unlink_foo); // returns the residue
168  void reset_sshot_stats(time_t last_update);
169  // void reset_purge_stats();
170 
171  void dump_recursively(int max_depth) const;
172 };
173 
174 }
175 
176 #endif
static void parent()
Definition: XrdPfc.hh:44
void note_created_dirs(DirState *node, DirState *last_existing)
Count the DirState nodes a lookup created, from node up to last_existing.
void note_file_open(DirState *node, DirState *last_existing, bool existing_file)
std::function< int(const std::string &)> unlink_func
std::vector< DirState * > m_reap_candidates
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 collect_reap_candidates)
DirState * find_dirstate_for_lfn(const std::string &lfn, DirState **last_existing_dir=nullptr)
long long reap_empty_leaf_dirs(unlink_func unlink_foo)
void reset_sshot_stats(time_t last_update)
DirState * m_parent
DirUsage m_recursive_subdir_usage
DirState * get_parent()
std::map< std::string, DirState > DsMap_t
DsMap_t::iterator DsMap_i
int count_dirs_to_level(int max_depth) const
DirState * create_child(const std::string &dir)
DirStats m_recursive_subdir_stats
DirState * find_dir(const std::string &dir, bool create_subdirs)
long long m_RecursiveStBlocksFuzz
sum over the subdirs, set by propagation
int generate_dir_path(std::string &result)
DirState()
Constructor.
DirState * find_path_tok(PathTokenizer &pt, int pos, bool create_subdirs, DirState **last_existing_dir=nullptr)
DirState * find_path(const std::string &path, int max_depth, bool parse_as_lfn, bool create_subdirs, DirState **last_existing_dir=nullptr)
void update_stats_and_usages(std::vector< DirState * > *reap_candidates=nullptr)
void dump_recursively(const char *name, int max_depth) const
void upward_propagate_initial_scan_usages()
DirStats m_sshot_stats
long long m_StBlocksFuzz
bool is_empty_leaf() const