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