SENSEI
A frame work for generic in situ analytics
Profiler.h
1 #ifndef sensei_Profiler_h
2 #define sensei_Profiler_h
3 
4 #include "senseiConfig.h"
5 #define SENSEI_HAS_MPI
6 
7 #include <string>
8 #include <thread>
9 #include <ostream>
10 #include <mpi.h>
11 
12 namespace sensei
13 {
14 
15 // A class containing methods managing memory and time profiling
16 // Each timed event logs rank, event name, start and end time, and
17 // duration.
18 class SENSEI_EXPORT Profiler
19 {
20 public:
21  // Initialize logging from environment variables, and/or the timer
22  // API below. This is a collective call with respect to the timer's
23  // communicator.
24  //
25  // If found in the environment the following variable override the
26  // the current settings
27  //
28  // PROFILER_ENABLE : bit mask turns on or off logging,
29  // 0x01 -- event profiling enabled
30  // 0x02 -- memory profiling enabled
31  // PROFILER_LOG_FILE : path to write timer log to
32  // MEMPROF_LOG_FILE : path to write memory profiler log to
33  // MEMPROF_INTERVAL : number of seconds between memory recordings
34  //
35  static int Initialize();
36 
37  // Finalize the log. this is where logs are written and cleanup occurs.
38  // All processes in the communicator must call, and it must be called
39  // prior to MPI_Finalize.
40  static int Finalize();
41 
42  // this can occur after MPI_Finalize. It should only be called by rank 0.
43  // Any remaining events will be appeneded to the log file. This is necessary
44  // to time MPI_Initialize/Finalize and log associated I/O.
45  static int Flush();
46 
47  // Sets the communicator for MPI calls. This must be called prior to
48  // initialization.
49  // default value: MPI_COMM_NULL
50  static void SetCommunicator(MPI_Comm comm);
51 
52  // Sets the path to write the timer log to
53  // overriden by PROFILER_LOG_FILE environment variable
54  // default value; Timer.csv
55  static void SetTimerLogFile(const std::string &fileName);
56 
57  // Sets the path to write the timer log to
58  // overriden by MEMPROF_LOG_FILE environment variable
59  // default value: MemProfLog.csv
60  static void SetMemProfLogFile(const std::string &fileName);
61 
62  // Sets the number of seconds in between memory use recordings
63  // overriden by MEMPROF_INTERVAL environment variable.
64  static void SetMemProfInterval(int interval);
65 
66  // Enable/Disable logging. Overriden by PROFILER_ENABLE environment
67  // variable. In the default format a CSV file is generated capturing each
68  // ranks timer events. default value: disabled
69  static void Enable(int arg = 0x03);
70  static void Disable();
71 
72  // return true if loggin is enabled.
73  static bool Enabled();
74 
75  // @brief Log start of an event.
76  //
77  // This marks the beginning of a event that must be logged. The @arg
78  // eventname must match when calling endEvent() to mark the end of the
79  // event.
80  static int StartEvent(const char *eventname, long long nbytes=-1ll);
81 
82  // @brief Log end of a log-able event.
83  //
84  // This marks the end of a event that must be logged. The @arg eventname
85  // must match when calling endEvent() to mark the end of the event.
86  static int EndEvent(const char *eventname, long long nbytes=-1ll);
87 
88  // write contents of the string to the file.
89  static int WriteCStdio(const char *fileName, const char *mode,
90  const std::string &str);
91 
92  // write contents of the string to the file in rank order
93  // the file is truncated first or created
94  static int WriteMpiIo(MPI_Comm comm, const char *fileName,
95  const std::string &str);
96 
97  // checks to see if all active events have been ended.
98  // will report errors if not
99  static int Validate();
100 
101  // setnd the current contents of the log to the stream
102  static int ToStream(std::ostream &os);
103 };
104 
105 // TimeEvent -- A helper class that times it's life.
106 // A timer event is created that starts at the object's construction and ends
107 // at its destruction. The pointer to the event name must be valid throughout
108 // the objects life.
109 template <int bufferSize>
111 {
112 public:
113  // logs an event named
114  // <className>::<method> port=<p>
115  TimeEvent(const char *className,
116  const char *method, int port) : Eventname(Buffer)
117  {
118  snprintf(Buffer, bufferSize, "%s::%s port=%d",
119  className, method, port);
120  Profiler::StartEvent(Eventname);
121  }
122 
123  // logs an event named
124  // <className>::<method>
125  TimeEvent(const char *className,
126  int nThreads, int nReqs) : Eventname(Buffer)
127  {
128  snprintf(Buffer, bufferSize,
129  "%s threadPool process nThreads=%d nReqs=%d",
130  className, nThreads, nReqs);
131  Profiler::StartEvent(Eventname);
132  }
133 
134 
135  // logs an event named:
136  // <className>::<method>
137  TimeEvent(const char *className,
138  const char *method) : Eventname(Buffer)
139  {
140  Buffer[0] = '\0';
141  strcat(Buffer, className);
142  strcat(Buffer, method);
143  Profiler::StartEvent(Eventname);
144  }
145 
146  // logs an event named:
147  // <name>
148  TimeEvent(const char *name) : Eventname(name)
149  { Profiler::StartEvent(name); }
150 
151  ~TimeEvent()
152  { Profiler::EndEvent(this->Eventname); }
153 
154 private:
155  char Buffer[bufferSize];
156  const char *Eventname;
157 };
158 
159 }
160 
161 #endif
int Initialize(MPI_Comm comm, const std::string &fileName, InTransitDataAdaptor *&dataAdaptor)
Creates a sensei::ConfigurableAnalysis adaptor and sensei::InTransitDataAdaptor based on a SENSEI XML...
Definition: Profiler.h:110
Definition: Profiler.h:18
SENSEI.
Definition: ADIOS2AnalysisAdaptor.h:27