diff --git a/src/api/rocprofilerv2.cpp b/src/api/rocprofilerv2.cpp index f8af37f1..9792c2af 100644 --- a/src/api/rocprofilerv2.cpp +++ b/src/api/rocprofilerv2.cpp @@ -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 @@ -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 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 @@ -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; } diff --git a/src/core/profiling_lock.cpp b/src/core/profiling_lock.cpp index 1d44c0b1..efa15b26 100644 --- a/src/core/profiling_lock.cpp +++ b/src/core/profiling_lock.cpp @@ -9,8 +9,8 @@ #include #include #include -#include "util/exception.h" #include "profiling_lock.h" +#include #define ROCPROFILER_LOCK_FILE "/tmp/rocprofiler_process.lock" #define ROCPROFILER_PID_FILE "/tmp/rocprofiler.pid" @@ -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()); } @@ -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) { diff --git a/src/core/profiling_lock.h b/src/core/profiling_lock.h index 4c813388..f05d3fa4 100644 --- a/src/core/profiling_lock.h +++ b/src/core/profiling_lock.h @@ -1,6 +1,7 @@ #ifndef _SRC_CORE_PROFILING_LOCK_H #define _SRC_CORE_PROFILING_LOCK_H +#include enum LockMode{ PROFILER_V1_LOCK, @@ -18,6 +19,7 @@ class ProfilingLock { const char *lock_file; const char *pid_file; + std::atomic already_locked{false}; int lock; }; diff --git a/src/core/rocprofiler.cpp b/src/core/rocprofiler.cpp index b8b92528..225f54e9 100644 --- a/src/core/rocprofiler.cpp +++ b/src/core/rocprofiler.cpp @@ -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(); @@ -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) { @@ -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) {