Skip to content

Commit

Permalink
SWDEV-476512: Further rocsys fixes
Browse files Browse the repository at this point in the history
Change-Id: I9ae8534a2b6b23f76514920a6c4d39025bc882bc
(cherry picked from commit c4adefc)
  • Loading branch information
ApoKalipse-V committed Aug 8, 2024
1 parent 3f1d70b commit 7b3cccc
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 85 deletions.
57 changes: 30 additions & 27 deletions plugin/perfetto/perfetto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ std::string get_kernel_name(rocprofiler_record_profiler_t& profiler_record) {

class perfetto_plugin_t {
public:
perfetto_plugin_t() {
perfetto_plugin_t(int filename_ext) {
const char* output_dir = getenv("OUTPUT_PATH");
const char* temp_file_name = getenv("OUT_FILE_NAME");
std::string output_file_name = temp_file_name ? std::string(temp_file_name) + "_" : "";
if (filename_ext > 0)
output_file_name += "_run"+std::to_string(filename_ext)+"_";

if (output_dir == nullptr) output_dir = "./";

Expand Down Expand Up @@ -193,13 +195,16 @@ class perfetto_plugin_t {
is_valid_ = true;
}

void delete_perfetto_plugin() {
if (is_valid_ && tracing_session_ && internal_buffer_finished.load(std::memory_order_acquire)) {
writing_lock.lock();
bool delete_perfetto_plugin() {
if (is_valid_ && tracing_session_)
{
tracing_session_->StopBlocking();
// close(file_descriptor_);
writing_lock.unlock();
is_valid_ = false;
close(file_descriptor_);
tracing_session_.reset();
return true;
}
return false;
}

const char* GetDomainName(rocprofiler_tracer_activity_domain_t domain) {
Expand Down Expand Up @@ -232,8 +237,6 @@ class perfetto_plugin_t {
// ToDO: rename this variable?
if (!tracing_session_) rocprofiler::warning("Tracing session is deleted!\n");

writing_lock.lock();

const uint64_t device_id = profiler_record.gpu_id.handle;
const uint64_t queue_id = profiler_record.queue_id.handle;
const uint64_t correlation_id = profiler_record.correlation_id.value;
Expand Down Expand Up @@ -321,14 +324,12 @@ class perfetto_plugin_t {
TRACE_COUNTER("COUNTERS", counters_track, profiler_record.timestamps.end.value, 0);
}

writing_lock.unlock();
return 0;
}

int FlushTracerRecord(rocprofiler_record_tracer_t tracer_record,
rocprofiler_session_id_t session_id) {
if (!tracing_session_) rocprofiler::warning("Tracing session is deleted!\n");
writing_lock.lock();
uint64_t device_id = tracer_record.agent_id.handle;
const char* operation_name_c = nullptr;
// ROCTX domain Operation ID doesn't have a name
Expand Down Expand Up @@ -556,7 +557,6 @@ class perfetto_plugin_t {
break;
}
}
writing_lock.unlock();
return 0;
}

Expand Down Expand Up @@ -585,7 +585,6 @@ class perfetto_plugin_t {
}
rocprofiler_next_record(begin, &begin, session_id, buffer_id);
}
internal_buffer_finished.exchange(true, std::memory_order_acq_rel);
return 0;
}

Expand All @@ -598,8 +597,6 @@ class perfetto_plugin_t {
bool is_valid_{false};
size_t roctx_track_entries_{0};

std::atomic<bool> internal_buffer_finished{false};

// Correlate stream id(s) with correlation id(s) to identify the stream id of every HIP activity
std::unordered_map<uint64_t, uint64_t> stream_ids_;

Expand Down Expand Up @@ -650,41 +647,47 @@ int rocprofiler_plugin_initialize(uint32_t rocprofiler_major_version,
rocprofiler_minor_version > ROCPROFILER_VERSION_MINOR)
return -1;

// std::lock_guard<std::mutex> lock(writing_lock);
writing_lock.lock();
if (perfetto_plugin != nullptr) return -1;
//if (perfetto_plugin != nullptr && perfetto_plugin->IsValid()) return -1;

perfetto_plugin = new perfetto_plugin_t();
if (perfetto_plugin->IsValid()) {
writing_lock.unlock();
std::lock_guard<std::mutex> lock(writing_lock);
if (perfetto_plugin != nullptr)
perfetto_plugin->delete_perfetto_plugin();

static int perfetto_init_count = 0;
perfetto_plugin = new perfetto_plugin_t(perfetto_init_count++);

if (perfetto_plugin->IsValid())
return 0;
}

// delete perfetto_plugin;
// perfetto_plugin = nullptr;
writing_lock.unlock();
return -1;
}

void rocprofiler_plugin_finalize() {

void rocprofiler_plugin_finalize()
{
std::lock_guard<std::mutex> lock(writing_lock);
if (!perfetto_plugin) return;
perfetto_plugin->delete_perfetto_plugin();
// delete perfetto_plugin;
// perfetto_plugin = nullptr;
if (perfetto_plugin->delete_perfetto_plugin())
{
delete perfetto_plugin;
perfetto_plugin = nullptr;
}
}

ROCPROFILER_EXPORT int rocprofiler_plugin_write_buffer_records(
const rocprofiler_record_header_t* begin, const rocprofiler_record_header_t* end,
rocprofiler_session_id_t session_id, rocprofiler_buffer_id_t buffer_id) {

std::lock_guard<std::mutex> lock(writing_lock);
if (!perfetto_plugin || !perfetto_plugin->IsValid()) return -1;
return perfetto_plugin->WriteBufferRecords(begin, end, session_id, buffer_id);
}

ROCPROFILER_EXPORT int rocprofiler_plugin_write_record(rocprofiler_record_tracer_t record) {
if (record.header.id.handle == 0) return 0;

std::lock_guard<std::mutex> lock(writing_lock);
if (!perfetto_plugin || !perfetto_plugin->IsValid()) return -1;
return perfetto_plugin->FlushTracerRecord(record, rocprofiler_session_id_t{0});
}
8 changes: 3 additions & 5 deletions src/core/hsa/queues/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,10 +538,7 @@ void Queue::ResetSessionID(rocprofiler_session_id_t id)
{
std::unique_lock<std::shared_mutex> session_id_lock(session_id_mutex);
session_id = id;
if (session_id.handle != 0)
session = rocprofiler::ROCProfiler_Singleton::GetInstance().GetSession(session_id);
else
session = nullptr;
session = nullptr;
}

bool Queue::CheckNeededProfileConfigs()
Expand Down Expand Up @@ -618,7 +615,8 @@ void Queue::WriteInterceptor(const void* packets, uint64_t pkt_count, uint64_t u

std::shared_lock<std::shared_mutex> session_id_lock(session_id_mutex);

if (session_id.handle == 0 || session_id.handle != rocprofiler::ROCProfiler_Singleton::GetInstance().GetCurrentSessionId().handle)
if (session == nullptr || session_id.handle == 0 ||
session_id.handle != rocprofiler::ROCProfiler_Singleton::GetInstance().GetCurrentSessionId().handle)
{
session_id_lock.unlock();
CheckNeededProfileConfigs();
Expand Down
2 changes: 1 addition & 1 deletion src/core/session/att/att.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void AttTracer::AddPendingSignals(
kernel_object,
original_completion_signal,
new_completion_signal,
session_id_,
session_id,
buffer_id,
profile,
kernel_properties,
Expand Down
9 changes: 4 additions & 5 deletions src/core/session/profiler/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,14 @@ void Profiler::AddPendingSignals(
uint64_t correlation_id)
{
std::lock_guard<std::mutex> lock(sessions_pending_signals_lock_);
if (bIsSessionDestroying.load())
return;

if (sessions_pending_signals_.find(writer_id) == sessions_pending_signals_.end())
sessions_pending_signals_.emplace(writer_id, std::vector<pending_signal_ptr_t>{});

sessions_pending_signals_.at(writer_id).emplace_back(
new pending_signal_t{
kernel_object, original_completion_signal, new_completion_signal,
session_id_, buffer_id, session_data_count, std::move(profile),
session_id, buffer_id, session_data_count, std::move(profile),
kernel_properties, thread_id, queue_index, correlation_id
}
);
Expand All @@ -143,9 +141,10 @@ void Profiler::AddPendingSignals(
std::vector<pending_signal_ptr_t> Profiler::MovePendingSignals(uint32_t writer_id)
{
std::lock_guard<std::mutex> lock(sessions_pending_signals_lock_);

auto it = sessions_pending_signals_.find(writer_id);
if (it == sessions_pending_signals_.end())
rocprofiler::fatal("writer_id is not found in the pending_signals");
return {};

auto move_pending = std::move(it->second);
sessions_pending_signals_.erase(writer_id);
Expand All @@ -157,8 +156,8 @@ std::vector<pending_signal_ptr_t> Profiler::MovePendingSignals(uint32_t writer_i

void Profiler::WaitForPendingAndDestroy()
{
bIsSessionDestroying.store(true);
std::unique_lock<std::mutex> lk(sessions_pending_signals_lock_);
bIsSessionDestroying.store(true);
if (sessions_pending_signals_.size() == 0)
return;

Expand Down
1 change: 1 addition & 0 deletions src/tools/rocsys/rocsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,6 @@ int main(int argc, char* argv[]) {
report("ROCSYS:: Error: Not possible to reach here, please report(invalid sys_type)!\n", 1);
}
}
msync(shmd, sizeof(shmd->command), MS_SYNC | MS_INVALIDATE);
return 1;
}
Loading

0 comments on commit 7b3cccc

Please sign in to comment.