Skip to content

Commit 8003f55

Browse files
Reland "[llvm-exegesis] Add thread IDs to subprocess memory names (#84451)"
This reverts commit aefad27. This relands commit 6bbe8a2. This patch was casuing build failures on non-Linux platforms due to the default implementations for the functions not being updated. This ended up causing out-of-line definition errors. Fixed for the relanding.
1 parent 65f07b8 commit 8003f55

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
@@ -301,6 +301,7 @@ class SubProcessFunctionExecutorImpl
301301
if (AddMemDefError)
302302
return AddMemDefError;
303303

304+
long ParentTID = SubprocessMemory::getCurrentTID();
304305
pid_t ParentOrChildPID = fork();
305306

306307
if (ParentOrChildPID == -1) {
@@ -314,7 +315,7 @@ class SubProcessFunctionExecutorImpl
314315
// Unregister handlers, signal handling is now handled through ptrace in
315316
// the host process.
316317
sys::unregisterHandlers();
317-
prepareAndRunBenchmark(PipeFiles[0], Key);
318+
prepareAndRunBenchmark(PipeFiles[0], Key, ParentTID);
318319
// The child process terminates in the above function, so we should never
319320
// get to this point.
320321
llvm_unreachable("Child process didn't exit when expected.");
@@ -415,8 +416,8 @@ class SubProcessFunctionExecutorImpl
415416
setrlimit(RLIMIT_CORE, &rlim);
416417
}
417418

418-
[[noreturn]] void prepareAndRunBenchmark(int Pipe,
419-
const BenchmarkKey &Key) const {
419+
[[noreturn]] void prepareAndRunBenchmark(int Pipe, const BenchmarkKey &Key,
420+
long ParentTID) const {
420421
// Disable core dumps in the child process as otherwise everytime we
421422
// encounter an execution failure like a segmentation fault, we will create
422423
// a core dump. We report the information directly rather than require the
@@ -473,7 +474,7 @@ class SubProcessFunctionExecutorImpl
473474

474475
Expected<int> AuxMemFDOrError =
475476
SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
476-
Key.MemoryValues, ParentPID, CounterFileDescriptor);
477+
Key.MemoryValues, ParentPID, ParentTID, CounterFileDescriptor);
477478
if (!AuxMemFDOrError)
478479
exit(ChildProcessExitCodeE::AuxiliaryMemorySetupFailed);
479480

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)