Loading...
Searching...
No Matches
XrdPfcDirStateSnapshot.cc
Go to the documentation of this file.
#include "XrdPfcDirStateSnapshot.hh"
#include "XrdPfcPathParseTools.hh"
#include "XrdPfc.hh"
#include "XrdPfcTrace.hh"
#include "XrdOuc/XrdOucJson.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOuc/XrdOucEnv.hh"
#include "XrdOss/XrdOss.hh"
#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <fcntl.h>
// Redefine to also support ordered_json ... we want to keep variable order in JSON save files.
#define PFC_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \
inline void to_json(nlohmann::json &nlohmann_json_j, const Type &nlohmann_json_t) { \
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
} \
inline void from_json(const nlohmann::json &nlohmann_json_j, Type &nlohmann_json_t) { \
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
} \
inline void to_json(nlohmann::ordered_json &nlohmann_json_j, const Type &nlohmann_json_t) { \
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
} \
inline void from_json(const nlohmann::ordered_json &nlohmann_json_j, Type &nlohmann_json_t) { \
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
}
namespace XrdPfc
{
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_LastOpenTime, m_LastCloseTime, m_StBlocks, m_NFilesOpen, m_NFiles, m_NDirectories)
PFC_DEFINE_TYPE_NON_INTRUSIVE(DirStateElement,
m_dir_name, m_stats, m_usage,
m_parent, m_daughters_begin, m_daughters_end)
PFC_DEFINE_TYPE_NON_INTRUSIVE(DataFsSnapshot,
m_sshot_stats_reset_time, m_usage_update_time, m_disk_total, m_disk_used, m_file_usage, m_meta_total, m_meta_used,
m_dir_states)
}
/*
namespace
{
// Open file for writing, throw exception on failure.
void open_ofstream(std::ofstream &ofs, const std::string &fname, const char *pfx = nullptr)
{
ofs.open(fname, std::ofstream::trunc);
if (!ofs)
{
char m[2048];
snprintf(m, 2048, "%s%sError opening %s for write: %m", pfx ? pfx : "", pfx ? " " : "", fname.c_str());
throw std::runtime_error(m);
}
}
}
*/
using namespace XrdPfc;
namespace
{
XrdSysTrace* GetTrace() { return Cache::GetInstance().GetTrace(); }
const char *m_traceID = "DirStateSnapshot";
}
void DataFsSnapshot::write_json_file(const std::string &file_path, XrdOss& oss, bool include_preamble)
{
// Create the data file.const
const Configuration &conf = Cache::Conf();
const char *myUser = conf.m_username.c_str();
XrdOucEnv myEnv;
const char* size_str = "524288";
myEnv.Put("oss.asize", size_str); // advisory size
myEnv.Put("oss.cgroup", conf.m_data_space.c_str()); // AMT: data or metadata space
mode_t mode = 0644;
int cret;
if ((cret = oss.Create(myUser, file_path.c_str(), mode, myEnv, XRDOSS_mkpath)) != XrdOssOK)
{
TRACE(Error, "Create failed for data file " << file_path << ERRNO_AND_ERRSTR(-cret));
return;
}
XrdOssDF *myFile = oss.newFile(myUser);
if ((cret = myFile->Open(file_path.c_str(), O_RDWR, mode, myEnv)) != XrdOssOK)
{
TRACE(Error, "Open failed for data file " << file_path << ERRNO_AND_ERRSTR(-cret));
delete myFile;
return;
}
// Fill the data file.
std::ostringstream os;
if (include_preamble)
{
os << "{ \"dirstate_snapshot\": ";
}
nlohmann::ordered_json j;
to_json(j, *this);
os << std::setw(1);
os << j;
if (include_preamble)
{
os << " }";
}
os << "\n";
myFile->Ftruncate(0);
myFile->Write(os.str().c_str(), 0, os.str().size());
myFile->Close(); delete myFile;
// Create the info file.
std::string cinfo_path(file_path + Info::s_infoExtension);
if ((cret = oss.Create(myUser, cinfo_path.c_str(), mode, myEnv, XRDOSS_mkpath)) != XrdOssOK)
{
TRACE(Error, "Create failed for info file " << cinfo_path << ERRNO_AND_ERRSTR(-cret));
myFile->Close(); delete myFile;
return;
}
XrdOssDF *myInfoFile = oss.newFile(myUser);
if ((cret = myInfoFile->Open(cinfo_path.c_str(), O_RDWR, mode, myEnv)) != XrdOssOK)
{
TRACE(Error, "Open failed for info file " << cinfo_path << ERRNO_AND_ERRSTR(-cret));
delete myInfoFile;
myFile->Close(); delete myFile;
return;
}
// Fill up cinfo.
Info myInfo(GetTrace(), false);
myInfo.SetBufferSizeFileSizeAndCreationTime(512*1024, os.str().size());
myInfo.SetAllBitsSynced();
myInfo.Write(myInfoFile, cinfo_path.c_str());
myInfoFile->Close();
delete myInfoFile;
}
void DataFsSnapshot::dump()
{
nlohmann::ordered_json j;
to_json(j, *this);
std::cout << j.dump(3) << "\n";
}
XrdOss.hh
XrdOssOK
#define XrdOssOK
Definition XrdOss.hh:50
XRDOSS_mkpath
#define XRDOSS_mkpath
Definition XrdOss.hh:466
XrdOucEnv.hh
PFC_DEFINE_TYPE_NON_INTRUSIVE
#define PFC_DEFINE_TYPE_NON_INTRUSIVE(Type,...)
Definition XrdPfcDirStateSnapshot.cc:18
XrdPfcDirStateSnapshot.hh
XrdPfcPathParseTools.hh
XrdPfcTrace.hh
ERRNO_AND_ERRSTR
#define ERRNO_AND_ERRSTR(err_code)
Definition XrdPfcTrace.hh:46
XrdPfc.hh
Error
@ Error
Definition XrdTlsNotaryUtils.hh:42
TRACE
#define TRACE(act, x)
Definition XrdTrace.hh:63
XrdOssDF
Definition XrdOss.hh:63
XrdOssDF::Ftruncate
virtual int Ftruncate(unsigned long long flen)
Definition XrdOss.hh:164
XrdOssDF::Close
virtual int Close(long long *retsz=0)=0
XrdOssDF::Open
virtual int Open(const char *path, int Oflag, mode_t Mode, XrdOucEnv &env)
Definition XrdOss.hh:200
XrdOssDF::Write
virtual ssize_t Write(const void *buffer, off_t offset, size_t size)
Definition XrdOss.hh:345
XrdOss
Definition XrdOss.hh:499
XrdOss::Create
virtual int Create(const char *tid, const char *path, mode_t mode, XrdOucEnv &env, int opts=0)=0
XrdOss::newFile
virtual XrdOssDF * newFile(const char *tident)=0
XrdOucEnv
Definition XrdOucEnv.hh:42
XrdOucEnv::Put
void Put(const char *varname, const char *value)
Definition XrdOucEnv.hh:85
XrdPfc::Cache::Conf
static const Configuration & Conf()
Definition XrdPfc.cc:134
XrdPfc::Cache::GetInstance
static Cache & GetInstance()
Singleton access.
Definition XrdPfc.cc:132
XrdPfc::Cache::GetTrace
XrdSysTrace * GetTrace() const
Definition XrdPfc.hh:295
XrdPfc::DirStats
Definition XrdPfcStats.hh:147
XrdPfc::Info
Status of cached file. Can be read from and written into a binary file.
Definition XrdPfcInfo.hh:41
XrdPfc::Info::s_infoExtension
static const char * s_infoExtension
Definition XrdPfcInfo.hh:309
XrdPfc::Info::Write
bool Write(XrdOssDF *fp, const char *dname, const char *fname=0)
Definition XrdPfcInfo.cc:266
XrdPfc::Info::SetAllBitsSynced
void SetAllBitsSynced()
Mark all blocks as synced to disk.
Definition XrdPfcInfo.cc:144
XrdPfc::Info::SetBufferSizeFileSizeAndCreationTime
void SetBufferSizeFileSizeAndCreationTime(long long bs, long long fs)
Definition XrdPfcInfo.cc:161
XrdSysTrace
Definition XrdSysTrace.hh:49
XrdPfc
Definition XrdPfc.hh:41
XrdPfc::m_sshot_stats_reset_time
m_NDirectories m_sshot_stats_reset_time
Definition XrdPfcDirStateSnapshot.cc:43
XrdPfc::m_NFiles
m_NFiles
Definition XrdPfcDirStateSnapshot.cc:38
XrdPfc::m_StBlocks
m_StBlocks
Definition XrdPfcDirStateSnapshot.cc:38
XrdPfc::m_usage_update_time
m_NDirectories m_usage_update_time
Definition XrdPfcDirStateSnapshot.cc:43
XrdPfc::m_LastOpenTime
m_LastOpenTime
Definition XrdPfcDirStateSnapshot.cc:38
XrdPfc::m_meta_total
m_NDirectories m_meta_total
Definition XrdPfcDirStateSnapshot.cc:43
XrdPfc::m_meta_used
m_NDirectories m_meta_used
Definition XrdPfcDirStateSnapshot.cc:43
XrdPfc::m_disk_used
m_NDirectories m_disk_used
Definition XrdPfcDirStateSnapshot.cc:43
XrdPfc::m_disk_total
m_NDirectories m_disk_total
Definition XrdPfcDirStateSnapshot.cc:43
XrdPfc::PFC_DEFINE_TYPE_NON_INTRUSIVE
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
XrdPfc::m_NFilesOpen
m_NFilesOpen
Definition XrdPfcDirStateSnapshot.cc:38
XrdPfc::m_file_usage
m_NDirectories m_file_usage
Definition XrdPfcDirStateSnapshot.cc:43
XrdPfc::m_LastCloseTime
m_LastCloseTime
Definition XrdPfcDirStateSnapshot.cc:38
XrdPfc::Configuration
Contains parameters configurable from the xrootd config file.
Definition XrdPfc.hh:64
XrdPfc::Configuration::m_data_space
std::string m_data_space
oss space for data files
Definition XrdPfc.hh:88
XrdPfc::Configuration::m_username
std::string m_username
username passed to oss plugin
Definition XrdPfc.hh:87
XrdPfc::DataFsSnapshot
Definition XrdPfcDirStateSnapshot.hh:36
XrdPfc::DataFsSnapshot::write_json_file
void write_json_file(const std::string &fname, XrdOss &oss, bool include_preamble)
Definition XrdPfcDirStateSnapshot.cc:70
XrdPfc::DataFsSnapshot::dump
void dump()
Definition XrdPfcDirStateSnapshot.cc:153
XrdPfc::DirStateElement
Definition XrdPfcDirStateSnapshot.hh:19
XrdPfc::DirUsage
Definition XrdPfcDirStateBase.hh:16