summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/performance_query.c
blob: aa103516a58ec8e5c93e4a41613c84b24777b808 (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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
 * Copyright © 2012-2017 Intel Corporation
 *
 * 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.
 */

/**
 * \file performance_query.c
 * Core Mesa support for the INTEL_performance_query extension.
 */

#include <stdbool.h>
#include "glheader.h"
#include "context.h"
#include "enums.h"
#include "hash.h"
#include "macros.h"
#include "mtypes.h"
#include "performance_query.h"
#include "util/ralloc.h"

void
_mesa_init_performance_queries(struct gl_context *ctx)
{
   ctx->PerfQuery.Objects = _mesa_NewHashTable();
}

static void
free_performance_query(GLuint key, void *data, void *user)
{
   struct gl_perf_query_object *m = data;
   struct gl_context *ctx = user;

   ctx->Driver.DeletePerfQuery(ctx, m);
}

void
_mesa_free_performance_queries(struct gl_context *ctx)
{
   _mesa_HashDeleteAll(ctx->PerfQuery.Objects,
                       free_performance_query, ctx);
   _mesa_DeleteHashTable(ctx->PerfQuery.Objects);
}

static inline struct gl_perf_query_object *
lookup_object(struct gl_context *ctx, GLuint id)
{
   return _mesa_HashLookup(ctx->PerfQuery.Objects, id);
}

static GLuint
init_performance_query_info(struct gl_context *ctx)
{
   if (ctx->Driver.InitPerfQueryInfo)
      return ctx->Driver.InitPerfQueryInfo(ctx);
   else
      return 0;
}

/* For INTEL_performance_query, query id 0 is reserved to be invalid. */
static inline unsigned
queryid_to_index(GLuint queryid)
{
   return queryid - 1;
}

static inline GLuint
index_to_queryid(unsigned index)
{
   return index + 1;
}

static inline bool
queryid_valid(const struct gl_context *ctx, unsigned numQueries, GLuint queryid)
{
   GLuint index = queryid_to_index(queryid);
   return index >= 0 && index < numQueries;
}

static inline GLuint
counterid_to_index(GLuint counterid)
{
   return counterid - 1;
}

static void
output_clipped_string(GLchar *stringRet,
                      GLuint stringMaxLen,
                      const char *string)
{
   if (!stringRet)
      return;

   strncpy(stringRet, string ? string : "", stringMaxLen);

   /* No specification given about whether returned strings needs
    * to be zero-terminated. Zero-terminate the string always as we
    * don't otherwise communicate the length of the returned
    * string.
    */
   if (stringMaxLen > 0)
      stringRet[stringMaxLen - 1] = '\0';
}

/*****************************************************************************/

extern void GLAPIENTRY
_mesa_GetFirstPerfQueryIdINTEL(GLuint *queryId)
{
   GET_CURRENT_CONTEXT(ctx);

   unsigned numQueries;

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If queryId pointer is equal to 0, INVALID_VALUE error is generated."
    */
   if (!queryId) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetFirstPerfQueryIdINTEL(queryId == NULL)");
      return;
   }

   numQueries = init_performance_query_info(ctx);

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If the given hardware platform doesn't support any performance
    *    queries, then the value of 0 is returned and INVALID_OPERATION error
    *    is raised."
    */
   if (numQueries == 0) {
      *queryId = 0;
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetFirstPerfQueryIdINTEL(no queries supported)");
      return;
   }

   *queryId = index_to_queryid(0);
}

extern void GLAPIENTRY
_mesa_GetNextPerfQueryIdINTEL(GLuint queryId, GLuint *nextQueryId)
{
   GET_CURRENT_CONTEXT(ctx);

   unsigned numQueries;

   /* The GL_INTEL_performance_query spec says:
    *
    *    "The result is passed in location pointed by nextQueryId. If query
    *    identified by queryId is the last query available the value of 0 is
    *    returned. If the specified performance query identifier is invalid
    *    then INVALID_VALUE error is generated. If nextQueryId pointer is
    *    equal to 0, an INVALID_VALUE error is generated.  Whenever error is
    *    generated, the value of 0 is returned."
    */

   if (!nextQueryId) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetNextPerfQueryIdINTEL(nextQueryId == NULL)");
      return;
   }

   numQueries = init_performance_query_info(ctx);

   if (!queryid_valid(ctx, numQueries, queryId)) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetNextPerfQueryIdINTEL(invalid query)");
      return;
   }

   if (queryid_valid(ctx, numQueries, ++queryId))
      *nextQueryId = queryId;
   else
      *nextQueryId = 0;
}

