Skip to contentSkip to conversation

bpftool: introduce btf c dump sorting

PATCH Applied Mykyta Yatsenko [email protected] 3 participants 3 messages May 14, 2024 – May 17, 2024

Subsystems (2)
lore
Share & tools
lore

Mykyta Yatsenko [email protected]

author
#0 lore raw

PATCH v4 1/1 bpftool: introduce btf c dump sorting

From: Mykyta Yatsenko <[email protected]>

Sort bpftool c dump output; aiming to simplify vmlinux.h diffing and forcing more natural type definitions ordering.

Definitions are sorted first by their BTF kind ranks, then by their base type name and by their own name.

Type ranks

Assign ranks to btf kinds (defined in function btf_type_rank) to set next order:
1. Anonymous enums/enums64
2. Named enums/enums64
3. Trivial types typedefs (ints, then floats)
4. Structs/Unions
5. Function prototypes
6. Forward declarations

Type rank is set to maximum for unnamed reference types, structs and unions to avoid emitting those types early. They will be emitted as part of the type chain starting with named type.

Lexicographical ordering

Each type is assigned a sort_name and own_name.
sort_name is the resolved name of the final base type for reference types (typedef, pointer, array etc). Sorting by sort_name allows to group typedefs of the same base type. sort_name for non-reference type is the same as own_name. own_name is a direct name of particular type, is used as final sorting step.

---
 .../bpf/bpftool/Documentation/bpftool-btf.rst |   6 +-
 tools/bpf/bpftool/bash-completion/bpftool     |   3 +
 tools/bpf/bpftool/btf.c                       | 138 +++++++++++++++++-
 3 files changed, 140 insertions(+), 7 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-btf.rst +4 -2
index eaba24320fb2..3f6bca03ad2e 100644
@@ -28,7 +28,7 @@ BTF COMMANDS
 | **bpftool** **btf help**
 |
 | *BTF_SRC* := { **id** *BTF_ID* | **prog** *PROG* | **map** *MAP* [{**key** | **value** | **kv** | **all**}] | **file** *FILE* }
-| *FORMAT* := { **raw** | **c** }
+| *FORMAT* := { **raw** | **c** [**unsorted**] }
 | *MAP* := { **id** *MAP_ID* | **pinned** *FILE* }
 | *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* | **name** *PROG_NAME* }
 
@@ -63,7 +63,9 @@ bpftool btf dump *BTF_SRC*
     pahole.
 
     **format** option can be used to override default (raw) output format. Raw
-    (**raw**) or C-syntax (**c**) output formats are supported.
+    (**raw**) or C-syntax (**c**) output formats are supported. With C-style
+    formatting, the output is sorted by default. Use the **unsorted** option
+    to avoid sorting the output.
 
 bpftool btf help
     Print short help message.
tools/bpf/bpftool/bash-completion/bpftool +3 -0
index 04afe2ac2228..be99d49b8714 100644
@@ -930,6 +930,9 @@ _bpftool()
                         format)
                             COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
                             ;;
+                        c)
+                            COMPREPLY=( $( compgen -W "unsorted" -- "$cur" ) )
+                            ;;
                         *)
                             # emit extra options
                             case ${words[3]} in
tools/bpf/bpftool/btf.c +133 -5

192 lines not on this page · raw message

Signature · 1 line
2.44.0
maintainer Acked-by Andrii Nakryiko Tested-by Andrii Nakryiko Reviewed-by on v4 1/1
#1 lore raw

2024-05-14 14:12 UTC+0100 ~ Mykyta Yatsenko [email protected]

Quoted 25 lines

From: Mykyta Yatsenko <[email protected]>

Sort bpftool c dump output; aiming to simplify vmlinux.h diffing and forcing more natural type definitions ordering.

Definitions are sorted first by their BTF kind ranks, then by their base type name and by their own name.

Type ranks

Assign ranks to btf kinds (defined in function btf_type_rank) to set next order:
1. Anonymous enums/enums64
2. Named enums/enums64
3. Trivial types typedefs (ints, then floats)
4. Structs/Unions
5. Function prototypes
6. Forward declarations

Type rank is set to maximum for unnamed reference types, structs and unions to avoid emitting those types early. They will be emitted as part of the type chain starting with named type.

Lexicographical ordering

Quoted 8 lines

Each type is assigned a sort_name and own_name.
sort_name is the resolved name of the final base type for reference types (typedef, pointer, array etc). Sorting by sort_name allows to group typedefs of the same base type. sort_name for non-reference type is the same as own_name. own_name is a direct name of particular type, is used as final sorting step.

Please keep the tags from previous versions when the changes are trivial:

Thanks a lot!

#2 lore raw
In reply to Mykyta Yatsenko [email protected]
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 9 lines · On Tue, 14 May 2024 14:12:21 +0100 you wrote:

From: Mykyta Yatsenko <[email protected]>

Sort bpftool c dump output; aiming to simplify vmlinux.h diffing and forcing more natural type definitions ordering.

Definitions are sorted first by their BTF kind ranks, then by their base type name and by their own name.

[...]

Here is the summary with links:
- [v4,bpf-next] bpftool: introduce btf c dump sorting

    https://git.kernel.org/bpf/bpf-next/c/45053110e260

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html