Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 338892
b: refs/heads/master
c: 752fde4
h: refs/heads/master
v: v3
  • Loading branch information
Arnaldo Carvalho de Melo committed Oct 24, 2012
1 parent 4f31e63 commit 1881c67
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 23 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 9a8e85ad0b61ec0720df7d24392163c2a12626d0
refs/heads/master: 752fde44fd1c4a411d709c7d4ad0f121f427f234
199 changes: 177 additions & 22 deletions trunk/tools/perf/builtin-trace.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "builtin.h"
#include "util/color.h"
#include "util/evlist.h"
#include "util/machine.h"
#include "util/thread.h"
#include "util/parse-options.h"
#include "util/thread_map.h"
#include "event-parse.h"
Expand Down Expand Up @@ -43,22 +46,123 @@ struct syscall {
struct syscall_fmt *fmt;
};

struct thread_trace {
u64 entry_time;
u64 exit_time;
bool entry_pending;
char *entry_str;
};

static struct thread_trace *thread_trace__new(void)
{
return zalloc(sizeof(struct thread_trace));
}

static struct thread_trace *thread__trace(struct thread *thread)
{
if (thread == NULL)
goto fail;

if (thread->priv == NULL)
thread->priv = thread_trace__new();

if (thread->priv == NULL)
goto fail;

return thread->priv;
fail:
color_fprintf(stdout, PERF_COLOR_RED,
"WARNING: not enough memory, dropping samples!\n");
return NULL;
}

struct trace {
int audit_machine;
struct {
int max;
struct syscall *table;
} syscalls;
struct perf_record_opts opts;
struct machine host;
u64 base_time;
bool multiple_threads;
};

static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp)
{
double ts = (double)(tstamp - trace->base_time) / NSEC_PER_MSEC;

return fprintf(fp, "%10.3f: ", ts);
}

static bool done = false;

static void sig_handler(int sig __maybe_unused)
{
done = true;
}

static size_t trace__fprintf_entry_head(struct trace *trace, struct thread *thread,
u64 tstamp, FILE *fp)
{
size_t printed = trace__fprintf_tstamp(trace, tstamp, fp);

if (trace->multiple_threads)
printed += fprintf(fp, "%d ", thread->pid);

return printed;
}

static int trace__process_event(struct machine *machine, union perf_event *event)
{
int ret = 0;

switch (event->header.type) {
case PERF_RECORD_LOST:
color_fprintf(stdout, PERF_COLOR_RED,
"LOST %" PRIu64 " events!\n", event->lost.lost);
ret = machine__process_lost_event(machine, event);
default:
ret = machine__process_event(machine, event);
break;
}

return ret;
}

static int trace__tool_process(struct perf_tool *tool __maybe_unused,
union perf_event *event,
struct perf_sample *sample __maybe_unused,
struct machine *machine)
{
return trace__process_event(machine, event);
}

static int trace__symbols_init(struct trace *trace, struct perf_evlist *evlist)
{
int err = symbol__init();

if (err)
return err;

machine__init(&trace->host, "", HOST_KERNEL_ID);
machine__create_kernel_maps(&trace->host);

if (perf_target__has_task(&trace->opts.target)) {
err = perf_event__synthesize_thread_map(NULL, evlist->threads,
trace__tool_process,
&trace->host);
} else {
err = perf_event__synthesize_threads(NULL, trace__tool_process,
&trace->host);
}

if (err)
symbol__exit();

return err;
}

static int trace__read_syscall_info(struct trace *trace, int id)
{
char tp_name[128];
Expand Down Expand Up @@ -100,7 +204,8 @@ static int trace__read_syscall_info(struct trace *trace, int id)
return sc->tp_format != NULL ? 0 : -1;
}

