XRootD
Loading...
Searching...
No Matches
XrdPfcDirStateSnapshot.cc
Go to the documentation of this file.
3#include "XrdPfc.hh"
4#include "XrdPfcTrace.hh"
5
6#include "XrdOuc/XrdOucJson.hh"
7#include "XrdOuc/XrdOucEnv.hh"
8#include "XrdOuc/XrdOucEnv.hh"
9#include "XrdOss/XrdOss.hh"
10
11#include <fstream>
12#include <sstream>
13#include <iostream>
14#include <iomanip>
15#include <fcntl.h>
16
17// Redefine to also support ordered_json ... we want to keep variable order in JSON save files.
18#define PFC_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
19 inline void to_json(nlohmann::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::json &nlohmann_json_j, Type &nlohmann_json_t) { \
23 NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
24 } \
25 inline void to_json(nlohmann::ordered_json &nlohmann_json_j, const Type &nlohmann_json_t) { \
26 NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
27 } \
28 inline void from_json(const nlohmann::ordered_json &nlohmann_json_j, Type &nlohmann_json_t) { \
29 NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
30 }
31
32namespace XrdPfc
33{
35 m_NumIos, m_Duration, m_BytesHit, m_BytesMissed, m_BytesBypassed, m_BytesWritten, m_StBlocksAdded, m_NCksumErrors,
36 m_StBlocksRemoved, m_NFilesOpened, m_NFilesClosed, m_NFilesCreated, m_NFilesRemoved, m_NDirectoriesCreated, m_NDirectoriesRemoved)
40 m_dir_name, m_stats, m_usage,
41 m_parent, m_daughters_begin, m_daughters_end)
44 m_dir_states)
45}
46/*
47namespace
48{
49// Open file for writing, throw exception on failure.
50void open_ofstream(std::ofstream &ofs, const std::string &fname, const char *pfx = nullptr)
51{
52 ofs.open(fname, std::ofstream::trunc);
53 if (!ofs)
54 {
55 char m[2048];
56 snprintf(m, 2048, "%s%sError opening %s for write: %m", pfx ? pfx : "", pfx ? " " : "", fname.c_str());
57 throw std::runtime_error(m);
58 }
59}
60}
61*/
62using namespace XrdPfc;
63
64namespace
65{
66 XrdSysTrace* GetTrace() { return Cache::GetInstance().GetTrace(); }
67 const char *m_traceID = "DirStateSnapshot";
68}
69
70void DataFsSnapshot::write_json_file(const std::string &file_path, XrdOss& oss, bool include_preamble)
71{
72 // Create the data file.const
73 const Configuration &conf = Cache::Conf();
74 const char *myUser = conf.m_username.c_str();
75 XrdOucEnv myEnv;
76
77 const char* size_str = "524288";
78 myEnv.Put("oss.asize", size_str); // advisory size
79 myEnv.Put("oss.cgroup", conf.m_data_space.c_str()); // AMT: data or metadata space
80
81 mode_t mode = 0644;
82
83 int cret;
84 if ((cret = oss.Create(myUser, file_path.c_str(), mode, myEnv, XRDOSS_mkpath)) != XrdOssOK)
85 {
86 TRACE(Error, "Create failed for data file " << file_path << ERRNO_AND_ERRSTR(-cret));
87 return;
88 }
89
90 XrdOssDF *myFile = oss.newFile(myUser);
91 if ((cret = myFile->Open(file_path.c_str(), O_RDWR, mode, myEnv)) != XrdOssOK)
92 {
93 TRACE(Error, "Open failed for data file " << file_path << ERRNO_AND_ERRSTR(-cret));
94 delete myFile;
95 return;
96 }
97
98 // Fill the data file.
99 std::ostringstream os;
100
101 if (include_preamble)
102 {
103 os << "{ \"dirstate_snapshot\": ";
104 }
105
106 nlohmann::ordered_json j;
107 to_json(j, *this);
108
109 os << std::setw(1);
110 os << j;
111
112 if (include_preamble)
113 {
114 os << " }";
115 }
116
117 os << "\n";
118 myFile->Ftruncate(0);
119 myFile->Write(os.str().c_str(), 0, os.str().size());
120 myFile->Close(); delete myFile;
121
122 // Create the info file.
123
124 std::string cinfo_path(file_path + Info::s_infoExtension);
125
126 if ((cret = oss.Create(myUser, cinfo_path.c_str(), mode, myEnv, XRDOSS_mkpath)) != XrdOssOK)
127 {
128 TRACE(Error, "Create failed for info file " << cinfo_path << ERRNO_AND_ERRSTR(-cret));
129 myFile->Close(); delete myFile;
130 return;
131 }
132
133 XrdOssDF *myInfoFile = oss.newFile(myUser);
134 if ((cret = myInfoFile->Open(cinfo_path.c_str(), O_RDWR, mode, myEnv)) != XrdOssOK)
135 {
136 TRACE(Error, "Open failed for info file " << cinfo_path << ERRNO_AND_ERRSTR(-cret));
137 delete myInfoFile;
138 myFile->Close(); delete myFile;
139 return;
140 }
141
142 // Fill up cinfo.
143
144 Info myInfo(GetTrace(), false);
145 myInfo.SetBufferSizeFileSizeAndCreationTime(512*1024, os.str().size());
146 myInfo.SetAllBitsSynced();
147
148 myInfo.Write(myInfoFile, cinfo_path.c_str());
149 myInfoFile->Close();
150 delete myInfoFile;
151}
152
154{
155 nlohmann::ordered_json j;
156 to_json(j, *this);
157 std::cout << j.dump(3) << "\n";
158}
#define XrdOssOK
Definition XrdOss.hh:50
#define XRDOSS_mkpath
Definition XrdOss.hh:466
#define PFC_DEFINE_TYPE_NON_INTRUSIVE(Type,...)
#define ERRNO_AND_ERRSTR(err_code)
#define TRACE(act, x)
Definition XrdTrace.hh:63
virtual int Ftruncate(unsigned long long flen)
Definition XrdOss.hh:164
virtual int Close(long long *retsz=0)=0
virtual int Open(const char *path, int Oflag, mode_t Mode, XrdOucEnv &env)
Definition XrdOss.hh:200
virtual ssize_t Write(const void *buffer, off_t offset, size_t size)
Definition XrdOss.hh:345
virtual int Create(const char *tid, const char *path, mode_t mode, XrdOucEnv &env, int opts=0)=0
virtual XrdOssDF * newFile(const char *tident)=0
void Put(const char *varname, const char *value)
Definition XrdOucEnv.hh:85
static const Configuration & Conf()
Definition XrdPfc.cc:134
XrdSysTrace * GetTrace()
Definition XrdPfc.hh:283
static Cache & GetInstance()
Singleton access.
Definition XrdPfc.cc:132
Status of cached file. Can be read from and written into a binary file.
Definition XrdPfcInfo.hh:41
static const char * s_infoExtension
bool Write(XrdOssDF *fp, const char *dname, const char *fname=0)
void SetAllBitsSynced()
Mark all blocks as synced to disk.
void SetBufferSizeFileSizeAndCreationTime(long long bs, long long fs)
m_NDirectories m_sshot_stats_reset_time
m_NDirectories m_usage_update_time
m_NDirectories m_meta_total
m_NDirectories m_meta_used
m_NDirectories m_disk_used
m_NDirectories m_disk_total
m_NDirectories m_file_usage
Contains parameters configurable from the xrootd config file.
Definition XrdPfc.hh:64
std::string m_data_space
oss space for data files
Definition XrdPfc.hh:88
std::string m_username
username passed to oss plugin
Definition XrdPfc.hh:87
void write_json_file(const std::string &fname, XrdOss &oss, bool include_preamble)