File: tracing_api.cpp

package info (click to toggle)
intel-compute-runtime 20.44.18297-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,780 kB
  • sloc: cpp: 379,729; lisp: 4,931; python: 299; sh: 196; makefile: 8
file content (206 lines) | stat: -rw-r--r-- 6,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (C) 2019-2020 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "opencl/source/tracing/tracing_api.h"

#include "opencl/source/tracing/tracing_handle.h"
#include "opencl/source/tracing/tracing_notify.h"

namespace HostSideTracing {

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

bool addTracingClient() {
    uint32_t state = tracingState.load(std::memory_order_acquire);
    state = TRACING_SET_ENABLED_BIT(state);
    state = TRACING_UNSET_LOCKED_BIT(state);
    AtomicBackoff backoff;
    while (!tracingState.compare_exchange_weak(state, state + 1, std::memory_order_release,
                                               std::memory_order_acquire)) {
        if (!TRACING_GET_ENABLED_BIT(state)) {
            return false;
        } else if (TRACING_GET_LOCKED_BIT(state)) {
            DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(state) != 0);
            state = TRACING_UNSET_LOCKED_BIT(state);
            backoff.pause();
        } else {
            backoff.pause();
        }
    }
    return true;
}

void removeTracingClient() {
    DEBUG_BREAK_IF(!TRACING_GET_ENABLED_BIT(tracingState.load(std::memory_order_acquire)));
    DEBUG_BREAK_IF(TRACING_GET_LOCKED_BIT(tracingState.load(std::memory_order_acquire)));
    DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(tracingState.load(std::memory_order_acquire)) == 0);
    tracingState.fetch_sub(1, std::memory_order_acq_rel);
}

static void LockTracingState() {
    uint32_t state = tracingState.load(std::memory_order_acquire);
    state = TRACING_ZERO_CLIENT_COUNTER(state);
    state = TRACING_UNSET_LOCKED_BIT(state);
    AtomicBackoff backoff;
    while (!tracingState.compare_exchange_weak(state, TRACING_SET_LOCKED_BIT(state),
                                               std::memory_order_release, std::memory_order_acquire)) {
        state = TRACING_ZERO_CLIENT_COUNTER(state);
        state = TRACING_UNSET_LOCKED_BIT(state);
        backoff.pause();
    }
    DEBUG_BREAK_IF(!TRACING_GET_LOCKED_BIT(tracingState.load(std::memory_order_acquire)));
    DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(tracingState.load(std::memory_order_acquire)) > 0);
}

static void UnlockTracingState() {
    DEBUG_BREAK_IF(!TRACING_GET_LOCKED_BIT(tracingState.load(std::memory_order_acquire)));
    DEBUG_BREAK_IF(TRACING_GET_CLIENT_COUNTER(tracingState.load(std::memory_order_acquire)) > 0);
    tracingState.fetch_and(~TRACING_STATE_LOCKED_BIT, std::memory_order_acq_rel);
}

} // namespace HostSideTracing

using namespace HostSideTracing;

cl_int CL_API_CALL clCreateTracingHandleINTEL(cl_device_id device, cl_tracing_callback callback, void *userData, cl_tracing_handle *handle) {
    if (device == nullptr || callback == nullptr || handle == nullptr) {
        return CL_INVALID_VALUE;
    }

    *handle = new _cl_tracing_handle;
    if (*handle == nullptr) {
        return CL_OUT_OF_HOST_MEMORY;
    }

    (*handle)->device = device;
    (*handle)->handle = new TracingHandle(callback, userData);
    if ((*handle)->handle == nullptr) {
        delete *handle;
        return CL_OUT_OF_HOST_MEMORY;
    }

    return CL_SUCCESS;
}

cl_int CL_API_CALL clSetTracingPointINTEL(cl_tracing_handle handle, cl_function_id fid, cl_bool enable) {
    if (handle == nullptr) {
        return CL_INVALID_VALUE;
    }

    DEBUG_BREAK_IF(handle->handle == nullptr);
    if (static_cast<uint32_t>(fid) >= CL_FUNCTION_COUNT) {
        return CL_INVALID_VALUE;
    }

    handle->handle->setTracingPoint(fid, enable);

    return CL_SUCCESS;
}

cl_int CL_API_CALL clDestroyTracingHandleINTEL(cl_tracing_handle handle) {
    if (handle == nullptr) {
        return CL_INVALID_VALUE;
    }

    DEBUG_BREAK_IF(handle->handle == nullptr);
    delete handle->handle;
    delete handle;

    return CL_SUCCESS;
}

cl_int CL_API_CALL clEnableTracingINTEL(cl_tracing_handle handle) {
    if (handle == nullptr) {
        return CL_INVALID_VALUE;
    }

    LockTracingState();

    size_t i = 0;
    DEBUG_BREAK_IF(handle->handle == nullptr);
    while (i < TRACING_MAX_HANDLE_COUNT && tracingHandle[i] != nullptr) {
        if (tracingHandle[i] == handle->handle) {
            UnlockTracingState();
            return CL_INVALID_VALUE;
        }
        ++i;
    }

    if (i == TRACING_MAX_HANDLE_COUNT) {
        UnlockTracingState();
        return CL_OUT_OF_RESOURCES;
    }

    DEBUG_BREAK_IF(tracingHandle[i] != nullptr);
    tracingHandle[i] = handle->handle;
    if (i == 0) {
        tracingState.fetch_or(TRACING_STATE_ENABLED_BIT, std::memory_order_acq_rel);
    }

    UnlockTracingState();
    return CL_SUCCESS;
}

cl_int CL_API_CALL clDisableTracingINTEL(cl_tracing_handle handle) {
    if (handle == nullptr) {
        return CL_INVALID_VALUE;
    }

    LockTracingState();

    size_t size = 0;
    while (size < TRACING_MAX_HANDLE_COUNT && tracingHandle[size] != nullptr) {
        ++size;
    }

    size_t i = 0;
    DEBUG_BREAK_IF(handle->handle == nullptr);
    while (i < TRACING_MAX_HANDLE_COUNT && tracingHandle[i] != nullptr) {
        if (tracingHandle[i] == handle->handle) {
            if (size == 1) {
                DEBUG_BREAK_IF(i != 0);
                tracingState.fetch_and(~TRACING_STATE_ENABLED_BIT, std::memory_order_acq_rel);
                tracingHandle[i] = nullptr;
            } else {
                tracingHandle[i] = tracingHandle[size - 1];
                tracingHandle[size - 1] = nullptr;
            }
            UnlockTracingState();
            return CL_SUCCESS;
        }
        ++i;
    }

    UnlockTracingState();
    return CL_INVALID_VALUE;
}

cl_int CL_API_CALL clGetTracingStateINTEL(cl_tracing_handle handle, cl_bool *enable) {
    if (handle == nullptr || enable == nullptr) {
        return CL_INVALID_VALUE;
    }

    LockTracingState();

    *enable = CL_FALSE;

    size_t i = 0;
    DEBUG_BREAK_IF(handle->handle == nullptr);
    while (i < TRACING_MAX_HANDLE_COUNT && tracingHandle[i] != nullptr) {
        if (tracingHandle[i] == handle->handle) {
            *enable = CL_TRUE;
            break;
        }
        ++i;
    }

    UnlockTracingState();
    return CL_SUCCESS;
}