| 1 |
/* |
| 2 |
* Copyright (C) 2019-2023 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "opencl/source/tracing/tracing_api.h" |
| 9 |
|
| 10 |
#include "opencl/source/tracing/tracing_handle.h" |
| 11 |
#include "opencl/source/tracing/tracing_notify.h" |
| 12 |
|
| 13 |
namespace HostSideTracing { |
| 14 |
|
| 15 |
// [XYZZ..Z] - { X - enabled/disabled bit, Y - locked/unlocked bit, ZZ..Z - client count bits } |
| 16 |
std::atomic<uint32_t> tracingState(0); |
| 17 |
TracingHandle *tracingHandle[tracingMaxHandleCount] = {nullptr}; |
| 18 |
std::atomic<uint32_t> tracingCorrelationId(0); |
| 19 |
|
| 20 |
bool addTracingClient() { |
| 21 |
uint32_t state = tracingState.load(std::memory_order_acquire); |
| 22 |
state = TRACING_SET_ENABLED_BIT(state); |
| 23 |
state = TRACING_UNSET_LOCKED_BIT(state); |
| 24 |
AtomicBackoff backoff; |
| 25 |
while (!tracingState.compare_exchange_weak(state, state + 1, std::memory_order_release, |
| 26 |
std::memory_order_acquire)) { |
| 27 |
if (!TRACING_GET_ENABLED_BIT(state)) { |
| 28 |
return false; |
| 29 |
} else if (TRACING_GET_LOCKED_BIT(state)) { |
| 30 |
DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(state) != 0); |
| 31 |
state = TRACING_UNSET_LOCKED_BIT(state); |
| 32 |
backoff.pause(); |
| 33 |
} else { |
| 34 |
backoff.pause(); |
| 35 |
} |
| 36 |
} |
| 37 |
return true; |
| 38 |
} |
| 39 |
|
| 40 |
void removeTracingClient() { |
| 41 |
DEBUG_BREAK_IF(!TRACING_GET_ENABLED_BIT(tracingState.load(std::memory_order_acquire))); |
| 42 |
DEBUG_BREAK_IF(TRACING_GET_LOCKED_BIT(tracingState.load(std::memory_order_acquire))); |
| 43 |
DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(tracingState.load(std::memory_order_acquire)) == 0); |
| 44 |
tracingState.fetch_sub(1, std::memory_order_acq_rel); |
| 45 |
} |
| 46 |
|
| 47 |
static void lockTracingState() { |
| 48 |
uint32_t state = tracingState.load(std::memory_order_acquire); |
| 49 |
state = TRACING_ZERO_CLIENT_COUNTER(state); |
| 50 |
state = TRACING_UNSET_LOCKED_BIT(state); |
| 51 |
AtomicBackoff backoff; |
| 52 |
while (!tracingState.compare_exchange_weak(state, TRACING_SET_LOCKED_BIT(state), |
| 53 |
std::memory_order_release, std::memory_order_acquire)) { |
| 54 |
state = TRACING_ZERO_CLIENT_COUNTER(state); |
| 55 |
state = TRACING_UNSET_LOCKED_BIT(state); |
| 56 |
backoff.pause(); |
| 57 |
} |
| 58 |
DEBUG_BREAK_IF(!TRACING_GET_LOCKED_BIT(tracingState.load(std::memory_order_acquire))); |
| 59 |
DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(tracingState.load(std::memory_order_acquire)) > 0); |
| 60 |
} |
| 61 |
|
| 62 |
static void unlockTracingState() { |
| 63 |
DEBUG_BREAK_IF(!TRACING_GET_LOCKED_BIT(tracingState.load(std::memory_order_acquire))); |
| 64 |
DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(tracingState.load(std::memory_order_acquire)) > 0); |
| 65 |
tracingState.fetch_and(~tracingStateLockedBit, std::memory_order_acq_rel); |
| 66 |
} |
| 67 |
|
| 68 |
} // namespace HostSideTracing |
| 69 |
|
| 70 |
using namespace HostSideTracing; |
| 71 |
|
| 72 |
cl_int CL_API_CALL clCreateTracingHandleINTEL(cl_device_id device, cl_tracing_callback callback, void *userData, cl_tracing_handle *handle) { |
| 73 |
if (device == nullptr || callback == nullptr || handle == nullptr) { |
| 74 |
return CL_INVALID_VALUE; |
| 75 |
} |
| 76 |
|
| 77 |
*handle = new _cl_tracing_handle; |
| 78 |
if (*handle == nullptr) { |
| 79 |
return CL_OUT_OF_HOST_MEMORY; |
| 80 |
} |
| 81 |
|
| 82 |
(*handle)->device = device; |
| 83 |
(*handle)->handle = new TracingHandle(callback, userData); |
| 84 |
if ((*handle)->handle == nullptr) { |
| 85 |
delete *handle; |
| 86 |
return CL_OUT_OF_HOST_MEMORY; |
| 87 |
} |
| 88 |
|
| 89 |
return CL_SUCCESS; |
| 90 |
} |
| 91 |
|
| 92 |
cl_int CL_API_CALL clSetTracingPointINTEL(cl_tracing_handle handle, ClFunctionId fid, cl_bool enable) { |
| 93 |
if (handle == nullptr) { |
| 94 |
return CL_INVALID_VALUE; |
| 95 |
} |
| 96 |
|
| 97 |
DEBUG_BREAK_IF(handle->handle == nullptr); |
| 98 |
if (static_cast<uint32_t>(fid) >= CL_FUNCTION_COUNT) { |
| 99 |
return CL_INVALID_VALUE; |
| 100 |
} |
| 101 |
|
| 102 |
handle->handle->setTracingPoint(fid, enable); |
| 103 |
|
| 104 |
return CL_SUCCESS; |
| 105 |
} |
| 106 |
|
| 107 |
cl_int CL_API_CALL clDestroyTracingHandleINTEL(cl_tracing_handle handle) { |
| 108 |
if (handle == nullptr) { |
| 109 |
return CL_INVALID_VALUE; |
| 110 |
} |
| 111 |
|
| 112 |
DEBUG_BREAK_IF(handle->handle == nullptr); |
| 113 |
delete handle->handle; |
| 114 |
delete handle; |
| 115 |
|
| 116 |
return CL_SUCCESS; |
| 117 |
} |
| 118 |
|
| 119 |
cl_int CL_API_CALL clEnableTracingINTEL(cl_tracing_handle handle) { |
| 120 |
if (handle == nullptr) { |
| 121 |
return CL_INVALID_VALUE; |
| 122 |
} |
| 123 |
|
| 124 |
lockTracingState(); |
| 125 |
|
| 126 |
size_t i = 0; |
| 127 |
DEBUG_BREAK_IF(handle->handle == nullptr); |
| 128 |
while (i < tracingMaxHandleCount && tracingHandle[i] != nullptr) { |
| 129 |
if (tracingHandle[i] == handle->handle) { |
| 130 |
unlockTracingState(); |
| 131 |
return CL_INVALID_VALUE; |
| 132 |
} |
| 133 |
++i; |
| 134 |
} |
| 135 |
|
| 136 |
if (i == tracingMaxHandleCount) { |
| 137 |
unlockTracingState(); |
| 138 |
return CL_OUT_OF_RESOURCES; |
| 139 |
} |
| 140 |
|
| 141 |
DEBUG_BREAK_IF(tracingHandle[i] != nullptr); |
| 142 |
tracingHandle[i] = handle->handle; |
| 143 |
if (i == 0) { |
| 144 |
tracingState.fetch_or(tracingStateEnabledBit, std::memory_order_acq_rel); |
| 145 |
} |
| 146 |
|
| 147 |
unlockTracingState(); |
| 148 |
return CL_SUCCESS; |
| 149 |
} |
| 150 |
|
| 151 |
cl_int CL_API_CALL clDisableTracingINTEL(cl_tracing_handle handle) { |
| 152 |
if (handle == nullptr) { |
| 153 |
return CL_INVALID_VALUE; |
| 154 |
} |
| 155 |
|
| 156 |
lockTracingState(); |
| 157 |
|
| 158 |
size_t size = 0; |
| 159 |
while (size < tracingMaxHandleCount && tracingHandle[size] != nullptr) { |
| 160 |
++size; |
| 161 |
} |
| 162 |
|
| 163 |
size_t i = 0; |
| 164 |
DEBUG_BREAK_IF(handle->handle == nullptr); |
| 165 |
while (i < tracingMaxHandleCount && tracingHandle[i] != nullptr) { |
| 166 |
if (tracingHandle[i] == handle->handle) { |
| 167 |
if (size == 1) { |
| 168 |
DEBUG_BREAK_IF(i != 0); |
| 169 |
tracingState.fetch_and(~tracingStateEnabledBit, std::memory_order_acq_rel); |
| 170 |
tracingHandle[i] = nullptr; |
| 171 |
} else { |
| 172 |
tracingHandle[i] = tracingHandle[size - 1]; |
| 173 |
tracingHandle[size - 1] = nullptr; |
| 174 |
} |
| 175 |
unlockTracingState(); |
| 176 |
return CL_SUCCESS; |
| 177 |
} |
| 178 |
++i; |
| 179 |
} |
| 180 |
|
| 181 |
unlockTracingState(); |
| 182 |
return CL_INVALID_VALUE; |
| 183 |
} |
| 184 |
|
| 185 |
cl_int CL_API_CALL clGetTracingStateINTEL(cl_tracing_handle handle, cl_bool *enable) { |
| 186 |
if (handle == nullptr || enable == nullptr) { |
| 187 |
return CL_INVALID_VALUE; |
| 188 |
} |
| 189 |
|
| 190 |
lockTracingState(); |
| 191 |
|
| 192 |
*enable = CL_FALSE; |
| 193 |
|
| 194 |
size_t i = 0; |
| 195 |
DEBUG_BREAK_IF(handle->handle == nullptr); |
| 196 |
while (i < tracingMaxHandleCount && tracingHandle[i] != nullptr) { |
| 197 |
if (tracingHandle[i] == handle->handle) { |
| 198 |
*enable = CL_TRUE; |
| 199 |
break; |
| 200 |
} |
| 201 |
++i; |
| 202 |
} |
| 203 |
|
| 204 |
unlockTracingState(); |
| 205 |
return CL_SUCCESS; |
| 206 |
} |