diff options
author | Nicolas Noble <nnoble@google.com> | 2015-02-24 13:29:29 -0800 |
---|---|---|
committer | Nicolas Noble <nnoble@google.com> | 2015-02-24 16:52:06 -0800 |
commit | 085603c112d3d2daefb5c776590185e7680b6aca (patch) | |
tree | 93bc7ba09939408b6e1cb1b68c5b46e3447fdddf /src/core | |
parent | eeef8cd52b34282c89d1eadf7daff7bf401ac9b0 (diff) |
Second batch of feedback.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/statistics/census_log.c | 4 | ||||
-rw-r--r-- | src/core/support/alloc.c | 3 | ||||
-rw-r--r-- | src/core/support/cpu_linux.c | 25 | ||||
-rw-r--r-- | src/core/support/cpu_posix.c | 20 |
4 files changed, 30 insertions, 22 deletions
diff --git a/src/core/statistics/census_log.c b/src/core/statistics/census_log.c index 24e46876d2..ec56ce38df 100644 --- a/src/core/statistics/census_log.c +++ b/src/core/statistics/census_log.c @@ -475,11 +475,11 @@ void census_log_initialize(size_t size_in_mb, int discard_old_records) { g_log.block_being_read = NULL; gpr_atm_rel_store(&g_log.is_full, 0); g_log.core_local_blocks = (cl_core_local_block*)gpr_malloc_aligned( - g_log.num_cores * sizeof(cl_core_local_block), GPR_CACHELINE_SIZE); + g_log.num_cores * sizeof(cl_core_local_block), GPR_CACHELINE_SIZE_LOG); memset(g_log.core_local_blocks, 0, g_log.num_cores * sizeof(cl_core_local_block)); g_log.blocks = (cl_block*)gpr_malloc_aligned( - g_log.num_blocks * sizeof(cl_block), GPR_CACHELINE_SIZE); + g_log.num_blocks * sizeof(cl_block), GPR_CACHELINE_SIZE_LOG); memset(g_log.blocks, 0, g_log.num_blocks * sizeof(cl_block)); g_log.buffer = gpr_malloc(g_log.num_blocks * CENSUS_LOG_MAX_RECORD_SIZE); memset(g_log.buffer, 0, g_log.num_blocks * CENSUS_LOG_MAX_RECORD_SIZE); diff --git a/src/core/support/alloc.c b/src/core/support/alloc.c index 44f343b4f4..a19a0141d4 100644 --- a/src/core/support/alloc.c +++ b/src/core/support/alloc.c @@ -54,7 +54,8 @@ void *gpr_realloc(void *p, size_t size) { return p; } -void *gpr_malloc_aligned(size_t size, size_t alignment) { +void *gpr_malloc_aligned(size_t size, size_t alignment_log) { + size_t alignment = 1 << alignment_log; size_t extra = alignment - 1 + sizeof(void *); void *p = gpr_malloc(size + extra); void **ret = (void **)(((gpr_uintptr)p + extra) & ~(alignment - 1)); diff --git a/src/core/support/cpu_linux.c b/src/core/support/cpu_linux.c index ef6bf9ca09..37e840d4cf 100644 --- a/src/core/support/cpu_linux.c +++ b/src/core/support/cpu_linux.c @@ -39,25 +39,28 @@ #ifdef GPR_CPU_LINUX -#include <grpc/support/cpu.h> - #include <sched.h> #include <errno.h> #include <unistd.h> #include <string.h> +#include <grpc/support/cpu.h> #include <grpc/support/log.h> +#include <grpc/support/sync.h> -unsigned gpr_cpu_num_cores(void) { - static int ncpus = 0; - /* FIXME: !threadsafe */ - if (ncpus == 0) { - ncpus = sysconf(_SC_NPROCESSORS_ONLN); - if (ncpus < 1) { - gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1"); - ncpus = 1; - } +static int ncpus = 0; + +static void init_num_cpus() { + ncpus = sysconf(_SC_NPROCESSORS_ONLN); + if (ncpus < 1) { + gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1"); + ncpus = 1; } +} + +unsigned gpr_cpu_num_cores(void) { + static gpr_once once = GPR_ONCE_INIT; + gpr_once_init(&once, init_num_cpus); return ncpus; } diff --git a/src/core/support/cpu_posix.c b/src/core/support/cpu_posix.c index 91ce80c364..33c7b90b0b 100644 --- a/src/core/support/cpu_posix.c +++ b/src/core/support/cpu_posix.c @@ -43,15 +43,19 @@ static __thread char magic_thread_local; -unsigned gpr_cpu_num_cores(void) { - static int ncpus = 0; - if (ncpus == 0) { - ncpus = sysconf(_SC_NPROCESSORS_ONLN); - if (ncpus < 1) { - gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1"); - ncpus = 1; - } +static int ncpus = 0; + +static void init_ncpus() { + ncpus = sysconf(_SC_NPROCESSORS_ONLN); + if (ncpus < 1) { + gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1"); + ncpus = 1; } +} + +unsigned gpr_cpu_num_cores(void) { + static gpr_once once = GPR_ONCE_INIT; + gpr_once_init(&once, init_num_cpus); return ncpus; } |