Skip to content

Commit e3ccf19

Browse files
Eliminate the usage of non-trivially destructible global objects in API tracing
Change-Id: I13606191053c9d927cfe5fa7c58fc4d63bcd5e6d
1 parent b3249c2 commit e3ccf19

File tree

3 files changed

+1042
-715
lines changed

3 files changed

+1042
-715
lines changed

opencl/source/tracing/tracing_api.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace HostSideTracing {
1414

1515
// [XYZZ..Z] - { X - enabled/disabled bit, Y - locked/unlocked bit, ZZ..Z - client count bits }
1616
std::atomic<uint32_t> tracingState(0);
17-
std::vector<TracingHandle *> tracingHandle;
17+
TracingHandle *tracingHandle[TRACING_MAX_HANDLE_COUNT] = {nullptr};
1818
std::atomic<uint32_t> tracingCorrelationId(0);
1919

2020
bool addTracingClient() {
@@ -123,21 +123,24 @@ cl_int CL_API_CALL clEnableTracingINTEL(cl_tracing_handle handle) {
123123

124124
LockTracingState();
125125

126+
size_t i = 0;
126127
DEBUG_BREAK_IF(handle->handle == nullptr);
127-
for (size_t i = 0; i < tracingHandle.size(); ++i) {
128+
while (i < TRACING_MAX_HANDLE_COUNT && tracingHandle[i] != nullptr) {
128129
if (tracingHandle[i] == handle->handle) {
129130
UnlockTracingState();
130131
return CL_INVALID_VALUE;
131132
}
133+
++i;
132134
}
133135

134-
if (tracingHandle.size() == TRACING_MAX_HANDLE_COUNT) {
136+
if (i == TRACING_MAX_HANDLE_COUNT) {
135137
UnlockTracingState();
136138
return CL_OUT_OF_RESOURCES;
137139
}
138140

139-
tracingHandle.push_back(handle->handle);
140-
if (tracingHandle.size() == 1) {
141+
DEBUG_BREAK_IF(tracingHandle[i] != nullptr);
142+
tracingHandle[i] = handle->handle;
143+
if (i == 0) {
141144
tracingState.fetch_or(TRACING_STATE_ENABLED_BIT, std::memory_order_acq_rel);
142145
}
143146

@@ -152,19 +155,27 @@ cl_int CL_API_CALL clDisableTracingINTEL(cl_tracing_handle handle) {
152155

153156
LockTracingState();
154157

158+
size_t size = 0;
159+
while (size < TRACING_MAX_HANDLE_COUNT && tracingHandle[size] != nullptr) {
160+
++size;
161+
}
162+
163+
size_t i = 0;
155164
DEBUG_BREAK_IF(handle->handle == nullptr);
156-
for (size_t i = 0; i < tracingHandle.size(); ++i) {
165+
while (i < TRACING_MAX_HANDLE_COUNT && tracingHandle[i] != nullptr) {
157166
if (tracingHandle[i] == handle->handle) {
158-
if (tracingHandle.size() == 1) {
167+
if (size == 1) {
168+
DEBUG_BREAK_IF(i != 0);
159169
tracingState.fetch_and(~TRACING_STATE_ENABLED_BIT, std::memory_order_acq_rel);
160-
std::vector<TracingHandle *>().swap(tracingHandle);
170+
tracingHandle[i] = nullptr;
161171
} else {
162-
tracingHandle[i] = tracingHandle[tracingHandle.size() - 1];
163-
tracingHandle.pop_back();
172+
tracingHandle[i] = tracingHandle[size - 1];
173+
tracingHandle[size - 1] = nullptr;
164174
}
165175
UnlockTracingState();
166176
return CL_SUCCESS;
167177
}
178+
++i;
168179
}
169180

170181
UnlockTracingState();
@@ -180,12 +191,14 @@ cl_int CL_API_CALL clGetTracingStateINTEL(cl_tracing_handle handle, cl_bool *ena
180191

181192
*enable = CL_FALSE;
182193

194+
size_t i = 0;
183195
DEBUG_BREAK_IF(handle->handle == nullptr);
184-
for (size_t i = 0; i < tracingHandle.size(); ++i) {
196+
while (i < TRACING_MAX_HANDLE_COUNT && tracingHandle[i] != nullptr) {
185197
if (tracingHandle[i] == handle->handle) {
186198
*enable = CL_TRUE;
187199
break;
188200
}
201+
++i;
189202
}
190203

191204
UnlockTracingState();

0 commit comments

Comments
 (0)