Skip to content

Commit 50e6218

Browse files
Reland "[llvm-exegesis] Add thread IDs to subprocess memory names (#84451)"
This reverts commit 1fe9c41. This relands commit 6bbe8a2. This was causing build failures on one of the ARMv8 builders. Still not completely sure why, but relanding it to see if the failure pops up again. If it does, the plan is to fix forward by disabling tests on ARM temporarily as llvm-exegesis does not currently use SubprocessMemory on ARM.
1 parent 80fc612 commit 50e6218

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ class SubProcessFunctionExecutorImpl
405405
if (AddMemDefError)
406406
return AddMemDefError;
407407

408+
long ParentTID = SubprocessMemory::getCurrentTID();
408409
pid_t ParentOrChildPID = fork();
409410

410411
if (ParentOrChildPID == -1) {
@@ -418,7 +419,7 @@ class SubProcessFunctionExecutorImpl
418419
// Unregister handlers, signal handling is now handled through ptrace in
419420
// the host process.
420421
sys::unregisterHandlers();
421-
runChildSubprocess(PipeFiles[0], Key);
422+
runChildSubprocess(PipeFiles[0], Key, ParentTID);
422423
// The child process terminates in the above function, so we should never
423424
// get to this point.
424425
llvm_unreachable("Child process didn't exit when expected.");
@@ -439,8 +440,8 @@ class SubProcessFunctionExecutorImpl
439440
setrlimit(RLIMIT_CORE, &rlim);
440441
}
441442

442-
[[noreturn]] void runChildSubprocess(int Pipe,
443-
const BenchmarkKey &Key) const {
443+
[[noreturn]] void runChildSubprocess(int Pipe, const BenchmarkKey &Key,
444+
long ParentTID) const {
444445
// Disable core dumps in the child process as otherwise everytime we
445446
// encounter an execution failure like a segmentation fault, we will create
446447
// a core dump. We report the information directly rather than require the
@@ -497,7 +498,7 @@ class SubProcessFunctionExecutorImpl
497498

498499
Expected<int> AuxMemFDOrError =
499500
SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
500-
Key.MemoryValues, ParentPID, CounterFileDescriptor);
501+
Key.MemoryValues, ParentPID, ParentTID, CounterFileDescriptor);
501502
if (!AuxMemFDOrError)
502503
exit(ChildProcessExitCodeE::AuxiliaryMemorySetupFailed);
503504

llvm/tools/llvm-exegesis/lib/SubprocessMemory.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#include "SubprocessMemory.h"
1010
#include "Error.h"
1111
#include "llvm/Support/Error.h"
12+
#include "llvm/Support/FormatVariadic.h"
1213
#include <cerrno>
1314

1415
#ifdef __linux__
1516
#include <fcntl.h>
1617
#include <sys/mman.h>
18+
#include <sys/syscall.h>
1719
#include <unistd.h>
1820
#endif
1921

