Skip to content

Commit

Permalink
perf trace: Ignore thread hashing in summary
Browse files Browse the repository at this point in the history
Commit 91e467b ("perf machine: Use hashtable for machine
threads") made the iteration of thread tids unordered. The perf trace
--summary output sorts and prints each hash bucket, rather than all
threads globally. Change this behavior by turn all threads into a
list, sort the list by number of trace events then by tids, finally
print the list. This also allows the rbtree in threads to be not
accessed outside of machine.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240301053646.1449657-3-irogers@google.com
  • Loading branch information
Ian Rogers authored and Namhyung Kim committed Mar 4, 2024
1 parent 2f1e20f commit f178ffd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
41 changes: 23 additions & 18 deletions tools/perf/builtin-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
#include <linux/err.h>
#include <linux/filter.h>
#include <linux/kernel.h>
#include <linux/list_sort.h>
#include <linux/random.h>
#include <linux/stringify.h>
#include <linux/time64.h>
Expand Down Expand Up @@ -4312,34 +4313,38 @@ static unsigned long thread__nr_events(struct thread_trace *ttrace)
return ttrace ? ttrace->nr_events : 0;
}

DEFINE_RESORT_RB(threads,
(thread__nr_events(thread__priv(a->thread)) <
thread__nr_events(thread__priv(b->thread))),
struct thread *thread;
)
static int trace_nr_events_cmp(void *priv __maybe_unused,
const struct list_head *la,
const struct list_head *lb)
{
entry->thread = rb_entry(nd, struct thread_rb_node, rb_node)->thread;
struct thread_list *a = list_entry(la, struct thread_list, list);
struct thread_list *b = list_entry(lb, struct thread_list, list);
unsigned long a_nr_events = thread__nr_events(thread__priv(a->thread));
unsigned long b_nr_events = thread__nr_events(thread__priv(b->thread));

if (a_nr_events != b_nr_events)
return a_nr_events < b_nr_events ? -1 : 1;

/* Identical number of threads, place smaller tids first. */
return thread__tid(a->thread) < thread__tid(b->thread)
? -1
: (thread__tid(a->thread) > thread__tid(b->thread) ? 1 : 0);
}

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

for (i = 0; i < THREADS__TABLE_SIZE; i++) {
DECLARE_RESORT_RB_MACHINE_THREADS(threads, trace->host, i);
LIST_HEAD(threads);

if (threads == NULL) {
fprintf(fp, "%s", "Error sorting output by nr_events!\n");
return 0;
}
if (machine__thread_list(trace->host, &threads) == 0) {
struct thread_list *pos;

resort_rb__for_each_entry(nd, threads)
printed += trace__fprintf_thread(fp, threads_entry->thread, trace);
list_sort(NULL, &threads, trace_nr_events_cmp);

resort_rb__delete(threads);
list_for_each_entry(pos, &threads, list)
printed += trace__fprintf_thread(fp, pos->thread, trace);
}
thread_list__delete(&threads);
return printed;
}

Expand Down
5 changes: 0 additions & 5 deletions tools/perf/util/rb_resort.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,4 @@ struct __name##_sorted *__name = __name##_sorted__new
DECLARE_RESORT_RB(__name)(&__ilist->rblist.entries.rb_root, \
__ilist->rblist.nr_entries)

/* For 'struct machine->threads' */
#define DECLARE_RESORT_RB_MACHINE_THREADS(__name, __machine, hash_bucket) \
DECLARE_RESORT_RB(__name)(&__machine->threads[hash_bucket].entries.rb_root, \
__machine->threads[hash_bucket].nr)

#endif /* _PERF_RESORT_RB_H_ */

0 comments on commit f178ffd

Please sign in to comment.