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-2024 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 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
144 
145 bool Foam::profilingTrigger::possible() noexcept
146 {
147  return
148  (
150  #ifdef HAVE_EXTRAE
151  || bool(Extrae_event)
152  #endif
153  );
154 }
155 
156 
157 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
158 
159 void Foam::profilingTrigger::enter(const std::string& name)
160 {
161  // debug: std::cerr<< "enter profiling: " << name << '\n';
162 
163  ptr_ = profiling::New(name);
164 
165  #ifdef HAVE_EXTRAE
166  if (Extrae_event)
167  {
168  open_extrae_region(name);
169  }
170  #endif
171 }
172 
173 
174 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
175 
177 {
178  stop();
179 }
180 
181 
182 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
185 {
186  return ptr_;
187 }
188 
189 
191 {
192  #ifdef HAVE_EXTRAE
193  if (Extrae_event)
194  {
195  close_extrae_region();
196  }
197  #endif
198 
199  if (ptr_)
200  {
201  // profiling info pointer managed by pool storage, so no delete here
202  profiling::unstack(ptr_);
203  }
204 
205  ptr_ = nullptr;
206 }
207 
208 
209 // ************************************************************************* //
type
Types of root.
Definition: Roots.H:52
bool running() const noexcept
True if the triggered profiling section is active.
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:265
static bool active() noexcept
True if profiling is allowed and is active.
Definition: profiling.C:107
~profilingTrigger()
Destructor, calls stop()
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
static profilingInformation * New(const std::string &descr)
Existing or new element on pool, add to stack.
Definition: profiling.C:177
void stop()
Stop triggered profiling section.
label find(const T &val) const
Find index of the first occurrence of the value.
Definition: UList.C:172
const direction noexcept
Definition: scalarImpl.H:255
void resize_nocopy(const label len)
Alter addressable list size, allocating new space if required without necessarily recovering old cont...
Definition: DynamicListI.H:388