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.
tim::operation::finalize::flamegraph< Type > Struct Template Reference

#include "timemory/operations/types/finalize/flamegraph.hpp"

+ Collaboration diagram for tim::operation::finalize::flamegraph< Type >:

Public Types

using storage_type = impl::storage< Type, has_data >
 
using result_type = typename storage_type::result_array_t
 
using distrib_type = typename storage_type::dmp_result_t
 
using result_node = typename storage_type::result_node
 
using graph_type = typename storage_type::graph_t
 
using graph_node = typename storage_type::graph_node
 
using hierarchy_type = typename storage_type::uintvector_t
 

Public Member Functions

template<typename Up = Type>
 flamegraph (storage_type *, std::string, enable_if_t< trait::supports_flamegraph< Up >::value, int >=0)
 
template<typename Up = Type>
 flamegraph (storage_type *, std::string, enable_if_t<!trait::supports_flamegraph< Up >::value, int >=0)
 

Static Public Attributes

static constexpr bool has_data = true
 

Detailed Description

template<typename Type>
struct tim::operation::finalize::flamegraph< Type >

Definition at line 50 of file flamegraph.hpp.

Member Typedef Documentation

◆ distrib_type

template<typename Type >
using tim::operation::finalize::flamegraph< Type >::distrib_type = typename storage_type::dmp_result_t

Definition at line 55 of file flamegraph.hpp.

◆ graph_node

template<typename Type >
using tim::operation::finalize::flamegraph< Type >::graph_node = typename storage_type::graph_node

Definition at line 58 of file flamegraph.hpp.

◆ graph_type

template<typename Type >
using tim::operation::finalize::flamegraph< Type >::graph_type = typename storage_type::graph_t

Definition at line 57 of file flamegraph.hpp.

◆ hierarchy_type

template<typename Type >
using tim::operation::finalize::flamegraph< Type >::hierarchy_type = typename storage_type::uintvector_t

Definition at line 59 of file flamegraph.hpp.

◆ result_node

template<typename Type >
using tim::operation::finalize::flamegraph< Type >::result_node = typename storage_type::result_node

Definition at line 56 of file flamegraph.hpp.

◆ result_type

template<typename Type >
using tim::operation::finalize::flamegraph< Type >::result_type = typename storage_type::result_array_t

Definition at line 54 of file flamegraph.hpp.

◆ storage_type

template<typename Type >
using tim::operation::finalize::flamegraph< Type >::storage_type = impl::storage<Type, has_data>

Definition at line 53 of file flamegraph.hpp.

Constructor & Destructor Documentation

◆ flamegraph() [1/2]

template<typename Type >
template<typename Up >
tim::operation::finalize::flamegraph< Type >::flamegraph ( storage_type _data,
std::string  _label,
enable_if_t< trait::supports_flamegraph< Up >::value, int >  = 0 
)

Definition at line 75 of file flamegraph.hpp.

