]> git.cworth.org Git - fips/blob - metrics.c
996e7795e74cfe1929623bd9bf75762a6665269a
[fips] / metrics.c
1 /* Copyright © 2013, Intel Corporation
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21
22 #define _GNU_SOURCE
23
24 #include <inttypes.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <sys/time.h>
30
31 #include "fips-dispatch-gl.h"
32
33 #include "metrics.h"
34 #include "context.h"
35 #include "metrics-info.h"
36 #include "xmalloc.h"
37
38 int frames;
39 int verbose;
40
41 #define MAX_MONITORS_IN_FLIGHT 1000
42
43 /* Timer query */
44 typedef struct timer_query
45 {
46         unsigned id;
47
48         metrics_op_t op;
49         struct timer_query *next;
50 } timer_query_t;
51
52 /* Performance-monitor query */
53 typedef struct monitor
54 {
55         unsigned id;
56
57         metrics_op_t op;
58         struct monitor *next;
59 } monitor_t;
60
61 typedef struct op_metrics
62 {
63         /* This happens to also be the index into the
64          * metrics->op_metrics array currently
65          */
66         metrics_op_t op;
67         double time_ns;
68
69         double **counters;
70 } op_metrics_t;
71
72 struct metrics
73 {
74         /* Description of all available peformance counters, counter
75          * groups, their names and IDs, etc. */
76         metrics_info_t *info;
77
78         /* The current operation being measured. */
79         metrics_op_t op;
80
81         /* GL_TIME_ELAPSED query for which glEndQuery has not yet
82          * been called. */
83         unsigned timer_begun_id;
84
85         /* GL_TIME_ELAPSED queries for which glEndQuery has been
86          * called, (but results have not yet been queried). */
87         timer_query_t *timer_head;
88         timer_query_t *timer_tail;
89
90         /* Performance monitor for which glEndPerfMonitorAMD has not
91          * yet been called. */
92         unsigned monitor_begun_id;
93
94         /* Performance monitors for which glEndPerfMonitorAMD has
95          * been called, (but results have not yet been queried). */
96         monitor_t *monitor_head;
97         monitor_t *monitor_tail;
98
99         int monitors_in_flight;
100
101         unsigned num_op_metrics;
102         op_metrics_t *op_metrics;
103
104         /* Per-frame time and latency measurement. */
105         GLint64 swap_begin_timestamp;
106         unsigned swap_end_timestamp_id;
107         GLint64 previous_swap_end_timestamp;
108 };
109
110 metrics_t *
111 metrics_create (metrics_info_t *info)
112 {
113         metrics_t *metrics;
114
115         metrics = xmalloc (sizeof (metrics_t));
116
117         metrics->info = info;
118
119         metrics->op = 0;
120
121         metrics->timer_begun_id = 0;
122
123         metrics->timer_head = NULL;
124         metrics->timer_tail = NULL;
125
126         metrics->monitor_begun_id = 0;
127
128         metrics->monitor_head = NULL;
129         metrics->monitor_tail = NULL;
130
131         metrics->monitors_in_flight = 0;
132
133         metrics->num_op_metrics = 0;
134         metrics->op_metrics = NULL;
135
136         glGenQueries (1, &metrics->swap_end_timestamp_id);
137
138         /* Get the first frame timestamp started immediately. */
139         glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
140
141         return metrics;
142 }
143
144 void
145 metrics_fini (metrics_t *metrics)
146 {
147         timer_query_t *timer, *timer_next;
148         monitor_t *monitor, *monitor_next;
149
150         /* Discard and cleanup any outstanding queries. */
151         if (metrics->timer_begun_id) {
152                 glEndQuery (GL_TIME_ELAPSED);
153                 glDeleteQueries (1, &metrics->timer_begun_id);
154                 metrics->timer_begun_id = 0;
155         }
156
157         for (timer = metrics->timer_head;
158              timer;
159              timer = timer_next)
160         {
161                 glDeleteQueries (1, &timer->id);
162                 timer_next = timer->next;
163                 free (timer);
164         }
165         metrics->timer_head = NULL;
166         metrics->timer_tail = NULL;
167
168         if (metrics->info->have_perfmon) {
169
170                 if (metrics->monitor_begun_id) {
171                         glEndPerfMonitorAMD (metrics->monitor_begun_id);
172                         glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
173                         metrics->monitor_begun_id = 0;
174                 }
175
176                 for (monitor = metrics->monitor_head;
177                      monitor;
178                      monitor = monitor_next)
179                 {
180                         glDeletePerfMonitorsAMD (1, &monitor->id);
181                         monitor_next = monitor->next;
182                         free (monitor);
183                 }
184                 metrics->monitor_head = NULL;
185                 metrics->monitor_tail = NULL;
186
187         }
188
189         metrics->monitors_in_flight = 0;
190 }
191
192 void
193 metrics_destroy (metrics_t *metrics)
194 {
195         metrics_fini (metrics);
196
197         free (metrics);
198 }
199
200 static const char *
201 metrics_op_string (metrics_op_t op)
202 {
203         if (op >= METRICS_OP_SHADER)
204                 return "Shader program";
205
206         switch (op)
207         {
208         case METRICS_OP_ACCUM:
209                 return "glAccum*(+)";
210         case METRICS_OP_BUFFER_DATA:
211                 return "glBufferData(+)";
212         case METRICS_OP_BUFFER_SUB_DATA:
213                 return "glCopyBufferSubData*";
214         case METRICS_OP_BITMAP:
215                 return "glBitmap*";
216         case METRICS_OP_BLIT_FRAMEBUFFER:
217                 return "glBlitFramebuffer*";
218         case METRICS_OP_CLEAR:
219                 return "glClear(+)";
220         case METRICS_OP_CLEAR_BUFFER_DATA:
221                 return "glCearBufferData(+)";
222         case METRICS_OP_CLEAR_TEX_IMAGE:
223                 return "glClearTexImage(+)";
224         case METRICS_OP_COPY_PIXELS:
225                 return "glCopyPixels";
226         case METRICS_OP_COPY_TEX_IMAGE:
227                 return "glCopyTexImage(+)";
228         case METRICS_OP_DRAW_PIXELS:
229                 return "glDrawPixels";
230         case METRICS_OP_GET_TEX_IMAGE:
231                 return "glGetTexImage(+)";
232         case METRICS_OP_READ_PIXELS:
233                 return "glReadPixels*";
234         case METRICS_OP_TEX_IMAGE:
235                 return "glTexImage*(+)";
236         default:
237                 fprintf (stderr, "fips: Internal error: "
238                          "Unknown metrics op value: %d\n", op);
239                 exit (1);
240         }
241
242         return "";
243 }
244
245 void
246 metrics_counter_start (metrics_t *metrics)
247 {
248         unsigned i;
249
250         /* Initialize the timer_query object. */
251         glGenQueries (1, &metrics->timer_begun_id);
252
253         /* Most everything else in this function is
254          * performance-monitor related. If we don't have that
255          * extension, just start the timer query and be done. */
256         if (! metrics->info->have_perfmon) {
257                 glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
258                 return;
259         }
260
261         /* Initialize the performance-monitor object */
262         glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
263
264         for (i = 0; i < metrics->info->num_groups; i++)
265         {
266                 metrics_group_info_t *group;
267                 int num_counters;
268
269                 group = &metrics->info->groups[i];
270
271                 num_counters = group->num_counters;
272                 if (group->max_active_counters < group->num_counters)
273                 {
274                         fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
275                                  group->max_active_counters,
276                                  group->num_counters, i);
277                         num_counters = group->max_active_counters;
278
279                 }
280
281                 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
282                                                GL_TRUE, group->id,
283                                                num_counters,
284                                                group->counter_ids);
285         }
286
287         /* Start the queries */
288         glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
289
290         glBeginPerfMonitorAMD (metrics->monitor_begun_id);
291 }
292
293 void
294 metrics_counter_stop (metrics_t *metrics)
295 {
296         timer_query_t *timer;
297         monitor_t *monitor;
298
299         /* Stop the current timer and monitor. */
300         glEndQuery (GL_TIME_ELAPSED);
301
302         if (metrics->info->have_perfmon)
303                 glEndPerfMonitorAMD (metrics->monitor_begun_id);
304
305         /* Add these IDs to our lists of outstanding queries and
306          * monitors so the results can be collected later. */
307         timer = xmalloc (sizeof (timer_query_t));
308
309         timer->op = metrics->op;
310         timer->id = metrics->timer_begun_id;
311         timer->next = NULL;
312
313         if (metrics->timer_tail) {
314                 metrics->timer_tail->next = timer;
315                 metrics->timer_tail = timer;
316         } else {
317                 metrics->timer_tail = timer;
318                 metrics->timer_head = timer;
319         }
320
321         if (metrics->info->have_perfmon) {
322                 /* Create a new performance-monitor query */
323                 monitor = xmalloc (sizeof (monitor_t));
324
325                 monitor->op = metrics->op;
326                 monitor->id = metrics->monitor_begun_id;
327                 monitor->next = NULL;
328
329                 if (metrics->monitor_tail) {
330                         metrics->monitor_tail->next = monitor;
331                         metrics->monitor_tail = monitor;
332                 } else {
333                         metrics->monitor_tail = monitor;
334                         metrics->monitor_head = monitor;
335                 }
336         }
337
338         metrics->monitors_in_flight++;
339
340         /* Avoid being a resource hog and collect outstanding results
341          * once we have sent off a large number of
342          * queries. (Presumably, many of the outstanding queries are
343          * available by now.)
344          */
345         if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
346                 metrics_collect_available (metrics);
347 }
348
349 void
350 metrics_set_current_op (metrics_t *metrics, metrics_op_t op)
351 {
352         metrics->op = op;
353 }
354
355 metrics_op_t
356 metrics_get_current_op (metrics_t *metrics)
357 {
358         return metrics->op;
359 }
360
361 static void
362 op_metrics_init (metrics_info_t *info, op_metrics_t *metrics, metrics_op_t op)
363 {
364         unsigned i, j;
365
366         metrics->op = op;
367         metrics->time_ns = 0.0;
368
369         metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
370
371         for (i = 0; i < info->num_groups; i++) {
372                 metrics->counters[i] = xmalloc (sizeof (double) *
373                                                 info->groups[i].num_counters);
374                 for (j = 0; j < info->groups[i].num_counters; j++)
375                         metrics->counters[i][j] = 0.0;
376         }
377 }
378
379 static op_metrics_t *
380 _get_op_metrics (metrics_t *metrics, metrics_op_t op)
381 {
382         unsigned i;
383
384         if (op >= metrics->num_op_metrics)
385         {
386                 metrics->op_metrics = realloc (metrics->op_metrics,
387                                                (op + 1) * sizeof (op_metrics_t));
388                 for (i = metrics->num_op_metrics; i < op + 1; i++)
389                         op_metrics_init (metrics->info, &metrics->op_metrics[i], i);
390
391                 metrics->num_op_metrics = op + 1;
392         }
393
394         return &metrics->op_metrics[op];
395 }
396
397 static void
398 accumulate_program_metrics (metrics_t *metrics, metrics_op_t op,
399                             GLuint *result, GLuint size)
400 {
401 #define CONSUME(var)                                                    \
402         if (p + sizeof(var) > ((unsigned char *) result) + size)        \
403         {                                                               \
404                 fprintf (stderr, "Unexpected end-of-buffer while "      \
405                          "parsing results\n");                          \
406                 break;                                                  \
407         }                                                               \
408         (var) = *((typeof(var) *) p);                                   \
409         p += sizeof(var);
410
411         metrics_info_t *info = metrics->info;
412         op_metrics_t *op_metrics = _get_op_metrics (metrics, op);
413         unsigned char *p = (unsigned char *) result;
414
415         while (p < ((unsigned char *) result) + size)
416         {
417                 GLuint group_id, group_index;
418                 GLuint counter_id, counter_index;
419                 metrics_group_info_t *group;
420                 double value;
421                 unsigned i;
422
423                 CONSUME (group_id);
424                 CONSUME (counter_id);
425
426                 for (i = 0; i < info->num_groups; i++) {
427                         if (info->groups[i].id == group_id)
428                                 break;
429                 }
430                 group_index = i;
431                 assert (group_index < info->num_groups);
432                 group = &info->groups[group_index];
433
434                 for (i = 0; i < group->num_counters; i++) {
435                         if (group->counter_ids[i] == counter_id)
436                                 break;
437                 }
438                 counter_index = i;
439                 assert (counter_index < group->num_counters);
440
441                 switch (group->counter_types[counter_index])
442                 {
443                         uint uint_value;
444                         uint64_t uint64_value;
445                         float float_value;
446                 case GL_UNSIGNED_INT:
447                         CONSUME (uint_value);
448                         value = uint_value;
449                         break;
450                 case GL_UNSIGNED_INT64_AMD:
451                         CONSUME (uint64_value);
452                         value = uint64_value;
453                         break;
454                 case GL_PERCENTAGE_AMD:
455                 case GL_FLOAT:
456                         CONSUME (float_value);
457                         value = float_value;
458                         break;
459                 default:
460                         fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
461                                  group->counter_types[counter_index]);
462                         value = 0.0;
463                         break;
464                 }
465
466                 op_metrics->counters[group_index][counter_index] += value;
467         }
468 }
469
470 static void
471 accumulate_program_time (metrics_t *metrics, metrics_op_t op, unsigned time_ns)
472 {
473         op_metrics_t *op_metrics;
474
475         op_metrics = _get_op_metrics (metrics, op);
476
477         op_metrics->time_ns += time_ns;
478 }
479
480 typedef struct per_stage_metrics
481 {
482         op_metrics_t *metrics;
483         shader_stage_info_t *stage;
484         double time_ns;
485         double active;
486 } per_stage_metrics_t;
487
488 static int
489 _is_shader_stage_counter (metrics_info_t *info,
490                           unsigned group_index,
491                           unsigned counter_index)
492 {
493         shader_stage_info_t *stage;
494         unsigned i;
495
496         for (i = 0; i < info->num_shader_stages; i++) {
497                 stage = &info->stages[i];
498
499                 if (stage->active_group_index == group_index &&
500                     stage->active_counter_index == counter_index)
501                 {
502                         return 1;
503                 }
504
505                 if (stage->stall_group_index == group_index &&
506                     stage->stall_counter_index == counter_index)
507                 {
508                         return 1;
509                 }
510         }
511
512         return 0;
513 }
514
515 static void
516 print_per_stage_metrics (metrics_t *metrics,
517                          per_stage_metrics_t *per_stage,
518                          double total)
519 {
520         metrics_info_t *info = metrics->info;
521         op_metrics_t *op_metrics = per_stage->metrics;
522         metrics_group_info_t *group;
523         const char *op_string;
524         unsigned group_index, counter;
525         double value;
526
527         /* Don't print anything for stages with no alloted time. */
528         if (per_stage->time_ns == 0.0)
529                 return;
530
531         op_string = metrics_op_string (op_metrics->op);
532
533         printf ("%21s", op_string);
534
535         if (op_metrics->op >= METRICS_OP_SHADER) {
536                 printf (" %3d", op_metrics->op - METRICS_OP_SHADER);
537         } else {
538                 printf ("    ");
539
540         }
541
542         if (per_stage->stage)
543                 printf (" %cS:", per_stage->stage->name[0]);
544         else
545                 printf ("   :");
546
547         printf ("\t%7.2f ms (%4.1f%%)",
548                 per_stage->time_ns / 1e6,
549                 per_stage->time_ns / total * 100);
550
551         if (per_stage->active)
552                 printf (", %4.1f%% active", per_stage->active * 100);
553
554         printf ("\n");
555
556         /* I'm not seeing a lot of value printing the rest of these
557          * performance counters by default yet. Use --verbose to get
558          * them for now. */
559         if (! verbose)
560                 return;
561
562         printf ("[");
563         for (group_index = 0; group_index < info->num_groups; group_index++) {
564                 group = &info->groups[group_index];
565                 for (counter = 0; counter < group->num_counters; counter++) {
566
567                         /* Don't print this counter value if it's a
568                          * per-stage cycle counter, (which we have
569                          * already accounted for). */
570                         if (_is_shader_stage_counter (info, group_index, counter))
571                                 continue;
572
573                         value = op_metrics->counters[group_index][counter];
574                         if (value == 0.0)
575                                 continue;
576                         printf ("%s: %.2f ", group->counter_names[counter],
577                                 value / 1e6);
578                 }
579         }
580         printf ("]\n");
581 }
582
583 static int
584 time_compare(const void *in_a, const void *in_b, void *arg unused)
585 {
586         const per_stage_metrics_t *a = in_a;
587         const per_stage_metrics_t *b = in_b;
588
589
590         if (a->time_ns < b->time_ns)
591                 return -1;
592         if (a->time_ns > b->time_ns)
593                 return 1;
594         return 0;
595 }
596
597 static void
598 print_program_metrics (metrics_t *metrics)
599 {
600         metrics_info_t *info = metrics->info;
601         unsigned num_shader_stages = info->num_shader_stages;
602         per_stage_metrics_t *sorted, *per_stage;
603         double total_time, op_cycles;
604         op_metrics_t *op;
605         unsigned group_index, counter_index;
606         unsigned i, j, num_sorted;
607
608         if (! info->have_perfmon &&
609             ! info->printed_missing_perfmon_warning)
610         {
611                 fprintf (stderr,
612                          "Warning: The GL_AMD_performance_monitor extensions is not available.\n"
613                          "         Some fips features, (such as per-shader-stage timings),\n"
614                          "         will not be available.\n");
615
616                 info->printed_missing_perfmon_warning = true;
617         }
618
619         /* Make a sorted list of the per-stage operations by time
620          * used, and figure out the total so we can print percentages.
621          */
622         if (num_shader_stages)
623                 num_sorted = metrics->num_op_metrics * num_shader_stages;
624         else
625                 num_sorted = metrics->num_op_metrics;
626
627         sorted = xmalloc (sizeof (*sorted) * num_sorted);
628
629         total_time = 0.0;
630
631         for (i = 0; i < metrics->num_op_metrics; i++) {
632
633                 op = &metrics->op_metrics[i];
634
635                 /* Accumulate total time across all ops. */
636                 total_time += op->time_ns;
637
638                 /* Also, find total cycles in all stages of this op. */
639                 op_cycles = 0.0;
640
641                 if (num_shader_stages == 0) {
642                         per_stage = &sorted[i];
643                         per_stage->metrics = op;
644                         per_stage->stage = NULL;
645                         per_stage->time_ns = op->time_ns;
646                         per_stage->active = 0.0;
647                 }
648
649                 for (j = 0; j < num_shader_stages; j++) {
650                         /* Active cycles */
651                         group_index = info->stages[j].active_group_index;
652                         counter_index = info->stages[j].active_counter_index;
653                         op_cycles += op->counters[group_index][counter_index];
654
655                         /* Stall cycles */
656                         group_index = info->stages[j].stall_group_index;
657                         counter_index = info->stages[j].stall_counter_index;
658                         op_cycles += op->counters[group_index][counter_index];
659                 }
660
661                 for (j = 0; j < num_shader_stages; j++) {
662                         double active_cycles, stall_cycles, stage_cycles;
663
664                         /* Active cycles */
665                         group_index = info->stages[j].active_group_index;
666                         counter_index = info->stages[j].active_counter_index;
667                         active_cycles = op->counters[group_index][counter_index];
668
669                         /* Stall cycles */
670                         group_index = info->stages[j].stall_group_index;
671                         counter_index = info->stages[j].stall_counter_index;
672                         stall_cycles = op->counters[group_index][counter_index];
673
674                         stage_cycles = active_cycles + stall_cycles;
675
676                         per_stage = &sorted[i * num_shader_stages + j];
677                         per_stage->metrics = op;
678
679                         if (op_cycles) {
680                                 per_stage->stage = &info->stages[j];
681                                 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
682                         } else {
683                                 /* If we don't have any per-stage cycle counts
684                                  * for this operation, then use the first
685                                  * stage as a placeholder for all the time,
686                                  * but NULL-ify the stage info so that the
687                                  * report doesn't lie about this time being
688                                  * from any particular stage. */
689                                 per_stage->stage = NULL;
690                                 if (j == 0) {
691                                         per_stage->time_ns = op->time_ns;
692                                 } else {
693                                         per_stage->time_ns = 0.0;
694                                 }
695                         }
696
697                         if (stage_cycles) {
698                                 per_stage->active = active_cycles / stage_cycles;
699                         } else {
700                                 per_stage->active = 0.0;
701                         }
702                 }
703         }
704
705         qsort_r (sorted, num_sorted, sizeof (*sorted),
706                  time_compare, metrics->op_metrics);
707
708         for (i = 0; i < num_sorted; i++)
709                 print_per_stage_metrics (metrics, &sorted[i], total_time);
710
711         free (sorted);
712 }
713
714 void
715 metrics_collect_available (metrics_t *metrics)
716 {
717         /* Consume all timer queries that are ready. */
718         timer_query_t *timer = metrics->timer_head;
719
720         while (timer) {
721                 GLuint available, elapsed;
722
723                 glGetQueryObjectuiv (timer->id,
724                                      GL_QUERY_RESULT_AVAILABLE, &available);
725                 if (! available)
726                         break;
727
728                 glGetQueryObjectuiv (timer->id,
729                                      GL_QUERY_RESULT, &elapsed);
730
731                 accumulate_program_time (metrics, timer->op, elapsed);
732
733                 metrics->timer_head = timer->next;
734                 if (metrics->timer_head == NULL)
735                         metrics->timer_tail = NULL;
736
737                 glDeleteQueries (1, &timer->id);
738
739                 free (timer);
740                 timer = metrics->timer_head;
741         }
742
743         if (! metrics->info->have_perfmon)
744                 return;
745
746         /* And similarly for all performance monitors that are ready. */
747         monitor_t *monitor = metrics->monitor_head;
748
749         while (monitor) {
750                 GLuint available, result_size, *result;
751                 GLint bytes_written;
752
753                 glGetPerfMonitorCounterDataAMD (monitor->id,
754                                                 GL_PERFMON_RESULT_AVAILABLE_AMD,
755                                                 sizeof (available), &available,
756                                                 NULL);
757                 if (! available)
758                         break;
759
760                 glGetPerfMonitorCounterDataAMD (monitor->id,
761                                                 GL_PERFMON_RESULT_SIZE_AMD,
762                                                 sizeof (result_size),
763                                                 &result_size, NULL);
764
765                 result = xmalloc (result_size);
766
767                 glGetPerfMonitorCounterDataAMD (monitor->id,
768                                                 GL_PERFMON_RESULT_AMD,
769                                                 result_size, result,
770                                                 &bytes_written);
771
772                 accumulate_program_metrics (metrics, monitor->op, result, result_size);
773
774                 free (result);
775
776                 metrics->monitor_head = monitor->next;
777                 if (metrics->monitor_head == NULL)
778                         metrics->monitor_tail = NULL;
779
780                 glDeletePerfMonitorsAMD (1, &monitor->id);
781
782                 free (monitor);
783
784                 metrics->monitors_in_flight--;
785
786                 monitor = metrics->monitor_head;
787         }
788 }
789
790 void
791 metrics_end_frame_pre_swap (metrics_t *metrics)
792 {
793         GLuint64 swap_end_timestamp, frame_time_ns, latency_ns;
794
795         /* Don't leave any counters running over the end_frame work we
796          * do here. The counters will be started again at the end of
797          * metrics_end_frame_post_swap.  */
798
799         metrics_counter_stop (metrics);
800
801         /* Now that an entire frame's worth of content has gone by, we
802          * can be sure that the timer quiery for the previous frame's
803          * swap is available.
804          *
805          * Note: The only case in which this query isn't immediately
806          * available would be if there was effectively nothing in this
807          * frame. In that case, triggering a block on this query
808          * result is not a concern (since an empty frame is not
809          * interesting, and all of our counters are stopped anyway).
810          *
811          * Accepting this one block is much better than adding a
812          * linked list for these results that would reaslisticly never
813          * have more than one entry anyway.
814          */
815
816         glGetQueryObjectui64v (metrics->swap_end_timestamp_id,
817                                GL_QUERY_RESULT, &swap_end_timestamp);
818
819         if (frames > 0) {
820                 /* Subtract previous frame's timestamp to get frame time. */
821                 frame_time_ns = swap_end_timestamp - metrics->previous_swap_end_timestamp;
822
823                 latency_ns = swap_end_timestamp - metrics->swap_begin_timestamp;
824
825                 /* We've waited one frame to ensure we have a timestamp
826                  * result. So the time we've actually measured here is
827                  * for the previous frame. */
828                 printf ("Frame %d: %.2f ms (latency: %.2f ms)\n",
829                         frames - 1, frame_time_ns / 1e6, latency_ns / 1e6);
830         }
831
832         metrics->previous_swap_end_timestamp = swap_end_timestamp;
833
834         /* Before the actual swap call, we get the current timestamp
835          * value. This is a synchronous get so we can use this as the
836          * baseline for a frame latency measurment. */
837
838         glGetInteger64v (GL_TIMESTAMP, &metrics->swap_begin_timestamp);
839 }
840
841 void
842 metrics_end_frame_post_swap (metrics_t *metrics)
843 {
844         static int initialized = 0;
845         static struct timeval tv_start, tv_now;
846
847         /* Now that the swap command has been queued, we issue an
848          * asynchronous query of the timestamp value. Comparing this
849          * to the synchronous get we just sent in
850          * metrics_end_frame_pre_swap allows us to measure the
851          * per-frame swap latency. */
852
853         glQueryCounter (metrics->swap_end_timestamp_id, GL_TIMESTAMP);
854
855         if (! initialized) {
856                 gettimeofday (&tv_start, NULL);
857                 if (getenv ("FIPS_VERBOSE"))
858                         verbose = 1;
859                 initialized = 1;
860         }
861
862         metrics_collect_available (metrics);
863
864         frames++;
865
866         if (frames % 15 == 0) {
867                 double fps;
868
869                 gettimeofday (&tv_now, NULL);
870
871                 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
872                                          (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
873
874                 printf("FPS: %.3f\n", fps);
875
876                 print_program_metrics (metrics);
877         }
878
879         /* Start the counter up again now that we're done. */
880         metrics_counter_start (metrics);
881 }