perf report: Add 'tgid' sort key
authorNamhyung Kim <namhyung@kernel.org>
Fri, 9 May 2025 21:04:21 +0000 (14:04 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 13 May 2025 20:51:32 +0000 (17:51 -0300)
Sometimes we need to analyze the data in process level but current sort
keys only work on thread level.  Let's add 'tgid' sort key for that as
'pid' is already taken for thread.

This will look mostly the same, but it only uses tgid instead of tid.
Here's an example of a process with two threads (thloop).

  $ perf record -- perf test -w thloop

  $ perf report --stdio -s tgid,pid -H
  ...
  #
  #    Overhead  Tgid:Command / Pid:Command
  # ...........  ..........................
  #
     100.00%     2018407:perf
         50.34%     2018407:perf
         49.66%     2018409:perf

Suggested-by: Stephane Eranian <eranian@google.com>
Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: https://lore.kernel.org/r/20250509210421.197245-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/Documentation/perf-report.txt
tools/perf/util/hist.h
tools/perf/util/sort.c
tools/perf/util/sort.h

index 3376c471057506d98c0efb41373bec90bea790a5..acef3ff4178eff66e8f876ae16cdac7b1387f07b 100644 (file)
@@ -94,6 +94,7 @@ OPTIONS
 
        - comm: command (name) of the task which can be read via /proc/<pid>/comm
        - pid: command and tid of the task
+       - tgid: command and tgid of the task
        - dso: name of library or module executed at the time of sample
        - dso_size: size of library or module executed at the time of sample
        - symbol: name of function executed at the time of sample
index 355198fd70281f4364c69d56386476c0a0130c12..c64254088fc77246318dc72f0858942d470a0e87 100644 (file)
@@ -42,6 +42,7 @@ enum hist_column {
        HISTC_TIME,
        HISTC_DSO,
        HISTC_THREAD,
+       HISTC_TGID,
        HISTC_COMM,
        HISTC_CGROUP_ID,
        HISTC_CGROUP,
index 8efafa7c10822ee9c2d541ddd1e2ff8e891ff460..45e6546539600a46702e00e0e048d66d4c91d519 100644 (file)
@@ -141,6 +141,43 @@ struct sort_entry sort_thread = {
        .se_width_idx   = HISTC_THREAD,
 };
 
+/* --sort tgid */
+
+static int64_t
+sort__tgid_cmp(struct hist_entry *left, struct hist_entry *right)
+{
+       return thread__pid(right->thread) - thread__pid(left->thread);
+}
+
+static int hist_entry__tgid_snprintf(struct hist_entry *he, char *bf,
+                                      size_t size, unsigned int width)
+{
+       int tgid = thread__pid(he->thread);
+       const char *comm = NULL;
+
+       /* display comm of the thread-group leader */
+       if (thread__pid(he->thread) == thread__tid(he->thread)) {
+               comm = thread__comm_str(he->thread);
+       } else {
+               struct maps *maps = thread__maps(he->thread);
+               struct thread *leader = machine__find_thread(maps__machine(maps),
+                                                            tgid, tgid);
+               if (leader) {
+                       comm = thread__comm_str(leader);
+                       thread__put(leader);
+               }
+       }
+       width = max(7U, width) - 8;
+       return repsep_snprintf(bf, size, "%7d:%-*.*s", tgid, width, width, comm ?: "");
+}
+
+struct sort_entry sort_tgid = {
+       .se_header      = "   Tgid:Command",
+       .se_cmp         = sort__tgid_cmp,
+       .se_snprintf    = hist_entry__tgid_snprintf,
+       .se_width_idx   = HISTC_TGID,
+};
+
 /* --sort simd */
 
 static int64_t
@@ -2508,6 +2545,7 @@ static void sort_dimension_add_dynamic_header(struct sort_dimension *sd)
 
 static struct sort_dimension common_sort_dimensions[] = {
        DIM(SORT_PID, "pid", sort_thread),
+       DIM(SORT_TGID, "tgid", sort_tgid),
        DIM(SORT_COMM, "comm", sort_comm),
        DIM(SORT_DSO, "dso", sort_dso),
        DIM(SORT_SYM, "symbol", sort_sym),
index 6e92ac62b9c80a0bdc9accea8285d70c730b4828..a742ab7f3c677f48f2043d8923f20f1cf1dbbac7 100644 (file)
@@ -73,6 +73,7 @@ enum sort_type {
        SORT_SYM_OFFSET,
        SORT_ANNOTATE_DATA_TYPE_CACHELINE,
        SORT_PARALLELISM,
+       SORT_TGID,
 
        /* branch stack specific sort keys */
        __SORT_BRANCH_STACK,