selftests/bpf: Add benchmark for local_storage get
Subsystems (3)
PATCH v4 1/2 selftests/bpf: Add benchmark for local_storage get
Add a benchmarks to demonstrate the performance cliff for local_storage get as the number of local_storage maps increases beyond current local_storage implementation's cache size.
"sequential get" and "interleaved get" benchmarks are added, both of which do many bpf_task_storage_get calls on sets of task local_storage maps of various counts, while considering a single specific map to be 'important' and counting task_storage_gets to the important map separately in addition to normal 'hits' count of all gets. Goal here is to mimic scenario where a particular program using one map - the important one - is running on a system where many other local_storage maps exist and are accessed often.
While "sequential get" benchmark does bpf_task_storage_get for map 0, 1, ..., {9, 99, 999} in order, "interleaved" benchmark interleaves 4 bpf_task_storage_gets for the important map for every 10 map gets. This is meant to highlight performance differences when important map is accessed far more frequently than non-important maps.
A "hashmap control" benchmark is also included for easy comparison of standard bpf hashmap lookup vs local_storage get. The benchmark is identical to "sequential get", but creates and uses BPF_MAP_TYPE_HASH instead of local storage.
Addition of this benchmark is inspired by conversation with Alexei in a previous patchset's thread [0], which highlighted the need for such a benchmark to motivate and validate improvements to local_storage implementation. My approach in that series focused on improving performance for explicitly-marked 'important' maps and was rejected with feedback to make more generally-applicable improvements while avoiding explicitly marking maps as important. Thus the benchmark reports both general and important-map-focused metrics, so effect of future work on both is clear.
Regarding the benchmark results. On a powerful system (Skylake, 20 cores, 256gb ram):
Local Storage
=============
Hashmap Control w/ 500 maps
hashmap (control) sequential get: hits throughput: 48.338 ± 2.366 M ops/s, hits latency: 20.688 ns/op, important_hits throughput: 0.097 ± 0.005 M ops/s
num_maps: 1
local_storage cache sequential get: hits throughput: 44.503 ± 1.080 M ops/s, hits latency: 22.470 ns/op, important_hits throughput: 44.503 ± 1.080 M ops/s local_storage cache interleaved get: hits throughput: 54.963 ± 0.586 M ops/s, hits latency: 18.194 ns/op, important_hits throughput: 54.963 ± 0.586 M ops/s
num_maps: 10
local_storage cache sequential get: hits throughput: 43.743 ± 0.418 M ops/s, hits latency: 22.861 ns/op, important_hits throughput: 4.374 ± 0.042 M ops/s local_storage cache interleaved get: hits throughput: 50.073 ± 0.609 M ops/s, hits latency: 19.971 ns/op, important_hits throughput: 17.883 ± 0.217 M ops/s
num_maps: 16
local_storage cache sequential get: hits throughput: 43.962 ± 0.525 M ops/s, hits latency: 22.747 ns/op, important_hits throughput: 2.748 ± 0.033 M ops/s local_storage cache interleaved get: hits throughput: 48.166 ± 0.825 M ops/s, hits latency: 20.761 ns/op, important_hits throughput: 15.326 ± 0.263 M ops/s
num_maps: 17
local_storage cache sequential get: hits throughput: 33.207 ± 0.461 M ops/s, hits latency: 30.114 ns/op, important_hits throughput: 1.956 ± 0.027 M ops/s local_storage cache interleaved get: hits throughput: 43.540 ± 0.265 M ops/s, hits latency: 22.968 ns/op, important_hits throughput: 13.255 ± 0.081 M ops/s
num_maps: 24
local_storage cache sequential get: hits throughput: 19.402 ± 0.348 M ops/s, hits latency: 51.542 ns/op, important_hits throughput: 0.809 ± 0.015 M ops/s local_storage cache interleaved get: hits throughput: 22.981 ± 0.487 M ops/s, hits latency: 43.514 ns/op, important_hits throughput: 6.465 ± 0.137 M ops/s
num_maps: 32
local_storage cache sequential get: hits throughput: 13.378 ± 0.220 M ops/s, hits latency: 74.748 ns/op, important_hits throughput: 0.419 ± 0.007 M ops/s local_storage cache interleaved get: hits throughput: 16.894 ± 0.172 M ops/s, hits latency: 59.193 ns/op, important_hits throughput: 4.716 ± 0.048 M ops/s
num_maps: 100
local_storage cache sequential get: hits throughput: 6.070 ± 0.140 M ops/s, hits latency: 164.745 ns/op, important_hits throughput: 0.061 ± 0.001 M ops/s local_storage cache interleaved get: hits throughput: 7.323 ± 0.149 M ops/s, hits latency: 136.554 ns/op, important_hits throughput: 1.913 ± 0.039 M ops/s
num_maps: 1000
local_storage cache sequential get: hits throughput: 0.438 ± 0.012 M ops/s, hits latency: 2281.369 ns/op, important_hits throughput: 0.000 ± 0.000 M ops/s local_storage cache interleaved get: hits throughput: 0.522 ± 0.010 M ops/s, hits latency: 1913.937 ns/op, important_hits throughput: 0.131 ± 0.003 M ops/s
Looking at the "sequential get" results, it's clear that as the number of task local_storage maps grows beyond the current cache size (16), there's a significant reduction in hits throughput. Note that current local_storage implementation assigns a cache_idx to maps as they are created. Since "sequential get" is creating maps 0..n in order and then doing bpf_task_storage_get calls in the same order, the benchmark is effectively ensuring that a map will not be in cache when the program tries to access it.
For "interleaved get" results, important-map hits throughput is greatly increased as the important map is more likely to be in cache by virtue of being accessed far more frequently. Throughput still reduces as # maps increases, though.
To get a sense of the overhead of the benchmark program, I commented out bpf_task_storage_get/bpf_map_lookup_elem in local_storage_bench.c and ran the benchmark on the same host as the 'real' run. Results:
Local Storage
=============
Hashmap Control w/ 500 maps
hashmap (control) sequential get: hits throughput: 96.965 ± 1.346 M ops/s, hits latency: 10.313 ns/op, important_hits throughput: 0.194 ± 0.003 M ops/s
num_maps: 1
local_storage cache sequential get: hits throughput: 105.792 ± 1.860 M ops/s, hits latency: 9.453 ns/op, important_hits throughput: 105.792 ± 1.860 M ops/s local_storage cache interleaved get: hits throughput: 185.847 ± 4.014 M ops/s, hits latency: 5.381 ns/op, important_hits throughput: 185.847 ± 4.014 M ops/s
num_maps: 10
local_storage cache sequential get: hits throughput: 109.867 ± 1.358 M ops/s, hits latency: 9.102 ns/op, important_hits throughput: 10.987 ± 0.136 M ops/s local_storage cache interleaved get: hits throughput: 144.165 ± 1.256 M ops/s, hits latency: 6.936 ns/op, important_hits throughput: 51.487 ± 0.449 M ops/s
num_maps: 16
local_storage cache sequential get: hits throughput: 109.258 ± 1.902 M ops/s, hits latency: 9.153 ns/op, important_hits throughput: 6.829 ± 0.119 M ops/s local_storage cache interleaved get: hits throughput: 140.248 ± 1.836 M ops/s, hits latency: 7.130 ns/op, important_hits throughput: 44.624 ± 0.584 M ops/s
num_maps: 17
local_storage cache sequential get: hits throughput: 116.397 ± 7.679 M ops/s, hits latency: 8.591 ns/op, important_hits throughput: 6.856 ± 0.452 M ops/s local_storage cache interleaved get: hits throughput: 128.411 ± 4.927 M ops/s, hits latency: 7.787 ns/op, important_hits throughput: 39.093 ± 1.500 M ops/s
num_maps: 24
local_storage cache sequential get: hits throughput: 110.890 ± 0.976 M ops/s, hits latency: 9.018 ns/op, important_hits throughput: 4.624 ± 0.041 M ops/s local_storage cache interleaved get: hits throughput: 133.316 ± 1.889 M ops/s, hits latency: 7.501 ns/op, important_hits throughput: 37.503 ± 0.531 M ops/s
num_maps: 32
local_storage cache sequential get: hits throughput: 112.900 ± 1.171 M ops/s, hits latency: 8.857 ns/op, important_hits throughput: 3.534 ± 0.037 M ops/s local_storage cache interleaved get: hits throughput: 132.844 ± 1.207 M ops/s, hits latency: 7.528 ns/op, important_hits throughput: 37.081 ± 0.337 M ops/s
num_maps: 100
local_storage cache sequential get: hits throughput: 110.025 ± 4.714 M ops/s, hits latency: 9.089 ns/op, important_hits throughput: 1.100 ± 0.047 M ops/s local_storage cache interleaved get: hits throughput: 131.979 ± 5.013 M ops/s, hits latency: 7.577 ns/op, important_hits throughput: 34.472 ± 1.309 M ops/s
num_maps: 1000
local_storage cache sequential get: hits throughput: 117.850 ± 2.423 M ops/s, hits latency: 8.485 ns/op, important_hits throughput: 0.118 ± 0.002 M ops/s local_storage cache interleaved get: hits throughput: 141.268 ± 9.658 M ops/s, hits latency: 7.079 ns/op, important_hits throughput: 35.476 ± 2.425 M ops/s
Adjusting for overhead, latency numbers for "hashmap control" and "sequential get" are:
hashmap_control: ~10.4ns sequential_get_1: ~13.0ns sequential_get_10: ~13.8ns sequential_get_16: ~13.6ns sequential_get_17: ~21.5ns sequential_get_24: ~42.5ns sequential_get_32: ~65.9ns sequential_get_100: ~155.7ns sequential_get_1000: ~2270ns
Clearly demonstrating a cliff.
When running the benchmarks it may be necessary to bump 'open files' ulimit for a successful run.
[0]: https://lore.kernel.org/all/[email protected]
---
Changelog:
v3 -> v4:
* Remove ifs guarding increments in measure fn (Andrii)
* Refactor to use 1 bpf prog for all 3 benchmarks w/ global vars set from userspace before load to control behavior (Andrii)
* Greatly reduce variance in overhead by having benchmark bpf prog loop 10k times regardless of map count (Andrii)
* Also, move sync_fetch_and_incr out of do_lookup as the guaranteed second sync_fetch_and_incr call for num_maps = 1 was adding overhead
* Add second patch refactoring bench.c's mean/stddev calculations in reporting helper fns
v2 -> v3:
* Accessing 1k maps in ARRAY_OF_MAPS doesn't hit MAX_USED_MAPS limit, so just use 1 program (Alexei)
v1 -> v2:
* Adopt ARRAY_OF_MAPS approach for bpf program, allowing truly configurable # of maps (Andrii)
* Add hashmap benchmark (Alexei)
* Add discussion of overhead
tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bench.c | 55 ++++ tools/testing/selftests/bpf/bench.h | 4 + .../bpf/benchs/bench_local_storage.c | 250 ++++++++++++++++++ .../bpf/benchs/run_bench_local_storage.sh | 21 ++ .../selftests/bpf/benchs/run_common.sh | 17 ++ .../selftests/bpf/progs/local_storage_bench.c | 99 +++++++ 7 files changed, 449 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/benchs/bench_local_storage.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_local_storage.sh create mode 100644 tools/testing/selftests/bpf/progs/local_storage_bench.c
tools/testing/selftests/bpf/Makefile +3 -1
@@ -560,6 +560,7 @@ $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \ $(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_bench.skel.h $(OUTPUT)/bench_bpf_loop.o: $(OUTPUT)/bpf_loop_bench.skel.h $(OUTPUT)/bench_strncmp.o: $(OUTPUT)/strncmp_bench.skel.h +$(OUTPUT)/bench_local_storage.o: $(OUTPUT)/local_storage_bench.skel.h $(OUTPUT)/bench.o: bench.h testing_helpers.h $(BPFOBJ) $(OUTPUT)/bench: LDLIBS += -lm $(OUTPUT)/bench: $(OUTPUT)/bench.o \
@@ -571,7 +572,8 @@ $(OUTPUT)/bench: $(OUTPUT)/bench.o \ $(OUTPUT)/bench_ringbufs.o \ $(OUTPUT)/bench_bloom_filter_map.o \ $(OUTPUT)/bench_bpf_loop.o \ - $(OUTPUT)/bench_strncmp.o + $(OUTPUT)/bench_strncmp.o \ + $(OUTPUT)/bench_local_storage.o $(call msg,BINARY,,$@) $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(filter %.a %.o,$^) $(LDLIBS) -o $@
tools/testing/selftests/bpf/bench.c +55 -0
85 lines not on this page · raw message
tools/testing/selftests/bpf/bench.h +4 -0
@@ -34,6 +34,7 @@ struct bench_res {long hits;long drops;long false_hits; +long important_hits; };struct bench {
@@ -61,6 +62,9 @@ void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns);void false_hits_report_final (struct bench_res res[],int res_cnt);void ops_report_progress (int iter,struct bench_res *res,long delta_ns);void ops_report_final (struct bench_res res[],int res_cnt); +void local_storage_report_progress (int iter,struct bench_res *res, +long delta_ns); +void local_storage_report_final (struct bench_res res[],int res_cnt);static inline __u64 get_time_ns (void ) {
tools/testing/selftests/bpf/benchs/bench_local_storage.c new file +250 -0
250 lines not on this page · raw message
tools/testing/selftests/bpf/benchs/run_bench_local_storage.sh new file +21 -0
@@ -0,0 +1,21 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source ./benchs/run_common.sh + +set -eufo pipefail + +header "Local Storage" +subtitle "Hashmap Control w/ 500 maps" + summarize_local_storage "hashmap (control) sequential get: "\ + "$(./bench --nr_maps 500 local-storage-cache-hashmap-control)" + printf "\n" + +for i in 1 10 16 17 24 32 100 1000; do +subtitle "num_maps: $i" + summarize_local_storage "local_storage cache sequential get: "\ + "$(./bench --nr_maps $i local-storage-cache-seq-get)" + summarize_local_storage "local_storage cache interleaved get: "\ + "$(./bench --nr_maps $i local-storage-cache-int-get)" + printf "\n" +done
tools/testing/selftests/bpf/benchs/run_common.sh +17 -0
@@ -41,6 +41,16 @@ function ops() echo "$*" | sed -E "s/.*latency\s+([0-9]+\.[0-9]+\sns\/op).*/\1/" } +function local_storage() +{ + echo -n "hits throughput: " + echo -n "$*" | sed -E "s/.* hits throughput\s+([0-9]+\.[0-9]+ ± [0-9]+\.[0-9]+\sM\sops\/s).*/\1/" + echo -n -e ", hits latency: " + echo -n "$*" | sed -E "s/.* hits latency\s+([0-9]+\.[0-9]+\sns\/op).*/\1/" + echo -n ", important_hits throughput: " + echo "$*" | sed -E "s/.*important_hits throughput\s+([0-9]+\.[0-9]+ ± [0-9]+\.[0-9]+\sM\sops\/s).*/\1/" +} + function total() { echo "$*" | sed -E "s/.*total operations\s+([0-9]+\.[0-9]+ ± [0-9]+\.[0-9]+M\/s).*/\1/"
@@ -67,6 +77,13 @@ function summarize_ops() printf "%-20s %s\n" "$bench" "$(ops $summary)" } +function summarize_local_storage() +{ + bench="$1" + summary=$(echo $2 | tail -n1) + printf "%-20s %s\n" "$bench" "$(local_storage $summary)" +} + function summarize_total() { bench="$1"
tools/testing/selftests/bpf/progs/local_storage_bench.c new file +99 -0
99 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v4 2/2 selftests/bpf: refactor bench reporting functions
The report_progress and report_final functions in bench.c do similar calculation of mean and stddev, but each of these rewrites the entire calculation. Add helpers which calculate mean and stddev for simple fields of struct bench_res, or in the case of total_ops, sum of two fields, and move all extant calculations of mean and stddev to use the helpers.
Also add some macros for unit conversion constants to improve readability.
--- tools/testing/selftests/bpf/bench.c | 156 +++++++++++++++++----------- 1 file changed, 95 insertions(+), 61 deletions(-)
tools/testing/selftests/bpf/bench.c +95 -61
@@ -28,6 +28,62 @@ static int libbpf_print_fn(enum libbpf_print_level level,return vfprintf (stderr, format, args); } +#define benchres_getter (field) bench_res__ ## field +#define DEFINE_BENCHRES_GETTER (field) \ +static long benchres_getter(field)(struct bench_res *res) { return res->field; } + +DEFINE_BENCHRES_GETTER (hits); +DEFINE_BENCHRES_GETTER (drops); +DEFINE_BENCHRES_GETTER (important_hits); +static long benchres_getter (total_ops )(struct bench_res *res) +{ +return res->hits + res->drops; +} + +static double calc_mean_long (struct bench_res res[],int res_cnt, +long (*fn)(struct bench_res *res)) +{ +double sum; +int i; + +for (i =0 , sum =0 ; i < res_cnt; i++) + sum +=fn (&res[i]); +return sum / res_cnt; +} + +#define calc_mean_hits (res, res_cnt) calc_mean_long(res, res_cnt, benchres_getter(hits)) +#define calc_mean_drops (res, res_cnt) calc_mean_long(res, res_cnt, benchres_getter(drops)) +#define calc_mean_important_hits (res, res_cnt) \ + calc_mean_long(res, res_cnt, benchres_getter(important_hits)) +#define calc_mean_total_ops (res, res_cnt) calc_mean_long(res, res_cnt, benchres_getter(total_ops)) + +static double calc_stddev_long (struct bench_res res[],int res_cnt, +double mean,long (*fn)(struct bench_res *res)) +{ +double sum; +long val; +int i; + +for (i =0 , sum =0 ; i < res_cnt; i++) { + val =fn (&res[i]); + sum += (mean - val) * (mean - val) / (res_cnt -1.0 ); + } +return sqrt (sum); +} + +#define calc_stddev_hits (res, res_cnt, mean) \ + calc_stddev_long(res, res_cnt, mean, benchres_getter(hits)) +#define calc_stddev_drops (res, res_cnt, mean) \ + calc_stddev_long(res, res_cnt, mean, benchres_getter(drops)) +#define calc_stddev_important_hits (res, res_cnt, mean) \ + calc_stddev_long(res, res_cnt, mean, benchres_getter(important_hits)) +#define calc_stddev_total_ops (res, res_cnt, mean) \ + calc_stddev_long(res, res_cnt, mean, benchres_getter(total_ops)) + +#define NS_IN_SEC 1000000000 +#define NS_IN_SEC_F 1000000000.0 +#define MILLION_F 1000000.0 +void setup_libbpf (void ) {libbpf_set_strict_mode (LIBBPF_STRICT_ALL );
@@ -39,7 +95,7 @@ void false_hits_report_progress(int iter, struct bench_res *res, long delta_ns)long total = res->false_hits + res->hits + res->drops;printf ("Iter %3d (%7.3lfus): " , - iter, (delta_ns -1000000000 ) /1000.0 ); + iter, (delta_ns -NS_IN_SEC ) /1000.0 );printf ("%ld false hits of %ld total operations. Percentage = %2.2f %%\n" , res->false_hits, total, ((float )res->false_hits / total) *100 );
@@ -68,12 +124,12 @@ void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns)double hits_per_sec, drops_per_sec;double hits_per_prod; - hits_per_sec = res->hits /1000000.0 / (delta_ns /1000000000.0 ); + hits_per_sec = res->hits /MILLION_F / (delta_ns /NS_IN_SEC_F ); hits_per_prod = hits_per_sec / env.producer_cnt; - drops_per_sec = res->drops /1000000.0 / (delta_ns /1000000000.0 ); + drops_per_sec = res->drops /MILLION_F / (delta_ns /NS_IN_SEC_F );printf ("Iter %3d (%7.3lfus): " , - iter, (delta_ns -1000000000 ) /1000.0 ); + iter, (delta_ns -NS_IN_SEC ) /1000.0 );printf ("hits %8.3lfM/s (%7.3lfM/prod), drops %8.3lfM/s, total operations %8.3lfM/s\n" , hits_per_sec, hits_per_prod, drops_per_sec, hits_per_sec + drops_per_sec);
@@ -81,34 +137,26 @@ void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns)void hits_drops_report_final (struct bench_res res[],int res_cnt) { -int i;double hits_mean =0.0 , drops_mean =0.0 , total_ops_mean =0.0 ;double hits_stddev =0.0 , drops_stddev =0.0 , total_ops_stddev =0.0 ; -double total_ops; -for (i =0 ; i < res_cnt; i++) { - hits_mean += res[i].hits /1000000.0 / (0.0 + res_cnt); - drops_mean += res[i].drops /1000000.0 / (0.0 + res_cnt); - } - total_ops_mean = hits_mean + drops_mean; + hits_mean =calc_mean_hits (res, res_cnt); + drops_mean =calc_mean_drops (res, res_cnt); + total_ops_mean =calc_mean_total_ops (res, res_cnt);if (res_cnt >1 ) { -for (i =0 ; i < res_cnt; i++) { - hits_stddev += (hits_mean - res[i].hits /1000000.0 ) * - (hits_mean - res[i].hits /1000000.0 ) / - (res_cnt -1.0 ); - drops_stddev += (drops_mean - res[i].drops /1000000.0 ) * - (drops_mean - res[i].drops /1000000.0 ) / - (res_cnt -1.0 ); - total_ops = res[i].hits + res[i].drops; - total_ops_stddev += (total_ops_mean - total_ops /1000000.0 ) * - (total_ops_mean - total_ops /1000000.0 ) / - (res_cnt -1.0 ); - } - hits_stddev =sqrt (hits_stddev); - drops_stddev =sqrt (drops_stddev); - total_ops_stddev =sqrt (total_ops_stddev); + hits_stddev =calc_stddev_hits (res, res_cnt, hits_mean); + drops_stddev =calc_stddev_drops (res, res_cnt, drops_mean); + total_ops_stddev =calc_stddev_total_ops (res, res_cnt, total_ops_mean); } + + hits_mean /=MILLION_F ; + drops_mean /=MILLION_F ; + total_ops_mean /=MILLION_F ; + hits_stddev /=MILLION_F ; + drops_stddev /=MILLION_F ; + total_ops_stddev /=MILLION_F ; +printf ("Summary: hits %8.3lf \u00B1 %5.3lfM/s (%7.3lfM/prod), " , hits_mean, hits_stddev, hits_mean / env.producer_cnt); printf("drops %8.3lf \u00B1 %5.3lfM/s, ",
@@ -121,10 +169,10 @@ void ops_report_progress(int iter, struct bench_res *res, long delta_ns) {double hits_per_sec, hits_per_prod; - hits_per_sec = res->hits /1000000.0 / (delta_ns /1000000000.0 ); + hits_per_sec = res->hits /MILLION_F / (delta_ns /NS_IN_SEC_F ); hits_per_prod = hits_per_sec / env.producer_cnt; -printf ("Iter %3d (%7.3lfus): " , iter, (delta_ns -1000000000 ) /1000.0 ); +printf ("Iter %3d (%7.3lfus): " , iter, (delta_ns -NS_IN_SEC ) /1000.0 );printf ("hits %8.3lfM/s (%7.3lfM/prod)\n" , hits_per_sec, hits_per_prod); }
@@ -132,19 +180,13 @@ void ops_report_progress(int iter, struct bench_res *res, long delta_ns)void ops_report_final (struct bench_res res[],int res_cnt) {double hits_mean =0.0 , hits_stddev =0.0 ; -int i; - -for (i =0 ; i < res_cnt; i++) - hits_mean += res[i].hits /1000000.0 / (0.0 + res_cnt); -if (res_cnt >1 ) { -for (i =0 ; i < res_cnt; i++) - hits_stddev += (hits_mean - res[i].hits /1000000.0 ) * - (hits_mean - res[i].hits /1000000.0 ) / - (res_cnt -1.0 ); + hits_mean =calc_mean_hits (res, res_cnt); +if (res_cnt >1 ) + hits_stddev =calc_stddev_hits (res, res_cnt, hits_mean); + hits_mean /=MILLION_F ; + hits_stddev /=MILLION_F ; - hits_stddev =sqrt (hits_stddev); - }printf ("Summary: throughput %8.3lf \u00B1 %5.3lf M ops/s (%7.3lfM ops/prod), " , hits_mean, hits_stddev, hits_mean / env.producer_cnt);printf ("latency %8.3lf ns/op\n" ,1000.0 / hits_mean * env.producer_cnt);
@@ -154,12 +196,12 @@ void local_storage_report_progress(int iter, struct bench_res *res, long delta_ns) {double important_hits_per_sec, hits_per_sec; -double delta_sec = delta_ns /1000000000.0 ; +double delta_sec = delta_ns /NS_IN_SEC_F ; - hits_per_sec = res->hits /1000000.0 / delta_sec; - important_hits_per_sec = res->important_hits /1000000.0 / delta_sec; + hits_per_sec = res->hits /MILLION_F / delta_sec; + important_hits_per_sec = res->important_hits /MILLION_F / delta_sec; -printf ("Iter %3d (%7.3lfus): " , iter, (delta_ns -1000000000 ) /1000.0 ); +printf ("Iter %3d (%7.3lfus): " , iter, (delta_ns -NS_IN_SEC ) /1000.0 );printf ("hits %8.3lfM/s " , hits_per_sec);printf ("important_hits %8.3lfM/s\n" , important_hits_per_sec);
@@ -169,27 +211,19 @@ void local_storage_report_final(struct bench_res res[], int res_cnt) {double important_hits_mean =0.0 , important_hits_stddev =0.0 ;double hits_mean =0.0 , hits_stddev =0.0 ; -int i; -for (i =0 ; i < res_cnt; i++) { - hits_mean += res[i].hits /1000000.0 / (0.0 + res_cnt); - important_hits_mean += res[i].important_hits /1000000.0 / (0.0 + res_cnt); + hits_mean =calc_mean_hits (res, res_cnt); + important_hits_mean =calc_mean_important_hits (res, res_cnt); +if (res_cnt >1 ) { + hits_stddev =calc_stddev_hits (res, res_cnt, hits_mean); + important_hits_stddev = +calc_stddev_important_hits (res, res_cnt, important_hits_mean); } + hits_mean /=MILLION_F ; + important_hits_mean /=MILLION_F ; + hits_stddev /=MILLION_F ; + important_hits_stddev /=MILLION_F ; -if (res_cnt >1 ) { -for (i =0 ; i < res_cnt; i++) { - hits_stddev += (hits_mean - res[i].hits /1000000.0 ) * - (hits_mean - res[i].hits /1000000.0 ) / - (res_cnt -1.0 ); - important_hits_stddev += - (important_hits_mean - res[i].important_hits /1000000.0 ) * - (important_hits_mean - res[i].important_hits /1000000.0 ) / - (res_cnt -1.0 ); - } - - hits_stddev =sqrt (hits_stddev); - important_hits_stddev =sqrt (important_hits_stddev); - }printf ("Summary: hits throughput %8.3lf \u00B1 %5.3lf M ops/s, " , hits_mean, hits_stddev);printf ("hits latency %8.3lf ns/op, " ,1000.0 / hits_mean);
Signature · 1 line
2.30.2
Re: [PATCH v4 bpf-next 2/2] selftests/bpf: refactor bench reporting functions
Quoted 8 lines · Dave Marchevsky
The
report_progressandreport_finalfunctions inbench.cdo similar calculation of mean and stddev, but each of these rewrites the entire calculation. Add helpers which calculate mean and stddev for simple fields of structbench_res, or in the case oftotal_ops, sum of two fields, and move all extant calculations of mean and stddev to use the helpers.
Quoted 8 lines
Also add some macros for unit conversion constants to improve readability.
Signed-off-by Dave Marchevsky <[email protected]>--- tools/testing/selftests/bpf/bench.c | 156 +++++++++++++++++----------- 1 file changed, 95 insertions(+), 61 deletions(-)
please drop this patch, it doesn't look like an improvement
Quoted 8 lines
tools/testing/selftests/bpf/bench.c +0
-0@@ -28,6 +28,62 @@ static int libbpf_print_fn(enum libbpf_print_level level,return vfprintf (stderr, format, args); }
[...]
Quoted 127 lines · Dave Marchevsky
Add a benchmarks to demonstrate the performance cliff for
local_storageget as the number oflocal_storagemaps increases beyond currentlocal_storageimplementation's cache size."sequential get" and "interleaved get" benchmarks are added, both of which do many
bpf_task_storage_getcalls on sets of tasklocal_storagemaps of various counts, while considering a single specific map to be 'important' and countingtask_storage_getsto the important map separately in addition to normal 'hits' count of all gets. Goal here is to mimic scenario where a particular program using one map - the important one - is running on a system where many otherlocal_storagemaps exist and are accessed often.While "sequential get" benchmark does
bpf_task_storage_getfor map 0, 1, ..., {9, 99, 999} in order, "interleaved" benchmark interleaves 4bpf_task_storage_getsfor the important map for every 10 map gets. This is meant to highlight performance differences when important map is accessed far more frequently than non-important maps.A "hashmap control" benchmark is also included for easy comparison of standard bpf hashmap lookup vs
local_storageget. The benchmark is identical to "sequential get", but creates and usesBPF_MAP_TYPE_HASHinstead of local storage.Addition of this benchmark is inspired by conversation with Alexei in a previous patchset's thread [0], which highlighted the need for such a benchmark to motivate and validate improvements to
local_storageimplementation. My approach in that series focused on improving performance for explicitly-marked 'important' maps and was rejected with feedback to make more generally-applicable improvements while avoiding explicitly marking maps as important. Thus the benchmark reports both general and important-map-focused metrics, so effect of future work on both is clear.Regarding the benchmark results. On a powerful system (Skylake, 20 cores, 256gb ram):
Local Storage
=============
Hashmap Control w/ 500 maps
hashmap (control) sequential get: hits throughput: 48.338 ± 2.366 M ops/s, hits latency: 20.688 ns/op,important_hitsthroughput: 0.097 ± 0.005 M ops/snum_maps: 1
local_storagecache sequential get: hits throughput: 44.503 ± 1.080 M ops/s, hits latency: 22.470 ns/op,important_hitsthroughput: 44.503 ± 1.080 M ops/slocal_storagecache interleaved get: hits throughput: 54.963 ± 0.586 M ops/s, hits latency: 18.194 ns/op,important_hitsthroughput: 54.963 ± 0.586 M ops/snum_maps: 10
local_storagecache sequential get: hits throughput: 43.743 ± 0.418 M ops/s, hits latency: 22.861 ns/op,important_hitsthroughput: 4.374 ± 0.042 M ops/slocal_storagecache interleaved get: hits throughput: 50.073 ± 0.609 M ops/s, hits latency: 19.971 ns/op,important_hitsthroughput: 17.883 ± 0.217 M ops/snum_maps: 16
local_storagecache sequential get: hits throughput: 43.962 ± 0.525 M ops/s, hits latency: 22.747 ns/op,important_hitsthroughput: 2.748 ± 0.033 M ops/slocal_storagecache interleaved get: hits throughput: 48.166 ± 0.825 M ops/s, hits latency: 20.761 ns/op,important_hitsthroughput: 15.326 ± 0.263 M ops/snum_maps: 17
local_storagecache sequential get: hits throughput: 33.207 ± 0.461 M ops/s, hits latency: 30.114 ns/op,important_hitsthroughput: 1.956 ± 0.027 M ops/slocal_storagecache interleaved get: hits throughput: 43.540 ± 0.265 M ops/s, hits latency: 22.968 ns/op,important_hitsthroughput: 13.255 ± 0.081 M ops/snum_maps: 24
local_storagecache sequential get: hits throughput: 19.402 ± 0.348 M ops/s, hits latency: 51.542 ns/op,important_hitsthroughput: 0.809 ± 0.015 M ops/slocal_storagecache interleaved get: hits throughput: 22.981 ± 0.487 M ops/s, hits latency: 43.514 ns/op,important_hitsthroughput: 6.465 ± 0.137 M ops/snum_maps: 32
local_storagecache sequential get: hits throughput: 13.378 ± 0.220 M ops/s, hits latency: 74.748 ns/op,important_hitsthroughput: 0.419 ± 0.007 M ops/slocal_storagecache interleaved get: hits throughput: 16.894 ± 0.172 M ops/s, hits latency: 59.193 ns/op,important_hitsthroughput: 4.716 ± 0.048 M ops/snum_maps: 100
local_storagecache sequential get: hits throughput: 6.070 ± 0.140 M ops/s, hits latency: 164.745 ns/op,important_hitsthroughput: 0.061 ± 0.001 M ops/slocal_storagecache interleaved get: hits throughput: 7.323 ± 0.149 M ops/s, hits latency: 136.554 ns/op,important_hitsthroughput: 1.913 ± 0.039 M ops/snum_maps: 1000
local_storagecache sequential get: hits throughput: 0.438 ± 0.012 M ops/s, hits latency: 2281.369 ns/op,important_hitsthroughput: 0.000 ± 0.000 M ops/slocal_storagecache interleaved get: hits throughput: 0.522 ± 0.010 M ops/s, hits latency: 1913.937 ns/op,important_hitsthroughput: 0.131 ± 0.003 M ops/sLooking at the "sequential get" results, it's clear that as the number of task
local_storagemaps grows beyond the current cache size (16), there's a significant reduction in hits throughput. Note that currentlocal_storageimplementation assigns acache_idxto maps as they are created. Since "sequential get" is creating maps 0..n in order and then doingbpf_task_storage_getcalls in the same order, the benchmark is effectively ensuring that a map will not be in cache when the program tries to access it.For "interleaved get" results, important-map hits throughput is greatly increased as the important map is more likely to be in cache by virtue of being accessed far more frequently. Throughput still reduces as # maps increases, though.
To get a sense of the overhead of the benchmark program, I commented out bpf_task_storage_get/bpf_map_lookup_elem in
local_storage_bench.cand ran the benchmark on the same host as the 'real' run. Results:Local Storage
=============
Hashmap Control w/ 500 maps
hashmap (control) sequential get: hits throughput: 96.965 ± 1.346 M ops/s, hits latency: 10.313 ns/op,important_hitsthroughput: 0.194 ± 0.003 M ops/snum_maps: 1
local_storagecache sequential get: hits throughput: 105.792 ± 1.860 M ops/s, hits latency: 9.453 ns/op,important_hitsthroughput: 105.792 ± 1.860 M ops/slocal_storagecache interleaved get: hits throughput: 185.847 ± 4.014 M ops/s, hits latency: 5.381 ns/op,important_hitsthroughput: 185.847 ± 4.014 M ops/snum_maps: 10
local_storagecache sequential get: hits throughput: 109.867 ± 1.358 M ops/s, hits latency: 9.102 ns/op,important_hitsthroughput: 10.987 ± 0.136 M ops/slocal_storagecache interleaved get: hits throughput: 144.165 ± 1.256 M ops/s, hits latency: 6.936 ns/op,important_hitsthroughput: 51.487 ± 0.449 M ops/snum_maps: 16
local_storagecache sequential get: hits throughput: 109.258 ± 1.902 M ops/s, hits latency: 9.153 ns/op,important_hitsthroughput: 6.829 ± 0.119 M ops/slocal_storagecache interleaved get: hits throughput: 140.248 ± 1.836 M ops/s, hits latency: 7.130 ns/op,important_hitsthroughput: 44.624 ± 0.584 M ops/snum_maps: 17
local_storagecache sequential get: hits throughput: 116.397 ± 7.679 M ops/s, hits latency: 8.591 ns/op,important_hitsthroughput: 6.856 ± 0.452 M ops/slocal_storagecache interleaved get: hits throughput: 128.411 ± 4.927 M ops/s, hits latency: 7.787 ns/op,important_hitsthroughput: 39.093 ± 1.500 M ops/snum_maps: 24
local_storagecache sequential get: hits throughput: 110.890 ± 0.976 M ops/s, hits latency: 9.018 ns/op,important_hitsthroughput: 4.624 ± 0.041 M ops/slocal_storagecache interleaved get: hits throughput: 133.316 ± 1.889 M ops/s, hits latency: 7.501 ns/op,important_hitsthroughput: 37.503 ± 0.531 M ops/snum_maps: 32
local_storagecache sequential get: hits throughput: 112.900 ± 1.171 M ops/s, hits latency: 8.857 ns/op,important_hitsthroughput: 3.534 ± 0.037 M ops/slocal_storagecache interleaved get: hits throughput: 132.844 ± 1.207 M ops/s, hits latency: 7.528 ns/op,important_hitsthroughput: 37.081 ± 0.337 M ops/snum_maps: 100
local_storagecache sequential get: hits throughput: 110.025 ± 4.714 M ops/s, hits latency: 9.089 ns/op,important_hitsthroughput: 1.100 ± 0.047 M ops/slocal_storagecache interleaved get: hits throughput: 131.979 ± 5.013 M ops/s, hits latency: 7.577 ns/op,important_hitsthroughput: 34.472 ± 1.309 M ops/s
Quoted 8 lines
num_maps: 1000
local_storagecache sequential get: hits throughput: 117.850 ± 2.423 M ops/s, hits latency: 8.485 ns/op,important_hitsthroughput: 0.118 ± 0.002 M ops/slocal_storagecache interleaved get: hits throughput: 141.268 ± 9.658 M ops/s, hits latency: 7.079 ns/op,important_hitsthroughput: 35.476 ± 2.425 M ops/sAdjusting for overhead, latency numbers for "hashmap control" and "sequential get" are:
hashmap_control: ~10.4ns sequential_get_1: ~13.0ns
So what this benchmark doesn't demonstrate is why one would use local storage at all if hashmap is so fast :)
I think at least partially it's because of your choice to do fixed hashmap lookup with zero key. Think about how you'd replace local_storage with hashmap. You'd have task/socket/whatever ID as look up key. For different tasks you'd be looking up different pids. For your benchmark you have the same task all the time, but local_storage look up still does all the work to find local storage instance in a list of local storages for current task, so you don't have to use many tasks to simulate realistic lookup overhead (well, at least to some extent). But it seems not realistic for testing hashmap as an alternative to local_storage, for that I think we'd need to randomize key look up a bit. Unless I misunderstand what we are testing for hashmap use case.
But other than that LGTM.
Quoted 53 lines
sequential_get_10: ~13.8ns sequential_get_16: ~13.6ns sequential_get_17: ~21.5ns sequential_get_24: ~42.5ns sequential_get_32: ~65.9ns sequential_get_100: ~155.7ns sequential_get_1000: ~2270nsClearly demonstrating a cliff.
When running the benchmarks it may be necessary to bump 'open files' ulimit for a successful run.
[0]: https://lore.kernel.org/all/[email protected]
Signed-off-by Dave Marchevsky <[email protected]>---
Changelog:v3 -> v4:
* Remove ifs guarding increments in measure fn (Andrii)
* Refactor to use 1 bpf prog for all 3 benchmarks w/ global vars set from userspace before load to control behavior (Andrii)
* Greatly reduce variance in overhead by having benchmark bpf prog loop 10k times regardless of map count (Andrii)
* Also, movesync_fetch_and_incrout ofdo_lookupas the guaranteed secondsync_fetch_and_incrcall fornum_maps= 1 was adding overhead
* Add second patch refactoring bench.c's mean/stddev calculations in reporting helper fnsv2 -> v3:
* Accessing 1k maps inARRAY_OF_MAPSdoesn't hitMAX_USED_MAPSlimit, so just use 1 program (Alexei)v1 -> v2:
* AdoptARRAY_OF_MAPSapproach for bpf program, allowing truly configurable # of maps (Andrii)
* Add hashmap benchmark (Alexei)
* Add discussion of overheadtools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bench.c | 55 ++++ tools/testing/selftests/bpf/bench.h | 4 + .../bpf/benchs/bench_local_storage.c | 250 ++++++++++++++++++ .../bpf/benchs/run_bench_local_storage.sh | 21 ++ .../selftests/bpf/benchs/run_common.sh | 17 ++ .../selftests/bpf/progs/local_storage_bench.c | 99 +++++++ 7 files changed, 449 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/benchs/bench_local_storage.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_local_storage.sh create mode 100644 tools/testing/selftests/bpf/progs/local_storage_bench.c
[...]
tools/testing/selftests/bpf/benchs/bench_local_storage.c line 145
Quoted 10 lines
tools/testing/selftests/bpf/benchs/bench_local_storage.c new file +10
-0+ +static void hashmap_setup (void ) +{ +struct local_storage_bench *skel; + +setup_libbpf (); + + skel =local_storage_bench__open (); + ctx.skel = skel; + ctx.bpf_obj = skel->obj;
nit: ctx.skel->obj is the same as ctx.bpf_obj, so bpf_obj is probably not needed?
Quoted 7 lines
tools/testing/selftests/bpf/benchs/bench_local_storage.c new file +7
-0+ ctx.array_of_maps = skel->maps.array_of_hash_maps; + skel->rodata->use_hashmap =1 ; + skel->rodata->interleave =0 ; + +__setup (skel->progs.get_local, true); +} +
[...]
Quoted 153 lines · Andrii Nakryiko
Quoted 127 lines · Dave Marchevsky
Add a benchmarks to demonstrate the performance cliff for
local_storageget as the number oflocal_storagemaps increases beyond currentlocal_storageimplementation's cache size."sequential get" and "interleaved get" benchmarks are added, both of which do many
bpf_task_storage_getcalls on sets of tasklocal_storagemaps of various counts, while considering a single specific map to be 'important' and countingtask_storage_getsto the important map separately in addition to normal 'hits' count of all gets. Goal here is to mimic scenario where a particular program using one map - the important one - is running on a system where many otherlocal_storagemaps exist and are accessed often.While "sequential get" benchmark does
bpf_task_storage_getfor map 0, 1, ..., {9, 99, 999} in order, "interleaved" benchmark interleaves 4bpf_task_storage_getsfor the important map for every 10 map gets. This is meant to highlight performance differences when important map is accessed far more frequently than non-important maps.A "hashmap control" benchmark is also included for easy comparison of standard bpf hashmap lookup vs
local_storageget. The benchmark is identical to "sequential get", but creates and usesBPF_MAP_TYPE_HASHinstead of local storage.Addition of this benchmark is inspired by conversation with Alexei in a previous patchset's thread [0], which highlighted the need for such a benchmark to motivate and validate improvements to
local_storageimplementation. My approach in that series focused on improving performance for explicitly-marked 'important' maps and was rejected with feedback to make more generally-applicable improvements while avoiding explicitly marking maps as important. Thus the benchmark reports both general and important-map-focused metrics, so effect of future work on both is clear.Regarding the benchmark results. On a powerful system (Skylake, 20 cores, 256gb ram):
Local Storage
=============
Hashmap Control w/ 500 maps
hashmap (control) sequential get: hits throughput: 48.338 ± 2.366 M ops/s, hits latency: 20.688 ns/op,important_hitsthroughput: 0.097 ± 0.005 M ops/snum_maps: 1
local_storagecache sequential get: hits throughput: 44.503 ± 1.080 M ops/s, hits latency: 22.470 ns/op,important_hitsthroughput: 44.503 ± 1.080 M ops/slocal_storagecache interleaved get: hits throughput: 54.963 ± 0.586 M ops/s, hits latency: 18.194 ns/op,important_hitsthroughput: 54.963 ± 0.586 M ops/snum_maps: 10
local_storagecache sequential get: hits throughput: 43.743 ± 0.418 M ops/s, hits latency: 22.861 ns/op,important_hitsthroughput: 4.374 ± 0.042 M ops/slocal_storagecache interleaved get: hits throughput: 50.073 ± 0.609 M ops/s, hits latency: 19.971 ns/op,important_hitsthroughput: 17.883 ± 0.217 M ops/snum_maps: 16
local_storagecache sequential get: hits throughput: 43.962 ± 0.525 M ops/s, hits latency: 22.747 ns/op,important_hitsthroughput: 2.748 ± 0.033 M ops/slocal_storagecache interleaved get: hits throughput: 48.166 ± 0.825 M ops/s, hits latency: 20.761 ns/op,important_hitsthroughput: 15.326 ± 0.263 M ops/snum_maps: 17
local_storagecache sequential get: hits throughput: 33.207 ± 0.461 M ops/s, hits latency: 30.114 ns/op,important_hitsthroughput: 1.956 ± 0.027 M ops/slocal_storagecache interleaved get: hits throughput: 43.540 ± 0.265 M ops/s, hits latency: 22.968 ns/op,important_hitsthroughput: 13.255 ± 0.081 M ops/snum_maps: 24
local_storagecache sequential get: hits throughput: 19.402 ± 0.348 M ops/s, hits latency: 51.542 ns/op,important_hitsthroughput: 0.809 ± 0.015 M ops/slocal_storagecache interleaved get: hits throughput: 22.981 ± 0.487 M ops/s, hits latency: 43.514 ns/op,important_hitsthroughput: 6.465 ± 0.137 M ops/snum_maps: 32
local_storagecache sequential get: hits throughput: 13.378 ± 0.220 M ops/s, hits latency: 74.748 ns/op,important_hitsthroughput: 0.419 ± 0.007 M ops/slocal_storagecache interleaved get: hits throughput: 16.894 ± 0.172 M ops/s, hits latency: 59.193 ns/op,important_hitsthroughput: 4.716 ± 0.048 M ops/snum_maps: 100
local_storagecache sequential get: hits throughput: 6.070 ± 0.140 M ops/s, hits latency: 164.745 ns/op,important_hitsthroughput: 0.061 ± 0.001 M ops/slocal_storagecache interleaved get: hits throughput: 7.323 ± 0.149 M ops/s, hits latency: 136.554 ns/op,important_hitsthroughput: 1.913 ± 0.039 M ops/snum_maps: 1000
local_storagecache sequential get: hits throughput: 0.438 ± 0.012 M ops/s, hits latency: 2281.369 ns/op,important_hitsthroughput: 0.000 ± 0.000 M ops/slocal_storagecache interleaved get: hits throughput: 0.522 ± 0.010 M ops/s, hits latency: 1913.937 ns/op,important_hitsthroughput: 0.131 ± 0.003 M ops/sLooking at the "sequential get" results, it's clear that as the number of task
local_storagemaps grows beyond the current cache size (16), there's a significant reduction in hits throughput. Note that currentlocal_storageimplementation assigns acache_idxto maps as they are created. Since "sequential get" is creating maps 0..n in order and then doingbpf_task_storage_getcalls in the same order, the benchmark is effectively ensuring that a map will not be in cache when the program tries to access it.For "interleaved get" results, important-map hits throughput is greatly increased as the important map is more likely to be in cache by virtue of being accessed far more frequently. Throughput still reduces as # maps increases, though.
To get a sense of the overhead of the benchmark program, I commented out bpf_task_storage_get/bpf_map_lookup_elem in
local_storage_bench.cand ran the benchmark on the same host as the 'real' run. Results:Local Storage
=============
Hashmap Control w/ 500 maps
hashmap (control) sequential get: hits throughput: 96.965 ± 1.346 M ops/s, hits latency: 10.313 ns/op,important_hitsthroughput: 0.194 ± 0.003 M ops/snum_maps: 1
local_storagecache sequential get: hits throughput: 105.792 ± 1.860 M ops/s, hits latency: 9.453 ns/op,important_hitsthroughput: 105.792 ± 1.860 M ops/slocal_storagecache interleaved get: hits throughput: 185.847 ± 4.014 M ops/s, hits latency: 5.381 ns/op,important_hitsthroughput: 185.847 ± 4.014 M ops/snum_maps: 10
local_storagecache sequential get: hits throughput: 109.867 ± 1.358 M ops/s, hits latency: 9.102 ns/op,important_hitsthroughput: 10.987 ± 0.136 M ops/slocal_storagecache interleaved get: hits throughput: 144.165 ± 1.256 M ops/s, hits latency: 6.936 ns/op,important_hitsthroughput: 51.487 ± 0.449 M ops/snum_maps: 16
local_storagecache sequential get: hits throughput: 109.258 ± 1.902 M ops/s, hits latency: 9.153 ns/op,important_hitsthroughput: 6.829 ± 0.119 M ops/slocal_storagecache interleaved get: hits throughput: 140.248 ± 1.836 M ops/s, hits latency: 7.130 ns/op,important_hitsthroughput: 44.624 ± 0.584 M ops/snum_maps: 17
local_storagecache sequential get: hits throughput: 116.397 ± 7.679 M ops/s, hits latency: 8.591 ns/op,important_hitsthroughput: 6.856 ± 0.452 M ops/slocal_storagecache interleaved get: hits throughput: 128.411 ± 4.927 M ops/s, hits latency: 7.787 ns/op,important_hitsthroughput: 39.093 ± 1.500 M ops/snum_maps: 24
local_storagecache sequential get: hits throughput: 110.890 ± 0.976 M ops/s, hits latency: 9.018 ns/op,important_hitsthroughput: 4.624 ± 0.041 M ops/slocal_storagecache interleaved get: hits throughput: 133.316 ± 1.889 M ops/s, hits latency: 7.501 ns/op,important_hitsthroughput: 37.503 ± 0.531 M ops/snum_maps: 32
local_storagecache sequential get: hits throughput: 112.900 ± 1.171 M ops/s, hits latency: 8.857 ns/op,important_hitsthroughput: 3.534 ± 0.037 M ops/slocal_storagecache interleaved get: hits throughput: 132.844 ± 1.207 M ops/s, hits latency: 7.528 ns/op,important_hitsthroughput: 37.081 ± 0.337 M ops/snum_maps: 100
local_storagecache sequential get: hits throughput: 110.025 ± 4.714 M ops/s, hits latency: 9.089 ns/op,important_hitsthroughput: 1.100 ± 0.047 M ops/slocal_storagecache interleaved get: hits throughput: 131.979 ± 5.013 M ops/s, hits latency: 7.577 ns/op,important_hitsthroughput: 34.472 ± 1.309 M ops/sQuoted 8 lines
num_maps: 1000
local_storagecache sequential get: hits throughput: 117.850 ± 2.423 M ops/s, hits latency: 8.485 ns/op,important_hitsthroughput: 0.118 ± 0.002 M ops/slocal_storagecache interleaved get: hits throughput: 141.268 ± 9.658 M ops/s, hits latency: 7.079 ns/op,important_hitsthroughput: 35.476 ± 2.425 M ops/sAdjusting for overhead, latency numbers for "hashmap control" and "sequential get" are:
hashmap_control: ~10.4ns sequential_get_1: ~13.0nsSo what this benchmark doesn't demonstrate is why one would use local storage at all if hashmap is so fast :)
I think at least partially it's because of your choice to do fixed hashmap lookup with zero key. Think about how you'd replace
local_storagewith hashmap. You'd have task/socket/whatever ID as look up key. For different tasks you'd be looking up different pids. For your benchmark you have the same task all the time, butlocal_storagelook up still does all the work to find local storage instance in a list of local storages for current task, so you don't have to use many tasks to simulate realistic lookup overhead (well, at least to some extent). But it seems not realistic for testing hashmap as an alternative tolocal_storage, for that I think we'd need to randomize key look up a bit. Unless I misunderstand what we are testing for hashmap use case.
Quoted 1 line
But other than that LGTM.
This makes more sense than what I'm doing now, and I think better fulfills Alexei's request from v1 of this series:
Also could you add a hash map with key=tid.
It would be interesting to compare local storage with hash map.
iirc when local storage was introduced it was 2.5 times faster than large hashmap. Since then both have changed.
I tried changing the "hashmap control" benchmark to just have 1 inner hashmap, with max_entries 4,194,304 (PID_MAX_LIMIT on my system). use_hashmap branch of bpf prog does bpf_get_prandom_u32 to select key. Results are more promising:
Overhead: ~19.5ns (prandom call + additional modulo)
4,194,304 possible keys: ~150.5ns (~131ns w/o overhead)
100,000 possible keys: ~55ns (~35.5ns w/o overhead)
1,000 possible keys: ~31ns (~11.5ns w/o overhead)
10 possible keys: ~28ns (~8.5ns w/o overhead)So big hashmap with only a few keys used is faster than local_storage but far slower when many keys used. Somewhere between "100k keys" and "1k keys" is probably what performance would look like if a program which needed an entry per running task used hashmap instead of local_storage. Close to Alexei's memory of 2.5x difference.
Will send in v5.
Quoted 57 lines
Quoted 54 lines
sequential_get_10: ~13.8ns sequential_get_16: ~13.6ns sequential_get_17: ~21.5ns sequential_get_24: ~42.5ns sequential_get_32: ~65.9ns sequential_get_100: ~155.7ns sequential_get_1000: ~2270nsClearly demonstrating a cliff.
When running the benchmarks it may be necessary to bump 'open files' ulimit for a successful run.
[0]: https://lore.kernel.org/all/[email protected]
Signed-off-by Dave Marchevsky <[email protected]>---
Changelog:v3 -> v4:
* Remove ifs guarding increments in measure fn (Andrii)
* Refactor to use 1 bpf prog for all 3 benchmarks w/ global vars set from userspace before load to control behavior (Andrii)
* Greatly reduce variance in overhead by having benchmark bpf prog loop 10k times regardless of map count (Andrii)
* Also, movesync_fetch_and_incrout ofdo_lookupas the guaranteed secondsync_fetch_and_incrcall fornum_maps= 1 was adding overhead
* Add second patch refactoring bench.c's mean/stddev calculations in reporting helper fnsv2 -> v3:
* Accessing 1k maps inARRAY_OF_MAPSdoesn't hitMAX_USED_MAPSlimit, so just use 1 program (Alexei)v1 -> v2:
* AdoptARRAY_OF_MAPSapproach for bpf program, allowing truly configurable # of maps (Andrii)
* Add hashmap benchmark (Alexei)
* Add discussion of overheadtools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bench.c | 55 ++++ tools/testing/selftests/bpf/bench.h | 4 + .../bpf/benchs/bench_local_storage.c | 250 ++++++++++++++++++ .../bpf/benchs/run_bench_local_storage.sh | 21 ++ .../selftests/bpf/benchs/run_common.sh | 17 ++ .../selftests/bpf/progs/local_storage_bench.c | 99 +++++++ 7 files changed, 449 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/benchs/bench_local_storage.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_local_storage.sh create mode 100644 tools/testing/selftests/bpf/progs/local_storage_bench.c[...]
Quoted 13 lines
tools/testing/selftests/bpf/benchs/bench_local_storage.c line 145
Quoted 10 lines
tools/testing/selftests/bpf/benchs/bench_local_storage.c new file +10
-0+ +static void hashmap_setup (void ) +{ +struct local_storage_bench *skel; + +setup_libbpf (); + + skel =local_storage_bench__open (); + ctx.skel = skel; + ctx.bpf_obj = skel->obj;nit:
ctx.skel->objis the same as ctx.bpf_obj, sobpf_objis probably not needed?
Will get rid of it in v5.
Quoted 11 lines
Quoted 9 lines
tools/testing/selftests/bpf/benchs/bench_local_storage.c new file +7
-0+ ctx.array_of_maps = skel->maps.array_of_hash_maps; + skel->rodata->use_hashmap =1 ; + skel->rodata->interleave =0 ; + +__setup (skel->progs.get_local, true); +} +[...]