0001
0002
0003 #include "bench.h"
0004
0005
0006
0007 static struct count_global_ctx {
0008 struct counter hits;
0009 } count_global_ctx;
0010
0011 static void *count_global_producer(void *input)
0012 {
0013 struct count_global_ctx *ctx = &count_global_ctx;
0014
0015 while (true) {
0016 atomic_inc(&ctx->hits.value);
0017 }
0018 return NULL;
0019 }
0020
0021 static void *count_global_consumer(void *input)
0022 {
0023 return NULL;
0024 }
0025
0026 static void count_global_measure(struct bench_res *res)
0027 {
0028 struct count_global_ctx *ctx = &count_global_ctx;
0029
0030 res->hits = atomic_swap(&ctx->hits.value, 0);
0031 }
0032
0033
0034
0035 static struct count_local_ctx {
0036 struct counter *hits;
0037 } count_local_ctx;
0038
0039 static void count_local_setup(void)
0040 {
0041 struct count_local_ctx *ctx = &count_local_ctx;
0042
0043 ctx->hits = calloc(env.consumer_cnt, sizeof(*ctx->hits));
0044 if (!ctx->hits)
0045 exit(1);
0046 }
0047
0048 static void *count_local_producer(void *input)
0049 {
0050 struct count_local_ctx *ctx = &count_local_ctx;
0051 int idx = (long)input;
0052
0053 while (true) {
0054 atomic_inc(&ctx->hits[idx].value);
0055 }
0056 return NULL;
0057 }
0058
0059 static void *count_local_consumer(void *input)
0060 {
0061 return NULL;
0062 }
0063
0064 static void count_local_measure(struct bench_res *res)
0065 {
0066 struct count_local_ctx *ctx = &count_local_ctx;
0067 int i;
0068
0069 for (i = 0; i < env.producer_cnt; i++) {
0070 res->hits += atomic_swap(&ctx->hits[i].value, 0);
0071 }
0072 }
0073
0074 const struct bench bench_count_global = {
0075 .name = "count-global",
0076 .producer_thread = count_global_producer,
0077 .consumer_thread = count_global_consumer,
0078 .measure = count_global_measure,
0079 .report_progress = hits_drops_report_progress,
0080 .report_final = hits_drops_report_final,
0081 };
0082
0083 const struct bench bench_count_local = {
0084 .name = "count-local",
0085 .setup = count_local_setup,
0086 .producer_thread = count_local_producer,
0087 .consumer_thread = count_local_consumer,
0088 .measure = count_local_measure,
0089 .report_progress = hits_drops_report_progress,
0090 .report_final = hits_drops_report_final,
0091 };