Skip to content

Commit

Permalink
Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux…
Browse files Browse the repository at this point in the history
…/kernel/git/acme/linux into perf/core

Pull perf/core trace improvements from Arnaldo Carvalho de Melo:

 * Don't stop synthesizing threads when one vanishes, this is for
   the existing threads when we start a tool like trace.

 * Use sched:sched_stat_runtime to provide a thread summary, this
   produces the same output as the 'trace summary' subcommand of
   tglx's original "trace" tool.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
  • Loading branch information
Ingo Molnar committed Oct 26, 2012
2 parents 6ca2a9c + 1302d88 commit 8f7c1d0
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 14 deletions.
3 changes: 3 additions & 0 deletions tools/perf/Documentation/perf-trace.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs.
--duration:
Show only events that had a duration greater than N.M ms.

--sched:
Accrue thread runtime and provide a summary at the end of the session.

SEE ALSO
--------
linkperf:perf-record[1], linkperf:perf-script[1]
108 changes: 101 additions & 7 deletions tools/perf/builtin-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ struct thread_trace {
u64 entry_time;
u64 exit_time;
bool entry_pending;
unsigned long nr_events;
char *entry_str;
double runtime_ms;
};

static struct thread_trace *thread_trace__new(void)
Expand All @@ -77,16 +79,21 @@ static struct thread_trace *thread_trace__new(void)

static struct thread_trace *thread__trace(struct thread *thread)
{
struct thread_trace *ttrace;

if (thread == NULL)
goto fail;

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

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

return thread->priv;
ttrace = thread->priv;
++ttrace->nr_events;

return ttrace;
fail:
color_fprintf(stdout, PERF_COLOR_RED,
"WARNING: not enough memory, dropping samples!\n");
Expand All @@ -102,8 +109,11 @@ struct trace {
struct perf_record_opts opts;
struct machine host;
u64 base_time;
unsigned long nr_events;
bool sched;
bool multiple_threads;
double duration_filter;
double runtime_ms;
};

static bool trace__filter_duration(struct trace *trace, double t)
Expand Down Expand Up @@ -382,11 +392,37 @@ static int trace__sys_exit(struct trace *trace, struct perf_evsel *evsel,
return 0;
}

static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evsel,
struct perf_sample *sample)
{
u64 runtime = perf_evsel__intval(evsel, sample, "runtime");
double runtime_ms = (double)runtime / NSEC_PER_MSEC;
struct thread *thread = machine__findnew_thread(&trace->host, sample->tid);
struct thread_trace *ttrace = thread__trace(thread);

if (ttrace == NULL)
goto out_dump;

ttrace->runtime_ms += runtime_ms;
trace->runtime_ms += runtime_ms;
return 0;

out_dump:
printf("%s: comm=%s,pid=%u,runtime=%" PRIu64 ",vruntime=%" PRIu64 ")\n",
evsel->name,
perf_evsel__strval(evsel, sample, "comm"),
(pid_t)perf_evsel__intval(evsel, sample, "pid"),
runtime,
perf_evsel__intval(evsel, sample, "vruntime"));
return 0;
}

static int trace__run(struct trace *trace, int argc, const char **argv)
{
struct perf_evlist *evlist = perf_evlist__new(NULL, NULL);
struct perf_evsel *evsel;
int err = -1, i, nr_events = 0, before;
int err = -1, i;
unsigned long before;
const bool forks = argc > 0;

if (evlist == NULL) {
Expand All @@ -400,6 +436,13 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
goto out_delete_evlist;
}

if (trace->sched &&
perf_evlist__add_newtp(evlist, "sched", "sched_stat_runtime",
trace__sched_stat_runtime)) {
printf("Couldn't read the sched_stat_runtime tracepoint information!\n");
goto out_delete_evlist;
}

err = perf_evlist__create_maps(evlist, &trace->opts.target);
if (err < 0) {
printf("Problems parsing the target to trace, check your options!\n");
Expand Down Expand Up @@ -444,7 +487,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)

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

for (i = 0; i < evlist->nr_mmaps; i++) {
union perf_event *event;
Expand All @@ -454,7 +497,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
tracepoint_handler handler;
struct perf_sample sample;

++nr_events;
++trace->nr_events;

err = perf_evlist__parse_sample(evlist, event, &sample);
if (err) {
Expand Down Expand Up @@ -495,7 +538,7 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
}
}

if (nr_events == before) {
if (trace->nr_events == before) {
if (done)
goto out_delete_evlist;

Expand All @@ -513,6 +556,51 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
return err;
}

static size_t trace__fprintf_threads_header(FILE *fp)
{
size_t printed;

printed = fprintf(fp, "\n _____________________________________________________________________\n");
printed += fprintf(fp," __) Summary of events (__\n\n");
printed += fprintf(fp," [ task - pid ] [ events ] [ ratio ] [ runtime ]\n");
printed += fprintf(fp," _____________________________________________________________________\n\n");

return printed;
}

static size_t trace__fprintf_thread_summary(struct trace *trace, FILE *fp)
{
size_t printed = trace__fprintf_threads_header(fp);
struct rb_node *nd;

for (nd = rb_first(&trace->host.threads); nd; nd = rb_next(nd)) {
struct thread *thread = rb_entry(nd, struct thread, rb_node);
struct thread_trace *ttrace = thread->priv;
const char *color;
double ratio;

if (ttrace == NULL)
continue;

ratio = (double)ttrace->nr_events / trace->nr_events * 100.0;

color = PERF_COLOR_NORMAL;
if (ratio > 50.0)
color = PERF_COLOR_RED;
else if (ratio > 25.0)
color = PERF_COLOR_GREEN;
else if (ratio > 5.0)
color = PERF_COLOR_YELLOW;

printed += color_fprintf(fp, color, "%20s", thread->comm);
printed += fprintf(fp, " - %-5d :%11lu [", thread->pid, ttrace->nr_events);
printed += color_fprintf(fp, color, "%5.1f%%", ratio);
printed += fprintf(fp, " ] %10.3f ms\n", ttrace->runtime_ms);
}

return printed;
}

static int trace__set_duration(const struct option *opt, const char *str,
int unset __maybe_unused)
{
Expand Down Expand Up @@ -563,6 +651,7 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
OPT_CALLBACK(0, "duration", &trace, "float",
"show only events with duration > N.M ms",
trace__set_duration),
OPT_BOOLEAN(0, "sched", &trace.sched, "show blocking scheduler events"),
OPT_END()
};
int err;
Expand All @@ -587,5 +676,10 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
if (!argc && perf_target__none(&trace.opts.target))
trace.opts.target.system_wide = true;

return trace__run(&trace, argc, argv);
err = trace__run(&trace, argc, argv);

if (trace.sched && !err)
trace__fprintf_thread_summary(&trace, stdout);

return err;
}
13 changes: 6 additions & 7 deletions tools/perf/util/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,16 +405,15 @@ int perf_event__synthesize_threads(struct perf_tool *tool,

if (*end) /* only interested in proper numerical dirents */
continue;

if (__event__synthesize_thread(comm_event, mmap_event, pid, 1,
process, tool, machine) != 0) {
err = -1;
goto out_closedir;
}
/*
* We may race with exiting thread, so don't stop just because
* one thread couldn't be synthesized.
*/
__event__synthesize_thread(comm_event, mmap_event, pid, 1,
process, tool, machine);
}

err = 0;
out_closedir:
closedir(proc);
out_free_mmap:
free(mmap_event);
Expand Down

0 comments on commit 8f7c1d0

Please sign in to comment.