opencl-kernel-profiler is a perfetto-based OpenCL kernel profiler using the layering capability of the OpenCL-ICD-Loader
opencl-kernel-profiler is licensed under the terms of the Apache 2.0 license.
This is not an officially supported Google product. This project is not eligible for the Google Open Source Software Vulnerability Rewards Program.
opencl-kernel-profiler depends on the following:
- OpenCL-ICD-Loader
- OpenCL-Headers
- perfetto
- SPIRV-Tools (optional: to disassemble SPIR-V IL when
SPIRV_DISASSEMBLYis enabled)
opencl-kernel-profiler also (obviously) depends on a OpenCL implementation.
opencl-kernel-profiler uses CMake for its build system.
To compile it, run:
cmake -B <build_dir> -S <path-to-opencl-kernel-profiler> \
-DOPENCL_HEADER_PATH=<path-to-opencl-header> \
-DPERFETTO_SDK_PATH=<path-to-perfetto-sdk>
cmake --build <build_dir>For real-world examples, see:
- ChromeOS ebuild
- GitHub presubmit configuration
PERFETTO_SDK_PATH(REQUIRED): Path to perfetto SDK (expectsperfetto.ccandperfetto.hin this directory).PERFETTO_LIBRARY: Name of an existing perfetto library to link against (avoids compilingperfetto.cc).OPENCL_HEADER_PATH: Path to OpenCL-Headers.BACKEND: Perfetto backend to use:InProcess(default): The application generates the traces directly (Perfetto In-Process Mode).System: The system-wide Perfetto daemon (traced) collects the traces (Perfetto System Mode).
TRACE_MAX_SIZE(InProcess only): Default maximum trace buffer size in KB. Can be overridden at runtime. (Default:1024).TRACE_DEST(InProcess only): Default file path for the trace. Can be overridden at runtime. (Default:opencl-kernel-profiler.trace).SPIRV_DISASSEMBLY(optional): Enables SPIR-V disassembly in traces. Requires SPIRV-Tools.
To run an application with the opencl-kernel-profiler, one need to ensure the following point
- The application will link with the OpenCL-ICD-Loader. If not the case, one can override
LD_LIBRARY_PATHto point to where thelibOpenCL.socoming from the ICD Loader is. - The ICD Loader is build with layers enable (
ENABLE_OPENCL_LAYERS=ON). - The ICD Loader is using the correct OpenCL implementation. If not the case, one can override
OCL_ICD_FILENAMESto point to the appropriate OpenCL implementation library.
Make sure to have emerged and deployed the opencl-icd-loader as well as the opencl-kernel-profiler.
Then run the application using opencl-kernel-profiler.sh. This script will take care of setting all the environment variables needed to run with the opencl-kernel-profiler.
- Clone the project under
<aosp>/external/opencl-kernel-profiler - Compile the project:
m opencl-kernel-profiler
- Push the library and the
.layfile to the device. Note that/vendorpartition is usually read-only, so you may need to remount it first:adb root adb disable-verity adb reboot # Wait for the device to reboot, then: adb root adb remount adb push $OUT/vendor/lib64/opencl-kernel-profiler.so /vendor/lib64/ adb push $OUT/vendor/etc/Khronos/OpenCL/layers/opencl-kernel-profiler.lay /vendor/etc/Khronos/OpenCL/layers/
Any application using the OpenCL-ICD-Loader will go through the opencl-kernel-profiler.
The profiler can be configured at runtime using the following environment variables:
CLKP_TRACE_DEST(InProcess backend only): File path where the Perfetto trace will be saved. (Default:opencl-kernel-profiler.trace).CLKP_TRACE_MAX_SIZE(InProcess backend only): Maximum size of the trace buffer in KB. (Default:1024).CLKP_KERNEL_DIR: Directory path where kernel sources, binaries, and IL will be dumped. If not set, dumping is disabled.
Once traces have been generated, one can view them using the Perfetto Trace Viewer.
It is also possible to make SQL queries using the trace_processor tool. See the Perfetto SQL Analysis Quickstart.
Here is a simple example to extract all kernel sources from a trace:
echo "SELECT EXTRACT_ARG(arg_set_id, 'debug.string') FROM slice WHERE slice.name='clCreateProgramWithSource-args'" \
| ./trace_processor -q /dev/stdin <opencl-kernel-profiler.trace>If CLKP_KERNEL_DIR is set, the profiler dumps all programs/kernels to disk:
- OpenCL C sources are saved with a
.clextension. - Compiled binaries are saved with a
.binextension. - Intermediate Language (IL) is saved with
.spv(for SPIR-V) or.ilextension. - SPIR-V disassembly is saved with
.spvasmextension (ifSPIRV_DISASSEMBLYis enabled).
If CLKP_KERNEL_DIR is not set, no files are written. This dumping occurs independently of Perfetto tracing.
The layer intercepts the following OpenCL APIs to instrument execution and dump resources:
clCreateCommandQueue/clCreateCommandQueueWithProperties: ForcesCL_QUEUE_PROFILING_ENABLEto ensure hardware timestamps are available.clCreateProgramWithSource: Emits the source code to the trace (as an instant event) and dumps it toCLKP_KERNEL_DIRif configured.clCreateProgramWithBinary: Dumps the binary toCLKP_KERNEL_DIRif configured.clCreateProgramWithIL: Emits SPIR-V disassembly (if enabled) to the trace and dumps IL/disassembly toCLKP_KERNEL_DIRif configured.clCreateKernel: Tracks kernel-to-program relationships and kernel names.clEnqueueNDRangeKernel: Enqueues the kernel and registers a completion callback. The callback retrieves GPU start/end timestamps viaclGetEventProfilingInfoand emits a corresponding Perfetto slice.clReleaseCommandQueue: Cleans up the background helper thread and resources associated with the queue.
Every intercepted host API call also generates a host-side Perfetto slice.