HighLAND
MemoryUsage.hxx
1 #ifndef MemoryUsage_hxx_seen
2 #define MemoryUsage_hxx_seen
3 #include <typeinfo>
4 #include <TSystem.h>
5 #include <vector>
6 
7 /// This class can be used to generate histograms of the memory used by
8 /// a highland analysis. Logging can be enabled by specifying the "-m"
9 /// option on the command line. AnalysisBase and SimpleLoopBase will then
10 /// instantiate, log and write out the memory usage to the output file.
11 class MemoryUsage {
12  public:
13  struct CmpMemResident {
14  /// Comparison operator for selecting which ProcInfo_t has the
15  /// largest resident memory value.
16  bool operator()(ProcInfo_t lhs, ProcInfo_t rhs) {
17  return lhs.fMemResident < rhs.fMemResident;
18  }
19  };
20 
21  struct CmpMemVirtual {
22  /// Comparison operator for selecting which ProcInfo_t has the
23  /// largest virtual memory value.
24  bool operator()(ProcInfo_t lhs, ProcInfo_t rhs) {
25  return lhs.fMemVirtual < rhs.fMemVirtual;
26  }
27  };
28 
29  /// Construct the memory usage class with logging disabled.
30  MemoryUsage();
31  ~MemoryUsage();
32 
33  /// Called to enable the memory logging. Enable the logging of memory
34  /// usage. If this is not called, LogMemory() and Write() have no effects.
35  /// This can be called with a bool argument to set the enabled flag. If
36  /// the argument is false, then memory logging is disabled.
37  void Enable(bool enable = true);
38 
39  /// Log the current memory usage. Record the current memory being used by
40  /// the process. If Enable() has not been called, this function does
41  /// nothing. In general, the memory usage should be logged once per
42  /// event, but it can be called any number of times. Logging memory
43  /// requires saving two float and two longs into a vector.
44  void LogMemory();
45 
46  /// Write histograms of the memory usage to the output file.
47  void Write();
48 
49  private:
50  /// Whether to log memory usage. Set using Enable()
51  bool fEnabled;
52 
53  /// Record of the memory usage each time LogMemory() is called.
54  ///
55  /// From TSystem.h:
56  /// struct ProcInfo_t {
57  /// Float_t fCpuUser; // user time used by this process in seconds
58  /// Float_t fCpuSys; // system time used by this process in seconds
59  /// Long_t fMemResident; // resident memory used by this process in KB
60  /// Long_t fMemVirtual; // virtual memory used by this process in KB
61  /// }
62  std::vector<ProcInfo_t> fEventMemory;
63 };
64 #endif
65 
void LogMemory()
Definition: MemoryUsage.cxx:18
bool operator()(ProcInfo_t lhs, ProcInfo_t rhs)
Definition: MemoryUsage.hxx:24
bool operator()(ProcInfo_t lhs, ProcInfo_t rhs)
Definition: MemoryUsage.hxx:16
MemoryUsage()
Construct the memory usage class with logging disabled.
Definition: MemoryUsage.cxx:7
void Enable(bool enable=true)
Definition: MemoryUsage.cxx:14
void Write()
Write histograms of the memory usage to the output file.
Definition: MemoryUsage.cxx:27