perf events: Change perf parameter --pid to process-wide collection instead of thread-wide
Parameter --pid (or -p) of perf currently means a thread-wide collection. For exmaple, if a process whose id is 8888 has 10 threads, 'perf top -p 8888' just collects the main thread statistics. That's misleading. Users are used to attach a whole process when debugging a process by gdb. To follow normal usage style, the patch change --pid to process-wide collection and add --tid (-t) to mean a thread-wide collection. Usage example is: # perf top -p 8888 # perf record -p 8888 -f sleep 10 # perf stat -p 8888 -f sleep 10 Above commands collect the statistics of all threads of process 8888. Signed-off-by: Zhang Yanmin <yanmin_zhang@linux.intel.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Avi Kivity <avi@redhat.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Sheng Yang <sheng@linux.intel.com> Cc: Joerg Roedel <joro@8bytes.org> Cc: Jes Sorensen <Jes.Sorensen@redhat.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Gleb Natapov <gleb@redhat.com> Cc: zhiteng.huang@intel.com Cc: Zachary Amsden <zamsden@redhat.com> LKML-Reference: <1268922965-14774-3-git-send-email-acme@infradead.org> Signed-off-by: Ingo Molnar <mingo@elte.hu>
This commit is contained in:

committed by
Ingo Molnar

