]> git.cworth.org Git - fips/blob - metrics.c
Push oustanding-counter data down from context.c into metrics.c
[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 <stdio.h>
25 #include <stdlib.h>
26 #include <assert.h>
27 #include <sys/time.h>
28
29 #include "fips-dispatch-gl.h"
30
31 #include "metrics.h"
32 #include "context.h"
33 #include "metrics-info.h"
34 #include "xmalloc.h"
35
36 int frames;
37 int verbose;
38
39 #define MAX_MONITORS_IN_FLIGHT 1000
40
41 static const char *
42 metrics_op_string (metrics_op_t op)
43 {
44         if (op >= METRICS_OP_SHADER)
45                 return "Shader program";
46
47         switch (op)
48         {
49         case METRICS_OP_ACCUM:
50                 return "glAccum*(+)";
51         case METRICS_OP_BUFFER_DATA:
52                 return "glBufferData(+)";
53         case METRICS_OP_BUFFER_SUB_DATA:
54                 return "glCopyBufferSubData*";
55         case METRICS_OP_BITMAP:
56                 return "glBitmap*";
57         case METRICS_OP_BLIT_FRAMEBUFFER:
58                 return "glBlitFramebuffer*";
59         case METRICS_OP_CLEAR:
60                 return "glClear(+)";
61         case METRICS_OP_CLEAR_BUFFER_DATA:
62                 return "glCearBufferData(+)";
63         case METRICS_OP_CLEAR_TEX_IMAGE:
64                 return "glClearTexImage(+)";
65         case METRICS_OP_COPY_PIXELS:
66                 return "glCopyPixels";
67         case METRICS_OP_COPY_TEX_IMAGE:
68                 return "glCopyTexImage(+)";
69         case METRICS_OP_DRAW_PIXELS:
70                 return "glDrawPixels";
71         case METRICS_OP_GET_TEX_IMAGE:
72                 return "glGetTexImage(+)";
73         case METRICS_OP_READ_PIXELS:
74                 return "glReadPixels*";
75         case METRICS_OP_TEX_IMAGE:
76                 return "glTexImage*(+)";
77         default:
78                 fprintf (stderr, "fips: Internal error: "
79                          "Unknown metrics op value: %d\n", op);
80                 exit (1);
81         }
82
83         return "";
84 }
85
86 void
87 metrics_counter_start (void)
88 {
89         context_t *ctx = context_get_current ();
90         metrics_t *metrics = &ctx->metrics;
91         unsigned i;
92
93         /* Initialize the timer_query and monitor objects */
94         glGenQueries (1, &metrics->timer_begun_id);
95
96         glGenPerfMonitorsAMD (1, &metrics->monitor_begun_id);
97
98         for (i = 0; i < ctx->metrics_info.num_groups; i++)
99         {
100                 metrics_group_info_t *group;
101                 int num_counters;
102
103                 group = &ctx->metrics_info.groups[i];
104
105                 num_counters = group->num_counters;
106                 if (group->max_active_counters < group->num_counters)
107                 {
108                         fprintf (stderr, "Warning: Only monitoring %d/%d counters from group %d\n",
109                                  group->max_active_counters,
110                                  group->num_counters, i);
111                         num_counters = group->max_active_counters;
112
113                 }
114
115                 glSelectPerfMonitorCountersAMD(metrics->monitor_begun_id,
116                                                GL_TRUE, group->id,
117                                                num_counters,
118                                                group->counter_ids);
119         }
120
121         /* Start the queries */
122         glBeginQuery (GL_TIME_ELAPSED, metrics->timer_begun_id);
123
124         glBeginPerfMonitorAMD (metrics->monitor_begun_id);
125 }
126
127 void
128 metrics_counter_stop (void)
129 {
130         context_t *ctx = context_get_current ();
131         metrics_t *metrics = &ctx->metrics;
132         timer_query_t *timer;
133         monitor_t *monitor;
134
135         /* Stop the current timer and monitor. */
136         glEndQuery (GL_TIME_ELAPSED);
137         glEndPerfMonitorAMD (metrics->monitor_begun_id);
138
139         /* Add these IDs to our lists of outstanding queries and
140          * monitors so the results can be collected later. */
141         timer = xmalloc (sizeof (timer_query_t));
142
143         timer->op = metrics->op;
144         timer->id = metrics->timer_begun_id;
145         timer->next = NULL;
146
147         if (metrics->timer_tail) {
148                 metrics->timer_tail->next = timer;
149                 metrics->timer_tail = timer;
150         } else {
151                 metrics->timer_tail = timer;
152                 metrics->timer_head = timer;
153         }
154
155         /* Create a new performance-monitor query */
156         monitor = xmalloc (sizeof (monitor_t));
157
158         monitor->op = metrics->op;
159         monitor->id = metrics->monitor_begun_id;
160         monitor->next = NULL;
161
162         if (metrics->monitor_tail) {
163                 metrics->monitor_tail->next = monitor;
164                 metrics->monitor_tail = monitor;
165         } else {
166                 metrics->monitor_tail = monitor;
167                 metrics->monitor_head = monitor;
168         }
169
170         metrics->monitors_in_flight++;
171
172         /* Avoid being a resource hog and collect outstanding results
173          * once we have sent off a large number of
174          * queries. (Presumably, many of the outstanding queries are
175          * available by now.)
176          */
177         if (metrics->monitors_in_flight > MAX_MONITORS_IN_FLIGHT)
178                 metrics_collect_available ();
179 }
180
181 void
182 metrics_set_current_op (metrics_op_t op)
183 {
184         context_t *ctx = context_get_current ();
185         metrics_t *metrics = &ctx->metrics;
186
187         metrics->op = op;
188 }
189
190 metrics_op_t
191 metrics_get_current_op (void)
192 {
193         context_t *ctx = context_get_current ();
194         metrics_t *metrics = &ctx->metrics;
195
196         return metrics->op;
197 }
198
199 static void
200 op_metrics_init (context_t *ctx, op_metrics_t *metrics, metrics_op_t op)
201 {
202         metrics_info_t *info = &ctx->metrics_info;
203         unsigned i, j;
204
205         metrics->op = op;
206         metrics->time_ns = 0.0;
207
208         metrics->counters = xmalloc (sizeof(double *) * info->num_groups);
209
210         for (i = 0; i < info->num_groups; i++) {
211                 metrics->counters[i] = xmalloc (sizeof (double) *
212                                                 info->groups[i].num_counters);
213                 for (j = 0; j < info->groups[i].num_counters; j++)
214                         metrics->counters[i][j] = 0.0;
215         }
216 }
217
218 static op_metrics_t *
219 ctx_get_op_metrics (context_t *ctx, metrics_op_t op)
220 {
221         metrics_t *metrics = &ctx->metrics;
222         unsigned i;
223
224         if (op >= metrics->num_op_metrics)
225         {
226                 metrics->op_metrics = realloc (metrics->op_metrics,
227                                                (op + 1) * sizeof (op_metrics_t));
228                 for (i = metrics->num_op_metrics; i < op + 1; i++)
229                         op_metrics_init (ctx, &metrics->op_metrics[i], i);
230
231                 metrics->num_op_metrics = op + 1;
232         }
233
234         return &metrics->op_metrics[op];
235 }
236
237 static void
238 accumulate_program_metrics (metrics_op_t op, GLuint *result, GLuint size)
239 {
240 #define CONSUME(var)                                                    \
241         if (p + sizeof(var) > ((unsigned char *) result) + size)        \
242         {                                                               \
243                 fprintf (stderr, "Unexpected end-of-buffer while "      \
244                          "parsing results\n");                          \
245                 break;                                                  \
246         }                                                               \
247         (var) = *((typeof(var) *) p);                                   \
248         p += sizeof(var);
249
250         context_t *ctx = context_get_current ();
251         metrics_info_t *info = &ctx->metrics_info;
252         op_metrics_t *metrics = ctx_get_op_metrics (ctx, op);
253         unsigned char *p = (unsigned char *) result;
254
255         while (p < ((unsigned char *) result) + size)
256         {
257                 GLuint group_id, group_index;
258                 GLuint counter_id, counter_index;
259                 metrics_group_info_t *group;
260                 double value;
261                 unsigned i;
262
263                 CONSUME (group_id);
264                 CONSUME (counter_id);
265
266                 for (i = 0; i < info->num_groups; i++) {
267                         if (info->groups[i].id == group_id)
268                                 break;
269                 }
270                 group_index = i;
271                 assert (group_index < info->num_groups);
272                 group = &info->groups[group_index];
273
274                 for (i = 0; i < group->num_counters; i++) {
275                         if (group->counter_ids[i] == counter_id)
276                                 break;
277                 }
278                 counter_index = i;
279                 assert (counter_index < group->num_counters);
280
281                 switch (group->counter_types[counter_index])
282                 {
283                         uint uint_value;
284                         uint64_t uint64_value;
285                         float float_value;
286                 case GL_UNSIGNED_INT:
287                         CONSUME (uint_value);
288                         value = uint_value;
289                         break;
290                 case GL_UNSIGNED_INT64_AMD:
291                         CONSUME (uint64_value);
292                         value = uint64_value;
293                         break;
294                 case GL_PERCENTAGE_AMD:
295                 case GL_FLOAT:
296                         CONSUME (float_value);
297                         value = float_value;
298                         break;
299                 default:
300                         fprintf (stderr, "fips: Warning: Unknown counter value type (%d)\n",
301                                  group->counter_types[counter_index]);
302                         value = 0.0;
303                         break;
304                 }
305
306                 metrics->counters[group_index][counter_index] += value;
307         }
308 }
309
310 static void
311 accumulate_program_time (metrics_op_t op, unsigned time_ns)
312 {
313         context_t *ctx = context_get_current ();
314         op_metrics_t *metrics;
315
316         metrics = ctx_get_op_metrics (ctx, op);
317
318         metrics->time_ns += time_ns;
319 }
320
321 typedef struct per_stage_metrics
322 {
323         op_metrics_t *metrics;
324         shader_stage_info_t *stage;
325         double time_ns;
326         double active;
327 } per_stage_metrics_t;
328
329 static int
330 _is_shader_stage_counter (metrics_info_t *info,
331                           unsigned group_index,
332                           unsigned counter_index)
333 {
334         shader_stage_info_t *stage;
335         unsigned i;
336
337         for (i = 0; i < info->num_shader_stages; i++) {
338                 stage = &info->stages[i];
339
340                 if (stage->active_group_index == group_index &&
341                     stage->active_counter_index == counter_index)
342                 {
343                         return 1;
344                 }
345
346                 if (stage->stall_group_index == group_index &&
347                     stage->stall_counter_index == counter_index)
348                 {
349                         return 1;
350                 }
351         }
352
353         return 0;
354 }
355
356 static void
357 print_per_stage_metrics (context_t *ctx,
358                          per_stage_metrics_t *per_stage,
359                          double total)
360 {
361         metrics_info_t *info = &ctx->metrics_info;
362         op_metrics_t *metric = per_stage->metrics;
363         metrics_group_info_t *group;
364         const char *op_string;
365         unsigned group_index, counter;
366         double value;
367
368         /* Don't print anything for stages with no alloted time. */
369         if (per_stage->time_ns == 0.0)
370                 return;
371
372         op_string = metrics_op_string (metric->op);
373
374         printf ("%21s", op_string);
375
376         if (metric->op >= METRICS_OP_SHADER) {
377                 printf (" %3d", metric->op - METRICS_OP_SHADER);
378         } else {
379                 printf ("    ");
380
381         }
382
383         if (per_stage->stage)
384                 printf (" %cS:", per_stage->stage->name[0]);
385         else
386                 printf ("   :");
387
388         printf ("\t%7.2f ms (%4.1f%%)",
389                 per_stage->time_ns / 1e6,
390                 per_stage->time_ns / total * 100);
391
392         if (per_stage->active)
393                 printf (", %4.1f%% active", per_stage->active * 100);
394
395         printf ("\n");
396
397         /* I'm not seeing a lot of value printing the rest of these
398          * performance counters by default yet. Use --verbose to get
399          * them for now. */
400         if (! verbose)
401                 return;
402
403         printf ("[");
404         for (group_index = 0; group_index < info->num_groups; group_index++) {
405                 group = &info->groups[group_index];
406                 for (counter = 0; counter < group->num_counters; counter++) {
407
408                         /* Don't print this counter value if it's a
409                          * per-stage cycle counter, (which we have
410                          * already accounted for). */
411                         if (_is_shader_stage_counter (info, group_index, counter))
412                                 continue;
413
414                         value = metric->counters[group_index][counter];
415                         if (value == 0.0)
416                                 continue;
417                         printf ("%s: %.2f ", group->counter_names[counter],
418                                 value / 1e6);
419                 }
420         }
421         printf ("]\n");
422 }
423
424 static int
425 time_compare(const void *in_a, const void *in_b, void *arg unused)
426 {
427         const per_stage_metrics_t *a = in_a;
428         const per_stage_metrics_t *b = in_b;
429
430
431         if (a->time_ns < b->time_ns)
432                 return -1;
433         if (a->time_ns > b->time_ns)
434                 return 1;
435         return 0;
436 }
437
438 static void
439 print_program_metrics (void)
440 {
441         context_t *ctx = context_get_current ();
442         metrics_t *metrics = &ctx->metrics;
443         metrics_info_t *info = &ctx->metrics_info;
444         unsigned num_shader_stages = info->num_shader_stages;
445         per_stage_metrics_t *sorted, *per_stage;
446         double total_time, op_cycles;
447         op_metrics_t *op;
448         unsigned group_index, counter_index;
449         unsigned i, j, num_sorted;
450
451         /* Make a sorted list of the per-stage operations by time
452          * used, and figure out the total so we can print percentages.
453          */
454         num_sorted = metrics->num_op_metrics * num_shader_stages;
455
456         sorted = xmalloc (sizeof (*sorted) * num_sorted);
457
458         total_time = 0.0;
459
460         for (i = 0; i < metrics->num_op_metrics; i++) {
461
462                 op = &metrics->op_metrics[i];
463
464                 /* Accumulate total time across all ops. */
465                 total_time += op->time_ns;
466
467                 /* Also, find total cycles in all stages of this op. */
468                 op_cycles = 0.0;
469
470                 for (j = 0; j < num_shader_stages; j++) {
471                         /* Active cycles */
472                         group_index = info->stages[j].active_group_index;
473                         counter_index = info->stages[j].active_counter_index;
474                         op_cycles += op->counters[group_index][counter_index];
475
476                         /* Stall cycles */
477                         group_index = info->stages[j].stall_group_index;
478                         counter_index = info->stages[j].stall_counter_index;
479                         op_cycles += op->counters[group_index][counter_index];
480                 }
481
482                 for (j = 0; j < num_shader_stages; j++) {
483                         double active_cycles, stall_cycles, stage_cycles;
484
485                         /* Active cycles */
486                         group_index = info->stages[j].active_group_index;
487                         counter_index = info->stages[j].active_counter_index;
488                         active_cycles = op->counters[group_index][counter_index];
489
490                         /* Stall cycles */
491                         group_index = info->stages[j].stall_group_index;
492                         counter_index = info->stages[j].stall_counter_index;
493                         stall_cycles = op->counters[group_index][counter_index];
494
495                         stage_cycles = active_cycles + stall_cycles;
496
497                         per_stage = &sorted[i * num_shader_stages + j];
498                         per_stage->metrics = op;
499
500                         if (op_cycles) {
501                                 per_stage->stage = &info->stages[j];
502                                 per_stage->time_ns = op->time_ns * (stage_cycles / op_cycles);
503                         } else {
504                                 /* If we don't have any per-stage cycle counts
505                                  * for this operation, then use the first
506                                  * stage as a placeholder for all the time,
507                                  * but NULL-ify the stage info so that the
508                                  * report doesn't lie about this time being
509                                  * from any particular stage. */
510                                 per_stage->stage = NULL;
511                                 if (j == 0) {
512                                         per_stage->time_ns = op->time_ns;
513                                 } else {
514                                         per_stage->time_ns = 0.0;
515                                 }
516                         }
517
518                         if (stage_cycles) {
519                                 per_stage->active = active_cycles / stage_cycles;
520                         } else {
521                                 per_stage->active = 0.0;
522                         }
523                 }
524         }
525
526         qsort_r (sorted, num_sorted, sizeof (*sorted),
527                  time_compare, metrics->op_metrics);
528
529         for (i = 0; i < num_sorted; i++)
530                 print_per_stage_metrics (ctx, &sorted[i], total_time);
531
532         free (sorted);
533 }
534
535 /* Called at program exit.
536  *
537  * This is similar to metrics_info_fini, but only frees any used
538  * memory. Notably, it does not call any OpenGL functions, (since the
539  * OpenGL context no longer exists at program exit).
540  */
541 static void
542 metrics_exit (void)
543 {
544         context_t *ctx = context_get_current ();
545         metrics_t *metrics = &ctx->metrics;
546         metrics_info_t *info = &ctx->metrics_info;
547         unsigned i, j;
548         timer_query_t *timer, *timer_next;
549         monitor_t *monitor, *monitor_next;
550
551         if (verbose)
552                 printf ("fips: terminating\n");
553
554         if (! info->initialized)
555                 return;
556
557         for (timer = metrics->timer_head;
558              timer;
559              timer = timer_next)
560         {
561                 timer_next = timer->next;
562                 free (timer);
563         }
564
565         for (monitor = metrics->monitor_head;
566              monitor;
567              monitor = monitor_next)
568         {
569                 monitor_next = monitor->next;
570                 free (monitor);
571         }
572
573         for (i = 0; i < info->num_groups; i++) {
574                 metrics_group_info_t *group = &info->groups[i];
575
576                 for (j = 0; j < group->num_counters; i++)
577                         free (group->counter_names[j]);
578
579                 free (group->counter_types);
580                 free (group->counter_names);
581                 free (group->counter_ids);
582
583                 free (group->name);
584         }
585
586         free (info->groups);
587
588         for (i = 0; i < info->num_shader_stages; i++)
589                 free (info->stages[i].name);
590
591         free (info->stages);
592 }
593
594 void
595 metrics_collect_available (void)
596 {
597         context_t *ctx = context_get_current ();
598         metrics_t *metrics = &ctx->metrics;
599
600         /* Consume all timer queries that are ready. */
601         timer_query_t *timer = metrics->timer_head;
602
603         while (timer) {
604                 GLuint available, elapsed;
605
606                 glGetQueryObjectuiv (timer->id,
607                                      GL_QUERY_RESULT_AVAILABLE, &available);
608                 if (! available)
609                         break;
610
611                 glGetQueryObjectuiv (timer->id,
612                                      GL_QUERY_RESULT, &elapsed);
613
614                 accumulate_program_time (timer->op, elapsed);
615
616                 metrics->timer_head = timer->next;
617                 if (metrics->timer_head == NULL)
618                         metrics->timer_tail = NULL;
619
620                 glDeleteQueries (1, &timer->id);
621
622                 free (timer);
623                 timer = metrics->timer_head;
624         }
625
626         /* And similarly for all performance monitors that are ready. */
627         monitor_t *monitor = metrics->monitor_head;
628
629         while (monitor) {
630                 GLuint available, result_size, *result;
631                 GLint bytes_written;
632
633                 glGetPerfMonitorCounterDataAMD (monitor->id,
634                                                 GL_PERFMON_RESULT_AVAILABLE_AMD,
635                                                 sizeof (available), &available,
636                                                 NULL);
637                 if (! available)
638                         break;
639
640                 glGetPerfMonitorCounterDataAMD (monitor->id,
641                                                 GL_PERFMON_RESULT_SIZE_AMD,
642                                                 sizeof (result_size),
643                                                 &result_size, NULL);
644
645                 result = xmalloc (result_size);
646
647                 glGetPerfMonitorCounterDataAMD (monitor->id,
648                                                 GL_PERFMON_RESULT_AMD,
649                                                 result_size, result,
650                                                 &bytes_written);
651
652                 accumulate_program_metrics (monitor->op, result, result_size);
653
654                 free (result);
655
656                 metrics->monitor_head = monitor->next;
657                 if (metrics->monitor_head == NULL)
658                         metrics->monitor_tail = NULL;
659
660                 glDeletePerfMonitorsAMD (1, &monitor->id);
661
662                 free (monitor);
663
664                 metrics->monitors_in_flight--;
665
666                 monitor = metrics->monitor_head;
667         }
668 }
669
670
671 void
672 metrics_end_frame (void)
673 {
674         static int initialized = 0;
675         static struct timeval tv_start, tv_now;
676
677         if (! initialized) {
678                 gettimeofday (&tv_start, NULL);
679                 atexit (metrics_exit);
680                 if (getenv ("FIPS_VERBOSE"))
681                         verbose = 1;
682                 initialized = 1;
683         }
684
685         frames++;
686
687         metrics_collect_available ();
688
689         if (frames % 15 == 0) {
690                 double fps;
691
692                 gettimeofday (&tv_now, NULL);
693
694                 fps = (double) frames / (tv_now.tv_sec - tv_start.tv_sec +
695                                          (tv_now.tv_usec - tv_start.tv_usec) / 1.0e6);
696
697                 printf("FPS: %.3f\n", fps);
698
699                 print_program_metrics ();
700         }
701 }
702
703 void
704 metrics_init (metrics_t *metrics)
705 {
706         metrics->op = 0;
707
708         metrics->timer_begun_id = 0;
709
710         metrics->timer_head = NULL;
711         metrics->timer_tail = NULL;
712
713         metrics->monitor_begun_id = 0;
714
715         metrics->monitor_head = NULL;
716         metrics->monitor_tail = NULL;
717
718         metrics->monitors_in_flight = 0;
719
720         metrics->num_op_metrics = 0;
721         metrics->op_metrics = NULL;
722 }
723
724 void
725 metrics_fini (metrics_t *metrics)
726 {
727         timer_query_t *timer, *timer_next;
728         monitor_t *monitor, *monitor_next;
729
730         /* Since these metrics are going away, let's first collect
731          * whatever results might already be available. */
732         metrics_collect_available ();
733
734         /* If, after collection, any queries are still outstanding,
735          * just give up and clean them up. */
736         if (metrics->timer_begun_id) {
737                 glEndQuery (GL_TIME_ELAPSED);
738                 glDeleteQueries (1, &metrics->timer_begun_id);
739                 metrics->timer_begun_id = 0;
740         }
741
742         for (timer = metrics->timer_head;
743              timer;
744              timer = timer_next)
745         {
746                 glDeleteQueries (1, &timer->id);
747                 timer_next = timer->next;
748                 free (timer);
749         }
750         metrics->timer_head = NULL;
751         metrics->timer_tail = NULL;
752
753         if (metrics->monitor_begun_id) {
754                 glEndPerfMonitorAMD (metrics->monitor_begun_id);
755                 glDeletePerfMonitorsAMD (1, &metrics->monitor_begun_id);
756                 metrics->monitor_begun_id = 0;
757         }
758
759         for (monitor = metrics->monitor_head;
760              monitor;
761              monitor = monitor_next)
762         {
763                 glDeletePerfMonitorsAMD (1, &monitor->id);
764                 monitor_next = monitor->next;
765                 free (monitor);
766         }
767         metrics->monitor_head = NULL;
768         metrics->monitor_tail = NULL;
769
770         metrics->monitors_in_flight = 0;
771
772 }