77{
78 // auto node_init = dmp::is_initialized();
79 // auto node_size = dmp::size();
80 dmp::barrier();
81 auto node_rank = dmp::rank();
82 auto node_results = _data->dmp_get();
83 dmp::barrier();
84
85 if(node_rank != 0 || node_results.empty())
86 return;
87
88 result_type results;
89 for(auto&& itr : node_results)
90 {
91 for(auto&& nitr : itr)
92 {
93 results.emplace_back(std::move(nitr));
94 }
95 }
96
97 if(results.empty())
98 return;
99
100 using Archive = cereal::MinimalJSONOutputArchive;
101 // using Archive = cereal::PrettyJSONOutputArchive;
102 using policy_type = policy::output_archive<Archive, TIMEMORY_API>;
103
104 auto outfname = settings::compose_output_filename(_label, ".flamegraph.json");
105
106 if(outfname.length() > 0)
107 {
108 std::ofstream ofs{};
109 if(filepath::open(ofs, outfname))
110 {
111 manager::instance()->add_json_output(_label, outfname);
112 printf("[%s]|%i> Outputting '%s'...\n", _label.c_str(), node_rank,
113 outfname.c_str());
114
115 // ensure write final block during destruction before the file is closed
116 auto oa = policy_type::get(ofs);
117
118 oa->setNextName("traceEvents");
119 oa->startNode();
120 oa->makeArray();
121
122 using value_type = decay_t<decltype(std::declval<const Type>().get())>;
123 using offset_map_t = std::map<int64_t, value_type>;
124 using useoff_map_t = std::map<int64_t, bool>;
125 auto conv = units::usec;
126 offset_map_t total_offset;
127 offset_map_t last_offset;
128 offset_map_t last_value;
129 useoff_map_t use_last;
130 int64_t max_depth = 1;
131
132 for(auto& itr : results)
133 {
134 max_depth = std::max<int64_t>(max_depth, itr.depth() + 1);
135 use_last[itr.depth()] = false;
136 }
137
138 for(auto& itr : results)
139 {
140 auto _prefix = itr.prefix();
141 auto value = itr.data().get() * conv;
142
143 auto litr = last_offset.find(itr.depth());
144 if(litr != last_offset.end())
145 {
146 // for(int64_t i = 0; i < max_depth; ++i)
147 // use_last[i] = false;
148
149 total_offset[itr.depth()] += litr->second;
150
151 for(int64_t i = itr.depth() + 1; i < max_depth; ++i)
152 {
153 // use_last[i] = true;
154 total_offset[i] = total_offset[itr.depth()];
155 last_value[i] = litr->second;
156 auto ditr = last_offset.find(i);
157 if(ditr != last_offset.end())
158 last_offset.erase(ditr);
159 }
160 last_offset.erase(litr);
161 }
162
163 value_type offset = total_offset[itr.depth()];
164 if(use_last[itr.depth()])
165 offset += last_value[itr.depth()] - value;
166
167 oa->startNode();
168
169 // oa->setNextName("args");
170 // oa->startNode();
171 // (*oa)(cereal::make_nvp("detail", _prefix));
172 // (*oa)(cereal::make_nvp("count", itr.data().get_laps()));
173 // (*oa)(cereal::make_nvp("depth", itr.depth()));
174 // (*oa)(cereal::make_nvp("units", itr.data().get_display_unit()));
175 // oa->finishNode();
176
177 string_t _ph = "X";
178 if(_prefix.find(">>>") != std::string::npos)
179 _prefix = _prefix.substr(_prefix.find_first_of(">>>") + 3);
180 if(_prefix.find("|_") != std::string::npos)
181 _prefix = _prefix.substr(_prefix.find_first_of("|_") + 2);
182
183 (*oa)(cereal::make_nvp("dur", value));
184 (*oa)(cereal::make_nvp("name", _prefix));
185 (*oa)(cereal::make_nvp("ph", _ph));
186 (*oa)(cereal::make_nvp("pid", itr.pid()));
187 (*oa)(cereal::make_nvp("tid", itr.tid()));
188 (*oa)(cereal::make_nvp("ts", offset));
189
190 oa->finishNode();
191
192 last_offset[itr.depth()] = value;
193 last_value[itr.depth()] = value;
194 // total_offset[itr.depth()] += value;
195 }
196
197 /*
198 oa->startNode();
199 oa->setNextName("args");
200 oa->startNode();
201 (*oa)(cereal::make_nvp("name", _label));
202 oa->finishNode();
203 string_t _ph = "M";
204 string_t _cat = "";
205 string_t _name = "metric";
206 (*oa)(cereal::make_nvp("cat", _cat));
207 (*oa)(cereal::make_nvp("name", _name));
208 (*oa)(cereal::make_nvp("ph", _ph));
209 (*oa)(cereal::make_nvp("pid", process::get_id()));
210 (*oa)(cereal::make_nvp("tid", 0));
211 (*oa)(cereal::make_nvp("ts", 0));
212 oa->finishNode();
213 */
214
215 oa->finishNode();
216 }
217 if(ofs)
218 ofs << std::endl;
219 ofs.close();
220 }
221}
static pointer_t instance()
Get a shared pointer to the instance for the current thread.
std::string string_t
Definition: library.cpp:57
bool open(std::ofstream &_ofs, std::string _fpath, Args &&... _args)
Definition: filepath.hpp:207
max_depth
Definition: settings.cpp:1641
char const std::string & _prefix
Definition: config.cpp:55
auto get(const auto_bundle< Tag, Types... > &_obj)
typename storage_type::result_array_t result_type
Definition: flamegraph.hpp:54
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

References tim::_prefix, tim::settings::compose_output_filename(), tim::get(), tim::manager::instance(), tim::max_depth, and tim::filepath::open().

◆ flamegraph() [2/2]

template<typename Type >
template<typename Up >
tim::operation::finalize::flamegraph< Type >::flamegraph ( storage_type ,
std::string  ,
enable_if_t<!trait::supports_flamegraph< Up >::value, int >  = 0 
)

Definition at line 227 of file flamegraph.hpp.

229{}

Member Data Documentation

◆ has_data

template<typename Type >
constexpr bool tim::operation::finalize::flamegraph< Type >::has_data = true
staticconstexpr

Definition at line 52 of file flamegraph.hpp.


The documentation for this struct was generated from the following files: