Skip to contentSkip to conversation

perf ftrace: Check min/max latency only with bucket range

PATCH In review Namhyung Kim 3 participants 10 messages Jan 8, 2025 – Jan 10, 2025 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf ftrace: Check min/max latency only with bucket range

It's an optional feature and remains 0 when bucket range is not given. And it makes the histogram goes to the last entry always because any latency (num) is greater than or equal to 0.

Before:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    0 us |          0 |                                                |
       1 -    2 us |          0 |                                                |
       2 -    4 us |          0 |                                                |
       4 -    8 us |          0 |                                                |
       8 -   16 us |          0 |                                                |
      16 -   32 us |          0 |                                                |
      32 -   64 us |          0 |                                                |
      64 -  128 us |          0 |                                                |
     128 -  256 us |          0 |                                                |
     256 -  512 us |          0 |                                                |
     512 - 1024 us |          0 |                                                |
       1 -    2 ms |          0 |                                                |
       2 -    4 ms |          0 |                                                |
       4 -    8 ms |          0 |                                                |
       8 -   16 ms |          0 |                                                |
      16 -   32 ms |          0 |                                                |
      32 -   64 ms |          0 |                                                |
      64 -  128 ms |          0 |                                                |
     128 -  256 ms |          0 |                                                |
     256 -  512 ms |          0 |                                                |
     512 - 1024 ms |          0 |                                                |
       1 - ...  s  |       1353 | ############################################## |

After:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    0 us |        321 | ###########                                    |
       1 -    2 us |        132 | ####                                           |
       2 -    4 us |        202 | #######                                        |
       4 -    8 us |        188 | ######                                         |
       8 -   16 us |         16 |                                                |
      16 -   32 us |         12 |                                                |
      32 -   64 us |         30 | #                                              |
      64 -  128 us |         98 | ###                                            |
     128 -  256 us |         53 | #                                              |
     256 -  512 us |         57 | ##                                             |
     512 - 1024 us |          9 |                                                |
       1 -    2 ms |          9 |                                                |
       2 -    4 ms |          1 |                                                |
       4 -    8 ms |         98 | ###                                            |
       8 -   16 ms |          5 |                                                |
      16 -   32 ms |          7 |                                                |
      32 -   64 ms |         32 | #                                              |
      64 -  128 ms |         10 |                                                |
     128 -  256 ms |         10 |                                                |
     256 -  512 ms |          2 |                                                |
     512 - 1024 ms |          0 |                                                |
       1 - ...  s  |          0 |                                                |
Fixes 690a052a6d85c530 ("perf ftrace latency: Add --max-latency option")
Cc Gabriele Monaco <[email protected]>
---
 tools/perf/builtin-ftrace.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-ftrace.c +5 -3
index 08c1cc429b27e169..90cf2c9915a7e35b 100644
@@ -796,8 +796,10 @@ static void make_histogram(struct perf_ftrace *ftrace, int buckets[],
 			// than the min latency desired.
 			if (num > 0) // 1st entry: [ 1 unit .. bucket_range units ]
 				i = num / ftrace->bucket_range + 1;
+			if (num >= max_latency - min_latency)
+				i = NUM_BUCKET -1;
 		}
-		if (i >= NUM_BUCKET || num >= max_latency - min_latency)
+		if (i >= NUM_BUCKET)
 			i = NUM_BUCKET - 1;
 
 		num += min_latency;
@@ -1738,7 +1740,7 @@ int cmd_ftrace(int argc, const char **argv)
 			ret = -EINVAL;
 			goto out_delete_filters;
 		}
-		if (!ftrace.min_latency) {
+		if (ftrace.bucket_range && !ftrace.min_latency) {
 			/* default min latency should be the bucket range */
 			ftrace.min_latency = ftrace.bucket_range;
 		}
@@ -1749,7 +1751,7 @@ int cmd_ftrace(int argc, const char **argv)
 			ret = -EINVAL;
 			goto out_delete_filters;
 		}
