tools/testing/selftests/bpf/benchs/bench_bpf_timing.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/benchs/bench_bpf_timing.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/benchs/bench_bpf_timing.c
Extension
.c
Size
6509 bytes
Lines
299
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct timing_stats {
	double min, max;
	double median, p99;
	double mean, stddev;
	int count;
};

static int cmp_double(const void *a, const void *b)
{
	double da = *(const double *)a;
	double db = *(const double *)b;

	if (da < db)
		return -1;
	if (da > db)
		return 1;
	return 0;
}

static double percentile(const double *sorted, int n, double pct)
{
	int idx = (int)(n * pct / 100.0);

	if (idx >= n)
		idx = n - 1;
	return sorted[idx];
}

static int collect_samples(struct bpf_bench_timing *t,
			   double *out, int max_out)
{
	unsigned int nr_cpus = bpf_num_possible_cpus();
	__u32 timed_iters = t->batch_iters;
	int total = 0;

	if (nr_cpus > BENCH_NR_CPUS)
		nr_cpus = BENCH_NR_CPUS;

	for (unsigned int cpu = 0; cpu < nr_cpus; cpu++) {
		__u32 count = t->idx[cpu];

		if (count > BENCH_NR_SAMPLES)
			count = BENCH_NR_SAMPLES;

		for (__u32 i = 0; i < count && total < max_out; i++) {
			__u64 sample = t->samples[cpu][i];

			if (sample == 0)
				continue;
			out[total++] = (double)sample / timed_iters;
		}
	}

	qsort(out, total, sizeof(double), cmp_double);
	return total;
}

static int filter_outliers_iqr(double *sorted, int n)
{
	double q1, q3, iqr, lo, hi;
	int start = 0, end = n;

	if (n < 8)
		return n;

	q1 = sorted[n / 4];
	q3 = sorted[3 * n / 4];
	iqr = q3 - q1;
	lo = q1 - 1.5 * iqr;
	hi = q3 + 1.5 * iqr;

	while (start < end && sorted[start] < lo)
		start++;
	while (end > start && sorted[end - 1] > hi)
		end--;

	if (start > 0)
		memmove(sorted, sorted + start, (end - start) * sizeof(double));

	return end - start;
}

static void compute_stats(const double *sorted, int n,
			  struct timing_stats *s)
{
	double sum = 0, var_sum = 0;

	memset(s, 0, sizeof(*s));
	s->count = n;

Annotation

Implementation Notes