Commit 716c69fe authored by Ingo Molnar's avatar Ingo Molnar

perf top: Fall back to cpu-clock-tick hrtimer sampling if no cycle counter available

On architectures/CPUs without PMU support but with perfcounters
enabled 'perf top' currently fails because it cannot create a
cycle based hw-perfcounter.

Fall back to the cpu-clock-tick sw-perfcounter in this case, which
is hrtimer based and will always work (as long as perfcounters
is enabled).

Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
LKML-Reference: <new-submission>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 743ee1f8
...@@ -527,17 +527,13 @@ static void mmap_read(void) ...@@ -527,17 +527,13 @@ static void mmap_read(void)
} }
} }
static int __cmd_top(void) int nr_poll;
int group_fd;
static void start_counter(int i, int counter)
{ {
struct perf_counter_attr *attr; struct perf_counter_attr *attr;
pthread_t thread;
int i, counter, group_fd, nr_poll = 0;
unsigned int cpu; unsigned int cpu;
int ret;
for (i = 0; i < nr_cpus; i++) {
group_fd = -1;
for (counter = 0; counter < nr_counters; counter++) {
cpu = profile_cpu; cpu = profile_cpu;
if (target_pid == -1 && profile_cpu == -1) if (target_pid == -1 && profile_cpu == -1)
...@@ -548,14 +544,30 @@ static int __cmd_top(void) ...@@ -548,14 +544,30 @@ static int __cmd_top(void)
attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
attr->freq = freq; attr->freq = freq;
try_again:
fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0); fd[i][counter] = sys_perf_counter_open(attr, target_pid, cpu, group_fd, 0);
if (fd[i][counter] < 0) { if (fd[i][counter] < 0) {
int err = errno; int err = errno;
error("syscall returned with %d (%s)\n", error("sys_perf_counter_open() syscall returned with %d (%s)\n",
fd[i][counter], strerror(err)); fd[i][counter], strerror(err));
if (err == EPERM) if (err == EPERM)
printf("Are you root?\n"); 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_CPU_CYCLES) {
warning(" ... trying to fall back to cpu-clock-ticks\n");
attr->type = PERF_TYPE_SOFTWARE;
attr->config = PERF_COUNT_CPU_CLOCK;
goto try_again;
}
exit(-1); exit(-1);
} }
assert(fd[i][counter] >= 0); assert(fd[i][counter] >= 0);
...@@ -578,7 +590,18 @@ static int __cmd_top(void) ...@@ -578,7 +590,18 @@ static int __cmd_top(void)
PROT_READ, MAP_SHARED, fd[i][counter], 0); PROT_READ, MAP_SHARED, fd[i][counter], 0);
if (mmap_array[i][counter].base == MAP_FAILED) if (mmap_array[i][counter].base == MAP_FAILED)
die("failed to mmap with %d (%s)\n", errno, strerror(errno)); die("failed to mmap with %d (%s)\n", errno, strerror(errno));
} }
static int __cmd_top(void)
{
pthread_t thread;
int i, counter;
int ret;
for (i = 0; i < nr_cpus; i++) {
group_fd = -1;
for (counter = 0; counter < nr_counters; counter++)
start_counter(i, counter);
} }
/* Wait for a minimal set of events before starting the snapshot */ /* Wait for a minimal set of events before starting the snapshot */
......
...@@ -9,29 +9,29 @@ static void report(const char *prefix, const char *err, va_list params) ...@@ -9,29 +9,29 @@ static void report(const char *prefix, const char *err, va_list params)
{ {
char msg[1024]; char msg[1024];
vsnprintf(msg, sizeof(msg), err, params); vsnprintf(msg, sizeof(msg), err, params);
fprintf(stderr, "%s%s\n", prefix, msg); fprintf(stderr, " %s%s\n", prefix, msg);
} }
static NORETURN void usage_builtin(const char *err) static NORETURN void usage_builtin(const char *err)
{ {
fprintf(stderr, "\n usage: %s\n", err); fprintf(stderr, "\n Usage: %s\n", err);
exit(129); exit(129);
} }
static NORETURN void die_builtin(const char *err, va_list params) static NORETURN void die_builtin(const char *err, va_list params)
{ {
report("fatal: ", err, params); report(" Fatal: ", err, params);
exit(128); exit(128);
} }
static void error_builtin(const char *err, va_list params) static void error_builtin(const char *err, va_list params)
{ {
report("error: ", err, params); report(" Error: ", err, params);
} }
static void warn_builtin(const char *warn, va_list params) static void warn_builtin(const char *warn, va_list params)
{ {
report("warning: ", warn, params); report(" Warning: ", warn, params);
} }
/* If we are in a dlopen()ed .so write to a global variable would segfault /* If we are in a dlopen()ed .so write to a global variable would segfault
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment