Skip to content

Commit b52956c

Browse files
dsahernacmel
authored andcommitted
perf tools: Allow multiple threads or processes in record, stat, top
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 <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Paul Mackerras <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: David Ahern <[email protected]> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent eca1c3e commit b52956c

18 files changed

Lines changed: 185 additions & 56 deletions

tools/perf/Documentation/perf-record.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ OPTIONS
5252

5353
-p::
5454
--pid=::
55-
Record events on existing process ID.
55+
Record events on existing process ID (comma separated list).
5656

5757
-t::
5858
--tid=::
59-
Record events on existing thread ID.
59+
Record events on existing thread ID (comma separated list).
6060

6161
-u::
6262
--uid=::

tools/perf/Documentation/perf-stat.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ OPTIONS
3535
child tasks do not inherit counters
3636
-p::
3737
--pid=<pid>::
38-
stat events on existing process id
38+
stat events on existing process id (comma separated list)
3939

4040
-t::
4141
--tid=<tid>::
42-
stat events on existing thread id
42+
stat events on existing thread id (comma separated list)
4343

4444

4545
-a::

tools/perf/Documentation/perf-top.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ Default is to monitor all CPUS.
7272

7373
-p <pid>::
7474
--pid=<pid>::
75-
Profile events on existing Process ID.
75+
Profile events on existing Process ID (comma separated list).
7676

7777
-t <tid>::
7878
--tid=<tid>::
79-
Profile events on existing thread ID.
79+
Profile events on existing thread ID (comma separated list).
8080

8181
-u::
8282
--uid=::

