Skip to content

extend LeakChecking to track SVM and USM allocations #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions intercept/src/dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6338,6 +6338,7 @@ CL_API_ENTRY void* CL_API_CALL CLIRN(clSVMAlloc) (
// if ErrorLogging is enabled.
cl_int errorCode = ( retVal != NULL ) ? CL_SUCCESS : CL_INVALID_OPERATION;
CHECK_ERROR( errorCode );
ADD_POINTER_ALLOCATION( retVal );
CALL_LOGGING_EXIT( errorCode, "returned %p", retVal );

return retVal;
Expand Down Expand Up @@ -6370,6 +6371,7 @@ CL_API_ENTRY void CL_API_CALL CLIRN(clSVMFree) (

HOST_PERFORMANCE_TIMING_END();
REMOVE_SVM_ALLOCATION( svm_pointer );
ADD_POINTER_FREE( svm_pointer );
CALL_LOGGING_EXIT( CL_SUCCESS );
}
}
Expand Down Expand Up @@ -9736,6 +9738,7 @@ CL_API_ENTRY void* CL_API_CALL clHostMemAllocINTEL(
ADD_USM_ALLOCATION( retVal, size );
USM_ALLOC_PROPERTIES_CLEANUP( newProperties );
CHECK_ERROR( errcode_ret[0] );
ADD_POINTER_ALLOCATION( retVal );
CALL_LOGGING_EXIT( errcode_ret[0], "returned %p", retVal );

return retVal;
Expand Down Expand Up @@ -9812,6 +9815,7 @@ CL_API_ENTRY void* CL_API_CALL clDeviceMemAllocINTEL(
ADD_USM_ALLOCATION( retVal, size );
USM_ALLOC_PROPERTIES_CLEANUP( newProperties );
CHECK_ERROR( errcode_ret[0] );
ADD_POINTER_ALLOCATION( retVal );
CALL_LOGGING_EXIT( errcode_ret[0], "returned %p", retVal );

return retVal;
Expand Down Expand Up @@ -9888,6 +9892,7 @@ CL_API_ENTRY void* CL_API_CALL clSharedMemAllocINTEL(
ADD_USM_ALLOCATION( retVal, size );
USM_ALLOC_PROPERTIES_CLEANUP( newProperties );
CHECK_ERROR( errcode_ret[0] );
ADD_POINTER_ALLOCATION( retVal );
CALL_LOGGING_EXIT( errcode_ret[0], "returned %p", retVal );

return retVal;
Expand Down Expand Up @@ -9924,6 +9929,7 @@ CL_API_ENTRY cl_int CL_API_CALL clMemFreeINTEL(
HOST_PERFORMANCE_TIMING_END();
REMOVE_USM_ALLOCATION( ptr );
CHECK_ERROR( retVal );
ADD_POINTER_FREE( ptr );
CALL_LOGGING_EXIT( retVal );

return retVal;
Expand Down Expand Up @@ -9961,6 +9967,7 @@ clMemBlockingFreeINTEL(
HOST_PERFORMANCE_TIMING_END();
REMOVE_USM_ALLOCATION( ptr );
CHECK_ERROR( retVal );
ADD_POINTER_FREE( ptr );
CALL_LOGGING_EXIT( retVal );
DEVICE_PERFORMANCE_TIMING_CHECK();
FLUSH_CHROME_TRACE_BUFFERING();
Expand Down
12 changes: 12 additions & 0 deletions intercept/src/intercept.h
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,18 @@ inline CObjectTracker& CLIntercept::objectTracker()
pIntercept->objectTracker().AddRelease(_obj); \
}

#define ADD_POINTER_ALLOCATION( _ptr ) \
if( pIntercept->config().LeakChecking ) \
{ \
pIntercept->objectTracker().AddPointerAllocation(_ptr); \
}

#define ADD_POINTER_FREE( _ptr ) \
if( pIntercept->config().LeakChecking ) \
{ \
pIntercept->objectTracker().AddPointerFree(_ptr); \
}

///////////////////////////////////////////////////////////////////////////////
//
#define LOG_CLINFO() \
Expand Down
30 changes: 30 additions & 0 deletions intercept/src/objtracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ void CObjectTracker::ReportHelper(
}
}

void CObjectTracker::ReportHelper(
const std::string& label,
const CObjectTracker::CPointerTracker& tracker,
std::ostream& os )
{
size_t numAllocations = tracker.NumAllocations.load(std::memory_order_relaxed);
size_t numFrees = tracker.NumFrees.load(std::memory_order_relaxed);

if( numFrees < numAllocations )
{
os << "Possible leak of type " << label << "!" << std::endl;
os << " Number of Allocations: " << numAllocations << std::endl;
os << " Number of Frees: " << numFrees << std::endl;
}
else if( numFrees > numAllocations )
{
// If there are more frees than allocations then this is an unexpected
// situation. It usually means that some allocations aren't tracked
// correctly, or that a free returned an error.
os << "Unexpected counts for type " << label << "!" << std::endl;
os << " Number of Allocations: " << numAllocations << std::endl;
os << " Number of Frees: " << numFrees << std::endl;
}
else if( numAllocations )
{
os << "No " << label << " leaks detected." << std::endl;
}
}

void CObjectTracker::writeReport( std::ostream& os )
{
os << std::endl;
Expand All @@ -55,4 +84,5 @@ void CObjectTracker::writeReport( std::ostream& os )
ReportHelper( "cl_event", m_Events, os );
ReportHelper( "cl_semaphore_khr", m_Semaphores, os );
ReportHelper( "cl_command_buffer_khr", m_CommandBuffers, os );
ReportHelper( "SVM/USM allocation", m_Pointers, os );
}
32 changes: 32 additions & 0 deletions intercept/src/objtracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ class CObjectTracker
}
}

void AddPointerAllocation( const void* ptr )
{
if( ptr )
{
m_Pointers.NumAllocations.fetch_add(1, std::memory_order_relaxed);
}
}

void AddPointerFree( const void* ptr )
{
if( ptr )
{
m_Pointers.NumFrees.fetch_add(1, std::memory_order_relaxed);
}
}

private:
struct CTracker
{
Expand All @@ -57,6 +73,16 @@ class CObjectTracker
std::atomic<size_t> NumReleases;
};

struct CPointerTracker
{
CPointerTracker() :
NumAllocations(0),
NumFrees(0) {};

std::atomic<size_t> NumAllocations;
std::atomic<size_t> NumFrees;
};

CTracker m_Devices;
CTracker m_Contexts;
CTracker m_CommandQueues;
Expand All @@ -80,10 +106,16 @@ class CObjectTracker
CTracker& GetTracker( cl_semaphore_khr ) { return m_Semaphores; }
CTracker& GetTracker( cl_command_buffer_khr ) { return m_CommandBuffers; }

CPointerTracker m_Pointers;

static void ReportHelper(
const std::string& label,
const CTracker& tracker,
std::ostream& os );
static void ReportHelper(
const std::string& label,
const CPointerTracker& tracker,
std::ostream& os );

DISALLOW_COPY_AND_ASSIGN( CObjectTracker );
};