Skip to content

[llvm-exegesis] Make SubprocessMemoryTest use PIDs #65245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions llvm/unittests/tools/llvm-exegesis/X86/SubprocessMemoryTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,47 @@

#include "X86/TestBase.h"
#include "gtest/gtest.h"
#include <string>
#include <unordered_map>

#ifdef __linux__
#include <endian.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif // __linux__

// This needs to be updated anytime a test is added or removed from the test
// suite.
static constexpr const size_t TestCount = 4;

namespace llvm {
namespace exegesis {

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

class SubprocessMemoryTest : public X86TestBase {
protected:
int getSharedMemoryNumber(const unsigned TestNumber) {
// Do a process similar to 2D array indexing so that each process gets it's
// own shared memory space to avoid collisions. This will not overflow as
// the maximum value a PID can take on is 10^22.
return getpid() * TestCount + TestNumber;
}

void
testCommon(std::unordered_map<std::string, MemoryValue> MemoryDefinitions,
const int MainProcessPID) {
EXPECT_FALSE(SM.initializeSubprocessMemory(MainProcessPID));
EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions, MainProcessPID));
const unsigned TestNumber) {
EXPECT_FALSE(
SM.initializeSubprocessMemory(getSharedMemoryNumber(TestNumber)));
EXPECT_FALSE(SM.addMemoryDefinition(MemoryDefinitions,
getSharedMemoryNumber(TestNumber)));
}

std::string getSharedMemoryName(const unsigned TestNumber,
const unsigned DefinitionNumber) {
return "/" + std::to_string(getSharedMemoryNumber(TestNumber)) + "memdef" +
std::to_string(DefinitionNumber);
}

void checkSharedMemoryDefinition(const std::string &DefinitionName,
Expand Down Expand Up @@ -59,7 +80,7 @@ TEST_F(SubprocessMemoryTest, DISABLED_OneDefinition) {
TEST_F(SubprocessMemoryTest, OneDefinition) {
#endif
testCommon({{"test1", {APInt(8, 0xff), 4096, 0}}}, 0);
checkSharedMemoryDefinition("/0memdef0", 4096, {0xff});
checkSharedMemoryDefinition(getSharedMemoryName(0, 0), 4096, {0xff});
}

#if defined(__powerpc__) || defined(__s390x__)
Expand All @@ -71,9 +92,9 @@ TEST_F(SubprocessMemoryTest, MultipleDefinitions) {
{"test2", {APInt(8, 0xbb), 4096, 1}},
{"test3", {APInt(8, 0xcc), 4096, 2}}},
1);
checkSharedMemoryDefinition("/1memdef0", 4096, {0xaa});
checkSharedMemoryDefinition("/1memdef1", 4096, {0xbb});
checkSharedMemoryDefinition("/1memdef2", 4096, {0xcc});
checkSharedMemoryDefinition(getSharedMemoryName(1, 0), 4096, {0xaa});
checkSharedMemoryDefinition(getSharedMemoryName(1, 1), 4096, {0xbb});
checkSharedMemoryDefinition(getSharedMemoryName(1, 2), 4096, {0xcc});
}

#if defined(__powerpc__) || defined(__s390x__)
Expand All @@ -88,9 +109,9 @@ TEST_F(SubprocessMemoryTest, DefinitionFillsCompletely) {
std::vector<uint8_t> Test1Expected(512, 0xaa);
std::vector<uint8_t> Test2Expected(512, 0xbb);
std::vector<uint8_t> Test3Expected(512, 0xcc);
checkSharedMemoryDefinition("/2memdef0", 4096, Test1Expected);
checkSharedMemoryDefinition("/2memdef1", 4096, Test2Expected);
checkSharedMemoryDefinition("/2memdef2", 4096, Test3Expected);
checkSharedMemoryDefinition(getSharedMemoryName(2, 0), 4096, Test1Expected);
checkSharedMemoryDefinition(getSharedMemoryName(2, 1), 4096, Test2Expected);
checkSharedMemoryDefinition(getSharedMemoryName(2, 2), 4096, Test3Expected);
}

// The following test is only supported on little endian systems.
Expand Down Expand Up @@ -123,7 +144,7 @@ TEST_F(SubprocessMemoryTest, DefinitionEndTruncation) {
Test1Expected[I] = 0xaa;
}
}
checkSharedMemoryDefinition("/3memdef0", 4096, Test1Expected);
checkSharedMemoryDefinition(getSharedMemoryName(3, 0), 4096, Test1Expected);
}

#endif // defined(__linux__) && !defined(__ANDROID__)
Expand Down