1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include <sys/mman.h> 18 #include <memory> 19 20 #include <gtest/gtest.h> 21 22 #include <ion/ion.h> 23 #include "ion_test_fixture.h" 24 25 class Allocate : public IonTest {}; 26 27 TEST_F(Allocate, Allocate) { 28 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024}; 29 for (const auto& heap : ion_heaps) { 30 for (size_t size : allocationSizes) { 31 SCOPED_TRACE(::testing::Message() 32 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id); 33 SCOPED_TRACE(::testing::Message() << "size " << size); 34 int fd; 35 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &fd)); 36 ASSERT_TRUE(fd != 0); 37 ASSERT_EQ(close(fd), 0); // free the buffer 38 } 39 } 40 } 41 42 TEST_F(Allocate, AllocateCached) { 43 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024}; 44 for (const auto& heap : ion_heaps) { 45 for (size_t size : allocationSizes) { 46 SCOPED_TRACE(::testing::Message() 47 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id); 48 SCOPED_TRACE(::testing::Message() << "size " << size); 49 int fd; 50 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), ION_FLAG_CACHED, &fd)); 51 ASSERT_TRUE(fd != 0); 52 ASSERT_EQ(close(fd), 0); // free the buffer 53 } 54 } 55 } 56 57 TEST_F(Allocate, AllocateCachedNeedsSync) { 58 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024}; 59 for (const auto& heap : ion_heaps) { 60 for (size_t size : allocationSizes) { 61 SCOPED_TRACE(::testing::Message() 62 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id); 63 SCOPED_TRACE(::testing::Message() << "size " << size); 64 int fd; 65 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 66 ION_FLAG_CACHED_NEEDS_SYNC, &fd)); 67 ASSERT_TRUE(fd != 0); 68 ASSERT_EQ(close(fd), 0); // free the buffer 69 } 70 } 71 } 72 73 TEST_F(Allocate, RepeatedAllocate) { 74 static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024}; 75 for (const auto& heap : ion_heaps) { 76 for (size_t size : allocationSizes) { 77 SCOPED_TRACE(::testing::Message() 78 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id); 79 SCOPED_TRACE(::testing::Message() << "size " << size); 80 int fd; 81 82 for (unsigned int i = 0; i < 1024; i++) { 83 SCOPED_TRACE(::testing::Message() << "iteration " << i); 84 ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &fd)); 85 ASSERT_TRUE(fd != 0); 86 ASSERT_EQ(close(fd), 0); // free the buffer 87 } 88 } 89 } 90 } 91 92 TEST_F(Allocate, Large) { 93 for (const auto& heap : ion_heaps) { 94 SCOPED_TRACE(::testing::Message() 95 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id); 96 int fd; 97 ASSERT_EQ(-ENOMEM, 98 ion_alloc_fd(ionfd, 3UL * 1024 * 1024 * 1024, 0, (1 << heap.heap_id), 0, &fd)); 99 } 100 } 101 102 // Make sure all heaps always return zeroed pages 103 TEST_F(Allocate, Zeroed) { 104 auto zeroes_ptr = std::make_unique<char[]>(4096); 105 106 for (const auto& heap : ion_heaps) { 107 SCOPED_TRACE(::testing::Message() 108 << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id); 109 int fds[16]; 110 for (unsigned int i = 0; i < 16; i++) { 111 int map_fd = -1; 112 113 ASSERT_EQ(0, ion_alloc_fd(ionfd, 4096, 0, (1 << heap.heap_id), 0, &map_fd)); 114 ASSERT_GE(map_fd, 0); 115 116 void* ptr = NULL; 117 ptr = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED, map_fd, 0); 118 ASSERT_TRUE(ptr != NULL); 119 120 memset(ptr, 0xaa, 4096); 121 122 ASSERT_EQ(0, munmap(ptr, 4096)); 123 fds[i] = map_fd; 124 } 125 126 for (unsigned int i = 0; i < 16; i++) { 127 ASSERT_EQ(0, close(fds[i])); 128 } 129 130 int new_ionfd = ion_open(); 131 int map_fd = -1; 132 133 ASSERT_EQ(0, ion_alloc_fd(new_ionfd, 4096, 0, (1 << heap.heap_id), 0, &map_fd)); 134 ASSERT_GE(map_fd, 0); 135 136 void* ptr = NULL; 137 ptr = mmap(NULL, 4096, PROT_READ, MAP_SHARED, map_fd, 0); 138 ASSERT_TRUE(ptr != NULL); 139 140 ASSERT_EQ(0, memcmp(ptr, zeroes_ptr.get(), 4096)); 141 142 ASSERT_EQ(0, munmap(ptr, 4096)); 143 ASSERT_EQ(0, close(map_fd)); 144 ASSERT_EQ(0, ion_close(new_ionfd)); 145 } 146 } 147