tools/perf/builtin-record.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,6 @@ static const char * const record_usage[] = {
645645
*/
646646
static struct perf_record record = {
647647
.opts = {
648-
.target_pid = -1,
649-
.target_tid = -1,
650648
.mmap_pages = UINT_MAX,
651649
.user_freq = UINT_MAX,
652650
.user_interval = ULLONG_MAX,
@@ -670,9 +668,9 @@ const struct option record_options[] = {
670668
parse_events_option),
671669
OPT_CALLBACK(0, "filter", &record.evlist, "filter",
672670
"event filter", parse_filter),
673-
OPT_INTEGER('p', "pid", &record.opts.target_pid,
671+
OPT_STRING('p', "pid", &record.opts.target_pid, "pid",
674672
"record events on existing process id"),
675-
OPT_INTEGER('t', "tid", &record.opts.target_tid,
673+
OPT_STRING('t', "tid", &record.opts.target_tid, "tid",
676674
"record events on existing thread id"),
677675
OPT_INTEGER('r', "realtime", &record.realtime_prio,
678676
"collect data with this RT SCHED_FIFO priority"),
@@ -739,7 +737,7 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
739737

740738
argc = parse_options(argc, argv, record_options, record_usage,
741739
PARSE_OPT_STOP_AT_NON_OPTION);
742-
if (!argc && rec->opts.target_pid == -1 && rec->opts.target_tid == -1 &&
740+
if (!argc && !rec->opts.target_pid && !rec->opts.target_tid &&
743741
!rec->opts.system_wide && !rec->opts.cpu_list && !rec->uid_str)
744742
usage_with_options(record_usage, record_options);
745743

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

788-
if (rec->opts.target_pid != -1)
786+
if (rec->opts.target_pid)
789787
rec->opts.target_tid = rec->opts.target_pid;
790788

791789
if (perf_evlist__create_maps(evsel_list, rec->opts.target_pid,

tools/perf/builtin-stat.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ static int run_count = 1;
182182
static bool no_inherit = false;
183183
static bool scale = true;
184184
static bool no_aggr = false;
185-
static pid_t target_pid = -1;
186-
static pid_t target_tid = -1;
185+
static const char *target_pid;
186+
static const char *target_tid;
187187
static pid_t child_pid = -1;
188188
static bool null_run = false;
189189
static int detailed_run = 0;
@@ -296,7 +296,7 @@ static int create_perf_stat_counter(struct perf_evsel *evsel,
296296
if (system_wide)
297297
return perf_evsel__open_per_cpu(evsel, evsel_list->cpus,
298298
group, group_fd);
299-
if (target_pid == -1 && target_tid == -1) {
299+
if (!target_pid && !target_tid) {
300300
attr->disabled = 1;
301301
attr->enable_on_exec = 1;
302302
}
@@ -446,7 +446,7 @@ static int run_perf_stat(int argc __used, const char **argv)
446446
exit(-1);
447447
}
448448

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

452452
/*
@@ -968,14 +968,14 @@ static void print_stat(int argc, const char **argv)
968968
if (!csv_output) {
969969
fprintf(output, "\n");
970970
fprintf(output, " Performance counter stats for ");
971-
if(target_pid == -1 && target_tid == -1) {
971+
if (!target_pid && !target_tid) {
972972
fprintf(output, "\'%s", argv[0]);
973973
for (i = 1; i < argc; i++)
974974
fprintf(output, " %s", argv[i]);
975-
} else if (target_pid != -1)
976-
fprintf(output, "process id \'%d", target_pid);
975+
} else if (target_pid)
976+
fprintf(output, "process id \'%s", target_pid);
977977
else
978-
fprintf(output, "thread id \'%d", target_tid);
978+
fprintf(output, "thread id \'%s", target_tid);
979979

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

1193-
if (!argc && target_pid == -1 && target_tid == -1)
1193+
if (!argc && !target_pid && !target_tid)
11941194
usage_with_options(stat_usage, options);
11951195
if (run_count <= 0)
11961196
usage_with_options(stat_usage, options);
@@ -1206,10 +1206,11 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
12061206
if (add_default_attributes())
12071207
goto out;
12081208

1209-
if (target_pid != -1)
1209+
if (target_pid)
12101210
target_tid = target_pid;
12111211

1212-
evsel_list->threads = thread_map__new(target_pid, target_tid, UINT_MAX);
1212+
evsel_list->threads = thread_map__new_str(target_pid,
1213+
target_tid, UINT_MAX);
12131214
if (evsel_list->threads == NULL) {
12141215
pr_err("Problems finding threads of monitor\n");
12151216
usage_with_options(stat_usage, options);

tools/perf/builtin-test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,8 +1010,6 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t **maskp,
10101010
static int test__PERF_RECORD(void)
10111011
{
10121012
struct perf_record_opts opts = {
1013-
.target_pid = -1,
1014-
.target_tid = -1,
10151013
.no_delay = true,
10161014
.freq = 10,
10171015
.mmap_pages = 256,

tools/perf/builtin-top.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ static int __cmd_top(struct perf_top *top)
965965
if (ret)
966966
goto out_delete;
967967

968-
if (top->target_tid != -1 || top->uid != UINT_MAX)
968+
if (top->target_tid || top->uid != UINT_MAX)
969969
perf_event__synthesize_thread_map(&top->tool, top->evlist->threads,
970970
perf_event__process,
971971
&top->session->host_machine);
@@ -1103,8 +1103,6 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
11031103
struct perf_top top = {
11041104
.count_filter = 5,
11051105
.delay_secs = 2,
1106-
.target_pid = -1,
1107-
.target_tid = -1,
11081106
.uid = UINT_MAX,
11091107
.freq = 1000, /* 1 KHz */
11101108
.sample_id_all_avail = true,
@@ -1118,9 +1116,9 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
11181116
parse_events_option),
11191117
OPT_INTEGER('c', "count", &top.default_interval,
11201118
"event period to sample"),
1121-
OPT_INTEGER('p', "pid", &top.target_pid,
1119+
OPT_STRING('p', "pid", &top.target_pid, "pid",
11221120
"profile events on existing process id"),
1123-
OPT_INTEGER('t', "tid", &top.target_tid,
1121+
OPT_STRING('t', "tid", &top.target_tid, "tid",
11241122
"profile events on existing thread id"),
11251123
OPT_BOOLEAN('a', "all-cpus", &top.system_wide,
11261124
"system-wide collection from all CPUs"),
@@ -1210,13 +1208,13 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
12101208
goto out_delete_evlist;
12111209

12121210
/* CPU and PID are mutually exclusive */
1213-
if (top.target_tid > 0 && top.cpu_list) {
1211+
if (top.target_tid && top.cpu_list) {
12141212
printf("WARNING: PID switch overriding CPU\n");
12151213
sleep(1);
12161214
top.cpu_list = NULL;
12171215
}
12181216

1219-
if (top.target_pid != -1)
1217+
if (top.target_pid)
12201218
top.target_tid = top.target_pid;
12211219

12221220
if (perf_evlist__create_maps(top.evlist, top.target_pid,

tools/perf/perf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ extern const char perf_version_string[];
186186
void pthread__unblock_sigwinch(void);
187187

188188
struct perf_record_opts {
189-
pid_t target_pid;
190-
pid_t target_tid;
189+
const char *target_pid;
190+
const char *target_tid;
191191
uid_t uid;
192192
bool call_graph;
193193
bool group;

tools/perf/util/evlist.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,15 +593,15 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
593593
return perf_evlist__mmap_per_cpu(evlist, prot, mask);
594594
}
595595

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

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

604-
if (uid != UINT_MAX || (cpu_list == NULL && target_tid != -1))
604+
if (uid != UINT_MAX || (cpu_list == NULL && target_tid))
605605
evlist->cpus = cpu_map__dummy_new();
606606
else
607607
evlist->cpus = cpu_map__new(cpu_list);
@@ -820,7 +820,7 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist,
820820
exit(-1);
821821
}
822822

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

826826
close(child_ready_pipe[1]);

tools/perf/util/evlist.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ static inline void perf_evlist__set_maps(struct perf_evlist *evlist,
106106
evlist->threads = threads;
107107
}
108108

109-
int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid,
110-
pid_t tid, uid_t uid, const char *cpu_list);
109+
int perf_evlist__create_maps(struct perf_evlist *evlist, const char *target_pid,
110+
const char *tid, uid_t uid, const char *cpu_list);
111111
void perf_evlist__delete_maps(struct perf_evlist *evlist);
112112
int perf_evlist__set_filters(struct perf_evlist *evlist);
113113

0 commit comments

Comments
 (0)