profilingTrigger.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-2024 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::profilingTrigger
29 
30 Description
31  Triggers for starting/stopping code profiling.
32 
33 SourceFiles
34  profilingTrigger.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_profilingTrigger_H
39 #define Foam_profilingTrigger_H
40 
41 #include <string>
42 
43 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 
45 namespace Foam
46 {
47 
48 // Forward Declarations
49 class profilingInformation;
50 
51 /*---------------------------------------------------------------------------*\
52  Class profilingTrigger Declaration
53 \*---------------------------------------------------------------------------*/
54 
55 class profilingTrigger
56 {
57  // Private Data Members
58 
59  //- The profiling information
61 
62 
63  // Private Member Functions
64 
65  //- True if any profiling hooks are possible
66  static bool possible() noexcept;
67 
68  //- Helper: string concatenation (no-op)
69  static inline void string_concat(std::string& output) {}
70 
71  //- Helper: string concatenation (loop)
72  template<class StringType, class... StringArgs>
73  static inline void string_concat
74  (
75  std::string& output,
76  const StringType& str,
77  StringArgs&&... args
78  )
79  {
80  output += str;
81  string_concat(output, std::forward<StringArgs>(args)...);
82  }
83 
84  //- Enter profiling section
85  void enter(const std::string& name);
86 
87  //- Trigger entering of profiling section
88  template<class... StringArgs>
89  void trigger(std::string name, StringArgs&&... args)
90  {
91  string_concat(name, std::forward<StringArgs>(args)...);
92  // No checking for empty name (should not occur)
93  enter(name);
94  }
95 
96 
97 public:
98 
99  // Generated Methods
100 
101  //- No copy construct
102  profilingTrigger(const profilingTrigger&) = delete;
103 
104  //- No copy assignment
105  void operator=(const profilingTrigger&) = delete;
106 
107 
108  // Constructors
109 
110  //- Default construct, no profiling trigger
111  constexpr profilingTrigger() noexcept : ptr_(nullptr) {}
112 
113  //- Start profiling section (if profiling is active)
114  //- with given description.
115  // The description is generated by string concatenation of the
116  // parameters.
117  // Descriptions beginning with 'application::' are reserved for
118  // internal use.
119  template<class... StringArgs>
120  explicit profilingTrigger(StringArgs&&... description)
121  :
122  ptr_(nullptr)
123  {
124  if (possible())
125  {
126  // Delay string concatenation until actually needed
127  trigger(std::forward<StringArgs>(description)...);
128  }
129  }
130 
131 
132  //- Destructor, calls stop()
134 
135 
136  // Member Functions
137 
138  //- True if the triggered profiling section is active
139  bool running() const noexcept;
140 
141  //- Stop triggered profiling section
142  void stop();
143 };
144 
145 
146 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
147 
148 } // End namespace Foam
149 
150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151 
152 // Macros
153 
154 //- Define profiling trigger with specified name and description string.
155 //- The description is generated by string concatenation.
156 // \sa endProfiling
157 #define addProfiling(Name, ...) \
158  ::Foam::profilingTrigger profilingTriggerFor##Name(__VA_ARGS__)
159 
160 //- Remove profiling with specified name
161 // \sa addProfiling
162 #define endProfiling(Name) profilingTriggerFor##Name.stop()
163 
164 //- Define profiling trigger with specified name and description
165 //- corresponding to the compiler-defined function name string
166 // \sa addProfiling
167 #ifdef __GNUC__
168 #define addProfilingInFunction(Name) \
169  ::Foam::profilingTrigger profilingTriggerFor##Name(__PRETTY_FUNCTION__)
170 #else
171 #define addProfilingInFunction(Name) \
172  ::Foam::profilingTrigger profilingTriggerFor##Name(__func__)
173 #endif
174 
175 
176 #endif
177 
178 // ************************************************************************* //
bool running() const noexcept
True if the triggered profiling section is active.
~profilingTrigger()
Destructor, calls stop()
Triggers for starting/stopping code profiling.
constexpr profilingTrigger() noexcept
Default construct, no profiling trigger.
void operator=(const profilingTrigger &)=delete
No copy assignment.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
void stop()
Stop triggered profiling section.
const direction noexcept
Definition: Scalar.H:258
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
Foam::argList args(argc, argv)
Code profiling information in terms of time spent, number of calls etc.
Namespace for OpenFOAM.