Skip to content

Commit

Permalink
Only check lock if counter collection is used
Browse files Browse the repository at this point in the history
Only check the lock if counter collection is being
attempted by the profiler. Not when it is loaded.

SWDEV-474455

Change-Id: Ie14de3c8db57e0cbd279ffca51c333a375ca8654
Signed-off-by: Benjamin Welton <[email protected]>
  • Loading branch information
Benjamin Welton authored and bwelton committed Jul 22, 2024
1 parent 89dce3c commit c3404b8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
24 changes: 23 additions & 1 deletion src/api/rocprofilerv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,23 @@ ROCPROFILER_API rocprofiler_status_t rocprofiler_create_filter(
// rocprofiler::Exception(ROCPROFILER_STATUS_ERROR_FILTER_DATA_CORRUPTED); if (error_code == 0)
// throw rocprofiler::Exception(ROCPROFILER_STATUS_ERROR_SESSION_FILTER_DATA_MISMATCH);
auto& rocprofiler_singleton = rocprofiler::ROCProfiler_Singleton::GetInstance();
switch (filter_kind) {
case ROCPROFILER_COUNTERS_COLLECTION:
case ROCPROFILER_COUNTERS_SAMPLER:
try{
ProfilingLock::Lock(PROFILER_V2_LOCK);
}catch(std::exception& e){
std::cout << e.what();
abort();
}
break;
case ROCPROFILER_DISPATCH_TIMESTAMPS_COLLECTION:
case ROCPROFILER_PC_SAMPLING_COLLECTION:
case ROCPROFILER_ATT_TRACE_COLLECTION:
case ROCPROFILER_SPM_COLLECTION:
case ROCPROFILER_API_TRACE:
break;
}
if (!rocprofiler_singleton.FindSession(session_id))
throw rocprofiler::Exception(ROCPROFILER_STATUS_ERROR_SESSION_NOT_FOUND);
*filter_id = rocprofiler_singleton
Expand Down Expand Up @@ -507,6 +524,12 @@ ROCPROFILER_API rocprofiler_status_t rocprofiler_device_profiling_session_create
int cpu_index, int gpu_index) {
API_METHOD_PREFIX
std::vector<std::string> counters(counter_names, counter_names + num_counters);
try {
ProfilingLock::Lock(PROFILER_V2_LOCK);
} catch (std::exception& e) {
std::cout << e.what();
abort();
}
*session_id =
rocprofiler::ROCProfiler_Singleton::GetInstance().CreateDeviceProfilingSession(counters, cpu_index, gpu_index);
API_METHOD_SUFFIX
Expand Down Expand Up @@ -618,7 +641,6 @@ ROCPROFILER_EXPORT bool OnLoad(HsaApiTable* table, uint64_t runtime_version,
uint64_t failed_tool_count, const char* const* failed_tool_names) {
if (started) rocprofiler::fatal("HSA Tool started already!");
started = true;
ProfilingLock::Lock(PROFILER_V2_LOCK);
rocprofiler::HSASupport_Singleton::GetInstance().HSAInitialize(table);
return true;
}
Expand Down
16 changes: 9 additions & 7 deletions src/core/profiling_lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <sstream>
#include <sstream>
#include <cstring>
#include "util/exception.h"
#include "profiling_lock.h"
#include <stdexcept>

#define ROCPROFILER_LOCK_FILE "/tmp/rocprofiler_process.lock"
#define ROCPROFILER_PID_FILE "/tmp/rocprofiler.pid"
Expand Down Expand Up @@ -67,12 +67,12 @@ bool check_process_exists(int pid) {
}

void terminate_current_profiler_instance() {
EXC_RAISING(
0,
"\nA profiling instance already exists! Multiple profiling instances are not "
"allowed.\nCheck "
<< ROCPROFILER_PID_FILE
<< " and kill the process, delete this .pid file and try again.\nTerminating ...\n");
std::stringstream oss;
oss << "\nA profiling instance already exists! Multiple profiling instances are not "
<< "allowed.\nCheck " << ROCPROFILER_PID_FILE
<< " and kill the process, delete this .pid file and try again.\nTerminating "
"...\n";
throw std::runtime_error(oss.str());
}


Expand All @@ -95,6 +95,8 @@ void ProfilingLock::Lock(LockMode mode) {
bool is_standalone_mode_v1 = check_standalone_mode() && (mode == PROFILER_V1_LOCK);

ProfilingLock* profiling_lock = Instance();
// Check if we have already locked in this process
if (profiling_lock->already_locked.exchange(true)) return;
if (file_exists(profiling_lock->pid_file)) {
profiling_lock->lock = acquire_lock(profiling_lock->lock_file);
if (profiling_lock->lock < 1) {
Expand Down
2 changes: 2 additions & 0 deletions src/core/profiling_lock.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#ifndef _SRC_CORE_PROFILING_LOCK_H
#define _SRC_CORE_PROFILING_LOCK_H
#include <atomic>

enum LockMode{
PROFILER_V1_LOCK,
Expand All @@ -18,6 +19,7 @@ class ProfilingLock {

const char *lock_file;
const char *pid_file;
std::atomic<bool> already_locked{false};
int lock;
};

Expand Down
3 changes: 2 additions & 1 deletion src/core/rocprofiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ ROCPROFILER_EXPORT extern const uint32_t HSA_AMD_TOOL_PRIORITY = 25;
PUBLIC_API bool OnLoad(HsaApiTable* table, uint64_t runtime_version, uint64_t failed_tool_count,
const char* const* failed_tool_names) {
ONLOAD_TRACE_BEG();
ProfilingLock::Lock(PROFILER_V1_LOCK);
rocprofiler::SaveHsaApi(table);
rocprofiler::ProxyQueue::InitFactory();

Expand Down Expand Up @@ -488,6 +487,7 @@ PUBLIC_API hsa_status_t rocprofiler_open(hsa_agent_t agent, rocprofiler_feature_
uint32_t feature_count, rocprofiler_t** handle,
uint32_t mode, rocprofiler_properties_t* properties) {
API_METHOD_PREFIX
ProfilingLock::Lock(PROFILER_V1_LOCK);
rocprofiler::util::HsaRsrcFactory* hsa_rsrc = &rocprofiler::util::HsaRsrcFactory::Instance();
const rocprofiler::util::AgentInfo* agent_info = hsa_rsrc->GetAgentInfo(agent);
if (agent_info == NULL) {
Expand Down Expand Up @@ -673,6 +673,7 @@ rocprofiler_pool_open(hsa_agent_t agent, // GPU handle
rocprofiler_pool_properties_t* properties) // pool properties
{
API_METHOD_PREFIX
ProfilingLock::Lock(PROFILER_V1_LOCK);
rocprofiler::util::HsaRsrcFactory* hsa_rsrc = &rocprofiler::util::HsaRsrcFactory::Instance();
const rocprofiler::util::AgentInfo* agent_info = hsa_rsrc->GetAgentInfo(agent);
if (agent_info == NULL) {
Expand Down

0 comments on commit c3404b8

Please sign in to comment.