Debian Codegraph @

crt/aws-c-common/cmake/AwsThreadAffinity.cmake

raw
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 include(CheckSymbolExists)
5
6 # Check if the platform supports setting thread affinity
7 # (important for hitting full NIC entitlement on NUMA architectures)
8 function(aws_set_thread_affinity_method target)
9
10 # Non-POSIX, Android, and Apple platforms do not support thread affinity.
11 if (NOT UNIX OR ANDROID OR APPLE)
12 target_compile_definitions(${target} PRIVATE
13 -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_NONE)
14 return()
15 endif()
16
17 list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
18 list(APPEND CMAKE_REQUIRED_LIBRARIES pthread)
19
20 set(headers "pthread.h")
21 # BSDs put nonportable pthread declarations in a separate header.
22 if(CMAKE_SYSTEM_NAME MATCHES BSD)
23 set(headers "${headers};pthread_np.h")
24 endif()
25
26 # Using pthread attrs is the preferred method, but is glibc-specific.
27 check_symbol_exists(pthread_attr_setaffinity_np "${headers}" USE_PTHREAD_ATTR_SETAFFINITY)
28 if (USE_PTHREAD_ATTR_SETAFFINITY)
29 target_compile_definitions(${target} PRIVATE
30 -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_PTHREAD_ATTR)
31 return()
32 endif()
33
34 # This method is still nonportable, but is supported by musl and BSDs.
35 check_symbol_exists(pthread_setaffinity_np "${headers}" USE_PTHREAD_SETAFFINITY)
36 if (USE_PTHREAD_SETAFFINITY)
37 target_compile_definitions(${target} PRIVATE
38 -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_PTHREAD)
39 return()
40 endif()
41
42 # If we got here, we expected thread affinity support but didn't find it.
43 # We still build with degraded NUMA performance, but show a warning.
44 message(WARNING "No supported method for setting thread affinity")
45 target_compile_definitions(${target} PRIVATE
46 -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_NONE)
47 endfunction()