spinlock_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /* Test of gpr spin-lock support. */
  19. #include "src/core/lib/gpr/spinlock.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <grpc/support/alloc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpc/support/sync.h>
  25. #include <grpc/support/time.h>
  26. #include "src/core/lib/gprpp/thd.h"
  27. #include "test/core/util/test_config.h"
  28. /* ------------------------------------------------- */
  29. /* Tests for gpr_spinlock. */
  30. struct test {
  31. int thread_count; /* number of threads */
  32. grpc_core::Thread* threads;
  33. int64_t iterations; /* number of iterations per thread */
  34. int64_t counter;
  35. int incr_step; /* how much to increment/decrement refcount each time */
  36. gpr_spinlock mu; /* protects iterations, counter */
  37. };
  38. /* Return pointer to a new struct test. */
  39. static struct test* test_new(int threads, int64_t iterations, int incr_step) {
  40. struct test* m = static_cast<struct test*>(gpr_malloc(sizeof(*m)));
  41. m->thread_count = threads;
  42. m->threads = static_cast<grpc_core::Thread*>(
  43. gpr_malloc(sizeof(*m->threads) * static_cast<size_t>(threads)));
  44. m->iterations = iterations;
  45. m->counter = 0;
  46. m->thread_count = 0;
  47. m->incr_step = incr_step;
  48. m->mu = GPR_SPINLOCK_INITIALIZER;
  49. return m;
  50. }
  51. /* Return pointer to a new struct test. */
  52. static void test_destroy(struct test* m) {
  53. gpr_free(m->threads);
  54. gpr_free(m);
  55. }
  56. /* Create m->threads threads, each running (*body)(m) */
  57. static void test_create_threads(struct test* m, void (*body)(void* arg)) {
  58. int i;
  59. for (i = 0; i != m->thread_count; i++) {
  60. m->threads[i] = grpc_core::Thread("grpc_create_threads", body, m);
  61. m->threads[i].Start();
  62. }
  63. }
  64. /* Wait until all threads report done. */
  65. static void test_wait(struct test* m) {
  66. int i;
  67. for (i = 0; i != m->thread_count; i++) {
  68. m->threads[i].Join();
  69. }
  70. }
  71. /* Test several threads running (*body)(struct test *m) for increasing settings
  72. of m->iterations, until about timeout_s to 2*timeout_s seconds have elapsed.
  73. If extra!=NULL, run (*extra)(m) in an additional thread.
  74. incr_step controls by how much m->refcount should be incremented/decremented
  75. (if at all) each time in the tests.
  76. */
  77. static void test(const char* name, void (*body)(void* m), int timeout_s,
  78. int incr_step) {
  79. int64_t iterations = 1024;
  80. struct test* m;
  81. gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME);
  82. gpr_timespec time_taken;
  83. gpr_timespec deadline = gpr_time_add(
  84. start, gpr_time_from_micros(static_cast<int64_t>(timeout_s) * 1000000,
  85. GPR_TIMESPAN));
  86. fprintf(stderr, "%s:", name);
  87. fflush(stderr);
  88. while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0) {
  89. if (iterations < INT64_MAX / 2) iterations <<= 1;
  90. fprintf(stderr, " %ld", static_cast<long>(iterations));
  91. fflush(stderr);
  92. m = test_new(10, iterations, incr_step);
  93. test_create_threads(m, body);
  94. test_wait(m);
  95. if (m->counter != m->thread_count * m->iterations * m->incr_step) {
  96. fprintf(stderr, "counter %ld threads %d iterations %ld\n",
  97. static_cast<long>(m->counter), m->thread_count,
  98. static_cast<long>(m->iterations));
  99. fflush(stderr);
  100. GPR_ASSERT(0);
  101. }
  102. test_destroy(m);
  103. }
  104. time_taken = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start);
  105. fprintf(stderr, " done %lld.%09d s\n",
  106. static_cast<long long>(time_taken.tv_sec),
  107. static_cast<int>(time_taken.tv_nsec));
  108. fflush(stderr);
  109. }
  110. /* Increment m->counter on each iteration; then mark thread as done. */
  111. static void inc(void* v /*=m*/) {
  112. struct test* m = static_cast<struct test*>(v);
  113. int64_t i;
  114. for (i = 0; i != m->iterations; i++) {
  115. gpr_spinlock_lock(&m->mu);
  116. m->counter++;
  117. gpr_spinlock_unlock(&m->mu);
  118. }
  119. }
  120. /* Increment m->counter under lock acquired with trylock, m->iterations times;
  121. then mark thread as done. */
  122. static void inctry(void* v /*=m*/) {
  123. struct test* m = static_cast<struct test*>(v);
  124. int64_t i;
  125. for (i = 0; i != m->iterations;) {
  126. if (gpr_spinlock_trylock(&m->mu)) {
  127. m->counter++;
  128. gpr_spinlock_unlock(&m->mu);
  129. i++;
  130. }
  131. }
  132. }
  133. /* ------------------------------------------------- */
  134. int main(int argc, char* argv[]) {
  135. grpc::testing::TestEnvironment env(argc, argv);
  136. test("spinlock", &inc, 1, 1);
  137. test("spinlock try", &inctry, 1, 1);
  138. return 0;
  139. }