mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.0 KiB
140 lines
4.0 KiB
9 years ago
|
/*
|
||
|
*
|
||
8 years ago
|
* Copyright 2015 gRPC authors.
|
||
9 years ago
|
*
|
||
8 years ago
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
9 years ago
|
*
|
||
8 years ago
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
9 years ago
|
*
|
||
8 years ago
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
9 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
/* Test gpr per-cpu support:
|
||
|
gpr_cpu_num_cores()
|
||
|
gpr_cpu_current_cpu()
|
||
|
*/
|
||
|
|
||
|
#include <grpc/support/alloc.h>
|
||
|
#include <grpc/support/cpu.h>
|
||
|
#include <grpc/support/log.h>
|
||
|
#include <grpc/support/sync.h>
|
||
|
#include <grpc/support/thd.h>
|
||
|
#include <grpc/support/time.h>
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include "test/core/util/test_config.h"
|
||
|
|
||
|
/* Test structure is essentially:
|
||
|
1) Figure out how many cores are present on the test system
|
||
|
2) Create 3 times that many threads
|
||
|
3) Have each thread do some amount of work (basically want to
|
||
|
gaurantee that all threads are running at once, and enough of them
|
||
|
to run on all cores).
|
||
|
4) Each thread checks what core it is running on, and marks that core
|
||
|
as "used" in the test.
|
||
9 years ago
|
5) Count number of "used" cores.
|
||
9 years ago
|
|
||
|
The test will fail if:
|
||
|
1) gpr_cpu_num_cores() == 0
|
||
9 years ago
|
2) Any result from gpr_cpu_current_cpu() >= gpr_cpu_num_cores()
|
||
|
3) Ideally, we would fail if not all cores were seen as used. Unfortunately,
|
||
|
this is only probabilistically true, and depends on the OS, it's
|
||
|
scheduler, etc. So we just print out an indication of how many were seen;
|
||
|
hopefully developers can use this to sanity check their system.
|
||
9 years ago
|
*/
|
||
|
|
||
|
/* Status shared across threads */
|
||
|
struct cpu_test {
|
||
|
gpr_mu mu;
|
||
|
int nthreads;
|
||
9 years ago
|
uint32_t ncores;
|
||
9 years ago
|
int is_done;
|
||
|
gpr_cv done_cv;
|
||
7 years ago
|
int* used; /* is this core used? */
|
||
9 years ago
|
unsigned r; /* random number */
|
||
9 years ago
|
};
|
||
|
|
||
7 years ago
|
static void worker_thread(void* arg) {
|
||
|
struct cpu_test* ct = (struct cpu_test*)arg;
|
||
9 years ago
|
uint32_t cpu;
|
||
9 years ago
|
unsigned r = 12345678;
|
||
9 years ago
|
unsigned i, j;
|
||
8 years ago
|
/* Avoid repetitive division calculations */
|
||
|
int64_t max_i = 1000 / grpc_test_slowdown_factor();
|
||
7 years ago
|
int64_t max_j = 1000 / grpc_test_slowdown_factor();
|
||
8 years ago
|
for (i = 0; i < max_i; i++) {
|
||
9 years ago
|
/* run for a bit - just calculate something random. */
|
||
8 years ago
|
for (j = 0; j < max_j; j++) {
|
||
9 years ago
|
r = (r * 17) & ((r - i) | (r * i));
|
||
|
}
|
||
|
cpu = gpr_cpu_current_cpu();
|
||
|
GPR_ASSERT(cpu < ct->ncores);
|
||
|
gpr_mu_lock(&ct->mu);
|
||
|
ct->used[cpu] = 1;
|
||
9 years ago
|
for (j = 0; j < ct->ncores; j++) {
|
||
|
if (!ct->used[j]) break;
|
||
|
}
|
||
9 years ago
|
gpr_mu_unlock(&ct->mu);
|
||
9 years ago
|
if (j == ct->ncores) {
|
||
|
break; /* all cpus have been used - no further use in running this test */
|
||
|
}
|
||
9 years ago
|
}
|
||
|
gpr_mu_lock(&ct->mu);
|
||
|
ct->r = r; /* make it look like we care about r's value... */
|
||
|
ct->nthreads--;
|
||
|
if (ct->nthreads == 0) {
|
||
|
ct->is_done = 1;
|
||
|
gpr_cv_signal(&ct->done_cv);
|
||
|
}
|
||
|
gpr_mu_unlock(&ct->mu);
|
||
|
}
|
||
|
|
||
|
static void cpu_test(void) {
|
||
9 years ago
|
uint32_t i;
|
||
9 years ago
|
int cores_seen = 0;
|
||
9 years ago
|
struct cpu_test ct;
|
||
|
gpr_thd_id thd;
|
||
|
ct.ncores = gpr_cpu_num_cores();
|
||
|
GPR_ASSERT(ct.ncores > 0);
|
||
|
ct.nthreads = (int)ct.ncores * 3;
|
||
7 years ago
|
ct.used = static_cast<int*>(gpr_malloc(ct.ncores * sizeof(int)));
|
||
9 years ago
|
memset(ct.used, 0, ct.ncores * sizeof(int));
|
||
|
gpr_mu_init(&ct.mu);
|
||
|
gpr_cv_init(&ct.done_cv);
|
||
|
ct.is_done = 0;
|
||
|
for (i = 0; i < ct.ncores * 3; i++) {
|
||
7 years ago
|
GPR_ASSERT(
|
||
|
gpr_thd_new(&thd, "grpc_cpu_test", &worker_thread, &ct, nullptr));
|
||
9 years ago
|
}
|
||
|
gpr_mu_lock(&ct.mu);
|
||
|
while (!ct.is_done) {
|
||
7 years ago
|
gpr_cv_wait(&ct.done_cv, &ct.mu, gpr_inf_future(GPR_CLOCK_MONOTONIC));
|
||
9 years ago
|
}
|
||
|
gpr_mu_unlock(&ct.mu);
|
||
9 years ago
|
fprintf(stderr, "Saw cores [");
|
||
7 years ago
|
fflush(stderr);
|
||
9 years ago
|
for (i = 0; i < ct.ncores; i++) {
|
||
9 years ago
|
if (ct.used[i]) {
|
||
9 years ago
|
fprintf(stderr, "%d,", i);
|
||
7 years ago
|
fflush(stderr);
|
||
9 years ago
|
cores_seen++;
|
||
|
}
|
||
9 years ago
|
}
|
||
9 years ago
|
fprintf(stderr, "] (%d/%d)\n", cores_seen, ct.ncores);
|
||
7 years ago
|
fflush(stderr);
|
||
9 years ago
|
gpr_free(ct.used);
|
||
9 years ago
|
}
|
||
|
|
||
7 years ago
|
int main(int argc, char* argv[]) {
|
||
9 years ago
|
grpc_test_init(argc, argv);
|
||
|
cpu_test();
|
||
|
return 0;
|
||
|
}
|