extern void GLAPIENTRY
_mesa_GetPerfQueryIdByNameINTEL(char *queryName, GLuint *queryId)
{
   GET_CURRENT_CONTEXT(ctx);

   unsigned numQueries;
   unsigned i;

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If queryName does not reference a valid query name, an INVALID_VALUE
    *    error is generated."
    */
   if (!queryName) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetPerfQueryIdByNameINTEL(queryName == NULL)");
      return;
   }

   /* The specification does not state that this produces an error but
    * to be consistent with glGetFirstPerfQueryIdINTEL we generate an
    * INVALID_VALUE error
    */
   if (!queryId) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetPerfQueryIdByNameINTEL(queryId == NULL)");
      return;
   }

   numQueries = init_performance_query_info(ctx);

   for (i = 0; i < numQueries; ++i) {
      const GLchar *name;
      GLuint ignore;

      ctx->Driver.GetPerfQueryInfo(ctx, i, &name, &ignore, &ignore, &ignore);

      if (strcmp(name, queryName) == 0) {
         *queryId = index_to_queryid(i);
         return;
      }
   }

   _mesa_error(ctx, GL_INVALID_VALUE,
               "glGetPerfQueryIdByNameINTEL(invalid query name)");
}

extern void GLAPIENTRY
_mesa_GetPerfQueryInfoINTEL(GLuint queryId,
                            GLuint nameLength, GLchar *name,
                            GLuint *dataSize,
                            GLuint *numCounters,
                            GLuint *numActive,
                            GLuint *capsMask)
{
   GET_CURRENT_CONTEXT(ctx);

   unsigned numQueries = init_performance_query_info(ctx);
   unsigned queryIndex = queryid_to_index(queryId);
   const char *queryName;
   GLuint queryDataSize;
   GLuint queryNumCounters;
   GLuint queryNumActive;

   if (!queryid_valid(ctx, numQueries, queryId)) {
      /* The GL_INTEL_performance_query spec says:
       *
       *    "If queryId does not reference a valid query type, an
       *    INVALID_VALUE error is generated."
       */
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetPerfQueryInfoINTEL(invalid query)");
      return;
   }

   ctx->Driver.GetPerfQueryInfo(ctx, queryIndex,
                                &queryName,
                                &queryDataSize,
                                &queryNumCounters,
                                &queryNumActive);

   output_clipped_string(name, nameLength, queryName);

   if (dataSize)
      *dataSize = queryDataSize;

   if (numCounters)
      *numCounters = queryNumCounters;

   /* The GL_INTEL_performance_query spec says:
    *
    *    "-- the actual number of already created query instances in
    *    maxInstances location"
    *
    * 1) Typo in the specification, should be noActiveInstances.
    * 2) Another typo in the specification, maxInstances parameter is not listed
    *    in the declaration of this function in the list of new functions.
    */
   if (numActive)
      *numActive = queryNumActive;

   /* Assume for now that all queries are per-context */
   if (capsMask)
      *capsMask = GL_PERFQUERY_SINGLE_CONTEXT_INTEL;
}

