llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Assert "On startup, a program is expected to be assigned only to one processor group!" fires on Windows Server 2022

Open wjschmidt opened this issue 4 years ago • 20 comments

This assert exists in llvm/lib/Support/Windows/Threading.inc. As described at https://docs.microsoft.com/en-us/windows/win32/procthread/processor-groups, "Starting with Windows 11 and Windows Server 2022, it is no longer the case that applications are constrained by default to a single processor group. Instead, processes and their threads have processor affinities that by default span all processors in the system, across multiple groups on machines with more than 64 processors." This means that a large number of lit tests fail on large systems running these operating systems.

On the system where I noticed this (running Windows Server 2022 with 160 logical processors), the following lit tests all fail. If I comment out this assert, they all pass.

Failed Tests (89):
  Clang :: CodeGenCXX/profile-remap.cpp
  Clang :: Profile/c-counter-overflows.c
  LLVM :: LTO/Resolution/X86/dead-strip-fulllto.ll
  LLVM :: LTO/Resolution/X86/lto-unit-check.ll
  LLVM :: Transforms/PGOProfile/misexpect-branch.ll
  LLVM :: tools/llvm-cov/native_separators.c
  LLVM :: tools/llvm-profdata/overflow-instr.test
  LLVM-Unit :: Support/./SupportTests.exe/Parallel/ForEachError
  lld :: COFF/Intel_COFF/whole_program_alias_02.ll
  lld :: COFF/Intel_COFF/whole_program_alias_03.ll
  lld :: COFF/Intel_COFF/whole_program_read_2.ll
  lld :: COFF/Intel_COFF/whole_program_read_trace_6.ll
  lld :: COFF/autoimport-x86.s
  lld :: COFF/cgprofile-txt.s
  lld :: COFF/comdat-selection-associative-largest.s
  lld :: COFF/crt-dyn-initializer-order.test
  lld :: COFF/debug-dwarf.test
  lld :: COFF/def-export-cpp.s
  lld :: COFF/dll.test
  lld :: COFF/dllimport-gc.test
  lld :: COFF/duplicate-imp-func.s
  lld :: COFF/empty-subsection.s
  lld :: COFF/export-all.s
  lld :: COFF/export32.test
  lld :: COFF/force-multipleres.test
  lld :: COFF/gfids-gc.s
  lld :: COFF/icf-safe.s
  lld :: COFF/incremental.test
  lld :: COFF/lto-comdat.ll
  lld :: COFF/lto-new-pass-manager.ll
  lld :: COFF/merge-resource-manifest.test
  lld :: COFF/opt.test
  lld :: COFF/pdb-globals-dia-vfunc-simple.test
  lld :: COFF/pdb-none.test
  lld :: COFF/pdb-type-server-missing.yaml
  lld :: COFF/pdb-type-server-simple.test
  lld :: COFF/pdbaltpath.test
  lld :: COFF/pdbstripped.test
  lld :: COFF/precomp-link.test
  lld :: COFF/rsds.test
  lld :: COFF/start-lib.ll
  lld :: COFF/symtab.test
  lld :: COFF/tls-alignment-64.ll
  lld :: COFF/unwind.test
  lld :: COFF/winsysroot.test
  lld :: COFF/wrap-import.ll
  lld :: COFF/wrap.s
  lld :: ELF/Intel_ELF/comdat-linkonce.s
  lld :: ELF/Intel_ELF/intel_advanced_opt.ll
  lld :: ELF/Intel_ELF/whole_program_alias_03.ll
  lld :: ELF/Intel_ELF/whole_program_alias_05.ll
  lld :: ELF/Intel_ELF/whole_program_read_1.ll
  lld :: ELF/Intel_ELF/whole_program_read_trace_1.ll
  lld :: ELF/Intel_ELF/whole_program_read_trace_5.ll
  lld :: ELF/abs-conflict.s
  lld :: ELF/allow-multiple-definition.s
  lld :: ELF/cgprofile-rela.test
  lld :: ELF/gnu-hash-table-rwsegment.s
  lld :: ELF/gnu-ifunc-nonpreemptible.s
  lld :: ELF/gnu-ifunc-plt.s
  lld :: ELF/linkerscript/filename-spec.s
  lld :: ELF/linkerscript/input-relative.s
  lld :: ELF/linkerscript/linkerscript.s
  lld :: ELF/linkerscript/linkorder.s
  lld :: ELF/linkerscript/loadaddr.s
  lld :: ELF/linkerscript/memory-attr.test
  lld :: ELF/linkerscript/memory-err.s
  lld :: ELF/linkerscript/memory-gap-explicit-expr.test
  lld :: ELF/linkerscript/memory-ignored-dot-assign.test
  lld :: ELF/linkerscript/memory-include.test
  lld :: ELF/linkerscript/memory-loc-counter.test
  lld :: ELF/linkerscript/memory-nonalloc.test
  lld :: ELF/linkerscript/memory.s
  lld :: ELF/linkerscript/merge-output-sections.s
  lld :: ELF/linkerscript/no-space.s
  lld :: ELF/linkerscript/non-alloc-segment.s
  lld :: ELF/lto/cache.ll
  lld :: ELF/lto/devirt_vcall_vis_public.ll
  lld :: ELF/lto/emit-asm.ll
  lld :: ELF/lto/inline-asm.ll
  lld :: ELF/lto/internalize-basic.ll
  lld :: ELF/lto/parallel-internalize.ll
  lld :: ELF/lto/wrap-unreferenced-before-codegen.test
  lld :: ELF/no-soname.s
  lld :: ELF/oformat-binary-ttext.s
  lld :: ELF/partition-icf.s
  lld :: ELF/relocatable.s
  lld :: ELF/relocation-common.s
  lld :: ELF/version-script.s

