* [PATCH v2] perf report: Add 'tgid' sort key
@ 2025-02-13 23:35 Namhyung Kim
0 siblings, 0 replies; 3+ messages in thread
From: Namhyung Kim @ 2025-02-13 23:35 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Ian Rogers, Kan Liang
Cc: Jiri Olsa, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
linux-perf-users, Stephane Eranian
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>
---
tools/perf/Documentation/perf-report.txt | 1 +
tools/perf/util/hist.h | 1 +
tools/perf/util/sort.c | 38 ++++++++++++++++++++++++
tools/perf/util/sort.h | 1 +
4 files changed, 41 insertions(+)
diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt
index 87f86451940623f3..4050ec4038425bf0 100644
--- a/tools/perf/Documentation/perf-report.txt
+++ b/tools/perf/Documentation/perf-report.txt
@@ -79,6 +79,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
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index 46c8373e314657fa..c164e178e0a48a8e 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -38,6 +38,7 @@ enum hist_column {
HISTC_TIME,
HISTC_DSO,
HISTC_THREAD,
+ HISTC_TGID,
HISTC_COMM,
HISTC_CGROUP_ID,
HISTC_CGROUP,
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index 3dd33721823f365d..b4034bd6629d4156 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -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
@@ -2501,6 +2538,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),
diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h
index a8572574e1686be6..6044eb1d61447c0d 100644
--- a/tools/perf/util/sort.h
+++ b/tools/perf/util/sort.h
@@ -72,6 +72,7 @@ enum sort_type {
SORT_ANNOTATE_DATA_TYPE_OFFSET,
SORT_SYM_OFFSET,
SORT_ANNOTATE_DATA_TYPE_CACHELINE,
+ SORT_TGID,
/* branch stack specific sort keys */
__SORT_BRANCH_STACK,
--
2.48.1.601.g30ceb7b040-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v2] perf report: Add 'tgid' sort key
@ 2025-05-09 21:04 Namhyung Kim
2025-05-13 20:52 ` Arnaldo Carvalho de Melo
0 siblings, 1 reply; 3+ messages in thread
From: Namhyung Kim @ 2025-05-09 21:04 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo, Ian Rogers, Kan Liang
Cc: Jiri Olsa, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
linux-perf-users, Stephane Eranian
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>
---
tools/perf/Documentation/perf-report.txt | 1 +
tools/perf/util/hist.h | 1 +
tools/perf/util/sort.c | 38 ++++++++++++++++++++++++
tools/perf/util/sort.h | 1 +
4 files changed, 41 insertions(+)
diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt
index 3376c471057506d9..acef3ff4178eff66 100644
--- a/tools/perf/Documentation/perf-report.txt
+++ b/tools/perf/Documentation/perf-report.txt
@@ -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
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index 355198fd70281f43..c64254088fc77246 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -42,6 +42,7 @@ enum hist_column {
HISTC_TIME,
HISTC_DSO,
HISTC_THREAD,
+ HISTC_TGID,
HISTC_COMM,
HISTC_CGROUP_ID,
HISTC_CGROUP,
diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index 8efafa7c10822ee9..45e6546539600a46 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -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),
diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h
index 6e92ac62b9c80a0b..a742ab7f3c677f48 100644
--- a/tools/perf/util/sort.h
+++ b/tools/perf/util/sort.h
@@ -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,
--
2.49.0.1015.ga840276032-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] perf report: Add 'tgid' sort key
2025-05-09 21:04 [PATCH v2] perf report: Add 'tgid' sort key Namhyung Kim
@ 2025-05-13 20:52 ` Arnaldo Carvalho de Melo
0 siblings, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2025-05-13 20:52 UTC (permalink / raw)
To: Namhyung Kim
Cc: Ian Rogers, Kan Liang, Jiri Olsa, Adrian Hunter, Peter Zijlstra,
Ingo Molnar, LKML, linux-perf-users, Stephane Eranian
On Fri, May 09, 2025 at 02:04:21PM -0700, Namhyung Kim wrote:
> 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>
Thanks, applied to perf-tools-next,
- Arnaldo
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
> tools/perf/Documentation/perf-report.txt | 1 +
> tools/perf/util/hist.h | 1 +
> tools/perf/util/sort.c | 38 ++++++++++++++++++++++++
> tools/perf/util/sort.h | 1 +
> 4 files changed, 41 insertions(+)
>
> diff --git a/tools/perf/Documentation/perf-report.txt b/tools/perf/Documentation/perf-report.txt
> index 3376c471057506d9..acef3ff4178eff66 100644
> --- a/tools/perf/Documentation/perf-report.txt
> +++ b/tools/perf/Documentation/perf-report.txt
> @@ -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
> diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
> index 355198fd70281f43..c64254088fc77246 100644
> --- a/tools/perf/util/hist.h
> +++ b/tools/perf/util/hist.h
> @@ -42,6 +42,7 @@ enum hist_column {
> HISTC_TIME,
> HISTC_DSO,
> HISTC_THREAD,
> + HISTC_TGID,
> HISTC_COMM,
> HISTC_CGROUP_ID,
> HISTC_CGROUP,
> diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
> index 8efafa7c10822ee9..45e6546539600a46 100644
> --- a/tools/perf/util/sort.c
> +++ b/tools/perf/util/sort.c
> @@ -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),
> diff --git a/tools/perf/util/sort.h b/tools/perf/util/sort.h
> index 6e92ac62b9c80a0b..a742ab7f3c677f48 100644
> --- a/tools/perf/util/sort.h
> +++ b/tools/perf/util/sort.h
> @@ -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,
> --
> 2.49.0.1015.ga840276032-goog
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-05-13 20:52 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-09 21:04 [PATCH v2] perf report: Add 'tgid' sort key Namhyung Kim
2025-05-13 20:52 ` Arnaldo Carvalho de Melo
-- strict thread matches above, loose matches on Subject: below --
2025-02-13 23:35 Namhyung Kim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).