extern void GLAPIENTRY
_mesa_GetPerfCounterInfoINTEL(GLuint queryId, GLuint counterId,
                              GLuint nameLength, GLchar *name,
                              GLuint descLength, GLchar *desc,
                              GLuint *offset,
                              GLuint *dataSize,
                              GLuint *typeEnum,
                              GLuint *dataTypeEnum,
                              GLuint64 *rawCounterMaxValue)
{
   GET_CURRENT_CONTEXT(ctx);

   unsigned numQueries = init_performance_query_info(ctx);
   unsigned queryIndex = queryid_to_index(queryId);
   const char *queryName;
   GLuint queryDataSize;
   GLuint queryNumCounters;
   GLuint queryNumActive;
   unsigned counterIndex;
   const char *counterName;
   const char *counterDesc;
   GLuint counterOffset;
   GLuint counterDataSize;
   GLuint counterTypeEnum;
   GLuint counterDataTypeEnum;
   GLuint64 counterRawMax;

   if (!queryid_valid(ctx, numQueries, queryId)) {
      /* The GL_INTEL_performance_query spec says:
       *
       *    "If the pair of queryId and counterId does not reference a valid
       *    counter, an INVALID_VALUE error is generated."
       */
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetPerfCounterInfoINTEL(invalid queryId)");
      return;
   }

   ctx->Driver.GetPerfQueryInfo(ctx, queryIndex,
                                &queryName,
                                &queryDataSize,
                                &queryNumCounters,
                                &queryNumActive);

   counterIndex = counterid_to_index(counterId);

   if (counterIndex < 0 || counterIndex >= queryNumCounters) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetPerfCounterInfoINTEL(invalid counterId)");
      return;
   }

   ctx->Driver.GetPerfCounterInfo(ctx, queryIndex, counterIndex,
                                  &counterName,
                                  &counterDesc,
                                  &counterOffset,
                                  &counterDataSize,
                                  &counterTypeEnum,
                                  &counterDataTypeEnum,
                                  &counterRawMax);

   output_clipped_string(name, nameLength, counterName);
   output_clipped_string(desc, descLength, counterDesc);

   if (offset)
      *offset = counterOffset;

   if (dataSize)
      *dataSize = counterDataSize;

   if (typeEnum)
      *typeEnum = counterTypeEnum;

   if (dataTypeEnum)
      *dataTypeEnum = counterDataTypeEnum;

   if (rawCounterMaxValue)
      *rawCounterMaxValue = counterRawMax;

   if (rawCounterMaxValue) {
      /* The GL_INTEL_performance_query spec says:
       *
       *    "for some raw counters for which the maximal value is
       *    deterministic, the maximal value of the counter in 1 second is
       *    returned in the location pointed by rawCounterMaxValue, otherwise,
       *    the location is written with the value of 0."
       *
       *    Since it's very useful to be able to report a maximum value for
       *    more that just counters using the _COUNTER_RAW_INTEL or
       *    _COUNTER_DURATION_RAW_INTEL enums (e.g. for a _THROUGHPUT tools
       *    want to be able to visualize the absolute throughput with respect
       *    to the theoretical maximum that's possible) and there doesn't seem
       *    to be any reason not to allow _THROUGHPUT counters to also be
       *    considerer "raw" here, we always leave it up to the backend to
       *    decide when it's appropriate to report a maximum counter value or 0
       *    if not.
       */
      *rawCounterMaxValue = counterRawMax;
   }
}

extern void GLAPIENTRY
_mesa_CreatePerfQueryINTEL(GLuint queryId, GLuint *queryHandle)
{
   GET_CURRENT_CONTEXT(ctx);

   unsigned numQueries = init_performance_query_info(ctx);
   GLuint id;
   struct gl_perf_query_object *obj;

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If queryId does not reference a valid query type, an INVALID_VALUE
    *    error is generated."
    */
   if (!queryid_valid(ctx, numQueries, queryId)) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glCreatePerfQueryINTEL(invalid queryId)");
      return;
   }

   /* This is not specified in the extension, but is the only sane thing to
    * do.
    */
   if (queryHandle == NULL) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glCreatePerfQueryINTEL(queryHandle == NULL)");
      return;
   }

   id = _mesa_HashFindFreeKeyBlock(ctx->PerfQuery.Objects, 1);
   if (!id) {
      /* The GL_INTEL_performance_query spec says:
       *
       *    "If the query instance cannot be created due to exceeding the
       *    number of allowed instances or driver fails query creation due to
       *    an insufficient memory reason, an OUT_OF_MEMORY error is
       *    generated, and the location pointed by queryHandle returns NULL."
       */
      _mesa_error_no_memory(__func__);
      return;
   }

   obj = ctx->Driver.NewPerfQueryObject(ctx, queryid_to_index(queryId));
   if (obj == NULL) {
      _mesa_error_no_memory(__func__);
      return;
   }

   obj->Id = id;
   obj->Active = false;
   obj->Ready = false;

   _mesa_HashInsert(ctx->PerfQuery.Objects, id, obj);
   *queryHandle = id;
}

extern void GLAPIENTRY
_mesa_DeletePerfQueryINTEL(GLuint queryHandle)
{
   GET_CURRENT_CONTEXT(ctx);

   struct gl_perf_query_object *obj = lookup_object(ctx, queryHandle);

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If a query handle doesn't reference a previously created performance
    *    query instance, an INVALID_VALUE error is generated."
    */
   if (obj == NULL) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glDeletePerfQueryINTEL(invalid queryHandle)");
      return;
   }

   /* To avoid complications in the backend we never ask the backend to
    * delete an active query or a query object while we are still
    * waiting for data.
    */

   if (obj->Active)
      _mesa_EndPerfQueryINTEL(queryHandle);

   if (obj->Used && !obj->Ready) {
      ctx->Driver.WaitPerfQuery(ctx, obj);
      obj->Ready = true;
   }

   _mesa_HashRemove(ctx->PerfQuery.Objects, queryHandle);
   ctx->Driver.DeletePerfQuery(ctx, obj);
}

