CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DQMFileSaverOnline.cc
Go to the documentation of this file.
11 
12 #include "DQMFileSaverOnline.h"
13 
14 #include <TString.h>
15 #include <TSystem.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <fstream>
20 #include <iostream>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include <openssl/md5.h>
26 #include <filesystem>
27 #include <boost/iostreams/device/mapped_file.hpp>
28 
29 using namespace dqm;
30 
32  backupLumiCount_ = ps.getUntrackedParameter<int>("backupLumiCount", 1);
33  keepBackupLumi_ = ps.getUntrackedParameter<bool>("keepBackupLumi", false);
34 }
35 
37 
39  if (backupLumiCount_ > 0) {
40  if (fp.lumi_ % backupLumiCount_ == 0) {
41  // actual saving is done here
42  makeSnapshot(fp, false);
43  }
44  }
45 }
46 
47 void DQMFileSaverOnline::saveRun(const FileParameters& fp) const { makeSnapshot(fp, true); }
48 
49 void DQMFileSaverOnline::makeSnapshot(const FileParameters& fp, bool final) const {
50  int pid = getpid();
51  char hostname[64];
52  gethostname(hostname, 64);
53  hostname[63] = 0;
54 
55  char suffix[128];
56  if (!final) {
57  snprintf(suffix, 127, ".ls%08ld_host%s_pid%08d", fp.lumi_, hostname, pid);
58  } else {
59  suffix[0] = 0;
60  }
61 
62  std::string prefix = filename(fp, false);
63 
64  std::string root_fp = prefix + ".root" + suffix;
65  std::string meta_fp = prefix + ".root.origin" + suffix;
66 
67  std::string tmp_root_fp = root_fp + ".tmp";
68  std::string tmp_meta_fp = meta_fp + ".tmp";
69 
70  // run_ and lumi_ are ignored if dqmstore is not in multithread mode
72 
73  logFileAction("Writing DQM Root file: ", root_fp);
74  // logFileAction("Writing DQM Origin file: ", meta_fp);
75 
76  //char rewrite[128];
77  //snprintf(rewrite, 128, "\\1Run %ld/\\2/Run summary", fp.run_);
78 
79  //store->save(tmp_root_fp, /* filename */
80  // "", /* path */
81  // "^(Reference/)?([^/]+)", /* pattern */
82  // rewrite, /* rewrite */
83  // store->mtEnabled() ? fp.run_ : 0, /* run */
84  // 0, /* lumi */
85  // fp.saveReference_, /* ref */
86  // fp.saveReferenceQMin_, /* ref minStatus */
87  // "RECREATE"); /* fileupdate */
88  // TODO: some parameters prepared here are now unused, and the code should
89  // eventually be removed.
90  LegacyIOHelper h(&*store);
91  h.save(tmp_root_fp, "", fp.run_, /* saveall */ true, "RECREATE");
92 
93  // write metadata
94  // format.origin: md5:d566a34b27f48d507150a332b189398b 294835
95  // /home/dqmprolocal/output/DQM_V0001_FED_R000194224.root
96  std::ofstream meta_fd(tmp_meta_fp);
97  meta_fd << fillOrigin(tmp_root_fp, root_fp);
98  meta_fd.close();
99 
100  checkError("Rename failed: ", root_fp, ::rename(tmp_root_fp.c_str(), root_fp.c_str()));
101  checkError("Rename failed: ", meta_fp, ::rename(tmp_meta_fp.c_str(), meta_fp.c_str()));
102 
103  SnapshotFiles files = {root_fp, meta_fp};
104  if (final) {
105  // final will never be cleared
107 
108  saveJobReport(root_fp);
109  } else {
110  appendSnapshot(SnapshotFiles{root_fp, meta_fp});
111  }
112 }
113 
115  std::lock_guard<std::mutex> lock(snapshots_lock_);
116 
117  if (!keepBackupLumi_) {
118  while (!snapshots_.empty()) {
119  SnapshotFiles& x = snapshots_.front();
120 
121  // logFileAction("Deleting old snapshot (origin): ", x.meta);
122  checkError("Unlink failed: ", x.meta, ::unlink(x.meta.c_str()));
123 
124  logFileAction("Deleting old snapshot (root): ", x.data);
125  checkError("Unlink failed: ", x.data, ::unlink(x.data.c_str()));
126 
127  snapshots_.pop_front();
128  }
129  }
130 
131  if (!f.data.empty()) {
132  snapshots_.push_back(f);
133  }
134 }
135 
136 void DQMFileSaverOnline::checkError(const char* msg, const std::string& file, int status) const {
137  if (status != 0) {
138  std::string actual_msg = msg;
139  actual_msg += std::strerror(status);
140  logFileAction(actual_msg, file);
141  }
142 }
143 
145  // format.origin (one line):
146  // md5:d566a34b27f48d507150a332b189398b 294835 final_filename.root
147 
148  unsigned char md5[MD5_DIGEST_LENGTH];
149 
150  boost::iostreams::mapped_file_source fp(filename);
151 
152  MD5((unsigned char*)fp.data(), fp.size(), md5);
153 
154  std::ostringstream hash;
155  for (unsigned char& i : md5) {
156  hash << std::hex << std::setfill('0') << std::setw(2) << (int)i;
157  }
158 
159  std::ostringstream out;
160  out << "md5:" << hash.str() << " " << fp.size() << " " << final_filename;
161  return out.str();
162 }
163 
166  desc.setComment("Saves histograms from DQM store, online workflow.");
167 
168  desc.addUntracked<int>("backupLumiCount", 10)
169  ->setComment(
170  "How often the backup file will be generated, in lumisections (-1 "
171  "disables).");
172 
173  desc.addUntracked<bool>("keepBackupLumi", false)
174  ->setComment(
175  "Usually the backup old backup is deleted once the new file is "
176  "available. Setting this to true ensures that no backup files are "
177  "ever deleted. Useful for ML applications, which use backups as a "
178  "'history' of what happened during the run.");
179 
181 
182  // Changed to use addDefault instead of add here because previously
183  // DQMFileSaverOnline and DQMFileSaverPB both used the module label
184  // "saver" which caused conflicting cfi filenames to be generated.
185  // add could be used if unique module labels were given.
186  descriptions.addDefault(desc);
187 }
188 
T getUntrackedParameter(std::string const &, T const &) const
void saveJobReport(const std::string &filename) const
void save(std::string const &filename, std::string const &path="", uint32_t const run=0, bool saveall=true, std::string const &fileupdate="RECREATE")
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
list status
Definition: mps_update.py:107
static const std::string fillOrigin(const std::string &filename, const std::string &final_filename)
static const std::string filename(const FileParameters &fp, bool useLumi=false)
void checkError(const char *msg, const std::string &file, int status) const
void setComment(std::string const &value)
void addDefault(ParameterSetDescription const &psetDescription)
~DQMFileSaverOnline() override
void appendSnapshot(SnapshotFiles new_snap) const
void logFileAction(const std::string &msg, const std::string &fileName) const
void makeSnapshot(const FileParameters &fp, bool final) const
std::list< SnapshotFiles > snapshots_
void saveRun(const FileParameters &fp) const override
tuple msg
Definition: mps_check.py:285
tuple filename
Definition: lut2db_cfg.py:20
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
float x
static void fillDescription(edm::ParameterSetDescription &d)
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
DQMFileSaverOnline(const edm::ParameterSet &ps)
void saveLumi(const FileParameters &fp) const override