profiling.H 7.59 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
            active      true;
36
            cpuInfo     false;
37
            memInfo     false;
38
            sysInfo     false;
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
#include "profilingTrigger.H"
56
#include "HashPtrTable.H"
57
#include "Tuple2.H"
58
59
60
61
62
#include "LIFOStack.H"
#include "Map.H"
#include "Time.H"
#include "clockTime.H"

63
64
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

65
66
67
68
69
namespace Foam
{

// Forward declaration of classes
class Ostream;
70
71
class cpuInfo;
class memInfo;
72
class profilingSysInfo;
73
74

/*---------------------------------------------------------------------------*\
mark's avatar
mark committed
75
                          Class profiling Declaration
76
77
\*---------------------------------------------------------------------------*/

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

84
    // Public typedefs
85

86
87
        typedef profilingInformation Information;
        typedef profilingTrigger Trigger;
88

89
90
91
92
93
    // Static data members

        //- Flag if profiling is allowed
        static int allowed;

94
95
private:

96
    // Private classes, typedefs
97
98

        typedef profilingSysInfo sysInfo;
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

        //- Profile information lookup is qualified by parent id
        typedef Tuple2<string, label> Key;

        //- Hashing for information lookup
        class HashKey
        :
            public Hash<Key>
        {
        public:

            HashKey()
            {}

            //- Hash qualified by the parent id to avoid collisions
            unsigned operator()(const Key& key) const
            {
                return
                (
                    Hash<string>()(key.first())
                  + Hash<label>()(key.second())
                );
            }
        };

        typedef HashPtrTable<Information, Key, HashKey> StorageContainer;
125
126
127
        typedef LIFOStack<Information*> StackContainer;


128
129
130
    // Private Static Data Members

        //- Only one global pool object is possible
131
        static profiling* pool_;
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150


    // Private Data Members

        //- 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_;

151
152
153
154
155
156
157
158
159
        //- General system information (optional)
        sysInfo* sysInfo_;

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

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

160
161
162
163

    // Private Member Functions

        //- Disallow default bitwise copy construct
mark's avatar
mark committed
164
        profiling(const profiling&) = delete;
165
166

        //- Disallow default bitwise assignment
mark's avatar
mark committed
167
        void operator=(const profiling&) = delete;
168
169
170
171
172
173


protected:

    // Friendship

174
        friend class profilingTrigger;
175
176
177
178
179
        friend class Time;


    // Constructors

180
        //- Construct IO object, everything enabled
181
        profiling(const IOobject& io, const Time& owner);
182

183
        //- Construct IO object with finer control over behaviour
184
185
186
187
188
189
        profiling
        (
            const dictionary& dict,
            const IOobject& io,
            const Time& owner
        );
190

191
192

    //- Destructor
mark's avatar
mark committed
193
    ~profiling();
194
195
196
197


    // Protected Member Functions

198
199
200
        //- Find named profiling information element with specified parent.
        //  Return nullptr on failure.
        profilingInformation* find(const string& descr, const label parentId);
201
202

        //- Add to hashed storage,
203
        //  \return pointer to newly stored element for chaining
204
        profilingInformation* store(profilingInformation* info);
205
206

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

209
        //- Remove from stack and remove timer lookup (based on Id).
210
        //  \return pointer to profiling information element
211
        profilingInformation* pop();
212

213
214
215

    // Static control elements

216
        //- Singleton to initialize profiling pool, everything enabled
217
218
219
220
221
        static void initialize
        (
            const IOobject& ioObj,
            const Time& owner
        );
222

223
        //- Singleton to initialize profiling pool with finer control
224
225
226
227
228
229
        static void initialize
        (
            const dictionary& dict,
            const IOobject& ioObj,
            const Time& owner
        );
230

231
        //- Stop profiling, cleanup pool if possible
232
        static void stop(const Time& owner);
233

234
        //- Existing or new element on pool, add to stack.
235
        //  Returns null if profiling has not been initialized
236
237
238
239
240
        static profilingInformation* New
        (
            const string& descr,
            clockTime& timer
        );
241
242

        //- Remove the information from the top of the stack
243
        static void unstack(const profilingInformation* info);
244
245
246

public:

247
    // Static Member Functions
248

249
        //- True if profiling is allowed and is active
250
        static bool active();
251

252
253
254
        //- Disallow profiling by forcing the InfoSwitch off.
        static void disable();

255
256
        //- Print profiling information to specified output
        //  Forwards to writeData member of top-level object
257
        static bool print(Ostream& os);
258

259
260
        //- Write profiling information now
        static bool writeNow();
261

262
263
264

    // Member Functions

265
266
        //- The owner of the profiling
        const Time& owner() const;
267

268
        //- The size of the current stack
269
        label size() const;
270
271

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

274
275
276
277
278
        //- Write as uncompressed ASCII, using given format
        virtual bool writeObject
        (
            IOstream::streamFormat ignoreAlwaysASCII,
            IOstream::versionNumber ver,
279
280
            IOstream::compressionType ignoreAlwaysUncompressed,
            const bool valid
281
282
        ) const;

283
284
285
286
287
288
289
290
291
292
293
};

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

} // End namespace Foam

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

#endif

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