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