2017-04-18 01:23:08 +07:00
|
|
|
#include <inttypes.h>
|
2017-04-20 06:57:47 +07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2013-01-24 22:10:38 +07:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "perf.h"
|
|
|
|
|
2015-12-15 22:39:39 +07:00
|
|
|
#include <subcmd/parse-options.h>
|
2013-01-24 22:10:38 +07:00
|
|
|
#include "util/trace-event.h"
|
|
|
|
#include "util/tool.h"
|
|
|
|
#include "util/session.h"
|
2013-10-15 21:27:32 +07:00
|
|
|
#include "util/data.h"
|
2016-02-15 15:34:34 +07:00
|
|
|
#include "util/mem-events.h"
|
2016-02-15 15:34:35 +07:00
|
|
|
#include "util/debug.h"
|
2017-04-20 07:34:35 +07:00
|
|
|
#include "util/symbol.h"
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2014-12-17 22:23:55 +07:00
|
|
|
#define MEM_OPERATION_LOAD 0x1
|
|
|
|
#define MEM_OPERATION_STORE 0x2
|
2013-01-24 22:10:38 +07:00
|
|
|
|
|
|
|
struct perf_mem {
|
|
|
|
struct perf_tool tool;
|
|
|
|
char const *input_name;
|
|
|
|
bool hide_unresolved;
|
|
|
|
bool dump_raw;
|
perf mem: Support using -f to override perf.data file ownership
Enable perf mem to use perf.data when it is not owned by current user or
root.
Example:
# perf mem -t load record ls
# chown Yunlong.Song:Yunlong.Song perf.data
# ls -al perf.data
-rw------- 1 Yunlong.Song Yunlong.Song 16392 Apr 2 14:34 perf.data
# id
uid=0(root) gid=0(root) groups=0(root),64(pkcs11)
Before this patch:
# perf mem -D report
File perf.data not owned by current user or root (use -f to override)
# perf mem -D -f report
Error: unknown switch `f'
usage: perf mem [<options>] {record|report}
-t, --type <type> memory operations(load,store) Default load,store
-D, --dump-raw-samples
dump raw samples in ASCII
-U, --hide-unresolved
Only display entries resolved to a symbol
-i, --input <file> input file name
-C, --cpu <cpu> list of cpus to profile
-x, --field-separator <separator>
separator for columns, no spaces will be added
between columns '.' is reserved.
As shown above, the -f option does not work at all.
After this patch:
# perf mem -D report
File perf.data not owned by current user or root (use -f to override)
# perf mem -D -f report
# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL
39095 39095 0xffffffff81127e40 0x016ffff887f45148338 8 0x68100142
/proc/kcore:perf_event_aux
39095 39095 0xffffffff8100a3fe 0xffff89007f8cb7d0 6 0x68100142
/proc/kcore:native_sched_clock
39095 39095 0xffffffff81309139 0xffff88bf44c9ded8 6 0x68100142
/proc/kcore:acpi_map_lookup
39095 39095 0xffffffff810f8c4c 0xffff89007f8ccd88 6 0x68100142
/proc/kcore:rcu_nmi_exit
39095 39095 0xffffffff81136346 0xffff88fea995dd50 6 0x68100142
/proc/kcore:unlock_page
39095 39095 0xffffffff812a64a2 0xffff88fea995dcc8 6 0x68100142
/proc/kcore:half_md4_transform
39095 39095 0x7f0cf877c7e9 0x25dfb94 6 0x68100142
/lib64/libc-2.19.so:__readdir64
39095 39095 0x7f0cf87575a3 0x7f0cf9163731 6 0x68100142
/lib64/libc-2.19.so:__strcoll_l
39095 39095 0xffffffff8116910e 0xffffea01c1bfbd50 23 0x68100242
/proc/kcore:page_remove_rmap
As shown above, the -f option really works now.
Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1427982439-27388-7-git-send-email-yunlong.song@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-02 20:47:15 +07:00
|
|
|
bool force;
|
2017-08-30 00:11:10 +07:00
|
|
|
bool phys_addr;
|
2014-12-17 23:53:27 +07:00
|
|
|
int operation;
|
2013-01-24 22:10:38 +07:00
|
|
|
const char *cpu_list;
|
|
|
|
DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
|
|
|
|
};
|
|
|
|
|
2016-02-15 15:34:35 +07:00
|
|
|
static int parse_record_events(const struct option *opt,
|
|
|
|
const char *str, int unset __maybe_unused)
|
|
|
|
{
|
|
|
|
struct perf_mem *mem = *(struct perf_mem **)opt->value;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
if (strcmp(str, "list")) {
|
|
|
|
if (!perf_mem_events__parse(str)) {
|
|
|
|
mem->operation = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
|
|
|
|
struct perf_mem_event *e = &perf_mem_events[j];
|
|
|
|
|
2016-02-24 15:46:42 +07:00
|
|
|
fprintf(stderr, "%-13s%-*s%s\n",
|
|
|
|
e->tag,
|
2017-02-17 15:17:38 +07:00
|
|
|
verbose > 0 ? 25 : 0,
|
|
|
|
verbose > 0 ? perf_mem_events__name(j) : "",
|
2016-02-24 15:46:42 +07:00
|
|
|
e->supported ? ": available" : "");
|
2016-02-15 15:34:35 +07:00
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const __usage[] = {
|
|
|
|
"perf mem record [<options>] [<command>]",
|
|
|
|
"perf mem record [<options>] -- <command> [<options>]",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char * const *record_mem_usage = __usage;
|
|
|
|
|
2014-12-17 23:53:27 +07:00
|
|
|
static int __cmd_record(int argc, const char **argv, struct perf_mem *mem)
|
2013-01-24 22:10:38 +07:00
|
|
|
{
|
|
|
|
int rec_argc, i = 0, j;
|
|
|
|
const char **rec_argv;
|
|
|
|
int ret;
|
2016-03-24 19:52:16 +07:00
|
|
|
bool all_user = false, all_kernel = false;
|
2016-02-15 15:34:35 +07:00
|
|
|
struct option options[] = {
|
|
|
|
OPT_CALLBACK('e', "event", &mem, "event",
|
|
|
|
"event selector. use 'perf mem record -e list' to list available events",
|
|
|
|
parse_record_events),
|
2016-06-15 01:19:11 +07:00
|
|
|
OPT_UINTEGER(0, "ldlat", &perf_mem_events__loads_ldlat, "mem-loads latency"),
|
2016-02-15 15:34:35 +07:00
|
|
|
OPT_INCR('v', "verbose", &verbose,
|
|
|
|
"be more verbose (show counter open errors, etc)"),
|
2016-12-12 17:35:39 +07:00
|
|
|
OPT_BOOLEAN('U', "all-user", &all_user, "collect only user level data"),
|
|
|
|
OPT_BOOLEAN('K', "all-kernel", &all_kernel, "collect only kernel level data"),
|
2016-02-15 15:34:35 +07:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
argc = parse_options(argc, argv, options, record_mem_usage,
|
|
|
|
PARSE_OPT_STOP_AT_NON_OPTION);
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2016-03-24 19:52:16 +07:00
|
|
|
rec_argc = argc + 9; /* max number of arguments */
|
2013-01-24 22:10:38 +07:00
|
|
|
rec_argv = calloc(rec_argc + 1, sizeof(char *));
|
|
|
|
if (!rec_argv)
|
|
|
|
return -1;
|
|
|
|
|
2014-12-17 22:23:55 +07:00
|
|
|
rec_argv[i++] = "record";
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2016-02-15 15:34:35 +07:00
|
|
|
if (mem->operation & MEM_OPERATION_LOAD)
|
2016-02-15 15:34:34 +07:00
|
|
|
perf_mem_events[PERF_MEM_EVENTS__LOAD].record = true;
|
2016-02-15 15:34:35 +07:00
|
|
|
|
2016-08-11 15:50:57 +07:00
|
|
|
if (mem->operation & MEM_OPERATION_STORE)
|
|
|
|
perf_mem_events[PERF_MEM_EVENTS__STORE].record = true;
|
|
|
|
|
2016-02-15 15:34:35 +07:00
|
|
|
if (perf_mem_events[PERF_MEM_EVENTS__LOAD].record)
|
2014-12-17 22:23:55 +07:00
|
|
|
rec_argv[i++] = "-W";
|
|
|
|
|
|
|
|
rec_argv[i++] = "-d";
|
|
|
|
|
2017-08-30 00:11:10 +07:00
|
|
|
if (mem->phys_addr)
|
|
|
|
rec_argv[i++] = "--phys-data";
|
|
|
|
|
2016-02-15 15:34:34 +07:00
|
|
|
for (j = 0; j < PERF_MEM_EVENTS__MAX; j++) {
|
|
|
|
if (!perf_mem_events[j].record)
|
|
|
|
continue;
|
2014-12-17 22:23:55 +07:00
|
|
|
|
2016-02-24 15:46:42 +07:00
|
|
|
if (!perf_mem_events[j].supported) {
|
|
|
|
pr_err("failed: event '%s' not supported\n",
|
2016-02-24 15:46:43 +07:00
|
|
|
perf_mem_events__name(j));
|
2017-09-14 02:14:19 +07:00
|
|
|
free(rec_argv);
|
2016-02-24 15:46:42 +07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:23:55 +07:00
|
|
|
rec_argv[i++] = "-e";
|
2016-02-24 15:46:43 +07:00
|
|
|
rec_argv[i++] = perf_mem_events__name(j);
|
2016-02-15 15:34:34 +07:00
|
|
|
};
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2016-03-24 19:52:16 +07:00
|
|
|
if (all_user)
|
|
|
|
rec_argv[i++] = "--all-user";
|
|
|
|
|
|
|
|
if (all_kernel)
|
|
|
|
rec_argv[i++] = "--all-kernel";
|
|
|
|
|
2016-02-15 15:34:35 +07:00
|
|
|
for (j = 0; j < argc; j++, i++)
|
2013-01-24 22:10:38 +07:00
|
|
|
rec_argv[i] = argv[j];
|
|
|
|
|
2016-02-15 15:34:35 +07:00
|
|
|
if (verbose > 0) {
|
|
|
|
pr_debug("calling: record ");
|
|
|
|
|
|
|
|
while (rec_argv[j]) {
|
|
|
|
pr_debug("%s ", rec_argv[j]);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
pr_debug("\n");
|
|
|
|
}
|
|
|
|
|
2017-03-27 21:47:20 +07:00
|
|
|
ret = cmd_record(i, rec_argv);
|
2013-01-24 22:10:38 +07:00
|
|
|
free(rec_argv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dump_raw_samples(struct perf_tool *tool,
|
|
|
|
union perf_event *event,
|
|
|
|
struct perf_sample *sample,
|
|
|
|
struct machine *machine)
|
|
|
|
{
|
|
|
|
struct perf_mem *mem = container_of(tool, struct perf_mem, tool);
|
|
|
|
struct addr_location al;
|
|
|
|
const char *fmt;
|
|
|
|
|
2016-03-23 04:39:09 +07:00
|
|
|
if (machine__resolve(machine, &al, sample) < 0) {
|
2013-01-24 22:10:38 +07:00
|
|
|
fprintf(stderr, "problem processing %d event, skipping it.\n",
|
|
|
|
event->header.type);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (al.filtered || (mem->hide_unresolved && al.sym == NULL))
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
goto out_put;
|
2013-01-24 22:10:38 +07:00
|
|
|
|
|
|
|
if (al.map != NULL)
|
|
|
|
al.map->dso->hit = 1;
|
|
|
|
|
2017-08-30 00:11:10 +07:00
|
|
|
if (mem->phys_addr) {
|
|
|
|
if (symbol_conf.field_sep) {
|
|
|
|
fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s0x%016"PRIx64
|
|
|
|
"%s%"PRIu64"%s0x%"PRIx64"%s%s:%s\n";
|
|
|
|
} else {
|
|
|
|
fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
|
|
|
|
"%s0x%016"PRIx64"%s%5"PRIu64"%s0x%06"PRIx64
|
|
|
|
"%s%s:%s\n";
|
|
|
|
symbol_conf.field_sep = " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(fmt,
|
|
|
|
sample->pid,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->tid,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->ip,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->addr,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->phys_addr,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->weight,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->data_src,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
|
|
|
|
al.sym ? al.sym->name : "???");
|
2013-01-24 22:10:38 +07:00
|
|
|
} else {
|
2017-08-30 00:11:10 +07:00
|
|
|
if (symbol_conf.field_sep) {
|
|
|
|
fmt = "%d%s%d%s0x%"PRIx64"%s0x%"PRIx64"%s%"PRIu64
|
|
|
|
"%s0x%"PRIx64"%s%s:%s\n";
|
|
|
|
} else {
|
|
|
|
fmt = "%5d%s%5d%s0x%016"PRIx64"%s0x016%"PRIx64
|
|
|
|
"%s%5"PRIu64"%s0x%06"PRIx64"%s%s:%s\n";
|
|
|
|
symbol_conf.field_sep = " ";
|
|
|
|
}
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2017-08-30 00:11:10 +07:00
|
|
|
printf(fmt,
|
|
|
|
sample->pid,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->tid,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->ip,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->addr,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->weight,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
sample->data_src,
|
|
|
|
symbol_conf.field_sep,
|
|
|
|
al.map ? (al.map->dso ? al.map->dso->long_name : "???") : "???",
|
|
|
|
al.sym ? al.sym->name : "???");
|
|
|
|
}
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 06:43:22 +07:00
|
|
|
out_put:
|
|
|
|
addr_location__put(&al);
|
2013-01-24 22:10:38 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int process_sample_event(struct perf_tool *tool,
|
|
|
|
union perf_event *event,
|
|
|
|
struct perf_sample *sample,
|
2013-12-20 03:00:45 +07:00
|
|
|
struct perf_evsel *evsel __maybe_unused,
|
2013-01-24 22:10:38 +07:00
|
|
|
struct machine *machine)
|
|
|
|
{
|
2013-12-20 03:00:45 +07:00
|
|
|
return dump_raw_samples(tool, event, sample, machine);
|
2013-01-24 22:10:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int report_raw_events(struct perf_mem *mem)
|
|
|
|
{
|
2013-10-15 21:27:32 +07:00
|
|
|
struct perf_data_file file = {
|
|
|
|
.path = input_name,
|
|
|
|
.mode = PERF_DATA_MODE_READ,
|
perf mem: Support using -f to override perf.data file ownership
Enable perf mem to use perf.data when it is not owned by current user or
root.
Example:
# perf mem -t load record ls
# chown Yunlong.Song:Yunlong.Song perf.data
# ls -al perf.data
-rw------- 1 Yunlong.Song Yunlong.Song 16392 Apr 2 14:34 perf.data
# id
uid=0(root) gid=0(root) groups=0(root),64(pkcs11)
Before this patch:
# perf mem -D report
File perf.data not owned by current user or root (use -f to override)
# perf mem -D -f report
Error: unknown switch `f'
usage: perf mem [<options>] {record|report}
-t, --type <type> memory operations(load,store) Default load,store
-D, --dump-raw-samples
dump raw samples in ASCII
-U, --hide-unresolved
Only display entries resolved to a symbol
-i, --input <file> input file name
-C, --cpu <cpu> list of cpus to profile
-x, --field-separator <separator>
separator for columns, no spaces will be added
between columns '.' is reserved.
As shown above, the -f option does not work at all.
After this patch:
# perf mem -D report
File perf.data not owned by current user or root (use -f to override)
# perf mem -D -f report
# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL
39095 39095 0xffffffff81127e40 0x016ffff887f45148338 8 0x68100142
/proc/kcore:perf_event_aux
39095 39095 0xffffffff8100a3fe 0xffff89007f8cb7d0 6 0x68100142
/proc/kcore:native_sched_clock
39095 39095 0xffffffff81309139 0xffff88bf44c9ded8 6 0x68100142
/proc/kcore:acpi_map_lookup
39095 39095 0xffffffff810f8c4c 0xffff89007f8ccd88 6 0x68100142
/proc/kcore:rcu_nmi_exit
39095 39095 0xffffffff81136346 0xffff88fea995dd50 6 0x68100142
/proc/kcore:unlock_page
39095 39095 0xffffffff812a64a2 0xffff88fea995dcc8 6 0x68100142
/proc/kcore:half_md4_transform
39095 39095 0x7f0cf877c7e9 0x25dfb94 6 0x68100142
/lib64/libc-2.19.so:__readdir64
39095 39095 0x7f0cf87575a3 0x7f0cf9163731 6 0x68100142
/lib64/libc-2.19.so:__strcoll_l
39095 39095 0xffffffff8116910e 0xffffea01c1bfbd50 23 0x68100242
/proc/kcore:page_remove_rmap
As shown above, the -f option really works now.
Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1427982439-27388-7-git-send-email-yunlong.song@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-02 20:47:15 +07:00
|
|
|
.force = mem->force,
|
2013-10-15 21:27:32 +07:00
|
|
|
};
|
2013-01-24 22:10:38 +07:00
|
|
|
int ret;
|
2013-10-15 21:27:32 +07:00
|
|
|
struct perf_session *session = perf_session__new(&file, false,
|
|
|
|
&mem->tool);
|
2013-01-24 22:10:38 +07:00
|
|
|
|
|
|
|
if (session == NULL)
|
2014-09-24 08:33:37 +07:00
|
|
|
return -1;
|
2013-01-24 22:10:38 +07:00
|
|
|
|
|
|
|
if (mem->cpu_list) {
|
|
|
|
ret = perf_session__cpu_bitmap(session, mem->cpu_list,
|
|
|
|
mem->cpu_bitmap);
|
2015-07-01 19:08:19 +07:00
|
|
|
if (ret < 0)
|
2013-01-24 22:10:38 +07:00
|
|
|
goto out_delete;
|
|
|
|
}
|
|
|
|
|
2015-07-01 19:08:19 +07:00
|
|
|
ret = symbol__init(&session->header.env);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out_delete;
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2017-08-30 00:11:10 +07:00
|
|
|
if (mem->phys_addr)
|
|
|
|
printf("# PID, TID, IP, ADDR, PHYS ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
|
|
|
|
else
|
|
|
|
printf("# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL\n");
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2015-07-01 19:08:19 +07:00
|
|
|
ret = perf_session__process_events(session);
|
2013-01-24 22:10:38 +07:00
|
|
|
|
|
|
|
out_delete:
|
|
|
|
perf_session__delete(session);
|
2015-07-01 19:08:19 +07:00
|
|
|
return ret;
|
2013-01-24 22:10:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int report_events(int argc, const char **argv, struct perf_mem *mem)
|
|
|
|
{
|
|
|
|
const char **rep_argv;
|
|
|
|
int ret, i = 0, j, rep_argc;
|
|
|
|
|
|
|
|
if (mem->dump_raw)
|
|
|
|
return report_raw_events(mem);
|
|
|
|
|
|
|
|
rep_argc = argc + 3;
|
|
|
|
rep_argv = calloc(rep_argc + 1, sizeof(char *));
|
|
|
|
if (!rep_argv)
|
|
|
|
return -1;
|
|
|
|
|
2014-12-17 22:23:55 +07:00
|
|
|
rep_argv[i++] = "report";
|
|
|
|
rep_argv[i++] = "--mem-mode";
|
|
|
|
rep_argv[i++] = "-n"; /* display number of samples */
|
2013-01-24 22:10:38 +07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* there is no weight (cost) associated with stores, so don't print
|
|
|
|
* the column
|
|
|
|
*/
|
2017-08-30 00:11:10 +07:00
|
|
|
if (!(mem->operation & MEM_OPERATION_LOAD)) {
|
|
|
|
if (mem->phys_addr)
|
|
|
|
rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
|
|
|
|
"dso_daddr,tlb,locked,phys_daddr";
|
|
|
|
else
|
|
|
|
rep_argv[i++] = "--sort=mem,sym,dso,symbol_daddr,"
|
|
|
|
"dso_daddr,tlb,locked";
|
|
|
|
} else if (mem->phys_addr)
|
|
|
|
rep_argv[i++] = "--sort=local_weight,mem,sym,dso,symbol_daddr,"
|
|
|
|
"dso_daddr,snoop,tlb,locked,phys_daddr";
|
2013-01-24 22:10:38 +07:00
|
|
|
|
|
|
|
for (j = 1; j < argc; j++, i++)
|
|
|
|
rep_argv[i] = argv[j];
|
|
|
|
|
2017-03-27 21:47:20 +07:00
|
|
|
ret = cmd_report(i, rep_argv);
|
2013-01-24 22:10:38 +07:00
|
|
|
free(rep_argv);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:23:55 +07:00
|
|
|
struct mem_mode {
|
|
|
|
const char *name;
|
|
|
|
int mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define MEM_OPT(n, m) \
|
|
|
|
{ .name = n, .mode = (m) }
|
|
|
|
|
|
|
|
#define MEM_END { .name = NULL }
|
|
|
|
|
|
|
|
static const struct mem_mode mem_modes[]={
|
|
|
|
MEM_OPT("load", MEM_OPERATION_LOAD),
|
|
|
|
MEM_OPT("store", MEM_OPERATION_STORE),
|
|
|
|
MEM_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
parse_mem_ops(const struct option *opt, const char *str, int unset)
|
|
|
|
{
|
|
|
|
int *mode = (int *)opt->value;
|
|
|
|
const struct mem_mode *m;
|
|
|
|
char *s, *os = NULL, *p;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (unset)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* str may be NULL in case no arg is passed to -t */
|
|
|
|
if (str) {
|
|
|
|
/* because str is read-only */
|
|
|
|
s = os = strdup(str);
|
|
|
|
if (!s)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* reset mode */
|
|
|
|
*mode = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
p = strchr(s, ',');
|
|
|
|
if (p)
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
for (m = mem_modes; m->name; m++) {
|
|
|
|
if (!strcasecmp(s, m->name))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!m->name) {
|
|
|
|
fprintf(stderr, "unknown sampling op %s,"
|
|
|
|
" check man page\n", s);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
*mode |= m->mode;
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
break;
|
|
|
|
|
|
|
|
s = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
if (*mode == 0)
|
|
|
|
*mode = MEM_OPERATION_LOAD;
|
|
|
|
error:
|
|
|
|
free(os);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-27 21:47:20 +07:00
|
|
|
int cmd_mem(int argc, const char **argv)
|
2013-01-24 22:10:38 +07:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
struct perf_mem mem = {
|
|
|
|
.tool = {
|
|
|
|
.sample = process_sample_event,
|
|
|
|
.mmap = perf_event__process_mmap,
|
2013-08-21 17:10:25 +07:00
|
|
|
.mmap2 = perf_event__process_mmap2,
|
2013-01-24 22:10:38 +07:00
|
|
|
.comm = perf_event__process_comm,
|
|
|
|
.lost = perf_event__process_lost,
|
|
|
|
.fork = perf_event__process_fork,
|
|
|
|
.build_id = perf_event__process_build_id,
|
perf tools: Add PERF_RECORD_NAMESPACES to include namespaces related info
Introduce a new option to record PERF_RECORD_NAMESPACES events emitted
by the kernel when fork, clone, setns or unshare are invoked. And update
perf-record documentation with the new option to record namespace
events.
Committer notes:
Combined it with a later patch to allow printing it via 'perf report -D'
and be able to test the feature introduced in this patch. Had to move
here also perf_ns__name(), that was introduced in another later patch.
Also used PRIu64 and PRIx64 to fix the build in some enfironments wrt:
util/event.c:1129:39: error: format '%lx' expects argument of type 'long unsigned int', but argument 6 has type 'long long unsigned int' [-Werror=format=]
ret += fprintf(fp, "%u/%s: %lu/0x%lx%s", idx
^
Testing it:
# perf record --namespaces -a
^C[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 1.083 MB perf.data (423 samples) ]
#
# perf report -D
<SNIP>
3 2028902078892 0x115140 [0xa0]: PERF_RECORD_NAMESPACES 14783/14783 - nr_namespaces: 7
[0/net: 3/0xf0000081, 1/uts: 3/0xeffffffe, 2/ipc: 3/0xefffffff, 3/pid: 3/0xeffffffc,
4/user: 3/0xeffffffd, 5/mnt: 3/0xf0000000, 6/cgroup: 3/0xeffffffb]
0x1151e0 [0x30]: event: 9
.
. ... raw event: size 48 bytes
. 0000: 09 00 00 00 02 00 30 00 c4 71 82 68 0c 7f 00 00 ......0..q.h....
. 0010: a9 39 00 00 a9 39 00 00 94 28 fe 63 d8 01 00 00 .9...9...(.c....
. 0020: 03 00 00 00 00 00 00 00 ce c4 02 00 00 00 00 00 ................
<SNIP>
NAMESPACES events: 1
<SNIP>
#
Signed-off-by: Hari Bathini <hbathini@linux.vnet.ibm.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com>
Cc: Aravinda Prasad <aravinda@linux.vnet.ibm.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Eric Biederman <ebiederm@xmission.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sargun Dhillon <sargun@sargun.me>
Cc: Steven Rostedt <rostedt@goodmis.org>
Link: http://lkml.kernel.org/r/148891930386.25309.18412039920746995488.stgit@hbathini.in.ibm.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2017-03-08 03:41:43 +07:00
|
|
|
.namespaces = perf_event__process_namespaces,
|
2014-07-06 19:18:21 +07:00
|
|
|
.ordered_events = true,
|
2013-01-24 22:10:38 +07:00
|
|
|
},
|
|
|
|
.input_name = "perf.data",
|
2014-12-17 23:53:27 +07:00
|
|
|
/*
|
|
|
|
* default to both load an store sampling
|
|
|
|
*/
|
|
|
|
.operation = MEM_OPERATION_LOAD | MEM_OPERATION_STORE,
|
2013-01-24 22:10:38 +07:00
|
|
|
};
|
|
|
|
const struct option mem_options[] = {
|
2014-12-17 23:53:27 +07:00
|
|
|
OPT_CALLBACK('t', "type", &mem.operation,
|
2014-12-17 22:23:55 +07:00
|
|
|
"type", "memory operations(load,store) Default load,store",
|
|
|
|
parse_mem_ops),
|
2013-01-24 22:10:38 +07:00
|
|
|
OPT_BOOLEAN('D', "dump-raw-samples", &mem.dump_raw,
|
|
|
|
"dump raw samples in ASCII"),
|
|
|
|
OPT_BOOLEAN('U', "hide-unresolved", &mem.hide_unresolved,
|
|
|
|
"Only display entries resolved to a symbol"),
|
|
|
|
OPT_STRING('i', "input", &input_name, "file",
|
|
|
|
"input file name"),
|
|
|
|
OPT_STRING('C', "cpu", &mem.cpu_list, "cpu",
|
|
|
|
"list of cpus to profile"),
|
2015-03-20 09:57:52 +07:00
|
|
|
OPT_STRING_NOEMPTY('x', "field-separator", &symbol_conf.field_sep,
|
2013-01-24 22:10:38 +07:00
|
|
|
"separator",
|
|
|
|
"separator for columns, no spaces will be added"
|
|
|
|
" between columns '.' is reserved."),
|
perf mem: Support using -f to override perf.data file ownership
Enable perf mem to use perf.data when it is not owned by current user or
root.
Example:
# perf mem -t load record ls
# chown Yunlong.Song:Yunlong.Song perf.data
# ls -al perf.data
-rw------- 1 Yunlong.Song Yunlong.Song 16392 Apr 2 14:34 perf.data
# id
uid=0(root) gid=0(root) groups=0(root),64(pkcs11)
Before this patch:
# perf mem -D report
File perf.data not owned by current user or root (use -f to override)
# perf mem -D -f report
Error: unknown switch `f'
usage: perf mem [<options>] {record|report}
-t, --type <type> memory operations(load,store) Default load,store
-D, --dump-raw-samples
dump raw samples in ASCII
-U, --hide-unresolved
Only display entries resolved to a symbol
-i, --input <file> input file name
-C, --cpu <cpu> list of cpus to profile
-x, --field-separator <separator>
separator for columns, no spaces will be added
between columns '.' is reserved.
As shown above, the -f option does not work at all.
After this patch:
# perf mem -D report
File perf.data not owned by current user or root (use -f to override)
# perf mem -D -f report
# PID, TID, IP, ADDR, LOCAL WEIGHT, DSRC, SYMBOL
39095 39095 0xffffffff81127e40 0x016ffff887f45148338 8 0x68100142
/proc/kcore:perf_event_aux
39095 39095 0xffffffff8100a3fe 0xffff89007f8cb7d0 6 0x68100142
/proc/kcore:native_sched_clock
39095 39095 0xffffffff81309139 0xffff88bf44c9ded8 6 0x68100142
/proc/kcore:acpi_map_lookup
39095 39095 0xffffffff810f8c4c 0xffff89007f8ccd88 6 0x68100142
/proc/kcore:rcu_nmi_exit
39095 39095 0xffffffff81136346 0xffff88fea995dd50 6 0x68100142
/proc/kcore:unlock_page
39095 39095 0xffffffff812a64a2 0xffff88fea995dcc8 6 0x68100142
/proc/kcore:half_md4_transform
39095 39095 0x7f0cf877c7e9 0x25dfb94 6 0x68100142
/lib64/libc-2.19.so:__readdir64
39095 39095 0x7f0cf87575a3 0x7f0cf9163731 6 0x68100142
/lib64/libc-2.19.so:__strcoll_l
39095 39095 0xffffffff8116910e 0xffffea01c1bfbd50 23 0x68100242
/proc/kcore:page_remove_rmap
As shown above, the -f option really works now.
Signed-off-by: Yunlong Song <yunlong.song@huawei.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1427982439-27388-7-git-send-email-yunlong.song@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-02 20:47:15 +07:00
|
|
|
OPT_BOOLEAN('f', "force", &mem.force, "don't complain, do it"),
|
2017-08-30 00:11:10 +07:00
|
|
|
OPT_BOOLEAN('p', "phys-data", &mem.phys_addr, "Record/Report sample physical addresses"),
|
2013-01-24 22:10:38 +07:00
|
|
|
OPT_END()
|
|
|
|
};
|
2014-03-15 10:17:52 +07:00
|
|
|
const char *const mem_subcommands[] = { "record", "report", NULL };
|
|
|
|
const char *mem_usage[] = {
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-02-24 15:46:42 +07:00
|
|
|
if (perf_mem_events__init()) {
|
|
|
|
pr_err("failed: memory events not supported\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-15 10:17:52 +07:00
|
|
|
argc = parse_options_subcommand(argc, argv, mem_options, mem_subcommands,
|
|
|
|
mem_usage, PARSE_OPT_STOP_AT_NON_OPTION);
|
2013-01-24 22:10:38 +07:00
|
|
|
|
2014-12-17 23:53:27 +07:00
|
|
|
if (!argc || !(strncmp(argv[0], "rec", 3) || mem.operation))
|
2013-01-24 22:10:38 +07:00
|
|
|
usage_with_options(mem_usage, mem_options);
|
|
|
|
|
|
|
|
if (!mem.input_name || !strlen(mem.input_name)) {
|
|
|
|
if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
|
|
|
|
mem.input_name = "-";
|
|
|
|
else
|
|
|
|
mem.input_name = "perf.data";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strncmp(argv[0], "rec", 3))
|
2014-12-17 23:53:27 +07:00
|
|
|
return __cmd_record(argc, argv, &mem);
|
2013-01-24 22:10:38 +07:00
|
|
|
else if (!strncmp(argv[0], "rep", 3))
|
|
|
|
return report_events(argc, argv, &mem);
|
|
|
|
else
|
|
|
|
usage_with_options(mem_usage, mem_options);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|