Shinichiro Hamaji | 1a444a8 | 2016-02-16 13:49:49 +0900 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Koichi Shiraishi | df8cd05 | 2016-09-06 15:05:35 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 1a444a8 | 2016-02-16 13:49:49 +0900 | [diff] [blame] | 17 | #include "affinity.h" |
| 18 | |
| 19 | #include "flags.h" |
| 20 | #include "log.h" |
| 21 | |
| 22 | #ifdef __linux__ |
| 23 | |
Dan Willemsen | 5db7f7c | 2017-02-22 23:59:06 -0800 | [diff] [blame] | 24 | #include <random> |
| 25 | |
Shinichiro Hamaji | 1a444a8 | 2016-02-16 13:49:49 +0900 | [diff] [blame] | 26 | #include <sched.h> |
Dan Willemsen | 5db7f7c | 2017-02-22 23:59:06 -0800 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
Shinichiro Hamaji | 1a444a8 | 2016-02-16 13:49:49 +0900 | [diff] [blame] | 29 | |
| 30 | void SetAffinityForSingleThread() { |
| 31 | cpu_set_t cs; |
| 32 | CPU_ZERO(&cs); |
Dan Willemsen | 5db7f7c | 2017-02-22 23:59:06 -0800 | [diff] [blame] | 33 | std::default_random_engine generator(getpid()); |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 34 | std::uniform_int_distribution<int> distribution(0, g_flags.num_cpus - 1); |
Dan Willemsen | 5db7f7c | 2017-02-22 23:59:06 -0800 | [diff] [blame] | 35 | int cpu = distribution(generator); |
| 36 | |
| 37 | // Try to come up with a CPU and one close to it. This should work on most |
| 38 | // hyperthreaded system, but may be less optimal under stranger setups. |
| 39 | // Choosing two completely different CPUs would work here as well, it's just a |
| 40 | // couple percent faster if they're close (and still faster than letting the |
| 41 | // scheduler do whatever it wants). |
| 42 | cpu = cpu - (cpu % 2); |
| 43 | CPU_SET(cpu, &cs); |
| 44 | if (g_flags.num_cpus > 1) |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 45 | CPU_SET(cpu + 1, &cs); |
Dan Willemsen | 5db7f7c | 2017-02-22 23:59:06 -0800 | [diff] [blame] | 46 | |
Shinichiro Hamaji | 1a444a8 | 2016-02-16 13:49:49 +0900 | [diff] [blame] | 47 | if (sched_setaffinity(0, sizeof(cs), &cs) < 0) |
| 48 | WARN("sched_setaffinity: %s", strerror(errno)); |
| 49 | } |
| 50 | |
| 51 | void SetAffinityForMultiThread() { |
| 52 | cpu_set_t cs; |
| 53 | CPU_ZERO(&cs); |
| 54 | for (int i = 0; i < g_flags.num_cpus; i++) { |
| 55 | CPU_SET(i, &cs); |
| 56 | } |
| 57 | if (sched_setaffinity(0, sizeof(cs), &cs) < 0) |
| 58 | WARN("sched_setaffinity: %s", strerror(errno)); |
| 59 | } |
| 60 | |
| 61 | #else |
| 62 | |
| 63 | void SetAffinityForSingleThread() {} |
| 64 | void SetAffinityForMultiThread() {} |
| 65 | |
| 66 | #endif |