File: AwsThreadAffinity.cmake

package info (click to toggle)
aws-crt-python 0.16.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,328 kB
  • sloc: ansic: 330,743; python: 18,949; makefile: 6,271; sh: 3,712; asm: 754; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (47 lines) | stat: -rw-r--r-- 1,915 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

include(CheckSymbolExists)

# Check if the platform supports setting thread affinity
# (important for hitting full NIC entitlement on NUMA architectures)
function(aws_set_thread_affinity_method target)

    # Non-POSIX, Android, and Apple platforms do not support thread affinity.
    if (NOT UNIX OR ANDROID OR APPLE)
        target_compile_definitions(${target} PRIVATE
            -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_NONE)
        return()
    endif()

    list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
    list(APPEND CMAKE_REQUIRED_LIBRARIES pthread)

    set(headers "pthread.h")
    # BSDs put nonportable pthread declarations in a separate header.
    if(CMAKE_SYSTEM_NAME MATCHES BSD)
        set(headers "${headers};pthread_np.h")
    endif()

    # Using pthread attrs is the preferred method, but is glibc-specific.
    check_symbol_exists(pthread_attr_setaffinity_np "${headers}" USE_PTHREAD_ATTR_SETAFFINITY)
    if (USE_PTHREAD_ATTR_SETAFFINITY)
        target_compile_definitions(${target} PRIVATE
            -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_PTHREAD_ATTR)
        return()
    endif()

    # This method is still nonportable, but is supported by musl and BSDs.
    check_symbol_exists(pthread_setaffinity_np "${headers}" USE_PTHREAD_SETAFFINITY)
    if (USE_PTHREAD_SETAFFINITY)
        target_compile_definitions(${target} PRIVATE
            -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_PTHREAD)
        return()
    endif()

    # If we got here, we expected thread affinity support but didn't find it.
    # We still build with degraded NUMA performance, but show a warning.
    message(WARNING "No supported method for setting thread affinity")
    target_compile_definitions(${target} PRIVATE
        -DAWS_AFFINITY_METHOD=AWS_AFFINITY_METHOD_NONE)
endfunction()