^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "perf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "util/trace-event.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "util/tool.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "util/session.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "util/data.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "util/map_symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "util/mem-events.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "util/dso.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "util/map.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "util/symbol.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define MEM_OPERATION_LOAD 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define MEM_OPERATION_STORE 0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct perf_mem {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct perf_tool tool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) char const *input_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) bool hide_unresolved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) bool dump_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) bool force;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) bool phys_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int operation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const char *cpu_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int parse_record_events(const struct option *opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) const char *str, int unset __maybe_unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct perf_mem *mem = *(struct perf_mem **)opt->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!strcmp(str, "list")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) perf_mem_events__list();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (perf_mem_events__parse(str))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) exit(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mem->operation = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static const char * const __usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) "perf mem record [<options>] [<command>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) "perf mem record [<options>] -- <command> [<options>]",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static const char * const *record_mem_usage = __usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int rec_argc, i = 0, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const char **rec_argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) bool all_user = false, all_kernel = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct option options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) OPT_CALLBACK('e', "event", &mem, "event",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) "event selector. use 'perf mem record -e list' to list available events",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) parse_record_events),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) OPT_INCR('v', "verbose", &verbose,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) "be more verbose (show counter open errors, etc)"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) argc = parse_options(argc, argv, options, record_mem_usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) PARSE_OPT_KEEP_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) rec_argc = argc + 9; /* max number of arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) rec_argv = calloc(rec_argc + 1, sizeof(char *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!rec_argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) rec_argv[i++] = "record";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (mem->operation & MEM_OPERATION_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (mem->operation & MEM_OPERATION_STORE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) rec_argv[i++] = "-W";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) rec_argv[i++] = "-d";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (mem->phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) rec_argv[i++] = "--phys-data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!perf_mem_events[j].record)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!perf_mem_events[j].supported) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pr_err("failed: event '%s' not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) perf_mem_events__name(j));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) free(rec_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) rec_argv[i++] = "-e";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) rec_argv[i++] = perf_mem_events__name(j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (all_user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) rec_argv[i++] = "--all-user";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (all_kernel)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) rec_argv[i++] = "--all-kernel";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) for (j = 0; j < argc; j++, i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) rec_argv[i] = argv[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (verbose > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) pr_debug("calling: record ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) while (rec_argv[j]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pr_debug("%s ", rec_argv[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) j++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ret = cmd_record(i, rec_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) free(rec_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) dump_raw_samples(struct perf_tool *tool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct addr_location al;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) const char *fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (machine__resolve(machine, &al, sample) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) fprintf(stderr, "problem processing %d event, skipping it.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) event->header.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (al.map != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) al.map->dso->hit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (mem->phys_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (symbol_conf.field_sep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s0x%016"PRIx64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) "%s%"PRIu64"%s0x%"PRIx64"%s%s:%s\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) "%s0x%016"PRIx64"%s%5"PRIu64"%s0x%06"PRIx64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) "%s%s:%s\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) symbol_conf.field_sep = " ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) printf(fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) sample->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) sample->tid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) sample->ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) sample->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) sample->phys_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) sample->weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) sample->data_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) al.sym ? al.sym->name : "???");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (symbol_conf.field_sep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) "%s0x%"PRIx64"%s%s:%s\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) "%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) symbol_conf.field_sep = " ";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) printf(fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) sample->pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) sample->tid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) sample->ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) sample->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) sample->weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) sample->data_src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) al.sym ? al.sym->name : "???");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) addr_location__put(&al);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int process_sample_event(struct perf_tool *tool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) union perf_event *event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct perf_sample *sample,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct evsel *evsel __maybe_unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct machine *machine)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return dump_raw_samples(tool, event, sample, machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int report_raw_events(struct perf_mem *mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct perf_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) .path = input_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) .mode = PERF_DATA_MODE_READ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) .force = mem->force,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct perf_session *session = perf_session__new(&data, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) &mem->tool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (IS_ERR(session))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return PTR_ERR(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (mem->cpu_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) ret = perf_session__cpu_bitmap(session, mem->cpu_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) mem->cpu_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) ret = symbol__init(&session->header.env);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) goto out_delete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (mem->phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) printf("# PID, TID, IP, ADDR, PHYS ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ret = perf_session__process_events(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) out_delete:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) perf_session__delete(session);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static int report_events(int argc, const char **argv, struct perf_mem *mem)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) const char **rep_argv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) int ret, i = 0, j, rep_argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (mem->dump_raw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return report_raw_events(mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) rep_argc = argc + 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) rep_argv = calloc(rep_argc + 1, sizeof(char *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (!rep_argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) rep_argv[i++] = "report";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) rep_argv[i++] = "--mem-mode";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) rep_argv[i++] = "-n"; /* display number of samples */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * there is no weight (cost) associated with stores, so don't print
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * the column
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!(mem->operation & MEM_OPERATION_LOAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (mem->phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) "dso_daddr,tlb,locked,phys_daddr";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) "dso_daddr,tlb,locked";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) } else if (mem->phys_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) rep_argv[i++] = "--sort=local_weight,mem,sym,dso,symbol_daddr,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) "dso_daddr,snoop,tlb,locked,phys_daddr";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) for (j = 1; j < argc; j++, i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) rep_argv[i] = argv[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ret = cmd_report(i, rep_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) free(rep_argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct mem_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) #define MEM_OPT(n, m) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) { .name = n, .mode = (m) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #define MEM_END { .name = NULL }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static const struct mem_mode mem_modes[]={
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MEM_OPT("load", MEM_OPERATION_LOAD),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MEM_OPT("store", MEM_OPERATION_STORE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) MEM_END
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) parse_mem_ops(const struct option *opt, const char *str, int unset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int *mode = (int *)opt->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) const struct mem_mode *m;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) char *s, *os = NULL, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) int ret = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (unset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /* str may be NULL in case no arg is passed to -t */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) /* because str is read-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) s = os = strdup(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* reset mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) p = strchr(s, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) for (m = mem_modes; m->name; m++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!strcasecmp(s, m->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (!m->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) fprintf(stderr, "unknown sampling op %s,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) " check man page\n", s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) *mode |= m->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) s = p + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (*mode == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) *mode = MEM_OPERATION_LOAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) free(os);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int cmd_mem(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct perf_mem mem = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) .tool = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) .sample = process_sample_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) .mmap = perf_event__process_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) .mmap2 = perf_event__process_mmap2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) .comm = perf_event__process_comm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) .lost = perf_event__process_lost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) .fork = perf_event__process_fork,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) .build_id = perf_event__process_build_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .namespaces = perf_event__process_namespaces,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .ordered_events = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .input_name = "perf.data",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * default to both load an store sampling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) .operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) const struct option mem_options[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) OPT_CALLBACK('t', "type", &mem.operation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) "type", "memory operations(load,store) Default load,store",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) parse_mem_ops),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) "dump raw samples in ASCII"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) "Only display entries resolved to a symbol"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) OPT_STRING('i', "input", &input_name, "file",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) "input file name"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) "list of cpus to profile"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) "separator",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) "separator for columns, no spaces will be added"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) " between columns '.' is reserved."),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) OPT_END()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) const char *const mem_subcommands[] = { "record", "report", NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) const char *mem_usage[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) if (perf_mem_events__init()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) pr_err("failed: memory events not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) mem_usage, PARSE_OPT_KEEP_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) usage_with_options(mem_usage, mem_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (!mem.input_name || !strlen(mem.input_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) mem.input_name = "-";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) mem.input_name = "perf.data";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!strncmp(argv[0], "rec", 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return __cmd_record(argc, argv, &mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) else if (!strncmp(argv[0], "rep", 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return report_events(argc, argv, &mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) usage_with_options(mem_usage, mem_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }