profiling.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2009-2016 Bernhard Gschaider
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::profiling
29 
30 Description
31  Code profiling.
32 
33  This is typically activated from within system/controlDict as follows
34  (defaults shown):
35  \code
36  profiling
37  {
38  active true;
39  cpuInfo false;
40  memInfo false;
41  sysInfo false;
42  }
43  \endcode
44  or simply using all defaults:
45  \code
46  profiling
47  {}
48  \endcode
49 
50 SourceFiles
51  profiling.C
52 
53 \*---------------------------------------------------------------------------*/
54 
55 #ifndef profiling_H
56 #define profiling_H
57 
58 #include "profilingTrigger.H"
59 #include "IOdictionary.H"
60 #include "DynamicList.H"
61 #include "PtrDynList.H"
62 #include "Time.H"
63 #include "clockTime.H"
64 #include <memory>
65 
66 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
67 
68 namespace Foam
69 {
70 
71 // Forward Declarations
72 class Ostream;
73 class cpuInfo;
74 class memInfo;
75 class profilingInformation;
76 class profilingSysInfo;
77 class profilingTrigger;
78 
79 /*---------------------------------------------------------------------------*\
80  Class profiling Declaration
81 \*---------------------------------------------------------------------------*/
82 
83 class profiling
84 :
85  public IOdictionary
86 {
87 public:
88 
89  // Public Typedefs
90 
92  typedef profilingTrigger Trigger;
93 
94 
95  // Static Data Members
96 
97  //- Flag if profiling is allowed
98  static int allowed;
99 
100 private:
101 
102  // Private Typedefs
103 
104  typedef profilingSysInfo sysInfo;
105 
106 
107  // Private Static Data Members
108 
109  //- Only one global object is possible
110  static std::unique_ptr<profiling> singleton_;
111 
112 
113  // Private Data Members
114 
115  //- The owner of the profiling
116  const Time& owner_;
117 
118  //- Storage of profiling information (memory management)
120 
121  //- Parent/child relationships for lookup purposes
123 
124  //- LIFO stack of profiling information
126 
127  //- LIFO stack of clock values
129 
130  //- General system information (optional)
131  std::unique_ptr<sysInfo> sysInfo_;
132 
133  //- CPU-Information (optional)
134  std::unique_ptr<cpuInfo> cpuInfo_;
135 
136  //- MEM-Information (optional)
137  std::unique_ptr<memInfo> memInfo_;
138 
139 
140  // Private Member Functions
141 
142  //- No copy construct
143  profiling(const profiling&) = delete;
144 
145  //- No copy assignment
146  void operator=(const profiling&) = delete;
147 
148 
149 protected:
150 
151  // Friendship
152 
153  friend class profilingTrigger;
154  friend class Time;
155 
156 
157  // Constructors
158 
159  //- Construct IO object, everything enabled
160  profiling
161  (
162  const IOobject& io,
163  const Time& owner,
164  const bool allEnabled = true
165  );
166 
167  //- Construct IO object with finer control over behaviour
168  profiling
169  (
170  const dictionary& dict,
171  const IOobject& io,
172  const Time& owner
173  );
174 
175 
176  // Protected Member Functions
177 
178  //- Clear all profiling and restart with new profiling
179  // \return pointer to stored information element
180  Information* create();
181 
182  //- Get or create named profiling information element with the
183  //- specified parent.
184  // \return pointer to stored information element
186  (
188  const string& descr
189  );
190 
191  //- Add to stack of active information and begin timer datum
192  void beginTimer(Information* info);
193 
194  //- Remove from stack of active information and update elapsed time
195  // \return pointer to profiling information element (for reference)
197 
198 
199  // Static control elements
200 
201  //- Singleton to initialize profiling pool, everything enabled
202  static void initialize
203  (
204  const IOobject& ioObj,
205  const Time& owner
206  );
207 
208  //- Singleton to initialize profiling pool with finer control
209  static void initialize
210  (
211  const dictionary& dict,
212  const IOobject& ioObj,
213  const Time& owner
214  );
215 
216  //- Stop profiling, cleanup pool if possible
217  static void stop(const Time& owner);
218 
219  //- Existing or new element on pool, add to stack.
220  // Returns nullptr if profiling has not been initialized
221  static profilingInformation* New(const string& descr);
222 
223  //- Remove the information from the top of the stack
224  static void unstack(const profilingInformation* info);
225 
226 
227 public:
228 
229  //- Destructor. Top-level clears the singleton.
230  ~profiling();
231 
232 
233  // Static Member Functions
234 
235  //- True if profiling is allowed and is active
236  static bool active();
237 
238  //- Disallow profiling by forcing the InfoSwitch off.
239  static void disable();
240 
241  //- Print profiling information to specified output
242  // Forwards to writeData member of top-level object
243  static bool print(Ostream& os);
244 
245  //- Write profiling information now
246  static bool writeNow();
247 
248 
249  // Member Functions
250 
251  //- The owner of the profiling
252  const Time& owner() const;
253 
254  //- The size of the current stack
255  label size() const noexcept;
256 
257  //- writeData member function required by regIOobject
258  virtual bool writeData(Ostream& os) const;
259 
260  //- Write as uncompressed ASCII
261  virtual bool writeObject
262  (
263  IOstreamOption /*ignore*/,
264  const bool valid
265  ) const;
266 };
267 
268 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
269 
270 } // End namespace Foam
271 
272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
273 
274 #endif
275 
276 // ************************************************************************* //
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:169
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::profiling::create
Information * create()
Clear all profiling and restart with new profiling.
Definition: profiling.C:44
Foam::profiling::allowed
static int allowed
Flag if profiling is allowed.
Definition: profiling.H:97
Foam::PtrDynList
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:54
Foam::IOobject::info
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:689
Foam::profiling::stop
static void stop(const Time &owner)
Stop profiling, cleanup pool if possible.
Definition: profiling.C:172
Foam::profilingInformation
Code profiling information in terms of time spent, number of calls etc.
Definition: profilingInformation.H:56
Foam::profiling::active
static bool active()
True if profiling is allowed and is active.
Definition: profiling.C:111
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::profiling::owner
const Time & owner() const
The owner of the profiling.
Definition: profiling.C:297
Foam::profiling::endTimer
Information * endTimer()
Remove from stack of active information and update elapsed time.
Definition: profiling.C:97
PtrDynList.H
Foam::profiling::~profiling
~profiling()
Destructor. Top-level clears the singleton.
Definition: profiling.C:286
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::profilingSysInfo
General system information useful for profiling.
Definition: profilingSysInfo.H:51
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::profiling::disable
static void disable()
Disallow profiling by forcing the InfoSwitch off.
Definition: profiling.C:117
Foam::profiling::unstack
static void unstack(const profilingInformation *info)
Remove the information from the top of the stack.
Definition: profiling.C:206
Foam::profiling::beginTimer
void beginTimer(Information *info)
Add to stack of active information and begin timer datum.
Definition: profiling.C:89
Foam::profiling::initialize
static void initialize(const IOobject &ioObj, const Time &owner)
Singleton to initialize profiling pool, everything enabled.
Definition: profiling.C:146
Foam::profiling::writeData
virtual bool writeData(Ostream &os) const
writeData member function required by regIOobject
Definition: profiling.C:309
IOdictionary.H
Time.H
Foam::dictionary::parent
const dictionary & parent() const noexcept
Return the parent dictionary.
Definition: dictionaryI.H:80
Foam::profilingTrigger
Triggers for starting/stopping code profiling.
Definition: profilingTrigger.H:54
Foam::profiling::New
static profilingInformation * New(const string &descr)
Existing or new element on pool, add to stack.
Definition: profiling.C:181
Foam::profiling::size
label size() const noexcept
The size of the current stack.
Definition: profiling.C:303
Foam::profiling::print
static bool print(Ostream &os)
Print profiling information to specified output.
Definition: profiling.C:123
Foam::profiling::Trigger
profilingTrigger Trigger
Definition: profiling.H:91
DynamicList.H
Foam::profiling
Code profiling.
Definition: profiling.H:82
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::profiling::Information
profilingInformation Information
Definition: profiling.H:90
profilingTrigger.H
clockTime.H
Foam::profiling::writeObject
virtual bool writeObject(IOstreamOption, const bool valid) const
Write as uncompressed ASCII.
Definition: profiling.C:385
Foam::profiling::writeNow
static bool writeNow()
Write profiling information now.
Definition: profiling.C:134