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-2023 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 Foam_profiling_H
56 #define Foam_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 cpuInfo;
73 class memInfo;
74 class profilingInformation;
75 class profilingSysInfo;
76 class profilingTrigger;
77 
78 /*---------------------------------------------------------------------------*\
79  Class profiling Declaration
80 \*---------------------------------------------------------------------------*/
81 
82 class profiling
83 :
84  public IOdictionary
85 {
86 public:
87 
88  // Public Typedefs
89 
91  typedef profilingTrigger Trigger;
92 
93 
94  // Static Data Members
95 
96  //- Flag if profiling is allowed
97  static int allowed;
98 
99 private:
100 
101  // Private Typedefs
102 
103  typedef profilingSysInfo sysInfo;
104 
105 
106  // Private Static Data Members
107 
108  //- Only one global object is possible
109  static std::unique_ptr<profiling> singleton_;
110 
111 
112  // Private Data Members
113 
114  //- The owner of the profiling
115  const Time& owner_;
116 
117  //- Storage of profiling information (memory management)
119 
120  //- Parent/child relationships for lookup purposes
122 
123  //- LIFO stack of profiling information
125 
126  //- LIFO stack of clock values
128 
129  //- General system information (optional)
130  std::unique_ptr<sysInfo> sysInfo_;
131 
132  //- CPU-Information (optional)
133  std::unique_ptr<cpuInfo> cpuInfo_;
134 
135  //- MEM-Information (optional)
136  std::unique_ptr<memInfo> memInfo_;
137 
138 
139 protected:
140 
141  // Friendship
142 
143  friend class profilingTrigger;
144  friend class Time;
145 
146 
147  // Generated Methods
148 
149  //- No copy construct
150  profiling(const profiling&) = delete;
151 
152  //- No copy assignment
153  void operator=(const profiling&) = delete;
154 
155 
156  // Constructors
157 
158  //- Construct IO object, everything enabled
160  (
161  const IOobject& io,
162  const Time& owner,
163  const bool allEnabled = true
164  );
165 
166  //- Construct IO object with finer control over behaviour
167  profiling
168  (
169  const dictionary& dict,
170  const IOobject& io,
171  const Time& owner
172  );
173 
174 
175  // Protected Member Functions
176 
177  //- Clear all profiling and restart with new profiling
178  // \return pointer to stored information element
179  Information* create();
180 
181  //- Get or create named profiling information element with the
182  //- specified parent.
183  // \return pointer to stored information element
185  (
187  const std::string& descr
188  );
189 
190  //- Add to stack of active information and begin timer datum
191  void beginTimer(Information* info);
192 
193  //- Remove from stack of active information and update elapsed time
194  // \return pointer to profiling information element (for reference)
196 
197 
198  // Static control elements
199 
200  //- Singleton to initialize profiling pool, everything enabled
201  static void initialize
202  (
203  const IOobject& ioObj,
204  const Time& owner
205  );
206 
207  //- Singleton to initialize profiling pool with finer control
208  static void initialize
209  (
210  const dictionary& dict,
211  const IOobject& ioObj,
212  const Time& owner
213  );
214 
215  //- Stop profiling, cleanup pool if possible
216  static void stop(const Time& owner);
217 
218  //- Existing or new element on pool, add to stack.
219  // Returns nullptr if profiling has not been initialized
220  static profilingInformation* New(const std::string& descr);
221 
222  //- Remove the information from the top of the stack
223  static void unstack(const profilingInformation* info);
224 
225 
226 public:
227 
228  //- Destructor. Top-level clears the singleton.
229  ~profiling();
230 
231 
232  // Static Member Functions
233 
234  //- True if profiling is allowed and is active
235  static bool active() noexcept;
236 
237  //- Disallow profiling - turns the InfoSwitch off
238  static void disable() noexcept;
239 
240  //- Print profiling information to specified output
241  // Forwards to writeData member of top-level object
242  static bool print(Ostream& os);
243 
244  //- Write profiling information now
245  static bool writeNow();
246 
247 
248  // Member Functions
249 
250  //- The owner of the profiling
251  const Time& owner() const noexcept;
252 
253  //- The size of the current stack
254  label size() const noexcept;
255 
256  //- writeData member function required by regIOobject
257  virtual bool writeData(Ostream& os) const;
258 
259  //- Write as uncompressed ASCII
260  virtual bool writeObject
261  (
262  IOstreamOption /*ignore*/,
263  const bool writeOnProc
264  ) const;
265 };
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 } // End namespace Foam
270 
271 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
272 
273 #endif
274 
275 // ************************************************************************* //
label size() const noexcept
The size of the current stack.
Definition: profiling.C:298
dictionary dict
profilingTrigger Trigger
Definition: profiling.H:86
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
profilingInformation Information
Definition: profiling.H:85
static bool active() noexcept
True if profiling is allowed and is active.
Definition: profiling.C:107
static bool writeNow()
Write profiling information now.
Definition: profiling.C:130
Triggers for starting/stopping code profiling.
A simple container for options an IOstream can normally have.
Code profiling.
Definition: profiling.H:77
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
static void disable() noexcept
Disallow profiling - turns the InfoSwitch off.
Definition: profiling.C:113
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:50
static void unstack(const profilingInformation *info)
Remove the information from the top of the stack.
Definition: profiling.C:204
void beginTimer(Information *info)
Add to stack of active information and begin timer datum.
Definition: profiling.C:83
virtual bool writeData(Ostream &os) const
writeData member function required by regIOobject
Definition: profiling.C:304
void operator=(const profiling &)=delete
No copy assignment.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
const dictionary & parent() const noexcept
Return the parent dictionary.
Definition: dictionaryI.H:77
static void initialize(const IOobject &ioObj, const Time &owner)
Singleton to initialize profiling pool, everything enabled.
Definition: profiling.C:142
static int allowed
Flag if profiling is allowed.
Definition: profiling.H:94
static profilingInformation * New(const std::string &descr)
Existing or new element on pool, add to stack.
Definition: profiling.C:177
static void stop(const Time &owner)
Stop profiling, cleanup pool if possible.
Definition: profiling.C:168
InfoProxy< IOobject > info() const noexcept
Return info proxy, for printing information to a stream.
Definition: IOobject.H:955
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
virtual bool writeObject(IOstreamOption, const bool writeOnProc) const
Write as uncompressed ASCII.
Definition: profiling.C:372
static bool print(Ostream &os)
Print profiling information to specified output.
Definition: profiling.C:119
OBJstream os(runTime.globalPath()/outputName)
Information * endTimer()
Remove from stack of active information and update elapsed time.
Definition: profiling.C:91
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:48
~profiling()
Destructor. Top-level clears the singleton.
Definition: profiling.C:281
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
const Time & owner() const noexcept
The owner of the profiling.
Definition: profiling.C:292
Code profiling information in terms of time spent, number of calls etc.
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
profiling(const profiling &)=delete
No copy construct.
Namespace for OpenFOAM.
General system information useful for profiling.
Information * create()
Clear all profiling and restart with new profiling.
Definition: profiling.C:38