WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / benchs / bench_count.c
blobbefba7a8264362fefa25b6479335270efe53c415
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 #include "bench.h"
5 /* COUNT-GLOBAL benchmark */
7 static struct count_global_ctx {
8 struct counter hits;
9 } count_global_ctx;
11 static void *count_global_producer(void *input)
13 struct count_global_ctx *ctx = &count_global_ctx;
15 while (true) {
16 atomic_inc(&ctx->hits.value);
18 return NULL;
21 static void *count_global_consumer(void *input)
23 return NULL;
26 static void count_global_measure(struct bench_res *res)
28 struct count_global_ctx *ctx = &count_global_ctx;
30 res->hits = atomic_swap(&ctx->hits.value, 0);
33 /* COUNT-local benchmark */
35 static struct count_local_ctx {
36 struct counter *hits;
37 } count_local_ctx;
39 static void count_local_setup()
41 struct count_local_ctx *ctx = &count_local_ctx;
43 ctx->hits = calloc(env.consumer_cnt, sizeof(*ctx->hits));
44 if (!ctx->hits)
45 exit(1);
48 static void *count_local_producer(void *input)
50 struct count_local_ctx *ctx = &count_local_ctx;
51 int idx = (long)input;
53 while (true) {
54 atomic_inc(&ctx->hits[idx].value);
56 return NULL;
59 static void *count_local_consumer(void *input)
61 return NULL;
64 static void count_local_measure(struct bench_res *res)
66 struct count_local_ctx *ctx = &count_local_ctx;
67 int i;
69 for (i = 0; i < env.producer_cnt; i++) {
70 res->hits += atomic_swap(&ctx->hits[i].value, 0);
74 const struct bench bench_count_global = {
75 .name = "count-global",
76 .producer_thread = count_global_producer,
77 .consumer_thread = count_global_consumer,
78 .measure = count_global_measure,
79 .report_progress = hits_drops_report_progress,
80 .report_final = hits_drops_report_final,
83 const struct bench bench_count_local = {
84 .name = "count-local",
85 .setup = count_local_setup,
86 .producer_thread = count_local_producer,
87 .consumer_thread = count_local_consumer,
88 .measure = count_local_measure,
89 .report_progress = hits_drops_report_progress,
90 .report_final = hits_drops_report_final,