XRootD
XrdPfcDirStateSnapshot.cc
Go to the documentation of this file.
3 
4 #include "XrdOuc/XrdOucJson.hh"
5 
6 #include <fstream>
7 #include <iostream>
8 #include <iomanip>
9 
10 
11 // Redefine to also support ordered_json ... we want to keep variable order in JSON save files.
12 #define PFC_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
13  inline void to_json(nlohmann::json &nlohmann_json_j, const Type &nlohmann_json_t) { \
14  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
15  } \
16  inline void from_json(const nlohmann::json &nlohmann_json_j, Type &nlohmann_json_t) { \
17  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
18  } \
19  inline void to_json(nlohmann::ordered_json &nlohmann_json_j, const Type &nlohmann_json_t) { \
20  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
21  } \
22  inline void from_json(const nlohmann::ordered_json &nlohmann_json_j, Type &nlohmann_json_t) { \
23  NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
24  }
25 
26 namespace XrdPfc
27 {
29  m_NumIos, m_Duration, m_BytesHit, m_BytesMissed, m_BytesBypassed, m_BytesWritten, m_StBlocksAdded, m_NCksumErrors,
30  m_StBlocksRemoved, m_NFilesOpened, m_NFilesClosed, m_NFilesCreated, m_NFilesRemoved, m_NDirectoriesCreated, m_NDirectoriesRemoved)
34  m_dir_name, m_stats, m_usage,
35  m_parent, m_daughters_begin, m_daughters_end)
38  m_dir_states)
39 }
40 
41 namespace
42 {
43 // Open file for writing, throw exception on failure.
44 void open_ofstream(std::ofstream &ofs, const std::string &fname, const char *pfx = nullptr)
45 {
46  ofs.open(fname, std::ofstream::trunc);
47  if (!ofs)
48  {
49  char m[2048];
50  snprintf(m, 2048, "%s%sError opening %s for write: %m", pfx ? pfx : "", pfx ? " " : "", fname.c_str());
51  throw std::runtime_error(m);
52  }
53 }
54 }
55 
56 using namespace XrdPfc;
57 
58 void DataFsSnapshot::write_json_file(const std::string &fname, bool include_preamble)
59 {
60  // Throws exception on failed file-open.
61 
62  std::ofstream ofs;
63  open_ofstream(ofs, fname, __func__);
64 
65  if (include_preamble)
66  {
67  ofs << "{ \"dirstate_snapshot\": ";
68  }
69 
70  nlohmann::ordered_json j;
71  to_json(j, *this);
72 
73  ofs << std::setw(1);
74  ofs << j;
75 
76  if (include_preamble)
77  {
78  ofs << " }";
79  }
80 
81  ofs << "\n";
82  ofs.close();
83 }
84 
86 {
87  nlohmann::ordered_json j; // = *this;
88  to_json(j, *this);
89  std::cout << j.dump(3) << "\n";
90 }
91 
92 // DataFsPurgeshot
93 
94 int DataFsPurgeshot::find_dir_entry_from_tok(int entry, PathTokenizer &pt, int pos, int *last_existing_entry) const
95 {
96  if (pos == pt.get_n_dirs())
97  return entry;
98 
99  const DirPurgeElement &dpe = m_dir_vec[entry];
100  for (int i = dpe.m_daughters_begin; i != dpe.m_daughters_end; ++i)
101  {
102  if (m_dir_vec[i].m_dir_name == pt.get_dir(pos)) {
103  return find_dir_entry_from_tok(i, pt, pos + 1, last_existing_entry);
104  }
105  }
106  if (last_existing_entry)
107  *last_existing_entry = entry;
108  return -1;
109 }
110 
111 int DataFsPurgeshot::find_dir_entry_for_dir_path(const std::string &dir_path) const
112 {
113  PathTokenizer pt(dir_path, -1, false);
114  return find_dir_entry_from_tok(0, pt, 0, nullptr);
115 }
116 
117 const DirUsage* DataFsPurgeshot::find_dir_usage_for_dir_path(const std::string &dir_path) const
118 {
119  int entry = find_dir_entry_for_dir_path(dir_path);
120  return entry >= 0 ? &m_dir_vec[entry].m_usage : nullptr;
121 }
Definition: XrdPfc.hh:41
m_NDirectories m_usage_update_time
m_NDirectories m_meta_total
m_NDirectories m_meta_used
m_NDirectories m_stats_reset_time
m_NDirectories m_disk_used
m_NDirectories m_disk_total
PFC_DEFINE_TYPE_NON_INTRUSIVE(DirStats, m_NumIos, m_Duration, m_BytesHit, m_BytesMissed, m_BytesBypassed, m_BytesWritten, m_StBlocksAdded, m_NCksumErrors, m_StBlocksRemoved, m_NFilesOpened, m_NFilesClosed, m_NFilesCreated, m_NFilesRemoved, m_NDirectoriesCreated, m_NDirectoriesRemoved) PFC_DEFINE_TYPE_NON_INTRUSIVE(DirUsage
m_NDirectories m_file_usage
int find_dir_entry_from_tok(int entry, PathTokenizer &pt, int pos, int *last_existing_entry) const
std::vector< DirPurgeElement > m_dir_vec
int find_dir_entry_for_dir_path(const std::string &dir_path) const
const DirUsage * find_dir_usage_for_dir_path(const std::string &dir_path) const
void write_json_file(const std::string &fname, bool include_preamble)
const char * get_dir(int pos)