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
LIB_H += util/debug.h
LIB_H += util/debugfs.h
LIB_H += util/event.h
LIB_H += util/evsel.h
LIB_H += util/exec_cmd.h
LIB_H += util/types.h
LIB_H += util/levenshtein.h
......@@ -404,6 +405,7 @@ LIB_H += util/parse-options.h
LIB_H += util/parse-events.h
LIB_H += util/quote.h
LIB_H += util/util.h
LIB_H += util/xyarray.h
LIB_H += util/header.h
LIB_H += util/help.h
LIB_H += util/session.h
......@@ -433,6 +435,7 @@ LIB_OBJS += $(OUTPUT)util/ctype.o
LIB_OBJS += $(OUTPUT)util/debugfs.o
LIB_OBJS += $(OUTPUT)util/environment.o
LIB_OBJS += $(OUTPUT)util/event.o
LIB_OBJS += $(OUTPUT)util/evsel.o
LIB_OBJS += $(OUTPUT)util/exec_cmd.o
LIB_OBJS += $(OUTPUT)util/help.o
LIB_OBJS += $(OUTPUT)util/levenshtein.o
......@@ -470,6 +473,7 @@ LIB_OBJS += $(OUTPUT)util/sort.o
LIB_OBJS += $(OUTPUT)util/hist.o
LIB_OBJS += $(OUTPUT)util/probe-event.o
LIB_OBJS += $(OUTPUT)util/util.o
LIB_OBJS += $(OUTPUT)util/xyarray.o
LIB_OBJS += $(OUTPUT)util/cpumap.o
BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o
......
This diff is collapsed.
This diff is collapsed.
......@@ -234,6 +234,85 @@ static int test__vmlinux_matches_kallsyms(void)
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 {
const char *desc;
int (*func)(void);
......@@ -242,6 +321,10 @@ static struct test {
.desc = "vmlinux symtab matches kallsyms",
.func = test__vmlinux_matches_kallsyms,
},
{
.desc = "detect open syscall event",
.func = test__open_syscall_event,
},
{
.func = NULL,
},
......
This diff is collapsed.
......@@ -286,6 +286,8 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
status = p->fn(argc, argv, prefix);
exit_browser(status);
perf_evsel_list__delete();
if (status)
return status & 0xff;
......
......@@ -4,32 +4,53 @@
#include <assert.h>
#include <stdio.h>
int cpumap[MAX_NR_CPUS];
static int default_cpu_map(void)
static struct cpu_map *cpu_map__default_new(void)
{
int nr_cpus, i;
struct cpu_map *cpus;
int nr_cpus;
nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
assert(nr_cpus <= MAX_NR_CPUS);
assert((int)nr_cpus >= 0);
if (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)
cpumap[i] = i;
cpus->nr = nr_cpus;
}
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;
int nr_cpus = 0;
int *tmp_cpus = NULL, *tmp;
int max_entries = 0;
int n, cpu, prev;
char sep;
onlnf = fopen("/sys/devices/system/cpu/online", "r");
if (!onlnf)
return default_cpu_map();
return cpu_map__default_new();
sep = 0;
prev = -1;
......@@ -38,12 +59,28 @@ static int read_all_cpu_map(void)
if (n <= 0)
break;
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)
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 == '-')
prev = cpu;
else
......@@ -51,24 +88,31 @@ static int read_all_cpu_map(void)
if (n == 1 || sep == '\n')
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;
char *p = NULL;
int i, nr_cpus = 0;
int *tmp_cpus = NULL, *tmp;
int max_entries = 0;
if (!cpu_list)
return read_all_cpu_map();
return cpu_map__read_all_cpu_map();
if (!isdigit(*cpu_list))
goto invalid;
goto out;
while (isdigit(*cpu_list)) {
p = NULL;
......@@ -94,21 +138,42 @@ int read_cpu_map(const char *cpu_list)
for (; start_cpu <= end_cpu; start_cpu++) {
/* check for duplicates */
for (i = 0; i < nr_cpus; i++)
if (cpumap[i] == (int)start_cpu)
if (tmp_cpus[i] == (int)start_cpu)
goto invalid;
assert(nr_cpus < MAX_NR_CPUS);
cpumap[nr_cpus++] = (int)start_cpu;
if (nr_cpus == max_entries) {
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)
++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:
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
#define __PERF_CPUMAP_H
extern int read_cpu_map(const char *cpu_list);
extern int cpumap[];
struct cpu_map {
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 */
#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)
/* Write trace info */
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;
}
......@@ -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,
int fd, void *buf, size_t size)
{
if (do_read(fd, buf, size) <= 0)
if (readn(fd, buf, size) <= 0)
return -1;
if (self->needs_swap)
......@@ -662,7 +662,7 @@ int perf_file_header__read(struct perf_file_header *self,
{
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)))
return -1;
......@@ -823,7 +823,7 @@ static int perf_file_header__read_pipe(struct perf_pipe_file_header *self,
struct perf_header *ph, int fd,
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)))
return -1;
......@@ -1133,8 +1133,7 @@ int event__process_event_type(event_t *self,
return 0;
}
int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
int nb_events,
int event__synthesize_tracing_data(int fd, struct list_head *pattrs,
event__handler_t process,
struct perf_session *session __unused)
{
......@@ -1145,7 +1144,7 @@ int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
memset(&ev, 0, sizeof(ev));
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)
return size;
aligned_size = ALIGN(size, sizeof(u64));
......@@ -1155,7 +1154,7 @@ int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
process(&ev, NULL, session);
err = read_tracing_data(fd, pattrs, nb_events);
err = read_tracing_data(fd, pattrs);
write_padded(fd, NULL, 0, padding);
return aligned_size;
......
......@@ -113,8 +113,7 @@ int event__synthesize_event_types(event__handler_t process,
int event__process_event_type(event_t *self,
struct perf_session *session);
int event__synthesize_tracing_data(int fd, struct perf_event_attr *pattrs,
int nb_events,
int event__synthesize_tracing_data(int fd, struct list_head *pattrs,
event__handler_t process,
struct perf_session *session);
int event__process_tracing_data(event_t *self,
......
#include "../../../include/linux/hw_breakpoint.h"
#include "util.h"
#include "../perf.h"
#include "evsel.h"
#include "parse-options.h"
#include "parse-events.h"
#include "exec_cmd.h"
......@@ -12,8 +13,7 @@
int nr_counters;
struct perf_event_attr attrs[MAX_COUNTERS];
char *filters[MAX_COUNTERS];
LIST_HEAD(evsel_list);
struct event_symbol {
u8 type;
......@@ -266,10 +266,10 @@ static char *event_cache_name(u8 cache_type, u8 cache_op, u8 cache_result)
return name;
}
const char *event_name(int counter)
const char *event_name(struct perf_evsel *evsel)
{
u64 config = attrs[counter].config;
int type = attrs[counter].type;
u64 config = evsel->attr.config;
int type = evsel->attr.type;
return __event_name(type, config);
}
......@@ -814,9 +814,6 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u
return -1;
for (;;) {
if (nr_counters == MAX_COUNTERS)
return -1;
memset(&attr, 0, sizeof(attr));
ret = parse_event_symbols(&str, &attr);
if (ret == EVT_FAILED)
......@@ -826,8 +823,13 @@ int parse_events(const struct option *opt __used, const char *str, int unset __u
return -1;
if (ret != EVT_HANDLED_ALL) {
attrs[nr_counters] = attr;
nr_counters++;
struct perf_evsel *evsel;
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)
......@@ -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 unset __used)
{
int i = nr_counters - 1;
int len = strlen(str);
struct perf_evsel *last = NULL;
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,
"-F option should follow a -e tracepoint option\n");
return -1;
}
filters[i] = malloc(len + 1);
if (!filters[i]) {
last->filter = strdup(str);
if (last->filter == NULL) {
fprintf(stderr, "not enough memory to hold filter string\n");
return -1;
}
strcpy(filters[i], str);
return 0;
}
......@@ -967,3 +970,26 @@ void print_events(void)
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 @@
* 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 tracepoint_path {
......@@ -13,14 +23,11 @@ struct tracepoint_path {
};
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 struct perf_event_attr attrs[MAX_COUNTERS];
extern char *filters[MAX_COUNTERS];
extern const char *event_name(int ctr);
const char *event_name(struct perf_evsel *event);
extern const char *__event_name(int type, u64 config);
extern int parse_events(const struct option *opt, const char *str, int unset);
......@@ -33,5 +40,4 @@ extern void print_events(void);
extern char debugfs_path[];
extern int valid_debugfs_mount(const char *debugfs);
#endif /* __PERF_PARSE_EVENTS_H */
......@@ -838,23 +838,6 @@ static struct thread *perf_session__register_idle_thread(struct perf_session *se
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))
volatile int session_done;
......@@ -872,7 +855,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
head = 0;
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)
goto done;
......@@ -892,8 +875,7 @@ static int __perf_session__process_pipe_events(struct perf_session *self,
p += sizeof(struct perf_event_header);
if (size - sizeof(struct perf_event_header)) {
err = do_read(self->fd, p,
size - sizeof(struct perf_event_header));
err = readn(self->fd, p, size - sizeof(struct perf_event_header));
if (err <= 0) {
if (err == 0) {
pr_err("unexpected end of event stream\n");
......
......@@ -109,7 +109,6 @@ void mem_bswap_64(void *src, int byte_size);
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__set_sample_id_all(struct perf_session *session, bool value);
void perf_session__set_sample_type(struct perf_session *session, u64 type);
......
......@@ -16,35 +16,50 @@ static int filter(const struct dirent *dir)
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];
int items;
struct dirent **namelist = NULL;
int ret = 0;
int i;
sprintf(name, "/proc/%d/task", pid);
items = scandir(name, &namelist, filter, NULL);
if (items <= 0)
return -ENOENT;
*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);
return NULL;
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++)
free(namelist[i]);
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)
......
......@@ -18,11 +18,24 @@ struct thread {
int comm_len;
};
struct thread_map {
int nr;
int map[];
};
struct perf_session;
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__comm_len(struct thread *self);
struct thread *perf_session__findnew(struct perf_session *self, pid_t pid);
......
......@@ -34,11 +34,13 @@
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <linux/list.h>
#include <linux/kernel.h>
#include "../perf.h"
#include "trace-event.h"
#include "debugfs.h"
#include "evsel.h"
#define VERSION "0.5"
......@@ -469,16 +471,17 @@ static void read_ftrace_printk(void)
}
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;
int i, nr_tracepoints = 0;
struct perf_evsel *pos;
int nr_tracepoints = 0;
for (i = 0; i < nb_events; i++) {
if (pattrs[i].type != PERF_TYPE_TRACEPOINT)
list_for_each_entry(pos, pattrs, node) {
if (pos->attr.type != PERF_TYPE_TRACEPOINT)
continue;
++nr_tracepoints;
ppath->next = tracepoint_id_to_path(pattrs[i].config);
ppath->next = tracepoint_id_to_path(pos->attr.config);
if (!ppath->next)
die("%s\n", "No memory to alloc tracepoints list");
ppath = ppath->next;
......@@ -487,21 +490,21 @@ get_tracepoints_path(struct perf_event_attr *pattrs, int nb_events)
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++)
if (pattrs[i].type == PERF_TYPE_TRACEPOINT)
list_for_each_entry(pos, pattrs, node)
if (pos->attr.type == PERF_TYPE_TRACEPOINT)
return true;
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];
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.
......@@ -545,14 +548,13 @@ int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events)
return 0;
}
ssize_t read_tracing_data_size(int fd, struct perf_event_attr *pattrs,
int nb_events)
ssize_t read_tracing_data_size(int fd, struct list_head *pattrs)
{
ssize_t size;
int err = 0;
calc_data_size = 1;
err = read_tracing_data(fd, pattrs, nb_events);
err = read_tracing_data(fd, pattrs);
size = calc_data_size - 1;
calc_data_size = 0;
......
......@@ -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);
unsigned long long eval_flag(const char *flag);
int read_tracing_data(int fd, struct perf_event_attr *pattrs, int nb_events);
ssize_t read_tracing_data_size(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 list_head *pattrs);
/* taken from kernel/trace/trace.h */
enum trace_flag_type {
......
......@@ -114,3 +114,20 @@ unsigned long convert_unit(unsigned long value, char *unit)
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);
bool strglobmatch(const char *str, const char *pat);
bool strlazymatch(const char *str, const char *pat);
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) _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