Skip to content

Commit d325081

Browse files
committed
Added perfetto tracks and traces with cached data
1 parent 0e28188 commit d325081

File tree

14 files changed

+1374
-41
lines changed

14 files changed

+1374
-41
lines changed

projects/rocprofiler-systems/source/lib/core/config.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ configure_settings(bool _init)
315315
ROCPROFSYS_CONFIG_SETTING(bool, "ROCPROFSYS_USE_ROCPD", "Enable rocpd backend", false,
316316
"backend", "rocpd");
317317

318+
ROCPROFSYS_CONFIG_SETTING(bool, "ROCPROFSYS_CACHING_PERFETTO",
319+
"Enable perfetto with trace cache", false, "backend",
320+
"perfetto_caching");
321+
318322
ROCPROFSYS_CONFIG_SETTING(bool, "ROCPROFSYS_USE_ROCM",
319323
"Enable ROCm API and kernel tracing", true, "backend",
320324
"rocm");
@@ -2398,6 +2402,13 @@ get_use_rocpd()
23982402
return static_cast<tim::tsettings<bool>&>(*_v).get();
23992403
}
24002404

2405+
bool&
2406+
get_caching_perfetto()
2407+
{
2408+
static auto _v = get_config()->at("ROCPROFSYS_CACHING_PERFETTO");
2409+
return static_cast<tim::tsettings<bool>&>(*_v).get();
2410+
}
2411+
24012412
tmp_file::tmp_file(std::string _v)
24022413
: filename{ std::move(_v) }
24032414
{}

projects/rocprofiler-systems/source/lib/core/config.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ get_database_absolute_path(std::string_view database_name, std::string_view tag)
367367
bool&
368368
get_use_rocpd() ROCPROFSYS_HOT;
369369

370+
bool&
371+
get_caching_perfetto() ROCPROFSYS_HOT;
372+
370373
struct tmp_file
371374
{
372375
tmp_file(std::string);

projects/rocprofiler-systems/source/lib/core/perfetto.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ post_process(tim::manager* _timemory_manager, bool& _perfetto_output_error)
208208
_data = char_vec_t{ tracing_session->ReadTraceBlocking() };
209209
}
210210

211+
tracing_session.reset();
211212
return _data;
212213
};
213214

projects/rocprofiler-systems/source/lib/core/trace_cache/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(trace_cache_sources
2626
${CMAKE_CURRENT_LIST_DIR}/buffer_storage.cpp
2727
${CMAKE_CURRENT_LIST_DIR}/metadata_registry.cpp
2828
${CMAKE_CURRENT_LIST_DIR}/rocpd_post_processing.cpp
29+
${CMAKE_CURRENT_LIST_DIR}/perfetto_post_processing.cpp
2930
)
3031

3132
set(trace_cache_headers
@@ -35,6 +36,7 @@ set(trace_cache_headers
3536
${CMAKE_CURRENT_LIST_DIR}/cache_utility.hpp
3637
${CMAKE_CURRENT_LIST_DIR}/metadata_registry.hpp
3738
${CMAKE_CURRENT_LIST_DIR}/rocpd_post_processing.hpp
39+
${CMAKE_CURRENT_LIST_DIR}/perfetto_post_processing.hpp
3840
${CMAKE_CURRENT_LIST_DIR}/sample_type.hpp
3941
)
4042

projects/rocprofiler-systems/source/lib/core/trace_cache/cache_manager.cpp

Lines changed: 80 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@
2626
#include "core/trace_cache/storage_parser.hpp"
2727
#include "debug.hpp"
2828
#include "library/runtime.hpp"
29+
#include "perfetto_post_processing.hpp"
2930
#include "trace_cache/cache_utility.hpp"
3031
#include "trace_cache/metadata_registry.hpp"
3132
#include "trace_cache/rocpd_post_processing.hpp"
3233
#include <algorithm>
34+
#include <iterator>
3335
#include <memory>
3436
#include <vector>
3537

@@ -70,7 +72,8 @@ list_dir_files(const std::string& path)
7072
DIR* dir = opendir(path.c_str());
7173
if(dir == nullptr)
7274
{
73-
ROCPROFSYS_THROW("Error opening directory: %s", path.c_str());
75+
std::cerr << "Error opening directory: " << path << std::endl;
76+
return {};
7477
}
7578

7679
std::vector<std::string> result{};
@@ -153,35 +156,24 @@ cache_manager::post_process_bulk()
153156
shutdown();
154157
}
155158

