9
9
#include " SubprocessMemory.h"
10
10
#include " Error.h"
11
11
#include " llvm/Support/Error.h"
12
+ #include " llvm/Support/FormatVariadic.h"
12
13
#include < cerrno>
13
14
14
15
#ifdef __linux__
15
16
#include < fcntl.h>
16
17
#include < sys/mman.h>
18
+ #include < sys/syscall.h>
17
19
#include < unistd.h>
18
20
#endif
19
21
@@ -22,12 +24,21 @@ namespace exegesis {
22
24
23
25
#if defined(__linux__) && !defined(__ANDROID__)
24
26
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
+
25
34
Error SubprocessMemory::initializeSubprocessMemory (pid_t ProcessID) {
26
35
// Add the PID to the shared memory name so that if we're running multiple
27
36
// processes at the same time, they won't interfere with each other.
28
37
// 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);
31
42
int AuxiliaryMemoryFD = shm_open (AuxiliaryMemoryName.c_str (),
32
43
O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
33
44
if (AuxiliaryMemoryFD == -1 )
@@ -47,8 +58,8 @@ Error SubprocessMemory::addMemoryDefinition(
47
58
pid_t ProcessPID) {
48
59
SharedMemoryNames.reserve (MemoryDefinitions.size ());
49
60
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 );
52
63
SharedMemoryNames.push_back (SharedMemoryName);
53
64
int SharedMemoryFD =
54
65
shm_open (SharedMemoryName.c_str (), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
@@ -82,8 +93,9 @@ Error SubprocessMemory::addMemoryDefinition(
82
93
83
94
Expected<int > SubprocessMemory::setupAuxiliaryMemoryInSubprocess (
84
95
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);
87
99
int AuxiliaryMemoryFileDescriptor =
88
100
shm_open (AuxiliaryMemoryName.c_str (), O_RDWR, S_IRUSR | S_IWUSR);
89
101
if (AuxiliaryMemoryFileDescriptor == -1 )
@@ -97,8 +109,8 @@ Expected<int> SubprocessMemory::setupAuxiliaryMemoryInSubprocess(
97
109
return make_error<Failure>(" Mapping auxiliary memory failed" );
98
110
AuxiliaryMemoryMapping[0 ] = CounterFileDescriptor;
99
111
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 );
102
114
AuxiliaryMemoryMapping[AuxiliaryMemoryOffset + MemVal.Index ] =
103
115
shm_open (MemoryValueName.c_str (), O_RDWR, S_IRUSR | S_IWUSR);
104
116
if (AuxiliaryMemoryMapping[AuxiliaryMemoryOffset + MemVal.Index ] == -1 )
0 commit comments