kstats: kernel metric collector
v2 cover kstats: kernel metric collector
This patchset introduces a small library to collect per-cpu samples and accumulate distributions to be exported through debugfs.
Note that use case and performance are orthogonal to those of the perf/tracing architecture, which is why this is proposed as a standalone component.
I have used this to trace the execution times of small sections of code (network stack functions, drivers and xdp processing, ...), whole functions, or notification latencies (e.g. IPI dispatch, etc.). The fine granularity of the aggregation helps identifying outliers, multimodal distributions, caching effects and general performance-related issues.
Samples are 64-bit values that are aggregated into per-cpu logarithmic buckets with configurable density (typical sample collectors use one buckets for each power of 2, powers of 2, but that is very coarse and corresponds to 1 significant bit per value; in quickstats one can specify the number of significant bits, up to 5, which is useful for finer grain measurements).
There are two ways to trace a block of code: manual annotations has the best performance at runtime and is done as follows:
// create metric and entry in debugfs
struct kstats *key = kstats_new("foo", frac_bits);
...
// instrument the code
u64 t0 = ktime_get_ns(); // about 20ns
<section of code to measure>
t0 = ktime_get_ns() - t0; // about 20ns
kstats_record(key, t0); // 5ns hot cache, 300ns coldThis method has an accuracy of about 20-30ns (inherited from the clock) and an overhead with hot/cold cache as show above.
Values can be read from debugfs in an easy to parse format
# cat /sys/kernel/debug/kstats/foo ... slot 55 CPU 0 count 589 avg 480 p 0.027613 slot 55 CPU 1 count 18 avg 480 p 0.002572 slot 55 CPU 2 count 25 avg 480 p 0.003325 ... slot 55 CPUS 28 count 814 avg 480 p 0.002474 ... slot 97 CPU 13 count 1150 avg 20130 p 0.447442 slot 97 CPUS 28 count 152585 avg 19809 p 0.651747 ...
Writing STOP, START, RESET to the file executes the corresponding action
echo RESET > /sys/kernel/debug/kstats/foo
The second instrumentation mechanism uses kretrprobes or tracepoints and lets tracing be enabled or removed at runtime from the command line for any globally visible function
echo trace some_function bits 3 > /sys/kernel/debug/kstats/_control
Data are exported or controlled in the same way as above. Accuracy is worse due to the presence of kretprobe trampolines: 90ns with hot cache, 500ns with cold cache. The overhead on the traced function is 250ns hot, 1500ns cold.
Hope you find this useful.
Luigi Rizzo (2):
kstats: kernel metric collector
kstats: kretprobe and tracepoint support
include/linux/kstats.h | 62 ++++ lib/Kconfig.debug | 7 + lib/Makefile | 1 + lib/kstats.c | 654 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 724 insertions(+) create mode 100644 include/linux/kstats.h create mode 100644 lib/kstats.c
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
PATCH v2 1/2 kstats: kernel metric collector
kstats is a helper to accumulate in-kernel metrics (timestamps, sizes, etc.) and show distributions through debugfs.
Set CONFIG_KSTATS=m or y to enable it.
Creating a metric takes one line of code (and one to destroy it):
struct kstats *key = kstats_new("foo", 3 /* frac_bits */);
...
kstats_delete(key);The following line records a u64 sample:
kstats_record(key, value);
kstats_record() is cheap (5ns hot cache, 250ns cold cache). Samples are accumulated in a per-cpu array with 2^frac_bits slots for each power of 2. Using frac_bits = 3 gives about 30 slots per decade.
Each metric has an entry in /sys/kernel/debug/kstats which can be used to read and control data collection:
- writing start/stop/reset starts and stops sample collection, or resets the counters, e.g.
echo reset > /sys/kernel/debug/kstats/foo
- reading from the node produces a detailed output that can be processed with external tools for improved presentation:
cat /sys/kernel/debug/kstats/foo
...
slot 55 CPU 0 count 589 avg 480 p 0.027613 slot 55 CPU 1 count 18 avg 480 p 0.002572 slot 55 CPU 2 count 25 avg 480 p 0.003325 ... slot 55 CPUS 16 count 814 avg 480 p 0.002474 ... slot 97 CPU 8 count 1150 avg 20130 p 0.447442 slot 97 CPU 12 count 26 avg 20294 p 0.275555 slot 97 CPUS 16 count 152585 avg 19809 p 0.651747 slot 98 CPU 0 count 38 avg 21360 p 0.954691 slot 98 CPU 1 count 456 avg 21412 p 0.872619 ... slot 144 CPUS 16 count 12 avg 1061896 p 0.999999 slot 146 CPU 4 count 1 avg 1313664 p 1.000000 slot 146 CPUS 16 count 1 avg 1313664 p 1.000000
Postprocessing examples:
look at statistics for a single CPU (note double space)
cd /sys/kernel/kstats/
echo reset > foo; watch grep "'CPU 12 '" foo
Use gnuplot to plot distribution and cdf in a terminal from a remote host
gnuplot
set terminal dumb size 200 80
set logscale x
plot "<ssh root@myhost grep CPUS /sys/kernel/debug/kstats/foo" u 8:6 w l title "distribution" plot "<ssh root@myhost grep CPUS /sys/kernel/debug/kstats/foo" u 8:10 w l title "cdf"
--- include/linux/kstats.h | 62 +++++++++ lib/Kconfig.debug | 7 + lib/Makefile | 1 + lib/kstats.c | 290 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 360 insertions(+) create mode 100644 include/linux/kstats.h create mode 100644 lib/kstats.c
include/linux/kstats.h new file +62 -0
@@ -0,0 +1,62 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_KSTATS_H +#define _LINUX_KSTATS_H + +#include <linux/types.h> + +/* +* Helper to collect and report kernel metrics. Use as follows: +* +* - creates a new debugfs entry in /sys/kernel/debug/kstats/foo +* to collect the metric, accumulating samples in 2^frac_bits slots +* per power of 2 +* +* struct kstats *key = kstats_new("foo", frac_bits); +* +* - add instrumentation around code: +* +* u64 dt = ktime_get_ns(); // about 20ns +* <section of code to measure> +* dt = ktime_get_ns() - dt; // about 20ns +* kstats_record(key, dt); // 5ns hot cache, 300ns cold +* +* - read values from debugfs +* cat /sys/kernel/debug/kstats/foo +* ... +* slot 55 CPU 0 count 589 avg 480 p 0.027613 +* slot 55 CPU 1 count 18 avg 480 p 0.002572 +* slot 55 CPU 2 count 25 avg 480 p 0.003325 +* ... +* slot 55 CPUS 28 count 814 avg 480 p 0.002474 +* ... +* slot 97 CPU 13 count 1150 avg 20130 p 0.447442 +* slot 97 CPUS 28 count 152585 avg 19809 p 0.651747 +* ... +* +* - write to the file STOP, START, RESET executes the corresponding action +* +* echo RESET > /sys/kernel/debug/kstats/foo +*/ + +struct kstats ; + +#if defined(CONFIG_KSTATS ) || defined(CONFIG_KSTATS_MODULE ) +/* Add an entry to debugfs. */ +struct kstats *kstats_new (const char *name,u8 frac_bits); + +/* Record a sample */ +void kstats_record (struct kstats *key,u64 value); + +/* Remove an entry and frees memory */ +void kstats_delete (struct kstats *key); +#else +static inline struct kstats *kstats_new (const char *name,u8 frac_bits) +{ +return NULL ; +} + +static inline void kstats_record (struct kstats *key,u64 value) {} +static inline void kstats_delete (struct kstats *key) {} +#endif + +#endif /* _LINUX_KSTATS_H */
lib/Kconfig.debug +7 -0
@@ -1452,6 +1452,13 @@ config LATENCYTOP Enable this option if you want to use the LatencyTOP tool to find out which userspace is blocking on what kernel operations. +config KSTATS + tristate "collect percpu metrics and export distributions through debugfs" + depends on DEBUG_FS + help + Helper library to collect percpu kernel metrics, exporting + distributions through debugfs. See lib/kstats.c + source "kernel/trace/Kconfig" config PROVIDE_OHCI1394_DMA_INIT
lib/Makefile +1 -0
@@ -236,6 +236,7 @@ obj-$(CONFIG_RBTREE_TEST) += rbtree_test.o obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o obj-$(CONFIG_PERCPU_TEST) += percpu_test.o +obj-$(CONFIG_KSTATS) += kstats.o obj-$(CONFIG_ASN1) += asn1_decoder.o
lib/kstats.c new file +290 -0
290 lines not on this page · raw message
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
Quoted 10 lines · Luigi Rizzo
kstats is a helper to accumulate in-kernel metrics (timestamps, sizes, etc.) and show distributions through debugfs.
SetCONFIG_KSTATS=mor y to enable it.Creating a metric takes one line of code (and one to destroy it):
struct kstats *key = kstats_new("foo", 3 /* frac_bits */); ... kstats_delete(key);
Quoted 7 lines
The following line records a u64 sample:
kstats_record(key, value);
kstats_record()is cheap (5ns hot cache, 250ns cold cache). Samples are accumulated in a per-cpu array with 2^frac_bits slots for each power of 2. Usingfrac_bits= 3 gives about 30 slots per decade.
So I think everybody + dog has written code like this, although I never bothered with the log2 based buckets myself. Nor have I ever bothered with doing a debugfs interface.
I find it very hard to convince myself something like this deserves to live upstream, vs. remaining in the local debug/hack toolbox.
Tracing has an aggregator (histogram), you can dump the raw deltas, or you can hack up a custom aggregator in a few lines, or you do BPF if you're so inclined.
Why do we need this specific one?
Quoted 16 lines · Peter Zijlstra
Quoted 14 lines · Luigi Rizzo
kstats is a helper to accumulate in-kernel metrics (timestamps, sizes, etc.) and show distributions through debugfs.
SetCONFIG_KSTATS=mor y to enable it.Creating a metric takes one line of code (and one to destroy it):
struct kstats *key = kstats_new("foo", 3 /* frac_bits */); ... kstats_delete(key);The following line records a u64 sample:
kstats_record(key, value);
Quoted 7 lines
Quoted 3 lines
kstats_record()is cheap (5ns hot cache, 250ns cold cache). Samples are accumulated in a per-cpu array with 2^frac_bits slots for each power of 2. Usingfrac_bits= 3 gives about 30 slots per decade.So I think everybody + dog has written code like this, although I never bothered with the log2 based buckets myself. Nor have I ever bothered with doing a debugfs interface.
the above is perhaps one excellent argument to why it may deserve to be in:
so that people don't need to write the measurement code time and again, or, as I have done myself multiple times, use some inferior hack (racy counter, coarse buckets) or give up measuring things and rely on guessing.
Quoted 6 lines
I find it very hard to convince myself something like this deserves to live upstream, vs. remaining in the local debug/hack toolbox.
Tracing has an aggregator (histogram), you can dump the raw deltas, or you can hack up a custom aggregator in a few lines, or you do BPF if you're so inclined.
And this is possibly another good argument: sometimes the systems where it would be interesting to collect data are not accessible to the developers with skills to write the monitoring code and run a modified kernel.
cheers
luigi
Hi Luigi,
Quoted 33 lines · On Wed, 26 Feb 2020 11:31:01 -0800 Luigi Rizzo <[email protected]> wrote:
Quoted 16 lines · Peter Zijlstra
Quoted 14 lines · Luigi Rizzo
kstats is a helper to accumulate in-kernel metrics (timestamps, sizes, etc.) and show distributions through debugfs.
SetCONFIG_KSTATS=mor y to enable it.Creating a metric takes one line of code (and one to destroy it):
struct kstats *key = kstats_new("foo", 3 /* frac_bits */); ... kstats_delete(key);The following line records a u64 sample:
kstats_record(key, value);Quoted 7 lines
Quoted 3 lines
kstats_record()is cheap (5ns hot cache, 250ns cold cache). Samples are accumulated in a per-cpu array with 2^frac_bits slots for each power of 2. Usingfrac_bits= 3 gives about 30 slots per decade.So I think everybody + dog has written code like this, although I never bothered with the log2 based buckets myself. Nor have I ever bothered with doing a debugfs interface.
the above is perhaps one excellent argument to why it may deserve to be in:
so that people don't need to write the measurement code time and again, or, as I have done myself multiple times, use some inferior hack (racy counter, coarse buckets) or give up measuring things and rely on guessing.Quoted 3 lines
I find it very hard to convince myself something like this deserves to live upstream, vs. remaining in the local debug/hack toolbox.
Quoted 7 lines
Quoted 3 lines
Tracing has an aggregator (histogram), you can dump the raw deltas, or you can hack up a custom aggregator in a few lines, or you do BPF if you're so inclined.
And this is possibly another good argument: sometimes the systems where it would be interesting to collect data are not accessible to the developers with skills to write the monitoring code and run a modified kernel.
Would you mean the histogram requires more skills to use it?
I think Peter's point is that there is already an "ability" and "interface" to do that in kenrel. If you think that is not easy to use, can you modify existing features or add a user tool to use it easier?
Thank you,
Signature · 1 line
Masami Hiramatsu <[email protected]>