HOPS
HOPS class reference
MHO_Snapshot.hh
Go to the documentation of this file.
1 #ifndef MHO_Snapshot_HH__
2 #define MHO_Snapshot_HH__
3 
4 #include <cstdlib>
5 #include <iostream>
6 #include <ostream>
7 #include <set>
8 #include <sstream>
9 #include <string>
10 #include <unistd.h>
11 
12 //global messaging util
13 #include "MHO_Message.hh"
14 
15 //handles reading directories, listing files etc.
17 
18 //needed to read hops files and extract objects
22 #include "MHO_ContainerStore.hh"
23 
24 namespace hops
25 {
26 
35 //
36 //TODO make this class thread safe
41 {
42 
43  public:
44  //since this is a singleton we need to remove ability to copy/move
45  MHO_Snapshot(MHO_Snapshot const&) = delete;
47  MHO_Snapshot& operator=(MHO_Snapshot const&) = delete;
49 
50  // @brief Returns the directory into which snapshot files are written:
51  // <install_prefix>/snapshot, resolved at runtime via
52  // MHO_DirectoryInterface::GetHopsInstallPrefix(). Falls back to "." (the
53  // current directory) if the install prefix cannot be determined, so that
54  // no absolute install path is baked into the binary..
55  static std::string GetSnapshotDirectory()
56  {
57  std::string prefix = MHO_DirectoryInterface::GetHopsInstallPrefix();
58  if(prefix.empty())
59  {
60  msg_warn("snapshot",
61  "could not determine HOPS install prefix; writing snapshot files to the current directory" << eom);
62  return std::string(".");
63  }
64  return prefix + "/snapshot";
65  }
66 
74  {
75  static MHO_Snapshot instance;
76  return instance;
77  }
78 
85  void SetExecutableName(std::string exe_name) { fExeName = exe_name; };
86 
90  void AcceptAllKeys() { fAcceptAllKeys = true; }
91 
95  void LimitToKeySet() { fAcceptAllKeys = false; }
96 
103  void AddKey(const std::string& key);
110  void RemoveKey(const std::string& key);
114  void RemoveAllKeys();
115 
124  template< typename XObjType > void DumpObject(XObjType* obj, const char* key, const char* name)
125  {
126  DumpObject(obj, std::string(key), std::string(name));
127  }
128 
137  template< typename XObjType > void DumpObject(XObjType* obj, std::string key, std::string name)
138  {
139  if(PassSnapshot(key))
140  {
141  std::string output_file = fPrefix + fExeName + fPostfix;
143  bool status = inter.OpenToAppend(output_file);
144 
145  std::cout << "dump to file: " << output_file << std::endl;
146 
147  if(status)
148  {
149  inter.Write(*obj, name);
150  fCountLabel++;
151  }
152  else
153  {
154  msg_error("file", "error writing object " << name << " to file: " << output_file << eom);
155  }
156  inter.Close();
157  }
158  }
159 
170  template< typename XObjType >
171  void DumpObject(XObjType* obj, std::string key, std::string name, std::string file, int line)
172  {
173  if(PassSnapshot(key))
174  {
175  std::string output_file = fPrefix + fExeName + fPostfix;
177  bool status = inter.OpenToAppend(output_file);
178 
179  msg_debug("snapshot", "dumping object (" << name << ") snapshot to file: " << output_file << eom);
180 
181  if(status)
182  {
183  obj->Insert(std::string("name"), name);
184  obj->Insert(std::string("snapshot_key"), key);
185  obj->Insert(std::string("executable"), fExeName);
186  obj->Insert(std::string("file"), file);
187  obj->Insert(std::string("line"), line);
188  obj->Insert(std::string("count_label"), (int)fCountLabel);
189  inter.Write(*obj, name);
190  fCountLabel++;
191  }
192  else
193  {
194  msg_error("file", "error writing object " << name << " to file: " << output_file << eom);
195  }
196  inter.Close();
197  }
198  }
199 
200  private:
206  int GetPID()
207  {
208  pid_t pid = getpid();
209  return (int)pid;
210  }
211 
212  //no public access to constructor
213  //set up the directory where we dump snapshots
214  MHO_Snapshot(): fCurrentKeyIsAllowed(false), fAcceptAllKeys(false)
215  {
216  std::string dir_string = GetSnapshotDirectory();
217 
218  //dump data into a file for later inspection
219  std::stringstream ss;
220  ss << ".pid";
221  ss << GetPID();
222 
223  fPrefix = dir_string + "/";
224  fPostfix = ss.str() + ".snap";
225  fExeName = "";
226  };
227 
228  virtual ~MHO_Snapshot(){};
229 
230  bool PassSnapshot(std::string key);
231 
232  //used to construct filename in which to dump snapshots
233  int fPID;
234  std::string fPrefix;
235  std::string fPostfix;
236  std::string fExeName;
237 
238  //label is used to count the number of snapshots we have dumped
239  uint32_t fCountLabel;
240 
241  std::set< std::string > fKeys; //keys of which messages we will accept for output
242  bool fCurrentKeyIsAllowed; //current key is in allowed set
243  bool fAcceptAllKeys;
244 };
245 
246 //this is defined as a compiler flag via build system
247 #ifdef HOPS_ENABLE_SNAPSHOTS
248  //allow object snapshots to be dumped when enabled
249  #define take_snapshot(xKEY, xNAME, xOBJECT) MHO_Snapshot::GetInstance().DumpObject(xOBJECT, xKEY, xNAME);
250  #define take_snapshot_here(xKEY, xNAME, xFILE, xLINE, xOBJECT) \
251  MHO_Snapshot::GetInstance().DumpObject(xOBJECT, xKEY, xNAME, xFILE, xLINE);
252 
253 #else
254  //snapshot not enables, define to nothing
255  #define take_snapshot(xKEY, xNAME, xOBJECT)
256  #define take_snapshot_here(xKEY, xNAME, xFILE, xLINE, xOBJECT)
257 #endif
258 
259 } // namespace hops
260 
261 #endif
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:291
#define msg_warn(xKEY, xCONTENT)
Definition: MHO_Message.hh:248
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:238
Class MHO_BinaryFileInterface.
Definition: MHO_BinaryFileInterface.hh:27
bool OpenToAppend(const std::string &obj_filename, const std::string &index_filename="")
Opens a file for appending objects and optionally streams keys to an index file.
Definition: MHO_BinaryFileInterface.hh:87
void Close()
Closes file and key/index streamers if open.
Definition: MHO_BinaryFileInterface.hh:319
bool Write(const XWriteType &obj, const std::string &shortname="")
Writes an object (must inherit from MHO_Serializable) to a file (with optional shortname string)....
Definition: MHO_BinaryFileInterface.hh:347
static std::string GetHopsInstallPrefix()
Returns the absolute path of the HOPS install prefix, determined at runtime from the on-disk location...
Definition: MHO_DirectoryInterface.cc:73
Class MHO_Snapshot.
Definition: MHO_Snapshot.hh:41
void DumpObject(XObjType *obj, std::string key, std::string name)
Dumps an object of type XObjType to a file specified by key and name.
Definition: MHO_Snapshot.hh:137
static MHO_Snapshot & GetInstance()
provides public access to the only static instance
Definition: MHO_Snapshot.hh:73
void DumpObject(XObjType *obj, const char *key, const char *name)
Dumps an object of type XObjType to a file specified by key and name.
Definition: MHO_Snapshot.hh:124
void RemoveAllKeys()
Clears all keys in the MHO_Snapshot object.
Definition: MHO_Snapshot.cc:20
MHO_Snapshot(MHO_Snapshot const &)=delete
void AddKey(const std::string &key)
Inserts a key into the set of keys for MHO_Snapshot.
Definition: MHO_Snapshot.cc:6
MHO_Snapshot & operator=(MHO_Snapshot &&)=delete
static std::string GetSnapshotDirectory()
Definition: MHO_Snapshot.hh:55
void AcceptAllKeys()
Sets the internal flag to accept all keys.
Definition: MHO_Snapshot.hh:90
void SetExecutableName(std::string exe_name)
Setter for executable name.
Definition: MHO_Snapshot.hh:85
MHO_Snapshot & operator=(MHO_Snapshot const &)=delete
void LimitToKeySet()
Sets internal flag to limit keys based on key set.
Definition: MHO_Snapshot.hh:95
MHO_Snapshot(MHO_Snapshot &&)=delete
void DumpObject(XObjType *obj, std::string key, std::string name, std::string file, int line)
Dumps an object to a file with given key, name, file and line number.
Definition: MHO_Snapshot.hh:171
void RemoveKey(const std::string &key)
Removes a key from the MHO_Snapshot object if it exists.
Definition: MHO_Snapshot.cc:11
struct type_status status
Definition: fourfit3.c:53
Definition: MHO_AdhocFlagging.hh:18