1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2020 Facebook */
3#include "bench.h"
4
5/* COUNT-GLOBAL benchmark */
6
7static struct count_global_ctx {
8	struct counter hits;
9} count_global_ctx;
10
11static void *count_global_producer(void *input)
12{
13	struct count_global_ctx *ctx = &count_global_ctx;
14
15	while (true) {
16		atomic_inc(&ctx->hits.value);
17	}
18	return NULL;
19}
20
21static void *count_global_consumer(void *input)
22{
23	return NULL;
24}
25
26static void count_global_measure(struct bench_res *res)
27{
28	struct count_global_ctx *ctx = &count_global_ctx;
29
30	res->hits = atomic_swap(&ctx->hits.value, 0);
31}
32
33/* COUNT-local benchmark */
34
35static struct count_local_ctx {
36	struct counter *hits;
37} count_local_ctx;
38
39static void count_local_setup()
40{
41	struct count_local_ctx *ctx = &count_local_ctx;
42
43	ctx->hits = calloc(env.consumer_cnt, sizeof(*ctx->hits));
44	if (!ctx->hits)
45		exit(1);
46}
47
48static void *count_local_producer(void *input)
49{
50	struct count_local_ctx *ctx = &count_local_ctx;
51	int idx = (long)input;
52
53	while (true) {
54		atomic_inc(&ctx->hits[idx].value);
55	}
56	return NULL;
57}
58
59static void *count_local_consumer(void *input)
60{
61	return NULL;
62}
63
64static void count_local_measure(struct bench_res *res)
65{
66	struct count_local_ctx *ctx = &count_local_ctx;
67	int i;
68
69	for (i = 0; i < env.producer_cnt; i++) {
70		res->hits += atomic_swap(&ctx->hits[i].value, 0);
71	}
72}
73
74const 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,
81};
82
83const 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,
91};
92