2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file performance_monitor.c
26 * Core Mesa support for the AMD_performance_monitor extension.
28 * In order to implement this extension, start by defining two enums:
29 * one for Groups, and one for Counters. These will be used as indexes into
30 * arrays, so they should start at 0 and increment from there.
32 * Counter IDs need to be globally unique. That is, you can't have counter 7
33 * in group A and counter 7 in group B. A global enum of all available
34 * counters is a convenient way to guarantee this.
44 #include "performance_monitor.h"
49 _mesa_init_performance_monitors(struct gl_context
*ctx
)
51 ctx
->PerfMonitor
.Monitors
= _mesa_NewHashTable();
52 ctx
->PerfMonitor
.NumGroups
= 0;
53 ctx
->PerfMonitor
.Groups
= NULL
;
56 static struct gl_perf_monitor_object
*
57 new_performance_monitor(struct gl_context
*ctx
, GLuint index
)
60 struct gl_perf_monitor_object
*m
= ctx
->Driver
.NewPerfMonitor(ctx
);
66 rzalloc_array(NULL
, unsigned, ctx
->PerfMonitor
.NumGroups
);
69 ralloc_array(NULL
, BITSET_WORD
*, ctx
->PerfMonitor
.NumGroups
);
71 if (m
->ActiveGroups
== NULL
|| m
->ActiveCounters
== NULL
)
74 for (i
= 0; i
< ctx
->PerfMonitor
.NumGroups
; i
++) {
75 const struct gl_perf_monitor_group
*g
= &ctx
->PerfMonitor
.Groups
[i
];
77 m
->ActiveCounters
[i
] = rzalloc_array(m
->ActiveCounters
, BITSET_WORD
,
78 BITSET_WORDS(g
->NumCounters
));
79 if (m
->ActiveCounters
[i
] == NULL
)
86 ralloc_free(m
->ActiveGroups
);
87 ralloc_free(m
->ActiveCounters
);
88 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
92 static inline struct gl_perf_monitor_object
*
93 lookup_monitor(struct gl_context
*ctx
, GLuint id
)
95 return (struct gl_perf_monitor_object
*)
96 _mesa_HashLookup(ctx
->PerfMonitor
.Monitors
, id
);
99 static inline const struct gl_perf_monitor_group
*
100 get_group(const struct gl_context
*ctx
, GLuint id
)
102 if (id
>= ctx
->PerfMonitor
.NumGroups
)
105 return &ctx
->PerfMonitor
.Groups
[id
];
108 static inline const struct gl_perf_monitor_counter
*
109 get_counter(const struct gl_perf_monitor_group
*group_obj
, GLuint id
)
111 if (id
>= group_obj
->NumCounters
)
114 return &group_obj
->Counters
[id
];
117 /*****************************************************************************/
120 _mesa_GetPerfMonitorGroupsAMD(GLint
*numGroups
, GLsizei groupsSize
,
123 GET_CURRENT_CONTEXT(ctx
);
125 if (numGroups
!= NULL
)
126 *numGroups
= ctx
->PerfMonitor
.NumGroups
;
128 if (groupsSize
> 0 && groups
!= NULL
) {
130 unsigned n
= MIN2(groupsSize
, ctx
->PerfMonitor
.NumGroups
);
132 /* We just use the index in the Groups array as the ID. */
133 for (i
= 0; i
< n
; i
++)
139 _mesa_GetPerfMonitorCountersAMD(GLuint group
, GLint
*numCounters
,
140 GLint
*maxActiveCounters
,
141 GLsizei countersSize
, GLuint
*counters
)
143 GET_CURRENT_CONTEXT(ctx
);
144 const struct gl_perf_monitor_group
*group_obj
= get_group(ctx
, group
);
145 if (group_obj
== NULL
) {
146 _mesa_error(ctx
, GL_INVALID_VALUE
,
147 "glGetPerfMonitorCountersAMD(invalid group)");
151 if (maxActiveCounters
!= NULL
)
152 *maxActiveCounters
= group_obj
->MaxActiveCounters
;
154 if (numCounters
!= NULL
)
155 *numCounters
= group_obj
->NumCounters
;
157 if (counters
!= NULL
) {
159 unsigned n
= MIN2(group_obj
->NumCounters
, countersSize
);
160 for (i
= 0; i
< n
; i
++) {
161 /* We just use the index in the Counters array as the ID. */
168 _mesa_GetPerfMonitorGroupStringAMD(GLuint group
, GLsizei bufSize
,
169 GLsizei
*length
, GLchar
*groupString
)
171 GET_CURRENT_CONTEXT(ctx
);
173 const struct gl_perf_monitor_group
*group_obj
= get_group(ctx
, group
);
175 if (group_obj
== NULL
) {
176 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetPerfMonitorGroupStringAMD");
181 /* Return the number of characters that would be required to hold the
182 * group string, excluding the null terminator.
185 *length
= strlen(group_obj
->Name
);
188 *length
= MIN2(strlen(group_obj
->Name
), bufSize
);
189 if (groupString
!= NULL
)
190 strncpy(groupString
, group_obj
->Name
, bufSize
);
195 _mesa_GetPerfMonitorCounterStringAMD(GLuint group
, GLuint counter
,
196 GLsizei bufSize
, GLsizei
*length
,
197 GLchar
*counterString
)
199 GET_CURRENT_CONTEXT(ctx
);
201 const struct gl_perf_monitor_group
*group_obj
;
202 const struct gl_perf_monitor_counter
*counter_obj
;
204 group_obj
= get_group(ctx
, group
);
206 if (group_obj
== NULL
) {
207 _mesa_error(ctx
, GL_INVALID_VALUE
,
208 "glGetPerfMonitorCounterStringAMD(invalid group)");
212 counter_obj
= get_counter(group_obj
, counter
);
214 if (counter_obj
== NULL
) {
215 _mesa_error(ctx
, GL_INVALID_VALUE
,
216 "glGetPerfMonitorCounterStringAMD(invalid counter)");
221 /* Return the number of characters that would be required to hold the
222 * counter string, excluding the null terminator.
225 *length
= strlen(counter_obj
->Name
);
228 *length
= MIN2(strlen(counter_obj
->Name
), bufSize
);
229 if (counterString
!= NULL
)
230 strncpy(counterString
, counter_obj
->Name
, bufSize
);
235 _mesa_GetPerfMonitorCounterInfoAMD(GLuint group
, GLuint counter
, GLenum pname
,
238 GET_CURRENT_CONTEXT(ctx
);
240 const struct gl_perf_monitor_group
*group_obj
;
241 const struct gl_perf_monitor_counter
*counter_obj
;
243 group_obj
= get_group(ctx
, group
);
245 if (group_obj
== NULL
) {
246 _mesa_error(ctx
, GL_INVALID_VALUE
,
247 "glGetPerfMonitorCounterInfoAMD(invalid group)");
251 counter_obj
= get_counter(group_obj
, counter
);
253 if (counter_obj
== NULL
) {
254 _mesa_error(ctx
, GL_INVALID_VALUE
,
255 "glGetPerfMonitorCounterInfoAMD(invalid counter)");
260 case GL_COUNTER_TYPE_AMD
:
261 *((GLenum
*) data
) = counter_obj
->Type
;
264 case GL_COUNTER_RANGE_AMD
:
265 switch (counter_obj
->Type
) {
267 case GL_PERCENTAGE_AMD
: {
268 float *f_data
= data
;
269 f_data
[0] = counter_obj
->Minimum
.f
;
270 f_data
[1] = counter_obj
->Maximum
.f
;
273 case GL_UNSIGNED_INT
: {
274 uint32_t *u32_data
= data
;
275 u32_data
[0] = counter_obj
->Minimum
.u32
;
276 u32_data
[1] = counter_obj
->Maximum
.u32
;
279 case GL_UNSIGNED_INT64_AMD
: {
280 uint64_t *u64_data
= data
;
281 u64_data
[0] = counter_obj
->Minimum
.u64
;
282 u64_data
[1] = counter_obj
->Maximum
.u64
;
286 assert(!"Should not get here: invalid counter type");
291 _mesa_error(ctx
, GL_INVALID_ENUM
,
292 "glGetPerfMonitorCounterInfoAMD(pname)");
298 _mesa_GenPerfMonitorsAMD(GLsizei n
, GLuint
*monitors
)
301 GET_CURRENT_CONTEXT(ctx
);
303 if (MESA_VERBOSE
& VERBOSE_API
)
304 _mesa_debug(ctx
, "glGenPerfMonitorsAMD(%d)\n", n
);
307 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPerfMonitorsAMD(n < 0)");
311 if (monitors
== NULL
)
314 /* We don't actually need them to be contiguous, but this is what
315 * the rest of Mesa does, so we may as well.
317 first
= _mesa_HashFindFreeKeyBlock(ctx
->PerfMonitor
.Monitors
, n
);
320 for (i
= 0; i
< n
; i
++) {
321 struct gl_perf_monitor_object
*m
=
322 new_performance_monitor(ctx
, first
+ i
);
324 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPerfMonitorsAMD");
327 monitors
[i
] = first
+ i
;
328 _mesa_HashInsert(ctx
->PerfMonitor
.Monitors
, first
+ i
, m
);
331 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPerfMonitorsAMD");
337 _mesa_DeletePerfMonitorsAMD(GLsizei n
, GLuint
*monitors
)
340 GET_CURRENT_CONTEXT(ctx
);
342 if (MESA_VERBOSE
& VERBOSE_API
)
343 _mesa_debug(ctx
, "glDeletePerfMonitorsAMD(%d)\n", n
);
346 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeletePerfMonitorsAMD(n < 0)");
350 if (monitors
== NULL
)
353 for (i
= 0; i
< n
; i
++) {
354 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitors
[i
]);
357 /* Give the driver a chance to stop the monitor if it's active. */
359 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
361 _mesa_HashRemove(ctx
->PerfMonitor
.Monitors
, monitors
[i
]);
362 ralloc_free(m
->ActiveGroups
);
363 ralloc_free(m
->ActiveCounters
);
364 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
366 /* "INVALID_VALUE error will be generated if any of the monitor IDs
367 * in the <monitors> parameter to DeletePerfMonitorsAMD do not
368 * reference a valid generated monitor ID."
370 _mesa_error(ctx
, GL_INVALID_VALUE
,
371 "glDeletePerfMonitorsAMD(invalid monitor)");
377 _mesa_SelectPerfMonitorCountersAMD(GLuint monitor
, GLboolean enable
,
378 GLuint group
, GLint numCounters
,
381 GET_CURRENT_CONTEXT(ctx
);
383 struct gl_perf_monitor_object
*m
;
384 const struct gl_perf_monitor_group
*group_obj
;
386 m
= lookup_monitor(ctx
, monitor
);
388 /* "INVALID_VALUE error will be generated if the <monitor> parameter to
389 * SelectPerfMonitorCountersAMD does not reference a monitor created by
390 * GenPerfMonitorsAMD."
393 _mesa_error(ctx
, GL_INVALID_VALUE
,
394 "glSelectPerfMonitorCountersAMD(invalid monitor)");
398 group_obj
= get_group(ctx
, group
);
400 /* "INVALID_VALUE error will be generated if the <group> parameter to
401 * GetPerfMonitorCountersAMD, GetPerfMonitorCounterStringAMD,
402 * GetPerfMonitorCounterStringAMD, GetPerfMonitorCounterInfoAMD, or
403 * SelectPerfMonitorCountersAMD does not reference a valid group ID."
405 if (group_obj
== NULL
) {
406 _mesa_error(ctx
, GL_INVALID_VALUE
,
407 "glSelectPerfMonitorCountersAMD(invalid group)");
411 /* "INVALID_VALUE error will be generated if the <numCounters> parameter to
412 * SelectPerfMonitorCountersAMD is less than 0."
414 if (numCounters
< 0) {
415 _mesa_error(ctx
, GL_INVALID_VALUE
,
416 "glSelectPerfMonitorCountersAMD(numCounters < 0)");
420 /* "When SelectPerfMonitorCountersAMD is called on a monitor, any outstanding
421 * results for that monitor become invalidated and the result queries
422 * PERFMON_RESULT_SIZE_AMD and PERFMON_RESULT_AVAILABLE_AMD are reset to 0."
424 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
426 /* Sanity check the counter ID list. */
427 for (i
= 0; i
< numCounters
; i
++) {
428 if (counterList
[i
] >= group_obj
->NumCounters
) {
429 _mesa_error(ctx
, GL_INVALID_VALUE
,
430 "glSelectPerfMonitorCountersAMD(invalid counter ID)");
436 /* Enable the counters */
437 for (i
= 0; i
< numCounters
; i
++) {
438 ++m
->ActiveGroups
[group
];
439 BITSET_SET(m
->ActiveCounters
[group
], counterList
[i
]);
442 /* Disable the counters */
443 for (i
= 0; i
< numCounters
; i
++) {
444 --m
->ActiveGroups
[group
];
445 BITSET_CLEAR(m
->ActiveCounters
[group
], counterList
[i
]);
451 _mesa_BeginPerfMonitorAMD(GLuint monitor
)
453 GET_CURRENT_CONTEXT(ctx
);
455 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
458 _mesa_error(ctx
, GL_INVALID_VALUE
,
459 "glBeginPerfMonitorAMD(invalid monitor)");
463 /* "INVALID_OPERATION error will be generated if BeginPerfMonitorAMD is
464 * called when a performance monitor is already active."
467 _mesa_error(ctx
, GL_INVALID_OPERATION
,
468 "glBeginPerfMonitor(already active)");
472 /* The driver is free to return false if it can't begin monitoring for
473 * any reason. This translates into an INVALID_OPERATION error.
475 if (ctx
->Driver
.BeginPerfMonitor(ctx
, m
)) {
478 _mesa_error(ctx
, GL_INVALID_OPERATION
,
479 "glBeginPerfMonitor(driver unable to begin monitoring)");
484 _mesa_EndPerfMonitorAMD(GLuint monitor
)
486 GET_CURRENT_CONTEXT(ctx
);
488 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
491 _mesa_error(ctx
, GL_INVALID_VALUE
, "glEndPerfMonitorAMD(invalid monitor)");
495 /* "INVALID_OPERATION error will be generated if EndPerfMonitorAMD is called
496 * when a performance monitor is not currently started."
499 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginPerfMonitor(not active)");
503 ctx
->Driver
.EndPerfMonitor(ctx
, m
);
509 * Return the number of bytes needed to store a monitor's result.
512 perf_monitor_result_size(const struct gl_context
*ctx
,
513 const struct gl_perf_monitor_object
*m
)
515 unsigned group
, counter
;
518 for (group
= 0; group
< ctx
->PerfMonitor
.NumGroups
; group
++) {
519 const struct gl_perf_monitor_group
*g
= &ctx
->PerfMonitor
.Groups
[group
];
520 for (counter
= 0; counter
< g
->NumCounters
; counter
++) {
521 const struct gl_perf_monitor_counter
*c
= &g
->Counters
[counter
];
523 if (!BITSET_TEST(m
->ActiveCounters
[group
], counter
))
526 size
+= sizeof(uint32_t); /* Group ID */
527 size
+= sizeof(uint32_t); /* Counter ID */
528 size
+= _mesa_perf_monitor_counter_size(c
);
535 _mesa_GetPerfMonitorCounterDataAMD(GLuint monitor
, GLenum pname
,
536 GLsizei dataSize
, GLuint
*data
,
539 GET_CURRENT_CONTEXT(ctx
);
541 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
544 _mesa_error(ctx
, GL_INVALID_VALUE
,
545 "glGetPerfMonitorCounterDataAMD(invalid monitor)");
549 /* "It is an INVALID_OPERATION error for <data> to be NULL." */
551 _mesa_error(ctx
, GL_INVALID_OPERATION
,
552 "glGetPerfMonitorCounterDataAMD(data == NULL)");
556 /* We need at least enough room for a single value. */
557 if (dataSize
< sizeof(GLuint
)) {
558 if (bytesWritten
!= NULL
)
563 /* AMD appears to return 0 for all queries unless a result is available. */
564 if (!ctx
->Driver
.IsPerfMonitorResultAvailable(ctx
, m
)) {
566 if (bytesWritten
!= NULL
)
567 *bytesWritten
= sizeof(GLuint
);
572 case GL_PERFMON_RESULT_AVAILABLE_AMD
:
574 if (bytesWritten
!= NULL
)
575 *bytesWritten
= sizeof(GLuint
);
577 case GL_PERFMON_RESULT_SIZE_AMD
:
578 *data
= perf_monitor_result_size(ctx
, m
);
579 if (bytesWritten
!= NULL
)
580 *bytesWritten
= sizeof(GLuint
);
582 case GL_PERFMON_RESULT_AMD
:
583 ctx
->Driver
.GetPerfMonitorResult(ctx
, m
, dataSize
, data
, bytesWritten
);
586 _mesa_error(ctx
, GL_INVALID_ENUM
,
587 "glGetPerfMonitorCounterDataAMD(pname)");
592 * Returns how many bytes a counter's value takes up.
595 _mesa_perf_monitor_counter_size(const struct gl_perf_monitor_counter
*c
)
599 case GL_PERCENTAGE_AMD
:
600 return sizeof(GLfloat
);
601 case GL_UNSIGNED_INT
:
602 return sizeof(GLuint
);
603 case GL_UNSIGNED_INT64_AMD
:
604 return sizeof(uint64_t);
606 assert(!"Should not get here: invalid counter type");