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