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
);
70 rzalloc_array(NULL
, unsigned, ctx
->PerfMonitor
.NumGroups
);
73 ralloc_array(NULL
, BITSET_WORD
*, ctx
->PerfMonitor
.NumGroups
);
75 if (m
->ActiveGroups
== NULL
|| m
->ActiveCounters
== NULL
)
78 for (i
= 0; i
< ctx
->PerfMonitor
.NumGroups
; i
++) {
79 const struct gl_perf_monitor_group
*g
= &ctx
->PerfMonitor
.Groups
[i
];
81 m
->ActiveCounters
[i
] = rzalloc_array(m
->ActiveCounters
, BITSET_WORD
,
82 BITSET_WORDS(g
->NumCounters
));
83 if (m
->ActiveCounters
[i
] == NULL
)
90 ralloc_free(m
->ActiveGroups
);
91 ralloc_free(m
->ActiveCounters
);
92 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
97 free_performance_monitor(GLuint key
, void *data
, void *user
)
99 struct gl_perf_monitor_object
*m
= data
;
100 struct gl_context
*ctx
= user
;
102 ralloc_free(m
->ActiveGroups
);
103 ralloc_free(m
->ActiveCounters
);
104 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
108 _mesa_free_performance_monitors(struct gl_context
*ctx
)
110 _mesa_HashDeleteAll(ctx
->PerfMonitor
.Monitors
,
111 free_performance_monitor
, ctx
);
112 _mesa_DeleteHashTable(ctx
->PerfMonitor
.Monitors
);
115 static inline struct gl_perf_monitor_object
*
116 lookup_monitor(struct gl_context
*ctx
, GLuint id
)
118 return (struct gl_perf_monitor_object
*)
119 _mesa_HashLookup(ctx
->PerfMonitor
.Monitors
, id
);
122 static inline const struct gl_perf_monitor_group
*
123 get_group(const struct gl_context
*ctx
, GLuint id
)
125 if (id
>= ctx
->PerfMonitor
.NumGroups
)
128 return &ctx
->PerfMonitor
.Groups
[id
];
131 static inline const struct gl_perf_monitor_counter
*
132 get_counter(const struct gl_perf_monitor_group
*group_obj
, GLuint id
)
134 if (id
>= group_obj
->NumCounters
)
137 return &group_obj
->Counters
[id
];
140 /*****************************************************************************/
143 _mesa_GetPerfMonitorGroupsAMD(GLint
*numGroups
, GLsizei groupsSize
,
146 GET_CURRENT_CONTEXT(ctx
);
148 if (numGroups
!= NULL
)
149 *numGroups
= ctx
->PerfMonitor
.NumGroups
;
151 if (groupsSize
> 0 && groups
!= NULL
) {
153 unsigned n
= MIN2((GLuint
) groupsSize
, ctx
->PerfMonitor
.NumGroups
);
155 /* We just use the index in the Groups array as the ID. */
156 for (i
= 0; i
< n
; i
++)
162 _mesa_GetPerfMonitorCountersAMD(GLuint group
, GLint
*numCounters
,
163 GLint
*maxActiveCounters
,
164 GLsizei countersSize
, GLuint
*counters
)
166 GET_CURRENT_CONTEXT(ctx
);
167 const struct gl_perf_monitor_group
*group_obj
= get_group(ctx
, group
);
168 if (group_obj
== NULL
) {
169 _mesa_error(ctx
, GL_INVALID_VALUE
,
170 "glGetPerfMonitorCountersAMD(invalid group)");
174 if (maxActiveCounters
!= NULL
)
175 *maxActiveCounters
= group_obj
->MaxActiveCounters
;
177 if (numCounters
!= NULL
)
178 *numCounters
= group_obj
->NumCounters
;
180 if (counters
!= NULL
) {
182 unsigned n
= MIN2(group_obj
->NumCounters
, (GLuint
) countersSize
);
183 for (i
= 0; i
< n
; i
++) {
184 /* We just use the index in the Counters array as the ID. */
191 _mesa_GetPerfMonitorGroupStringAMD(GLuint group
, GLsizei bufSize
,
192 GLsizei
*length
, GLchar
*groupString
)
194 GET_CURRENT_CONTEXT(ctx
);
196 const struct gl_perf_monitor_group
*group_obj
= get_group(ctx
, group
);
198 if (group_obj
== NULL
) {
199 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetPerfMonitorGroupStringAMD");
204 /* Return the number of characters that would be required to hold the
205 * group string, excluding the null terminator.
208 *length
= strlen(group_obj
->Name
);
211 *length
= MIN2(strlen(group_obj
->Name
), bufSize
);
212 if (groupString
!= NULL
)
213 strncpy(groupString
, group_obj
->Name
, bufSize
);
218 _mesa_GetPerfMonitorCounterStringAMD(GLuint group
, GLuint counter
,
219 GLsizei bufSize
, GLsizei
*length
,
220 GLchar
*counterString
)
222 GET_CURRENT_CONTEXT(ctx
);
224 const struct gl_perf_monitor_group
*group_obj
;
225 const struct gl_perf_monitor_counter
*counter_obj
;
227 group_obj
= get_group(ctx
, group
);
229 if (group_obj
== NULL
) {
230 _mesa_error(ctx
, GL_INVALID_VALUE
,
231 "glGetPerfMonitorCounterStringAMD(invalid group)");
235 counter_obj
= get_counter(group_obj
, counter
);
237 if (counter_obj
== NULL
) {
238 _mesa_error(ctx
, GL_INVALID_VALUE
,
239 "glGetPerfMonitorCounterStringAMD(invalid counter)");
244 /* Return the number of characters that would be required to hold the
245 * counter string, excluding the null terminator.
248 *length
= strlen(counter_obj
->Name
);
251 *length
= MIN2(strlen(counter_obj
->Name
), bufSize
);
252 if (counterString
!= NULL
)
253 strncpy(counterString
, counter_obj
->Name
, bufSize
);
258 _mesa_GetPerfMonitorCounterInfoAMD(GLuint group
, GLuint counter
, GLenum pname
,
261 GET_CURRENT_CONTEXT(ctx
);
263 const struct gl_perf_monitor_group
*group_obj
;
264 const struct gl_perf_monitor_counter
*counter_obj
;
266 group_obj
= get_group(ctx
, group
);
268 if (group_obj
== NULL
) {
269 _mesa_error(ctx
, GL_INVALID_VALUE
,
270 "glGetPerfMonitorCounterInfoAMD(invalid group)");
274 counter_obj
= get_counter(group_obj
, counter
);
276 if (counter_obj
== NULL
) {
277 _mesa_error(ctx
, GL_INVALID_VALUE
,
278 "glGetPerfMonitorCounterInfoAMD(invalid counter)");
283 case GL_COUNTER_TYPE_AMD
:
284 *((GLenum
*) data
) = counter_obj
->Type
;
287 case GL_COUNTER_RANGE_AMD
:
288 switch (counter_obj
->Type
) {
290 case GL_PERCENTAGE_AMD
: {
291 float *f_data
= data
;
292 f_data
[0] = counter_obj
->Minimum
.f
;
293 f_data
[1] = counter_obj
->Maximum
.f
;
296 case GL_UNSIGNED_INT
: {
297 uint32_t *u32_data
= data
;
298 u32_data
[0] = counter_obj
->Minimum
.u32
;
299 u32_data
[1] = counter_obj
->Maximum
.u32
;
302 case GL_UNSIGNED_INT64_AMD
: {
303 uint64_t *u64_data
= data
;
304 u64_data
[0] = counter_obj
->Minimum
.u64
;
305 u64_data
[1] = counter_obj
->Maximum
.u64
;
309 assert(!"Should not get here: invalid counter type");
314 _mesa_error(ctx
, GL_INVALID_ENUM
,
315 "glGetPerfMonitorCounterInfoAMD(pname)");
321 _mesa_GenPerfMonitorsAMD(GLsizei n
, GLuint
*monitors
)
324 GET_CURRENT_CONTEXT(ctx
);
326 if (MESA_VERBOSE
& VERBOSE_API
)
327 _mesa_debug(ctx
, "glGenPerfMonitorsAMD(%d)\n", n
);
330 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGenPerfMonitorsAMD(n < 0)");
334 if (monitors
== NULL
)
337 /* We don't actually need them to be contiguous, but this is what
338 * the rest of Mesa does, so we may as well.
340 first
= _mesa_HashFindFreeKeyBlock(ctx
->PerfMonitor
.Monitors
, n
);
343 for (i
= 0; i
< n
; i
++) {
344 struct gl_perf_monitor_object
*m
=
345 new_performance_monitor(ctx
, first
+ i
);
347 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPerfMonitorsAMD");
350 monitors
[i
] = first
+ i
;
351 _mesa_HashInsert(ctx
->PerfMonitor
.Monitors
, first
+ i
, m
);
354 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glGenPerfMonitorsAMD");
360 _mesa_DeletePerfMonitorsAMD(GLsizei n
, GLuint
*monitors
)
363 GET_CURRENT_CONTEXT(ctx
);
365 if (MESA_VERBOSE
& VERBOSE_API
)
366 _mesa_debug(ctx
, "glDeletePerfMonitorsAMD(%d)\n", n
);
369 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeletePerfMonitorsAMD(n < 0)");
373 if (monitors
== NULL
)
376 for (i
= 0; i
< n
; i
++) {
377 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitors
[i
]);
380 /* Give the driver a chance to stop the monitor if it's active. */
382 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
386 _mesa_HashRemove(ctx
->PerfMonitor
.Monitors
, monitors
[i
]);
387 ralloc_free(m
->ActiveGroups
);
388 ralloc_free(m
->ActiveCounters
);
389 ctx
->Driver
.DeletePerfMonitor(ctx
, m
);
391 /* "INVALID_VALUE error will be generated if any of the monitor IDs
392 * in the <monitors> parameter to DeletePerfMonitorsAMD do not
393 * reference a valid generated monitor ID."
395 _mesa_error(ctx
, GL_INVALID_VALUE
,
396 "glDeletePerfMonitorsAMD(invalid monitor)");
402 _mesa_SelectPerfMonitorCountersAMD(GLuint monitor
, GLboolean enable
,
403 GLuint group
, GLint numCounters
,
406 GET_CURRENT_CONTEXT(ctx
);
408 struct gl_perf_monitor_object
*m
;
409 const struct gl_perf_monitor_group
*group_obj
;
411 m
= lookup_monitor(ctx
, monitor
);
413 /* "INVALID_VALUE error will be generated if the <monitor> parameter to
414 * SelectPerfMonitorCountersAMD does not reference a monitor created by
415 * GenPerfMonitorsAMD."
418 _mesa_error(ctx
, GL_INVALID_VALUE
,
419 "glSelectPerfMonitorCountersAMD(invalid monitor)");
423 group_obj
= get_group(ctx
, group
);
425 /* "INVALID_VALUE error will be generated if the <group> parameter to
426 * GetPerfMonitorCountersAMD, GetPerfMonitorCounterStringAMD,
427 * GetPerfMonitorCounterStringAMD, GetPerfMonitorCounterInfoAMD, or
428 * SelectPerfMonitorCountersAMD does not reference a valid group ID."
430 if (group_obj
== NULL
) {
431 _mesa_error(ctx
, GL_INVALID_VALUE
,
432 "glSelectPerfMonitorCountersAMD(invalid group)");
436 /* "INVALID_VALUE error will be generated if the <numCounters> parameter to
437 * SelectPerfMonitorCountersAMD is less than 0."
439 if (numCounters
< 0) {
440 _mesa_error(ctx
, GL_INVALID_VALUE
,
441 "glSelectPerfMonitorCountersAMD(numCounters < 0)");
445 /* "When SelectPerfMonitorCountersAMD is called on a monitor, any outstanding
446 * results for that monitor become invalidated and the result queries
447 * PERFMON_RESULT_SIZE_AMD and PERFMON_RESULT_AVAILABLE_AMD are reset to 0."
449 ctx
->Driver
.ResetPerfMonitor(ctx
, m
);
451 /* Sanity check the counter ID list. */
452 for (i
= 0; i
< numCounters
; i
++) {
453 if (counterList
[i
] >= group_obj
->NumCounters
) {
454 _mesa_error(ctx
, GL_INVALID_VALUE
,
455 "glSelectPerfMonitorCountersAMD(invalid counter ID)");
461 /* Enable the counters */
462 for (i
= 0; i
< numCounters
; i
++) {
463 ++m
->ActiveGroups
[group
];
464 BITSET_SET(m
->ActiveCounters
[group
], counterList
[i
]);
467 /* Disable the counters */
468 for (i
= 0; i
< numCounters
; i
++) {
469 --m
->ActiveGroups
[group
];
470 BITSET_CLEAR(m
->ActiveCounters
[group
], counterList
[i
]);
476 _mesa_BeginPerfMonitorAMD(GLuint monitor
)
478 GET_CURRENT_CONTEXT(ctx
);
480 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
483 _mesa_error(ctx
, GL_INVALID_VALUE
,
484 "glBeginPerfMonitorAMD(invalid monitor)");
488 /* "INVALID_OPERATION error will be generated if BeginPerfMonitorAMD is
489 * called when a performance monitor is already active."
492 _mesa_error(ctx
, GL_INVALID_OPERATION
,
493 "glBeginPerfMonitor(already active)");
497 /* The driver is free to return false if it can't begin monitoring for
498 * any reason. This translates into an INVALID_OPERATION error.
500 if (ctx
->Driver
.BeginPerfMonitor(ctx
, m
)) {
504 _mesa_error(ctx
, GL_INVALID_OPERATION
,
505 "glBeginPerfMonitor(driver unable to begin monitoring)");
510 _mesa_EndPerfMonitorAMD(GLuint monitor
)
512 GET_CURRENT_CONTEXT(ctx
);
514 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
517 _mesa_error(ctx
, GL_INVALID_VALUE
, "glEndPerfMonitorAMD(invalid monitor)");
521 /* "INVALID_OPERATION error will be generated if EndPerfMonitorAMD is called
522 * when a performance monitor is not currently started."
525 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glBeginPerfMonitor(not active)");
529 ctx
->Driver
.EndPerfMonitor(ctx
, m
);
536 * Return the number of bytes needed to store a monitor's result.
539 perf_monitor_result_size(const struct gl_context
*ctx
,
540 const struct gl_perf_monitor_object
*m
)
542 unsigned group
, counter
;
545 for (group
= 0; group
< ctx
->PerfMonitor
.NumGroups
; group
++) {
546 const struct gl_perf_monitor_group
*g
= &ctx
->PerfMonitor
.Groups
[group
];
547 for (counter
= 0; counter
< g
->NumCounters
; counter
++) {
548 const struct gl_perf_monitor_counter
*c
= &g
->Counters
[counter
];
550 if (!BITSET_TEST(m
->ActiveCounters
[group
], counter
))
553 size
+= sizeof(uint32_t); /* Group ID */
554 size
+= sizeof(uint32_t); /* Counter ID */
555 size
+= _mesa_perf_monitor_counter_size(c
);
562 _mesa_GetPerfMonitorCounterDataAMD(GLuint monitor
, GLenum pname
,
563 GLsizei dataSize
, GLuint
*data
,
566 GET_CURRENT_CONTEXT(ctx
);
568 struct gl_perf_monitor_object
*m
= lookup_monitor(ctx
, monitor
);
569 bool result_available
;
572 _mesa_error(ctx
, GL_INVALID_VALUE
,
573 "glGetPerfMonitorCounterDataAMD(invalid monitor)");
577 /* "It is an INVALID_OPERATION error for <data> to be NULL." */
579 _mesa_error(ctx
, GL_INVALID_OPERATION
,
580 "glGetPerfMonitorCounterDataAMD(data == NULL)");
584 /* We need at least enough room for a single value. */
585 if (dataSize
< sizeof(GLuint
)) {
586 if (bytesWritten
!= NULL
)
591 /* If the monitor has never ended, there is no result. */
592 result_available
= m
->Ended
&&
593 ctx
->Driver
.IsPerfMonitorResultAvailable(ctx
, m
);
595 /* AMD appears to return 0 for all queries unless a result is available. */
596 if (!result_available
) {
598 if (bytesWritten
!= NULL
)
599 *bytesWritten
= sizeof(GLuint
);
604 case GL_PERFMON_RESULT_AVAILABLE_AMD
:
606 if (bytesWritten
!= NULL
)
607 *bytesWritten
= sizeof(GLuint
);
609 case GL_PERFMON_RESULT_SIZE_AMD
:
610 *data
= perf_monitor_result_size(ctx
, m
);
611 if (bytesWritten
!= NULL
)
612 *bytesWritten
= sizeof(GLuint
);
614 case GL_PERFMON_RESULT_AMD
:
615 ctx
->Driver
.GetPerfMonitorResult(ctx
, m
, dataSize
, data
, bytesWritten
);
618 _mesa_error(ctx
, GL_INVALID_ENUM
,
619 "glGetPerfMonitorCounterDataAMD(pname)");
624 * Returns how many bytes a counter's value takes up.
627 _mesa_perf_monitor_counter_size(const struct gl_perf_monitor_counter
*c
)
631 case GL_PERCENTAGE_AMD
:
632 return sizeof(GLfloat
);
633 case GL_UNSIGNED_INT
:
634 return sizeof(GLuint
);
635 case GL_UNSIGNED_INT64_AMD
:
636 return sizeof(uint64_t);
638 assert(!"Should not get here: invalid counter type");