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.
components.hpp
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
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/**
26 * \file timemory/components/gperftools/components.hpp
27 * \brief Implementation of the gperftools component(s)
28 */
29
30#pragma once
31
33#include "timemory/components/gperftools/backends.hpp"
38#include "timemory/units.hpp"
39
40//======================================================================================//
41//
42namespace tim
43{
44namespace component
45{
46//
47//--------------------------------------------------------------------------------------//
48// start/stop gperftools cpu profiler
49//
50struct gperftools_cpu_profiler : public base<gperftools_cpu_profiler, void>
51{
52 using value_type = void;
55
56 static std::string label() { return "gperftools_cpu_profiler"; }
58 {
59 return "Control switch for gperftools CPU profiler";
60 }
61 static value_type record() {}
62
63 static void thread_init() { gperftools::cpu::register_thread(); }
64
65 static void global_finalize()
66 {
67 if(gperftools::cpu::is_running())
68 {
69 gperftools::cpu::profiler_flush();
70 gperftools::cpu::profiler_stop();
71 }
72 }
73
74 void start()
75 {
76 if(!gperftools::cpu::is_running())
77 {
78 index = this_type::get_index()++;
79 const auto& _dmp_info = get_dmp_info();
80 bool _dmp_init = std::get<0>(_dmp_info);
81 int32_t _dmp_rank = std::get<1>(_dmp_info);
83 label() + "_" + std::to_string(index), ".dat", _dmp_init, _dmp_rank);
84 auto ret = gperftools::cpu::profiler_start(fname);
85 if(ret == 0)
86 {
87 fprintf(stderr, "[gperftools_cpu_profiler]> Error starting %s...",
88 fname.c_str());
89 }
90 }
91 }
92
93 void stop()
94 {
95 if(index >= 0)
96 {
97 gperftools::cpu::profiler_flush();
98 gperftools::cpu::profiler_stop();
99 }
100 }
101
102private:
103 int32_t index = -1; // if this is >= zero, then we flush and stop
104
105private:
106 static std::atomic<int64_t>& get_index()
107 {
108 static std::atomic<int64_t> _instance(0);
109 return _instance;
110 }
111
112 using dmp_info_t = std::tuple<bool, int32_t, int32_t>;
113
114 static const dmp_info_t& get_dmp_info()
115 {
116 static dmp_info_t _info{ dmp::is_initialized(), dmp::rank(), dmp::size() };
117 return _info;
118 }
119};
120//
121//--------------------------------------------------------------------------------------//
122// start/stop gperftools cpu profiler
123//
124struct gperftools_heap_profiler : public base<gperftools_heap_profiler, void>
125{
126 using value_type = void;
129
130 static std::string label() { return "gperftools_heap_profiler"; }
132 {
133 return "Control switch for the gperftools heap profiler";
134 }
135 static value_type record() {}
136
137 static void global_finalize()
138 {
139 if(gperftools::heap::is_running())
140 {
141 gperftools::heap::profiler_flush("global_finalize");
142 gperftools::heap::profiler_stop();
143 }
144 }
145
146 void start()
147 {
148 if(!gperftools::heap::is_running())
149 {
150 index = this_type::get_index()++;
151 auto fname = settings::compose_output_filename(label(), ".dat");
152 auto ret = gperftools::heap::profiler_start(fname);
153 if(ret > 0)
154 {
155 fprintf(stderr, "[gperftools_heap_profiler]> Error starting %s...",
156 prefix.c_str());
157 }
158 }
159 }
160
161 void stop()
162 {
163 if(index >= 0)
164 {
165 gperftools::heap::profiler_flush(prefix);
166 gperftools::heap::profiler_stop();
167 }
168 }
169
171
172protected:
174 int32_t index = -1; // if this is >= zero, then we flush and stop
175
176private:
177 static std::atomic<int64_t>& get_index()
178 {
179 static std::atomic<int64_t> _instance(0);
180 return _instance;
181 }
182};
183//
184} // namespace component
185} // namespace tim
186//
187//======================================================================================//
Declare the gperftools component types.
Definition: kokkosp.cpp:39
char const std::string & _prefix
Definition: config.cpp:55
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
void set_prefix(const std::string &_prefix)
Definition: components.hpp:170
static string_t compose_output_filename(string_t _tag, string_t _ext, bool _use_suffix=use_output_suffix(), int32_t _suffix=default_process_suffix(), bool _make_dir=false, std::string _explicit={})
Definition: settings.cpp:322