Skip to content

Commit 39922dc

Browse files
namhyungacmel
authored andcommitted
perf report: Add 'tgid' sort key
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>
1 parent b922881 commit 39922dc

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

tools/perf/Documentation/perf-report.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ OPTIONS
9494

9595
- comm: command (name) of the task which can be read via /proc/<pid>/comm
9696
- pid: command and tid of the task
97+
- tgid: command and tgid of the task
9798
- dso: name of library or module executed at the time of sample
9899
- dso_size: size of library or module executed at the time of sample
99100
- symbol: name of function executed at the time of sample

tools/perf/util/hist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ enum hist_column {
4242
HISTC_TIME,
4343
HISTC_DSO,
4444
HISTC_THREAD,
45+
HISTC_TGID,
4546
HISTC_COMM,
4647
HISTC_CGROUP_ID,
4748
HISTC_CGROUP,

tools/perf/util/sort.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,43 @@ struct sort_entry sort_thread = {
141141
.se_width_idx = HISTC_THREAD,
142142
};
143143

144+
/* --sort tgid */
145+
146+
static int64_t
147+
sort__tgid_cmp(struct hist_entry *left, struct hist_entry *right)
148+
{
149+
return thread__pid(right->thread) - thread__pid(left->thread);
150+
}
151+
152+
static int hist_entry__tgid_snprintf(struct hist_entry *he, char *bf,
153+
size_t size, unsigned int width)
154+
{
155+
int tgid = thread__pid(he->thread);
156+
const char *comm = NULL;
157+
158+
/* display comm of the thread-group leader */
159+
if (thread__pid(he->thread) == thread__tid(he->thread)) {
160+
comm = thread__comm_str(he->thread);
161+
} else {
162+
struct maps *maps = thread__maps(he->thread);
163+
struct thread *leader = machine__find_thread(maps__machine(maps),
164+
tgid, tgid);
165+
if (leader) {
166+
comm = thread__comm_str(leader);
167+
thread__put(leader);
168+
}
169+
}
170+
width = max(7U, width) - 8;
171+
return repsep_snprintf(bf, size, "%7d:%-*.*s", tgid, width, width, comm ?: "");
172+
}
173+
174+
struct sort_entry sort_tgid = {
175+
.se_header = " Tgid:Command",
176+
.se_cmp = sort__tgid_cmp,
177+
.se_snprintf = hist_entry__tgid_snprintf,
178+
.se_width_idx = HISTC_TGID,
179+
};
180+
144181
/* --sort simd */
145182

146183
static int64_t
@@ -2508,6 +2545,7 @@ static void sort_dimension_add_dynamic_header(struct sort_dimension *sd)
25082545

25092546
static struct sort_dimension common_sort_dimensions[] = {
25102547
DIM(SORT_PID, "pid", sort_thread),
2548+
DIM(SORT_TGID, "tgid", sort_tgid),
25112549
DIM(SORT_COMM, "comm", sort_comm),
25122550
DIM(SORT_DSO, "dso", sort_dso),
25132551
DIM(SORT_SYM, "symbol", sort_sym),

tools/perf/util/sort.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ enum sort_type {
7373
SORT_SYM_OFFSET,
7474
SORT_ANNOTATE_DATA_TYPE_CACHELINE,
7575
SORT_PARALLELISM,
76+
SORT_TGID,
7677

7778
/* branch stack specific sort keys */
7879
__SORT_BRANCH_STACK,

0 commit comments

Comments
 (0)