CMS 3D CMS Logo

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 <boost/filesystem.hpp>
27 #include <boost/iostreams/device/mapped_file.hpp>
28 
29 using namespace dqm;
30 
32  : DQMFileSaverBase(ps) {
33  backupLumiCount_ = ps.getUntrackedParameter<int>("backupLumiCount", 1);
34  keepBackupLumi_ = ps.getUntrackedParameter<bool>("keepBackupLumi", false);
35 }
36 
38 
40  if (backupLumiCount_ > 0) {
41  if (fp.lumi_ % backupLumiCount_ == 0) {
42  // actual saving is done here
43  makeSnapshot(fp, false);
44  }
45  }
46 }
47 
49  makeSnapshot(fp, true);
50 }
51 
53  bool final) const {
54  int pid = getpid();
55  char hostname[64];
56  gethostname(hostname, 64);
57  hostname[63] = 0;
58 
59  char suffix[128];
60  if (!final) {
61  snprintf(suffix, 127, ".ls%08ld_host%s_pid%08d", fp.lumi_, hostname, pid);
62  } else {
63  suffix[0] = 0;
64  }
65 
66  std::string prefix = filename(fp, false);
67 
68  std::string root_fp = prefix + ".root" + suffix;
69  std::string meta_fp = prefix + ".root.origin" + suffix;
70 
71  std::string tmp_root_fp = root_fp + ".tmp";
72  std::string tmp_meta_fp = meta_fp + ".tmp";
73 
74  // run_ and lumi_ are ignored if dqmstore is not in multithread mode
76 
77  logFileAction("Writing DQM Root file: ", root_fp);
78  // logFileAction("Writing DQM Origin file: ", meta_fp);
79 
80  char rewrite[128];
81  snprintf(rewrite, 128, "\\1Run %ld/\\2/Run summary", fp.run_);
82 
83  store->save(tmp_root_fp, /* filename */
84  "", /* path */
85  "^(Reference/)?([^/]+)", /* pattern */
86  rewrite, /* rewrite */
87  store->mtEnabled() ? fp.run_ : 0, /* run */
88  0, /* lumi */
89  fp.saveReference_, /* ref */
90  fp.saveReferenceQMin_, /* ref minStatus */
91  "RECREATE"); /* fileupdate */
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,
101  ::rename(tmp_root_fp.c_str(), root_fp.c_str()));
102  checkError("Rename failed: ", meta_fp,
103  ::rename(tmp_meta_fp.c_str(), meta_fp.c_str()));
104 
105  SnapshotFiles files = {root_fp, meta_fp};
106  if (final) {
107  // final will never be cleared
109 
110  saveJobReport(root_fp);
111  } else {
112  appendSnapshot(SnapshotFiles{root_fp, meta_fp});
113  }
114 }
115 
117  std::lock_guard<std::mutex> lock(snapshots_lock_);
118 
119  if (!keepBackupLumi_) {
120  while (!snapshots_.empty()) {
121  SnapshotFiles& x = snapshots_.front();
122 
123  // logFileAction("Deleting old snapshot (origin): ", x.meta);
124  checkError("Unlink failed: ", x.meta, ::unlink(x.meta.c_str()));
125 
126  logFileAction("Deleting old snapshot (root): ", x.data);
127  checkError("Unlink failed: ", x.data, ::unlink(x.data.c_str()));
128 
129  snapshots_.pop_front();
130  }
131  }
132 
133  if (!f.data.empty()) {
134  snapshots_.push_back(f);
135  }
136 }
137 
139  int status) const {
140  if (status != 0) {
141  std::string actual_msg = msg;
142  actual_msg += std::strerror(status);
143  logFileAction(actual_msg, file);
144  }
145 }
146 
148  const std::string& filename, const std::string& final_filename) {
149  // format.origin (one line):
150  // md5:d566a34b27f48d507150a332b189398b 294835 final_filename.root
151 
152  unsigned char md5[MD5_DIGEST_LENGTH];
153 
154  boost::iostreams::mapped_file_source fp(filename);
155 
156  MD5((unsigned char*)fp.data(), fp.size(), md5);
157 
158  std::ostringstream hash;
159  for (unsigned char & i : md5) {
160  hash << std::hex << std::setfill('0') << std::setw(2) << (int)i;
161  }
162 
163  std::ostringstream out;
164  out << "md5:" << hash.str() << " " << fp.size() << " " << final_filename;
165  return out.str();
166 }
167 
169  edm::ConfigurationDescriptions& descriptions) {
171  desc.setComment("Saves histograms from DQM store, online workflow.");
172 
173  desc.addUntracked<int>("backupLumiCount", 10)
174  ->setComment(
175  "How often the backup file will be generated, in lumisections (-1 "
176  "disables).");
177 
178  desc.addUntracked<bool>("keepBackupLumi", false)
179  ->setComment(
180  "Usually the backup old backup is deleted once the new file is "
181  "available. Setting this to true ensures that no backup files are "
182  "ever deleted. Useful for ML applications, which use backups as a "
183  "'history' of what happened during the run.");
184 
186 
187  // Changed to use addDefault instead of add here because previously
188  // DQMFileSaverOnline and DQMFileSaverPB both used the module label
189  // "saver" which caused conflicting cfi filenames to be generated.
190  // add could be used if unique module labels were given.
191  descriptions.addDefault(desc);
192 }
193 
T getUntrackedParameter(std::string const &, T const &) const
void saveJobReport(const std::string &filename) const
Definition: rename.py:1
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
static const std::string fillOrigin(const std::string &filename, const std::string &final_filename)
void saveRun(const FileParameters &fp) const override
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)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void addDefault(ParameterSetDescription const &psetDescription)
~DQMFileSaverOnline() override
void appendSnapshot(SnapshotFiles new_snap) const
double f[11][100]
void logFileAction(const std::string &msg, const std::string &fileName) const
void makeSnapshot(const FileParameters &fp, bool final) const
std::list< SnapshotFiles > snapshots_
DQMStore::SaveReferenceTag saveReference_
tuple msg
Definition: mps_check.py:285
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static void fillDescription(edm::ParameterSetDescription &d)
void saveLumi(const FileParameters &fp) const override
DQMFileSaverOnline(const edm::ParameterSet &ps)