extern void GLAPIENTRY
_mesa_BeginPerfQueryINTEL(GLuint queryHandle)
{
   GET_CURRENT_CONTEXT(ctx);

   struct gl_perf_query_object *obj = lookup_object(ctx, queryHandle);

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If a query handle doesn't reference a previously created performance
    *    query instance, an INVALID_VALUE error is generated."
    */
   if (obj == NULL) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glBeginPerfQueryINTEL(invalid queryHandle)");
      return;
   }

   /* The GL_INTEL_performance_query spec says:
    *
    *    "Note that some query types, they cannot be collected in the same
    *    time. Therefore calls of BeginPerfQueryINTEL() cannot be nested if
    *    they refer to queries of such different types. In such case
    *    INVALID_OPERATION error is generated."
    *
    * We also generate an INVALID_OPERATION error if the driver can't begin
    * a query for its own reasons, and for nesting the same query.
    */
   if (obj->Active) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glBeginPerfQueryINTEL(already active)");
      return;
   }

   /* To avoid complications in the backend we never ask the backend to
    * reuse a query object and begin a new query while we are still
    * waiting for data on that object.
    */
   if (obj->Used && !obj->Ready) {
      ctx->Driver.WaitPerfQuery(ctx, obj);
      obj->Ready = true;
   }

   if (ctx->Driver.BeginPerfQuery(ctx, obj)) {
      obj->Used = true;
      obj->Active = true;
      obj->Ready = false;
   } else {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glBeginPerfQueryINTEL(driver unable to begin query)");
   }
}

extern void GLAPIENTRY
_mesa_EndPerfQueryINTEL(GLuint queryHandle)
{
   GET_CURRENT_CONTEXT(ctx);

   struct gl_perf_query_object *obj = lookup_object(ctx, queryHandle);

   /* Not explicitly covered in the spec, but for consistency... */
   if (obj == NULL) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glEndPerfQueryINTEL(invalid queryHandle)");
      return;
   }

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If a performance query is not currently started, an
    *    INVALID_OPERATION error will be generated."
    */

   if (!obj->Active) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glEndPerfQueryINTEL(not active)");
      return;
   }

   ctx->Driver.EndPerfQuery(ctx, obj);

   obj->Active = false;
   obj->Ready = false;
}

extern void GLAPIENTRY
_mesa_GetPerfQueryDataINTEL(GLuint queryHandle, GLuint flags,
                            GLsizei dataSize, void *data, GLuint *bytesWritten)
{
   GET_CURRENT_CONTEXT(ctx);

   struct gl_perf_query_object *obj = lookup_object(ctx, queryHandle);

   /* Not explicitly covered in the spec, but for consistency... */
   if (obj == NULL) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glEndPerfQueryINTEL(invalid queryHandle)");
      return;
   }

   /* The GL_INTEL_performance_query spec says:
    *
    *    "If bytesWritten or data pointers are NULL then an INVALID_VALUE
    *    error is generated."
    */
   if (!bytesWritten || !data) {
      _mesa_error(ctx, GL_INVALID_VALUE,
                  "glGetPerfQueryDataINTEL(bytesWritten or data is NULL)");
      return;
   }

   /* Just for good measure in case a lazy application is only
    * checking this and not checking for errors...
    */
   *bytesWritten = 0;

   /* Not explicitly covered in the spec but to be consistent with
    * EndPerfQuery which validates that an application only ends an
    * active query we also validate that an application doesn't try
    * and get the data for a still active query...
    */
   if (obj->Active) {
      _mesa_error(ctx, GL_INVALID_OPERATION,
                  "glGetPerfQueryDataINTEL(query still active)");
      return;
   }

   obj->Ready = ctx->Driver.IsPerfQueryReady(ctx, obj);

   if (!obj->Ready) {
      if (flags == GL_PERFQUERY_FLUSH_INTEL) {
         ctx->Driver.Flush(ctx);
      } else if (flags == GL_PERFQUERY_WAIT_INTEL) {
         ctx->Driver.WaitPerfQuery(ctx, obj);
         obj->Ready = true;
      }
   }

   if (obj->Ready)
      ctx->Driver.GetPerfQueryData(ctx, obj, dataSize, data, bytesWritten);
}