From 4b03f02a613455d438471873f5735c0e7b6c26a3 Mon Sep 17 00:00:00 2001 From: Saleel Kudchadker Date: Mon, 10 May 2021 18:11:45 -0700 Subject: [PATCH] SWDEV-280773 - Honor CPU affinity with env var Setting AMD_CPU_AFFINITY = 1 will make runtime honor core affinity that the process may set. This is disabled by default as it can prevent worker thread or any thread that runtime creates from getting scheduled thus affecting performance. Change-Id: Ibe4cc95e7b99caee5ce750b7bf66e09e999cc9a3 [ROCm/clr commit: 1398719b0dee218e168b9b8264590c074df18e35] --- projects/clr/rocclr/os/os_posix.cpp | 19 +++++++++++-------- projects/clr/rocclr/utils/flags.hpp | 12 ++++++------ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/projects/clr/rocclr/os/os_posix.cpp b/projects/clr/rocclr/os/os_posix.cpp index 075b3182ba..ce9b5bdb44 100644 --- a/projects/clr/rocclr/os/os_posix.cpp +++ b/projects/clr/rocclr/os/os_posix.cpp @@ -23,6 +23,7 @@ #include "os/os.hpp" #include "thread/thread.hpp" #include "utils/util.hpp" +#include "utils/flags.hpp" #include #include @@ -368,14 +369,16 @@ const void* Os::createOsThread(amd::Thread* thread) { // We never plan the use join, so free the resources now. ::pthread_attr_setdetachstate(&threadAttr, PTHREAD_CREATE_DETACHED); - cpu_set_t cpuset; - if (processorCount_ > 0) { - CPU_ZERO(&cpuset); - for (int i = 0; i < processorCount_; i++) { - CPU_SET(i, &cpuset); - } - if (0 != pthread_attr_setaffinity_np(&threadAttr, sizeof(cpu_set_t), &cpuset)) { - fatal("pthread_attr_setaffinity_np failed to set affinity"); + if (!AMD_CPU_AFFINITY) { + cpu_set_t cpuset; + if (processorCount_ > 0) { + CPU_ZERO(&cpuset); + for (int i = 0; i < processorCount_; i++) { + CPU_SET(i, &cpuset); + } + if (0 != pthread_attr_setaffinity_np(&threadAttr, sizeof(cpu_set_t), &cpuset)) { + fatal("pthread_attr_setaffinity_np failed to set affinity"); + } } } diff --git a/projects/clr/rocclr/utils/flags.hpp b/projects/clr/rocclr/utils/flags.hpp index 3fb639f7f3..50c0b23f55 100644 --- a/projects/clr/rocclr/utils/flags.hpp +++ b/projects/clr/rocclr/utils/flags.hpp @@ -233,8 +233,6 @@ release(uint, HIP_HIDDEN_FREE_MEM, 0, \ "0 = Disable") \ release(size_t, GPU_FORCE_BLIT_COPY_SIZE, 0, \ "Size in KB of the threshold below which to force blit instead for sdma") \ -release(bool, ROC_BARRIER_SYNC, true, \ - "Enable AQL barrier packet for synchronization") \ release(bool, ROC_ACTIVE_WAIT, false, \ "Forces unconditional active wait for GPU") \ release(bool, ROC_ENABLE_LARGE_BAR, true, \ @@ -254,10 +252,12 @@ release(bool, PAL_EMBED_KERNEL_MD, false, \ release(cstring, ROC_GLOBAL_CU_MASK, "", \ "Sets a global CU mask (entered as hex value) for all queues," \ "Each active bit represents using one CU (e.g., 0xf enables only 4 CUs)") \ -release(cstring, AMD_LOG_LEVEL_FILE, "", \ - "Set output file for AMD_LOG_LEVEL, Default is stderr") \ -release(size_t, PAL_PREPINNED_MEMORY_SIZE, 64, \ - "Size in KBytes of prepinned memory") +release(cstring, AMD_LOG_LEVEL_FILE, "", \ + "Set output file for AMD_LOG_LEVEL, Default is stderr") \ +release(size_t, PAL_PREPINNED_MEMORY_SIZE, 64, \ + "Size in KBytes of prepinned memory") \ +release(bool, AMD_CPU_AFFINITY, false, \ + "Reset CPU affinity of any runtime threads") namespace amd {