xref: /aosp_15_r20/external/grpc-grpc/test/core/gpr/spinlock_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 // Test of gpr spin-lock support.
20 
21 #include "src/core/lib/gpr/spinlock.h"
22 
23 #include <stdint.h>
24 #include <stdio.h>
25 
26 #include <memory>
27 
28 #include "gtest/gtest.h"
29 
30 #include <grpc/support/alloc.h>
31 #include <grpc/support/time.h>
32 
33 #include "src/core/lib/gprpp/thd.h"
34 #include "test/core/util/test_config.h"
35 
36 // -------------------------------------------------
37 // Tests for gpr_spinlock.
38 struct test {
39   int thread_count;  // number of threads
40   grpc_core::Thread* threads;
41 
42   int64_t iterations;  // number of iterations per thread
43   int64_t counter;
44   int incr_step;  // how much to increment/decrement refcount each time
45 
46   gpr_spinlock mu;  // protects iterations, counter
47 };
48 
49 // Return pointer to a new struct test.
test_new(int threads,int64_t iterations,int incr_step)50 static struct test* test_new(int threads, int64_t iterations, int incr_step) {
51   struct test* m = static_cast<struct test*>(gpr_malloc(sizeof(*m)));
52   m->thread_count = threads;
53   m->threads = static_cast<grpc_core::Thread*>(
54       gpr_malloc(sizeof(*m->threads) * static_cast<size_t>(threads)));
55   m->iterations = iterations;
56   m->counter = 0;
57   m->thread_count = 0;
58   m->incr_step = incr_step;
59   m->mu = GPR_SPINLOCK_INITIALIZER;
60   return m;
61 }
62 
63 // Return pointer to a new struct test.
test_destroy(struct test * m)64 static void test_destroy(struct test* m) {
65   gpr_free(m->threads);
66   gpr_free(m);
67 }
68 
69 // Create m->threads threads, each running (*body)(m)
test_create_threads(struct test * m,void (* body)(void * arg))70 static void test_create_threads(struct test* m, void (*body)(void* arg)) {
71   int i;
72   for (i = 0; i != m->thread_count; i++) {
73     m->threads[i] = grpc_core::Thread("grpc_create_threads", body, m);
74     m->threads[i].Start();
75   }
76 }
77 
78 // Wait until all threads report done.
test_wait(struct test * m)79 static void test_wait(struct test* m) {
80   int i;
81   for (i = 0; i != m->thread_count; i++) {
82     m->threads[i].Join();
83   }
84 }
85 
86 // Test several threads running (*body)(struct test *m) for increasing settings
87 // of m->iterations, until about timeout_s to 2*timeout_s seconds have elapsed.
88 // If extra!=NULL, run (*extra)(m) in an additional thread.
89 // incr_step controls by how much m->refcount should be incremented/decremented
90 // (if at all) each time in the tests.
91 //
test(void (* body)(void * m),int timeout_s,int incr_step)92 static void test(void (*body)(void* m), int timeout_s, int incr_step) {
93   int64_t iterations = 1024;
94   struct test* m;
95   gpr_timespec start = gpr_now(GPR_CLOCK_REALTIME);
96   gpr_timespec time_taken;
97   gpr_timespec deadline = gpr_time_add(
98       start, gpr_time_from_micros(static_cast<int64_t>(timeout_s) * 1000000,
99                                   GPR_TIMESPAN));
100   while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0) {
101     if (iterations < INT64_MAX / 2) iterations <<= 1;
102     fprintf(stderr, " %ld", static_cast<long>(iterations));
103     fflush(stderr);
104     m = test_new(10, iterations, incr_step);
105     test_create_threads(m, body);
106     test_wait(m);
107     if (m->counter != m->thread_count * m->iterations * m->incr_step) {
108       fprintf(stderr, "counter %ld  threads %d  iterations %ld\n",
109               static_cast<long>(m->counter), m->thread_count,
110               static_cast<long>(m->iterations));
111       fflush(stderr);
112       FAIL();
113     }
114     test_destroy(m);
115   }
116   time_taken = gpr_time_sub(gpr_now(GPR_CLOCK_REALTIME), start);
117   fprintf(stderr, " done %lld.%09d s\n",
118           static_cast<long long>(time_taken.tv_sec),
119           static_cast<int>(time_taken.tv_nsec));
120   fflush(stderr);
121 }
122 
123 // Increment m->counter on each iteration; then mark thread as done.
inc(void * v)124 static void inc(void* v /*=m*/) {
125   struct test* m = static_cast<struct test*>(v);
126   int64_t i;
127   for (i = 0; i != m->iterations; i++) {
128     gpr_spinlock_lock(&m->mu);
129     m->counter++;
130     gpr_spinlock_unlock(&m->mu);
131   }
132 }
133 
134 // Increment m->counter under lock acquired with trylock, m->iterations times;
135 // then mark thread as done.
inctry(void * v)136 static void inctry(void* v /*=m*/) {
137   struct test* m = static_cast<struct test*>(v);
138   int64_t i;
139   for (i = 0; i != m->iterations;) {
140     if (gpr_spinlock_trylock(&m->mu)) {
141       m->counter++;
142       gpr_spinlock_unlock(&m->mu);
143       i++;
144     }
145   }
146 }
147 
148 // -------------------------------------------------
149 
TEST(SpinlockTest,Spinlock)150 TEST(SpinlockTest, Spinlock) { test(&inc, 1, 1); }
151 
TEST(SpinlockTest,SpinlockTry)152 TEST(SpinlockTest, SpinlockTry) { test(&inctry, 1, 1); }
153 
main(int argc,char ** argv)154 int main(int argc, char** argv) {
155   grpc::testing::TestEnvironment env(&argc, argv);
156   ::testing::InitGoogleTest(&argc, argv);
157   return RUN_ALL_TESTS();
158 }
159