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 the 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 
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
66 
67 namespace Foam
68 {
69 
70 // Forward declaration of classes
71 class Ostream;
72 class cpuInfo;
73 class memInfo;
74 class profilingSysInfo;
75 
76 /*---------------------------------------------------------------------------*\
77  Class profiling Declaration
78 \*---------------------------------------------------------------------------*/
79 
80 class profiling
81 :
82  public IOdictionary
83 {
84 public:
85 
86  // Public Typedefs
87 
89  typedef profilingTrigger Trigger;
90 
91  // Static data members
92 
93  //- Flag if profiling is allowed
94  static int allowed;
95 
96 private:
97 
98  // Private classes, typedefs
99 
100  typedef profilingSysInfo sysInfo;
101 
102 
103  // Private Static Data Members
104 
105  //- Only one global object is possible
106  static profiling* singleton_;
107 
108 
109  // Private Data Members
110 
111  //- The owner of the profiling
112  const Time& owner_;
113 
114  //- Storage of profiling information
116 
117  //- Parent/child relationships for lookup purposes
119 
120  //- LIFO stack of profiling information
122 
123  //- LIFO stack of clock values
125 
126  //- General system information (optional)
127  sysInfo* sysInfo_;
128 
129  //- CPU-Information (optional)
130  cpuInfo* cpuInfo_;
131 
132  //- MEM-Information (optional)
133  memInfo* memInfo_;
134 
135 
136  // Private Member Functions
137 
138  //- No copy construct
139  profiling(const profiling&) = delete;
140 
141  //- No copy assignment
142  void operator=(const profiling&) = delete;
143 
144 
145 protected:
146 
147  // Friendship
148 
149  friend class profilingTrigger;
150  friend class Time;
151 
152 
153  // Constructors
154 
155  //- Construct IO object, everything enabled
156  profiling(const IOobject& io, const Time& owner);
157 
158  //- Construct IO object with finer control over behaviour
159  profiling
160  (
161  const dictionary& dict,
162  const IOobject& io,
163  const Time& owner
164  );
165 
166 
167  //- Destructor
168  ~profiling();
169 
170 
171  // Protected Member Functions
172 
173  //- Clear all profiling and restart with new profiling
174  // \return pointer to stored information element
175  Information* create(const zero);
176 
177  //- Get or create named profiling information element with the
178  //- specified parent.
179  // \return pointer to stored information element
181  (
183  const string& descr
184  );
185 
186  //- Add to stack of active information and begin timer datum
187  void beginTimer(Information* info);
188 
189  //- Remove from stack of active information and update elapsed time
190  // \return pointer to profiling information element (for reference)
192 
193 
194  // Static control elements
195 
196  //- Singleton to initialize profiling pool, everything enabled
197  static void initialize
198  (
199  const IOobject& ioObj,
200  const Time& owner
201  );
202 
203  //- Singleton to initialize profiling pool with finer control
204  static void initialize
205  (
206  const dictionary& dict,
207  const IOobject& ioObj,
208  const Time& owner
209  );
210 
211  //- Stop profiling, cleanup pool if possible
212  static void stop(const Time& owner);
213 
214  //- Existing or new element on pool, add to stack.
215  // Returns nullptr if profiling has not been initialized
216  static profilingInformation* New(const string& descr);
217 
218  //- Remove the information from the top of the stack
219  static void unstack(const profilingInformation* info);
220 
221 public:
222 
223  // Static Member Functions
224 
225  //- True if profiling is allowed and is active
226  static bool active();
227 
228  //- Disallow profiling by forcing the InfoSwitch off.
229  static void disable();
230 
231  //- Print profiling information to specified output
232  // Forwards to writeData member of top-level object
233  static bool print(Ostream& os);
234 
235  //- Write profiling information now
236  static bool writeNow();
237 
238 
239  // Member Functions
240 
241  //- The owner of the profiling
242  const Time& owner() const;
243 
244  //- The size of the current stack
245  label size() const noexcept;
246 
247  //- writeData member function required by regIOobject
248  virtual bool writeData(Ostream& os) const;
249 
250  //- Write as uncompressed ASCII
251  virtual bool writeObject
252  (
253  IOstreamOption /*ignore*/,
254  const bool valid
255  ) const;
256 };
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 } // End namespace Foam
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 
264 #endif
265 
266 // ************************************************************************* //
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:104
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::allowed
static int allowed
Flag if profiling is allowed.
Definition: profiling.H:93
Foam::PtrDynList
A dynamically resizable PtrList with allocation management.
Definition: PtrDynList.H:53
Foam::profiling::create
Information * create(const zero)
Clear all profiling and restart with new profiling.
Definition: profiling.C:45
Foam::IOobject::info
InfoProxy< IOobject > info() const
Return info proxy.
Definition: IOobject.H:532
Foam::profiling::stop
static void stop(const Time &owner)
Stop profiling, cleanup pool if possible.
Definition: profiling.C:173
Foam::profilingInformation
Code profiling information in terms of time spent, number of calls etc.
Definition: profilingInformation.H:60
Foam::profiling::active
static bool active()
True if profiling is allowed and is active.
Definition: profiling.C:112
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::dictionary::parent
const dictionary & parent() const
Return the parent dictionary.
Definition: dictionary.H:472
Foam::profiling::owner
const Time & owner() const
The owner of the profiling.
Definition: profiling.C:307
Foam::profiling::endTimer
Information * endTimer()
Remove from stack of active information and update elapsed time.
Definition: profiling.C:98
PtrDynList.H
Foam::profiling::~profiling
~profiling()
Destructor.
Definition: profiling.C:292
Foam::cpuInfo
General CPU characteristics.
Definition: cpuInfo.H:59
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::profilingSysInfo
General system information useful for profiling.
Definition: profilingSysInfo.H:52
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::memInfo
Memory usage information for the current process, and the system memory that is free.
Definition: memInfo.H:62
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::profiling::disable
static void disable()
Disallow profiling by forcing the InfoSwitch off.
Definition: profiling.C:118
Foam::profiling::unstack
static void unstack(const profilingInformation *info)
Remove the information from the top of the stack.
Definition: profiling.C:208
Foam::profiling::beginTimer
void beginTimer(Information *info)
Add to stack of active information and begin timer datum.
Definition: profiling.C:90
Foam::profiling::initialize
static void initialize(const IOobject &ioObj, const Time &owner)
Singleton to initialize profiling pool, everything enabled.
Definition: profiling.C:147
Foam::profiling::writeData
virtual bool writeData(Ostream &os) const
writeData member function required by regIOobject
Definition: profiling.C:319
IOdictionary.H
Time.H
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:183
Foam::profiling::size
label size() const noexcept
The size of the current stack.
Definition: profiling.C:313
Foam::profiling::print
static bool print(Ostream &os)
Print profiling information to specified output.
Definition: profiling.C:124
Foam::profiling::Trigger
profilingTrigger Trigger
Definition: profiling.H:88
DynamicList.H
Foam::profiling
Code profiling.
Definition: profiling.H:79
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:87
profilingTrigger.H
clockTime.H
Foam::profiling::writeObject
virtual bool writeObject(IOstreamOption, const bool valid) const
Write as uncompressed ASCII.
Definition: profiling.C:395
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62
Foam::profiling::writeNow
static bool writeNow()
Write profiling information now.
Definition: profiling.C:135