profiling.H 12 KB
Newer Older
1
2
3
4
5
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2009-2016 Bernhard Gschaider
mark's avatar
mark committed
6
     \\/     M anipulation  | Copyright (C) 2016-2107 OpenCFD Ltd.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

Class
mark's avatar
mark committed
25
    Foam::profiling
26
27
28
29

Description
    Code profiling.

30
31
    This is typically activated from within the system/controlDict as follows
    (defaults shown):
32
33
34
    \code
        profiling
        {
35
36
37
38
            active      true;
            cpuInfo     true;
            memInfo     false;
            sysInfo     true;
39
40
41
42
43
44
45
46
        }
    \endcode
    or simply using all defaults:
    \code
        profiling
        {}
    \endcode

47
SourceFiles
mark's avatar
mark committed
48
    profiling.C
49
50
51

\*---------------------------------------------------------------------------*/

mark's avatar
mark committed
52
53
#ifndef profiling_H
#define profiling_H
54
55
56
57
58
59
60

#include "HashPtrTable.H"
#include "LIFOStack.H"
#include "Map.H"
#include "Time.H"
#include "clockTime.H"

61
62
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

63
64
65
66
67
namespace Foam
{

// Forward declaration of classes
class Ostream;
68
69
70
class dictionary;
class cpuInfo;
class memInfo;
71
72

/*---------------------------------------------------------------------------*\
mark's avatar
mark committed
73
                          Class profiling Declaration
74
75
\*---------------------------------------------------------------------------*/

mark's avatar
mark committed
76
class profiling
77
78
79
80
81
82
83
84
85
:
    public regIOobject
{
public:

    // Forward declarations of components

        class Information;
        class Trigger;
86
        class sysInfo;
87
88
89
90
91
92

private:

    // Private Static Data Members

        //- Only one global pool object is possible
mark's avatar
mark committed
93
        static profiling *pool_;
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116


    // Private Data Members

        typedef HashPtrTable<Information, string> StorageContainer;
        typedef LIFOStack<Information*> StackContainer;


        //- The owner of the profiling
        const Time& owner_;

        //- A global timer for the profiling
        clockTime clockTime_;

        //- Storage of profiling information
        StorageContainer hash_;

        //- Local stack of profiling information
        StackContainer stack_;

        //- Note the timers (by Id) for the correct stack-output
        Map<clockTime*> timers_;

117
118
119
120
121
122
123
124
125
        //- General system information (optional)
        sysInfo* sysInfo_;

        //- CPU-Information (optional)
        cpuInfo* cpuInfo_;

        //- MEM-Information (optional)
        memInfo* memInfo_;

126
127
128
129

    // Private Member Functions

        //- Disallow default bitwise copy construct
mark's avatar
mark committed
130
        profiling(const profiling&) = delete;
131
132

        //- Disallow default bitwise assignment
mark's avatar
mark committed
133
        void operator=(const profiling&) = delete;
134
135
136
137
138
139
140
141
142
143
144


protected:

    // Friendship

        friend class Time;


    // Constructors

145
        //- Construct IO object, everything enabled
mark's avatar
mark committed
146
        profiling(const IOobject&, const Time&);
147

148
        //- Construct IO object with finer control over behaviour
mark's avatar
mark committed
149
        profiling(const dictionary&, const IOobject&, const Time&);
150

151
152

    //- Destructor
mark's avatar
mark committed
153
    ~profiling();
154
155
156
157
158
159
160
161
162
163
164
165
166
167


    // Protected Member Functions

        //- Find profiling information element or null on failure
        Information* find(const string& name);

        //- Add to hashed storage,
        //  returns pointer to newly stored element for chaining
        Information* store(Information*);

        //- Add to stack and set timer lookup (based on Id)
        void push(Information*, clockTime& timer);

168
169
170
171
        //- Remove from stack and remove timer lookup (based on Id).
        //  Returns pointer to profiling information element
        Information* pop();

172
173
174

    // Static control elements

175
        //- Singleton to initialize profiling pool, everything enabled
176
177
        static void initialize(const IOobject&, const Time&);

178
179
180
        //- Singleton to initialize profiling pool with finer control
        static void initialize(const dictionary&, const IOobject&, const Time&);

181
182
183
        //- Stop profiling, cleanup pool if possible
        static void stop(const Time&);

184
        //- Existing or new element on pool, add to stack.
185
186
187
188
189
190
191
192
        //  Returns null if profiling has not been initialized
        static Information* New(const string& name, clockTime& timer);

        //- Remove the information from the top of the stack
        static void unstack(const Information*);

public:

193
    // Static Member Functions
194

195
196
        //- True if profiling is active
        static bool active();
197

198
199
200
201
        //- Print profiling information to specified output
        //  Forwards to writeData member of top-level object
        static bool print(Ostream&);

202
203
        //- Write profiling information now
        static bool writeNow();
204

205
206
207

    // Member Functions

208
209
        //- The owner of the profiling
        const Time& owner() const;
210

211
212
        //- The size of the current stack
        Foam::label size() const;
213
214
215
216

        //- writeData member function required by regIOobject
        virtual bool writeData(Ostream&) const;

217
218
219
220
221
222
223
224
        //- Write as uncompressed ASCII, using given format
        virtual bool writeObject
        (
            IOstream::streamFormat ignoreAlwaysASCII,
            IOstream::versionNumber ver,
            IOstream::compressionType ignoreAlwaysUncompressed
        ) const;

225
226
227
};


mark's avatar
mark committed
228
229
230
231
// Forward declaration of friend functions and operators
Ostream& operator<<(Ostream& os, const profiling::Information& info);


232
/*---------------------------------------------------------------------------*\
mark's avatar
mark committed
233
                   Class profiling::Information Declaration
234
235
\*---------------------------------------------------------------------------*/

mark's avatar
mark committed
236
class profiling::Information
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{
    // Private Static Data Members

        //- Counter to generate the ids
        static label nextId_;

        //- get a new ID and update the counter
        static label getNextId();

        //- raise the next possible ID (to avoid ID-clashes during reading)
        static void raiseID(label maxVal);


    // Private Data Members

        //- Unique id to identify it
        const label id_;

        //- What this timer does
        const string description_;

        //- Pointer to the parent object (or self for top-level)
        Information* parent_;

        //- Nr of times this was called
262
        long calls_;
263
264
265
266
267
268
269

        //- Total time spent
        scalar totalTime_;

        //- Time spent in children
        scalar childTime_;

270
271
272
273
        //- Max memory usage on call.
        //  Only valid when the calling profiling has memInfo active.
        mutable int maxMem_;

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        //- Is this information currently on the stack?
        mutable bool onStack_;


    // Private Member Functions

        //- Disallow default bitwise copy construct
        Information(const Information&) = delete;

        //- Disallow default bitwise assignment
        void operator=(const Information&) = delete;


protected:

    // Friendship

mark's avatar
mark committed
291
        friend class profiling;
292
293
294
295
296
297
298
299
300
301
302


    // Constructors

        //- Construct null - only the master-element
        Information();


    // Member Functions

        //- Mark as being on the stack
303
        void push() const;
304
305

        //- Mark as being off the stack
306
        void pop() const;
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371


        //- Write the profiling times, optionally with additional values
        //  Use dictionary format.
        Ostream& write
        (
            Ostream& os,
            const bool offset = false,
            const scalar& elapsedTime = 0,
            const scalar& childTime = 0
        ) const;

public:


    // Constructors

        //- Construct from components
        Information(Information* parent, const string& descr);


    //- Destructor
    ~Information();


    // Member Functions

    // Access

        inline label id() const
        {
            return id_;
        }


        inline const string& description() const
        {
            return description_;
        }


        inline Information& parent() const
        {
            return *parent_;
        }


        inline label calls() const
        {
            return calls_;
        }


        inline const scalar& totalTime() const
        {
            return totalTime_;
        }


        inline const scalar& childTime() const
        {
            return childTime_;
        }


372
373
374
375
376
377
        inline int maxMem() const
        {
            return maxMem_;
        }


378
379
380
381
382
383
384
385
386
387
388
389
390
391
        inline bool onStack() const
        {
            return onStack_;
        }


    // Edit

        //- Update it with a new timing information
        void update(const scalar& elapsedTime);


    // IOstream Operators

mark's avatar
mark committed
392
        friend Ostream& operator<<(Ostream& os, const Information& info);
393
394
395
396
397

};


/*---------------------------------------------------------------------------*\
mark's avatar
mark committed
398
                      Class profiling::Trigger Declaration
399
400
\*---------------------------------------------------------------------------*/

mark's avatar
mark committed
401
class profiling::Trigger
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
{
    // Private Data Members

        //- The timer for the profiling information
        clockTime clock_;

        //- The profiling information
        Information *ptr_;


    // Private Member Functions

        //- Disallow default bitwise copy construct
        Trigger(const Trigger&) = delete;

        //- Disallow default bitwise assignment
        void operator=(const Trigger&) = delete;


public:

    // Constructors

425
426
427
        //- Construct profiling with given description.
        //  Descriptions beginning with 'application::' are reserved for
        //  internal use.
428
429
        Trigger(const char* name);

430
431
432
        //- Construct profiling with given description.
        //  Descriptions beginning with 'application::' are reserved for
        //  internal use.
433
434
435
436
437
438
439
440
441
442
443
444
        Trigger(const string& name);


    //- Destructor
    ~Trigger();


    // Member Functions

    // Access

        //- True if the triggered profiling is active
445
        bool running() const;
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465


    // Edit

        //- Stop triggered profiling
        void stop();

};


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

// Macros

//- Define profiling with specified name and description string
//  This is required if the description contains space, colons etc.
466
467
//  \sa addProfiling0
//  \sa endProfiling
468
#define addProfiling(name,descr)                                              \
mark's avatar
mark committed
469
470
471
472
473
474
    ::Foam::profiling::Trigger  profilingTriggerFor##name(descr)

//- Define profiling with specified name and description correspond to the name
//  \sa addProfiling
//  \sa endProfiling
#define addProfiling0(name)                                                    \
475
    ::Foam::profiling::Trigger  profilingTriggerFor##name(#name)
476

477
478
479
480
481
482
483
484
485
486
487
//- Define profiling with specified name and description correspond to the
//  compiler-defined function name string:
//  \sa addProfiling
//  \sa endProfiling
#ifdef __GNUC__
    #define addProfilingInFunction(name)                                       \
    ::Foam::profiling::Trigger  profilingTriggerFor##name(__PRETTY_FUNCTION__)
#else
    #define addProfilingInFunction(name)                                       \
    ::Foam::profiling::Trigger  profilingTriggerFor##name(__func__)
#endif
488
489

//- Remove profiling with specified name
490
491
//  \sa addProfiling
//  \sa addProfiling0
492
493
494
495
496
497
#define endProfiling(name)      profilingTriggerFor##name.stop()


#endif

// ************************************************************************* //