Retry mmap with a smaller size.

Change-Id: If8ea046af789394c1f58be9cc3a2b8100cee214a
This commit is contained in:
Piotr Fusik
2019-01-14 10:55:22 +01:00
committed by sys_ocldev
parent ad3bfd84cb
commit 40870f9c7d
5 changed files with 63 additions and 104 deletions

View File

@@ -1,11 +1,12 @@
/*
* Copyright (C) 2017-2018 Intel Corporation
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include <limits>
#include "runtime/os_interface/32bit_memory.h"
#include "runtime/os_interface/linux/drm_32bit_memory.cpp"
#include "runtime/helpers/aligned_memory.h"
@@ -16,6 +17,7 @@ static bool failMmap = false;
static bool fail32BitMmap = false;
static bool failUpperRange = false;
static bool failLowerRanger = false;
static size_t maxMmapLength = std::numeric_limits<size_t>::max();
static uintptr_t startUpperHeap = maxMmap32BitAddress;
static uintptr_t lowerRangeHeapStart = lowerRangeStart;
@@ -33,8 +35,9 @@ void *MockMmap(void *addr, size_t length, int prot, int flags,
bool returnLowerRange = false;
mmapCallCount++;
if (failMmap)
if (failMmap || length > maxMmapLength) {
return MAP_FAILED;
}
if (mmapFailCount > 0) {
mmapFailCount--;
@@ -83,20 +86,20 @@ int MockMunmap(void *addr, size_t length) noexcept {
return 0;
}
class mockAllocator32Bit : public Allocator32bit {
class MockAllocator32Bit : public Allocator32bit {
public:
class OsInternalsPublic : public Allocator32bit::OsInternals {
};
mockAllocator32Bit(Allocator32bit::OsInternals *osInternalsIn) : Allocator32bit(osInternalsIn) {
MockAllocator32Bit(Allocator32bit::OsInternals *osInternalsIn) : Allocator32bit(osInternalsIn) {
}
mockAllocator32Bit() {
MockAllocator32Bit() {
this->osInternals->mmapFunction = MockMmap;
this->osInternals->munmapFunction = MockMunmap;
resetState();
}
~mockAllocator32Bit() {
~MockAllocator32Bit() {
resetState();
}
static void resetState() {
@@ -104,6 +107,7 @@ class mockAllocator32Bit : public Allocator32bit {
failUpperRange = false;
failLowerRanger = false;
failMmap = false;
maxMmapLength = std::numeric_limits<size_t>::max();
startUpperHeap = maxMmap32BitAddress;
lowerRangeHeapStart = lowerRangeStart;
offsetIn32BitRange = 0u;
@@ -116,6 +120,6 @@ class mockAllocator32Bit : public Allocator32bit {
return new OsInternalsPublic;
}
OsInternals *getosInternal() { return this->osInternals.get(); }
OsInternals *getOsInternals() const { return this->osInternals.get(); }
};
} // namespace OCLRT

View File

@@ -2235,45 +2235,42 @@ TEST(Allocator32BitUsingHeapAllocator, given32BitAllocatorWhenMMapFailsThenNullp
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(true);
mockAllocator32Bit::resetState();
MockAllocator32Bit::resetState();
failMmap = true;
mockAllocator32Bit::OsInternalsPublic *osInternals = mockAllocator32Bit::createOsInternals();
MockAllocator32Bit::OsInternalsPublic *osInternals = MockAllocator32Bit::createOsInternals();
osInternals->mmapFunction = MockMmap;
osInternals->munmapFunction = MockMunmap;
mockAllocator32Bit *mock32BitAllocator = new mockAllocator32Bit(osInternals);
MockAllocator32Bit mock32BitAllocator{osInternals};
size_t size = 100u;
auto ptr = mock32BitAllocator->allocate(size);
auto ptr = mock32BitAllocator.allocate(size);
EXPECT_EQ(0llu, ptr);
EXPECT_EQ(2u, mmapCallCount);
delete mock32BitAllocator;
}
TEST(Allocator32BitUsingHeapAllocator, given32BitAllocatorWhenFirstMMapFailsThenSecondIsCalledWithSmallerSize) {
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(true);
mockAllocator32Bit::resetState();
mmapFailCount = 1u;
mockAllocator32Bit::OsInternalsPublic *osInternals = mockAllocator32Bit::createOsInternals();
MockAllocator32Bit::resetState();
maxMmapLength = getSizeToMap() - 1;
MockAllocator32Bit::OsInternalsPublic *osInternals = MockAllocator32Bit::createOsInternals();
osInternals->mmapFunction = MockMmap;
osInternals->munmapFunction = MockMunmap;
mockAllocator32Bit *mock32BitAllocator = new mockAllocator32Bit(osInternals);
MockAllocator32Bit mock32BitAllocator{osInternals};
size_t size = 100u;
auto ptr = mock32BitAllocator->allocate(size);
auto ptr = mock32BitAllocator.allocate(size);
EXPECT_NE(0llu, ptr);
EXPECT_EQ(2u, mmapCallCount);
EXPECT_NE(nullptr, osInternals->heapBasePtr);
EXPECT_NE(0u, osInternals->heapSize);
delete mock32BitAllocator;
}
TEST(DrmAllocator32Bit, allocateReturnsPointer) {
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
size_t size = 100u;
auto ptr = mock32BitAllocator.allocate(size);
EXPECT_NE(0u, (uintptr_t)ptr);
@@ -2285,7 +2282,7 @@ TEST(DrmAllocator32Bit, freeMapFailedPointer) {
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
size_t size = 100u;
int result = mock32BitAllocator.free(reinterpret_cast<uint64_t>(MAP_FAILED), size);
EXPECT_EQ(0, result);
@@ -2295,7 +2292,7 @@ TEST(DrmAllocator32Bit, freeNullPtrPointer) {
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
uint32_t size = 100u;
int result = mock32BitAllocator.free(0llu, size);
EXPECT_EQ(0, result);
@@ -2305,7 +2302,7 @@ TEST(DrmAllocator32Bit, freeLowerRangeAfterTwoMmapFails) {
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
mmapFailCount = 2;
size_t size = 100u;
auto ptr = mock32BitAllocator.allocate(size);
@@ -2318,7 +2315,7 @@ TEST(DrmAllocator32Bit, given32BitAllocatorWhenMMapFailsThenUpperHeapIsBrowsedFo
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
fail32BitMmap = true;
size_t size = 100u;
auto ptr = mock32BitAllocator.allocate(size);
@@ -2330,7 +2327,7 @@ TEST(DrmAllocator32Bit, given32BitAllocatorWith32AndUpperHeapsExhaustedThenPoint
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
fail32BitMmap = true;
failUpperRange = true;
size_t size = 100u;
@@ -2343,7 +2340,7 @@ TEST(DrmAllocator32Bit, given32bitRegionExhaustedWhenTwoAllocationsAreCreatedThe
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
fail32BitMmap = true;
size_t size = 100u;
auto ptr = (uintptr_t)mock32BitAllocator.allocate(size);
@@ -2356,7 +2353,7 @@ TEST(DrmAllocator32Bit, given32bitRegionExhaustedWhenTwoAllocationsAreCreatedThe
EXPECT_EQ(4u, mmapCallCount);
mock32BitAllocator.free(ptr2, size);
auto getInternals = mock32BitAllocator.getosInternal();
auto getInternals = mock32BitAllocator.getOsInternals();
EXPECT_EQ(ptr2, getInternals->upperRangeAddress);
mock32BitAllocator.free(ptr, size);
@@ -2369,7 +2366,7 @@ TEST(DrmAllocator32Bit, given32bitRegionAndUpperRegionExhaustedWhenTwoAllocation
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
fail32BitMmap = true;
failUpperRange = true;
size_t size = 100u;
@@ -2383,7 +2380,7 @@ TEST(DrmAllocator32Bit, given32bitRegionAndUpperRegionExhaustedWhenTwoAllocation
EXPECT_EQ(6u, mmapCallCount);
mock32BitAllocator.free(ptr2, size);
auto getInternals = mock32BitAllocator.getosInternal();
auto getInternals = mock32BitAllocator.getOsInternals();
EXPECT_EQ(ptr2, getInternals->lowerRangeAddress);
mock32BitAllocator.free(ptr, size);
@@ -2396,7 +2393,7 @@ TEST(DrmAllocator32Bit, given32bitAllocatorWithAllHeapsExhaustedWhenAskedForAllo
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
fail32BitMmap = true;
failLowerRanger = true;
failUpperRange = true;
@@ -2414,7 +2411,7 @@ TEST(DrmAllocator32Bit, given32bitAllocatorWithUpperHeapCloseToFullWhenAskedForA
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
fail32BitMmap = true;
size_t size = 3 * 1024 * 1024 * 1029u;
@@ -2429,7 +2426,7 @@ TEST(DrmAllocator32Bit, givenMapFailedAsInputToFreeFunctionWhenItIsCalledThenUnm
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
mock32BitAllocator.free(reinterpret_cast<uint64_t>(MAP_FAILED), 100u);
EXPECT_EQ(0u, unmapCallCount);
}
@@ -2438,7 +2435,7 @@ TEST(DrmAllocator32Bit, givenNullptrAsInputToFreeFunctionWhenItIsCalledThenUnmap
DebugManagerStateRestore restore;
DebugManager.flags.UseNewHeapAllocator.set(false);
mockAllocator32Bit mock32BitAllocator;
MockAllocator32Bit mock32BitAllocator;
mock32BitAllocator.free(0llu, 100u);
EXPECT_EQ(0u, unmapCallCount);
}