Documentation: Add kernel minimization guide
Subsystems (5)
cover Documentation: Add kernel minimization guide
Hi all,
This series adds documentation and a script for measuring source lines of code (SLOC) in kernel builds, supporting developers and system integrators who need to trim a Linux kernel.
The Workload analysis section of the admin-guide already describes tracing a workload. This series builds on that guidance with a worked example of using workload observations to inform kernel minimization and measure the resulting configuration. The guide is intended for developers working on embedded and safety-critical use cases, including work related to ELISA.
The series builds on the ELISA Aerospace Working Group's ongoing minimization discussions and iterations in
https://github.com/elisa-tech/wg-aerospace/pull/257. That effort explores a generic, newcomer-oriented workflow for tracing a workload, iterating on a kernel configuration, measuring kernel metrics, and understanding userspace metrics. The ELISA documentation aims to present a complete containerized workflow, with analysis steps to guide users through successive iterations. This series contributes reusable, general-purpose kernel documentation and tooling upstream to support that broader process. Clear upstream guidance can also give AI-assisted tools reliable steering context as they help users iterate on kernel configurations. One likely early application is producing minimized release configurations for Space Grade Linux.
Rendered drafts are available online:
Kernel minimization:
https://matthew-l-weber.github.io/linux/admin-guide/kernel-minimization.html
Workload tracing:
https://matthew-l-weber.github.io/linux/admin-guide/workload-tracing.html
The series and its review discussion are in PR #1:
https://github.com/matthew-l-weber/linux/pull/1
The patches are:
1/4 Documentation: kcov: clarify coverage example
Clarify that the saved count bounds the printed entries while KCOV remains enabled until the disable ioctl. Document the relevant config requirements and PC symbolization.
2/4 scripts: Add kernel-sloc helper script
Add a helper to count source lines from compile_commands.json or estimate distinct source lines represented in a vmlinux DWARF line table.
3/4 Documentation: admin-guide: Add kernel minimization guide
Add the guide to the Workload analysis section and add its MAINTAINERS coverage.
4/4 Documentation: admin-guide: Update workload tracing guide
Expand workload tracing guidance with ftrace function-graph tracing, syscall tracing under QEMU with the GDB stub, and mapping observations to kernel configuration symbols.
Tool-use disclosure: I used GitHub Copilot CLI while preparing this series. It helped revise the KCOV example, edit parts of the documentation, add MAINTAINERS entries, and integrate error-handling revisions to kernel-sloc. I reviewed the changes and take responsibility for the series.
I checked kernel-sloc syntax and help output, and ran checkpatch.pl --strict on all four patches. The GitHub Actions htmldocs build completed successfully with no warnings from the changed files; the published documentation is available at
https://matthew-l-weber.github.io/linux/admin-guide/index.html#workload-analysis.
I did not run make htmldocs locally.
Any feedback is appreciated.
Thanks,
Matthew
Matthew Weber (4):
Documentation: kcov: clarify coverage example
scripts: Add kernel-sloc helper script
Documentation: admin-guide: Add kernel minimization guide
Documentation: admin-guide: Update workload tracing guide
Documentation/admin-guide/index.rst | 1 + .../admin-guide/kernel-minimization.rst | 413 ++++++++++++++++++ .../quickly-build-trimmed-linux.rst | 4 + .../admin-guide/workload-tracing.rst | 139 +++++- Documentation/dev-tools/kcov.rst | 44 ++- MAINTAINERS | 13 + scripts/kernel-sloc | 299 +++++++++++++ 7 files changed, 903 insertions(+), 10 deletions(-) create mode 100644 Documentation/admin-guide/kernel-minimization.rst create mode 100755 scripts/kernel-sloc
base-commit: a592a36e49372172d7c7551ec19ed18184c935e1
Signature · 1 line
2.39.5
PATCH 1/1 Documentation: kcov: clarify coverage example
Clarify that the saved count bounds the printed entries while KCOV remains enabled, including during output. The examples continue to disable KCOV after printing, as before.
Document the KCOV configuration requirements and how to symbolize collected PCs.
--- Documentation/dev-tools/kcov.rst | 44 +++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-)
Documentation/dev-tools/kcov.rst +41 -3
@@ -31,6 +31,15 @@ Collecting comparison operands is supported with GCC 8+ or with Clang. To enable KCOV, configure the kernel with:: CONFIG_KCOV=y + CONFIG_DEBUG_FS=y + +``CONFIG_KCOV_INSTRUMENT_ALL=y`` (the default) instruments eligible kernel +code throughout the build. If it is disabled, only explicitly selected code +is instrumented. + +To resolve collected PCs to source locations, also build with debug information +(``CONFIG_DEBUG_INFO=y``) and retain the matching ``vmlinux``. Debug information +is not required for collection itself. To enable comparison operands collection, set::
@@ -94,7 +103,7 @@ single syscall from within a test program: __atomic_store_n(&cover[0], 0, __ATOMIC_RELAXED); /* Call the target syscall call. */ read(-1, NULL, 0); - /* Read number of PCs collected. */ + /* Save the output count; KCOV continues recording until disabled. */ n = __atomic_load_n(&cover[0], __ATOMIC_RELAXED); for (i = 0; i < n; i++) printf("0x%lx\n", cover[i + 1]);
@@ -111,7 +120,36 @@ single syscall from within a test program: return 0; } -After piping through ``addr2line`` the output of the program looks as follows:: +In PC mode, the first ``unsigned long`` holds the count and the remaining +``COVER_SIZE - 1`` slots hold PCs. KCOV stops adding entries when the buffer +is full, so increase the buffer or shorten the collection window if the count +reaches it. An empty buffer can indicate that the target path was not +instrumented; it is not proof that no kernel code ran. + +The count is read while KCOV is enabled and sets the number of entries printed. +KCOV remains enabled while those entries are printed, so the output syscalls +may add entries to the buffer. They are not included in the printed list, +because the loop is bounded by the saved count. Syscall entry/return paths +around the target can also appear. + +Symbolizing PCs +~~~~~~~~~~~~~~~ + +Save the example as ``kcov-example.c``, compile it for the target, and run it +where debugfs is mounted and accessible:: + + cc -O2 -o kcov-example kcov-example.c + sudo ./kcov-example > kcov-pcs.txt + addr2line -f -i -e /path/to/matching/vmlinux < kcov-pcs.txt > kcov-symbols.txt + +For a cross-built target, use the matching toolchain's compiler and +``addr2line`` (for example ``aarch64-linux-gnu-addr2line``). Use the exact build's +debug ELF, not another build's ``vmlinux``. KCOV subtracts the kernel's KASLR +offset on architectures that support it; do not apply an assumed relocation +adjustment. Instrumented modules need their own debug ELF and load-address +handling rather than symbolization against ``vmlinux``. + +The output can look as follows:: SyS_read fs/read_write.c:562
@@ -184,7 +222,7 @@ Comparison operands collection is similar to coverage collection: perror("ioctl"), exit(1); __atomic_store_n(&cover[0], 0, __ATOMIC_RELAXED); read(-1, NULL, 0); - /* Read number of comparisons collected. */ + /* Save the output count, as in PC mode. */ n = __atomic_load_n(&cover[0], __ATOMIC_RELAXED); for (i = 0; i < n; i++) { uint64_t ip;
Signature · 1 line
2.39.5
PATCH 2/4 scripts: Add kernel-sloc helper script
In reply to Matthew WeberAdd a helper script scripts/kernel-sloc to count source lines of code (SLOC) for a configured Linux kernel build. It supports counting whole compiled sources from compile_commands.json as well as estimating distinct source lines represented in a vmlinux ELF using DWARF line-table addresses.
It is intended for system integrators and application developers who analyze a trimmed kernel, for example for safety critical applications, and need to report how much kernel code a given configuration builds and links. Add a MAINTAINERS entry for the script.
--- MAINTAINERS | 6 + scripts/kernel-sloc | 299 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 305 insertions(+) create mode 100755 scripts/kernel-sloc
MAINTAINERS +6 -0
@@ -13762,6 +13762,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git F: Documentation/dev-tools/kselftest* F: tools/testing/selftests/ +KERNEL SLOC HELPER +M: Matthew Weber <[email protected]> +L: [email protected] +S: Maintained +F: scripts/kernel-sloc + KERNEL SMB3 SERVER (KSMBD) M: Namjae Jeon <[email protected]> M: Namjae Jeon <[email protected]>
scripts/kernel-sloc new file +299 -0
299 lines not on this page · raw message
Signature · 1 line
2.39.5
Nicolas Schier
maintainerRe: [PATCH 2/4] scripts: Add kernel-sloc helper script
Quoted 11 lines · Matthew Weber
Add a helper script
scripts/kernel-slocto count source lines of code (SLOC) for a configured Linux kernel build. It supports counting whole compiled sources from compile_commands.json as well as estimating distinct source lines represented in a vmlinux ELF using DWARF line-table addresses.It is intended for system integrators and application developers who analyze a trimmed kernel, for example for safety critical applications, and need to report how much kernel code a given configuration builds and links. Add a
MAINTAINERSentry for the script.
Quoted 7 lines
Signed-off-by Matthew Weber <[email protected]>--- MAINTAINERS | 6 + scripts/kernel-sloc | 299 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 305 insertions(+) create mode 100755 scripts/kernel-sloc
Thanks for your patches.
Disclaimer: I did not really look into your patch set yet, but let me note some major formal things ahead:
Something got heavily wrong with the patch set mails: while it looks like this patch is number 2 from a series of 4 patches, the other patches sent in the series are not numbered correctly. Patch management tooling (b4) even recognises this patch set as three different versions of a patch set, thus reviewers/maintainers cannot use that here. Usually, 'git send-email' takes care of that in a very proper way. Did you sent your patches one-by-one in some manual way?
MAINTAINERS T: git git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git · line 13769
Quoted 13 lines
MAINTAINERS +5
-0@@ -13762,6 +13762,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest.git F: Documentation/dev-tools/kselftest* F: tools/testing/selftests/ +KERNEL SLOC HELPER +M: Matthew Weber <[email protected]> +L: [email protected] +S: Maintained +F: scripts/kernel-sloc
If 'kernel-sloc' is accepted, we will have to put it to scripts/contrib/, as 'kernel-sloc' is not part of the Linux build. 'scripts/contrib/' does not exist yet, but we are urged to stop mixing build-related scripts with non-build-related ones in scripts/.
scripts/kernel-sloc line 2
Quoted 12 lines
MAINTAINERS +1
-0+ KERNEL SMB3 SERVER (KSMBD) M: Namjae Jeon <[email protected]> M: Namjae Jeon <[email protected]>scripts/kernel-sloc new file +2
-0@@ -0,0 +1,299 @@ +#!/bin/sh +# SPDX-License-Identifier: BSD-3-Clause
Almost all files below scripts/ are licensed under GPL. Please consider to switch to some GPL licensing or dual-license for consistency.
Quoted 48 lines
scripts/kernel-sloc new file +48
-0+# +# kernel-sloc - Count source lines of code for a configured kernel build. +# +# Adapted from the Unikraft build-time SLOC reporting approach +# (https://github.com/orgs/unikraft/discussions/1849#discussioncomment-19352529), +# which counts compiled sources rather than the whole source tree. +# +# The kernel mode counts whole compiled files, including inactive #if branches, +# but not the headers they include. The dwarf mode estimates source lines +# represented by debug information in a linked ELF; it does not measure runtime +# coverage. Neither count is a certification or structural-coverage result. + +set -eu + +die() { + echo "ERROR: $*" >&2 + exit 1 +} + +need_command() { + command -v "$1" >/dev/null 2>&1 || die "required command not found: $1" +} + +need_cloc() { + command -v cloc >/dev/null 2>&1 || + die "required command not found: cloc (install cloc to use kernel, tree, or files mode)" +} + +usage() { + printf 'Usage: %s [--report JSON_FILE] MODE TARGET...\n' "$0" + printf '%s\n' \ + ' kernel BUILD_DIR Count whole compiled sources from compile_commands.json.' \ + ' Requires cloc and python3. Generate the database after' \ + ' a clean build of the configuration being measured.' \ + ' dwarf ELF Estimate distinct source lines using DWARF addresses.' \ + ' Requires GNU objdump, addr2line, and python3, plus' \ + ' an ELF built with debug information (CONFIG_DEBUG_INFO).' \ + ' Set OBJDUMP and ADDR2LINE for cross builds, or set' \ + ' CROSS_COMPILE to the toolchain prefix.' \ + ' tree DIR Count all C, C++, and header sources; requires cloc.' \ + ' files FILE... Count individual source files; requires cloc.' \ + ' --report FILE JSON inventory for kernel or dwarf; parent must exist.' \ + ' Lists compiled files, or DWARF source_lines and functions.' \ + ' Does not measure execution or list every linked symbol.' \ + ' -h, --help Show this help and exit successfully.' \ + '' \ + 'Success prints one integer on stdout and a summary on stderr.' \ + 'Errors exit non-zero without printing a count.' \
scripts/kernel-sloc line 60
Quoted 10 lines
scripts/kernel-sloc new file +10
-0+ 'Counts depend on the tool versions and build options; record both.' \ + '' \ + 'Examples:' + printf ' %s --report compiled.json kernel .\n' "$0" + printf ' %s --report linked.json dwarf vmlinux\n' "$0" + printf ' %s files foo.c\n' "$0" + printf ' %s tree /path/to/files\n' "$0" + printf '%s\n' \ + 'files omits linked libc; tree includes unused C/C++/headers, not other' \ + 'languages. Neither is a deployed-footprint total.'
bike-shedding:
cat <<-eof
...
eof
might be valuable alternative to all those 'printf' calls.
Quoted 7 lines
scripts/kernel-sloc new file +7
-0+} + +# Extract the code total from cloc CSV, including the empty-tree case. +cloc_sum() { + awk -F',' '$2 == "SUM" {total = $5} END {print total + 0}' +} +
scripts/kernel-sloc line 77
Quoted 10 lines
scripts/kernel-sloc new file +10
-0+cloc_sum_from_list() { + cloc --list-file="$1" --skip-uniqueness --csv --quiet >"$2" || + die "cloc failed while counting source files" + cloc_sum <"$2" +} + +# Write the report before printing the count; failed writes are not success. +write_report() { + [ -n "$report" ] || return 0 + python3 - "$report" "$mode" "$target" "$1" "$2" <<'PYREPORT'
Please do not embedd more than a handful of Python code lines in a shell script. Common Python linting is simplier in a pure Python script. And if Python is needed for the task, why is it wrapped in shell instead of implementing the whole thing in Python?
I'd like to read your reply before reviewing this any further.
Kind regards,
Nicolas
Nathan Chancellor
maintainerRe: [PATCH 2/4] scripts: Add kernel-sloc helper script
Quoted 10 lines · Nicolas Schier
scripts/kernel-sloc line 77
Quoted 4 lines · Matthew Weber
scripts/kernel-sloc new file +4
-0+# Write the report before printing the count; failed writes are not success. +write_report() { + [ -n "$report" ] || return 0 + python3 - "$report" "$mode" "$target" "$1" "$2" <<'PYREPORT'Please do not embedd more than a handful of Python code lines in a shell script. Common Python linting is simplier in a pure Python script. And if Python is needed for the task, why is it wrapped in shell instead of implementing the whole thing in Python?
Very much agreed, this stood out when I scanned the series initially. I think this would read much better as a full Python script, it feels like there is very little reason for this to be a shell script with this amount of complexity.
Signature · 2 lines
Cheers, Nathan
PATCH 1/1 Documentation: admin-guide: Add kernel minimization guide
In reply to Matthew WeberAdd kernel-minimization.rst to the Workload analysis section of the admin-guide, for application developers and system integrators who need to trim a Linux kernel for a single known workload, for example when analyzing the kernel for safety critical applications.
The guide explains how to trim a kernel for a workload running directly as PID 1, cross-compiled for QEMU arm64 with CONFIG_MODULES=n, and how to measure the resulting build.
--- Documentation/admin-guide/index.rst | 1 + .../admin-guide/kernel-minimization.rst | 413 ++++++++++++++++++ .../quickly-build-trimmed-linux.rst | 4 + MAINTAINERS | 6 + 4 files changed, 424 insertions(+) create mode 100644 Documentation/admin-guide/kernel-minimization.rst
Documentation/admin-guide/index.rst +1 -0
@@ -178,6 +178,7 @@ subsystems expectations will be found here. :maxdepth: 1 workload-tracing + kernel-minimization Everything else ---------------
Documentation/admin-guide/kernel-minimization.rst new file +413 -0
413 lines not on this page · raw message
Documentation/admin-guide/quickly-build-trimmed-linux.rst +4 -0
@@ -8,6 +8,10 @@ How to quickly build a trimmed Linux kernel This guide explains how to swiftly build Linux kernels that are ideal for testing purposes, but perfectly fine for day-to-day use, too. +.. note:: To shrink a kernel down to only what one known workload needs, and + to measure the result, see :doc:`kernel-minimization`, which builds on this + guide. + The essence of the process (aka 'TL;DR') ========================================
MAINTAINERS +6 -0
@@ -28154,6 +28154,12 @@ F: sound/soc/codecs/arizona* F: sound/soc/codecs/cs47l24* F: sound/soc/codecs/wm* +WORKLOAD ANALYSIS DOCUMENTATION +M: Matthew Weber <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/admin-guide/kernel-minimization.rst + WORKQUEUE M: Tejun Heo <[email protected]> R: Lai Jiangshan <[email protected]>
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH] Documentation: admin-guide: Add kernel minimization guide
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] Documentation: admin-guide: kernel-minimization.rst references nonexistent scripts/kernel-sloc…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Documentation: admin-guide: kernel-minimization.rst references nonexistent scripts/kernel-sloc
- [Low] Documentation: admin-guide: kernel-minimization.rst cites nonexistent workload-tracing sections --
commit 0e6d4d6be9381b61780fc4af63f1b4ba2d519ab3
Author: Matthew Weber <[email protected]>
Documentation: admin-guide: Add kernel minimization guide
This commit adds Documentation/admin-guide/kernel-minimization.rst to explain how to trim a Linux kernel for a single known workload running as PID 1 under QEMU arm64 with CONFIG_MODULES=n, and how to measure the resulting build.
Quoted 6 lines
Documentation/admin-guide/kernel-minimization.rst new file +0
-0@@ -0,0 +1,413 @@
[ ... ]
Documentation/admin-guide/kernel-minimization.rst line 201
Quoted 8 lines
Documentation/admin-guide/kernel-minimization.rst new file +8
-0+Deciding what to remove +======================= + +Follow :doc:`workload-tracing` for syscall and kernel-path collection. Use its +"Tracing a minimal user space" section with the program from +`A minimal workload`_, and its "Mapping observations to configuration" section +for source and Kconfig analysis. Re-test each removal with the same PID 1 +workload.
[Severity: Low]
Are the "Tracing a minimal user space" and "Mapping observations to configuration" sections missing from Documentation/admin-guide/workload-tracing.rst, or were those sections meant to be added in a companion patch?
Currently Documentation/admin-guide/workload-tracing.rst does not contain either of those sections.
[ ... ]
Quoted 11 lines
Documentation/admin-guide/kernel-minimization.rst new file +11
-0+Counting kernel SLOC +-------------------- + +Install ``cloc`` for compiled-source counts and ensure ``python3`` is available +for generating the compilation database. On Debian/Ubuntu:: + + sudo apt install cloc python3 + +Use ``scripts/kernel-sloc``; ``scripts/kernel-sloc --help`` lists its modes, +requirements, and tool overrides. Successful runs print one integer on stdout +and a human-readable summary on stderr. Errors exit non-zero without a count.
[ ... ]
Documentation/admin-guide/kernel-minimization.rst line 286
Quoted 8 lines
+.. code-block:: sh
Documentation/admin-guide/kernel-minimization.rst new file +7
-0+ + # debug-info estimate: distinct source lines represented in vmlinux + scripts/kernel-sloc --report linked.json dwarf vmlinux > sloc-dwarf.txt + + # regenerate the database only after completing this clean build + python3 scripts/clang-tools/gen_compile_commands.py + scripts/kernel-sloc --report compiled.json kernel . > sloc-compiled.txt
[Severity: Low]
Is scripts/kernel-sloc missing from the tree?
The Counting kernel SLOC, Reducing the user space, and References sections instruct readers to run scripts/kernel-sloc, but this script does not exist in the repository and is not added by this patch, so those commands will fail with a missing file error.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 1/1 Documentation: admin-guide: Update workload tracing guide
In reply to Matthew WeberExpand workload-tracing.rst, which is aimed at application developers and system integrators analyzing which kernel code a workload relies on, with guidance on ftrace function graph tracing for target binaries, syscall trapping under QEMU with the GDB stub, and mapping workload observations to kernel configuration symbols.
Add the file to the Workload analysis MAINTAINERS entry.
--- .../admin-guide/workload-tracing.rst | 139 +++++++++++++++++- MAINTAINERS | 1 + 2 files changed, 133 insertions(+), 7 deletions(-)
Documentation/admin-guide/workload-tracing.rst +132 -7
@@ -176,6 +176,20 @@ by three workloads we have chose for this analysis. * stress-ng * paxtest +To retain arguments, results, and calls from child processes, run the +application with its normal inputs and environment:: + + strace -f -o syscalls.log ./app + strace -f -c -o syscall-summary.txt ./app + +These are separate runs. ``-f`` follows children and threads; ``-c`` produces +a summary rather than the chronological trace. For a narrower investigation, +add ``-e trace=%file`` to the first command to select file-related calls. +``-k`` can add user-space stack traces when strace supports unwinding; it +does not expose the kernel call stack. For an application that does not exit, +stop the tracing session after a defined observation interval. Include +startup and representative error inputs, not just a successful steady state. + What is cscope and how do we use it? ====================================
@@ -588,6 +602,116 @@ times each system call is invoked, and the corresponding Linux subsystem. | arch_prctl | 2 | Process Mgmt. | sys_arch_prctl() | +-------------------+-----------+-----------------+----------------------+ +Kernel paths reached by the workload +==================================== + +To go beyond the syscall boundary, use the function graph tracer described +in :doc:`/trace/ftrace`. In a tracing-capable test system or QEMU guest, +install ``trace-cmd`` and check that the running kernel provides +``CONFIG_FTRACE``, ``CONFIG_FUNCTION_TRACER``, and +``CONFIG_FUNCTION_GRAPH_TRACER``. Enabling ``FTRACE`` alone is not sufficient. +Check availability and collect a task-filtered graph:: + + sudo trace-cmd list -t + sudo trace-cmd record -p function_graph -F -c -O nofuncgraph-irqs \ + --user "$USER" -o functions.dat ./app + trace-cmd report -i functions.dat > functions.txt + +Use an isolated measurement system: these commands change its tracing state. +``-F`` filters to the launched application, ``-c`` follows its children, and +``--user`` avoids running the workload as root just because the recorder needs +privileges. Preserve the workload's original user and environment. The option +``nofuncgraph-irqs`` excludes interrupt execution from the graph; asynchronous +work in other tasks is outside this task filter too. Check the report for lost +events before treating missing functions as unobserved. The perf examples +above provide sampled profiles, not an exhaustive list of executed functions. + +Tracing a minimal user space +============================ + +A single-binary initramfs has no shell or guest tracing tools. Initial discovery +can use the same application and libc in a full guest for the target +architecture, but that does not reproduce its launch as PID 1. The following +inspects the actual boot with QEMU's host-side GDB stub without adding tools to +the archive. + +Follow :doc:`/process/debugging/gdb-kernel-debugging` for the debug build and +matching ``vmlinux`` setup. Add ``nokaslr`` to the guest kernel command line +and ``-S -gdb tcp:127.0.0.1:1234`` to the QEMU boot command. ``-S`` pauses the +guest before boot, allowing breakpoints to be installed. Connect host GDB:: + + gdb-multiarch /path/to/build/vmlinux + (gdb) target remote localhost:1234 + (gdb) tbreak kernel_init + (gdb) continue + +For a native 64-bit arm64 workload, the following records syscall entry numbers +and the first argument from the saved user registers. It starts after PID 1's +kernel initialization, before the application executes:: + + (gdb) set pagination off + (gdb) set logging file pid1-syscalls.log + (gdb) set logging enabled on + (gdb) break do_el0_svc + (gdb) commands + > silent + > printf "syscall=%lu arg0=%#lx\n", regs->regs[8], regs->regs[0] + > continue + > end + (gdb) continue + +Interrupt GDB with Ctrl-C after the observation interval, then disable logging. +Resolve the numbers using the matching target build's syscall-number definitions. +These are entry observations, not decoded arguments or syscall results. To +inspect a particular kernel path, disable the logging breakpoint, set a +breakpoint on its implementation, and use ``bt`` or ``step`` at the hit. +Entry functions and register layouts are architecture- and revision-specific; +check them against the matching source. Once other processes exist, an entry +breakpoint can also capture their calls; GDB threads in QEMU represent vCPUs, +not Linux tasks. This is not a task-filtered whole-kernel coverage collector. + +QEMU also offers low-level logging with ``-d`` (list options with ``-d help``) +and ``-D qemu.log``. For example, ``-d in_asm`` logs translated instruction +blocks, not every execution or a workload-specific call trace. A block that is +not logged again may still have executed again from the translation cache, so +the log cannot show that a block did not run. + +Mapping observations to configuration +------------------------------------- + +Use the source revision and architecture matching the measured kernel, rather +than assuming the syscall tables above describe every build. Locate each +syscall implementation with cscope or its ``SYSCALL_DEFINE`` declaration, +follow calls into the relevant subsystem, and inspect its Makefile and Kconfig. +For example, ``eventfd2`` is implemented in ``fs/eventfd.c``; +``fs/Makefile`` builds ``eventfd.o`` under ``CONFIG_EVENTFD``. Use the +configuration help to review its dependencies before changing the option. +Other paths, such as the core ``write`` syscall, are built unconditionally +and may reach different drivers depending on the file descriptor. A syscall +does not necessarily map to one removable option. + +Retain results as well as names: a libc may probe an optional syscall, tolerate +``ENOSYS``, and take a fallback path. Observing the call alone does not prove +that the feature must remain enabled. Conversely, boot and recovery dependencies +may be absent from an application's syscall trace. Record the observed path, +source/configuration mapping, and result of re-testing after the change. + +For the distinction between built code and observed execution, use +:doc:`/dev-tools/gcov` for execution counts and :doc:`/dev-tools/kcov` for +task-scoped kernel PCs. Follow their collection and symbolization instructions +in a separate measurement build; neither strace nor a sampled perf profile +supplies those coverage results. Unobserved code remains a candidate for +investigation, not proof of an unnecessary feature. + +Keep the built-code inventory alongside the runtime observations using +``scripts/kernel-sloc --report compiled.json kernel .`` and, for the matching +debug ELF, ``scripts/kernel-sloc --report linked.json dwarf vmlinux``. See +:doc:`kernel-minimization` ("Counting kernel SLOC") for setup and scope. The +reports list compiled files and DWARF-resolved functions/source lines, not +execution. Compare full source paths as well as function names; repeated +static names and inlining make names alone insufficient. An unobserved entry +is a review candidate, not proof that its configuration can be removed. + Conclusion ==========
@@ -597,10 +721,11 @@ information on the resources in use by workloads using strace. References ========== - * `Discovery Linux Kernel Subsystems used by OpenAPS <https://elisa.tech/blog/2022/02/02/discovery-linux-kernel-subsystems-used-by-openaps>`_ - * `ELISA-White-Papers-Discovering Linux kernel subsystems used by a workload <https://github.com/elisa-tech/ELISA-White-Papers/blob/master/Processes/Discovering_Linux_kernel_subsystems_used_by_a_workload.md>`_ - * `strace <https://man7.org/linux/man-pages/man1/strace.1.html>`_ - * `perf <https://man7.org/linux/man-pages/man1/perf.1.html>`_ - * `paxtest README <https://github.com/opntr/paxtest-freebsd/blob/hardenedbsd/0.9.14-hbsd/README>`_ - * `stress-ng <https://www.mankier.com/1/stress-ng>`_ - * `Monitoring and managing system status and performance <https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/monitoring_and_managing_system_status_and_performance/index>`_ + * `Discovery Linux Kernel Subsystems used by OpenAPS <https://elisa.tech/blog/2022/02/02/discovery-linux-kernel-subsystems-used-by-openaps>`__ + * `ELISA-White-Papers-Discovering Linux kernel subsystems used by a workload <https://github.com/elisa-tech/ELISA-White-Papers/blob/master/Processes/Discovering_Linux_kernel_subsystems_used_by_a_workload.md>`__ + * `strace <https://man7.org/linux/man-pages/man1/strace.1.html>`__ + * `perf <https://man7.org/linux/man-pages/man1/perf.1.html>`__ + * `paxtest README <https://github.com/opntr/paxtest-freebsd/blob/hardenedbsd/0.9.14-hbsd/README>`__ + * `stress-ng <https://www.mankier.com/1/stress-ng>`__ + * `Monitoring and managing system status and performance <https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/monitoring_and_managing_system_status_and_performance/index>`__ + * :doc:`Kernel minimization <kernel-minimization>`
MAINTAINERS +1 -0
@@ -28159,6 +28159,7 @@ M: Matthew Weber <[email protected]> L: [email protected] S: Maintained F: Documentation/admin-guide/kernel-minimization.rst +F: Documentation/admin-guide/workload-tracing.rst WORKQUEUE M: Tejun Heo <[email protected]>
Signature · 1 line
2.39.5