1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2020, Intel Corporation
7 * Authors: Alexander Antonov <alexander.antonov@linux.intel.com>
10 #include <api/fs/fs.h>
11 #include <linux/kernel.h>
12 #include <linux/err.h>
17 #include <sys/types.h>
24 #include "util/cpumap.h"
25 #include "util/debug.h"
26 #include "util/iostat.h"
27 #include "util/counts.h"
34 #define UNCORE_IIO_PMU_PATH "devices/uncore_iio_%d"
35 #define SYSFS_UNCORE_PMU_PATH "%s/"UNCORE_IIO_PMU_PATH
36 #define PLATFORM_MAPPING_PATH UNCORE_IIO_PMU_PATH"/die%d"
39 * Each metric requiries one IIO event which increments at every 4B transfer
40 * in corresponding direction. The formulas to compute metrics are generic:
41 * #EventCount * 4B / (1024 * 1024)
43 static const char * const iostat_metrics[] = {
50 static inline int iostat_metrics_count(void)
52 return sizeof(iostat_metrics) / sizeof(char *);
55 static const char *iostat_metric_by_idx(int idx)
57 return *(iostat_metrics + idx % iostat_metrics_count());
60 struct iio_root_port {
68 struct iio_root_ports_list {
69 struct iio_root_port **rps;
73 static struct iio_root_ports_list *root_ports;
75 static void iio_root_port_show(FILE *output,
76 const struct iio_root_port * const rp)
79 fprintf(output, "S%d-uncore_iio_%d<%04x:%02x>\n",
80 rp->die, rp->pmu_idx, rp->domain, rp->bus);
83 static struct iio_root_port *iio_root_port_new(u32 domain, u8 bus,
86 struct iio_root_port *p = calloc(1, sizeof(*p));
97 static void iio_root_ports_list_free(struct iio_root_ports_list *list)
102 for (idx = 0; idx < list->nr_entries; idx++)
103 free(list->rps[idx]);
109 static struct iio_root_port *iio_root_port_find_by_notation(
110 const struct iio_root_ports_list * const list, u32 domain, u8 bus)
113 struct iio_root_port *rp;
116 for (idx = 0; idx < list->nr_entries; idx++) {
118 if (rp && rp->domain == domain && rp->bus == bus)
125 static int iio_root_ports_list_insert(struct iio_root_ports_list *list,
126 struct iio_root_port * const rp)
128 struct iio_root_port **tmp_buf;
131 rp->idx = list->nr_entries++;
132 tmp_buf = realloc(list->rps,
133 list->nr_entries * sizeof(*list->rps));
135 pr_err("Failed to realloc memory\n");
138 tmp_buf[rp->idx] = rp;
144 static int iio_mapping(u8 pmu_idx, struct iio_root_ports_list * const list)
150 struct iio_root_port *rp;
154 for (int die = 0; die < cpu__max_node(); die++) {
155 scnprintf(path, MAX_PATH, PLATFORM_MAPPING_PATH, pmu_idx, die);
156 if (sysfs__read_str(path, &buf, &size) < 0) {
159 pr_err("Mode iostat is not supported\n");
162 ret = sscanf(buf, "%04x:%02hhx", &domain, &bus);
165 pr_err("Invalid mapping data: iio_%d; die%d\n",
169 rp = iio_root_port_new(domain, bus, die, pmu_idx);
170 if (!rp || iio_root_ports_list_insert(list, rp)) {
179 static u8 iio_pmu_count(void)
183 const char *sysfs = sysfs__mountpoint();
187 snprintf(path, sizeof(path), SYSFS_UNCORE_PMU_PATH,
189 if (access(path, F_OK) != 0)
196 static int iio_root_ports_scan(struct iio_root_ports_list **list)
199 struct iio_root_ports_list *tmp_list;
200 u8 pmu_count = iio_pmu_count();
203 pr_err("Unsupported uncore pmu configuration\n");
207 tmp_list = calloc(1, sizeof(*tmp_list));
211 for (u8 pmu_idx = 0; pmu_idx < pmu_count; pmu_idx++) {
212 ret = iio_mapping(pmu_idx, tmp_list);
220 iio_root_ports_list_free(tmp_list);
225 static int iio_root_port_parse_str(u32 *domain, u8 *bus, char *str)
230 * Expected format domain:bus:
231 * Valid domain range [0:ffff]
232 * Valid bus range [0:ff]
233 * Example: 0000:af, 0:3d, 01:7
235 regcomp(®ex, "^([a-f0-9A-F]{1,}):([a-f0-9A-F]{1,2})", REG_EXTENDED);
236 ret = regexec(®ex, str, 0, NULL, 0);
237 if (ret || sscanf(str, "%08x:%02hhx", domain, bus) != 2)
238 pr_warning("Unrecognized root port format: %s\n"
239 "Please use the following format:\n"
240 "\t [domain]:[bus]\n"
241 "\t for example: 0000:3d\n", str);
247 static int iio_root_ports_list_filter(struct iio_root_ports_list **list,
250 char *tok, *tmp, *filter_copy = NULL;
251 struct iio_root_port *rp;
255 struct iio_root_ports_list *tmp_list = calloc(1, sizeof(*tmp_list));
260 filter_copy = strdup(filter);
264 for (tok = strtok_r(filter_copy, ",", &tmp); tok;
265 tok = strtok_r(NULL, ",", &tmp)) {
266 if (!iio_root_port_parse_str(&domain, &bus, tok)) {
267 rp = iio_root_port_find_by_notation(*list, domain, bus);
269 (*list)->rps[rp->idx] = NULL;
270 ret = iio_root_ports_list_insert(tmp_list, rp);
275 } else if (!iio_root_port_find_by_notation(tmp_list,
277 pr_warning("Root port %04x:%02x were not found\n",
282 if (tmp_list->nr_entries == 0) {
283 pr_err("Requested root ports were not found\n");
287 iio_root_ports_list_free(*list);
289 iio_root_ports_list_free(tmp_list);
297 static int iostat_event_group(struct evlist *evl,
298 struct iio_root_ports_list *list)
302 const char *iostat_cmd_template =
303 "{uncore_iio_%x/event=0x83,umask=0x04,ch_mask=0xF,fc_mask=0x07/,\
304 uncore_iio_%x/event=0x83,umask=0x01,ch_mask=0xF,fc_mask=0x07/,\
305 uncore_iio_%x/event=0xc0,umask=0x04,ch_mask=0xF,fc_mask=0x07/,\
306 uncore_iio_%x/event=0xc0,umask=0x01,ch_mask=0xF,fc_mask=0x07/}";
307 const int len_template = strlen(iostat_cmd_template) + 1;
308 struct evsel *evsel = NULL;
309 int metrics_count = iostat_metrics_count();
310 char *iostat_cmd = calloc(len_template, 1);
315 for (idx = 0; idx < list->nr_entries; idx++) {
316 sprintf(iostat_cmd, iostat_cmd_template,
317 list->rps[idx]->pmu_idx, list->rps[idx]->pmu_idx,
318 list->rps[idx]->pmu_idx, list->rps[idx]->pmu_idx);
319 ret = parse_events(evl, iostat_cmd, NULL);
324 evlist__for_each_entry(evl, evsel) {
325 evsel->priv = list->rps[evsel->core.idx / metrics_count];
327 list->nr_entries = 0;
329 iio_root_ports_list_free(list);
334 int iostat_prepare(struct evlist *evlist, struct perf_stat_config *config)
336 if (evlist->core.nr_entries > 0) {
337 pr_warning("The -e and -M options are not supported."
338 "All chosen events/metrics will be dropped\n");
339 evlist__delete(evlist);
340 evlist = evlist__new();
345 config->metric_only = true;
346 config->aggr_mode = AGGR_GLOBAL;
348 return iostat_event_group(evlist, root_ports);
351 int iostat_parse(const struct option *opt, const char *str,
352 int unset __maybe_unused)
355 struct perf_stat_config *config = (struct perf_stat_config *)opt->data;
357 ret = iio_root_ports_scan(&root_ports);
359 config->iostat_run = true;
361 iostat_mode = IOSTAT_RUN;
362 else if (!strcmp(str, "list"))
363 iostat_mode = IOSTAT_LIST;
365 iostat_mode = IOSTAT_RUN;
366 ret = iio_root_ports_list_filter(&root_ports, str);
372 void iostat_list(struct evlist *evlist, struct perf_stat_config *config)
375 struct iio_root_port *rp = NULL;
377 evlist__for_each_entry(evlist, evsel) {
378 if (rp != evsel->priv) {
380 iio_root_port_show(config->output, rp);
385 void iostat_release(struct evlist *evlist)
388 struct iio_root_port *rp = NULL;
390 evlist__for_each_entry(evlist, evsel) {
391 if (rp != evsel->priv) {
398 void iostat_prefix(struct evlist *evlist,
399 struct perf_stat_config *config,
400 char *prefix, struct timespec *ts)
402 struct iio_root_port *rp = evlist->selected->priv;
406 sprintf(prefix, "%6lu.%09lu%s%04x:%02x%s",
407 ts->tv_sec, ts->tv_nsec,
408 config->csv_sep, rp->domain, rp->bus,
411 sprintf(prefix, "%04x:%02x%s", rp->domain, rp->bus,
416 void iostat_print_header_prefix(struct perf_stat_config *config)
418 if (config->csv_output)
419 fputs("port,", config->output);
420 else if (config->interval)
421 fprintf(config->output, "# time port ");
423 fprintf(config->output, " port ");
426 void iostat_print_metric(struct perf_stat_config *config, struct evsel *evsel,
427 struct perf_stat_output_ctx *out)
429 double iostat_value = 0;
430 u64 prev_count_val = 0;
431 const char *iostat_metric = iostat_metric_by_idx(evsel->core.idx);
432 u8 die = ((struct iio_root_port *)evsel->priv)->die;
433 struct perf_counts_values *count = perf_counts(evsel->counts, die, 0);
435 if (count->run && count->ena) {
436 if (evsel->prev_raw_counts && !out->force_header) {
437 struct perf_counts_values *prev_count =
438 perf_counts(evsel->prev_raw_counts, die, 0);
440 prev_count_val = prev_count->val;
441 prev_count->val = count->val;
443 iostat_value = (count->val - prev_count_val) /
444 ((double) count->run / count->ena);
446 out->print_metric(config, out->ctx, NULL, "%8.0f", iostat_metric,
447 iostat_value / (256 * 1024));
450 void iostat_print_counters(struct evlist *evlist,
451 struct perf_stat_config *config, struct timespec *ts,
452 char *prefix, iostat_print_counter_t print_cnt_cb)
454 void *perf_device = NULL;
455 struct evsel *counter = evlist__first(evlist);
457 evlist__set_selected(evlist, counter);
458 iostat_prefix(evlist, config, prefix, ts);
459 fprintf(config->output, "%s", prefix);
460 evlist__for_each_entry(evlist, counter) {
461 perf_device = evlist->selected->priv;
462 if (perf_device && perf_device != counter->priv) {
463 evlist__set_selected(evlist, counter);
464 iostat_prefix(evlist, config, prefix, ts);
465 fprintf(config->output, "\n%s", prefix);
467 print_cnt_cb(config, counter, prefix);
469 fputc('\n', config->output);