timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
library.h
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
27#if defined(__cplusplus)
28# include <cstdint>
29# include <cstdio>
30# include <cstdlib>
31#else
32# include <stdbool.h>
33# include <stddef.h>
34# include <stdint.h>
35# include <stdio.h>
36# include <stdlib.h>
37# include <string.h>
38#endif
39
41#include "timemory/enum.h"
42
43//======================================================================================//
44
45#if defined(TIMEMORY_USE_MPI) && defined(TIMEMORY_USE_GOTCHA)
46# define TIMEMORY_MPI_GOTCHA
47#endif
48
49// this is used bc it is easier to remove from generated XML when documentation is
50// rendered
51#if !defined(TIMEMORY_VISIBLE)
52# define TIMEMORY_VISIBLE TIMEMORY_VISIBILITY("default")
53#endif
54
55//======================================================================================//
56//
57// C struct for settings
58//
59//======================================================================================//
60
61typedef struct
62{
63 int enabled;
64 int auto_output;
65 int file_output;
66 int text_output;
67 int json_output;
68 int cout_output;
69 int precision;
70 int width;
71 int scientific;
72 // skipping remainder
73} timemory_settings;
74
75//======================================================================================//
76
77#if defined(__cplusplus)
78extern "C"
79{
80#endif // if defined(__cplusplus)
81
82 /// \fn uint64_t timemory_get_unique_id(void)
83 /// Returns a unique integer for a thread.
84 extern uint64_t timemory_get_unique_id(void) TIMEMORY_VISIBLE;
85
86 /// \fn void timemory_create_record(const char* name, uint64_t* id, int n, int* ct)
87 /// \param [in] name label for the record
88 /// \param [in,out] id assigned a unique identifier for the record
89 /// \param [in] n number of components
90 /// \param [in] ct array of enumeration identifiers of size n
91 ///
92 /// Function called by \ref timemory_begin_record, \ref
93 /// timemory_begin_record_enum, \ref timemory_begin_record_types, \ref
94 /// timemory_get_begin_record, \ref timemory_get_begin_record_enum, \ref
95 /// timemory_get_begin_record_types, \ref timemory_push_region for creating and
96 /// starting the current collection of components.
97 extern void timemory_create_record(const char* name, uint64_t* id, int n,
98 int* ct) TIMEMORY_VISIBLE;
99
100 /// \fn void timemory_delete_record(uint64_t nid)
101 /// Deletes the record created by \ref timemory_create_record.
102 extern void timemory_delete_record(uint64_t nid) TIMEMORY_VISIBLE;
103
104 /// \fn bool timemory_library_is_initialized(void)
105 /// Returns whether the library is initialized or not.
106 extern bool timemory_library_is_initialized(void) TIMEMORY_VISIBLE;
107
108 /// \fn void timemory_named_init_library(const char* name)
109 /// Initializes timemory with the exe name
110 extern void timemory_named_init_library(char* name) TIMEMORY_VISIBLE;
111
112 /// \fn void timemory_init_library(int argc, char** argv)
113 /// Initializes timemory. Not strictly necessary but highly recommended.
114 extern void timemory_init_library(int argc, char** argv) TIMEMORY_VISIBLE;
115
116 /// \fn void timemory_finalize_library(void)
117 /// Finalizes timemory. Output will be generated. Any attempt to store
118 /// data within timemory storage is undefined after this point and will likely
119 /// cause errors.
120 extern void timemory_finalize_library(void) TIMEMORY_VISIBLE;
121
122 /// \fn void timemory_pause(void)
123 /// Turn off timemory collection
124 extern void timemory_pause(void) TIMEMORY_VISIBLE;
125
126 /// \fn void timemory_resume(void)
127 /// Turn on timemory collection
128 extern void timemory_resume(void) TIMEMORY_VISIBLE;
129
130 /// \fn void timemory_set_default(const char* components)
131 /// Pass in a default set of components to use. Will be overridden by
132 /// TIMEMORY_COMPONENTS environment variable.
133 ///
134 /// \code{.cpp}
135 /// timemory_set_default("wall_clock, cpu_clock, cpu_util");
136 /// \endcode
137 extern void timemory_set_default(const char* components) TIMEMORY_VISIBLE;
138
139 /// \fn void timemory_set_environ(const char* evar, const char* eval, int ovr, int up)
140 /// \param [in] evar Environment variable name
141 /// \param [in] eval Environment variable value
142 /// \param [in] ovr Overwrite existing environment variable when > 0
143 /// \param [in] up Update settings when > 0
144 ///
145 /// Set an environment variable and (potentially) update settings with new value.
146 ///
147 /// \code{.cpp}
148 /// // overwrites the TIMEMORY_GLOBAL_COMPONENTS environment variable and updates
149 /// // settings
150 /// timemory_set_environ("TIIMEMORY_GLOBAL_COMPONENTS,
151 /// "wall_clock, cpu_clock, cpu_util", 1, 1);
152 /// \endcode
153 extern void timemory_set_environ(const char* evar, const char* eval, int ovr,
154 int up) TIMEMORY_VISIBLE;
155
156 /// \fn void timemory_add_components(const char* components)
157 /// Add some components to the current set of components being collected
158 /// Any components which are currently being collected are ignored.
159 ///
160 /// \code{.cpp}
161 /// timemory_add_components("peak_rss, priority_context_switch");
162 /// \endcode
163
164 extern void timemory_add_components(const char* components) TIMEMORY_VISIBLE;
165
166 /// \fn void timemory_remove_components(const char* components)
167 /// Remove some components to the current set of components being collected.
168 /// Any components which are not currently being collected are ignored.
169 ///
170 /// \code{.cpp}
171 /// timemory_add_components("priority_context_switch, read_bytes");
172 /// \endcode
173 extern void timemory_remove_components(const char* components) TIMEMORY_VISIBLE;
174
175 /// \fn void timemory_push_components(const char* components)
176 /// Replace the current set of components with a new set of components.
177 ///
178 /// \code{.cpp}
179 /// timemory_push_components("priority_context_switch, read_bytes");
180 /// \endcode
181 extern void timemory_push_components(const char* components) TIMEMORY_VISIBLE;
182
183 /// \fn void timemory_push_components_enum(int args, ...)
184 /// Replace the current set of components with a new set of components with
185 /// the set of enumerations provided to the function. First argument should be the
186 /// number of new components.
187 ///
188 /// \code{.cpp}
189 /// timemory_push_components(2, WALL_CLOCK, CPU_CLOCK);
190 /// \endcode
191 extern void timemory_push_components_enum(int args, ...) TIMEMORY_VISIBLE;
192
193 /// \fn void timemory_pop_components(void)
194 /// Inverse of the last \ref timemory_push_components or \ref
195 /// timemory_push_components_enum call. Popping all components will restore to set the
196 /// configured as the default.
197 extern void timemory_pop_components(void) TIMEMORY_VISIBLE;
198
199 /// \fn void timemory_begin_record(const char* name, uint64_t* id)
200 /// \param [in] name Label for the record
201 /// \param [in,out] id identifier passed back to \ref timemory_end_record
202 ///
203 /// \code{.cpp}
204 /// uint64_t idx = 0;
205 /// timemory_begin_record("foo", &idx);
206 /// // ...
207 /// timemory_end_record(idx);
208 /// \endcode
209 extern void timemory_begin_record(const char* name, uint64_t* id) TIMEMORY_VISIBLE;
210
211 /// \fn void timemory_begin_record_enum(const char* name, uint64_t*, ...)
212 /// Similar to \ref timemory_begin_record but accepts a specific enumerated
213 /// set of components, which is terminated by TIMEMORY_COMPONENTS_END.
214 ///
215 /// \code{.cpp}
216 /// uint64_t idx = 0;
217 /// timemory_begin_record("foo", &idx, WALL_CLOCK, CPU_UTIL, TIMEMORY_COMPONENTS_END);
218 /// // ...
219 /// timemory_end_record(idx);
220 /// \endcode
221 extern void timemory_begin_record_enum(const char* name, uint64_t*,
222 ...) TIMEMORY_VISIBLE;
223
224 /// \fn void timemory_begin_record_types(const char* name, uint64_t*, const char*)
225 /// Similar to \ref timemory_begin_record but accepts a specific set of
226 /// components as a string.
227 ///
228 /// \code{.cpp}
229 /// uint64_t idx = 0;
230 /// timemory_begin_record_types("foo", &idx, "wall_clock, cpu_util");
231 /// // ...
232 /// timemory_end_record(idx);
233 /// \endcode
234 extern void timemory_begin_record_types(const char* name, uint64_t*,
235 const char*) TIMEMORY_VISIBLE;
236
237 /// \fn uint64_t timemory_get_begin_record(const char* name)
238 /// Variant to \ref timemory_begin_record which returns a unique integer
239 extern uint64_t timemory_get_begin_record(const char* name) TIMEMORY_VISIBLE;
240
241 /// \fn uint64_t timemory_get_begin_record_enum(const char* name, ...)
242 /// Variant to \ref timemory_begin_record_enum which returns a unique integer
243 extern uint64_t timemory_get_begin_record_enum(const char* name,
244 ...) TIMEMORY_VISIBLE;
245
246 /// \fn uint64_t timemory_get_begin_record_types(const char* name, const char* types)
247 /// Variant to \ref timemory_begin_record_types which returns a unique integer
248 extern uint64_t timemory_get_begin_record_types(const char* name,
249 const char* ctypes) TIMEMORY_VISIBLE;
250
251 /// \fn void timemory_end_record(uint64_t id)
252 /// \param [in] id Identifier for the recording entry
253 ///
254 extern void timemory_end_record(uint64_t id) TIMEMORY_VISIBLE;
255
256 /// \fn void timemory_push_region(const char* name)
257 /// \param [in] name label for region
258 ///
259 /// Starts collection of components with label.
260 ///
261 /// \code{.cpp}
262 /// void foo()
263 /// {
264 /// timemory_push_region("foo");
265 /// // ...
266 /// timemory_pop_region("foo");
267 /// }
268 /// \endcode
269 extern void timemory_push_region(const char* name) TIMEMORY_VISIBLE;
270
271 /// \fn void timemory_pop_region(const char* name)
272 /// \param [in] name label for region
273 ///
274 /// Stops collection of components with label.
275 ///
276 /// \code{.cpp}
277 /// void foo()
278 /// {
279 /// timemory_push_region("foo");
280 /// // ...
281 /// timemory_pop_region("foo");
282 /// }
283 /// \endcode
284 extern void timemory_pop_region(const char* name) TIMEMORY_VISIBLE;
285
286 extern void c_timemory_init(int argc, char** argv,
287 timemory_settings) TIMEMORY_VISIBLE;
288 extern void c_timemory_finalize(void) TIMEMORY_VISIBLE;
289 extern int c_timemory_enabled(void) TIMEMORY_VISIBLE;
290 extern void* c_timemory_create_auto_timer(const char*) TIMEMORY_VISIBLE;
291 extern void c_timemory_delete_auto_timer(void*) TIMEMORY_VISIBLE;
292 extern void* c_timemory_create_auto_tuple(const char*, ...) TIMEMORY_VISIBLE;
293 extern void c_timemory_delete_auto_tuple(void*) TIMEMORY_VISIBLE;
294 extern const char* c_timemory_blank_label(const char*) TIMEMORY_VISIBLE;
295 extern const char* c_timemory_basic_label(const char*, const char*) TIMEMORY_VISIBLE;
296 extern const char* c_timemory_label(const char*, const char*, int,
297 const char*) TIMEMORY_VISIBLE;
298 extern int cxx_timemory_enabled(void) TIMEMORY_VISIBLE;
299 extern void cxx_timemory_init(int, char**, timemory_settings) TIMEMORY_VISIBLE;
300 extern void* cxx_timemory_create_auto_timer(const char*) TIMEMORY_VISIBLE;
301 extern void* cxx_timemory_create_auto_tuple(const char*, int,
302 const int*) TIMEMORY_VISIBLE;
303 extern void* cxx_timemory_delete_auto_timer(void*) TIMEMORY_VISIBLE;
304 extern void* cxx_timemory_delete_auto_tuple(void*) TIMEMORY_VISIBLE;
305 extern const char* cxx_timemory_label(int, int, const char*, const char*,
306 const char*) TIMEMORY_VISIBLE;
307
308 extern bool timemory_trace_is_initialized(void) TIMEMORY_VISIBLE;
309 extern void timemory_reset_throttle(const char* name) TIMEMORY_VISIBLE;
310 extern bool timemory_is_throttled(const char* name) TIMEMORY_VISIBLE;
311 extern void timemory_add_hash_id(uint64_t id, const char* name) TIMEMORY_VISIBLE;
312 extern void timemory_add_hash_ids(uint64_t nentries, uint64_t* ids,
313 const char** names) TIMEMORY_VISIBLE;
314 extern void timemory_push_trace_hash(uint64_t id) TIMEMORY_VISIBLE;
315 extern void timemory_pop_trace_hash(uint64_t id) TIMEMORY_VISIBLE;
316 extern void timemory_push_trace(const char* name) TIMEMORY_VISIBLE;
317 extern void timemory_pop_trace(const char* name) TIMEMORY_VISIBLE;
318 extern void timemory_trace_init(const char*, bool, const char*) TIMEMORY_VISIBLE;
319 extern void timemory_trace_finalize(void) TIMEMORY_VISIBLE;
320 extern void timemory_trace_set_env(const char*, const char*) TIMEMORY_VISIBLE;
321
322#if defined(TIMEMORY_MPI_GOTCHA)
323 /// \fn void timemory_trace_set_mpi(bool use, bool attached)
324 /// \param[in] use Use MPI gotcha
325 /// \param[in] attached Tracing application has attached to a running program
326 ///
327 /// This function is only declared and defined if timemory was built
328 /// with support for MPI and GOTCHA.
329 extern void timemory_trace_set_mpi(bool use, bool attached) TIMEMORY_VISIBLE;
330#endif
331
332 /// \typedef void (*timemory_create_func_t)(const char*, uint64_t*, int, int*)
333 /// function pointer type for \ref timemory_create_function
334 typedef void (*timemory_create_func_t)(const char*, uint64_t*, int, int*);
335
336 /// \typedef void (*timemory_delete_func_t)(uint64_t)
337 /// function pointer type for \ref timemory_delete_function
338 typedef void (*timemory_delete_func_t)(uint64_t);
339
340 /// \var timemory_create_func_t timemory_create_function
341 /// The function pointer to set to customize which components are used by
342 /// library interface.
343 /// \code{.cpp}
344 /// using namespace tim::component;
345 /// using test_list_t =
346 /// tim::component_list<wall_clock, cpu_util, cpu_clock, peak_rss>;
347 ///
348 /// static std::map<uint64_t, std::shared_ptr<test_list_t>> test_map;
349 ///
350 /// void
351 /// custom_create_record(const char* name, uint64_t* id, int n, int* ct)
352 /// {
353 /// uint64_t idx = timemory_get_unique_id();
354 /// auto tmp = std::make_shared<test_list_t>(name);
355 /// tim::initialize(*tmp, n, ct);
356 /// tmp->initialize<cpu_util, cpu_clock>();
357 /// test_map[idx] = tmp;
358 /// test_map[idx]->start();
359 /// *id = idx;
360 /// }
361 ///
362 /// void
363 /// main()
364 /// {
365 /// // ... using default create/delete functions ...
366 ///
367 /// timemory_create_function = &custom_create_record;
368 /// timemory_delete_function = ...;
369 ///
370 /// // ... using custom create/delete functions ...
371 ///
372 /// timemory_create_function = nullptr;
373 /// timemory_delete_function = ...;
374 ///
375 /// // ... using default create/delete functions ...
376 ///
377 /// }
378 /// \endcode
379 extern TIMEMORY_DLL timemory_create_func_t timemory_create_function TIMEMORY_VISIBLE;
380
381 /// \var timemory_delete_func_t timemory_delete_function
382 /// The function pointer to set which deletes an entry created by \ref
383 /// timemory_create_function.
384 /// \code{.cpp}
385 ///
386 /// static std::map<uint64_t, std::shared_ptr<test_list_t>> test_map;
387 ///
388 /// void
389 /// custom_delete_record(uint64_t id)
390 /// {
391 /// auto itr = test_map.find(id);
392 /// if(itr != test_map.end())
393 /// {
394 /// itr->second->stop();
395 /// test_map.erase(itr);
396 /// }
397 /// }
398 ///
399 /// void
400 /// main()
401 /// {
402 /// // ... using default create/delete functions ...
403 ///
404 /// timemory_create_function = &custom_create_record;
405 /// timemory_delete_function = &custom_delete_record;
406 ///
407 /// // ... using custom create/delete functions ...
408 ///
409 /// timemory_create_function = nullptr;
410 /// timemory_delete_function = nullptr;
411 ///
412 /// // ... using default create/delete functions ...
413 ///
414 /// }
415 /// \endcode
416 extern TIMEMORY_DLL timemory_delete_func_t timemory_delete_function TIMEMORY_VISIBLE;
417
418#if defined(__cplusplus)
419}
420#endif // if defined(__cplusplus)
void timemory_create_record(const char *name, uint64_t *id, int n, int *ct)
Definition: library.cpp:156
void timemory_pause(void)
Turn off timemory collection.
Definition: library.cpp:313
void(* timemory_delete_func_t)(uint64_t)
function pointer type for timemory_delete_function
Definition: library.h:338
void timemory_set_environ(const char *evar, const char *eval, int ovr, int up)
Definition: library.cpp:335
timemory_create_func_t timemory_create_function
The function pointer to set to customize which components are used by library interface.
Definition: library.cpp:51
void timemory_trace_init(const char *, bool, const char *)
Definition: trace.cpp:671
timemory_delete_func_t timemory_delete_function
The function pointer to set which deletes an entry created by timemory_create_function.
Definition: library.cpp:52
void timemory_push_trace_hash(uint64_t id)
Definition: trace.cpp:448
void * c_timemory_create_auto_tuple(const char *,...)
Definition: timemory_c.c:83
void timemory_push_trace(const char *name)
Definition: trace.cpp:608
uint64_t timemory_get_unique_id(void)
Returns a unique integer for a thread.
Definition: library.cpp:144
void timemory_push_components_enum(int args,...)
Replace the current set of components with a new set of components with the set of enumerations provi...
Definition: library.cpp:377
void timemory_remove_components(const char *components)
Remove some components to the current set of components being collected. Any components which are not...
Definition: library.cpp:358
void timemory_push_region(const char *name)
Definition: library.cpp:573
void timemory_end_record(uint64_t id)
Definition: library.cpp:557
uint64_t timemory_get_begin_record_types(const char *name, const char *ctypes)
Variant to timemory_begin_record_types which returns a unique integer.
Definition: library.cpp:503
void cxx_timemory_init(int, char **, timemory_settings)
bool timemory_is_throttled(const char *name)
Definition: trace.cpp:374
uint64_t timemory_get_begin_record_enum(const char *name,...)
Variant to timemory_begin_record_enum which returns a unique integer.
Definition: library.cpp:524
void timemory_pop_components(void)
Inverse of the last timemory_push_components or timemory_push_components_enum call....
Definition: library.cpp:399
void(* timemory_create_func_t)(const char *, uint64_t *, int, int *)
Definition: library.h:334
void timemory_pop_trace(const char *name)
Definition: trace.cpp:632
void timemory_pop_region(const char *name)
Definition: library.cpp:587
bool timemory_library_is_initialized(void)
Returns whether the library is initialized or not.
Definition: library.cpp:195
void timemory_resume(void)
Turn on timemory collection.
Definition: library.cpp:318
void * cxx_timemory_delete_auto_tuple(void *)
void timemory_begin_record_enum(const char *name, uint64_t *,...)
Similar to timemory_begin_record but accepts a specific enumerated set of components,...
Definition: library.cpp:450
void timemory_init_library(int argc, char **argv)
Initializes timemory. Not strictly necessary but highly recommended.
Definition: library.cpp:212
int c_timemory_enabled(void)
Definition: timemory_c.c:69
void c_timemory_finalize(void)
Definition: timemory_c.c:65
void timemory_pop_trace_hash(uint64_t id)
Definition: trace.cpp:514
void timemory_add_components(const char *components)
Add some components to the current set of components being collected Any components which are current...
Definition: library.cpp:348
const char * c_timemory_blank_label(const char *)
Definition: timemory_c.c:131
void * cxx_timemory_delete_auto_timer(void *)
void timemory_set_default(const char *components)
Pass in a default set of components to use. Will be overridden by TIMEMORY_COMPONENTS environment var...
Definition: library.cpp:322
void timemory_delete_record(uint64_t nid)
Deletes the record created by timemory_create_record.
Definition: library.cpp:177
bool timemory_trace_is_initialized(void)
Definition: trace.cpp:366
const char * c_timemory_label(const char *, const char *, int, const char *)
Definition: timemory_c.c:145
void timemory_push_components(const char *components)
Replace the current set of components with a new set of components.
Definition: library.cpp:368
void timemory_trace_finalize(void)
Definition: trace.cpp:808
void timemory_named_init_library(char *name)
Definition: library.cpp:200
void c_timemory_init(int argc, char **argv, timemory_settings)
Definition: timemory_c.c:57
int cxx_timemory_enabled(void)
uint64_t timemory_get_begin_record(const char *name)
Variant to timemory_begin_record which returns a unique integer.
Definition: library.cpp:482
void timemory_begin_record_types(const char *name, uint64_t *, const char *)
Similar to timemory_begin_record but accepts a specific set of components as a string.
Definition: library.cpp:429
void * cxx_timemory_create_auto_timer(const char *)
void c_timemory_delete_auto_tuple(void *)
Definition: timemory_c.c:123
void timemory_begin_record(const char *name, uint64_t *id)
Definition: library.cpp:409
void timemory_finalize_library(void)
Finalizes timemory. Output will be generated. Any attempt to store data within timemory storage is un...
Definition: library.cpp:253
void timemory_add_hash_ids(uint64_t nentries, uint64_t *ids, const char **names)
Definition: trace.cpp:420
const char * c_timemory_basic_label(const char *, const char *)
Definition: timemory_c.c:138
void c_timemory_delete_auto_timer(void *)
Definition: timemory_c.c:115
const char * cxx_timemory_label(int, int, const char *, const char *, const char *)
void * c_timemory_create_auto_timer(const char *)
Definition: timemory_c.c:73
void timemory_trace_set_env(const char *, const char *)
Definition: trace.cpp:658
void timemory_add_hash_id(uint64_t id, const char *name)
Definition: trace.cpp:392
void timemory_reset_throttle(const char *name)
Definition: trace.cpp:382
void * cxx_timemory_create_auto_tuple(const char *, int, const int *)
text_output
Definition: settings.cpp:1624
json_output
Definition: settings.cpp:1625
scientific
Definition: settings.cpp:1646
char ** argv
Definition: config.cpp:55
file_output
Definition: settings.cpp:1623
auto_output
Definition: settings.cpp:1621
precision
Definition: settings.cpp:1643
cout_output
Definition: settings.cpp:1622
components
Definition: settings.cpp:1700