156-
auto _cache_files = get_cache_files();
157-
158-
if(get_use_rocpd())
159+
if(get_use_rocpd() || get_caching_perfetto())
159160
{
160161
ROCPROFSYS_PRINT(
161162
"Generating rocpd with collected data. This may take a while..\n");
162163

164+
auto _cache_files = get_cache_files();
165+
163166
std::vector<std::thread> rocpd_threads;
164167
ROCPROFSYS_SCOPED_SAMPLING_ON_CHILD_THREADS(false);
165168

166-
rocpd_threads.emplace_back([this]() {
167-
auto pid = getpid();
168-
auto ppid = get_root_process_id();
169-
rocpd_post_processing _post_processing(
170-
m_metadata, get_agent_manager_instance(), pid, ppid);
171-
storage_parser _parser(
172-
get_buffered_storage_filename(get_root_process_id(), getpid()));
173-
_post_processing.register_parser_callback(_parser);
174-
_post_processing.post_process_metadata();
175-
_parser.consume_storage();
176-
});
177-
178-
for(const auto& [pid, files] : _cache_files)
169+
if(get_caching_perfetto())
179170
{
180-
if(!files.buff_storage.empty() && !files.metadata.empty())
171+
for(const auto& [pid, files] : _cache_files)
181172
{
182-
rocpd_threads.emplace_back([pid = pid, files = files]() {
173+
if(!files.buff_storage.empty() && !files.metadata.empty())
174+
{
183175
ROCPROFSYS_DEBUG(
184-
"Creating database for [%d] from buffered storage "
176+
"Creating Perfetto trace for [%d] from buffered storage "
185177
"file: %s and from metadata file: %s\n",
186178
pid, files.buff_storage.c_str(), files.metadata.c_str());
187179

@@ -197,22 +189,82 @@ cache_manager::post_process_bulk()
197189
return;
198190
}
199191

200-
agent_manager _agent_manager{ _agents };
201-
auto ppid = get_root_process_id();
202-
rocpd_post_processing _post_processing(_metadata, _agent_manager,
203-
pid, ppid);
204-
storage_parser _parser(files.buff_storage);
205-
_post_processing.register_parser_callback(_parser);
206-
_post_processing.post_process_metadata();
192+
agent_manager _agent_manager{ _agents };
193+
194+
perfetto_post_processing _perfetto_post_processing(
195+
_metadata, pid, _agent_manager);
196+
197+
storage_parser _parser(files.buff_storage);
198+
199+
_perfetto_post_processing.register_parser_callback(_parser);
200+
_perfetto_post_processing.start_session();
201+
207202
_parser.consume_storage();
208-
});
203+
204+
bool _perfetto_error = false;
205+
_perfetto_post_processing.post_process(_perfetto_error);
206+
}
207+
}
208+
}
209+
210+
if(get_use_rocpd())
211+
{
212+
rocpd_threads.emplace_back([this]() {
213+
auto pid = getpid();
214+
auto ppid = get_root_process_id();
215+
rocpd_post_processing _post_processing(
216+
m_metadata, get_agent_manager_instance(), pid, ppid);
217+
storage_parser _parser(
218+
get_buffered_storage_filename(get_root_process_id(), getpid()));
219+
_post_processing.register_parser_callback(_parser);
220+
_post_processing.post_process_metadata();
221+
_parser.consume_storage();
222+
});
223+
224+
for(const auto& [pid, files] : _cache_files)
225+
{
226+
if(!files.buff_storage.empty() && !files.metadata.empty())
227+
{
228+
rocpd_threads.emplace_back([pid = pid, files = files]() {
229+
ROCPROFSYS_DEBUG(
230+
"Creating database for [%d] from buffered storage "
231+
"file: %s and from metadata file: %s\n",
232+
pid, files.buff_storage.c_str(), files.metadata.c_str());
233+
234+
std::vector<std::shared_ptr<agent>> _agents;
235+
metadata_registry _metadata;
236+
237+
auto res = _metadata.load_from_file(files.metadata, _agents);
238+
if(!res)
239+
{
240+
ROCPROFSYS_WARNING(
241+
0, "Load from file for metadata failed: %s\n",
242+
files.metadata.c_str());
243+
return;
244+
}
245+
246+
agent_manager _agent_manager{ _agents };
247+
auto ppid = get_root_process_id();
248+
rocpd_post_processing _post_processing(
249+
_metadata, _agent_manager, pid, ppid);
250+
storage_parser _parser(files.buff_storage);
251+
_post_processing.register_parser_callback(_parser);
252+
_post_processing.post_process_metadata();
253+
_parser.consume_storage();
254+
std::remove(files.metadata.c_str()); // Remove metadata file
255+
});
256+
}
209257
}
210258
}
211259

212260
for(auto& thread : rocpd_threads)
213261
{
214262
thread.join();
215263
}
264+
if(get_caching_perfetto())
265+
{
266+
clear_cache_files();
267+
}
216268
}
217269

218270
ROCPROFSYS_PRINT("Removing cached temporary files...\n");

0 commit comments

Comments
 (0)