OpenCL resource tracking

I wrote about the woes I had with the abysmal OpenCL implementations by both AMD and NVIDIA. Now, it’s not always the fault of others and I recently had to debug a GPU memory leak originating from our own software. The first thing I needed to indentify was if all OpenCL resources were referenced and dereferenced equally. However, the entire system is (as usual I suppose) a dynamic pile of layers of abstractions, so simple reasoning from the code was out of the question rather quickly. I remembered the GObject tracking library from which I took the interception and backtrace bits, re-implemented the OpenCL calls dealing with resource allocation and unreferencing to make the LD_PRELOAD library libocl-stat.so. Usage is similar to the GObject tracking library gobject-list:

$ LD_PRELOAD=/path/to/libocl-stat.so ./app

with an example output like (now):

OpenCL objects alive
====================
Contexts        1/1
Command queues  0/1
Buffers         0/9
Samplers        0/0
Kernels         3/11

Memory leaks
============
Leaking         0 B