Commit 92858553 authored by Ingo Molnar's avatar Ingo Molnar

Merge branch 'perf/test' of...

Merge branch 'perf/test' of git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux-2.6 into perf/core
parents cc222196 d854861c
...@@ -396,6 +396,7 @@ LIB_H += util/build-id.h ...@@ -396,6 +396,7 @@ LIB_H += util/build-id.h
LIB_H += util/debug.h LIB_H += util/debug.h
LIB_H += util/debugfs.h LIB_H += util/debugfs.h
LIB_H += util/event.h LIB_H += util/event.h
LIB_H += util/evsel.h
LIB_H += util/exec_cmd.h LIB_H += util/exec_cmd.h
LIB_H += util/types.h LIB_H += util/types.h
LIB_H += util/levenshtein.h LIB_H += util/levenshtein.h
...@@ -404,6 +405,7 @@ LIB_H += util/parse-options.h ...@@ -404,6 +405,7 @@ LIB_H += util/parse-options.h
LIB_H += util/parse-events.h LIB_H += util/parse-events.h
LIB_H += util/quote.h LIB_H += util/quote.h
LIB_H += util/util.h LIB_H += util/util.h
LIB_H += util/xyarray.h
LIB_H += util/header.h LIB_H += util/header.h
LIB_H += util/help.h LIB_H += util/help.h
LIB_H += util/session.h LIB_H += util/session.h
...@@ -433,6 +435,7 @@ LIB_OBJS += $(OUTPUT)util/ctype.o ...@@ -433,6 +435,7 @@ LIB_OBJS += $(OUTPUT)util/ctype.o
LIB_OBJS += $(OUTPUT)util/debugfs.o LIB_OBJS += $(OUTPUT)util/debugfs.o
LIB_OBJS += $(OUTPUT)util/environment.o LIB_OBJS += $(OUTPUT)util/environment.o
LIB_OBJS += $(OUTPUT)util/event.o LIB_OBJS += $(OUTPUT)util/event.o
LIB_OBJS += $(OUTPUT)util/evsel.o
LIB_OBJS += $(OUTPUT)util/exec_cmd.o LIB_OBJS += $(OUTPUT)util/exec_cmd.o
LIB_OBJS += $(OUTPUT)util/help.o LIB_OBJS += $(OUTPUT)util/help.o
LIB_OBJS += $(OUTPUT)util/levenshtein.o LIB_OBJS += $(OUTPUT)util/levenshtein.o
...@@ -470,6 +473,7 @@ LIB_OBJS += $(OUTPUT)util/sort.o ...@@ -470,6 +473,7 @@ LIB_OBJS += $(OUTPUT)util/sort.o
LIB_OBJS += $(OUTPUT)util/hist.o LIB_OBJS += $(OUTPUT)util/hist.o
LIB_OBJS += $(OUTPUT)util/probe-event.o LIB_OBJS += $(OUTPUT)util/probe-event.o
LIB_OBJS += $(OUTPUT)util/util.o LIB_OBJS += $(OUTPUT)util/util.o
LIB_OBJS += $(OUTPUT)util/xyarray.o
LIB_OBJS += $(OUTPUT)util/cpumap.o LIB_OBJS += $(OUTPUT)util/cpumap.o
BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "util/header.h" #include "util/header.h"
#include "util/event.h" #include "util/event.h"
#include "util/evsel.h"
#include "util/debug.h" #include "util/debug.h"
#include "util/session.h" #include "util/session.h"
#include "util/symbol.h" #include "util/symbol.h"
...@@ -27,18 +28,18 @@ ...@@ -27,18 +28,18 @@
#include <sched.h> #include <sched.h>
#include <sys/mman.h> #include <sys/mman.h>
#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
enum write_mode_t { enum write_mode_t {
WRITE_FORCE, WRITE_FORCE,
WRITE_APPEND WRITE_APPEND
}; };
static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
static u64 user_interval = ULLONG_MAX; static u64 user_interval = ULLONG_MAX;
static u64 default_interval = 0; static u64 default_interval = 0;
static u64 sample_type; static u64 sample_type;
static int nr_cpus = 0; static struct cpu_map *cpus;
static unsigned int page_size; static unsigned int page_size;
static unsigned int mmap_pages = 128; static unsigned int mmap_pages = 128;
static unsigned int user_freq = UINT_MAX; static unsigned int user_freq = UINT_MAX;
...@@ -53,8 +54,7 @@ static bool sample_id_all_avail = true; ...@@ -53,8 +54,7 @@ static bool sample_id_all_avail = true;
static bool system_wide = false; static bool system_wide = false;
static pid_t target_pid = -1; static pid_t target_pid = -1;
static pid_t target_tid = -1; static pid_t target_tid = -1;
static pid_t *all_tids = NULL; static struct thread_map *threads;
static int thread_num = 0;
static pid_t child_pid = -1; static pid_t child_pid = -1;
static bool no_inherit = false; static bool no_inherit = false;
static enum write_mode_t write_mode = WRITE_FORCE; static enum write_mode_t write_mode = WRITE_FORCE;
...@@ -81,7 +81,6 @@ static struct perf_session *session; ...@@ -81,7 +81,6 @@ static struct perf_session *session;
static const char *cpu_list; static const char *cpu_list;
struct mmap_data { struct mmap_data {
int counter;
void *base; void *base;
unsigned int mask; unsigned int mask;
unsigned int prev; unsigned int prev;
...@@ -229,12 +228,12 @@ static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int n ...@@ -229,12 +228,12 @@ static struct perf_header_attr *get_header_attr(struct perf_event_attr *a, int n
return h_attr; return h_attr;
} }
static void create_counter(int counter, int cpu) static void create_counter(struct perf_evsel *evsel, int cpu)
{ {
char *filter = filters[counter]; char *filter = evsel->filter;
struct perf_event_attr *attr = attrs + counter; struct perf_event_attr *attr = &evsel->attr;
struct perf_header_attr *h_attr; struct perf_header_attr *h_attr;
int track = !counter; /* only the first counter needs these */ int track = !evsel->idx; /* only the first counter needs these */
int thread_index; int thread_index;
int ret; int ret;
struct { struct {
...@@ -318,12 +317,11 @@ static void create_counter(int counter, int cpu) ...@@ -318,12 +317,11 @@ static void create_counter(int counter, int cpu)
retry_sample_id: retry_sample_id:
attr->sample_id_all = sample_id_all_avail ? 1 : 0; attr->sample_id_all = sample_id_all_avail ? 1 : 0;
for (thread_index = 0; thread_index < thread_num; thread_index++) { for (thread_index = 0; thread_index < threads->nr; thread_index++) {
try_again: try_again:
fd[nr_cpu][counter][thread_index] = sys_perf_event_open(attr, FD(evsel, nr_cpu, thread_index) = sys_perf_event_open(attr, threads->map[thread_index], cpu, group_fd, 0);
all_tids[thread_index], cpu, group_fd, 0);
if (fd[nr_cpu][counter][thread_index] < 0) { if (FD(evsel, nr_cpu, thread_index) < 0) {
int err = errno; int err = errno;
if (err == EPERM || err == EACCES) if (err == EPERM || err == EACCES)
...@@ -360,7 +358,7 @@ static void create_counter(int counter, int cpu) ...@@ -360,7 +358,7 @@ static void create_counter(int counter, int cpu)
} }
printf("\n"); printf("\n");
error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n", error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
fd[nr_cpu][counter][thread_index], strerror(err)); FD(evsel, nr_cpu, thread_index), strerror(err));
#if defined(__i386__) || defined(__x86_64__) #if defined(__i386__) || defined(__x86_64__)
if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP) if (attr->type == PERF_TYPE_HARDWARE && err == EOPNOTSUPP)
...@@ -374,7 +372,7 @@ static void create_counter(int counter, int cpu) ...@@ -374,7 +372,7 @@ static void create_counter(int counter, int cpu)
exit(-1); exit(-1);
} }
h_attr = get_header_attr(attr, counter); h_attr = get_header_attr(attr, evsel->idx);
if (h_attr == NULL) if (h_attr == NULL)
die("nomem\n"); die("nomem\n");
...@@ -385,7 +383,7 @@ static void create_counter(int counter, int cpu) ...@@ -385,7 +383,7 @@ static void create_counter(int counter, int cpu)
} }
} }
if (read(fd[nr_cpu][counter][thread_index], &read_data, sizeof(read_data)) == -1) { if (read(FD(evsel, nr_cpu, thread_index), &read_data, sizeof(read_data)) == -1) {
perror("Unable to read perf file descriptor"); perror("Unable to read perf file descriptor");
exit(-1); exit(-1);
} }
...@@ -395,43 +393,44 @@ static void create_counter(int counter, int cpu) ...@@ -395,43 +393,44 @@ static void create_counter(int counter, int cpu)
exit(-1); exit(-1);
} }
assert(fd[nr_cpu][counter][thread_index] >= 0); assert(FD(evsel, nr_cpu, thread_index) >= 0);
fcntl(fd[nr_cpu][counter][thread_index], F_SETFL, O_NONBLOCK); fcntl(FD(evsel, nr_cpu, thread_index), F_SETFL, O_NONBLOCK);
/* /*
* First counter acts as the group leader: * First counter acts as the group leader:
*/ */
if (group && group_fd == -1) if (group && group_fd == -1)
group_fd = fd[nr_cpu][counter][thread_index]; group_fd = FD(evsel, nr_cpu, thread_index);
if (counter || thread_index) { if (evsel->idx || thread_index) {
ret = ioctl(fd[nr_cpu][counter][thread_index], struct perf_evsel *first;
PERF_EVENT_IOC_SET_OUTPUT, first = list_entry(evsel_list.next, struct perf_evsel, node);
fd[nr_cpu][0][0]); ret = ioctl(FD(evsel, nr_cpu, thread_index),
PERF_EVENT_IOC_SET_OUTPUT,
FD(first, nr_cpu, 0));
if (ret) { if (ret) {
error("failed to set output: %d (%s)\n", errno, error("failed to set output: %d (%s)\n", errno,
strerror(errno)); strerror(errno));
exit(-1); exit(-1);
} }
} else { } else {
mmap_array[nr_cpu].counter = counter;
mmap_array[nr_cpu].prev = 0; mmap_array[nr_cpu].prev = 0;
mmap_array[nr_cpu].mask = mmap_pages*page_size - 1; mmap_array[nr_cpu].mask = mmap_pages*page_size - 1;
mmap_array[nr_cpu].base = mmap(NULL, (mmap_pages+1)*page_size, mmap_array[nr_cpu].base = mmap(NULL, (mmap_pages+1)*page_size,
PROT_READ|PROT_WRITE, MAP_SHARED, fd[nr_cpu][counter][thread_index], 0); PROT_READ | PROT_WRITE, MAP_SHARED, FD(evsel, nr_cpu, thread_index), 0);
if (mmap_array[nr_cpu].base == MAP_FAILED) { if (mmap_array[nr_cpu].base == MAP_FAILED) {
error("failed to mmap with %d (%s)\n", errno, strerror(errno)); error("failed to mmap with %d (%s)\n", errno, strerror(errno));
exit(-1); exit(-1);
} }
event_array[nr_poll].fd = fd[nr_cpu][counter][thread_index]; event_array[nr_poll].fd = FD(evsel, nr_cpu, thread_index);
event_array[nr_poll].events = POLLIN; event_array[nr_poll].events = POLLIN;
nr_poll++; nr_poll++;
} }
if (filter != NULL) { if (filter != NULL) {
ret = ioctl(fd[nr_cpu][counter][thread_index], ret = ioctl(FD(evsel, nr_cpu, thread_index),
PERF_EVENT_IOC_SET_FILTER, filter); PERF_EVENT_IOC_SET_FILTER, filter);
if (ret) { if (ret) {
error("failed to set filter with %d (%s)\n", errno, error("failed to set filter with %d (%s)\n", errno,
strerror(errno)); strerror(errno));
...@@ -446,11 +445,12 @@ static void create_counter(int counter, int cpu) ...@@ -446,11 +445,12 @@ static void create_counter(int counter, int cpu)
static void open_counters(int cpu) static void open_counters(int cpu)
{ {
int counter; struct perf_evsel *pos;
group_fd = -1; group_fd = -1;
for (counter = 0; counter < nr_counters; counter++)
create_counter(counter, cpu); list_for_each_entry(pos, &evsel_list, node)
create_counter(pos, cpu);
nr_cpu++; nr_cpu++;
} }
...@@ -537,7 +537,7 @@ static void mmap_read_all(void) ...@@ -537,7 +537,7 @@ static void mmap_read_all(void)
static int __cmd_record(int argc, const char **argv) static int __cmd_record(int argc, const char **argv)
{ {
int i, counter; int i;
struct stat st; struct stat st;
int flags; int flags;
int err; int err;
...@@ -604,7 +604,7 @@ static int __cmd_record(int argc, const char **argv) ...@@ -604,7 +604,7 @@ static int __cmd_record(int argc, const char **argv)
goto out_delete_session; goto out_delete_session;
} }
if (have_tracepoints(attrs, nr_counters)) if (have_tracepoints(&evsel_list))
perf_header__set_feat(&session->header, HEADER_TRACE_INFO); perf_header__set_feat(&session->header, HEADER_TRACE_INFO);
/* /*
...@@ -652,7 +652,7 @@ static int __cmd_record(int argc, const char **argv) ...@@ -652,7 +652,7 @@ static int __cmd_record(int argc, const char **argv)
} }
if (!system_wide && target_tid == -1 && target_pid == -1) if (!system_wide && target_tid == -1 && target_pid == -1)
all_tids[0] = child_pid; threads->map[0] = child_pid;
close(child_ready_pipe[1]); close(child_ready_pipe[1]);
close(go_pipe[0]); close(go_pipe[0]);
...@@ -666,17 +666,11 @@ static int __cmd_record(int argc, const char **argv) ...@@ -666,17 +666,11 @@ static int __cmd_record(int argc, const char **argv)
close(child_ready_pipe[0]); close(child_ready_pipe[0]);
} }
nr_cpus = read_cpu_map(cpu_list);
if (nr_cpus < 1) {
perror("failed to collect number of CPUs");
return -1;
}
if (!system_wide && no_inherit && !cpu_list) { if (!system_wide && no_inherit && !cpu_list) {
open_counters(-1); open_counters(-1);
} else { } else {
for (i = 0; i < nr_cpus; i++) for (i = 0; i < cpus->nr; i++)
open_counters(cpumap[i]); open_counters(cpus->map[i]);
} }
perf_session__set_sample_type(session, sample_type); perf_session__set_sample_type(session, sample_type);
...@@ -711,7 +705,7 @@ static int __cmd_record(int argc, const char **argv) ...@@ -711,7 +705,7 @@ static int __cmd_record(int argc, const char **argv)
return err; return err;
} }
if (have_tracepoints(attrs, nr_counters)) { if (have_tracepoints(&evsel_list)) {
/* /*
* FIXME err <= 0 here actually means that * FIXME err <= 0 here actually means that
* there were no tracepoints so its not really * there were no tracepoints so its not really
...@@ -720,8 +714,7 @@ static int __cmd_record(int argc, const char **argv) ...@@ -720,8 +714,7 @@ static int __cmd_record(int argc, const char **argv)
* return this more properly and also * return this more properly and also
* propagate errors that now are calling die() * propagate errors that now are calling die()
*/ */
err = event__synthesize_tracing_data(output, attrs, err = event__synthesize_tracing_data(output, &evsel_list,
nr_counters,
process_synthesized_event, process_synthesized_event,
session); session);
if (err <= 0) { if (err <= 0) {
...@@ -795,13 +788,13 @@ static int __cmd_record(int argc, const char **argv) ...@@ -795,13 +788,13 @@ static int __cmd_record(int argc, const char **argv)
if (done) { if (done) {
for (i = 0; i < nr_cpu; i++) { for (i = 0; i < nr_cpu; i++) {
for (counter = 0; struct perf_evsel *pos;
counter < nr_counters;
counter++) { list_for_each_entry(pos, &evsel_list, node) {
for (thread = 0; for (thread = 0;
thread < thread_num; thread < threads->nr;
thread++) thread++)
ioctl(fd[i][counter][thread], ioctl(FD(pos, i, thread),
PERF_EVENT_IOC_DISABLE); PERF_EVENT_IOC_DISABLE);
} }
} }
...@@ -887,7 +880,8 @@ const struct option record_options[] = { ...@@ -887,7 +880,8 @@ const struct option record_options[] = {
int cmd_record(int argc, const char **argv, const char *prefix __used) int cmd_record(int argc, const char **argv, const char *prefix __used)
{ {
int i, j, err = -ENOMEM; int err = -ENOMEM;
struct perf_evsel *pos;
argc = parse_options(argc, argv, record_options, record_usage, argc = parse_options(argc, argv, record_options, record_usage,
PARSE_OPT_STOP_AT_NON_OPTION); PARSE_OPT_STOP_AT_NON_OPTION);
...@@ -910,38 +904,32 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) ...@@ -910,38 +904,32 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
if (no_buildid_cache || no_buildid) if (no_buildid_cache || no_buildid)
disable_buildid_cache(); disable_buildid_cache();
if (!nr_counters) { if (list_empty(&evsel_list) && perf_evsel_list__create_default() < 0) {
nr_counters = 1; pr_err("Not enough memory for event selector list\n");
attrs[0].type = PERF_TYPE_HARDWARE; goto out_symbol_exit;
attrs[0].config = PERF_COUNT_HW_CPU_CYCLES;
} }
if (target_pid != -1) { if (target_pid != -1)
target_tid = target_pid; 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(record_usage, record_options);
}
} else {
all_tids=malloc(sizeof(pid_t));
if (!all_tids)
goto out_symbol_exit;
all_tids[0] = target_tid; threads = thread_map__new(target_pid, target_tid);
thread_num = 1; if (threads == NULL) {
pr_err("Problems finding threads of monitor\n");
usage_with_options(record_usage, record_options);
} }
for (i = 0; i < MAX_NR_CPUS; i++) { cpus = cpu_map__new(cpu_list);
for (j = 0; j < MAX_COUNTERS; j++) { if (cpus == NULL) {
fd[i][j] = malloc(sizeof(int)*thread_num); perror("failed to parse CPUs map");
if (!fd[i][j]) return -1;
goto out_free_fd; }
}
list_for_each_entry(pos, &evsel_list, node) {
if (perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
goto out_free_fd;
} }
event_array = malloc( event_array = malloc((sizeof(struct pollfd) * MAX_NR_CPUS *
sizeof(struct pollfd)*MAX_NR_CPUS*MAX_COUNTERS*thread_num); MAX_COUNTERS * threads->nr));
if (!event_array) if (!event_array)
goto out_free_fd; goto out_free_fd;
...@@ -968,12 +956,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __used) ...@@ -968,12 +956,8 @@ int cmd_record(int argc, const char **argv, const char *prefix __used)
out_free_event_array: out_free_event_array:
free(event_array); free(event_array);
out_free_fd: out_free_fd:
for (i = 0; i < MAX_NR_CPUS; i++) { thread_map__delete(threads);
for (j = 0; j < MAX_COUNTERS; j++) threads = NULL;
free(fd[i][j]);
}
free(all_tids);
all_tids = NULL;
out_symbol_exit: out_symbol_exit:
symbol__exit(); symbol__exit();
return err; return err;
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "util/parse-options.h" #include "util/parse-options.h"
#include "util/parse-events.h" #include "util/parse-events.h"
#include "util/event.h" #include "util/event.h"
#include "util/evsel.h"
#include "util/debug.h" #include "util/debug.h"
#include "util/header.h" #include "util/header.h"
#include "util/cpumap.h" #include "util/cpumap.h"
...@@ -71,7 +72,7 @@ static struct perf_event_attr default_attrs[] = { ...@@ -71,7 +72,7 @@ static struct perf_event_attr default_attrs[] = {
}; };
static bool system_wide = false; static bool system_wide = false;
static int nr_cpus = 0; static struct cpu_map *cpus;
static int run_idx = 0; static int run_idx = 0;
static int run_count = 1; static int run_count = 1;
...@@ -80,8 +81,7 @@ static bool scale = true; ...@@ -80,8 +81,7 @@ static bool scale = true;
static bool no_aggr = false; static bool no_aggr = false;
static pid_t target_pid = -1; static pid_t target_pid = -1;
static pid_t target_tid = -1; static pid_t target_tid = -1;
static pid_t *all_tids = NULL; static struct thread_map *threads;
static int thread_num = 0;
static pid_t child_pid = -1; static pid_t child_pid = -1;
static bool null_run = false; static bool null_run = false;
static bool big_num = true; static bool big_num = true;
...@@ -90,17 +90,6 @@ static const char *cpu_list; ...@@ -90,17 +90,6 @@ static const char *cpu_list;
static const char *csv_sep = NULL; static const char *csv_sep = NULL;
static bool csv_output = false; static bool csv_output = false;
static int *fd[MAX_NR_CPUS][MAX_COUNTERS];
static int event_scaled[MAX_COUNTERS];
static struct {
u64 val;
u64 ena;
u64 run;
} cpu_counts[MAX_NR_CPUS][MAX_COUNTERS];
static volatile int done = 0; static volatile int done = 0;
struct stats struct stats
...@@ -108,6 +97,22 @@ struct stats ...@@ -108,6 +97,22 @@ struct stats
double n, mean, M2; double n, mean, M2;
}; };
struct perf_stat {
struct stats res_stats[3];
};
static int perf_evsel__alloc_stat_priv(struct perf_evsel *evsel)
{
evsel->priv = zalloc(sizeof(struct perf_stat));
return evsel->priv == NULL ? -ENOMEM : 0;
}
static void perf_evsel__free_stat_priv(struct perf_evsel *evsel)
{
free(evsel->priv);
evsel->priv = NULL;
}
static void update_stats(struct stats *stats, u64 val) static void update_stats(struct stats *stats, u64 val)
{ {
double delta; double delta;
...@@ -147,75 +152,38 @@ static double stddev_stats(struct stats *stats) ...@@ -147,75 +152,38 @@ static double stddev_stats(struct stats *stats)
return sqrt(variance_mean); return sqrt(variance_mean);
} }
struct stats event_res_stats[MAX_COUNTERS][3];
struct stats runtime_nsecs_stats[MAX_NR_CPUS]; struct stats runtime_nsecs_stats[MAX_NR_CPUS];
struct stats runtime_cycles_stats[MAX_NR_CPUS]; struct stats runtime_cycles_stats[MAX_NR_CPUS];
struct stats runtime_branches_stats[MAX_NR_CPUS]; struct stats runtime_branches_stats[MAX_NR_CPUS];
struct stats walltime_nsecs_stats; struct stats walltime_nsecs_stats;
#define MATCH_EVENT(t, c, counter) \ static int create_perf_stat_counter(struct perf_evsel *evsel)
(attrs[counter].type == PERF_TYPE_##t && \
attrs[counter].config == PERF_COUNT_##c)
#define ERR_PERF_OPEN \
"counter %d, sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information."
static int create_perf_stat_counter(int counter, bool *perm_err)
{ {
struct perf_event_attr *attr = attrs + counter; struct perf_event_attr *attr = &evsel->attr;
int thread;
int ncreated = 0;
if (scale) if (scale)
attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED | attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
PERF_FORMAT_TOTAL_TIME_RUNNING; PERF_FORMAT_TOTAL_TIME_RUNNING;
if (system_wide) { if (system_wide)
int cpu; return perf_evsel__open_per_cpu(evsel, cpus);
for (cpu = 0; cpu < nr_cpus; cpu++) { attr->inherit = !no_inherit;
fd[cpu][counter][0] = sys_perf_event_open(attr, if (target_pid == -1 && target_tid == -1) {
-1, cpumap[cpu], -1, 0); attr->disabled = 1;
if (fd[cpu][counter][0] < 0) { attr->enable_on_exec = 1;
if (errno == EPERM || errno == EACCES)
*perm_err = true;
error(ERR_PERF_OPEN, counter,
fd[cpu][counter][0], strerror(errno));
} else {
++ncreated;
}
}
} else {
attr->inherit = !no_inherit;
if (target_pid == -1 && target_tid == -1) {
attr->disabled = 1;
attr->enable_on_exec = 1;
}
for (thread = 0; thread < thread_num; thread++) {
fd[0][counter][thread] = sys_perf_event_open(attr,
all_tids[thread], -1, -1, 0);
if (fd[0][counter][thread] < 0) {
if (errno == EPERM || errno == EACCES)
*perm_err = true;
error(ERR_PERF_OPEN, counter,
fd[0][counter][thread],
strerror(errno));
} else {
++ncreated;
}
}
} }
return ncreated; return perf_evsel__open_per_thread(evsel, threads);
} }
/* /*
* Does the counter have nsecs as a unit? * Does the counter have nsecs as a unit?
*/ */
static inline int nsec_counter(int counter) static inline int nsec_counter(struct perf_evsel *evsel)
{ {
if (MATCH_EVENT(SOFTWARE, SW_CPU_CLOCK, counter) || if (perf_evsel__match(evsel, SOFTWARE, SW_CPU_CLOCK) ||
MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
return 1; return 1;
return 0; return 0;
...@@ -225,54 +193,17 @@ static inline int nsec_counter(int counter) ...@@ -225,54 +193,17 @@ static inline int nsec_counter(int counter)
* Read out the results of a single counter: * Read out the results of a single counter:
* aggregate counts across CPUs in system-wide mode * aggregate counts across CPUs in system-wide mode
*/ */
static void read_counter_aggr(int counter) static int read_counter_aggr(struct perf_evsel *counter)
{ {
u64 count[3], single_count[3]; struct perf_stat *ps = counter->priv;
int cpu; u64 *count = counter->counts->aggr.values;
size_t res, nv; int i;
int scaled;
int i, thread;
count[0] = count[1] = count[2] = 0; if (__perf_evsel__read(counter, cpus->nr, threads->nr, scale) < 0)
return -1;
nv = scale ? 3 : 1;
for (cpu = 0; cpu < nr_cpus; cpu++) {
for (thread = 0; thread < thread_num; thread++) {
if (fd[cpu][counter][thread] < 0)
continue;
res = read(fd[cpu][counter][thread],
single_count, nv * sizeof(u64));
assert(res == nv * sizeof(u64));
close(fd[cpu][counter][thread]);
fd[cpu][counter][thread] = -1;
count[0] += single_count[0];
if (scale) {
count[1] += single_count[1];
count[2] += single_count[2];
}
}
}
scaled = 0;
if (scale) {
if (count[2] == 0) {
event_scaled[counter] = -1;
count[0] = 0;
return;
}
if (count[2] < count[1]) {
event_scaled[counter] = 1;
count[0] = (unsigned long long)
((double)count[0] * count[1] / count[2] + 0.5);
}
}
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
update_stats(&event_res_stats[counter][i], count[i]); update_stats(&ps->res_stats[i], count[i]);
if (verbose) { if (verbose) {
fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter), fprintf(stderr, "%s: %Ld %Ld %Ld\n", event_name(counter),
...@@ -282,74 +213,51 @@ static void read_counter_aggr(int counter) ...@@ -282,74 +213,51 @@ static void read_counter_aggr(int counter)
/* /*
* Save the full runtime - to allow normalization during printout: * Save the full runtime - to allow normalization during printout:
*/ */
if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
update_stats(&runtime_nsecs_stats[0], count[0]); update_stats(&runtime_nsecs_stats[0], count[0]);
if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter)) if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
update_stats(&runtime_cycles_stats[0], count[0]); update_stats(&runtime_cycles_stats[0], count[0]);
if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter)) if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
update_stats(&runtime_branches_stats[0], count[0]); update_stats(&runtime_branches_stats[0], count[0]);
return 0;
} }
/* /*
* Read out the results of a single counter: * Read out the results of a single counter:
* do not aggregate counts across CPUs in system-wide mode * do not aggregate counts across CPUs in system-wide mode
*/ */
static void read_counter(int counter) static int read_counter(struct perf_evsel *counter)
{ {
u64 count[3]; u64 *count;
int cpu; int cpu;
size_t res, nv;
count[0] = count[1] = count[2] = 0;
nv = scale ? 3 : 1;
for (cpu = 0; cpu < nr_cpus; cpu++) {
if (fd[cpu][counter][0] < 0)
continue;
res = read(fd[cpu][counter][0], count, nv * sizeof(u64));
assert(res == nv * sizeof(u64));
close(fd[cpu][counter][0]); for (cpu = 0; cpu < cpus->nr; cpu++) {
fd[cpu][counter][0] = -1; if (__perf_evsel__read_on_cpu(counter, cpu, 0, scale) < 0)
return -1;
if (scale) { count = counter->counts->cpu[cpu].values;
if (count[2] == 0) {
count[0] = 0;
} else if (count[2] < count[1]) {
count[0] = (unsigned long long)
((double)count[0] * count[1] / count[2] + 0.5);
}
}
cpu_counts[cpu][counter].val = count[0]; /* scaled count */
cpu_counts[cpu][counter].ena = count[1];
cpu_counts[cpu][counter].run = count[2];
if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) if (perf_evsel__match(counter, SOFTWARE, SW_TASK_CLOCK))
update_stats(&runtime_nsecs_stats[cpu], count[0]); update_stats(&runtime_nsecs_stats[cpu], count[0]);
if (MATCH_EVENT(HARDWARE, HW_CPU_CYCLES, counter)) if (perf_evsel__match(counter, HARDWARE, HW_CPU_CYCLES))
update_stats(&runtime_cycles_stats[cpu], count[0]); update_stats(&runtime_cycles_stats[cpu], count[0]);
if (MATCH_EVENT(HARDWARE, HW_BRANCH_INSTRUCTIONS, counter)) if (perf_evsel__match(counter, HARDWARE, HW_BRANCH_INSTRUCTIONS))
update_stats(&runtime_branches_stats[cpu], count[0]); update_stats(&runtime_branches_stats[cpu], count[0]);
} }
return 0;
} }
static int run_perf_stat(int argc __used, const char **argv) static int run_perf_stat(int argc __used, const char **argv)
{ {
unsigned long long t0, t1; unsigned long long t0, t1;
struct perf_evsel *counter;
int status = 0; int status = 0;
int counter, ncreated = 0;
int child_ready_pipe[2], go_pipe[2]; int child_ready_pipe[2], go_pipe[2];
bool perm_err = false;
const bool forks = (argc > 0); const bool forks = (argc > 0);
char buf; char buf;
if (!system_wide)
nr_cpus = 1;
if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) { if (forks && (pipe(child_ready_pipe) < 0 || pipe(go_pipe) < 0)) {
perror("failed to create pipes"); perror("failed to create pipes");
exit(1); exit(1);
...@@ -389,7 +297,7 @@ static int run_perf_stat(int argc __used, const char **argv) ...@@ -389,7 +297,7 @@ static int run_perf_stat(int argc __used, const char **argv)
} }
if (target_tid == -1 && target_pid == -1 && !system_wide) if (target_tid == -1 && target_pid == -1 && !system_wide)
all_tids[0] = child_pid; threads->map[0] = child_pid;
/* /*
* Wait for the child to be ready to exec. * Wait for the child to be ready to exec.
...@@ -401,19 +309,23 @@ static int run_perf_stat(int argc __used, const char **argv) ...@@ -401,19 +309,23 @@ static int run_perf_stat(int argc __used, const char **argv)
close(child_ready_pipe[0]); close(child_ready_pipe[0]);
} }
for (counter = 0; counter < nr_counters; counter++) list_for_each_entry(counter, &evsel_list, node) {
ncreated += create_perf_stat_counter(counter, &perm_err); if (create_perf_stat_counter(counter) < 0) {
if (errno == -EPERM || errno == -EACCES) {
if (ncreated < nr_counters) { error("You may not have permission to collect %sstats.\n"
if (perm_err) "\t Consider tweaking"
error("You may not have permission to collect %sstats.\n" " /proc/sys/kernel/perf_event_paranoid or running as root.",
"\t Consider tweaking" system_wide ? "system-wide " : "");
" /proc/sys/kernel/perf_event_paranoid or running as root.", } else {
system_wide ? "system-wide " : ""); error("open_counter returned with %d (%s). "
die("Not all events could be opened.\n"); "/bin/dmesg may provide additional information.\n",
if (child_pid != -1) errno, strerror(errno));
kill(child_pid, SIGTERM); }
return -1; if (child_pid != -1)
kill(child_pid, SIGTERM);
die("Not all events could be opened.\n");
return -1;
}
} }
/* /*
...@@ -433,25 +345,33 @@ static int run_perf_stat(int argc __used, const char **argv) ...@@ -433,25 +345,33 @@ static int run_perf_stat(int argc __used, const char **argv)
update_stats(&walltime_nsecs_stats, t1 - t0); update_stats(&walltime_nsecs_stats, t1 - t0);
if (no_aggr) { if (no_aggr) {
for (counter = 0; counter < nr_counters; counter++) list_for_each_entry(counter, &evsel_list, node) {
read_counter(counter); read_counter(counter);
perf_evsel__close_fd(counter, cpus->nr, 1);
}
} else { } else {
for (counter = 0; counter < nr_counters; counter++) list_for_each_entry(counter, &evsel_list, node) {
read_counter_aggr(counter); read_counter_aggr(counter);
perf_evsel__close_fd(counter, cpus->nr, threads->nr);
}
} }
return WEXITSTATUS(status); return WEXITSTATUS(status);
} }
static void print_noise(int counter, double avg) static void print_noise(struct perf_evsel *evsel, double avg)
{ {
struct perf_stat *ps;
if (run_count == 1) if (run_count == 1)
return; return;
ps = evsel->priv;
fprintf(stderr, " ( +- %7.3f%% )", fprintf(stderr, " ( +- %7.3f%% )",
100 * stddev_stats(&event_res_stats[counter][0]) / avg); 100 * stddev_stats(&ps->res_stats[0]) / avg);
} }
static void nsec_printout(int cpu, int counter, double avg) static void nsec_printout(int cpu, struct perf_evsel *evsel, double avg)
{ {
double msecs = avg / 1e6; double msecs = avg / 1e6;
char cpustr[16] = { '\0', }; char cpustr[16] = { '\0', };
...@@ -460,20 +380,19 @@ static void nsec_printout(int cpu, int counter, double avg) ...@@ -460,20 +380,19 @@ static void nsec_printout(int cpu, int counter, double avg)
if (no_aggr) if (no_aggr)
sprintf(cpustr, "CPU%*d%s", sprintf(cpustr, "CPU%*d%s",
csv_output ? 0 : -4, csv_output ? 0 : -4,
cpumap[cpu], csv_sep); cpus->map[cpu], csv_sep);
fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(counter)); fprintf(stderr, fmt, cpustr, msecs, csv_sep, event_name(evsel));
if (csv_output) if (csv_output)
return; return;
if (MATCH_EVENT(SOFTWARE, SW_TASK_CLOCK, counter)) { if (perf_evsel__match(evsel, SOFTWARE, SW_TASK_CLOCK))
fprintf(stderr, " # %10.3f CPUs ", fprintf(stderr, " # %10.3f CPUs ",
avg / avg_stats(&walltime_nsecs_stats)); avg / avg_stats(&walltime_nsecs_stats));
}
} }
static void abs_printout(int cpu, int counter, double avg) static void abs_printout(int cpu, struct perf_evsel *evsel, double avg)
{ {
double total, ratio = 0.0; double total, ratio = 0.0;
char cpustr[16] = { '\0', }; char cpustr[16] = { '\0', };
...@@ -489,23 +408,23 @@ static void abs_printout(int cpu, int counter, double avg) ...@@ -489,23 +408,23 @@ static void abs_printout(int cpu, int counter, double avg)
if (no_aggr) if (no_aggr)
sprintf(cpustr, "CPU%*d%s", sprintf(cpustr, "CPU%*d%s",
csv_output ? 0 : -4, csv_output ? 0 : -4,
cpumap[cpu], csv_sep); cpus->map[cpu], csv_sep);
else else
cpu = 0; cpu = 0;
fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(counter)); fprintf(stderr, fmt, cpustr, avg, csv_sep, event_name(evsel));
if (csv_output) if (csv_output)
return; return;
if (MATCH_EVENT(HARDWARE, HW_INSTRUCTIONS, counter)) { if (perf_evsel__match(evsel, HARDWARE, HW_INSTRUCTIONS)) {
total = avg_stats(&runtime_cycles_stats[cpu]); total = avg_stats(&runtime_cycles_stats[cpu]);
if (total) if (total)
ratio = avg / total; ratio = avg / total;
fprintf(stderr, " # %10.3f IPC ", ratio); fprintf(stderr, " # %10.3f IPC ", ratio);
} else if (MATCH_EVENT(HARDWARE, HW_BRANCH_MISSES, counter) && } else if (perf_evsel__match(evsel, HARDWARE, HW_BRANCH_MISSES) &&
runtime_branches_stats[cpu].n != 0) { runtime_branches_stats[cpu].n != 0) {
total = avg_stats(&runtime_branches_stats[cpu]); total = avg_stats(&runtime_branches_stats[cpu]);
...@@ -528,10 +447,11 @@ static void abs_printout(int cpu, int counter, double avg) ...@@ -528,10 +447,11 @@ static void abs_printout(int cpu, int counter, double avg)
* Print out the results of a single counter: * Print out the results of a single counter:
* aggregated counts in system-wide mode * aggregated counts in system-wide mode
*/ */
static void print_counter_aggr(int counter) static void print_counter_aggr(struct perf_evsel *counter)
{ {
double avg = avg_stats(&event_res_stats[counter][0]); struct perf_stat *ps = counter->priv;
int scaled = event_scaled[counter]; double avg = avg_stats(&ps->res_stats[0]);
int scaled = counter->counts->scaled;
if (scaled == -1) { if (scaled == -1) {
fprintf(stderr, "%*s%s%-24s\n", fprintf(stderr, "%*s%s%-24s\n",
...@@ -555,8 +475,8 @@ static void print_counter_aggr(int counter) ...@@ -555,8 +475,8 @@ static void print_counter_aggr(int counter)
if (scaled) { if (scaled) {
double avg_enabled, avg_running; double avg_enabled, avg_running;
avg_enabled = avg_stats(&event_res_stats[counter][1]); avg_enabled = avg_stats(&ps->res_stats[1]);
avg_running = avg_stats(&event_res_stats[counter][2]); avg_running = avg_stats(&ps->res_stats[2]);
fprintf(stderr, " (scaled from %.2f%%)", fprintf(stderr, " (scaled from %.2f%%)",
100 * avg_running / avg_enabled); 100 * avg_running / avg_enabled);
...@@ -569,19 +489,19 @@ static void print_counter_aggr(int counter) ...@@ -569,19 +489,19 @@ static void print_counter_aggr(int counter)
* Print out the results of a single counter: * Print out the results of a single counter:
* does not use aggregated count in system-wide * does not use aggregated count in system-wide
*/ */
static void print_counter(int counter) static void print_counter(struct perf_evsel *counter)
{ {
u64 ena, run, val; u64 ena, run, val;
int cpu; int cpu;
for (cpu = 0; cpu < nr_cpus; cpu++) { for (cpu = 0; cpu < cpus->nr; cpu++) {
val = cpu_counts[cpu][counter].val; val = counter->counts->cpu[cpu].val;
ena = cpu_counts[cpu][counter].ena; ena = counter->counts->cpu[cpu].ena;
run = cpu_counts[cpu][counter].run; run = counter->counts->cpu[cpu].run;
if (run == 0 || ena == 0) { if (run == 0 || ena == 0) {
fprintf(stderr, "CPU%*d%s%*s%s%-24s", fprintf(stderr, "CPU%*d%s%*s%s%-24s",
csv_output ? 0 : -4, csv_output ? 0 : -4,
cpumap[cpu], csv_sep, cpus->map[cpu], csv_sep,
csv_output ? 0 : 18, csv_output ? 0 : 18,
"<not counted>", csv_sep, "<not counted>", csv_sep,
event_name(counter)); event_name(counter));
...@@ -609,7 +529,8 @@ static void print_counter(int counter) ...@@ -609,7 +529,8 @@ static void print_counter(int counter)
static void print_stat(int argc, const char **argv) static void print_stat(int argc, const char **argv)
{ {
int i, counter; struct perf_evsel *counter;
int i;
fflush(stdout); fflush(stdout);
...@@ -632,10 +553,10 @@ static void print_stat(int argc, const char **argv) ...@@ -632,10 +553,10 @@ static void print_stat(int argc, const char **argv)
} }
if (no_aggr) { if (no_aggr) {
for (counter = 0; counter < nr_counters; counter++) list_for_each_entry(counter, &evsel_list, node)
print_counter(counter); print_counter(counter);
} else { } else {
for (counter = 0; counter < nr_counters; counter++) list_for_each_entry(counter, &evsel_list, node)
print_counter_aggr(counter); print_counter_aggr(counter);
} }
...@@ -720,8 +641,8 @@ static const struct option options[] = { ...@@ -720,8 +641,8 @@ static const struct option options[] = {
int cmd_stat(int argc, const char **argv, const char *prefix __used) int cmd_stat(int argc, const char **argv, const char *prefix __used)
{ {
int status; struct perf_evsel *pos;
int i,j; int status = -ENOMEM;
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
...@@ -757,41 +678,45 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) ...@@ -757,41 +678,45 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
/* Set attrs and nr_counters if no event is selected and !null_run */ /* Set attrs and nr_counters if no event is selected and !null_run */
if (!null_run && !nr_counters) { if (!null_run && !nr_counters) {
memcpy(attrs, default_attrs, sizeof(default_attrs)); size_t c;
nr_counters = ARRAY_SIZE(default_attrs); nr_counters = ARRAY_SIZE(default_attrs);
for (c = 0; c < ARRAY_SIZE(default_attrs); ++c) {
pos = perf_evsel__new(default_attrs[c].type,
default_attrs[c].config,
nr_counters);
if (pos == NULL)
goto out;
list_add(&pos->node, &evsel_list);
}
} }
if (system_wide) if (target_pid != -1)
nr_cpus = read_cpu_map(cpu_list); target_tid = target_pid;
else
nr_cpus = 1;
if (nr_cpus < 1) threads = thread_map__new(target_pid, target_tid);
if (threads == NULL) {
pr_err("Problems finding threads of monitor\n");
usage_with_options(stat_usage, options); usage_with_options(stat_usage, options);
}
if (target_pid != -1) { if (system_wide)
target_tid = target_pid; cpus = cpu_map__new(cpu_list);
thread_num = find_all_tid(target_pid, &all_tids); else
if (thread_num <= 0) { cpus = cpu_map__dummy_new();
fprintf(stderr, "Can't find all threads of pid %d\n",
target_pid);
usage_with_options(stat_usage, options);
}
} else {
all_tids=malloc(sizeof(pid_t));
if (!all_tids)
return -ENOMEM;
all_tids[0] = target_tid; if (cpus == NULL) {
thread_num = 1; perror("failed to parse CPUs map");
usage_with_options(stat_usage, options);
return -1;
} }
for (i = 0; i < MAX_NR_CPUS; i++) { list_for_each_entry(pos, &evsel_list, node) {
for (j = 0; j < MAX_COUNTERS; j++) { if (perf_evsel__alloc_stat_priv(pos) < 0 ||
fd[i][j] = malloc(sizeof(int)*thread_num); perf_evsel__alloc_counts(pos, cpus->nr) < 0 ||
if (!fd[i][j]) perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
return -ENOMEM; goto out_free_fd;
}
} }
/* /*
...@@ -814,6 +739,11 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used) ...@@ -814,6 +739,11 @@ int cmd_stat(int argc, const char **argv, const char *prefix __used)
if (status != -1) if (status != -1)
print_stat(argc, argv); print_stat(argc, argv);
out_free_fd:
list_for_each_entry(pos, &evsel_list, node)
perf_evsel__free_stat_priv(pos);
out:
thread_map__delete(threads);
threads = NULL;
return status; return status;
} }
...@@ -234,6 +234,85 @@ static int test__vmlinux_matches_kallsyms(void) ...@@ -234,6 +234,85 @@ static int test__vmlinux_matches_kallsyms(void)
return err; return err;
} }
#include "util/evsel.h"
#include <sys/types.h>
static int trace_event__id(const char *event_name)
{
char *filename;
int err = -1, fd;
if (asprintf(&filename,
"/sys/kernel/debug/tracing/events/syscalls/%s/id",
event_name) < 0)
return -1;
fd = open(filename, O_RDONLY);
if (fd >= 0) {
char id[16];
if (read(fd, id, sizeof(id)) > 0)
err = atoi(id);
close(fd);
}
free(filename);
return err;
}
static int test__open_syscall_event(void)
{
int err = -1, fd;
struct thread_map *threads;
struct perf_evsel *evsel;
unsigned int nr_open_calls = 111, i;
int id = trace_event__id("sys_enter_open");
if (id < 0) {
pr_debug("trace_event__id(\"sys_enter_open\") ");
return -1;
}
threads = thread_map__new(-1, getpid());
if (threads == NULL) {
pr_debug("thread_map__new ");
return -1;
}
evsel = perf_evsel__new(PERF_TYPE_TRACEPOINT, id, 0);
if (evsel == NULL) {
pr_debug("perf_evsel__new ");
goto out_thread_map_delete;
}
if (perf_evsel__open_per_thread(evsel, threads) < 0) {
pr_debug("perf_evsel__open_per_thread ");
goto out_evsel_delete;
}
for (i = 0; i < nr_open_calls; ++i) {
fd = open("/etc/passwd", O_RDONLY);
close(fd);
}
if (perf_evsel__read_on_cpu(evsel, 0, 0) < 0) {
pr_debug("perf_evsel__open_read_on_cpu ");
goto out_close_fd;
}
if (evsel->counts->cpu[0].val != nr_open_calls)
pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls, got %Ld ",
nr_open_calls, evsel->counts->cpu[0].val);
err = 0;
out_close_fd:
perf_evsel__close_fd(evsel, 1, threads->nr);
out_evsel_delete:
perf_evsel__delete(evsel);
out_thread_map_delete:
thread_map__delete(threads);
return err;
}
static struct test { static struct test {
const char *desc; const char *desc;
int (*func)(void); int (*func)(void);
...@@ -242,6 +321,10 @@ static struct test { ...@@ -242,6 +321,10 @@ static struct test {
.desc = "vmlinux symtab matches kallsyms", .desc = "vmlinux symtab matches kallsyms",
.func = test__vmlinux_matches_kallsyms, .func = test__vmlinux_matches_kallsyms,
}, },
{
.desc = "detect open syscall event",
.func = test__open_syscall_event,
},
{ {
.func = NULL, .func = NULL,
}, },
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "perf.h" #include "perf.h"
#include "util/color.h" #include "util/color.h"
#include "util/evsel.h"
#include "util/session.h" #include "util/session.h"
#include "util/symbol.h" #include "util/symbol.h"
#include "util/thread.h" #include "util/thread.h"
...@@ -29,6 +30,7 @@ ...@@ -29,6 +30,7 @@
#include "util/parse-options.h" #include "util/parse-options.h"
#include "util/parse-events.h" #include "util/parse-events.h"
#include "util/cpumap.h" #include "util/cpumap.h"
#include "util/xyarray.h"
#include "util/debug.h" #include "util/debug.h"
...@@ -55,7 +57,7 @@ ...@@ -55,7 +57,7 @@
#include <linux/unistd.h> #include <linux/unistd.h>
#include <linux/types.h> #include <linux/types.h>
static int *fd[MAX_NR_CPUS][MAX_COUNTERS]; #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
static bool system_wide = false; static bool system_wide = false;
...@@ -66,10 +68,9 @@ static int print_entries; ...@@ -66,10 +68,9 @@ static int print_entries;
static int target_pid = -1; static int target_pid = -1;
static int target_tid = -1; static int target_tid = -1;
static pid_t *all_tids = NULL; static struct thread_map *threads;
static int thread_num = 0;
static bool inherit = false; static bool inherit = false;
static int nr_cpus = 0; static struct cpu_map *cpus;
static int realtime_prio = 0; static int realtime_prio = 0;
static bool group = false; static bool group = false;
static unsigned int page_size; static unsigned int page_size;
...@@ -100,6 +101,7 @@ struct sym_entry *sym_filter_entry = NULL; ...@@ -100,6 +101,7 @@ struct sym_entry *sym_filter_entry = NULL;
struct sym_entry *sym_filter_entry_sched = NULL; struct sym_entry *sym_filter_entry_sched = NULL;
static int sym_pcnt_filter = 5; static int sym_pcnt_filter = 5;
static int sym_counter = 0; static int sym_counter = 0;
static struct perf_evsel *sym_evsel = NULL;
static int display_weighted = -1; static int display_weighted = -1;
static const char *cpu_list; static const char *cpu_list;
...@@ -353,7 +355,7 @@ static void show_details(struct sym_entry *syme) ...@@ -353,7 +355,7 @@ static void show_details(struct sym_entry *syme)
return; return;
symbol = sym_entry__symbol(syme); symbol = sym_entry__symbol(syme);
printf("Showing %s for %s\n", event_name(sym_counter), symbol->name); printf("Showing %s for %s\n", event_name(sym_evsel), symbol->name);
printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter); printf(" Events Pcnt (>=%d%%)\n", sym_pcnt_filter);
pthread_mutex_lock(&syme->src->lock); pthread_mutex_lock(&syme->src->lock);
...@@ -460,7 +462,8 @@ static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se) ...@@ -460,7 +462,8 @@ static void rb_insert_active_sym(struct rb_root *tree, struct sym_entry *se)
static void print_sym_table(void) static void print_sym_table(void)
{ {
int printed = 0, j; int printed = 0, j;
int counter, snap = !display_weighted ? sym_counter : 0; struct perf_evsel *counter;
int snap = !display_weighted ? sym_counter : 0;
float samples_per_sec = samples/delay_secs; float samples_per_sec = samples/delay_secs;
float ksamples_per_sec = kernel_samples/delay_secs; float ksamples_per_sec = kernel_samples/delay_secs;
float us_samples_per_sec = (us_samples)/delay_secs; float us_samples_per_sec = (us_samples)/delay_secs;
...@@ -532,7 +535,9 @@ static void print_sym_table(void) ...@@ -532,7 +535,9 @@ static void print_sym_table(void)
} }
if (nr_counters == 1 || !display_weighted) { if (nr_counters == 1 || !display_weighted) {
printf("%Ld", (u64)attrs[0].sample_period); struct perf_evsel *first;
first = list_entry(evsel_list.next, struct perf_evsel, node);
printf("%Ld", first->attr.sample_period);
if (freq) if (freq)
printf("Hz "); printf("Hz ");
else else
...@@ -540,9 +545,9 @@ static void print_sym_table(void) ...@@ -540,9 +545,9 @@ static void print_sym_table(void)
} }
if (!display_weighted) if (!display_weighted)
printf("%s", event_name(sym_counter)); printf("%s", event_name(sym_evsel));
else for (counter = 0; counter < nr_counters; counter++) { else list_for_each_entry(counter, &evsel_list, node) {
if (counter) if (counter->idx)
printf("/"); printf("/");
printf("%s", event_name(counter)); printf("%s", event_name(counter));
...@@ -558,12 +563,12 @@ static void print_sym_table(void) ...@@ -558,12 +563,12 @@ static void print_sym_table(void)
printf(" (all"); printf(" (all");
if (cpu_list) if (cpu_list)
printf(", CPU%s: %s)\n", nr_cpus > 1 ? "s" : "", cpu_list); printf(", CPU%s: %s)\n", cpus->nr > 1 ? "s" : "", cpu_list);
else { else {
if (target_tid != -1) if (target_tid != -1)
printf(")\n"); printf(")\n");
else else
printf(", %d CPU%s)\n", nr_cpus, nr_cpus > 1 ? "s" : ""); printf(", %d CPU%s)\n", cpus->nr, cpus->nr > 1 ? "s" : "");
} }
printf("%-*.*s\n", win_width, win_width, graph_dotted_line); printf("%-*.*s\n", win_width, win_width, graph_dotted_line);
...@@ -739,7 +744,7 @@ static void print_mapped_keys(void) ...@@ -739,7 +744,7 @@ static void print_mapped_keys(void)
fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries); fprintf(stdout, "\t[e] display entries (lines). \t(%d)\n", print_entries);
if (nr_counters > 1) if (nr_counters > 1)
fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_counter)); fprintf(stdout, "\t[E] active event counter. \t(%s)\n", event_name(sym_evsel));
fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter); fprintf(stdout, "\t[f] profile display filter (count). \t(%d)\n", count_filter);
...@@ -826,19 +831,23 @@ static void handle_keypress(struct perf_session *session, int c) ...@@ -826,19 +831,23 @@ static void handle_keypress(struct perf_session *session, int c)
break; break;
case 'E': case 'E':
if (nr_counters > 1) { if (nr_counters > 1) {
int i;
fprintf(stderr, "\nAvailable events:"); fprintf(stderr, "\nAvailable events:");
for (i = 0; i < nr_counters; i++)
fprintf(stderr, "\n\t%d %s", i, event_name(i)); list_for_each_entry(sym_evsel, &evsel_list, node)
fprintf(stderr, "\n\t%d %s", sym_evsel->idx, event_name(sym_evsel));
prompt_integer(&sym_counter, "Enter details event counter"); prompt_integer(&sym_counter, "Enter details event counter");
if (sym_counter >= nr_counters) { if (sym_counter >= nr_counters) {
fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(0)); sym_evsel = list_entry(evsel_list.next, struct perf_evsel, node);
sym_counter = 0; sym_counter = 0;
fprintf(stderr, "Sorry, no such event, using %s.\n", event_name(sym_evsel));
sleep(1); sleep(1);
break;
} }
list_for_each_entry(sym_evsel, &evsel_list, node)
if (sym_evsel->idx == sym_counter)
break;
} else sym_counter = 0; } else sym_counter = 0;
break; break;
case 'f': case 'f':
...@@ -978,7 +987,8 @@ static int symbol_filter(struct map *map, struct symbol *sym) ...@@ -978,7 +987,8 @@ static int symbol_filter(struct map *map, struct symbol *sym)
static void event__process_sample(const event_t *self, static void event__process_sample(const event_t *self,
struct sample_data *sample, struct sample_data *sample,
struct perf_session *session, int counter) struct perf_session *session,
struct perf_evsel *evsel)
{ {
u64 ip = self->ip.ip; u64 ip = self->ip.ip;
struct sym_entry *syme; struct sym_entry *syme;
...@@ -1071,9 +1081,9 @@ static void event__process_sample(const event_t *self, ...@@ -1071,9 +1081,9 @@ static void event__process_sample(const event_t *self,
syme = symbol__priv(al.sym); syme = symbol__priv(al.sym);
if (!syme->skip) { if (!syme->skip) {
syme->count[counter]++; syme->count[evsel->idx]++;
syme->origin = origin; syme->origin = origin;
record_precise_ip(syme, counter, ip); record_precise_ip(syme, evsel->idx, ip);
pthread_mutex_lock(&active_symbols_lock); pthread_mutex_lock(&active_symbols_lock);
if (list_empty(&syme->node) || !syme->node.next) if (list_empty(&syme->node) || !syme->node.next)
__list_insert_active_sym(syme); __list_insert_active_sym(syme);
...@@ -1082,12 +1092,24 @@ static void event__process_sample(const event_t *self, ...@@ -1082,12 +1092,24 @@ static void event__process_sample(const event_t *self,
} }
struct mmap_data { struct mmap_data {
int counter;
void *base; void *base;
int mask; int mask;
unsigned int prev; unsigned int prev;
}; };
static int perf_evsel__alloc_mmap_per_thread(struct perf_evsel *evsel,
int ncpus, int nthreads)
{
evsel->priv = xyarray__new(ncpus, nthreads, sizeof(struct mmap_data));
return evsel->priv != NULL ? 0 : -ENOMEM;
}
static void perf_evsel__free_mmap(struct perf_evsel *evsel)
{
xyarray__delete(evsel->priv);
evsel->priv = NULL;
}
static unsigned int mmap_read_head(struct mmap_data *md) static unsigned int mmap_read_head(struct mmap_data *md)
{ {
struct perf_event_mmap_page *pc = md->base; struct perf_event_mmap_page *pc = md->base;
...@@ -1100,8 +1122,11 @@ static unsigned int mmap_read_head(struct mmap_data *md) ...@@ -1100,8 +1122,11 @@ static unsigned int mmap_read_head(struct mmap_data *md)
} }
static void perf_session__mmap_read_counter(struct perf_session *self, static void perf_session__mmap_read_counter(struct perf_session *self,
struct mmap_data *md) struct perf_evsel *evsel,
int cpu, int thread_idx)
{ {
struct xyarray *mmap_array = evsel->priv;
struct mmap_data *md = xyarray__entry(mmap_array, cpu, thread_idx);
unsigned int head = mmap_read_head(md); unsigned int head = mmap_read_head(md);
unsigned int old = md->prev; unsigned int old = md->prev;
unsigned char *data = md->base + page_size; unsigned char *data = md->base + page_size;
...@@ -1155,7 +1180,7 @@ static void perf_session__mmap_read_counter(struct perf_session *self, ...@@ -1155,7 +1180,7 @@ static void perf_session__mmap_read_counter(struct perf_session *self,
event__parse_sample(event, self, &sample); event__parse_sample(event, self, &sample);
if (event->header.type == PERF_RECORD_SAMPLE) if (event->header.type == PERF_RECORD_SAMPLE)
event__process_sample(event, &sample, self, md->counter); event__process_sample(event, &sample, self, evsel);
else else
event__process(event, &sample, self); event__process(event, &sample, self);
old += size; old += size;
...@@ -1165,36 +1190,39 @@ static void perf_session__mmap_read_counter(struct perf_session *self, ...@@ -1165,36 +1190,39 @@ static void perf_session__mmap_read_counter(struct perf_session *self,
} }
static struct pollfd *event_array; 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) static void perf_session__mmap_read(struct perf_session *self)
{ {
int i, counter, thread_index; struct perf_evsel *counter;
int i, thread_index;
for (i = 0; i < nr_cpus; i++) { for (i = 0; i < cpus->nr; i++) {
for (counter = 0; counter < nr_counters; counter++) list_for_each_entry(counter, &evsel_list, node) {
for (thread_index = 0; for (thread_index = 0;
thread_index < thread_num; thread_index < threads->nr;
thread_index++) { thread_index++) {
perf_session__mmap_read_counter(self, perf_session__mmap_read_counter(self,
&mmap_array[i][counter][thread_index]); counter, i, thread_index);
} }
}
} }
} }
int nr_poll; int nr_poll;
int group_fd; int group_fd;
static void start_counter(int i, int counter) static void start_counter(int i, struct perf_evsel *evsel)
{ {
struct xyarray *mmap_array = evsel->priv;
struct mmap_data *mm;
struct perf_event_attr *attr; struct perf_event_attr *attr;
int cpu = -1; int cpu = -1;
int thread_index; int thread_index;
if (target_tid == -1) if (target_tid == -1)
cpu = cpumap[i]; cpu = cpus->map[i];
attr = attrs + counter; attr = &evsel->attr;
attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID; attr->sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID;
...@@ -1207,12 +1235,12 @@ static void start_counter(int i, int counter) ...@@ -1207,12 +1235,12 @@ static void start_counter(int i, int counter)
attr->inherit = (cpu < 0) && inherit; attr->inherit = (cpu < 0) && inherit;
attr->mmap = 1; attr->mmap = 1;
for (thread_index = 0; thread_index < thread_num; thread_index++) { for (thread_index = 0; thread_index < threads->nr; thread_index++) {
try_again: try_again:
fd[i][counter][thread_index] = sys_perf_event_open(attr, FD(evsel, i, thread_index) = sys_perf_event_open(attr,
all_tids[thread_index], cpu, group_fd, 0); threads->map[thread_index], cpu, group_fd, 0);
if (fd[i][counter][thread_index] < 0) { if (FD(evsel, i, thread_index) < 0) {
int err = errno; int err = errno;
if (err == EPERM || err == EACCES) if (err == EPERM || err == EACCES)
...@@ -1236,29 +1264,29 @@ static void start_counter(int i, int counter) ...@@ -1236,29 +1264,29 @@ static void start_counter(int i, int counter)
} }
printf("\n"); printf("\n");
error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n", error("sys_perf_event_open() syscall returned with %d (%s). /bin/dmesg may provide additional information.\n",
fd[i][counter][thread_index], strerror(err)); FD(evsel, i, thread_index), strerror(err));
die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
exit(-1); exit(-1);
} }
assert(fd[i][counter][thread_index] >= 0); assert(FD(evsel, i, thread_index) >= 0);
fcntl(fd[i][counter][thread_index], F_SETFL, O_NONBLOCK); fcntl(FD(evsel, i, thread_index), F_SETFL, O_NONBLOCK);
/* /*
* First counter acts as the group leader: * First counter acts as the group leader:
*/ */
if (group && group_fd == -1) if (group && group_fd == -1)
group_fd = fd[i][counter][thread_index]; group_fd = FD(evsel, i, thread_index);
event_array[nr_poll].fd = fd[i][counter][thread_index]; event_array[nr_poll].fd = FD(evsel, i, thread_index);
event_array[nr_poll].events = POLLIN; event_array[nr_poll].events = POLLIN;
nr_poll++; nr_poll++;
mmap_array[i][counter][thread_index].counter = counter; mm = xyarray__entry(mmap_array, i, thread_index);
mmap_array[i][counter][thread_index].prev = 0; mm->prev = 0;
mmap_array[i][counter][thread_index].mask = mmap_pages*page_size - 1; mm->mask = mmap_pages*page_size - 1;
mmap_array[i][counter][thread_index].base = mmap(NULL, (mmap_pages+1)*page_size, mm->base = mmap(NULL, (mmap_pages+1)*page_size,
PROT_READ, MAP_SHARED, fd[i][counter][thread_index], 0); PROT_READ, MAP_SHARED, FD(evsel, i, thread_index), 0);
if (mmap_array[i][counter][thread_index].base == MAP_FAILED) if (mm->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));
} }
} }
...@@ -1266,8 +1294,8 @@ static void start_counter(int i, int counter) ...@@ -1266,8 +1294,8 @@ static void start_counter(int i, int counter)
static int __cmd_top(void) static int __cmd_top(void)
{ {
pthread_t thread; pthread_t thread;
int i, counter; struct perf_evsel *counter;
int ret; int i, ret;
/* /*
* FIXME: perf_session__new should allow passing a O_MMAP, so that all this * FIXME: perf_session__new should allow passing a O_MMAP, so that all this
* mmap reading, etc is encapsulated in it. Use O_WRONLY for now. * mmap reading, etc is encapsulated in it. Use O_WRONLY for now.
...@@ -1281,9 +1309,9 @@ static int __cmd_top(void) ...@@ -1281,9 +1309,9 @@ static int __cmd_top(void)
else else
event__synthesize_threads(event__process, session); event__synthesize_threads(event__process, session);
for (i = 0; i < nr_cpus; i++) { for (i = 0; i < cpus->nr; i++) {
group_fd = -1; group_fd = -1;
for (counter = 0; counter < nr_counters; counter++) list_for_each_entry(counter, &evsel_list, node)
start_counter(i, counter); start_counter(i, counter);
} }
...@@ -1372,8 +1400,8 @@ static const struct option options[] = { ...@@ -1372,8 +1400,8 @@ static const struct option options[] = {
int cmd_top(int argc, const char **argv, const char *prefix __used) int cmd_top(int argc, const char **argv, const char *prefix __used)
{ {
int counter; struct perf_evsel *pos;
int i,j; int status = -ENOMEM;
page_size = sysconf(_SC_PAGE_SIZE); page_size = sysconf(_SC_PAGE_SIZE);
...@@ -1381,34 +1409,17 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) ...@@ -1381,34 +1409,17 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
if (argc) if (argc)
usage_with_options(top_usage, options); usage_with_options(top_usage, options);
if (target_pid != -1) { if (target_pid != -1)
target_tid = target_pid; 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; threads = thread_map__new(target_pid, target_tid);
thread_num = 1; if (threads == NULL) {
pr_err("Problems finding threads of monitor\n");
usage_with_options(top_usage, options);
} }
for (i = 0; i < MAX_NR_CPUS; i++) { event_array = malloc((sizeof(struct pollfd) *
for (j = 0; j < MAX_COUNTERS; j++) { MAX_NR_CPUS * MAX_COUNTERS * threads->nr));
fd[i][j] = malloc(sizeof(int)*thread_num);
mmap_array[i][j] = zalloc(
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) if (!event_array)
return -ENOMEM; return -ENOMEM;
...@@ -1419,15 +1430,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) ...@@ -1419,15 +1430,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
cpu_list = NULL; cpu_list = NULL;
} }
if (!nr_counters) if (!nr_counters && perf_evsel_list__create_default() < 0) {
nr_counters = 1; pr_err("Not enough memory for event selector list\n");
return -ENOMEM;
symbol_conf.priv_size = (sizeof(struct sym_entry) + }
(nr_counters + 1) * sizeof(unsigned long));
symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
if (symbol__init() < 0)
return -1;
if (delay_secs < 1) if (delay_secs < 1)
delay_secs = 1; delay_secs = 1;
...@@ -1444,23 +1450,33 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) ...@@ -1444,23 +1450,33 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* if (target_tid != -1)
* Fill in the ones not specifically initialized via -c: cpus = cpu_map__dummy_new();
*/ else
for (counter = 0; counter < nr_counters; counter++) { cpus = cpu_map__new(cpu_list);
if (attrs[counter].sample_period)
if (cpus == NULL)
usage_with_options(top_usage, options);
list_for_each_entry(pos, &evsel_list, node) {
if (perf_evsel__alloc_mmap_per_thread(pos, cpus->nr, threads->nr) < 0 ||
perf_evsel__alloc_fd(pos, cpus->nr, threads->nr) < 0)
goto out_free_fd;
/*
* Fill in the ones not specifically initialized via -c:
*/
if (pos->attr.sample_period)
continue; continue;
attrs[counter].sample_period = default_interval; pos->attr.sample_period = default_interval;
} }
if (target_tid != -1) symbol_conf.priv_size = (sizeof(struct sym_entry) +
nr_cpus = 1; (nr_counters + 1) * sizeof(unsigned long));
else
nr_cpus = read_cpu_map(cpu_list);
if (nr_cpus < 1) symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name == NULL);
usage_with_options(top_usage, options); if (symbol__init() < 0)
return -1;
get_term_dimensions(&winsize); get_term_dimensions(&winsize);
if (print_entries == 0) { if (print_entries == 0) {
...@@ -1468,5 +1484,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __used) ...@@ -1468,5 +1484,10 @@ int cmd_top(int argc, const char **argv, const char *prefix __used)
signal(SIGWINCH, sig_winch_handler); signal(SIGWINCH, sig_winch_handler);
} }
return __cmd_top(); status = __cmd_top();
out_free_fd:
list_for_each_entry(pos, &evsel_list, node)
perf_evsel__free_mmap(pos);
return status;
} }
...@@ -286,6 +286,8 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv) ...@@ -286,6 +286,8 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
status = p->fn(argc, argv, prefix); status = p->fn(argc, argv, prefix);
exit_browser(status); exit_browser(status);
perf_evsel_list__delete();
if (status) if (status)
return status & 0xff; return status & 0xff;
......
...@@ -4,32 +4,53 @@ ...@@ -4,32 +4,53 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
int cpumap[MAX_NR_CPUS]; static struct cpu_map *cpu_map__default_new(void)
static int default_cpu_map(void)
{ {
int nr_cpus, i; struct cpu_map *cpus;
int nr_cpus;
nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
assert(nr_cpus <= MAX_NR_CPUS); if (nr_cpus < 0)
assert((int)nr_cpus >= 0); return NULL;
cpus = malloc(sizeof(*cpus) + nr_cpus * sizeof(int));
if (cpus != NULL) {
int i;
for (i = 0; i < nr_cpus; ++i)
cpus->map[i] = i;
for (i = 0; i < nr_cpus; ++i) cpus->nr = nr_cpus;
cpumap[i] = i; }
return nr_cpus; return cpus;
} }
static int read_all_cpu_map(void) static struct cpu_map *cpu_map__trim_new(int nr_cpus, int *tmp_cpus)
{ {
size_t payload_size = nr_cpus * sizeof(int);
struct cpu_map *cpus = malloc(sizeof(*cpus) + payload_size);
if (cpus != NULL) {
cpus->nr = nr_cpus;
memcpy(cpus->map, tmp_cpus, payload_size);
}
return cpus;
}
static struct cpu_map *cpu_map__read_all_cpu_map(void)
{
struct cpu_map *cpus = NULL;
FILE *onlnf; FILE *onlnf;
int nr_cpus = 0; int nr_cpus = 0;
int *tmp_cpus = NULL, *tmp;
int max_entries = 0;
int n, cpu, prev; int n, cpu, prev;
char sep; char sep;
onlnf = fopen("/sys/devices/system/cpu/online", "r"); onlnf = fopen("/sys/devices/system/cpu/online", "r");
if (!onlnf) if (!onlnf)
return default_cpu_map(); return cpu_map__default_new();
sep = 0; sep = 0;
prev = -1; prev = -1;
...@@ -38,12 +59,28 @@ static int read_all_cpu_map(void) ...@@ -38,12 +59,28 @@ static int read_all_cpu_map(void)
if (n <= 0) if (n <= 0)
break; break;
if (prev >= 0) { if (prev >= 0) {
assert(nr_cpus + cpu - prev - 1 < MAX_NR_CPUS); int new_max = nr_cpus + cpu - prev - 1;
if (new_max >= max_entries) {
max_entries = new_max + MAX_NR_CPUS / 2;
tmp = realloc(tmp_cpus, max_entries * sizeof(int));
if (tmp == NULL)
goto out_free_tmp;
tmp_cpus = tmp;
}
while (++prev < cpu) while (++prev < cpu)
cpumap[nr_cpus++] = prev; tmp_cpus[nr_cpus++] = prev;
}
if (nr_cpus == max_entries) {
max_entries += MAX_NR_CPUS;
tmp = realloc(tmp_cpus, max_entries * sizeof(int));
if (tmp == NULL)
goto out_free_tmp;
tmp_cpus = tmp;
} }
assert (nr_cpus < MAX_NR_CPUS);
cpumap[nr_cpus++] = cpu; tmp_cpus[nr_cpus++] = cpu;
if (n == 2 && sep == '-') if (n == 2 && sep == '-')
prev = cpu; prev = cpu;
else else
...@@ -51,24 +88,31 @@ static int read_all_cpu_map(void) ...@@ -51,24 +88,31 @@ static int read_all_cpu_map(void)
if (n == 1 || sep == '\n') if (n == 1 || sep == '\n')
break; break;
} }
fclose(onlnf);
if (nr_cpus > 0)
return nr_cpus;
return default_cpu_map(); if (nr_cpus > 0)
cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
else
cpus = cpu_map__default_new();
out_free_tmp:
free(tmp_cpus);
fclose(onlnf);
return cpus;
} }
int read_cpu_map(const char *cpu_list) struct cpu_map *cpu_map__new(const char *cpu_list)
{ {
struct cpu_map *cpus = NULL;
unsigned long start_cpu, end_cpu = 0; unsigned long start_cpu, end_cpu = 0;
char *p = NULL; char *p = NULL;
int i, nr_cpus = 0; int i, nr_cpus = 0;
int *tmp_cpus = NULL, *tmp;
int max_entries = 0;
if (!cpu_list) if (!cpu_list)
return read_all_cpu_map(); return cpu_map__read_all_cpu_map();
if (!isdigit(*cpu_list)) if (!isdigit(*cpu_list))
goto invalid; goto out;
while (isdigit(*cpu_list)) { while (isdigit(*cpu_list)) {
p = NULL; p = NULL;
...@@ -94,21 +138,42 @@ int read_cpu_map(const char *cpu_list) ...@@ -94,21 +138,42 @@ int read_cpu_map(const char *cpu_list)
for (; start_cpu <= end_cpu; start_cpu++) { for (; start_cpu <= end_cpu; start_cpu++) {
/* check for duplicates */ /* check for duplicates */
for (i = 0; i < nr_cpus; i++) for (i = 0; i < nr_cpus; i++)
if (cpumap[i] == (int)start_cpu) if (tmp_cpus[i] == (int)start_cpu)
goto invalid; goto invalid;
assert(nr_cpus < MAX_NR_CPUS); if (nr_cpus == max_entries) {
cpumap[nr_cpus++] = (int)start_cpu; max_entries += MAX_NR_CPUS;
tmp = realloc(tmp_cpus, max_entries * sizeof(int));
if (tmp == NULL)
goto invalid;
tmp_cpus = tmp;
}
tmp_cpus[nr_cpus++] = (int)start_cpu;
} }
if (*p) if (*p)
++p; ++p;
cpu_list = p; cpu_list = p;
} }
if (nr_cpus > 0)
return nr_cpus;
return default_cpu_map(); if (nr_cpus > 0)
cpus = cpu_map__trim_new(nr_cpus, tmp_cpus);
else
cpus = cpu_map__default_new();
invalid: invalid:
return -1; free(tmp_cpus);
out:
return cpus;
}
struct cpu_map *cpu_map__dummy_new(void)
{
struct cpu_map *cpus = malloc(sizeof(*cpus) + sizeof(int));
if (cpus != NULL) {
cpus->nr = 1;
cpus->map[0] = -1;
}
return cpus;
} }
#ifndef __PERF_CPUMAP_H #ifndef __PERF_CPUMAP_H
#define __PERF_CPUMAP_H #define __PERF_CPUMAP_H
extern int read_cpu_map(const char *cpu_list); struct cpu_map {
extern int cpumap[]; int nr;
int map[];
};
struct cpu_map *cpu_map__new(const char *cpu_list);
struct cpu_map *cpu_map__dummy_new(void);
void *cpu_map__delete(struct cpu_map *map);
#endif /* __PERF_CPUMAP_H */ #endif /* __PERF_CPUMAP_H */
#include "evsel.h"
#include "../perf.h"
#include "util.h"
#include "cpumap.h"
#include "thread.h"
#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
struct perf_evsel *perf_evsel__new(u32 type, u64 config, int idx)
{
struct perf_evsel *evsel = zalloc(sizeof(*evsel));
if (evsel != NULL) {
evsel->idx = idx;
evsel->attr.type = type;
evsel->attr.config = config;
INIT_LIST_HEAD(&evsel->node);
}
return evsel;
}
int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
{
evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
return evsel->fd != NULL ? 0 : -ENOMEM;
}
int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
{
evsel->counts = zalloc((sizeof(*evsel->counts) +
(ncpus * sizeof(struct perf_counts_values))));
return evsel->counts != NULL ? 0 : -ENOMEM;
}
void perf_evsel__free_fd(struct perf_evsel *evsel)
{
xyarray__delete(evsel->fd);
evsel->fd = NULL;
}
void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
{
int cpu, thread;
for (cpu = 0; cpu < ncpus; cpu++)
for (thread = 0; thread < nthreads; ++thread) {
close(FD(evsel, cpu, thread));
FD(evsel, cpu, thread) = -1;
}
}
void perf_evsel__delete(struct perf_evsel *evsel)
{
assert(list_empty(&evsel->node));
xyarray__delete(evsel->fd);
free(evsel);
}
int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
int cpu, int thread, bool scale)
{
struct perf_counts_values count;
size_t nv = scale ? 3 : 1;
if (FD(evsel, cpu, thread) < 0)
return -EINVAL;
if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
return -ENOMEM;
if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
return -errno;
if (scale) {
if (count.run == 0)
count.val = 0;
else if (count.run < count.ena)
count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
} else
count.ena = count.run = 0;
evsel->counts->cpu[cpu] = count;
return 0;
}
int __perf_evsel__read(struct perf_evsel *evsel,
int ncpus, int nthreads, bool scale)
{
size_t nv = scale ? 3 : 1;
int cpu, thread;
struct perf_counts_values *aggr = &evsel->counts->aggr, count;
aggr->val = 0;
for (cpu = 0; cpu < ncpus; cpu++) {
for (thread = 0; thread < nthreads; thread++) {
if (FD(evsel, cpu, thread) < 0)
continue;
if (readn(FD(evsel, cpu, thread),
&count, nv * sizeof(u64)) < 0)
return -errno;
aggr->val += count.val;
if (scale) {
aggr->ena += count.ena;
aggr->run += count.run;
}
}
}
evsel->counts->scaled = 0;
if (scale) {
if (aggr->run == 0) {
evsel->counts->scaled = -1;
aggr->val = 0;
return 0;
}
if (aggr->run < aggr->ena) {
evsel->counts->scaled = 1;
aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
}
} else
aggr->ena = aggr->run = 0;
return 0;
}
int perf_evsel__open_per_cpu(struct perf_evsel *evsel, struct cpu_map *cpus)
{
int cpu;
if (evsel->fd == NULL && perf_evsel__alloc_fd(evsel, cpus->nr, 1) < 0)
return -1;
for (cpu = 0; cpu < cpus->nr; cpu++) {
FD(evsel, cpu, 0) = sys_perf_event_open(&evsel->attr, -1,
cpus->map[cpu], -1, 0);
if (FD(evsel, cpu, 0) < 0)
goto out_close;
}
return 0;
out_close:
while (--cpu >= 0) {
close(FD(evsel, cpu, 0));
FD(evsel, cpu, 0) = -1;
}
return -1;
}
int perf_evsel__open_per_thread(struct perf_evsel *evsel, struct thread_map *threads)
{
int thread;
if (evsel->fd == NULL && perf_evsel__alloc_fd(evsel, 1, threads->nr))
return -1;
for (thread = 0; thread < threads->nr; thread++) {
FD(evsel, 0, thread) = sys_perf_event_open(&evsel->attr,
threads->map[thread], -1, -1, 0);
if (FD(evsel, 0, thread) < 0)
goto out_close;
}
return 0;
out_close:
while (--thread >= 0) {
close(FD(evsel, 0, thread));
FD(evsel, 0, thread) = -1;
}
return -1;
}
int perf_evsel__open(struct perf_evsel *evsel,
struct cpu_map *cpus, struct thread_map *threads)
{
if (threads == NULL)
return perf_evsel__open_per_cpu(evsel, cpus);
return perf_evsel__open_per_thread(evsel, threads);
}
#ifndef __PERF_EVSEL_H
#define __PERF_EVSEL_H 1
#include <linux/list.h>
#include <stdbool.h>
#include <linux/perf_event.h>
#include "types.h"
#include "xyarray.h"
struct perf_counts_values {
union {
struct {
u64 val;
u64 ena;
u64 run;
};
u64 values[3];
};
};
struct perf_counts {
s8 scaled;
struct perf_counts_values aggr;
struct perf_counts_values cpu[];
};
struct perf_evsel {
struct list_head node;
struct perf_event_attr attr;
char *filter;
struct xyarray *fd;
struct perf_counts *counts;
int idx;
void *priv;
};
struct cpu_map;
struct thread_map;
struct perf_evsel *perf_evsel__new(u32 type, u64 config, int idx);
void perf_evsel__delete(struct perf_evsel *evsel);
int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus);
void perf_evsel__free_fd(struct perf_evsel *evsel);
void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
int perf_evsel__open_per_cpu(struct perf_evsel *evsel, struct cpu_map *cpus);
int perf_evsel__open_per_thread(struct perf_evsel *evsel, struct thread_map *threads);
int perf_evsel__open(struct perf_evsel *evsel,
struct cpu_map *cpus, struct thread_map *threads);
#define perf_evsel__match(evsel, t, c) \
(evsel->attr.type == PERF_TYPE_##t && \
evsel->attr.config == PERF_COUNT_##c)
int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
int cpu, int thread, bool scale);
/**
* perf_evsel__read_on_cpu - Read out the results on a CPU and thread
*
* @evsel - event selector to read value
* @cpu - CPU of interest
* @thread - thread of interest
*/
static inline int perf_evsel__read_on_cpu(struct perf_evsel *evsel,
int cpu, int thread)
{
return __perf_evsel__read_on_cpu(evsel, cpu, thread, false);
}
/**
* perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
*
* @evsel - event selector to read value
* @cpu - CPU of interest
* @thread - thread of interest
*/
static inline int perf_evsel__read_on_cpu_scaled(struct perf_evsel *evsel,
int cpu, int thread)
{
return __perf_evsel__read_on_cpu(evsel, cpu, thread, true);
}
int __perf_evsel__read(struct perf_evsel *evsel, int ncpus, int nthreads,
bool scale);
/**
* perf_evsel__read - Read the aggregate results on all CPUs
*
* @evsel - event selector to read value
* @ncpus - Number of cpus affected, from zero
* @nthreads - Number of threads affected, from zero
*/
static inline int perf_evsel__read(struct perf_evsel *evsel,
int ncpus, int nthreads)
{
return __perf_evsel__read(evsel, ncpus, nthreads, false);
}
/**
* perf_evsel__read_scaled - Read the aggregate results on all CPUs, scaled
*
* @evsel - event selector to read value
* @ncpus - Number of cpus affected, from zero
* @nthreads - Number of threads affected, from zero
*/
static inline int perf_evsel__read_scaled(struct perf_evsel *evsel,
int ncpus, int nthreads)
{
return __perf_evsel__read(evsel, ncpus, nthreads, true);
}
#endif /* __PERF_EVSEL_H */
...@@ -463,7 +463,7 @@ static int perf_header__adds_write(struct perf_header *self, int fd) ...@@ -463,7 +463,7 @@ static int perf_header__adds_write(struct perf_header *self, int fd)
/* Write trace info */ /* Write trace info */
trace_sec->offset = lseek(fd, 0, SEEK_CUR); trace_sec->offset = lseek(fd, 0, SEEK_CUR);
read_tracing_data(fd, attrs, nr_counters); read_tracing_data(fd, &evsel_list);
trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset; trace_sec->size = lseek(fd, 0, SEEK_CUR) - trace_sec->offset;
} }
...@@ -606,7 +606,7 @@ int perf_header__write(struct perf_header *self, int fd, bool at_exit) ...@@ -606,7 +606,7 @@ int perf_header__write(struct perf_header *self, int fd, bool at_exit)
static int perf_header__getbuffer64(struct perf_header *self, static int perf_header__getbuffer64(struct perf_header *self,
int fd, void *buf, size_t size) int fd, void *buf, size_t size)
{ {
if (do_read(fd, buf, size) <= 0) if (readn(fd, buf, size) <= 0)
return -1; return -1;
if (self->needs_swap) if (self->needs_swap)
...@@ -662,7 +662,7 @@ int perf_file_header__read(struct perf_file_header *self, ...@@ -662,7 +662,7 @@ int perf_file_header__read(struct perf_file_header *self,
{ {
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
if (do_read(fd, self, sizeof(*self)) <= 0 || if (readn(fd, self, sizeof(*self)) <= 0 ||
memcmp(&self->magic, __perf_magic, sizeof(self->magic))) memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
return -1; return -1;
...@@ -823,7 +823,7 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *self, ...@@ -823,7 +823,7 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
struct perf_header *ph, int fd, struct perf_header *ph, int fd,
bool repipe) bool repipe)
{ {
if (do_read(fd, self, sizeof(*self)) <= 0 || if (readn(fd, self, sizeof(*self)) <= 0 ||
memcmp(&self->magic, __perf_magic, sizeof(self->magic))) memcmp(&self->magic, __perf_magic, sizeof(self->magic)))
return -1; return -1;
...@@ -1133,8 +1133,7 @@ int event__process_event_type(event_t *self, ...@@ -1133,8 +1133,7 @@ int event__process_event_type(event_t *self,
return 0; return 0;
} }
int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs, int event__synthesize_tracing_data(int fd, struct list_head *pattrs,
int nb_events,
event__handler_t process, event__handler_t process,
struct perf_session *session __unused) struct perf_session *session __unused)
{ {
...@@ -1145,7 +1144,7 @@ int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs, ...@@ -1145,7 +1144,7 @@ int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
memset(&ev, 0, sizeof(ev)); memset(&ev, 0, sizeof(ev));
ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA;
size = read_tracing_data_size(fd, pattrs, nb_events); size = read_tracing_data_size(fd, pattrs);
if (size <= 0) if (size <= 0)
return size; return size;
aligned_size = ALIGN(size, sizeof(u64)); aligned_size = ALIGN(size, sizeof(u64));
...@@ -1155,7 +1154,7 @@ int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs, ...@@ -1155,7 +1154,7 @@ int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
process(&ev, NULL, session); process(&ev, NULL, session);
err = read_tracing_data(fd, pattrs, nb_events); err = read_tracing_data(fd, pattrs);
write_padded(fd, NULL, 0, padding); write_padded(fd, NULL, 0, padding);
return aligned_size; return aligned_size;
......
...@@ -113,8 +113,7 @@ int event__synthesize_event_types(event__handler_t process, ...@@ -113,8 +113,7 @@ int event__synthesize_event_types(event__handler_t process,
int event__process_event_type(event_t *self, int event__process_event_type(event_t *self,
struct perf_session *session); struct perf_session *session);
int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs, int event__synthesize_tracing_data(int fd, struct list_head *pattrs,
int nb_events,
event__handler_t process, event__handler_t process,
struct perf_session *session); struct perf_session *session);
int event__process_tracing_data(event_t *self, int event__process_tracing_data(event_t *self,
......
#include "../../../include/linux/hw_breakpoint.h" #include "../../../include/linux/hw_breakpoint.h"
#include "util.h" #include "util.h"
#include "../perf.h" #include "../perf.h"
#include "evsel.h"
#include "parse-options.h" #include "parse-options.h"
#include "parse-events.h" #include "parse-events.h"
#include "exec_cmd.h" #include "exec_cmd.h"
...@@ -12,8 +13,7 @@ ...@@ -12,8 +13,7 @@
int nr_counters; int nr_counters;
struct perf_event_attr attrs[MAX_COUNTERS]; LIST_HEAD(evsel_list);
char *filters[MAX_COUNTERS];
struct event_symbol { struct event_symbol {
u8 type; u8 type;
...@@ -266,10 +266,10 @@ static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result) ...@@ -266,10 +266,10 @@ static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
return name; return name;
} }
const char *event_name(int counter) const char *event_name(struct perf_evsel *evsel)
{ {
u64 config = attrs[counter].config; u64 config = evsel->attr.config;
int type = attrs[counter].type; int type = evsel->attr.type;
return __event_name(type, config); return __event_name(type, config);
} }
...@@ -814,9 +814,6 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u ...@@ -814,9 +814,6 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u
return -1; return -1;
for (;;) { for (;;) {
if (nr_counters == MAX_COUNTERS)
return -1;
memset(&attr, 0, sizeof(attr)); memset(&attr, 0, sizeof(attr));
ret = parse_event_symbols(&str, &attr); ret = parse_event_symbols(&str, &attr);
if (ret == EVT_FAILED) if (ret == EVT_FAILED)
...@@ -826,8 +823,13 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u ...@@ -826,8 +823,13 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u
return -1; return -1;
if (ret != EVT_HANDLED_ALL) { if (ret != EVT_HANDLED_ALL) {
attrs[nr_counters] = attr; struct perf_evsel *evsel;
nr_counters++; evsel = perf_evsel__new(attr.type, attr.config,
nr_counters);
if (evsel == NULL)
return -1;
list_add_tail(&evsel->node, &evsel_list);
++nr_counters;
} }
if (*str == 0) if (*str == 0)
...@@ -844,21 +846,22 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u ...@@ -844,21 +846,22 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u
int parse_filter(const struct option *opt __used, const char *str, int parse_filter(const struct option *opt __used, const char *str,
int unset __used) int unset __used)
{ {
int i = nr_counters - 1; struct perf_evsel *last = NULL;
int len = strlen(str);
if (i < 0 || attrs[i].type != PERF_TYPE_TRACEPOINT) { if (!list_empty(&evsel_list))
last = list_entry(evsel_list.prev, struct perf_evsel, node);
if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
fprintf(stderr, fprintf(stderr,
"-F option should follow a -e tracepoint option\n"); "-F option should follow a -e tracepoint option\n");
return -1; return -1;
} }
filters[i] = malloc(len + 1); last->filter = strdup(str);
if (!filters[i]) { if (last->filter == NULL) {
fprintf(stderr, "not enough memory to hold filter string\n"); fprintf(stderr, "not enough memory to hold filter string\n");
return -1; return -1;
} }
strcpy(filters[i], str);
return 0; return 0;
} }
...@@ -967,3 +970,26 @@ void print_events(void) ...@@ -967,3 +970,26 @@ void print_events(void)
exit(129); exit(129);
} }
int perf_evsel_list__create_default(void)
{
struct perf_evsel *evsel = perf_evsel__new(PERF_TYPE_HARDWARE,
PERF_COUNT_HW_CPU_CYCLES, 0);
if (evsel == NULL)
return -ENOMEM;
list_add(&evsel->node, &evsel_list);
++nr_counters;
return 0;
}
void perf_evsel_list__delete(void)
{
struct perf_evsel *pos, *n;
list_for_each_entry_safe(pos, n, &evsel_list, node) {
list_del_init(&pos->node);
perf_evsel__delete(pos);
}
nr_counters = 0;
}
...@@ -4,6 +4,16 @@ ...@@ -4,6 +4,16 @@
* Parse symbolic events/counts passed in as options: * Parse symbolic events/counts passed in as options:
*/ */
#include <linux/perf_event.h>
struct list_head;
struct perf_evsel;
extern struct list_head evsel_list;
int perf_evsel_list__create_default(void);
void perf_evsel_list__delete(void);
struct option; struct option;
struct tracepoint_path { struct tracepoint_path {
...@@ -13,14 +23,11 @@ struct tracepoint_path { ...@@ -13,14 +23,11 @@ struct tracepoint_path {
}; };
extern struct tracepoint_path *tracepoint_id_to_path(u64 config); extern struct tracepoint_path *tracepoint_id_to_path(u64 config);
extern bool have_tracepoints(struct perf_event_attr *pattrs, int nb_events); extern bool have_tracepoints(struct list_head *evsel_list);
extern int nr_counters; extern int nr_counters;
extern struct perf_event_attr attrs[MAX_COUNTERS]; const char *event_name(struct perf_evsel *event);
extern char *filters[MAX_COUNTERS];
extern const char *event_name(int ctr);
extern const char *__event_name(int type, u64 config); extern const char *__event_name(int type, u64 config);
extern int parse_events(const struct option *opt, const char *str, int unset); extern int parse_events(const struct option *opt, const char *str, int unset);
...@@ -33,5 +40,4 @@ extern void print_events(void); ...@@ -33,5 +40,4 @@ extern void print_events(void);
extern char debugfs_path[]; extern char debugfs_path[];
extern int valid_debugfs_mount(const char *debugfs); extern int valid_debugfs_mount(const char *debugfs);
#endif /* __PERF_PARSE_EVENTS_H */ #endif /* __PERF_PARSE_EVENTS_H */
...@@ -838,23 +838,6 @@ static struct thread *perf_session__register_idle_thread(struct perf_session *se ...@@ -838,23 +838,6 @@ static struct thread *perf_session__register_idle_thread(struct perf_session *se
return thread; return thread;
} }
int do_read(int fd, void *buf, size_t size)
{
void *buf_start = buf;
while (size) {
int ret = read(fd, buf, size);
if (ret <= 0)
return ret;
size -= ret;
buf += ret;
}
return buf - buf_start;
}
#define session_done() (*(volatile int *)(&session_done)) #define session_done() (*(volatile int *)(&session_done))
volatile int session_done; volatile int session_done;
...@@ -872,7 +855,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self, ...@@ -872,7 +855,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
head = 0; head = 0;
more: more:
err = do_read(self->fd, &event, sizeof(struct perf_event_header)); err = readn(self->fd, &event, sizeof(struct perf_event_header));
if (err <= 0) { if (err <= 0) {
if (err == 0) if (err == 0)
goto done; goto done;
...@@ -892,8 +875,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self, ...@@ -892,8 +875,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
p += sizeof(struct perf_event_header); p += sizeof(struct perf_event_header);
if (size - sizeof(struct perf_event_header)) { if (size - sizeof(struct perf_event_header)) {
err = do_read(self->fd, p, err = readn(self->fd, p, size - sizeof(struct perf_event_header));
size - sizeof(struct perf_event_header));
if (err <= 0) { if (err <= 0) {
if (err == 0) { if (err == 0) {
pr_err("unexpected end of event stream\n"); pr_err("unexpected end of event stream\n");
......
...@@ -109,7 +109,6 @@ void mem_bswap_64(void *src, int byte_size); ...@@ -109,7 +109,6 @@ void mem_bswap_64(void *src, int byte_size);
int perf_session__create_kernel_maps(struct perf_session *self); int perf_session__create_kernel_maps(struct perf_session *self);
int do_read(int fd, void *buf, size_t size);
void perf_session__update_sample_type(struct perf_session *self); void perf_session__update_sample_type(struct perf_session *self);
void perf_session__set_sample_id_all(struct perf_session *session, bool value); void perf_session__set_sample_id_all(struct perf_session *session, bool value);
void perf_session__set_sample_type(struct perf_session *session, u64 type); void perf_session__set_sample_type(struct perf_session *session, u64 type);
......
...@@ -16,35 +16,50 @@ static int filter(const struct dirent *dir) ...@@ -16,35 +16,50 @@ static int filter(const struct dirent *dir)
return 1; return 1;
} }
int find_all_tid(int pid, pid_t ** all_tid) struct thread_map *thread_map__new_by_pid(pid_t pid)
{ {
struct thread_map *threads;
char name[256]; char name[256];
int items; int items;
struct dirent **namelist = NULL; struct dirent **namelist = NULL;
int ret = 0;
int i; int i;
sprintf(name, "/proc/%d/task", pid); sprintf(name, "/proc/%d/task", pid);
items = scandir(name, &namelist, filter, NULL); items = scandir(name, &namelist, filter, NULL);
if (items <= 0) if (items <= 0)
return -ENOENT; return NULL;
*all_tid = malloc(sizeof(pid_t) * items);
if (!*all_tid) {
ret = -ENOMEM;
goto failure;
}
for (i = 0; i < items; i++)
(*all_tid)[i] = atoi(namelist[i]->d_name);
ret = items; threads = malloc(sizeof(*threads) + sizeof(pid_t) * items);
if (threads != NULL) {
for (i = 0; i < items; i++)
threads->map[i] = atoi(namelist[i]->d_name);
threads->nr = items;
}
failure:
for (i=0; i<items; i++) for (i=0; i<items; i++)
free(namelist[i]); free(namelist[i]);
free(namelist); free(namelist);
return ret; return threads;
}
struct thread_map *thread_map__new_by_tid(pid_t tid)
{
struct thread_map *threads = malloc(sizeof(*threads) + sizeof(pid_t));
if (threads != NULL) {
threads->map[0] = tid;
threads->nr = 1;
}
return threads;
}
struct thread_map *thread_map__new(pid_t pid, pid_t tid)
{
if (pid != -1)
return thread_map__new_by_pid(pid);
return thread_map__new_by_tid(tid);
} }
static struct thread *thread__new(pid_t pid) static struct thread *thread__new(pid_t pid)
......
...@@ -18,11 +18,24 @@ struct thread { ...@@ -18,11 +18,24 @@ struct thread {
int comm_len; int comm_len;
}; };
struct thread_map {
int nr;
int map[];
};
struct perf_session; struct perf_session;
void thread__delete(struct thread *self); void thread__delete(struct thread *self);
int find_all_tid(int pid, pid_t ** all_tid); struct thread_map *thread_map__new_by_pid(pid_t pid);
struct thread_map *thread_map__new_by_tid(pid_t tid);
struct thread_map *thread_map__new(pid_t pid, pid_t tid);
static inline void thread_map__delete(struct thread_map *threads)
{
free(threads);
}
int thread__set_comm(struct thread *self, const char *comm); int thread__set_comm(struct thread *self, const char *comm);
int thread__comm_len(struct thread *self); int thread__comm_len(struct thread *self);
struct thread *perf_session__findnew(struct perf_session *self, pid_t pid); struct thread *perf_session__findnew(struct perf_session *self, pid_t pid);
......
...@@ -34,11 +34,13 @@ ...@@ -34,11 +34,13 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <linux/list.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include "../perf.h" #include "../perf.h"
#include "trace-event.h" #include "trace-event.h"
#include "debugfs.h" #include "debugfs.h"
#include "evsel.h"
#define VERSION "0.5" #define VERSION "0.5"
...@@ -469,16 +471,17 @@ static void read_ftrace_printk(void) ...@@ -469,16 +471,17 @@ static void read_ftrace_printk(void)
} }
static struct tracepoint_path * static struct tracepoint_path *
get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events) get_tracepoints_path(struct list_head *pattrs)
{ {
struct tracepoint_path path, *ppath = &path; struct tracepoint_path path, *ppath = &path;
int i, nr_tracepoints = 0; struct perf_evsel *pos;
int nr_tracepoints = 0;
for (i = 0; i < nb_events; i++) { list_for_each_entry(pos, pattrs, node) {
if (pattrs[i].type != PERF_TYPE_TRACEPOINT) if (pos->attr.type != PERF_TYPE_TRACEPOINT)
continue; continue;
++nr_tracepoints; ++nr_tracepoints;
ppath->next = tracepoint_id_to_path(pattrs[i].config); ppath->next = tracepoint_id_to_path(pos->attr.config);
if (!ppath->next) if (!ppath->next)
die("%s\n", "No memory to alloc tracepoints list"); die("%s\n", "No memory to alloc tracepoints list");
ppath = ppath->next; ppath = ppath->next;
...@@ -487,21 +490,21 @@ get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events) ...@@ -487,21 +490,21 @@ get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events)
return nr_tracepoints > 0 ? path.next : NULL; return nr_tracepoints > 0 ? path.next : NULL;
} }
bool have_tracepoints(struct perf_event_attr *pattrs, int nb_events) bool have_tracepoints(struct list_head *pattrs)
{ {
int i; struct perf_evsel *pos;
for (i = 0; i < nb_events; i++) list_for_each_entry(pos, pattrs, node)
if (pattrs[i].type == PERF_TYPE_TRACEPOINT) if (pos->attr.type == PERF_TYPE_TRACEPOINT)
return true; return true;
return false; return false;
} }
int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events) int read_tracing_data(int fd, struct list_head *pattrs)
{ {
char buf[BUFSIZ]; char buf[BUFSIZ];
struct tracepoint_path *tps = get_tracepoints_path(pattrs, nb_events); struct tracepoint_path *tps = get_tracepoints_path(pattrs);
/* /*
* What? No tracepoints? No sense writing anything here, bail out. * What? No tracepoints? No sense writing anything here, bail out.
...@@ -545,14 +548,13 @@ int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events) ...@@ -545,14 +548,13 @@ int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events)
return 0; return 0;
} }
ssize_t read_tracing_data_size(int fd, struct perf_event_attr *pattrs, ssize_t read_tracing_data_size(int fd, struct list_head *pattrs)
int nb_events)
{ {
ssize_t size; ssize_t size;
int err = 0; int err = 0;
calc_data_size = 1; calc_data_size = 1;
err = read_tracing_data(fd, pattrs, nb_events); err = read_tracing_data(fd, pattrs);
size = calc_data_size - 1; size = calc_data_size - 1;
calc_data_size = 0; calc_data_size = 0;
......
...@@ -262,9 +262,8 @@ raw_field_value(struct event *event, const char *name, void *data); ...@@ -262,9 +262,8 @@ raw_field_value(struct event *event, const char *name, void *data);
void *raw_field_ptr(struct event *event, const char *name, void *data); void *raw_field_ptr(struct event *event, const char *name, void *data);
unsigned long long eval_flag(const char *flag); unsigned long long eval_flag(const char *flag);
int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events); int read_tracing_data(int fd, struct list_head *pattrs);
ssize_t read_tracing_data_size(int fd, struct perf_event_attr *pattrs, ssize_t read_tracing_data_size(int fd, struct list_head *pattrs);
int nb_events);
/* taken from kernel/trace/trace.h */ /* taken from kernel/trace/trace.h */
enum trace_flag_type { enum trace_flag_type {
......
...@@ -114,3 +114,20 @@ unsigned long convert_unit(unsigned long value, char *unit) ...@@ -114,3 +114,20 @@ unsigned long convert_unit(unsigned long value, char *unit)
return value; return value;
} }
int readn(int fd, void *buf, size_t n)
{
void *buf_start = buf;
while (n) {
int ret = read(fd, buf, n);
if (ret <= 0)
return ret;
n -= ret;
buf += ret;
}
return buf - buf_start;
}
...@@ -265,6 +265,7 @@ void argv_free(char **argv); ...@@ -265,6 +265,7 @@ void argv_free(char **argv);
bool strglobmatch(const char *str, const char *pat); bool strglobmatch(const char *str, const char *pat);
bool strlazymatch(const char *str, const char *pat); bool strlazymatch(const char *str, const char *pat);
unsigned long convert_unit(unsigned long value, char *unit); unsigned long convert_unit(unsigned long value, char *unit);
int readn(int fd, void *buf, size_t size);
#define _STR(x) #x #define _STR(x) #x
#define STR(x) _STR(x) #define STR(x) _STR(x)
......
#include "xyarray.h"
#include "util.h"
struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size)
{
size_t row_size = ylen * entry_size;
struct xyarray *xy = zalloc(sizeof(*xy) + xlen * row_size);
if (xy != NULL) {
xy->entry_size = entry_size;
xy->row_size = row_size;
}
return xy;
}
void xyarray__delete(struct xyarray *xy)
{
free(xy);
}
#ifndef _PERF_XYARRAY_H_
#define _PERF_XYARRAY_H_ 1
#include <sys/types.h>
struct xyarray {
size_t row_size;
size_t entry_size;
char contents[];
};
struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size);
void xyarray__delete(struct xyarray *xy);
static inline void *xyarray__entry(struct xyarray *xy, int x, int y)
{
return &xy->contents[x * xy->row_size + y * xy->entry_size];
}
#endif /* _PERF_XYARRAY_H_ */
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