profilingTrigger.C
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  Copyright (C) 2023 Josep Pocurull Serra, Barcelona Supercomputing Center
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "profiling.H"
31 #include "profilingTrigger.H"
32 #include "profilingInformation.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 // Extrae profiling hooks
37 // ----------------------
38 // https://tools.bsc.es/extrae
39 // ----------------------
40 
41 #ifdef HAVE_EXTRAE
42 #include <map>
43 #include <utility>
44 
45 // Weak functions for Extrae C api
46 extern "C"
47 {
48  typedef unsigned extrae_type_t;
49  typedef unsigned long long extrae_value_t;
50 
51  // Adds to the Paraver Configuration File human readable information
52  // regarding type and its values.
53  void Extrae_define_event_type
54  (
55  extrae_type_t *type,
56  char *type_description,
57  unsigned *nvalues,
58  extrae_value_t *values,
59  char **values_description
60  ) __attribute__((weak));
61 
62  // Adds a single timestamped event into the tracefile
63  void Extrae_event
64  (
65  extrae_type_t type,
66  extrae_value_t value
67  ) __attribute__((weak));
68 
69 } // End extern "C"
70 
71 
72 // Descriptor for the events
73 static char myExtrae_description[] = "OpenFOAM Extrae Profiling";
74 
75 static void open_extrae_region(const std::string& name)
76 {
77  // Event history (by name) of profiling triggers
78  static std::map<std::string, extrae_value_t> event_history;
79 
80  // Scratch space for transcribing map -> flat lists
81  static Foam::DynamicList<char*> eventNames;
82  static Foam::DynamicList<extrae_value_t> eventValues;
83 
84 
85  if (event_history.empty())
86  {
87  event_history.insert(std::make_pair("End", 0));
88  }
89 
90  extrae_type_t event_type = 7000;
91  extrae_value_t event_name;
92 
93  // Check if there is already an event with that name
94  auto iter = event_history.find(name);
95  if (iter != event_history.end())
96  {
97  event_name = iter->second;
98  }
99  else
100  {
101  // Update extrae defined events
102 
103  event_name = static_cast<extrae_value_t>(event_history.size());
104  event_history.insert(std::make_pair(name, event_name));
105 
106  unsigned numEvents = event_history.size();
107 
108  const Foam::label len(numEvents);
109 
110  eventNames.resize_nocopy(len);
111  eventValues.resize_nocopy(len);
112 
113  Foam::label i = 0;
114  for (const auto& iter : event_history)
115  {
116  eventNames[i] = const_cast<char*>(iter.first.data());
117  eventValues[i] = iter.second;
118  ++i;
119  }
120 
121  Extrae_define_event_type
122  (
123  &event_type,
124  myExtrae_description,
125  &numEvents,
126  eventValues.data(),
127  eventNames.data()
128  );
129  }
130 
131  Extrae_event(event_type, event_name);
132 }
133 
134 
135 static void close_extrae_region()
136 {
137  Extrae_event(7000, 0);
138 }
139 
140 #endif /* HAVE_EXTRAE */
141 
142 
143 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
146 :
147  ptr_(nullptr)
148 {}
149 
152 :
154 {}
155 
156 
158 :
159  ptr_(profiling::New(name))
160 {
161  #ifdef HAVE_EXTRAE
162  if (Extrae_event) open_extrae_region(std::string(name));
163  #endif
164 }
165 
166 
167 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
168 
170 {
171  stop();
172 }
173 
174 
175 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
178 {
179  return ptr_;
180 }
181 
182 
184 {
185  #ifdef HAVE_EXTRAE
186  if (Extrae_event) close_extrae_region();
187  #endif
188 
189  if (ptr_)
190  {
191  // profiling info pointer managed by pool storage, so no delete here
192  profiling::unstack(ptr_);
193  }
194 
195  ptr_ = nullptr;
196 }
197 
198 
199 // ************************************************************************* //
type
Types of root.
Definition: Roots.H:52
bool running() const noexcept
True if the triggered profiling is active.
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:272
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tf1, const word &name, const dimensionSet &dimensions, const bool initCopy=false)
Global function forwards to reuseTmpDimensionedField::New.
~profilingTrigger()
Destructor.
Triggers for starting/stopping code profiling.
Code profiling.
Definition: profiling.H:77
static void unstack(const profilingInformation *info)
Remove the information from the top of the stack.
Definition: profiling.C:204
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
void stop()
Stop triggered profiling.
label find(const T &val) const
Find index of the first occurrence of the value.
Definition: UList.C:173
const direction noexcept
Definition: Scalar.H:258
profilingTrigger() noexcept
Default construct, no profiling trigger.
void resize_nocopy(const label len)
Alter addressable list size, allocating new space if required without necessarily recovering old cont...
Definition: DynamicListI.H:375
A class for handling character strings derived from std::string.
Definition: string.H:72