-		if (!ftrace.max_latency) {
+		if (ftrace.bucket_range && !ftrace.max_latency) {
 			/* default max latency should depend on bucket range and num_buckets */
 			ftrace.max_latency = (NUM_BUCKET - 2) * ftrace.bucket_range +
 						ftrace.min_latency;
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
author maintainer
#1 lore raw

PATCH 2/2 perf ftrace: Fix display for range of the first bucket

When min_latency is not given, it prints 0 - 0. It should be 0 - 1.

Before:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    0 us |        321 | ###########                                    |
  ...

After:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    1 us |        699 | ############                                   |
  ...
Fixes b875b6bf608589 ("perf ftrace latency: Introduce --min-latency to narrow down into a latency range")
Cc Gabriele Monaco <[email protected]>
---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 90cf2c9915a7e35b..cfd770ec72867d77 100644
@@ -840,7 +840,7 @@ static void display_histogram(struct perf_ftrace *ftrace, int buckets[])
 	bar_len = buckets[0] * bar_total / total;
 
 	printf("  %4d - %4d %s | %10d | %.*s%*s |\n",
-	       0, min_latency, use_nsec ? "ns" : "us",
+	       0, min_latency ?: 1, use_nsec ? "ns" : "us",
 	       buckets[0], bar_len, bar, bar_total - bar_len, "");
 
 	for (i = 1; i < NUM_BUCKET - 1; i++) {
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
#2 lore raw

Re: [PATCH 2/2] perf ftrace: Fix display for range of the first bucket

Quoted 8 lines · Namhyung Kim

When min_latency is not given, it prints 0 - 0. It should be 0 - 1.

Before:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    0 us |        321 | ###########                                    |
  ...
Quoted 7 lines

After:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    1 us |        699 | ############                                   |
  ...
Fixes b875b6bf608589 ("perf ftrace latency: Introduce --min-latency to narrow down into a latency range")

I couldn't find the above cset in perf-tools not perf-tools-next nor in upstream, I fixed it up to:

Fixes 08b875b6bf608589 ("perf ftrace latency: Introduce --min-latency to narrow down into a latency range")

Thanks,

- Arnaldo

Quoted 21 lines
Cc Gabriele Monaco <[email protected]>
---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 90cf2c9915a7e35b..cfd770ec72867d77 100644
@@ -840,7 +840,7 @@ static void display_histogram(struct perf_ftrace *ftrace, int buckets[])
 	bar_len = buckets[0] * bar_total / total;
 
 	printf("  %4d - %4d %s | %10d | %.*s%*s |\n",
-	       0, min_latency, use_nsec ? "ns" : "us",
+	       0, min_latency ?: 1, use_nsec ? "ns" : "us",
 	       buckets[0], bar_len, bar, bar_total - bar_len, "");
 
 	for (i = 1; i < NUM_BUCKET - 1; i++) {
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
#3 lore raw

Re: [PATCH 2/2] perf ftrace: Fix display for range of the first bucket

Quoted 15 lines · Arnaldo Carvalho de Melo
Quoted 14 lines · Namhyung Kim

When min_latency is not given, it prints 0 - 0. It should be 0 - 1.

Before:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    0 us |        321 | ###########                                    |
  ...

After:

  $ sudo ./perf ftrace latency -a -T do_futex sleep 1
  #   DURATION     |      COUNT | GRAPH                                          |
       0 -    1 us |        699 | ############                                   |
  ...
Quoted 4 lines
Quoted 1 line
Fixes b875b6bf608589 ("perf ftrace latency: Introduce --min-latency to narrow down into a latency range")

I couldn't find the above cset in perf-tools not perf-tools-next nor in upstream, I fixed it up to:

It is just in perf-tools-next, so probably I did some rebase and that explains this. Anyway, that should be stable now.

- Arnaldo

Quoted 27 lines
Fixes 08b875b6bf608589 ("perf ftrace latency: Introduce --min-latency to narrow down into a latency range")

Thanks,

- Arnaldo

Quoted 21 lines
Cc Gabriele Monaco <[email protected]>
---
 tools/perf/builtin-ftrace.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/builtin-ftrace.c +1 -1
index 90cf2c9915a7e35b..cfd770ec72867d77 100644
@@ -840,7 +840,7 @@ static void display_histogram(struct perf_ftrace *ftrace, int buckets[])
 	bar_len = buckets[0] * bar_total / total;
 
 	printf("  %4d - %4d %s | %10d | %.*s%*s |\n",
-	       0, min_latency, use_nsec ? "ns" : "us",
+	       0, min_latency ?: 1, use_nsec ? "ns" : "us",
 	       buckets[0], bar_len, bar, bar_total - bar_len, "");
 
 	for (i = 1; i < NUM_BUCKET - 1; i++) {
Signature · 1 line
2.47.1.613.gc27f4b7a9f-goog
#4 lore raw
In reply to Namhyung Kim
Quoted 4 lines · Namhyung Kim

It's an optional feature and remains 0 when bucket range is not given.
And it makes the histogram goes to the last entry always because any latency (num) is greater than or equal to 0.

Thanks Namhyung for fixing this, something definitely slipped while testing..

I confirm your patches work well also when the bucket range is provided but the min latency isn't.

I'm wondering if it would be cleaner to propagate your changes (using min/max latency only if bucket_range is provided) also to make_histogram. That function currently works since we assume min_latency to be always 0, which is the case but probably not considering it altogether would look a bit better and prevent some headache in the future.

---
 tools/perf/builtin-ftrace.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
tools/perf/builtin-ftrace.c +5 -8
index 076c703e5c334..82d63d7af9d03 100644
@@ -779,19 +779,16 @@ static void make_histogram(struct perf_ftrace *ftrace, int buckets[],
 		if (ftrace->use_nsec)
 			num *= 1000;
 
-		i = 0;
-		if (num < min_latency)
-			goto do_inc;
-
-		num -= min_latency;
-
 		if (!ftrace->bucket_range) {
 			i = log2(num);
 			if (i < 0)
 				i = 0;
 		} else {
-			// Less than 1 unit (ms or ns), or, in the future,
-			// than the min latency desired.
+			i = 0;
+			if (num < min_latency)
+				goto do_inc;
+
+			num -= min_latency;
 			if (num > 0) // 1st entry: [ 1 unit .. bucket_range units ]
 				i = num / ftrace->bucket_range + 1;
 			if (num >= max_latency - min_latency)

base-commit: 257ccfaf9fbef6f54eabf1a8f8a8efcc9036439b

Signature · 1 line
2.47.1
author maintainer
#5 lore raw
Quoted 12 lines · Gabriele Monaco
Quoted 4 lines · Namhyung Kim

It's an optional feature and remains 0 when bucket range is not given.
And it makes the histogram goes to the last entry always because any latency (num) is greater than or equal to 0.

Thanks Namhyung for fixing this, something definitely slipped while testing..

I confirm your patches work well also when the bucket range is provided but the min latency isn't.

Quoted 6 lines

I'm wondering if it would be cleaner to propagate your changes (using min/max latency only if bucket_range is provided) also to make_histogram. That function currently works since we assume min_latency to be always 0, which is the case but probably not considering it altogether would look a bit better and prevent some headache in the future.

It looks good. One thing I concern is 'num += min_latency' before do_inc. I put it there to make it symmetric to 'num -= min_latency' so it should go to inside the block too.

Or you could factor it out as a function like 'i = get_bucket_index(num)' so that it can keep the original num for the stats.

Thanks,
Namhyung

Quoted 39 lines
---
 tools/perf/builtin-ftrace.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
tools/perf/builtin-ftrace.c +5 -8
index 076c703e5c334..82d63d7af9d03 100644
@@ -779,19 +779,16 @@ static void make_histogram(struct perf_ftrace *ftrace, int buckets[],
 		if (ftrace->use_nsec)
 			num *= 1000;
 
-		i = 0;
-		if (num < min_latency)
-			goto do_inc;
-
-		num -= min_latency;
-
 		if (!ftrace->bucket_range) {
 			i = log2(num);
 			if (i < 0)
 				i = 0;
 		} else {
-			// Less than 1 unit (ms or ns), or, in the future,
-			// than the min latency desired.
+			i = 0;
+			if (num < min_latency)
+				goto do_inc;
+
+			num -= min_latency;
 			if (num > 0) // 1st entry: [ 1 unit .. bucket_range units ]
 				i = num / ftrace->bucket_range + 1;
 			if (num >= max_latency - min_latency)

base-commit: 257ccfaf9fbef6f54eabf1a8f8a8efcc9036439b

Signature · 1 line
2.47.1
#6 lore raw
2025-01-10T00:46:49Z Namhyung Kim <[email protected]>:
Quoted 20 lines · Gabriele Monaco
Quoted 12 lines · Namhyung Kim
Quoted 4 lines

It's an optional feature and remains 0 when bucket range is not given.
And it makes the histogram goes to the last entry always because any latency (num) is greater than or equal to 0.

Thanks Namhyung for fixing this, something definitely slipped while testing..

I confirm your patches work well also when the bucket range is provided but the min latency isn't.

Quoted 6 lines

I'm wondering if it would be cleaner to propagate your changes (using min/max latency only if bucket_range is provided) also to make_histogram. That function currently works since we assume min_latency to be always 0, which is the case but probably not considering it altogether would look a bit better and prevent some headache in the future.

Quoted 7 lines

It looks good.  One thing I concern is 'num += min_latency' before do_inc.  I put it there to make it symmetric to 'num -= min_latency' so it should go to inside the block too.

Or you could factor it out as a function like 'i = get_bucket_index(num)' so that it can keep the original num for the stats.

Good point, I can have a deeper look at that. But I'd say it can come as a cleanup patch later. I have a couple more changes in mind and this would be no longer related to your changes.

Thanks,
Gabriele

maintainer Acked-by Gabriele Monaco
#7 lore raw
Quoted 26 lines · Gabriele Monaco
2025-01-10T00:46:49Z Namhyung Kim <[email protected]>:
Quoted 24 lines · Gabriele Monaco
Quoted 12 lines · Namhyung Kim
Quoted 4 lines

It's an optional feature and remains 0 when bucket range is not given.
And it makes the histogram goes to the last entry always because any latency (num) is greater than or equal to 0.

Thanks Namhyung for fixing this, something definitely slipped while testing..

I confirm your patches work well also when the bucket range is provided but the min latency isn't.

Quoted 6 lines

I'm wondering if it would be cleaner to propagate your changes (using min/max latency only if bucket_range is provided) also to make_histogram. That function currently works since we assume min_latency to be always 0, which is the case but probably not considering it altogether would look a bit better and prevent some headache in the future.

It looks good.  One thing I concern is 'num += min_latency' before do_inc.  I put it there to make it symmetric to 'num -= min_latency' so it should go to inside the block too.

Quoted 6 lines
Quoted 3 lines

Or you could factor it out as a function like 'i = get_bucket_index(num)' so that it can keep the original num for the stats.

Good point, I can have a deeper look at that. But I'd say it can come as a cleanup patch later. I have a couple more changes in mind and this would be no longer related to your changes.

I'm tentatively taking this as an:

But it would be great to have it as a Reviewed-by and perhaps a Tested-by, provided explicitely in response to this thread, ok?

Thanks,

- Arnaldo

Reviewed-by Tested-by
#8 lore raw
Quoted 47 lines · Arnaldo Carvalho de Melo
Quoted 39 lines · Gabriele Monaco
2025-01-10T00:46:49Z Namhyung Kim <[email protected]>:
Quoted 32 lines · Gabriele Monaco
Quoted 16 lines · Namhyung Kim
Quoted 6 lines

It's an optional feature and remains 0 when bucket range is not
given.
And it makes the histogram goes to the last entry always because any
latency (num) is greater than or equal to 0.

Thanks Namhyung for fixing this, something definitely slipped while
testing..

I confirm your patches work well also when the bucket range is provided but the
min latency isn't.

Quoted 8 lines

I'm wondering if it would be cleaner to propagate your changes (using
min/max latency only if bucket_range is provided) also to make_histogram. That function currently works since we assume min_latency to be always 0, which is the case but probably not considering it altogether would look a bit better and prevent some
headache in the future.

It looks good.  One thing I concern is 'num += min_latency' before
do_inc.  I put it there to make it symmetric to 'num -= min_latency'
so it should go to inside the block too.

Quoted 4 lines

Or you could factor it out as a function like 'i = get_bucket_index(num)'
so that it can keep the original num for the stats.

Quoted 4 lines

Good point, I can have a deeper look at that. But I'd say it can come as a cleanup patch later.
I have a couple more changes in mind and this would be no longer related to your changes.

I'm tentatively taking this as an:

Quoted 9 lines

But it would be great to have it as a Reviewed-by and perhaps a Tested-by, provided explicitely in response to this thread, ok?

Thanks,

- Arnaldo

I did test after applying both patches, went through the code and confirm my test worked as expected, and I confirm the issue is there before patching. I tested also in between, so feel free to add to both patches:

(I'm assuming you are referring to 1/2 and 2/2 and not the little patch I sent in the first answer)

Thanks,
Gabriele

#9 lore raw
Quoted 59 lines · Gabriele Monaco
Quoted 47 lines · Arnaldo Carvalho de Melo
Quoted 39 lines · Gabriele Monaco
2025-01-10T00:46:49Z Namhyung Kim <[email protected]>:
Quoted 32 lines · Gabriele Monaco
Quoted 16 lines · Namhyung Kim
Quoted 6 lines

It's an optional feature and remains 0 when bucket range is not
given.
And it makes the histogram goes to the last entry always because any
latency (num) is greater than or equal to 0.

Thanks Namhyung for fixing this, something definitely slipped while
testing..

I confirm your patches work well also when the bucket range is provided but the
min latency isn't.

Quoted 8 lines

I'm wondering if it would be cleaner to propagate your changes (using
min/max latency only if bucket_range is provided) also to make_histogram. That function currently works since we assume min_latency to be always 0, which is the case but probably not considering it altogether would look a bit better and prevent some
headache in the future.

It looks good.  One thing I concern is 'num += min_latency' before
do_inc.  I put it there to make it symmetric to 'num -= min_latency'
so it should go to inside the block too.

Quoted 4 lines

Or you could factor it out as a function like 'i = get_bucket_index(num)'
so that it can keep the original num for the stats.

Quoted 4 lines

Good point, I can have a deeper look at that. But I'd say it can come as a cleanup patch later.
I have a couple more changes in mind and this would be no longer related to your changes.

I'm tentatively taking this as an:

Quoted 9 lines

But it would be great to have it as a Reviewed-by and perhaps a Tested-by, provided explicitely in response to this thread, ok?

Thanks,

- Arnaldo

Quoted 7 lines

I did test after applying both patches, went through the code and confirm my test worked as expected, and I confirm the issue is there before patching. I tested also in between, so feel free to add to both patches:

Thanks, added.

Quoted 1 line

(I'm assuming you are referring to 1/2 and 2/2 and not the little patch

Yeah, Namhyung's 1/2 and 2/2 patches, these:

https://lore.kernel.org/r/[email protected]
https://lore.kernel.org/r/[email protected]

- Arnaldo

Quoted 4 lines

I sent in the first answer)

Thanks,
Gabriele