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


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:59