CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DQMFileSaverOnline.cc
Go to the documentation of this file.
11 
12 #include "DQMFileSaverOnline.h"
13 
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <iostream>
18 #include <vector>
19 #include <string>
20 #include <fstream>
21 #include <utility>
22 #include <TString.h>
23 #include <TSystem.h>
24 
25 #include <openssl/md5.h>
26 #include <boost/iostreams/device/mapped_file.hpp>
27 #include <boost/filesystem.hpp>
28 
29 using namespace dqm;
30 
32  : DQMFileSaverBase(ps) {
33 
34  backupLumiCount_ = ps.getUntrackedParameter<int>("backupLumiCount", 1);
35 }
36 
38 
40  if (backupLumiCount_ > 0) {
41  if (fp.lumi_ % backupLumiCount_ == 0) {
42 
43  // actual saving is done here
44  makeSnapshot(fp, false);
45  }
46  }
47 }
48 
50  makeSnapshot(fp, true);
51 }
52 
53 void DQMFileSaverOnline::makeSnapshot(const FileParameters& fp, 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  false /* resetMEs */
93  );
94 
95  // write metadata
96  // format.origin: md5:d566a34b27f48d507150a332b189398b 294835 /home/dqmprolocal/output/DQM_V0001_FED_R000194224.root
97  std::ofstream meta_fd(tmp_meta_fp);
98  meta_fd << fillOrigin(tmp_root_fp, root_fp);
99  meta_fd.close();
100 
101  checkError("Rename failed: ", root_fp, ::rename(tmp_root_fp.c_str(), root_fp.c_str()));
102  checkError("Rename failed: ", meta_fp, ::rename(tmp_meta_fp.c_str(), meta_fp.c_str()));
103 
104  SnapshotFiles files = { root_fp, meta_fp };
105  if (final) {
106  // final will never be cleared
108 
109  saveJobReport(root_fp);
110  } else {
111  appendSnapshot(SnapshotFiles{ root_fp, meta_fp });
112  }
113 }
114 
116  std::lock_guard<std::mutex> lock(snapshots_lock_);
117 
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  if (! f.data.empty()) {
131  snapshots_.push_back(f);
132  }
133 }
134 
135 void DQMFileSaverOnline::checkError(const char *msg, const std::string file, int status) const {
136  if (status != 0) {
137  std::string actual_msg = msg;
138  actual_msg += std::strerror(status);
139  logFileAction(actual_msg, file);
140  }
141 }
142 
144  const std::string final_filename) {
145 
146  // format.origin (one line):
147  // md5:d566a34b27f48d507150a332b189398b 294835 final_filename.root
148 
149  unsigned char md5[MD5_DIGEST_LENGTH];
150 
151  boost::iostreams::mapped_file_source fp(filename);
152 
153  MD5((unsigned char *)fp.data(), fp.size(), md5);
154 
155  std::ostringstream hash;
156  for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
157  hash << std::hex << std::setfill('0') << std::setw(2) << (int)(md5[i]);
158  }
159 
160  std::ostringstream out;
161  out << "md5:" << hash.str() << " " << fp.size() << " " << final_filename;
162  return out.str();
163 }
164 
165 
166 
168  edm::ConfigurationDescriptions& descriptions) {
169 
171  desc.setComment("Saves histograms from DQM store, online workflow.");
172 
173  desc.addUntracked<int>("backupLumiCount", 10)->setComment(
174  "How often the backup file will be generated, in lumisections (-1 disables).");
175 
177 
178  descriptions.add("saver", desc);
179 }
180 
T getUntrackedParameter(std::string const &, T const &) const
static const std::string fillOrigin(const std::string filename, const std::string final_filename)
int i
Definition: DBlmapReader.cc:9
void saveJobReport(const std::string &filename) const
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
static const std::string filename(const FileParameters &fp, bool useLumi=false)
T x() const
Cartesian x coordinate.
void setComment(std::string const &value)
void appendSnapshot(SnapshotFiles new_snap) const
void checkError(const char *msg, const std::string file, int status) 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_
virtual void saveRun(const FileParameters &fp) const override
DQMStore::SaveReferenceTag saveReference_
tuple pid
Definition: sysUtil.py:22
void add(std::string const &label, ParameterSetDescription const &psetDescription)
tuple filename
Definition: lut2db_cfg.py:20
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static void fillDescription(edm::ParameterSetDescription &d)
DQMFileSaverOnline(const edm::ParameterSet &ps)
tuple status
Definition: mps_update.py:57
virtual void saveLumi(const FileParameters &fp) const override