Skip to content

Commit 39cb7dc

Browse files
authored
extend LeakChecking to track SVM and USM allocations (#388)
1 parent 4fd27ff commit 39cb7dc

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

intercept/src/dispatch.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6338,6 +6338,7 @@ CL_API_ENTRY void* CL_API_CALL CLIRN(clSVMAlloc) (
63386338
// if ErrorLogging is enabled.
63396339
cl_int errorCode = ( retVal != NULL ) ? CL_SUCCESS : CL_INVALID_OPERATION;
63406340
CHECK_ERROR( errorCode );
6341+
ADD_POINTER_ALLOCATION( retVal );
63416342
CALL_LOGGING_EXIT( errorCode, "returned %p", retVal );
63426343

63436344
return retVal;
@@ -6370,6 +6371,7 @@ CL_API_ENTRY void CL_API_CALL CLIRN(clSVMFree) (
63706371

63716372
HOST_PERFORMANCE_TIMING_END();
63726373
REMOVE_SVM_ALLOCATION( svm_pointer );
6374+
ADD_POINTER_FREE( svm_pointer );
63736375
CALL_LOGGING_EXIT( CL_SUCCESS );
63746376
}
63756377
}
@@ -9736,6 +9738,7 @@ CL_API_ENTRY void* CL_API_CALL clHostMemAllocINTEL(
97369738
ADD_USM_ALLOCATION( retVal, size );
97379739
USM_ALLOC_PROPERTIES_CLEANUP( newProperties );
97389740
CHECK_ERROR( errcode_ret[0] );
9741+
ADD_POINTER_ALLOCATION( retVal );
97399742
CALL_LOGGING_EXIT( errcode_ret[0], "returned %p", retVal );
97409743

97419744
return retVal;
@@ -9812,6 +9815,7 @@ CL_API_ENTRY void* CL_API_CALL clDeviceMemAllocINTEL(
98129815
ADD_USM_ALLOCATION( retVal, size );
98139816
USM_ALLOC_PROPERTIES_CLEANUP( newProperties );
98149817
CHECK_ERROR( errcode_ret[0] );
9818+
ADD_POINTER_ALLOCATION( retVal );
98159819
CALL_LOGGING_EXIT( errcode_ret[0], "returned %p", retVal );
98169820

98179821
return retVal;
@@ -9888,6 +9892,7 @@ CL_API_ENTRY void* CL_API_CALL clSharedMemAllocINTEL(
98889892
ADD_USM_ALLOCATION( retVal, size );
98899893
USM_ALLOC_PROPERTIES_CLEANUP( newProperties );
98909894
CHECK_ERROR( errcode_ret[0] );
9895+
ADD_POINTER_ALLOCATION( retVal );
98919896
CALL_LOGGING_EXIT( errcode_ret[0], "returned %p", retVal );
98929897

98939898
return retVal;
@@ -9924,6 +9929,7 @@ CL_API_ENTRY cl_int CL_API_CALL clMemFreeINTEL(
99249929
HOST_PERFORMANCE_TIMING_END();
99259930
REMOVE_USM_ALLOCATION( ptr );
99269931
CHECK_ERROR( retVal );
9932+
ADD_POINTER_FREE( ptr );
99279933
CALL_LOGGING_EXIT( retVal );
99289934

99299935
return retVal;
@@ -9961,6 +9967,7 @@ clMemBlockingFreeINTEL(
99619967
HOST_PERFORMANCE_TIMING_END();
99629968
REMOVE_USM_ALLOCATION( ptr );
99639969
CHECK_ERROR( retVal );
9970+
ADD_POINTER_FREE( ptr );
99649971
CALL_LOGGING_EXIT( retVal );
99659972
DEVICE_PERFORMANCE_TIMING_CHECK();
99669973
FLUSH_CHROME_TRACE_BUFFERING();

intercept/src/intercept.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,18 @@ inline CObjectTracker& CLIntercept::objectTracker()
20082008
pIntercept->objectTracker().AddRelease(_obj); \
20092009
}
20102010

2011+
#define ADD_POINTER_ALLOCATION( _ptr ) \
2012+
if( pIntercept->config().LeakChecking ) \
2013+
{ \
2014+
pIntercept->objectTracker().AddPointerAllocation(_ptr); \
2015+
}
2016+
2017+
#define ADD_POINTER_FREE( _ptr ) \
2018+
if( pIntercept->config().LeakChecking ) \
2019+
{ \
2020+
pIntercept->objectTracker().AddPointerFree(_ptr); \
2021+
}
2022+
20112023
///////////////////////////////////////////////////////////////////////////////
20122024
//
20132025
#define LOG_CLINFO() \

intercept/src/objtracker.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ void CObjectTracker::ReportHelper(
4242
}
4343
}
4444

45+
void CObjectTracker::ReportHelper(
46+
const std::string& label,
47+
const CObjectTracker::CPointerTracker& tracker,
48+
std::ostream& os )
49+
{
50+
size_t numAllocations = tracker.NumAllocations.load(std::memory_order_relaxed);
51+
size_t numFrees = tracker.NumFrees.load(std::memory_order_relaxed);
52+
53+
if( numFrees < numAllocations )
54+
{
55+
os << "Possible leak of type " << label << "!" << std::endl;
56+
os << " Number of Allocations: " << numAllocations << std::endl;
57+
os << " Number of Frees: " << numFrees << std::endl;
58+
}
59+
else if( numFrees > numAllocations )
60+
{
61+
// If there are more frees than allocations then this is an unexpected
62+
// situation. It usually means that some allocations aren't tracked
63+
// correctly, or that a free returned an error.
64+
os << "Unexpected counts for type " << label << "!" << std::endl;
65+
os << " Number of Allocations: " << numAllocations << std::endl;
66+
os << " Number of Frees: " << numFrees << std::endl;
67+
}
68+
else if( numAllocations )
69+
{
70+
os << "No " << label << " leaks detected." << std::endl;
71+
}
72+
}
73+
4574
void CObjectTracker::writeReport( std::ostream& os )
4675
{
4776
os << std::endl;
@@ -55,4 +84,5 @@ void CObjectTracker::writeReport( std::ostream& os )
5584
ReportHelper( "cl_event", m_Events, os );
5685
ReportHelper( "cl_semaphore_khr", m_Semaphores, os );
5786
ReportHelper( "cl_command_buffer_khr", m_CommandBuffers, os );
87+
ReportHelper( "SVM/USM allocation", m_Pointers, os );
5888
}

intercept/src/objtracker.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ class CObjectTracker
4444
}
4545
}
4646

47+
void AddPointerAllocation( const void* ptr )
48+
{
49+
if( ptr )
50+
{
51+
m_Pointers.NumAllocations.fetch_add(1, std::memory_order_relaxed);
52+
}
53+
}
54+
55+
void AddPointerFree( const void* ptr )
56+
{
57+
if( ptr )
58+
{
59+
m_Pointers.NumFrees.fetch_add(1, std::memory_order_relaxed);
60+
}
61+
}
62+
4763
private:
4864
struct CTracker
4965
{
@@ -57,6 +73,16 @@ class CObjectTracker
5773
std::atomic<size_t> NumReleases;
5874
};
5975

76+
struct CPointerTracker
77+
{
78+
CPointerTracker() :
79+
NumAllocations(0),
80+
NumFrees(0) {};
81+
82+
std::atomic<size_t> NumAllocations;
83+
std::atomic<size_t> NumFrees;
84+
};
85+
6086
CTracker m_Devices;
6187
CTracker m_Contexts;
6288
CTracker m_CommandQueues;
@@ -80,10 +106,16 @@ class CObjectTracker
80106
CTracker& GetTracker( cl_semaphore_khr ) { return m_Semaphores; }
81107
CTracker& GetTracker( cl_command_buffer_khr ) { return m_CommandBuffers; }
82108

109+
CPointerTracker m_Pointers;
110+
83111
static void ReportHelper(
84112
const std::string& label,
85113
const CTracker& tracker,
86114
std::ostream& os );
115+
static void ReportHelper(
116+
const std::string& label,
117+
const CPointerTracker& tracker,
118+
std::ostream& os );
87119

88120
DISALLOW_COPY_AND_ASSIGN( CObjectTracker );
89121
};

0 commit comments

Comments
 (0)