Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
vjpai | 861eb9f | 2016-01-19 13:37:27 -0800 | [diff] [blame] | 3 | * Copyright 2016, Google Inc. |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
vjpai | 4f5146d | 2016-01-19 15:28:52 -0800 | [diff] [blame] | 34 | #include "test/cpp/qps/limit_cores.h" |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 35 | |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 36 | #include <grpc/support/cpu.h> |
| 37 | #include <grpc/support/log.h> |
| 38 | #include <grpc/support/port_platform.h> |
| 39 | |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 40 | #ifdef GPR_CPU_LINUX |
| 41 | #ifndef _GNU_SOURCE |
| 42 | #define _GNU_SOURCE |
| 43 | #endif |
| 44 | #include <sched.h> |
David Garcia Quintas | 711766d | 2016-03-21 11:27:37 -0700 | [diff] [blame] | 45 | |
| 46 | namespace grpc { |
| 47 | namespace testing { |
| 48 | |
vjpai | 574020e | 2016-02-03 07:58:23 -0800 | [diff] [blame] | 49 | int LimitCores(const int* cores, int cores_size) { |
Vijay Pai | 33e5118 | 2016-02-01 16:40:06 -0800 | [diff] [blame] | 50 | const int num_cores = gpr_cpu_num_cores(); |
Vijay Pai | c64736d | 2016-02-01 09:33:11 -0800 | [diff] [blame] | 51 | int cores_set = 0; |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 52 | |
vjpai | 574020e | 2016-02-03 07:58:23 -0800 | [diff] [blame] | 53 | cpu_set_t* cpup = CPU_ALLOC(num_cores); |
Vijay Pai | c64736d | 2016-02-01 09:33:11 -0800 | [diff] [blame] | 54 | GPR_ASSERT(cpup); |
Vijay Pai | 33e5118 | 2016-02-01 16:40:06 -0800 | [diff] [blame] | 55 | const size_t size = CPU_ALLOC_SIZE(num_cores); |
Vijay Pai | c64736d | 2016-02-01 09:33:11 -0800 | [diff] [blame] | 56 | CPU_ZERO_S(size, cpup); |
| 57 | |
| 58 | if (cores_size > 0) { |
| 59 | for (int i = 0; i < cores_size; i++) { |
| 60 | if (cores[i] < num_cores) { |
| 61 | CPU_SET_S(cores[i], size, cpup); |
| 62 | cores_set++; |
| 63 | } |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 64 | } |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 65 | } else { |
Vijay Pai | c64736d | 2016-02-01 09:33:11 -0800 | [diff] [blame] | 66 | for (int i = 0; i < num_cores; i++) { |
| 67 | CPU_SET_S(i, size, cpup); |
| 68 | cores_set++; |
| 69 | } |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 70 | } |
Vijay Pai | 6e5c33c | 2016-07-30 02:56:46 -0700 | [diff] [blame] | 71 | bool affinity_set = (sched_setaffinity(0, size, cpup) == 0); |
Vijay Pai | c64736d | 2016-02-01 09:33:11 -0800 | [diff] [blame] | 72 | CPU_FREE(cpup); |
Vijay Pai | 6e5c33c | 2016-07-30 02:56:46 -0700 | [diff] [blame] | 73 | return affinity_set ? cores_set : num_cores; |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 74 | } |
David Garcia Quintas | 711766d | 2016-03-21 11:27:37 -0700 | [diff] [blame] | 75 | |
David Garcia Quintas | ca2886a | 2016-03-21 12:04:18 -0700 | [diff] [blame] | 76 | } // namespace testing |
| 77 | } // namespace grpc |
| 78 | #else |
David Garcia Quintas | 711766d | 2016-03-21 11:27:37 -0700 | [diff] [blame] | 79 | namespace grpc { |
| 80 | namespace testing { |
| 81 | |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 82 | // LimitCores is not currently supported for non-Linux platforms |
vjpai | 574020e | 2016-02-03 07:58:23 -0800 | [diff] [blame] | 83 | int LimitCores(const int*, int) { return gpr_cpu_num_cores(); } |
David Garcia Quintas | ca2886a | 2016-03-21 12:04:18 -0700 | [diff] [blame] | 84 | |
Vijay Pai | 0e66efd | 2016-01-13 11:15:49 -0800 | [diff] [blame] | 85 | } // namespace testing |
| 86 | } // namespace grpc |
David Garcia Quintas | ca2886a | 2016-03-21 12:04:18 -0700 | [diff] [blame] | 87 | #endif |