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 //hops snapshot directory should be defined as <install_dir>/snapshot/
25 #ifndef HOPS_SNAPSHOT_DIR
26  #define HOPS_SNAPSHOT_DIR_STR "./"
27 #else
28  #define HOPS_SNAPSHOT_DIR_STR STRING(HOPS_SNAPSHOT_DIR)
29 #endif
30 
31 namespace hops
32 {
33 
42 //
43 //TODO make this class thread safe
48 {
49 
50  public:
51  //since this is a singleton we need to remove ability to copy/move
52  MHO_Snapshot(MHO_Snapshot const&) = delete;
54  MHO_Snapshot& operator=(MHO_Snapshot const&) = delete;
56 
64  {
65  if(fInstance == nullptr)
66  {
67  fInstance = new MHO_Snapshot();
68  }
69  return *fInstance;
70  }
71 
77  void SetExecutableName(std::string exe_name) { fExeName = exe_name; };
78 
84  void SetExecutableName(const char* exe_name) { SetExecutableName(std::string(exe_name)); }
85 
89  void AcceptAllKeys() { fAcceptAllKeys = true; }
90 
94  void LimitToKeySet() { fAcceptAllKeys = false; }
95 
101  void AddKey(const std::string& key);
107  void AddKey(const char* key);
113  void RemoveKey(const std::string& key);
119  void RemoveKey(const char* key);
123  void RemoveAllKeys();
124 
133  template< typename XObjType > void DumpObject(XObjType* obj, const char* key, const char* name)
134  {
135  DumpObject(obj, std::string(key), std::string(name));
136  }
137 
146  template< typename XObjType > void DumpObject(XObjType* obj, std::string key, std::string name)
147  {
148  if(PassSnapshot(key))
149  {
150  std::string output_file = fPrefix + fExeName + fPostfix;
152  bool status = inter.OpenToAppend(output_file);
153 
154  std::cout << "dump to file: " << output_file << std::endl;
155 
156  if(status)
157  {
158  uint32_t label = fCountLabel;
159  inter.Write(*obj, name, label);
160  fCountLabel++;
161  }
162  else
163  {
164  msg_error("file", "error writing object " << name << " to file: " << output_file << eom);
165  }
166  inter.Close();
167  }
168  }
169 
180  template< typename XObjType >
181  void DumpObject(XObjType* obj, std::string key, std::string name, std::string file, int line)
182  {
183  if(PassSnapshot(key))
184  {
185  std::string output_file = fPrefix + fExeName + fPostfix;
187  bool status = inter.OpenToAppend(output_file);
188 
189  msg_debug("snapshot", "dumping object (" << name << ") snapshot to file: " << output_file << eom);
190 
191  if(status)
192  {
193  obj->Insert(std::string("name"), name);
194  obj->Insert(std::string("snapshot_key"), key);
195  obj->Insert(std::string("executable"), fExeName);
196  obj->Insert(std::string("file"), file);
197  obj->Insert(std::string("line"), line);
198  obj->Insert(std::string("count_label"), (int)fCountLabel);
199  uint32_t label = fCountLabel;
200  inter.Write(*obj, name, label);
201  fCountLabel++;
202  }
203  else
204  {
205  msg_error("file", "error writing object " << name << " to file: " << output_file << eom);
206  }
207  inter.Close();
208  }
209  }
210 
211  private:
217  int GetPID()
218  {
219  pid_t pid = getpid();
220  return (int)pid;
221  }
222 
223  //no public access to constructor
224  //set up the stream, for now just point to std::cout
225  //but we may want to allow this to be configured post-construction
226  //perhaps we should also pipe information into log file(s)
227  MHO_Snapshot(): fCurrentKeyIsAllowed(false), fAcceptAllKeys(false)
228  {
229  std::string dir_string = HOPS_SNAPSHOT_DIR_STR;
230 
231  //dump bl_data into a file for later inspection
232  std::stringstream ss;
233  ss << ".pid";
234  ss << GetPID();
235 
236  fPrefix = dir_string + "/";
237  fPostfix = ss.str() + ".snap";
238  fExeName = "";
239  };
240 
241  virtual ~MHO_Snapshot(){};
242 
243  bool PassSnapshot(std::string key);
244 
245  static MHO_Snapshot* fInstance; //static global class instance
246 
247  //used to construct filename in which to dump snapshots
248  int fPID;
249  std::string fPrefix;
250  std::string fPostfix;
251  std::string fExeName;
252 
253  //label is used to count the number of snapshots we have dumped
254  uint32_t fCountLabel;
255 
256  std::set< std::string > fKeys; //keys of which messages we will accept for output
257  bool fCurrentKeyIsAllowed; //current key is in allowed set
258  bool fAcceptAllKeys;
259 };
260 
261 //this is defined as a compiler flag via build system
262 #ifdef HOPS_ENABLE_SNAPSHOTS
263  //allow object snapshots to be dumped when enabled
264  #define take_snapshot(xKEY, xNAME, xOBJECT) MHO_Snapshot::GetInstance().DumpObject(xOBJECT, xKEY, xNAME);
265  #define take_snapshot_here(xKEY, xNAME, xFILE, xLINE, xOBJECT) \
266  MHO_Snapshot::GetInstance().DumpObject(xOBJECT, xKEY, xNAME, xFILE, xLINE);
267 
268 #else
269  //snapshot not enables, define to nothing
270  #define take_snapshot(xKEY, xNAME, xOBJECT)
271  #define take_snapshot_here(xKEY, xNAME, xFILE, xLINE, xOBJECT)
272 #endif
273 
274 } // namespace hops
275 
276 #endif
#define msg_debug(xKEY, xCONTENT)
Definition: MHO_Message.hh:297
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
#define HOPS_SNAPSHOT_DIR_STR
Definition: MHO_Snapshot.hh:26
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:346
Class MHO_Snapshot.
Definition: MHO_Snapshot.hh:48
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:146
static MHO_Snapshot & GetInstance()
provides public access to the only static instance
Definition: MHO_Snapshot.hh:63
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:133
void RemoveAllKeys()
Clears all keys in the MHO_Snapshot object.
Definition: MHO_Snapshot.cc:38
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:9
MHO_Snapshot & operator=(MHO_Snapshot &&)=delete
void AcceptAllKeys()
Sets the internal flag to accept all keys.
Definition: MHO_Snapshot.hh:89
void SetExecutableName(const char *exe_name)
Setter for executable name.
Definition: MHO_Snapshot.hh:84
void SetExecutableName(std::string exe_name)
Setter for executable name.
Definition: MHO_Snapshot.hh:77
MHO_Snapshot & operator=(MHO_Snapshot const &)=delete
void LimitToKeySet()
Sets internal flag to limit keys based on key set.
Definition: MHO_Snapshot.hh:94
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:181
void RemoveKey(const std::string &key)
Removes a key-value pair from the MHO_Snapshot object's keys.
Definition: MHO_Snapshot.cc:19
struct type_status status
Definition: fourfit3.c:53
Definition: MHO_ChannelLabeler.hh:17