CMS 3D CMS Logo

DQMFileSaverOnline.cc
Go to the documentation of this file.
10 
11 #include "DQMFileSaverOnline.h"
12 
13 #include <TString.h>
14 #include <TSystem.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <fstream>
19 #include <iostream>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include <openssl/md5.h>
25 #include <boost/filesystem.hpp>
26 #include <boost/iostreams/device/mapped_file.hpp>
27 
28 using namespace dqm;
29 
31  backupLumiCount_ = ps.getUntrackedParameter<int>("backupLumiCount", 1);
32  keepBackupLumi_ = ps.getUntrackedParameter<bool>("keepBackupLumi", false);
33 }
34 
36 
38  if (backupLumiCount_ > 0) {
39  if (fp.lumi_ % backupLumiCount_ == 0) {
40  // actual saving is done here
41  makeSnapshot(fp, false);
42  }
43  }
44 }
45 
46 void DQMFileSaverOnline::saveRun(const FileParameters& fp) const { makeSnapshot(fp, true); }
47 
48 void DQMFileSaverOnline::makeSnapshot(const FileParameters& fp, bool final) const {
49  int pid = getpid();
50  char hostname[64];
51  gethostname(hostname, 64);
52  hostname[63] = 0;
53 
54  char suffix[128];
55  if (!final) {
56  snprintf(suffix, 127, ".ls%08ld_host%s_pid%08d", fp.lumi_, hostname, pid);
57  } else {
58  suffix[0] = 0;
59  }
60 
61  std::string prefix = filename(fp, false);
62 
63  std::string root_fp = prefix + ".root" + suffix;
64  std::string meta_fp = prefix + ".root.origin" + suffix;
65 
66  std::string tmp_root_fp = root_fp + ".tmp";
67  std::string tmp_meta_fp = meta_fp + ".tmp";
68 
69  // run_ and lumi_ are ignored if dqmstore is not in multithread mode
71 
72  logFileAction("Writing DQM Root file: ", root_fp);
73  // logFileAction("Writing DQM Origin file: ", meta_fp);
74 
75  char rewrite[128];
76  snprintf(rewrite, 128, "\\1Run %ld/\\2/Run summary", fp.run_);
77 
78  store->save(tmp_root_fp, /* filename */
79  "", /* path */
80  "^(Reference/)?([^/]+)", /* pattern */
81  rewrite, /* rewrite */
82  store->mtEnabled() ? fp.run_ : 0, /* run */
83  0, /* lumi */
84  fp.saveReference_, /* ref */
85  fp.saveReferenceQMin_, /* ref minStatus */
86  "RECREATE"); /* fileupdate */
87 
88  // write metadata
89  // format.origin: md5:d566a34b27f48d507150a332b189398b 294835
90  // /home/dqmprolocal/output/DQM_V0001_FED_R000194224.root
91  std::ofstream meta_fd(tmp_meta_fp);
92  meta_fd << fillOrigin(tmp_root_fp, root_fp);
93  meta_fd.close();
94 
95  checkError("Rename failed: ", root_fp, ::rename(tmp_root_fp.c_str(), root_fp.c_str()));
96  checkError("Rename failed: ", meta_fp, ::rename(tmp_meta_fp.c_str(), meta_fp.c_str()));
97 
98  SnapshotFiles files = {root_fp, meta_fp};
99  if (final) {
100  // final will never be cleared
102 
103  saveJobReport(root_fp);
104  } else {
105  appendSnapshot(SnapshotFiles{root_fp, meta_fp});
106  }
107 }
108 
110  std::lock_guard<std::mutex> lock(snapshots_lock_);
111 
112  if (!keepBackupLumi_) {
113  while (!snapshots_.empty()) {
114  SnapshotFiles& x = snapshots_.front();
115 
116  // logFileAction("Deleting old snapshot (origin): ", x.meta);
117  checkError("Unlink failed: ", x.meta, ::unlink(x.meta.c_str()));
118 
119  logFileAction("Deleting old snapshot (root): ", x.data);
120  checkError("Unlink failed: ", x.data, ::unlink(x.data.c_str()));
121 
122  snapshots_.pop_front();
123  }
124  }
125 
126  if (!f.data.empty()) {
127  snapshots_.push_back(f);
128  }
129 }
130 
131 void DQMFileSaverOnline::checkError(const char* msg, const std::string& file, int status) const {
132  if (status != 0) {
133  std::string actual_msg = msg;
134  actual_msg += std::strerror(status);
135  logFileAction(actual_msg, file);
136  }
137 }
138 
140  // format.origin (one line):
141  // md5:d566a34b27f48d507150a332b189398b 294835 final_filename.root
142 
143  unsigned char md5[MD5_DIGEST_LENGTH];
144 
145  boost::iostreams::mapped_file_source fp(filename);
146 
147  MD5((unsigned char*)fp.data(), fp.size(), md5);
148 
149  std::ostringstream hash;
150  for (unsigned char& i : md5) {
151  hash << std::hex << std::setfill('0') << std::setw(2) << (int)i;
152  }
153 
154  std::ostringstream out;
155  out << "md5:" << hash.str() << " " << fp.size() << " " << final_filename;
156  return out.str();
157 }
158 
161  desc.setComment("Saves histograms from DQM store, online workflow.");
162 
163  desc.addUntracked<int>("backupLumiCount", 10)
164  ->setComment(
165  "How often the backup file will be generated, in lumisections (-1 "
166  "disables).");
167 
168  desc.addUntracked<bool>("keepBackupLumi", false)
169  ->setComment(
170  "Usually the backup old backup is deleted once the new file is "
171  "available. Setting this to true ensures that no backup files are "
172  "ever deleted. Useful for ML applications, which use backups as a "
173  "'history' of what happened during the run.");
174 
176 
177  // Changed to use addDefault instead of add here because previously
178  // DQMFileSaverOnline and DQMFileSaverPB both used the module label
179  // "saver" which caused conflicting cfi filenames to be generated.
180  // add could be used if unique module labels were given.
181  descriptions.addDefault(desc);
182 }
183 
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)
void save(std::string const &filename, std::string const &path="", std::string const &pattern="", std::string const &rewrite="", uint32_t run=0, uint32_t lumi=0, SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, std::string const &fileupdate="RECREATE")
Definition: DQMStore.cc:2244