18e7c2a02SAndrii Nakryiko // SPDX-License-Identifier: GPL-2.0
28e7c2a02SAndrii Nakryiko /* Copyright (c) 2020 Facebook */
38e7c2a02SAndrii Nakryiko #include "bench.h"
48e7c2a02SAndrii Nakryiko
58e7c2a02SAndrii Nakryiko /* COUNT-GLOBAL benchmark */
68e7c2a02SAndrii Nakryiko
78e7c2a02SAndrii Nakryiko static struct count_global_ctx {
88e7c2a02SAndrii Nakryiko struct counter hits;
98e7c2a02SAndrii Nakryiko } count_global_ctx;
108e7c2a02SAndrii Nakryiko
count_global_producer(void * input)118e7c2a02SAndrii Nakryiko static void *count_global_producer(void *input)
128e7c2a02SAndrii Nakryiko {
138e7c2a02SAndrii Nakryiko struct count_global_ctx *ctx = &count_global_ctx;
148e7c2a02SAndrii Nakryiko
158e7c2a02SAndrii Nakryiko while (true) {
168e7c2a02SAndrii Nakryiko atomic_inc(&ctx->hits.value);
178e7c2a02SAndrii Nakryiko }
188e7c2a02SAndrii Nakryiko return NULL;
198e7c2a02SAndrii Nakryiko }
208e7c2a02SAndrii Nakryiko
count_global_measure(struct bench_res * res)218e7c2a02SAndrii Nakryiko static void count_global_measure(struct bench_res *res)
228e7c2a02SAndrii Nakryiko {
238e7c2a02SAndrii Nakryiko struct count_global_ctx *ctx = &count_global_ctx;
248e7c2a02SAndrii Nakryiko
258e7c2a02SAndrii Nakryiko res->hits = atomic_swap(&ctx->hits.value, 0);
268e7c2a02SAndrii Nakryiko }
278e7c2a02SAndrii Nakryiko
288e7c2a02SAndrii Nakryiko /* COUNT-local benchmark */
298e7c2a02SAndrii Nakryiko
308e7c2a02SAndrii Nakryiko static struct count_local_ctx {
318e7c2a02SAndrii Nakryiko struct counter *hits;
328e7c2a02SAndrii Nakryiko } count_local_ctx;
338e7c2a02SAndrii Nakryiko
count_local_setup(void)349a93bf3fSHou Tao static void count_local_setup(void)
358e7c2a02SAndrii Nakryiko {
368e7c2a02SAndrii Nakryiko struct count_local_ctx *ctx = &count_local_ctx;
378e7c2a02SAndrii Nakryiko
38*8ad663d3SHou Tao ctx->hits = calloc(env.producer_cnt, sizeof(*ctx->hits));
398e7c2a02SAndrii Nakryiko if (!ctx->hits)
408e7c2a02SAndrii Nakryiko exit(1);
418e7c2a02SAndrii Nakryiko }
428e7c2a02SAndrii Nakryiko
count_local_producer(void * input)438e7c2a02SAndrii Nakryiko static void *count_local_producer(void *input)
448e7c2a02SAndrii Nakryiko {
458e7c2a02SAndrii Nakryiko struct count_local_ctx *ctx = &count_local_ctx;
468e7c2a02SAndrii Nakryiko int idx = (long)input;
478e7c2a02SAndrii Nakryiko
488e7c2a02SAndrii Nakryiko while (true) {
498e7c2a02SAndrii Nakryiko atomic_inc(&ctx->hits[idx].value);
508e7c2a02SAndrii Nakryiko }
518e7c2a02SAndrii Nakryiko return NULL;
528e7c2a02SAndrii Nakryiko }
538e7c2a02SAndrii Nakryiko
count_local_measure(struct bench_res * res)548e7c2a02SAndrii Nakryiko static void count_local_measure(struct bench_res *res)
558e7c2a02SAndrii Nakryiko {
568e7c2a02SAndrii Nakryiko struct count_local_ctx *ctx = &count_local_ctx;
578e7c2a02SAndrii Nakryiko int i;
588e7c2a02SAndrii Nakryiko
598e7c2a02SAndrii Nakryiko for (i = 0; i < env.producer_cnt; i++) {
608e7c2a02SAndrii Nakryiko res->hits += atomic_swap(&ctx->hits[i].value, 0);
618e7c2a02SAndrii Nakryiko }
628e7c2a02SAndrii Nakryiko }
638e7c2a02SAndrii Nakryiko
648e7c2a02SAndrii Nakryiko const struct bench bench_count_global = {
658e7c2a02SAndrii Nakryiko .name = "count-global",
668e7c2a02SAndrii Nakryiko .producer_thread = count_global_producer,
678e7c2a02SAndrii Nakryiko .measure = count_global_measure,
688e7c2a02SAndrii Nakryiko .report_progress = hits_drops_report_progress,
698e7c2a02SAndrii Nakryiko .report_final = hits_drops_report_final,
708e7c2a02SAndrii Nakryiko };
718e7c2a02SAndrii Nakryiko
728e7c2a02SAndrii Nakryiko const struct bench bench_count_local = {
738e7c2a02SAndrii Nakryiko .name = "count-local",
748e7c2a02SAndrii Nakryiko .setup = count_local_setup,
758e7c2a02SAndrii Nakryiko .producer_thread = count_local_producer,
768e7c2a02SAndrii Nakryiko .measure = count_local_measure,
778e7c2a02SAndrii Nakryiko .report_progress = hits_drops_report_progress,
788e7c2a02SAndrii Nakryiko .report_final = hits_drops_report_final,
798e7c2a02SAndrii Nakryiko };
80