bench_count.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2020 Facebook */
  3. #include "bench.h"
  4. /* COUNT-GLOBAL benchmark */
  5. static struct count_global_ctx {
  6. struct counter hits;
  7. } count_global_ctx;
  8. static void *count_global_producer(void *input)
  9. {
  10. struct count_global_ctx *ctx = &count_global_ctx;
  11. while (true) {
  12. atomic_inc(&ctx->hits.value);
  13. }
  14. return NULL;
  15. }
  16. static void count_global_measure(struct bench_res *res)
  17. {
  18. struct count_global_ctx *ctx = &count_global_ctx;
  19. res->hits = atomic_swap(&ctx->hits.value, 0);
  20. }
  21. /* COUNT-local benchmark */
  22. static struct count_local_ctx {
  23. struct counter *hits;
  24. } count_local_ctx;
  25. static void count_local_setup(void)
  26. {
  27. struct count_local_ctx *ctx = &count_local_ctx;
  28. ctx->hits = calloc(env.producer_cnt, sizeof(*ctx->hits));
  29. if (!ctx->hits)
  30. exit(1);
  31. }
  32. static void *count_local_producer(void *input)
  33. {
  34. struct count_local_ctx *ctx = &count_local_ctx;
  35. int idx = (long)input;
  36. while (true) {
  37. atomic_inc(&ctx->hits[idx].value);
  38. }
  39. return NULL;
  40. }
  41. static void count_local_measure(struct bench_res *res)
  42. {
  43. struct count_local_ctx *ctx = &count_local_ctx;
  44. int i;
  45. for (i = 0; i < env.producer_cnt; i++) {
  46. res->hits += atomic_swap(&ctx->hits[i].value, 0);
  47. }
  48. }
  49. const struct bench bench_count_global = {
  50. .name = "count-global",
  51. .producer_thread = count_global_producer,
  52. .measure = count_global_measure,
  53. .report_progress = hits_drops_report_progress,
  54. .report_final = hits_drops_report_final,
  55. };
  56. const struct bench bench_count_local = {
  57. .name = "count-local",
  58. .setup = count_local_setup,
  59. .producer_thread = count_local_producer,
  60. .measure = count_local_measure,
  61. .report_progress = hits_drops_report_progress,
  62. .report_final = hits_drops_report_final,
  63. };