Skip to content

Commit

Permalink
perf tools: Allow multiple threads or processes in record, stat, top
Browse files Browse the repository at this point in the history
Allow a user to collect events for multiple threads or processes
using a comma separated list.

e.g., collect data on a VM and its vhost thread:
  perf top -p 21483,21485
  perf stat -p 21483,21485 -ddd
  perf record -p 21483,21485

or monitoring vcpu threads
  perf top -t 21488,21489
  perf stat -t 21488,21489 -ddd
  perf record -t 21488,21489

Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: http://lkml.kernel.org/r/1328718772-16688-1-git-send-email-dsahern@gmail.com
Signed-off-by: David Ahern <dsahern@gmail.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
David Ahern authored and Arnaldo Carvalho de Melo committed Feb 14, 2012
1 parent eca1c3e commit b52956c
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 56 deletions.
4 changes: 2 additions & 2 deletions tools/perf/Documentation/perf-record.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ OPTIONS

-p::
--pid=::
Record events on existing process ID.
Record events on existing process ID (comma separated list).

-t::
--tid=::
Record events on existing thread ID.
Record events on existing thread ID (comma separated list).

-u::
--uid=::
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/Documentation/perf-stat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ OPTIONS
child tasks do not inherit counters
-p::
--pid=<pid>::
stat events on existing process id
stat events on existing process id (comma separated list)

-t::
--tid=<tid>::
stat events on existing thread id
stat events on existing thread id (comma separated list)


-a::
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/Documentation/perf-top.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ Default is to monitor all CPUS.

-p <pid>::
--pid=<pid>::
Profile events on existing Process ID.
Profile events on existing Process ID (comma separated list).

-t <tid>::
--tid=<tid>::
Profile events on existing thread ID.
Profile events on existing thread ID (comma separated list).

