summaryrefslogtreecommitdiff
path: root/hsakmt/perfctr.c
blob: 64ab168fdadd5c5c15011233f52f595c8f33a2b4 (plain)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright © 2014 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including
 * the next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>
#include "libhsakmt.h"
#include "pmc_table.h"
#include "linux/kfd_ioctl.h"
#include <unistd.h>

#define BITS_PER_BYTE                   CHAR_BIT

#define HSA_PERF_MAGIC4CC   0x54415348

enum perf_trace_state {
    PERF_TRACE_STATE__STOPPED = 0,
    PERF_TRACE_STATE__STARTED
};

struct perf_trace {
    uint32_t magic4cc;
    uint32_t  gpu_id;
    enum perf_trace_state state;
};

extern int amd_hsa_thunk_lock_fd;

static HsaCounterProperties *counter_props[MAX_NODES] = {NULL};

static int blockid2uuid(enum perf_block_id block_id, HSA_UUID *uuid)
{
    int rc = 0;
    switch (block_id) {
    case PERFCOUNTER_BLOCKID__SQ:
        *uuid = HSA_PROFILEBLOCK_AMD_SQ;
        break;
    default:
        /* If we reach this point, it's a bug */
        rc = -1;
    }

    return rc;
}

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcGetCounterProperties(
    HSAuint32                   NodeId,             //IN
    HsaCounterProperties**      CounterProperties   //OUT
    )
{
    HSAKMT_STATUS rc = HSAKMT_STATUS_SUCCESS;
    uint32_t gpu_id, i, block_id;
    uint16_t dev_id;
    uint32_t counter_props_size = 0;
    uint32_t total_counters = 0;
    uint32_t total_concurrent = 0;
    struct perf_counter_block block = {0};

    if (CounterProperties == NULL)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    if (validate_nodeid(NodeId, &gpu_id) != 0)
        return HSAKMT_STATUS_INVALID_NODE_UNIT;



    if (counter_props[NodeId] == NULL) {
        dev_id = get_device_id_by_node(NodeId);
        for (i = 0; i < PERFCOUNTER_BLOCKID__MAX; i++) {
            rc = get_block_properties(dev_id, i, &block);
            if (rc != HSAKMT_STATUS_SUCCESS)
                return rc;
            total_concurrent += block.num_of_slots;
            total_counters += block.num_of_counters;
        }

        counter_props_size = sizeof(HsaCounterProperties) +
                sizeof(HsaCounterBlockProperties)*(PERFCOUNTER_BLOCKID__MAX-1) +
                sizeof(HsaCounter)*(total_counters-1);

        counter_props[NodeId] = malloc(counter_props_size);

        if (counter_props[NodeId] == NULL)
            return HSAKMT_STATUS_NO_MEMORY;

        counter_props[NodeId]->NumBlocks = PERFCOUNTER_BLOCKID__MAX;
        counter_props[NodeId]->NumConcurrent = total_concurrent;

        for (block_id = 0; block_id < PERFCOUNTER_BLOCKID__MAX; block_id++)
        {
            rc = get_block_properties(dev_id, block_id, &block);
            if (rc != HSAKMT_STATUS_SUCCESS) {
                free(counter_props[NodeId]);
                return rc;
            }

            /* Filling the SQ block */
            blockid2uuid(block_id, &counter_props[NodeId]->Blocks[block_id].BlockId);
            counter_props[NodeId]->Blocks[block_id].NumCounters = block.num_of_counters;
            counter_props[NodeId]->Blocks[block_id].NumConcurrent = block.num_of_slots;

            for (i = 0; i < block.num_of_counters; i++) {
                counter_props[NodeId]->Blocks[block_id].Counters[i].BlockIndex = block_id;
                counter_props[NodeId]->Blocks[block_id].Counters[i].CounterId = block.counter_ids[i];
                counter_props[NodeId]->Blocks[block_id].Counters[i].CounterSizeInBits = block.counter_size_in_bits;
                counter_props[NodeId]->Blocks[block_id].Counters[i].CounterMask = block.counter_mask;
                counter_props[NodeId]->Blocks[block_id].Counters[i].Flags.ui32.Global = 1;
                counter_props[NodeId]->Blocks[block_id].Counters[i].Type = HSA_PROFILE_TYPE_NONPRIV_IMMEDIATE;
            }
        }
    }

    *CounterProperties = counter_props[NodeId];

    return HSAKMT_STATUS_SUCCESS;
}

/**
  Registers a set of (HW) counters to be used for tracing/profiling
*/

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcRegisterTrace(
    HSAuint32           NodeId,             //IN
    HSAuint32           NumberOfCounters,   //IN
    HsaCounter*         Counters,           //IN
    HsaPmcTraceRoot*    TraceRoot           //OUT
    )
{
    uint32_t gpu_id, i;
    uint64_t min_buf_size = 0;
    uint32_t concurrent_counters[PERFCOUNTER_BLOCKID__MAX] = {0};
    struct perf_trace *trace = NULL;

    if (Counters == NULL || TraceRoot == NULL || NumberOfCounters == 0)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    if (validate_nodeid(NodeId, &gpu_id) != 0)
        return HSAKMT_STATUS_INVALID_NODE_UNIT;

    /* Calculating the minimum buffer size */
    for (i = 0; i < NumberOfCounters; i++) {
        if (Counters[i].BlockIndex >= PERFCOUNTER_BLOCKID__MAX)
            return HSAKMT_STATUS_INVALID_PARAMETER;
        min_buf_size += Counters[i].CounterSizeInBits/BITS_PER_BYTE;
        concurrent_counters[Counters[i].BlockIndex]++;
    }

    /* Verifying that the number of counters per block is not larger than the amount of slots */
    if (concurrent_counters[PERFCOUNTER_BLOCKID__SQ] > counter_props[NodeId]->Blocks[PERFCOUNTER_BLOCKID__SQ].NumConcurrent)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    trace = malloc(sizeof(trace));
    if (trace == NULL)
        return HSAKMT_STATUS_NO_MEMORY;

    trace->magic4cc = HSA_PERF_MAGIC4CC;
    trace->gpu_id = gpu_id;
    trace->state = PERF_TRACE_STATE__STOPPED;

    TraceRoot->NumberOfPasses = 1;
    TraceRoot->TraceBufferMinSizeBytes = PAGE_ALIGN_UP(min_buf_size);
    TraceRoot->TraceId = PORT_VPTR_TO_UINT64(trace);

    return HSAKMT_STATUS_SUCCESS;
}

