blob: 2ecab8fad2b87d4c5bf1dec3d63340ceb867dc0f [file] [log] [blame]
Eric Seckler384f9532020-08-06 09:41:561// 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 Bikineev7dd58ad2021-05-18 01:01:3910#include "third_party/abseil-cpp/absl/types/optional.h"
Eric Seckler384f9532020-08-06 09:41:5611
12namespace base {
13
Eric Secklera0467842020-08-10 11:55:4114enum class CpuAffinityMode {
Eric Seckler384f9532020-08-06 09:41:5615 // 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.
24BASE_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.
29BASE_EXPORT bool SetProcessCpuAffinityMode(ProcessHandle process_handle,
30 CpuAffinityMode affinity);
31
Mikhail Khokhlova2483f82020-09-30 14:40:3332// Return true if the current architecture has big or bigger cores.
33BASE_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 Bikineev7dd58ad2021-05-18 01:01:3939BASE_EXPORT absl::optional<CpuAffinityMode> CurrentThreadCpuAffinityMode();
Mikhail Khokhlova2483f82020-09-30 14:40:3340
Eric Seckler384f9532020-08-06 09:41:5641} // namespace base
42
43#endif // BASE_CPU_AFFINITY_POSIX_H_