xref: /aosp_15_r20/external/mesa3d/src/intel/perf/intel_perf_query_layout.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <getopt.h>
27 
28 #include "dev/intel_debug.h"
29 #include "dev/intel_device_info.h"
30 #include "intel_perf.h"
31 
32 static void
print_help(const char * name)33 print_help(const char *name)
34 {
35    fprintf(stdout, "%s -p <platform> ...\n", name);
36    fprintf(stdout, "    -p/--platform      <platform>  Platform to use (skl, icl, tgl, etc...)\n");
37    fprintf(stdout, "    -P/--print-metric  <name>      Print a given metric set\n");
38 }
39 
40 static void
print_metric_set(const struct intel_perf_query_info * metric_set)41 print_metric_set(const struct intel_perf_query_info *metric_set)
42 {
43    for (uint32_t c = 0; c < metric_set->n_counters; c++) {
44       const struct intel_perf_query_counter *counter = &metric_set->counters[c];
45       fprintf(stdout, "   %s: offset=%zx/0x%zx name=%s\n",
46               counter->symbol_name, counter->offset, counter->offset, counter->name);
47    }
48 }
49 
50 int
main(int argc,char * argv[])51 main(int argc, char *argv[])
52 {
53    const struct option perf_opts[] = {
54       { "help",          no_argument,       NULL, 'h' },
55       { "platform",      required_argument, NULL, 'p' },
56       { "print-metric",  required_argument, NULL, 'P' },
57       { NULL,            0,                 NULL,  0  }
58    };
59    const char *platform_name = NULL, *print_metric = NULL;
60 
61    int o = 0;
62    while ((o = getopt_long(argc, argv, "hp:P:a", perf_opts, NULL)) != -1) {
63       switch (o) {
64       case 'h':
65          print_help(argv[0]);
66          return EXIT_SUCCESS;
67       case 'p':
68          platform_name = optarg;
69          break;
70       case 'P':
71          print_metric = optarg;
72          break;
73       default:
74          break;
75       }
76    }
77 
78    if (!platform_name) {
79       fprintf(stderr, "No platform specified.\n");
80       return EXIT_FAILURE;
81    }
82 
83    int devid = intel_device_name_to_pci_device_id(platform_name);
84    if (!devid) {
85       fprintf(stderr, "Invalid platform name.\n");
86       return EXIT_FAILURE;
87    }
88 
89    struct intel_device_info devinfo;
90    if (!intel_get_device_info_from_pci_id(devid, &devinfo)) {
91       fprintf(stderr, "Unknown platform.\n");
92       return EXIT_FAILURE;
93    }
94 
95    /* Force metric loading. */
96    intel_debug |= DEBUG_NO_OACONFIG;
97 
98    struct intel_perf_config *perf_cfg = intel_perf_new(NULL);
99    intel_perf_init_metrics(perf_cfg, &devinfo, -1, true, true);
100 
101    if (!(perf_cfg->features_supported & INTEL_PERF_FEATURE_QUERY_PERF)) {
102       fprintf(stderr, "No supported queries for platform.\n");
103       intel_perf_free(perf_cfg);
104       return EXIT_FAILURE;
105    }
106 
107    if (print_metric) {
108       bool found = false;
109       for (uint32_t i = 0; i < perf_cfg->n_queries; i++) {
110          const struct intel_perf_query_info *metric_set = &perf_cfg->queries[i];
111 
112          if (metric_set->symbol_name && !strcmp(metric_set->symbol_name, print_metric)) {
113             fprintf(stdout, "%s name=%s size=%zx counters=%u:\n",
114                     metric_set->symbol_name, metric_set->name,
115                     metric_set->data_size, metric_set->n_counters);
116             print_metric_set(metric_set);
117             found = true;
118             break;
119          }
120       }
121 
122       if (!found) {
123          fprintf(stderr, "Unknown metric '%s'.\n", print_metric);
124          return EXIT_FAILURE;
125       }
126    } else {
127       for (uint32_t i = 0; i < perf_cfg->n_queries; i++) {
128          const struct intel_perf_query_info *metric_set = &perf_cfg->queries[i];
129 
130          fprintf(stdout, "%s name=%s size=%zx counters=%u:\n",
131                  metric_set->symbol_name, metric_set->name,
132                  metric_set->data_size, metric_set->n_counters);
133          print_metric_set(metric_set);
134       }
135    }
136 
137    intel_perf_free(perf_cfg);
138    return EXIT_SUCCESS;
139 }
140