static size_t syscall__fprintf_args(struct syscall *sc, unsigned long *args, FILE *fp)
static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
unsigned long *args)
{
int i = 0;
size_t printed = 0;
Expand All @@ -109,12 +214,15 @@ static size_t syscall__fprintf_args(struct syscall *sc, unsigned long *args, FIL
struct format_field *field;

for (field = sc->tp_format->format.fields->next; field; field = field->next) {
printed += fprintf(fp, "%s%s: %ld", printed ? ", " : "",
field->name, args[i++]);
printed += scnprintf(bf + printed, size - printed,
"%s%s: %ld", printed ? ", " : "",
field->name, args[i++]);
}
} else {
while (i < 6) {
printed += fprintf(fp, "%sarg%d: %ld", printed ? ", " : "", i, args[i]);
printed += scnprintf(bf + printed, size - printed,
"%sarg%d: %ld",
printed ? ", " : "", i, args[i]);
++i;
}
}
Expand Down Expand Up @@ -153,10 +261,14 @@ static struct syscall *trace__syscall_info(struct trace *trace,
static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
struct perf_sample *sample)
{
char *msg;
void *args;
size_t printed = 0;
struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
struct syscall *sc = trace__syscall_info(trace, evsel, sample);
struct thread_trace *ttrace = thread__trace(thread);

if (sc == NULL)
if (ttrace == NULL || sc == NULL)
return -1;

args = perf_evsel__rawptr(evsel, sample, "args");
Expand All @@ -165,8 +277,25 @@ static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
return -1;
}

printf("%s(", sc->name);
syscall__fprintf_args(sc, args, stdout);
ttrace = thread->priv;

if (ttrace->entry_str == NULL) {
ttrace->entry_str = malloc(1024);
if (!ttrace->entry_str)
return -1;
}

ttrace->entry_time = sample->time;
msg = ttrace->entry_str;
printed += scnprintf(msg + printed, 1024 - printed, "%s(", sc->name);

printed += syscall__scnprintf_args(sc, msg + printed, 1024 - printed, args);

if (!strcmp(sc->name, "exit_group") || !strcmp(sc->name, "exit")) {
trace__fprintf_entry_head(trace, thread, sample->time, stdout);
printf("%-70s\n", ttrace->entry_str);
} else
ttrace->entry_pending = true;

return 0;
}
Expand All @@ -175,13 +304,29 @@ static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
struct perf_sample *sample)
{
int ret;
struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
struct thread_trace *ttrace = thread__trace(thread);
struct syscall *sc = trace__syscall_info(trace, evsel, sample);

if (sc == NULL)
if (ttrace == NULL || sc == NULL)
return -1;

ret = perf_evsel__intval(evsel, sample, "ret");

ttrace = thread->priv;

ttrace->exit_time = sample->time;

trace__fprintf_entry_head(trace, thread, sample->time, stdout);

if (ttrace->entry_pending) {
printf("%-70s", ttrace->entry_str);
} else {
printf(" ... [");
color_fprintf(stdout, PERF_COLOR_YELLOW, "continued");
printf("]: %s()", sc->name);
}

if (ret < 0 && sc->fmt && sc->fmt->errmsg) {
char bf[256];
const char *emsg = strerror_r(-ret, bf, sizeof(bf)),
Expand All @@ -194,6 +339,9 @@ static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
printf(") = %d", ret);

putchar('\n');

ttrace->entry_pending = false;

return 0;
}

Expand Down Expand Up @@ -221,6 +369,12 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
goto out_delete_evlist;
}

err = trace__symbols_init(trace, evlist);
if (err < 0) {
printf("Problems initializing symbol libraries!\n");
goto out_delete_evlist;
}

perf_evlist__config_attrs(evlist, &trace->opts);

signal(SIGCHLD, sig_handler);
Expand Down Expand Up @@ -251,6 +405,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
if (forks)
perf_evlist__start_workload(evlist);

trace->multiple_threads = evlist->threads->map[0] == -1 || evlist->threads->nr > 1;
again:
before = nr_events;

Expand All @@ -264,32 +419,32 @@ static int trace__run(struct trace *trace, int argc, const char **argv)

++nr_events;

switch (type) {
case PERF_RECORD_SAMPLE:
break;
case PERF_RECORD_LOST:
printf("LOST %" PRIu64 " events!\n", event->lost.lost);
continue;
default:
printf("Unexpected %s event, skipping...\n",
perf_event__name(type));
continue;
}

err = perf_evlist__parse_sample(evlist, event, &sample);
if (err) {
printf("Can't parse sample, err = %d, skipping...\n", err);
continue;
}

if (trace->base_time == 0)
trace->base_time = sample.time;

if (type != PERF_RECORD_SAMPLE) {
trace__process_event(&trace->host, event);
continue;
}

evsel = perf_evlist__id2evsel(evlist, sample.id);
if (evsel == NULL) {
printf("Unknown tp ID %" PRIu64 ", skipping...\n", sample.id);
continue;
}

if (evlist->threads->map[0] == -1 || evlist->threads->nr > 1)
printf("%d ", sample.tid);
if (sample.raw_data == NULL) {
printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
perf_evsel__name(evsel), sample.tid,
sample.cpu, sample.raw_size);
continue;
}

if (sample.raw_data == NULL) {
printf("%s sample with no payload for tid: %d, cpu %d, raw_size=%d, skipping...\n",
Expand Down
4 changes: 4 additions & 0 deletions trunk/tools/perf/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ static inline int has_extension(const char *filename, const char *ext)
#undef tolower
#undef toupper

#ifndef NSEC_PER_MSEC
#define NSEC_PER_MSEC 1000000L
#endif

extern unsigned char sane_ctype[256];
#define GIT_SPACE 0x01
#define GIT_DIGIT 0x02
Expand Down

0 comments on commit 1881c67

Please sign in to comment.