parent
46be604b5b
commit
d6d901c23a
@@ -55,7 +55,7 @@
|
||||
#include <linux/unistd.h>
|
||||
#include <linux/types.h>
|
||||
|
||||
static int fd[MAX_NR_CPUS][MAX_COUNTERS];
|
||||
static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
|
||||
|
||||
static int system_wide = 0;
|
||||
|
||||
@@ -65,6 +65,9 @@ static int count_filter = 5;
|
||||
static int print_entries;
|
||||
|
||||
static int target_pid = -1;
|
||||
static int target_tid = -1;
|
||||
static pid_t *all_tids = NULL;
|
||||
static int thread_num = 0;
|
||||
static int inherit = 0;
|
||||
static int profile_cpu = -1;
|
||||
static int nr_cpus = 0;
|
||||
@@ -524,13 +527,15 @@ static void print_sym_table(void)
|
||||
|
||||
if (target_pid != -1)
|
||||
printf(" (target_pid: %d", target_pid);
|
||||
else if (target_tid != -1)
|
||||
printf(" (target_tid: %d", target_tid);
|
||||
else
|
||||
printf(" (all");
|
||||
|
||||
if (profile_cpu != -1)
|
||||
printf(", cpu: %d)\n", profile_cpu);
|
||||
else {
|
||||
if (target_pid != -1)
|
||||
if (target_tid != -1)
|
||||
printf(")\n");
|
||||
else
|
||||
printf(", %d CPUs)\n", nr_cpus);
|
||||
@@ -1129,16 +1134,21 @@ static void perf_session__mmap_read_counter(struct perf_session *self,
|
||||
md->prev = old;
|
||||
}
|
||||
|
||||
static struct pollfd event_array[MAX_NR_CPUS * MAX_COUNTERS];
|
||||
static struct mmap_data mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
|
||||
static struct pollfd *event_array;
|
||||
static struct mmap_data *mmap_array[MAX_NR_CPUS][MAX_COUNTERS];
|
||||
|
||||
static void perf_session__mmap_read(struct perf_session *self)
|
||||
{
|
||||
int i, counter;
|
||||
int i, counter, thread_index;
|
||||
|
||||
for (i = 0; i < nr_cpus; i++) {
|
||||
for (counter = 0; counter < nr_counters; counter++)
|
||||
perf_session__mmap_read_counter(self, &mmap_array[i][counter]);
|
||||
for (thread_index = 0;
|
||||
thread_index < thread_num;
|
||||
thread_index++) {
|
||||
perf_session__mmap_read_counter(self,
|
||||
&mmap_array[i][counter][thread_index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1149,9 +1159,10 @@ static void start_counter(int i, int counter)
|
||||
{
|
||||
struct perf_event_attr *attr;
|
||||
int cpu;
|
||||
int thread_index;
|
||||
|
||||
cpu = profile_cpu;
|
||||
if (target_pid == -1 && profile_cpu == -1)
|
||||
if (target_tid == -1 && profile_cpu == -1)
|
||||
cpu = cpumap[i];
|
||||
|
||||
attr = attrs + counter;
|
||||
@@ -1167,55 +1178,58 @@ static void start_counter(int i, int counter)
|
||||
attr->inherit = (cpu < 0) && inherit;
|
||||
attr->mmap = 1;
|
||||
|
||||
for (thread_index = 0; thread_index < thread_num; thread_index++) {
|
||||
try_again:
|
||||
fd[i][counter] = sys_perf_event_open(attr, target_pid, cpu, group_fd, 0);
|
||||
fd[i][counter][thread_index] = sys_perf_event_open(attr,
|
||||
all_tids[thread_index], cpu, group_fd, 0);
|
||||
|
||||
if (fd[i][counter] < 0) {
|
||||
int err = errno;
|
||||
if (fd[i][counter][thread_index] < 0) {
|
||||
int err = errno;
|
||||
|
||||
if (err == EPERM || err == EACCES)
|
||||
die("No permission - are you root?\n");
|
||||
/*
|
||||
* If it's cycles then fall back to hrtimer
|
||||
* based cpu-clock-tick sw counter, which
|
||||
* is always available even if no PMU support:
|
||||
*/
|
||||
if (attr->type == PERF_TYPE_HARDWARE
|
||||
&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
|
||||
if (err == EPERM || err == EACCES)
|
||||
die("No permission - are you root?\n");
|
||||
/*
|
||||
* If it's cycles then fall back to hrtimer
|
||||
* based cpu-clock-tick sw counter, which
|
||||
* is always available even if no PMU support:
|
||||
*/
|
||||
if (attr->type == PERF_TYPE_HARDWARE
|
||||
&& attr->config == PERF_COUNT_HW_CPU_CYCLES) {
|
||||
|
||||
if (verbose)
|
||||
warning(" ... trying to fall back to cpu-clock-ticks\n");
|
||||
if (verbose)
|
||||
warning(" ... trying to fall back to cpu-clock-ticks\n");
|
||||
|
||||
attr->type = PERF_TYPE_SOFTWARE;
|
||||
attr->config = PERF_COUNT_SW_CPU_CLOCK;
|
||||
goto try_again;
|
||||
attr->type = PERF_TYPE_SOFTWARE;
|
||||
attr->config = PERF_COUNT_SW_CPU_CLOCK;
|
||||
goto try_again;
|
||||
}
|
||||
printf("\n");
|
||||
error("perfcounter syscall returned with %d (%s)\n",
|
||||
fd[i][counter][thread_index], strerror(err));
|
||||
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
|
||||
exit(-1);
|
||||
}
|
||||
printf("\n");
|
||||
error("perfcounter syscall returned with %d (%s)\n",
|
||||
fd[i][counter], strerror(err));
|
||||
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
|
||||
exit(-1);
|
||||
assert(fd[i][counter][thread_index] >= 0);
|
||||
fcntl(fd[i][counter][thread_index], F_SETFL, O_NONBLOCK);
|
||||
|
||||
/*
|
||||
* First counter acts as the group leader:
|
||||
*/
|
||||
if (group && group_fd == -1)
|
||||
group_fd = fd[i][counter][thread_index];
|
||||
|
||||
event_array[nr_poll].fd = fd[i][counter][thread_index];
|
||||
event_array[nr_poll].events = POLLIN;
|
||||
nr_poll++;
|
||||
|
||||
mmap_array[i][counter][thread_index].counter = counter;
|
||||
mmap_array[i][counter][thread_index].prev = 0;
|
||||
mmap_array[i][counter][thread_index].mask = mmap_pages*page_size - 1;
|
||||
mmap_array[i][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size,
|
||||
PROT_READ, MAP_SHARED, fd[i][counter][thread_index], 0);
|
||||
if (mmap_array[i][counter][thread_index].base == MAP_FAILED)
|
||||
die("failed to mmap with %d (%s)\n", errno, strerror(errno));
|
||||
}
|
||||
assert(fd[i][counter] >= 0);
|
||||
fcntl(fd[i][counter], F_SETFL, O_NONBLOCK);
|
||||
|
||||
/*
|
||||
* First counter acts as the group leader:
|
||||
*/
|
||||
if (group && group_fd == -1)
|
||||
group_fd = fd[i][counter];
|
||||
|
||||
event_array[nr_poll].fd = fd[i][counter];
|
||||
event_array[nr_poll].events = POLLIN;
|
||||
nr_poll++;
|
||||
|
||||
mmap_array[i][counter].counter = counter;
|
||||
mmap_array[i][counter].prev = 0;
|
||||
mmap_array[i][counter].mask = mmap_pages*page_size - 1;
|
||||
mmap_array[i][counter].base = mmap(NULL, (mmap_pages+1)*page_size,
|
||||
PROT_READ, MAP_SHARED, fd[i][counter], 0);
|
||||
if (mmap_array[i][counter].base == MAP_FAILED)
|
||||
die("failed to mmap with %d (%s)\n", errno, strerror(errno));
|
||||
}
|
||||
|
||||
static int __cmd_top(void)
|
||||
@@ -1231,8 +1245,8 @@ static int __cmd_top(void)
|
||||
if (session == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
if (target_pid != -1)
|
||||
event__synthesize_thread(target_pid, event__process, session);
|
||||
if (target_tid != -1)
|
||||
event__synthesize_thread(target_tid, event__process, session);
|
||||
else
|
||||
event__synthesize_threads(event__process, session);
|
||||
|
||||
@@ -1243,7 +1257,7 @@ static int __cmd_top(void)
|
||||
}
|
||||
|
||||
/* Wait for a minimal set of events before starting the snapshot */
|
||||
poll(event_array, nr_poll, 100);
|
||||
poll(&event_array[0], nr_poll, 100);
|
||||
|
||||
perf_session__mmap_read(session);
|
||||
|
||||
@@ -1286,7 +1300,9 @@ static const struct option options[] = {
|
||||
OPT_INTEGER('c', "count", &default_interval,
|
||||
"event period to sample"),
|
||||
OPT_INTEGER('p', "pid", &target_pid,
|
||||
"profile events on existing pid"),
|
||||
"profile events on existing process id"),
|
||||
OPT_INTEGER('t', "tid", &target_tid,
|
||||
"profile events on existing thread id"),
|
||||
OPT_BOOLEAN('a', "all-cpus", &system_wide,
|
||||
"system-wide collection from all CPUs"),
|
||||
OPT_INTEGER('C', "CPU", &profile_cpu,
|
||||
@@ -1327,6 +1343,7 @@ static const struct option options[] = {
|
||||
int cmd_top(int argc, const char **argv, const char *prefix __used)
|
||||
{
|
||||
int counter;
|
||||
int i,j;
|
||||
|
||||
page_size = sysconf(_SC_PAGE_SIZE);
|
||||
|
||||
@@ -1334,8 +1351,39 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
|
||||
if (argc)
|
||||
usage_with_options(top_usage, options);
|
||||
|
||||
if (target_pid != -1) {
|
||||
target_tid = target_pid;
|
||||
thread_num = find_all_tid(target_pid, &all_tids);
|
||||
if (thread_num <= 0) {
|
||||
fprintf(stderr, "Can't find all threads of pid %d\n",
|
||||
target_pid);
|
||||
usage_with_options(top_usage, options);
|
||||
}
|
||||
} else {
|
||||
all_tids=malloc(sizeof(pid_t));
|
||||
if (!all_tids)
|
||||
return -ENOMEM;
|
||||
|
||||
all_tids[0] = target_tid;
|
||||
thread_num = 1;
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_NR_CPUS; i++) {
|
||||
for (j = 0; j < MAX_COUNTERS; j++) {
|
||||
fd[i][j] = malloc(sizeof(int)*thread_num);
|
||||
mmap_array[i][j] = malloc(
|
||||
sizeof(struct mmap_data)*thread_num);
|
||||
if (!fd[i][j] || !mmap_array[i][j])
|
||||
return -ENOMEM;
|
||||
}
|
||||
}
|
||||
event_array = malloc(
|
||||
sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num);
|
||||
if (!event_array)
|
||||
return -ENOMEM;
|
||||
|
||||
/* CPU and PID are mutually exclusive */
|
||||
if (target_pid != -1 && profile_cpu != -1) {
|
||||
if (target_tid > 0 && profile_cpu != -1) {
|
||||
printf("WARNING: PID switch overriding CPU\n");
|
||||
sleep(1);
|
||||
profile_cpu = -1;
|
||||
@@ -1376,7 +1424,7 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
|
||||
attrs[counter].sample_period = default_interval;
|
||||
}
|
||||
|
||||
if (target_pid != -1 || profile_cpu != -1)
|
||||
if (target_tid != -1 || profile_cpu != -1)
|
||||
nr_cpus = 1;
|
||||
else
|
||||
nr_cpus = read_cpu_map();
|
||||
|
Reference in New Issue
Block a user