-u::
--uid=::
Expand Down
10 changes: 4 additions & 6 deletions tools/perf/builtin-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,6 @@ static const char * const record_usage[] = {
*/
static struct perf_record record = {
.opts = {
.target_pid = -1,
.target_tid = -1,
.mmap_pages = UINT_MAX,
.user_freq = UINT_MAX,
.user_interval = ULLONG_MAX,
Expand All @@ -670,9 +668,9 @@ const struct option record_options[] = {
parse_events_option),
OPT_CALLBACK(0, "filter", &record.evlist, "filter",
"event filter", parse_filter),
OPT_INTEGER('p', "pid", &record.opts.target_pid,
OPT_STRING('p', "pid", &record.opts.target_pid, "pid",
"record events on existing process id"),
OPT_INTEGER('t', "tid", &record.opts.target_tid,
OPT_STRING('t', "tid", &record.opts.target_tid, "tid",
"record events on existing thread id"),
OPT_INTEGER('r', "realtime", &record.realtime_prio,
"collect data with this RT SCHED_FIFO priority"),
Expand Down Expand Up @@ -739,7 +737,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)

argc = parse_options(argc, argv, record_options, record_usage,
PARSE_OPT_STOP_AT_NON_OPTION);
if (!argc && rec->opts.target_pid == -1 && rec->opts.target_tid == -1 &&
if (!argc && !rec->opts.target_pid && !rec->opts.target_tid &&
!rec->opts.system_wide && !rec->opts.cpu_list && !rec->uid_str)
usage_with_options(record_usage, record_options);

Expand Down Expand Up @@ -785,7 +783,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
if (rec->uid_str != NULL && rec->opts.uid == UINT_MAX - 1)
goto out_free_fd;

if (rec->opts.target_pid != -1)
if (rec->opts.target_pid)
rec->opts.target_tid = rec->opts.target_pid;

if (perf_evlist__create_maps(evsel_list, rec->opts.target_pid,
Expand Down
31 changes: 16 additions & 15 deletions tools/perf/builtin-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ static int run_count = 1;
static bool no_inherit = false;
static bool scale = true;
static bool no_aggr = false;
static pid_t target_pid = -1;
static pid_t target_tid = -1;
static const char *target_pid;
static const char *target_tid;
static pid_t child_pid = -1;
static bool null_run = false;
static int detailed_run = 0;
Expand Down Expand Up @@ -296,7 +296,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel,
if (system_wide)
return perf_evsel__open_per_cpu(evsel, evsel_list->cpus,
group, group_fd);
if (target_pid == -1 && target_tid == -1) {
if (!target_pid && !target_tid) {
attr->disabled = 1;
attr->enable_on_exec = 1;
}
Expand Down Expand Up @@ -446,7 +446,7 @@ static int run_perf_stat(int argc __used, const char **argv)
exit(-1);
}

if (target_tid == -1 && target_pid == -1 && !system_wide)
if (!target_tid && !target_pid && !system_wide)
evsel_list->threads->map[0] = child_pid;

/*
Expand Down Expand Up @@ -968,14 +968,14 @@ static void print_stat(int argc, const char **argv)
if (!csv_output) {
fprintf(output, "\n");
fprintf(output, " Performance counter stats for ");
if(target_pid == -1 && target_tid == -1) {
if (!target_pid && !target_tid) {
fprintf(output, "\'%s", argv[0]);
for (i = 1; i < argc; i++)
fprintf(output, " %s", argv[i]);
} else if (target_pid != -1)
fprintf(output, "process id \'%d", target_pid);
} else if (target_pid)
fprintf(output, "process id \'%s", target_pid);
else
fprintf(output, "thread id \'%d", target_tid);
fprintf(output, "thread id \'%s", target_tid);

fprintf(output, "\'");
if (run_count > 1)
Expand Down Expand Up @@ -1049,10 +1049,10 @@ static const struct option options[] = {
"event filter", parse_filter),
OPT_BOOLEAN('i', "no-inherit", &no_inherit,
"child tasks do not inherit counters"),
OPT_INTEGER('p', "pid", &target_pid,
"stat events on existing process id"),
OPT_INTEGER('t', "tid", &target_tid,
"stat events on existing thread id"),
OPT_STRING('p', "pid", &target_pid, "pid",
"stat events on existing process id"),
OPT_STRING('t', "tid", &target_tid, "tid",
"stat events on existing thread id"),
OPT_BOOLEAN('a', "all-cpus", &system_wide,
"system-wide collection from all CPUs"),
OPT_BOOLEAN('g', "group", &group,
Expand Down Expand Up @@ -1190,7 +1190,7 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
} else if (big_num_opt == 0) /* User passed --no-big-num */
big_num = false;

if (!argc && target_pid == -1 && target_tid == -1)
if (!argc && !target_pid && !target_tid)
usage_with_options(stat_usage, options);
if (run_count <= 0)
usage_with_options(stat_usage, options);
Expand All @@ -1206,10 +1206,11 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
if (add_default_attributes())
goto out;

if (target_pid != -1)
if (target_pid)
target_tid = target_pid;

evsel_list->threads = thread_map__new(target_pid, target_tid, UINT_MAX);
evsel_list->threads = thread_map__new_str(target_pid,
target_tid, UINT_MAX);
if (evsel_list->threads == NULL) {
pr_err("Problems finding threads of monitor\n");
usage_with_options(stat_usage, options);
Expand Down
2 changes: 0 additions & 2 deletions tools/perf/builtin-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,6 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
static int test__PERF_RECORD(void)
{
struct perf_record_opts opts = {
.target_pid = -1,
.target_tid = -1,
.no_delay = true,
.freq = 10,
.mmap_pages = 256,
Expand Down
12 changes: 5 additions & 7 deletions tools/perf/builtin-top.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ static int __cmd_top(struct perf_top *top)
if (ret)
goto out_delete;

if (top->target_tid != -1 || top->uid != UINT_MAX)
if (top->target_tid || top->uid != UINT_MAX)
perf_event__synthesize_thread_map(&top->tool, top->evlist->threads,
perf_event__process,
&top->session->host_machine);
Expand Down Expand Up @@ -1103,8 +1103,6 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
struct perf_top top = {
.count_filter = 5,
.delay_secs = 2,
.target_pid = -1,
.target_tid = -1,
.uid = UINT_MAX,
.freq = 1000, /* 1 KHz */
.sample_id_all_avail = true,
Expand All @@ -1118,9 +1116,9 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
parse_events_option),
OPT_INTEGER('c', "count", &top.default_interval,
"event period to sample"),
OPT_INTEGER('p', "pid", &top.target_pid,
OPT_STRING('p', "pid", &top.target_pid, "pid",
"profile events on existing process id"),
OPT_INTEGER('t', "tid", &top.target_tid,
OPT_STRING('t', "tid", &top.target_tid, "tid",
"profile events on existing thread id"),
OPT_BOOLEAN('a', "all-cpus", &top.system_wide,
"system-wide collection from all CPUs"),
Expand Down Expand Up @@ -1210,13 +1208,13 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
goto out_delete_evlist;

/* CPU and PID are mutually exclusive */
if (top.target_tid > 0 && top.cpu_list) {
if (top.target_tid && top.cpu_list) {
printf("WARNING: PID switch overriding CPU\n");
sleep(1);
top.cpu_list = NULL;
}

if (top.target_pid != -1)
if (top.target_pid)
top.target_tid = top.target_pid;

if (perf_evlist__create_maps(top.evlist, top.target_pid,
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ extern const char perf_version_string[];
void pthread__unblock_sigwinch(void);

struct perf_record_opts {
pid_t target_pid;
pid_t target_tid;
const char *target_pid;
const char *target_tid;
uid_t uid;
bool call_graph;
bool group;
Expand Down
10 changes: 5 additions & 5 deletions tools/perf/util/evlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,15 +593,15 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
return perf_evlist__mmap_per_cpu(evlist, prot, mask);
}

int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid,
pid_t target_tid, uid_t uid, const char *cpu_list)
int perf_evlist__create_maps(struct perf_evlist *evlist, const char *target_pid,
const char *target_tid, uid_t uid, const char *cpu_list)
{
evlist->threads = thread_map__new(target_pid, target_tid, uid);
evlist->threads = thread_map__new_str(target_pid, target_tid, uid);

if (evlist->threads == NULL)
return -1;

if (uid != UINT_MAX || (cpu_list == NULL && target_tid != -1))
if (uid != UINT_MAX || (cpu_list == NULL && target_tid))
evlist->cpus = cpu_map__dummy_new();
else
evlist->cpus = cpu_map__new(cpu_list);
Expand Down Expand Up @@ -820,7 +820,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist,
exit(-1);
}

if (!opts->system_wide && opts->target_tid == -1 && opts->target_pid == -1)
if (!opts->system_wide && !opts->target_tid && !opts->target_pid)
evlist->threads->map[0] = evlist->workload.pid;

close(child_ready_pipe[1]);
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/util/evlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ static inline void perf_evlist__set_maps(struct perf_evlist *evlist,
evlist->threads = threads;
}

int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid,
pid_t tid, uid_t uid, const char *cpu_list);
int perf_evlist__create_maps(struct perf_evlist *evlist, const char *target_pid,
const char *tid, uid_t uid, const char *cpu_list);
void perf_evlist__delete_maps(struct perf_evlist *evlist);
int perf_evlist__set_filters(struct perf_evlist *evlist);

Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/evsel.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts)
attr->mmap = track;
attr->comm = track;

if (opts->target_pid == -1 && opts->target_tid == -1 && !opts->system_wide) {
if (!opts->target_pid && !opts->target_tid && !opts->system_wide) {
attr->disabled = 1;
attr->enable_on_exec = 1;
}
Expand Down
2 changes: 2 additions & 0 deletions tools/perf/util/python-ext-sources
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ util/util.c
util/xyarray.c
util/cgroup.c
util/debugfs.c
util/strlist.c
../../lib/rbtree.c
Loading

0 comments on commit b52956c

Please sign in to comment.