wjschmidt avatar Jul 19 '22 20:07 wjschmidt

@aganea is the one who wrote the assert and likely should take a look. @amccarth-google also reviewed this patch.

Also looks like @Bigcheese is the code owner here, but @rnk and @sam-mccall both might have additional input.

erichkeane avatar Oct 21 '22 19:10 erichkeane

@wjschmidt Are you able to debug? This codepath is only taken when the affinity mask has been changed on process startup, ie. START /AFFINITY 0x1 app.exe. I'm not too sure why it has passed the test ProcessAffinityMask != SystemAffinityMask if you hadn't changed the affinity mask.

aganea avatar Oct 21 '22 20:10 aganea

I'm not sure how effective I can be debugging this. I'm not a Windows programmer, and rarely have to debug in the Windows environment... so on the one occasion when I needed to, this popped up. I'm asking around to see if there's anything to change affinity in the internal compiler driver.

wjschmidt avatar Oct 21 '22 21:10 wjschmidt

If there's something you'd like me to specifically check, I'll be happy to look.

wjschmidt avatar Oct 21 '22 21:10 wjschmidt

I was unable to reproduce on a 112 vCPU or a 160 vCPU GCE instance. @wjschmidt Would you be able to post anything relevant to reproduce the issue? cmake script, logs, etc. Additionally, can you apply the following diff, I'd like to understand the underlying problem, even if the fix is trivial. Microsoft documentation states that affinity mask cannot cross processor groups, so I don't understand why you have an affinity mask set, and several processor groups assigned to the process.

diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.inc
index 11f34817dbbf..256aceba779c 100644
--- a/llvm/lib/Support/Windows/Threading.inc
+++ b/llvm/lib/Support/Windows/Threading.inc
@@ -207,6 +207,7 @@ static ArrayRef<ProcessorGroup> getProcessorGroups() {
     if (::GetProcessAffinityMask(GetCurrentProcess(), &ProcessAffinityMask,
                                  &SystemAffinityMask) &&
         ProcessAffinityMask != SystemAffinityMask) {
+      llvm::outs() << "Mask: " << ProcessAffinityMask << " " << SystemAffinityMask << '\n';
       // We don't expect more that 4 CPU groups on Windows (256 processors).
       USHORT GroupCount = 4;
       USHORT GroupArray[4]{};

And then:

C:\git\llvm-project>build\unittests\Support\SupportTests.exe --gtest_filter=Parallel.ForEachError
Note: Google Test filter = Parallel.ForEachError
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Parallel
[ RUN      ] Parallel.ForEachError
[       OK ] Parallel.ForEachError (1 ms)
[----------] 1 test from Parallel (2 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (2 ms total)
[  PASSED  ] 1 test.

C:\git\llvm-project>start /b /affinity F build\unittests\Support\SupportTests.exe --gtest_filter=Parallel.ForEachError

C:\git\llvm-project>Note: Google Test filter = Parallel.ForEachError
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Parallel
[ RUN      ] Parallel.ForEachError
Mask: 15 65535
[       OK ] Parallel.ForEachError (1 ms)
[----------] 1 test from Parallel (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (2 ms total)
[  PASSED  ] 1 test.

The first command doesn't go through the assert, while the second command goes through the codepath that shows the assert, which is your case. Please let me know how that goes on your end.

aganea avatar Oct 24 '22 12:10 aganea

Thank you, I'll take a look at this shortly!

wjschmidt avatar Oct 24 '22 12:10 wjschmidt

For this last test, I see the same results as you except that the mask results in the second case are "Mask: 15 4294967295". This is starting from a fresh environment with the patch applied.

One theory I have is that there's a benchmark in our test suite that's changing the affinity and not changing it back, leaving us exposed to the assert afterwards. I'm hunting for possible offenders.

wjschmidt avatar Oct 24 '22 15:10 wjschmidt

Sorry, please ignore the previous comment. I just realized I was using a version of the compiler that had worked around the assert. Let me retry.

wjschmidt avatar Oct 24 '22 15:10 wjschmidt

OK, with the assert re-enabled, I still see the results reported above. The test passes in both cases, and in the second case I see "Mask: 15 4294967295".

wjschmidt avatar Oct 24 '22 15:10 wjschmidt

What if you run build\unittests\Support\SupportTests.exe in a shell, with the diff above + assert enabled? Do you still see the assert firing? What is the value of Mask in that case?

What if you launch the tests through LIT? > python3 build\bin\llvm-lit.py llvm\test --filter SupportTests -a -vv Do you see the assert?

I'd be interested to see how many CPUs are there in each "processor group". You should have something similar in the Task Manager, the "NUMA nodes" will show you the "processor groups": Screenshot 2022-10-24 125339

aganea avatar Oct 24 '22 16:10 aganea

For the last question, I assume since I'm seeing two separate graphs we have two processor groups, probably one per socket. image

wjschmidt avatar Oct 24 '22 17:10 wjschmidt

Here's a peculiarity. If I set the affinity to something unreasonable, the system affinity mask changes:

$ start /b /affinity FfffF unittests/Support/SupportTests.exe --gtest_filter=Parallel.ForEachError                                                                                                       $ Note: Google Test filter = Parallel.ForEachError
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from Parallel
[ RUN      ] Parallel.ForEachError
Mask: 1048575 18446744073709551615
[       OK ] Parallel.ForEachError (4 ms)
[----------] 1 test from Parallel (4 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (5 ms total)
[  PASSED  ] 1 test.                                                                         

wjschmidt avatar Oct 24 '22 20:10 wjschmidt

Sorry I was a bit misleading - the Task Manager only shows the sockets, not the groups, since a group cannot contain more that 64 "processors/cores/hyper-threads". The following cmd-line application shows details about the groups.

Here's a peculiarity. If I set the affinity to something unreasonable, the system affinity mask changes:

Can you manually run the same command line several times? FFFFF is a perfectly reasonable bit mask for your system. Thing is that the OS traditionally allocates new processes to "process groups" in a round-robin manner. If you have 3 groups in your system, 2 of 64-cpus & 1 of 32-cpus, the second mask should change about 1 times out of 3 runs (if no other processes are started in the meanwhile). But now with Windows Server 2022, if an app. inherits all groups by default, it could be that the main thread randomly runs on any of the "processor groups".

I think overall the test ProcessAffinityMask != SystemAffinityMask does not make sense in a context of an asymmetric number of "processor groups".

aganea avatar Oct 24 '22 20:10 aganea

OK, I don't have authority to install CpuGroups.exe on this system, so we'll have to do without that.

Indeed, running the test several times shows the system affinity mask toggling back and forth between those two values in a semi-random way.

wjschmidt avatar Oct 24 '22 20:10 wjschmidt

I have verified that there's a specific benchmark that seems to be changing the affinity mask. From a fresh environment, the first time I build that benchmark, it succeeds. After I run it, if I attempt to build it again, I run into the assert and the build fails.

wjschmidt avatar Oct 24 '22 21:10 wjschmidt

I managed to repro on a GCP instance. I'll get back with a fix.

aganea avatar Nov 15 '22 02:11 aganea

Great news. Thanks very much!

wjschmidt avatar Nov 15 '22 14:11 wjschmidt

I've posted https://reviews.llvm.org/D138747

The new behavior in Windows 11 seems to be causing some (random) issues when using the ::GetProcessAffinityMask API. I can randomly see the returned process & system mask being different. This happens even in a simple program such as:

#include <process.h>
#include <stdio.h>
#include <inttypes.h>
#include <windows.h>

[[noreturn]] static void die(const char *msg) {
  printf("%s (%lu)\n", msg, GetLastError());
  exit(1);
}

int main() {
  DWORD_PTR ProcessAffinityMask = 0, SystemAffinityMask = 0;
  if (!::GetProcessAffinityMask(GetCurrentProcess(), &ProcessAffinityMask,
                               &SystemAffinityMask)) {
    die("failed 1");
  }

  printf("Mask: %" PRIx64 " %" PRIx64 "\n", ProcessAffinityMask,
         SystemAffinityMask);

  // We don't expect more that 4 CPU groups on Windows (256 processors).
  USHORT GroupCount = 0;
  if (::GetProcessGroupAffinity(GetCurrentProcess(), &GroupCount, nullptr)) {
    // Can't succeed if we pass 0 groups.
    die("failed 2");
  }
  if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
    die("failed 3");
  }
  USHORT *GroupArray = (USHORT *)calloc(GroupCount, sizeof(USHORT));
  if (!::GetProcessGroupAffinity(GetCurrentProcess(), &GroupCount, GroupArray)) {
    die("failed 4");
  }

  printf("%d groups:\n", GroupCount);
  for (unsigned i = 0; i < GroupCount; ++i) {
    printf("\tin group %d\n", GroupArray[i]);
  }

  free(GroupArray);
}

I think Windows has some kind of round-robin policy internally when scheduling the starting process mask & then it has similar policies when running the threads internally. The issue is more frequent when launching & stopping many executable, like when running the LLVM tests.

Please let me know if that doesn't fix the issue for you. I've tested on several instance sizes.

aganea avatar Nov 26 '22 19:11 aganea

Thanks very much! I've asked someone in our org to test the patch in the environment where the problems were occurring. Will get back to you, but assume that this will handle the issues, so please don't wait on me in case they are slow.

wjschmidt avatar Nov 28 '22 13:11 wjschmidt

Our local testing with this patch was successful. Thanks very much! Looking forward to the patch landing.

wjschmidt avatar Nov 29 '22 14:11 wjschmidt