2222
2323#include " core/trace_cache/perfetto_post_processing.hpp"
2424#include " common.hpp"
25- #include " config.hpp"
2625#include " core/agent_manager.hpp"
2726#include " library/tracing.hpp"
2827#include " perfetto.hpp"
@@ -56,26 +55,6 @@ get_perfetto_initialized()
5655 return _initialized;
5756}
5857
59- auto &
60- get_perfetto_tmp_file ()
61- {
62- static std::shared_ptr<tmp_file> _tmp_file = nullptr ;
63- return _tmp_file;
64- }
65-
66- int
67- get_perffeto_temp_fd (const std::string& _pid)
68- {
69- auto & _tmp_file = get_perfetto_tmp_file ();
70- if (config::get_use_tmp_files ())
71- {
72- auto _base = JOIN (" -" , " cached-perfetto-trace" , _pid);
73- _tmp_file = config::get_tmp_file (_base, " proto" );
74- _tmp_file->open (O_RDWR | O_CREAT | O_TRUNC, 0600 );
75- }
76- return ((_tmp_file) ? _tmp_file->fd : -1 );
77- }
78-
7958void
8059initialize_perfetto ()
8160{
@@ -185,7 +164,7 @@ write_track_data(const struct backtrace_region_sample& _sample)
185164
186165 auto _track = get_track (Category{}, _track_name, _thread_id);
187166
188- auto add_annotations = [&](::perfetto::EventContext ctx) {
167+ auto add_annotations = [&](::perfetto::EventContext& ctx) {
189168 std::vector<annotation_entry> annotations = {
190169 { " begin_ns" , _sample.start_timestamp }, { " end_ns" , _sample.end_timestamp }
191170 };
@@ -223,6 +202,7 @@ perfetto_post_processing::perfetto_post_processing(metadata_registry& metadata,
223202: m_metadata(metadata)
224203, m_process_id(pid)
225204, m_agent_manager(agent_mngr)
205+ , m_tmp_file(nullptr )
226206, m_tracing_session(nullptr )
227207{
228208 if (get_caching_perfetto ())
@@ -285,8 +265,14 @@ perfetto_post_processing::start_session()
285265 ROCPROFSYS_VERBOSE (2 ,
286266 " Starting perfetto post-processing session with cached data...\n " );
287267
288- auto temp_fd = get_perffeto_temp_fd (std::to_string (m_process_id));
289-
268+ int temp_fd = -1 ;
269+ if (config::get_use_tmp_files ())
270+ {
271+ auto _base = JOIN (" -" , " cached-perfetto-trace" , std::to_string (m_process_id));
272+ m_tmp_file = config::get_tmp_file (_base, " proto" );
273+ m_tmp_file->open (O_RDWR | O_CREAT | O_TRUNC, 0600 );
274+ temp_fd = m_tmp_file->fd ;
275+ }
290276 m_tracing_session->Setup (m_session_config, temp_fd);
291277 m_tracing_session->StartBlocking ();
292278}
@@ -312,18 +298,17 @@ perfetto_post_processing::post_process(bool& _perfetto_output_error)
312298 stop_session ();
313299
314300 auto _get_session_data = [this ]() {
315- auto _data = char_vec_t {};
316- auto _tmp_file = get_perfetto_tmp_file ();
317- if (_tmp_file && *_tmp_file)
301+ auto _data = char_vec_t {};
302+ if (m_tmp_file && *m_tmp_file)
318303 {
319- _tmp_file ->close ();
320- FILE* _fdata = ::fopen (_tmp_file ->filename .c_str (), " rb" );
304+ m_tmp_file ->close ();
305+ FILE* _fdata = ::fopen (m_tmp_file ->filename .c_str (), " rb" );
321306
322307 if (!_fdata)
323308 {
324309 ROCPROFSYS_VERBOSE (
325310 -1 , " Error! perfetto temp trace file '%s' could not be read" ,
326- _tmp_file ->filename .c_str ());
311+ m_tmp_file ->filename .c_str ());
327312 return char_vec_t { m_tracing_session->ReadTraceBlocking () };
328313 }
329314
@@ -338,7 +323,7 @@ perfetto_post_processing::post_process(bool& _perfetto_output_error)
338323 ROCPROFSYS_CI_THROW (
339324 _fnum_read != _fnum_elem,
340325 " Error! read %zu elements from perfetto trace file '%s'. Expected %zu\n " ,
341- _fnum_read, _tmp_file ->filename .c_str (), _fnum_elem);
326+ _fnum_read, m_tmp_file ->filename .c_str (), _fnum_elem);
342327 }
343328 else
344329 {
@@ -392,12 +377,11 @@ perfetto_post_processing::post_process(bool& _perfetto_output_error)
392377 _filename.c_str ());
393378 }
394379
395- auto & _tmp_file = get_perfetto_tmp_file ();
396- if (_tmp_file)
380+ if (m_tmp_file)
397381 {
398- _tmp_file ->close ();
399- _tmp_file ->remove ();
400- _tmp_file .reset ();
382+ m_tmp_file ->close ();
383+ m_tmp_file ->remove ();
384+ m_tmp_file .reset ();
401385 }
402386
403387 m_tracing_session.reset ();
@@ -440,14 +424,12 @@ perfetto_post_processing::get_kernel_dispatch_callback() const
440424 ctx, { { " begin_ns" , _beg_ts },
441425 { " end_ns" , _end_ts },
442426 { " corr_id" , _corr_id },
443- { " stream_id" , static_cast <uint64_t >(_stream_handle) },
444- { " queue" , static_cast <uint64_t >(_queue_id_handle) },
445- { " dispatch_id" , static_cast <uint64_t >(_kds.dispatch_id ) },
446- { " kernel_id" , static_cast <uint64_t >(_kds.kernel_id ) },
447- { " private_segment_size" ,
448- static_cast <uint64_t >(_kds.private_segment_size ) },
449- { " group_segment_size" ,
450- static_cast <uint64_t >(_kds.group_segment_size ) },
427+ { " stream_id" , _stream_handle },
428+ { " queue" , _queue_id_handle },
429+ { " dispatch_id" , _kds.dispatch_id },
430+ { " kernel_id" , _kds.kernel_id },
431+ { " private_segment_size" , _kds.private_segment_size },
432+ { " group_segment_size" , _kds.group_segment_size },
451433 { " workgroup_size" ,
452434 JOIN (" " , " (" ,
453435 JOIN (' ,' , _kds.workgroup_size_x , _kds.workgroup_size_y ,
@@ -500,18 +482,16 @@ perfetto_post_processing::get_memory_copy_callback() const
500482 category::rocm_memory_copy{}, _track_desc, _dst_agent_log_node_id, _thrd_id);
501483
502484 auto add_perfetto_annotations = [&](::perfetto::EventContext ctx) {
503- annotate_perfetto (
504- ctx,
505- { { " begin_ns" , _beg_ts },
506- { " end_ns" , _end_ts },
507- { " corr_id" , _corr_id },
508- { " stream_id" , static_cast <uint64_t >(_stream_id) },
509- { " bytes" , static_cast <uint64_t >(_mcs.bytes ) },
510- { " src_agent_id" , static_cast <uint64_t >(_mcs.src_agent_id_handle ) },
511- { " dst_agent_id" , static_cast <uint64_t >(_mcs.dst_agent_id_handle ) },
512- { " operation" , _name },
513- { " src_address" , static_cast <uint64_t >(_mcs.src_address_value ) },
514- { " dst_address" , static_cast <uint64_t >(_mcs.dst_address_value ) } });
485+ annotate_perfetto (ctx, { { " begin_ns" , _beg_ts },
486+ { " end_ns" , _end_ts },
487+ { " corr_id" , _corr_id },
488+ { " stream_id" , _stream_id },
489+ { " bytes" , _mcs.bytes },
490+ { " src_agent_id" , _src_agent_log_node_id },
491+ { " dst_agent_id" , _dst_agent_log_node_id },
492+ { " operation" , _name },
493+ { " src_address" , _mcs.src_address_value },
494+ { " dst_address" , _mcs.dst_address_value } });
515495 };
516496
517497 tracing::push_perfetto (category::rocm_memory_copy{}, _name.c_str (), _track,
@@ -572,14 +552,13 @@ perfetto_post_processing::get_memory_allocate_callback() const
572552 _agent_logical_node_id, _thrd_id);
573553
574554 auto add_perfetto_annotations = [&](::perfetto::EventContext ctx) {
575- annotate_perfetto (
576- ctx, { { " begin_ns" , _beg_ts },
577- { " end_ns" , _end_ts },
578- { " corr_id" , _corr_id },
579- { " stream_id" , static_cast <uint64_t >(_stream_id) },
580- { " bytes" , static_cast <uint64_t >(_mas.allocation_size ) },
581- { " agent_id" , static_cast <uint64_t >(_mas.agent_id_handle ) },
582- { " address" , static_cast <uint64_t >(_mas.address_value ) } });
555+ annotate_perfetto (ctx, { { " begin_ns" , _beg_ts },
556+ { " end_ns" , _end_ts },
557+ { " corr_id" , _corr_id },
558+ { " stream_id" , _stream_id },
559+ { " bytes" , _alloc_size },
560+ { " agent_id" , _agent_logical_node_id },
561+ { " address" , _addr_val } });
583562 };
584563
585564 tracing::push_perfetto (category::rocm_memory_allocate{}, operation, _track,
0 commit comments