Debian Codegraph @

test/core/gpr/spinlock_test.cc

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