Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_CPU_AFFINITY_POSIX_H_ |
| 6 | #define BASE_CPU_AFFINITY_POSIX_H_ |
| 7 | |
| 8 | #include "base/process/process_handle.h" |
| 9 | #include "base/threading/platform_thread.h" |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 10 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 11 | |
| 12 | namespace base { |
| 13 | |
Eric Seckler | a046784 | 2020-08-10 11:55:41 | [diff] [blame] | 14 | enum class CpuAffinityMode { |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 15 | // No restrictions on affinity. |
| 16 | kDefault, |
| 17 | // Restrict execution to LITTLE cores only. Only has an effect on platforms |
| 18 | // where we detect presence of big.LITTLE-like CPU architectures. |
| 19 | kLittleCoresOnly |
| 20 | }; |
| 21 | |
| 22 | // Sets or clears restrictions on the CPU affinity of the specified thread. |
| 23 | // Returns false if updating the affinity failed. |
| 24 | BASE_EXPORT bool SetThreadCpuAffinityMode(PlatformThreadId thread_id, |
| 25 | CpuAffinityMode affinity); |
| 26 | // Like SetThreadAffinityMode, but affects all current and future threads of |
| 27 | // the given process. Note that this may not apply to threads that are created |
| 28 | // in parallel to the execution of this function. |
| 29 | BASE_EXPORT bool SetProcessCpuAffinityMode(ProcessHandle process_handle, |
| 30 | CpuAffinityMode affinity); |
| 31 | |
Mikhail Khokhlov | a2483f8 | 2020-09-30 14:40:33 | [diff] [blame] | 32 | // Return true if the current architecture has big or bigger cores. |
| 33 | BASE_EXPORT bool HasBigCpuCores(); |
| 34 | |
| 35 | // For architectures with big cores, return the affinity mode that matches |
| 36 | // the CPU affinity of the current thread. If no affinity mode exactly matches, |
| 37 | // or if the architecture doesn't have different types of cores, |
| 38 | // return nullopt. |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 39 | BASE_EXPORT absl::optional<CpuAffinityMode> CurrentThreadCpuAffinityMode(); |
Mikhail Khokhlov | a2483f8 | 2020-09-30 14:40:33 | [diff] [blame] | 40 | |
Eric Seckler | 384f953 | 2020-08-06 09:41:56 | [diff] [blame] | 41 | } // namespace base |
| 42 | |
| 43 | #endif // BASE_CPU_AFFINITY_POSIX_H_ |