/**
  Unregisters a set of (HW) counters used for tracing/profiling
*/

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcUnregisterTrace(
    HSAuint32   NodeId,     //IN
    HSATraceId  TraceId     //IN
    )
{
    uint32_t gpu_id;
    struct perf_trace *trace;

    if (TraceId == 0)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    if (validate_nodeid(NodeId, &gpu_id) != 0)
        return HSAKMT_STATUS_INVALID_NODE_UNIT;

    trace = (struct perf_trace *)PORT_UINT64_TO_VPTR(TraceId);

    if (trace->magic4cc != HSA_PERF_MAGIC4CC)
        return HSAKMT_STATUS_INVALID_HANDLE;

    if (trace->gpu_id != gpu_id)
        return HSAKMT_STATUS_INVALID_NODE_UNIT;

    /* If the trace is in the running state, stop it */
    if (trace->state == PERF_TRACE_STATE__STARTED) {
        HSAKMT_STATUS status = hsaKmtPmcStopTrace(TraceId);
        if (status != HSAKMT_STATUS_SUCCESS)
            return status;
    }

    free(trace);

    return HSAKMT_STATUS_SUCCESS;
}


/**
  Allows a user mode process to get exclusive access to the defined set of (HW) counters
  used for tracing/profiling
*/

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcAcquireTraceAccess(
    HSAuint32   NodeId,     //IN
    HSATraceId  TraceId     //IN
    )
{
    struct perf_trace *trace;

    if (TraceId == 0)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    trace = (struct perf_trace *)PORT_UINT64_TO_VPTR(TraceId);

    if (trace->magic4cc != HSA_PERF_MAGIC4CC)
        return HSAKMT_STATUS_INVALID_HANDLE;

    if (amd_hsa_thunk_lock_fd > 0) {
	if (lockf( amd_hsa_thunk_lock_fd, F_TLOCK, 0 ) != 0)
	    return HSAKMT_STATUS_ERROR;
	else
	   return HSAKMT_STATUS_SUCCESS;
    }
    else {
	    return HSAKMT_STATUS_ERROR;
    }
}


/**
  Allows a user mode process to release exclusive access to the defined set of (HW) counters
  used for tracing/profiling
*/

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcReleaseTraceAccess(
    HSAuint32   NodeId,     //IN
    HSATraceId  TraceId     //IN
    )
{
    struct perf_trace *trace;

    if (TraceId == 0)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    trace = (struct perf_trace *)PORT_UINT64_TO_VPTR(TraceId);

    if (trace->magic4cc != HSA_PERF_MAGIC4CC)
        return HSAKMT_STATUS_INVALID_HANDLE;

    if (amd_hsa_thunk_lock_fd > 0) {
	if (lockf( amd_hsa_thunk_lock_fd, F_ULOCK, 0 ) != 0)
	    return HSAKMT_STATUS_ERROR;
	else
	   return HSAKMT_STATUS_SUCCESS;
    }
    else {
	    return HSAKMT_STATUS_ERROR;
    }

}


/**
  Starts tracing operation on a previously established set of performance counters
*/

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcStartTrace(
    HSATraceId  TraceId,                //IN
    void*       TraceBuffer,            //IN (page aligned)
    HSAuint64   TraceBufferSizeBytes    //IN (page aligned)
    )
{
    struct perf_trace *trace = (struct perf_trace *)PORT_UINT64_TO_VPTR(TraceId);

    if (TraceId == 0 || TraceBuffer == NULL || TraceBufferSizeBytes == 0)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    if (trace->magic4cc != HSA_PERF_MAGIC4CC)
        return HSAKMT_STATUS_INVALID_HANDLE;

    trace->state = PERF_TRACE_STATE__STARTED;

    return HSAKMT_STATUS_SUCCESS;
}


/**
   Forces an update of all the counters that a previously started trace operation has registered
*/

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcQueryTrace(
    HSATraceId    TraceId   //IN
    )
{
    struct perf_trace *trace = (struct perf_trace *)PORT_UINT64_TO_VPTR(TraceId);

    if (TraceId == 0)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    if (trace->magic4cc != HSA_PERF_MAGIC4CC)
        return HSAKMT_STATUS_INVALID_HANDLE;

    return HSAKMT_STATUS_SUCCESS;
}


/**
  Stops tracing operation on a previously established set of performance counters
*/

HSAKMT_STATUS
HSAKMTAPI
hsaKmtPmcStopTrace(
    HSATraceId  TraceId     //IN
    )
{
    struct perf_trace *trace = (struct perf_trace *)PORT_UINT64_TO_VPTR(TraceId);

    if (TraceId == 0)
        return HSAKMT_STATUS_INVALID_PARAMETER;

    if (trace->magic4cc != HSA_PERF_MAGIC4CC)
        return HSAKMT_STATUS_INVALID_HANDLE;

    trace->state = PERF_TRACE_STATE__STOPPED;

    return HSAKMT_STATUS_SUCCESS;
}