@@ -22,12 +24,21 @@ namespace exegesis {
2224

2325
#if defined(__linux__) && !defined(__ANDROID__)
2426

27+
long SubprocessMemory::getCurrentTID() {
28+
// We're using the raw syscall here rather than the gettid() function provided
29+
// by most libcs for compatibility as gettid() was only added to glibc in
30+
// version 2.30.
31+
return syscall(SYS_gettid);
32+
}
33+
2534
Error SubprocessMemory::initializeSubprocessMemory(pid_t ProcessID) {
2635
// Add the PID to the shared memory name so that if we're running multiple
2736
// processes at the same time, they won't interfere with each other.
2837
// This comes up particularly often when running the exegesis tests with
29-
// llvm-lit
30-
std::string AuxiliaryMemoryName = "/auxmem" + std::to_string(ProcessID);
38+
// llvm-lit. Additionally add the TID so that downstream consumers
39+
// using multiple threads don't run into conflicts.
40+
std::string AuxiliaryMemoryName =
41+
formatv("/{0}auxmem{1}", getCurrentTID(), ProcessID);
3142
int AuxiliaryMemoryFD = shm_open(AuxiliaryMemoryName.c_str(),
3243
O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
3344
if (AuxiliaryMemoryFD == -1)
@@ -47,8 +58,8 @@ Error SubprocessMemory::addMemoryDefinition(
4758
pid_t ProcessPID) {
4859
SharedMemoryNames.reserve(MemoryDefinitions.size());
4960
for (auto &[Name, MemVal] : MemoryDefinitions) {
50-
std::string SharedMemoryName = "/" + std::to_string(ProcessPID) + "memdef" +
51-
std::to_string(MemVal.Index);
61+
std::string SharedMemoryName =
62+
formatv("/{0}t{1}memdef{2}", ProcessPID, getCurrentTID(), MemVal.Index);
5263
SharedMemoryNames.push_back(SharedMemoryName);
5364
int SharedMemoryFD =
5465
shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
@@ -82,8 +93,9 @@ Error SubprocessMemory::addMemoryDefinition(
8293

8394
Expected<int> SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
8495
std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
85-
pid_t ParentPID, int CounterFileDescriptor) {
86-
std::string AuxiliaryMemoryName = "/auxmem" + std::to_string(ParentPID);
96+
pid_t ParentPID, long ParentTID, int CounterFileDescriptor) {
97+
std::string AuxiliaryMemoryName =
98+
formatv("/{0}auxmem{1}", ParentTID, ParentPID);
8799
int AuxiliaryMemoryFileDescriptor =
88100
shm_open(AuxiliaryMemoryName.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
89101
if (AuxiliaryMemoryFileDescriptor == -1)
@@ -97,8 +109,8 @@ Expected<int> SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
97109
return make_error<Failure>("Mapping auxiliary memory failed");
98110
AuxiliaryMemoryMapping[0] = CounterFileDescriptor;
99111
for (auto &[Name, MemVal] : MemoryDefinitions) {
100-
std::string MemoryValueName = "/" + std::to_string(ParentPID) + "memdef" +
101-
std::to_string(MemVal.Index);
112+
std::string MemoryValueName =
113+
formatv("/{0}t{1}memdef{2}", ParentPID, ParentTID, MemVal.Index);
102114
AuxiliaryMemoryMapping[AuxiliaryMemoryOffset + MemVal.Index] =
103115
shm_open(MemoryValueName.c_str(), O_RDWR, S_IRUSR | S_IWUSR);
104116
if (AuxiliaryMemoryMapping[AuxiliaryMemoryOffset + MemVal.Index] == -1)
@@ -133,7 +145,7 @@ Error SubprocessMemory::addMemoryDefinition(
133145

134146
Expected<int> SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
135147
std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
136-
pid_t ParentPID, int CounterFileDescriptor) {
148+
pid_t ParentPID, long ParentTID, int CounterFileDescriptor) {
137149
return make_error<Failure>(
138150
"setupAuxiliaryMemoryInSubprocess is only supported on Linux");
139151
}

llvm/tools/llvm-exegesis/lib/SubprocessMemory.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class SubprocessMemory {
3535
static constexpr const size_t AuxiliaryMemoryOffset = 1;
3636
static constexpr const size_t AuxiliaryMemorySize = 4096;
3737

38+
// Gets the thread ID for the calling thread.
39+
static long getCurrentTID();
40+
3841
Error initializeSubprocessMemory(pid_t ProcessID);
3942

4043
// The following function sets up memory definitions. It creates shared
@@ -54,7 +57,7 @@ class SubprocessMemory {
5457
// section.
5558
static Expected<int> setupAuxiliaryMemoryInSubprocess(
5659
std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
57-
pid_t ParentPID, int CounterFileDescriptor);
60+
pid_t ParentPID, long ParentTID, int CounterFileDescriptor);
5861

5962
~SubprocessMemory();
6063

llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <endian.h>
1818
#include <fcntl.h>
1919
#include <sys/mman.h>
20+
#include <sys/syscall.h>
2021
#include <unistd.h>
2122
#endif // __linux__
2223

@@ -49,7 +50,9 @@ class SubprocessMemoryTest : public X86TestBase {
4950

5051
std::string getSharedMemoryName(const unsigned TestNumber,
5152
const unsigned DefinitionNumber) {
52-
return "/" + std::to_string(getSharedMemoryNumber(TestNumber)) + "memdef" +
53+
long CurrentTID = syscall(SYS_gettid);
54+
return "/" + std::to_string(getSharedMemoryNumber(TestNumber)) + "t" +
55+
std::to_string(CurrentTID) + "memdef" +
5356
std::to_string(DefinitionNumber);
5457
}
5558

0 commit comments

Comments
 (0)