Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
ctiller | d94ad10 | 2014-12-23 08:53:43 -0800 | [diff] [blame] | 19 | #include <grpc/support/port_platform.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 20 | |
ctiller | d94ad10 | 2014-12-23 08:53:43 -0800 | [diff] [blame] | 21 | #ifdef GPR_CPU_POSIX |
| 22 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 23 | #include <errno.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 24 | #include <string.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 26 | |
| 27 | #include <grpc/support/log.h> |
Craig Tiller | b5a79f6 | 2015-02-24 21:02:51 -0800 | [diff] [blame] | 28 | #include <grpc/support/sync.h> |
yang-g | 6955c5e | 2017-02-13 15:49:27 -0800 | [diff] [blame] | 29 | #include <grpc/support/useful.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 30 | |
Jorge Canizales | 30e666c | 2015-03-13 14:45:40 -0700 | [diff] [blame] | 31 | static __thread char magic_thread_local; |
| 32 | |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 33 | static long ncpus = 0; |
Nicolas Noble | 085603c | 2015-02-24 13:29:29 -0800 | [diff] [blame] | 34 | |
| 35 | static void init_ncpus() { |
| 36 | ncpus = sysconf(_SC_NPROCESSORS_ONLN); |
Jan Tattermusch | f423b06 | 2016-01-19 19:16:36 -0800 | [diff] [blame] | 37 | if (ncpus < 1 || ncpus > INT32_MAX) { |
Nicolas Noble | 085603c | 2015-02-24 13:29:29 -0800 | [diff] [blame] | 38 | gpr_log(GPR_ERROR, "Cannot determine number of CPUs: assuming 1"); |
| 39 | ncpus = 1; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | } |
Nicolas Noble | 085603c | 2015-02-24 13:29:29 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | unsigned gpr_cpu_num_cores(void) { |
| 44 | static gpr_once once = GPR_ONCE_INIT; |
Craig Tiller | b5a79f6 | 2015-02-24 21:02:51 -0800 | [diff] [blame] | 45 | gpr_once_init(&once, init_ncpus); |
Craig Tiller | ebc7ef2 | 2015-09-10 22:19:25 -0700 | [diff] [blame] | 46 | return (unsigned)ncpus; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jorge Canizales | 30e666c | 2015-03-13 14:45:40 -0700 | [diff] [blame] | 49 | unsigned gpr_cpu_current_cpu(void) { |
| 50 | /* NOTE: there's no way I know to return the actual cpu index portably... |
| 51 | most code that's using this is using it to shard across work queues though, |
| 52 | so here we use thread identity instead to achieve a similar though not |
| 53 | identical effect */ |
yang-g | 6955c5e | 2017-02-13 15:49:27 -0800 | [diff] [blame] | 54 | return (unsigned)GPR_HASH_POINTER(&magic_thread_local, gpr_cpu_num_cores()); |
Jorge Canizales | 30e666c | 2015-03-13 14:45:40 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | #endif /* GPR_CPU_POSIX */ |