Path: blob/master/tools/testing/selftests/bpf/benchs/bench_count.c
29270 views
// SPDX-License-Identifier: GPL-2.01/* Copyright (c) 2020 Facebook */2#include "bench.h"34/* COUNT-GLOBAL benchmark */56static struct count_global_ctx {7struct counter hits;8} count_global_ctx;910static void *count_global_producer(void *input)11{12struct count_global_ctx *ctx = &count_global_ctx;1314while (true) {15atomic_inc(&ctx->hits.value);16}17return NULL;18}1920static void count_global_measure(struct bench_res *res)21{22struct count_global_ctx *ctx = &count_global_ctx;2324res->hits = atomic_swap(&ctx->hits.value, 0);25}2627/* COUNT-local benchmark */2829static struct count_local_ctx {30struct counter *hits;31} count_local_ctx;3233static void count_local_setup(void)34{35struct count_local_ctx *ctx = &count_local_ctx;3637ctx->hits = calloc(env.producer_cnt, sizeof(*ctx->hits));38if (!ctx->hits)39exit(1);40}4142static void *count_local_producer(void *input)43{44struct count_local_ctx *ctx = &count_local_ctx;45int idx = (long)input;4647while (true) {48atomic_inc(&ctx->hits[idx].value);49}50return NULL;51}5253static void count_local_measure(struct bench_res *res)54{55struct count_local_ctx *ctx = &count_local_ctx;56int i;5758for (i = 0; i < env.producer_cnt; i++) {59res->hits += atomic_swap(&ctx->hits[i].value, 0);60}61}6263const struct bench bench_count_global = {64.name = "count-global",65.producer_thread = count_global_producer,66.measure = count_global_measure,67.report_progress = hits_drops_report_progress,68.report_final = hits_drops_report_final,69};7071const struct bench bench_count_local = {72.name = "count-local",73.setup = count_local_setup,74.producer_thread = count_local_producer,75.measure = count_local_measure,76.report_progress = hits_drops_report_progress,77.report_final = hits_drops_report_final,78};798081