Debian Codegraph @

test/core/gpr/cpu_test.cc

raw
1 /*
2 *
3 * Copyright 2015 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 gpr per-cpu support:
20 gpr_cpu_num_cores()
21 gpr_cpu_current_cpu()
22 */
23
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "gtest/gtest.h"
29
30 #include <grpc/grpc.h>
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/cpu.h>
33 #include <grpc/support/sync.h>
34 #include <grpc/support/time.h>
35
36 #include "src/core/lib/gprpp/thd.h"
37 #include "test/core/util/test_config.h"
38
39 /* Test structure is essentially:
40 1) Figure out how many cores are present on the test system
41 2) Create 3 times that many threads
42 3) Have each thread do some amount of work (basically want to
43 gaurantee that all threads are running at once, and enough of them
44 to run on all cores).
45 4) Each thread checks what core it is running on, and marks that core
46 as "used" in the test.
47 5) Count number of "used" cores.
48
49 The test will fail if:
50 1) gpr_cpu_num_cores() == 0
51 2) Any result from gpr_cpu_current_cpu() >= gpr_cpu_num_cores()
52 3) Ideally, we would fail if not all cores were seen as used. Unfortunately,
53 this is only probabilistically true, and depends on the OS, it's
54 scheduler, etc. So we just print out an indication of how many were seen;
55 hopefully developers can use this to sanity check their system.
56 */
57
58 /* Status shared across threads */
59 struct cpu_test {
60 gpr_mu mu;
61 int nthreads;
62 uint32_t ncores;
63 int is_done;
64 gpr_cv done_cv;
65 int* used; /* is this core used? */
66 unsigned r; /* random number */
67 };
68
69 static void worker_thread(void* arg) {
70 struct cpu_test* ct = static_cast<struct cpu_test*>(arg);
71 uint32_t cpu;
72 unsigned r = 12345678;
73 unsigned i, j;
74 /* Avoid repetitive division calculations */
75 int64_t max_i = 1000 / grpc_test_slowdown_factor();
76 int64_t max_j = 1000 / grpc_test_slowdown_factor();
77 for (i = 0; i < max_i; i++) {
78 /* run for a bit - just calculate something random. */
79 for (j = 0; j < max_j; j++) {
80 r = (r * 17) & ((r - i) | (r * i));
81 }
82 cpu = gpr_cpu_current_cpu();
83 ASSERT_LT(cpu, ct->ncores);
84 gpr_mu_lock(&ct->mu);
85 ct->used[cpu] = 1;
86 for (j = 0; j < ct->ncores; j++) {
87 if (!ct->used[j]) break;
88 }
89 gpr_mu_unlock(&ct->mu);
90 if (j == ct->ncores) {
91 break; /* all cpus have been used - no further use in running this test */
92 }
93 }
94 gpr_mu_lock(&ct->mu);
95 ct->r = r; /* make it look like we care about r's value... */
96 ct->nthreads--;
97 if (ct->nthreads == 0) {
98 ct->is_done = 1;
99 gpr_cv_signal(&ct->done_cv);
100 }
101 gpr_mu_unlock(&ct->mu);
102 }
103
104 static void cpu_test(void) {
105 uint32_t i;
106 int cores_seen = 0;
107 struct cpu_test ct;
108 ct.ncores = gpr_cpu_num_cores();
109 ASSERT_GT(ct.ncores, 0);
110 ct.nthreads = static_cast<int>(ct.ncores) * 3;
111 ct.used = static_cast<int*>(gpr_malloc(ct.ncores * sizeof(int)));
112 memset(ct.used, 0, ct.ncores * sizeof(int));
113 gpr_mu_init(&ct.mu);
114 gpr_cv_init(&ct.done_cv);
115 ct.is_done = 0;
116
117 uint32_t nthreads = ct.ncores * 3;
118 grpc_core::Thread* thd =
119 static_cast<grpc_core::Thread*>(gpr_malloc(sizeof(*thd) * nthreads));
120
121 for (i = 0; i < nthreads; i++) {
122 thd[i] = grpc_core::Thread("grpc_cpu_test", &worker_thread, &ct);
123 thd[i].Start();
124 }
125 gpr_mu_lock(&ct.mu);
126 while (!ct.is_done) {
127 gpr_cv_wait(&ct.done_cv, &ct.mu, gpr_inf_future(GPR_CLOCK_MONOTONIC));
128 }
129 gpr_mu_unlock(&ct.mu);
130 for (i = 0; i < nthreads; i++) {
131 thd[i].Join();
132 }
133 gpr_free(thd);
134 fprintf(stderr, "Saw cores [");
135 fflush(stderr);
136 for (i = 0; i < ct.ncores; i++) {
137 if (ct.used[i]) {
138 fprintf(stderr, "%d,", i);
139 fflush(stderr);
140 cores_seen++;
141 }
142 }
143 fprintf(stderr, "] (%d/%d)\n", cores_seen, ct.ncores);
144 fflush(stderr);
145 gpr_mu_destroy(&ct.mu);
146 gpr_cv_destroy(&ct.done_cv);
147 gpr_free(ct.used);
148 }
149
150 TEST(CpuTest, MainTest) { cpu_test(); }
151
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 }